Browse Source

Add if not exists for tmp table when using importer (#3256)

Internal Jira: CDPD-53198
Mahesh Balakrishnan 2 years ago
parent
commit
d6f7b058d7

+ 6 - 6
desktop/libs/indexer/src/indexer/indexers/sql_tests.py

@@ -80,7 +80,7 @@ class TestSQLIndexer(object):
     assert_equal(
     assert_equal(
       [statement.strip() for statement in u'''DROP TABLE IF EXISTS `default`.`hue__tmp_export_table`;
       [statement.strip() for statement in u'''DROP TABLE IF EXISTS `default`.`hue__tmp_export_table`;
 
 
-CREATE TABLE `default`.`hue__tmp_export_table`
+CREATE TABLE IF NOT EXISTS `default`.`hue__tmp_export_table`
 (
 (
   `id` int ) COMMENT "No comment!"
   `id` int ) COMMENT "No comment!"
 PARTITIONED BY (
 PARTITIONED BY (
@@ -312,7 +312,7 @@ def test_generate_create_kudu_table_with_data():
 
 
     assert_true('''DROP TABLE IF EXISTS `default`.`hue__tmp_index_data`;''' in sql, sql)
     assert_true('''DROP TABLE IF EXISTS `default`.`hue__tmp_index_data`;''' in sql, sql)
 
 
-    statement = '''CREATE EXTERNAL TABLE `default`.`hue__tmp_index_data`
+    statement = '''CREATE EXTERNAL TABLE IF NOT EXISTS `default`.`hue__tmp_index_data`
 (
 (
   `business_id` string ,
   `business_id` string ,
   `cool` bigint ,
   `cool` bigint ,
@@ -419,7 +419,7 @@ def test_generate_create_parquet_table():
 
 
   assert_true('''USE default;''' in sql, sql)
   assert_true('''USE default;''' in sql, sql)
 
 
-  statement = '''CREATE EXTERNAL TABLE `default`.`hue__tmp_parquet_table`
+  statement = '''CREATE EXTERNAL TABLE IF NOT EXISTS `default`.`hue__tmp_parquet_table`
 (
 (
   `acct_client` string ,
   `acct_client` string ,
   `tran_amount` double ,
   `tran_amount` double ,
@@ -512,7 +512,7 @@ def test_generate_create_iceberg_table():
   print(sql)
   print(sql)
   assert_true('''USE default;''' in sql, sql)
   assert_true('''USE default;''' in sql, sql)
 
 
-  statement = '''CREATE EXTERNAL TABLE `default`.`hue__tmp_parquet_table`
+  statement = '''CREATE EXTERNAL TABLE IF NOT EXISTS `default`.`hue__tmp_parquet_table`
 (
 (
   `acct_client` string ,
   `acct_client` string ,
   `tran_amount` double ,
   `tran_amount` double ,
@@ -605,7 +605,7 @@ def test_generate_create_orc_table_transactional():
 
 
   assert_true('''USE default;''' in sql, sql)
   assert_true('''USE default;''' in sql, sql)
 
 
-  statement = '''CREATE EXTERNAL TABLE `default`.`hue__tmp_parquet_table`
+  statement = '''CREATE EXTERNAL TABLE IF NOT EXISTS `default`.`hue__tmp_parquet_table`
 (
 (
   `acct_client` string ,
   `acct_client` string ,
   `tran_amount` double ,
   `tran_amount` double ,
@@ -829,7 +829,7 @@ def test_create_ddl_with_nonascii():
 
 
   assert_true('''USE default;''' in sql, sql)
   assert_true('''USE default;''' in sql, sql)
 
 
-  statement = '''CREATE TABLE `default`.`hue__tmp_renamed_chinese_cities_gb2312`
+  statement = '''CREATE TABLE IF NOT EXISTS `default`.`hue__tmp_renamed_chinese_cities_gb2312`
 (
 (
   `Before` string ,
   `Before` string ,
   `old_Chinese_name` string ,
   `old_Chinese_name` string ,

+ 5 - 1
desktop/libs/indexer/src/indexer/templates/gen/create_table_statement.mako

@@ -84,7 +84,11 @@ CREATE \
 % if table.get("external", False):
 % if table.get("external", False):
 EXTERNAL \
 EXTERNAL \
 % endif
 % endif
-TABLE ${ '`%s`.`%s`' % (database, table["name"]) | n }
+TABLE \
+% if 'hue__tmp_' in table["name"]:
+IF NOT EXISTS \
+% endif
+${ '`%s`.`%s`' % (database, table["name"]) | n }
 ${ column_list(table, columns) | n } \
 ${ column_list(table, columns) | n } \
 % if kudu_partition_columns  and table.get('file_format') == 'kudu':
 % if kudu_partition_columns  and table.get('file_format') == 'kudu':
 PARTITION BY ${ ', '.join([kudu_partition(partition) for partition in kudu_partition_columns]) | n }
 PARTITION BY ${ ', '.join([kudu_partition(partition) for partition in kudu_partition_columns]) | n }