@@ -520,22 +520,31 @@ def _make_add_column_sql(
520520 """Make one or more ADD COLUMN sql clauses to be joined in ALTER TABLE statement(s)"""
521521 return [f"ADD COLUMN { self ._get_column_def_sql (c , table_format )} " for c in new_columns ]
522522
523+ def _make_create_table (self , qualified_name : str , table : TTableSchema ) -> str :
524+ not_exists_clause = " "
525+ if (
526+ table ["name" ] in self .schema .dlt_table_names ()
527+ and self .capabilities .create_table_not_exists
528+ ):
529+ not_exists_clause = " IF NOT EXISTS "
530+ return f"CREATE TABLE{ not_exists_clause } { qualified_name } "
531+
523532 def _get_table_update_sql (
524533 self , table_name : str , new_columns : Sequence [TColumnSchema ], generate_alter : bool
525534 ) -> List [str ]:
526535 # build sql
527- canonical_name = self .sql_client .make_qualified_table_name (table_name )
536+ qualified_name = self .sql_client .make_qualified_table_name (table_name )
528537 table = self .prepare_load_table (table_name )
529538 table_format = table .get ("table_format" )
530539 sql_result : List [str ] = []
531540 if not generate_alter :
532541 # build CREATE
533- sql = f"CREATE TABLE { canonical_name } (\n "
542+ sql = self . _make_create_table ( qualified_name , table ) + " (\n "
534543 sql += ",\n " .join ([self ._get_column_def_sql (c , table_format ) for c in new_columns ])
535544 sql += ")"
536545 sql_result .append (sql )
537546 else :
538- sql_base = f"ALTER TABLE { canonical_name } \n "
547+ sql_base = f"ALTER TABLE { qualified_name } \n "
539548 add_column_statements = self ._make_add_column_sql (new_columns , table_format )
540549 if self .capabilities .alter_add_multi_column :
541550 column_sql = ",\n "
@@ -559,13 +568,13 @@ def _get_table_update_sql(
559568 if hint == "not_null" :
560569 logger .warning (
561570 f"Column(s) { hint_columns } with NOT NULL are being added to existing"
562- f" table { canonical_name } . If there's data in the table the operation"
571+ f" table { qualified_name } . If there's data in the table the operation"
563572 " will fail."
564573 )
565574 else :
566575 logger .warning (
567576 f"Column(s) { hint_columns } with hint { hint } are being added to existing"
568- f" table { canonical_name } . Several hint types may not be added to"
577+ f" table { qualified_name } . Several hint types may not be added to"
569578 " existing tables."
570579 )
571580 return sql_result
0 commit comments