@@ -279,6 +279,8 @@ impl TableVersion {
279
279
impl TableCatalog {
280
280
/// Returns the SQL definition when the table was created, purified with best effort
281
281
/// if it's a table.
282
+ ///
283
+ /// See [`Self::create_sql_ast_purified`] for more details.
282
284
pub fn create_sql_purified ( & self ) -> String {
283
285
self . create_sql_ast_purified ( )
284
286
. map ( |stmt| stmt. to_string ( ) )
@@ -297,7 +299,7 @@ impl TableCatalog {
297
299
let name = ast:: ObjectName ( vec ! [ self . name. as_str( ) . into( ) ] ) ;
298
300
ast:: Statement :: default_create_table ( name)
299
301
} else {
300
- self . create_sql_ast ( ) ?
302
+ self . create_sql_ast_raw ( ) ?
301
303
} ;
302
304
303
305
match try_purify_table_source_create_sql_ast (
@@ -316,7 +318,7 @@ impl TableCatalog {
316
318
}
317
319
}
318
320
319
- self . create_sql_ast ( )
321
+ self . create_sql_ast_raw ( )
320
322
}
321
323
}
322
324
@@ -481,14 +483,33 @@ impl TableCatalog {
481
483
}
482
484
483
485
/// Returns the SQL definition when the table was created.
486
+ ///
487
+ /// See [`Self::create_sql_ast`] for more details.
484
488
pub fn create_sql ( & self ) -> String {
485
- self . definition . clone ( )
489
+ self . create_sql_ast ( )
490
+ . map ( |stmt| stmt. to_string ( ) )
491
+ . unwrap_or_else ( |_| self . definition . clone ( ) )
486
492
}
487
493
488
494
/// Returns the parsed SQL definition when the table was created.
489
495
///
496
+ /// Re-create the table with this statement may have different schema if the schema is derived
497
+ /// from external systems (like schema registry) or it's created by `CREATE TABLE AS`. If this
498
+ /// is not desired, use [`Self::create_sql_ast_purified`] instead.
499
+ ///
490
500
/// Returns error if it's invalid.
491
501
pub fn create_sql_ast ( & self ) -> Result < ast:: Statement > {
502
+ if let TableType :: Table = self . table_type ( )
503
+ && self . definition . is_empty ( )
504
+ {
505
+ // Fix `CREATE TABLE AS`.
506
+ self . create_sql_ast_purified ( )
507
+ } else {
508
+ self . create_sql_ast_raw ( )
509
+ }
510
+ }
511
+
512
+ fn create_sql_ast_raw ( & self ) -> Result < ast:: Statement > {
492
513
Ok ( Parser :: parse_sql ( & self . definition )
493
514
. context ( "unable to parse definition sql" ) ?
494
515
. into_iter ( )
0 commit comments