Skip to content

Commit

Permalink
fix: create as query respect ddl_column_type_nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
TCeason committed Dec 18, 2024
1 parent 41e51e5 commit 50d3efe
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 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;

0 comments on commit 50d3efe

Please sign in to comment.