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; diff --git a/tests/sqllogictests/suites/base/05_ddl/05_0028_ddl_alter_table_add_drop_column.test b/tests/sqllogictests/suites/base/05_ddl/05_0028_ddl_alter_table_add_drop_column.test index d4b168d49dcd..c875ebeb8f5f 100644 --- a/tests/sqllogictests/suites/base/05_ddl/05_0028_ddl_alter_table_add_drop_column.test +++ b/tests/sqllogictests/suites/base/05_ddl/05_0028_ddl_alter_table_add_drop_column.test @@ -255,7 +255,7 @@ ALTER TABLE `05_0028_at_t0_4` MODIFY COLUMN d uint64 COMMENT 'middle' query TT SHOW CREATE TABLE `05_0028_at_t0_4` ---- -05_0028_at_t0_4 CREATE TABLE "05_0028_at_t0_4" ( a FLOAT NOT NULL, d BIGINT UNSIGNED NULL COMMENT 'middle', e INT NULL COMMENT 'end' ) ENGINE=FUSE +05_0028_at_t0_4 CREATE TABLE "05_0028_at_t0_4" ( a FLOAT NULL, d BIGINT UNSIGNED NULL COMMENT 'middle', e INT NULL COMMENT 'end' ) ENGINE=FUSE query IIT SELECT * FROM `05_0028_at_t0_4` order by a diff --git a/tests/sqllogictests/suites/mode/standalone/explain/explain.test b/tests/sqllogictests/suites/mode/standalone/explain/explain.test index 940f72d817bf..28971cd47a0f 100644 --- a/tests/sqllogictests/suites/mode/standalone/explain/explain.test +++ b/tests/sqllogictests/suites/mode/standalone/explain/explain.test @@ -5,10 +5,10 @@ statement ok drop table if exists t2 all statement ok -create OR REPLACE table t1 as select number as a, number as b from numbers(1) +settings (ddl_column_type_nullable=0) create OR REPLACE table t1 as select number as a, number as b from numbers(1) statement ok -create OR REPLACE table t2 as select number as a, number as b from numbers(5) +settings (ddl_column_type_nullable=0) create OR REPLACE table t2 as select number as a, number as b from numbers(5) statement error 1005 explain explain select t1.a from t1 where a > 0 @@ -584,7 +584,7 @@ statement ok drop table if exists t3 statement ok -create OR REPLACE table t3 as select number as a, number as b from numbers(10) +settings (ddl_column_type_nullable=0) create OR REPLACE table t3 as select number as a, number as b from numbers(10) query T explain select * from t1,t2, t3 where (t1.a > 1 and t2.a > 2) or (t1.b < 3 and t2.b < 4) or t3.a = 2 diff --git a/tests/sqllogictests/suites/mode/standalone/explain/explain_analyze_partial.test b/tests/sqllogictests/suites/mode/standalone/explain/explain_analyze_partial.test index fa80fddaa6fe..b654e2eff726 100644 --- a/tests/sqllogictests/suites/mode/standalone/explain/explain_analyze_partial.test +++ b/tests/sqllogictests/suites/mode/standalone/explain/explain_analyze_partial.test @@ -1,3 +1,6 @@ +statement ok +set ddl_column_type_nullable=0; + statement ok create or replace table t2 as select number as b from numbers(10); @@ -110,3 +113,6 @@ drop table t1; statement ok drop table t2; + +statement ok +unset ddl_column_type_nullable; diff --git a/tests/sqllogictests/suites/mode/standalone/explain/join.test b/tests/sqllogictests/suites/mode/standalone/explain/join.test index d8d562aaca54..26ba975fbf99 100644 --- a/tests/sqllogictests/suites/mode/standalone/explain/join.test +++ b/tests/sqllogictests/suites/mode/standalone/explain/join.test @@ -8,13 +8,13 @@ statement ok drop table if exists t2 statement ok -create table t as select number from numbers(1) +settings (ddl_column_type_nullable=0) create table t as select number from numbers(1) statement ok -create table t1 as select number from numbers(10) +settings (ddl_column_type_nullable=0) create table t1 as select number from numbers(10) statement ok -create table t2 as select number from numbers(100) +settings (ddl_column_type_nullable=0) create table t2 as select number from numbers(100) query T explain select t.number from t, t1 where t.number = t1.number @@ -656,7 +656,7 @@ drop table t2 # https://github.com/datafuselabs/databend/issues/10523 statement ok -create table t1 as select * from numbers(1) +settings (ddl_column_type_nullable=0) create table t1 as select * from numbers(1) statement ok set enable_cbo = 0 diff --git a/tests/sqllogictests/suites/mode/standalone/explain/join_reorder/chain.test b/tests/sqllogictests/suites/mode/standalone/explain/join_reorder/chain.test index edae6aeedf97..f60abc9e5e64 100644 --- a/tests/sqllogictests/suites/mode/standalone/explain/join_reorder/chain.test +++ b/tests/sqllogictests/suites/mode/standalone/explain/join_reorder/chain.test @@ -1,3 +1,6 @@ +statement ok +set ddl_column_type_nullable=0; + statement ok drop database if exists join_reorder @@ -518,3 +521,6 @@ HashJoin: LEFT ANTI statement ok drop database join_reorder + +statement ok +unset ddl_column_type_nullable; diff --git a/tests/sqllogictests/suites/mode/standalone/explain/selectivity/modulo.test b/tests/sqllogictests/suites/mode/standalone/explain/selectivity/modulo.test index 1b65037bfe0b..8fc0eb31ab75 100644 --- a/tests/sqllogictests/suites/mode/standalone/explain/selectivity/modulo.test +++ b/tests/sqllogictests/suites/mode/standalone/explain/selectivity/modulo.test @@ -1,5 +1,5 @@ statement ok -create or replace table t1 as select * from numbers(1000); +settings (ddl_column_type_nullable=0) create or replace table t1 as select * from numbers(1000); # selectivity == 0.0 query T diff --git a/tests/sqllogictests/suites/mode/standalone/explain/selectivity/pr_16069.test b/tests/sqllogictests/suites/mode/standalone/explain/selectivity/pr_16069.test index fe5f103a4a97..eb6ca932993c 100644 --- a/tests/sqllogictests/suites/mode/standalone/explain/selectivity/pr_16069.test +++ b/tests/sqllogictests/suites/mode/standalone/explain/selectivity/pr_16069.test @@ -1,5 +1,5 @@ statement ok -create or replace table t as select * from numbers(100); +settings (ddl_column_type_nullable=0) create or replace table t as select * from numbers(100); statement ok analyze table t; diff --git a/tests/sqllogictests/suites/mode/standalone/explain/union.test b/tests/sqllogictests/suites/mode/standalone/explain/union.test index c1adc37d901a..76480538fb6a 100644 --- a/tests/sqllogictests/suites/mode/standalone/explain/union.test +++ b/tests/sqllogictests/suites/mode/standalone/explain/union.test @@ -225,10 +225,10 @@ statement ok drop table t2 statement ok -create table t1 as select number as a from numbers(10000); +settings (ddl_column_type_nullable=0) create table t1 as select number as a from numbers(10000); statement ok -create table t2 as select number as b from numbers(10000); +settings (ddl_column_type_nullable=0) create table t2 as select number as b from numbers(10000); query T explain select * from t1 where t1.a < 0 union all select a from t2 join t1 on t1.a = t2.b where a <0; diff --git a/tests/sqllogictests/suites/mode/standalone/explain_native/explain.test b/tests/sqllogictests/suites/mode/standalone/explain_native/explain.test index 87762e3ab2da..2d3bf614e96b 100644 --- a/tests/sqllogictests/suites/mode/standalone/explain_native/explain.test +++ b/tests/sqllogictests/suites/mode/standalone/explain_native/explain.test @@ -5,10 +5,10 @@ statement ok drop table if exists t2 all statement ok -create table t1 as select number as a, number as b from numbers(1) +settings (ddl_column_type_nullable=0) create table t1 as select number as a, number as b from numbers(1) statement ok -create table t2 as select number as a, number as b from numbers(5) +settings (ddl_column_type_nullable=0) create table t2 as select number as a, number as b from numbers(5) query T explain select t1.a from t1 where a > 0 @@ -548,7 +548,7 @@ statement ok drop table if exists t3 statement ok -create table t3 as select number as a, number as b from numbers(10) +settings (ddl_column_type_nullable=0) create table t3 as select number as a, number as b from numbers(10) query T explain select * from t1,t2, t3 where (t1.a > 1 and t2.a > 2) or (t1.b < 3 and t2.b < 4) or t3.a = 2 diff --git a/tests/sqllogictests/suites/mode/standalone/explain_native/fold_count.test b/tests/sqllogictests/suites/mode/standalone/explain_native/fold_count.test index a772a1c9cb19..862e13d5fc4f 100644 --- a/tests/sqllogictests/suites/mode/standalone/explain_native/fold_count.test +++ b/tests/sqllogictests/suites/mode/standalone/explain_native/fold_count.test @@ -2,7 +2,7 @@ statement ok drop table if exists t statement ok -create table t as select * from numbers(1000) +settings (ddl_column_type_nullable=0) create table t as select * from numbers(1000) query T explain select count(*) from t diff --git a/tests/sqllogictests/suites/mode/standalone/explain_native/join.test b/tests/sqllogictests/suites/mode/standalone/explain_native/join.test index a7642bf884ae..45f8c55cad77 100644 --- a/tests/sqllogictests/suites/mode/standalone/explain_native/join.test +++ b/tests/sqllogictests/suites/mode/standalone/explain_native/join.test @@ -8,13 +8,13 @@ statement ok drop table if exists t2 statement ok -create table t as select number from numbers(1) +settings (ddl_column_type_nullable=0) create table t as select number from numbers(1) statement ok -create table t1 as select number from numbers(10) +settings (ddl_column_type_nullable=0) create table t1 as select number from numbers(10) statement ok -create table t2 as select number from numbers(100) +settings (ddl_column_type_nullable=0) create table t2 as select number from numbers(100) query T explain select t.number from t, t1 where t.number = t1.number @@ -574,7 +574,7 @@ set enable_cbo = 0 # https://github.com/datafuselabs/databend/issues/10523 statement ok -create table t1 as select * from numbers(1) +settings (ddl_column_type_nullable=0) create table t1 as select * from numbers(1) query I select t1.number FROM diff --git a/tests/sqllogictests/suites/mode/standalone/explain_native/join_reorder/chain.test b/tests/sqllogictests/suites/mode/standalone/explain_native/join_reorder/chain.test index edae6aeedf97..dec160370195 100644 --- a/tests/sqllogictests/suites/mode/standalone/explain_native/join_reorder/chain.test +++ b/tests/sqllogictests/suites/mode/standalone/explain_native/join_reorder/chain.test @@ -11,19 +11,19 @@ statement ok drop table if exists t statement ok -create table t as select number as a from numbers(1) +settings (ddl_column_type_nullable=0) create table t as select number as a from numbers(1) statement ok drop table if exists t1 statement ok -create table t1 as select number as a from numbers(10) +settings (ddl_column_type_nullable=0) create table t1 as select number as a from numbers(10) statement ok drop table if exists t2 statement ok -create table t2 as select number as a from numbers(100) +settings (ddl_column_type_nullable=0) create table t2 as select number as a from numbers(100) query T explain select * from t, t1, t2 where t.a = t1.a and t1.a = t2.a