Skip to content

Commit

Permalink
fix: create as query respect ddl_column_type_nullable (#17071)
Browse files Browse the repository at this point in the history
* fix: create as query respect ddl_column_type_nullable

* fix test
  • Loading branch information
TCeason authored Dec 19, 2024
1 parent c2294d8 commit 5620b78
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 28 deletions.
20 changes: 15 additions & 5 deletions src/query/sql/src/planner/binder/ddl/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Result<Vec<_>>>()?;
Expand Down Expand Up @@ -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<TableDataType> {
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<TableDataType> {
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()))),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

Expand Down Expand Up @@ -110,3 +113,6 @@ drop table t1;

statement ok
drop table t2;

statement ok
unset ddl_column_type_nullable;
8 changes: 4 additions & 4 deletions tests/sqllogictests/suites/mode/standalone/explain/join.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
statement ok
set ddl_column_type_nullable=0;

statement ok
drop database if exists join_reorder

Expand Down Expand Up @@ -518,3 +521,6 @@ HashJoin: LEFT ANTI

statement ok
drop database join_reorder

statement ok
unset ddl_column_type_nullable;
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tests/sqllogictests/suites/mode/standalone/explain/union.test
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5620b78

Please sign in to comment.