diff --git a/src/query/sql/src/planner/binder/ddl/table.rs b/src/query/sql/src/planner/binder/ddl/table.rs index 3d1bc8a327d9..a11f08656a7b 100644 --- a/src/query/sql/src/planner/binder/ddl/table.rs +++ b/src/query/sql/src/planner/binder/ddl/table.rs @@ -537,7 +537,10 @@ impl Binder { .map(|column_binding| { Ok(TableField::new( &column_binding.column_name, - create_as_select_infer_schema_type(&column_binding.data_type)?, + create_as_select_infer_schema_type( + &column_binding.data_type, + self.is_column_not_null(), + )?, )) }) .collect::>>()?; @@ -1780,9 +1783,16 @@ async fn verify_external_location_privileges(dal: Operator) -> Result<()> { .expect("join must succeed") } -fn create_as_select_infer_schema_type(data_type: &DataType) -> Result { - match data_type { - DataType::Null => Ok(TableDataType::Nullable(Box::new(TableDataType::String))), - _ => infer_schema_type(data_type), +fn create_as_select_infer_schema_type( + data_type: &DataType, + not_null: bool, +) -> Result { + use DataType::*; + + match (data_type, not_null) { + (Null, _) => Ok(TableDataType::Nullable(Box::new(TableDataType::String))), + (dt, true) => infer_schema_type(dt), + (Nullable(_), false) => infer_schema_type(data_type), + (dt, false) => infer_schema_type(&Nullable(Box::new(dt.clone()))), } } diff --git a/tests/sqllogictests/suites/base/05_ddl/05_0000_ddl_create_tables.test b/tests/sqllogictests/suites/base/05_ddl/05_0000_ddl_create_tables.test index 01e23ba86fae..c3d9cd3649ed 100644 --- a/tests/sqllogictests/suites/base/05_ddl/05_0000_ddl_create_tables.test +++ b/tests/sqllogictests/suites/base/05_ddl/05_0000_ddl_create_tables.test @@ -596,6 +596,9 @@ alter table t_opt_retention set options(data_retention_period_in_hours = 2); #issue 16794 +statement ok +set ddl_column_type_nullable=0; + statement ok create or replace table b as select 123 as col1, null as col2; @@ -617,3 +620,31 @@ select * from b; statement ok drop table if exists b; + +statement ok +set ddl_column_type_nullable=1; + +statement ok +create or replace table b as select 123 as col1; + +query T +desc b; +---- +col1 TINYINT UNSIGNED YES NULL (empty) + +statement ok +set ddl_column_type_nullable=0; + +statement ok +create or replace table b as select 123 as col1; + +query T +desc b; +---- +col1 TINYINT UNSIGNED NO 0 (empty) + +statement ok +unset ddl_column_type_nullable; + +statement ok +drop table if exists b;