Apache DataFusion SQL Query Engine
infer column nullability on joins (#6060)
comphead560ad353619f
diff --git a/datafusion/core/tests/sqllogictests/test_files/join.slt b/datafusion/core/tests/sqllogictests/test_files/join.sltindex b940232a74..2f49c5b97c 100644--- a/datafusion/core/tests/sqllogictests/test_files/join.slt+++ b/datafusion/core/tests/sqllogictests/test_files/join.slt@@ -532,17 +532,40 @@ FROM t1# test create table from query with LEFT joinstatement ok-create table temp as+create table left_join_test aswith t1 as (select 1 as col1, 'asd' as col2),t2 as (select 1 as col3, 'sdf' as col4)select col2, col4 from t1 left join t2 on col1 = col3+# test create table from query with RIGHT join+statement ok+create table right_join_test as+with t1 as (select 1 as col1, 'asd' as col2),+ t2 as (select 1 as col3, 'sdf' as col4)+select col2, col4 from t1 right join t2 on col1 = col3++# test create table from query with FULL OUTER join+statement ok+create table full_join_test as+with t1 as (select 1 as col1, 'asd' as col2),+ t2 as (select 1 as col3, 'sdf' as col4)+select col2, col4 from t1 full outer join t2 on col1 = col3+statement okdrop table IF EXISTS t1;statement okdrop table IF EXISTS t2;+statement ok+drop table IF EXISTS left_join_test;++statement ok+drop table IF EXISTS right_join_test;++statement ok+drop table IF EXISTS full_join_test;+# batch sizestatement okset datafusion.execution.batch_size = 8192;diff --git a/datafusion/expr/src/logical_plan/builder.rs b/datafusion/expr/src/logical_plan/builder.rsindex ec2a27542c..d72f6b462c 100644--- a/datafusion/expr/src/logical_plan/builder.rs+++ b/datafusion/expr/src/logical_plan/builder.rs@@ -1041,11 +1041,18 @@ pub fn build_join_schema(right: &DFSchema,join_type: &JoinType,) -> Result<DFSchema> {+ fn nullify_fields(fields: &[DFField]) -> Vec<DFField> {+ fields+ .iter()+ .map(|f| f.clone().with_nullable(true))+ .collect()+ }+let right_fields = right.fields();let left_fields = left.fields();let fields: Vec<DFField> = match join_type {- JoinType::Inner | JoinType::Full | JoinType::Right => {+ JoinType::Inner => {// left then rightleft_fields.iter()@@ -1055,14 +1062,25 @@ pub fn build_join_schema(}JoinType::Left => {// left then right, right set to nullable in case of not matched scenario- let right_fields_nullable: Vec<DFField> = right_fields- .iter()- .map(|f| f.clone().with_nullable(true))- .collect();-left_fields.iter()- .chain(&right_fields_nullable)+ .chain(&nullify_fields(right_fields))+ .cloned()+ .collect()+ }+ JoinType::Right => {+ // left then right, left set to nullable in case of not matched scenario+ nullify_fields(left_fields)+ .iter()+ .chain(right_fields.iter())+ .cloned()+ .collect()+ }+ JoinType::Full => {+ // left then right, all set to nullable in case of not matched scenario+ nullify_fields(left_fields)+ .iter()+ .chain(&nullify_fields(right_fields)).cloned().collect()}