Skip to content

Commit 6b7659d

Browse files
committed
Fix but, update tests
1 parent 28f0c28 commit 6b7659d

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

datafusion/expr-common/src/type_coercion/binary.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -507,18 +507,19 @@ fn type_union_resolution_coercion(
507507
None
508508
}
509509

510-
let types = lhs
510+
let coerced_types = lhs
511511
.iter()
512512
.map(|lhs_field| search_corresponding_coerced_type(lhs_field, rhs))
513513
.collect::<Option<Vec<_>>>()?;
514514

515-
let fields = types
515+
// preserve the field name and nullability
516+
let orig_fields = std::iter::zip(lhs.iter(), rhs.iter());
517+
518+
let fields: Vec<FieldRef> = coerced_types
516519
.into_iter()
517-
.enumerate()
518-
.map(|(i, datatype)| {
519-
Arc::new(Field::new(format!("c{i}"), datatype, true))
520-
})
521-
.collect::<Vec<FieldRef>>();
520+
.zip(orig_fields)
521+
.map(|(datatype, (lhs, rhs))| coerce_fields(datatype, lhs, rhs))
522+
.collect();
522523
Some(DataType::Struct(fields.into()))
523524
}
524525
_ => {

datafusion/sqllogictest/test_files/case.slt

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ create or replace table t as values
432432
);
433433

434434
# This case forces all branches to be coerced to the same type
435-
query error
435+
query ?
436436
SELECT
437437
case
438438
when column1 > 0 then column2
@@ -441,13 +441,10 @@ SELECT
441441
end
442442
FROM t;
443443
----
444-
DataFusion error: type_coercion
445-
caused by
446-
Error during planning: Failed to coerce then ([List(Field { name: "item", data_type: Struct([Field { name: "foo", data_type: Utf8View, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), List(Field { name: "item", data_type: Struct([Field { name: "foo", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} })]) and else (Some(List(Field { name: "item", data_type: Struct([Field { name: "foo", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }))) to common types in CASE WHEN expression
447-
444+
[{foo: baz}]
448445

449446
# different orders of the branches
450-
query error
447+
query ?
451448
SELECT
452449
case
453450
when column1 > 0 then column3 -- NB different order
@@ -456,10 +453,7 @@ SELECT
456453
end
457454
FROM t;
458455
----
459-
DataFusion error: type_coercion
460-
caused by
461-
Error during planning: Failed to coerce then ([List(Field { name: "item", data_type: Struct([Field { name: "foo", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), List(Field { name: "item", data_type: Struct([Field { name: "foo", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} })]) and else (Some(List(Field { name: "item", data_type: Struct([Field { name: "foo", data_type: Utf8View, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }))) to common types in CASE WHEN expression
462-
456+
[{foo: bar}]
463457

464458
# different orders of the branches
465459
query ?
@@ -471,7 +465,7 @@ SELECT
471465
end
472466
FROM t;
473467
----
474-
[{c0: blarg}]
468+
[{foo: blarg}]
475469

476470

477471

0 commit comments

Comments
 (0)