Skip to content

Commit 67d05ca

Browse files
committed
feat[expr-common]: support REE in coalesce
We were missing a couple of branches to unwrap REE in type_union_resolution_coercion. Signed-off-by: Alfonso Subiotto Marques <alfonso.subiotto@polarsignals.com>
1 parent 3aefba7 commit 67d05ca

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ use std::sync::Arc;
2323

2424
use crate::operator::Operator;
2525

26-
use arrow::array::{Array, new_empty_array};
26+
use arrow::array::{new_empty_array, Array};
2727
use arrow::compute::can_cast_types;
2828
use arrow::datatypes::IntervalUnit::MonthDayNano;
2929
use arrow::datatypes::TimeUnit::*;
3030
use arrow::datatypes::{
31-
DECIMAL32_MAX_PRECISION, DECIMAL32_MAX_SCALE, DECIMAL64_MAX_PRECISION,
32-
DECIMAL64_MAX_SCALE, DECIMAL128_MAX_PRECISION, DECIMAL128_MAX_SCALE,
33-
DECIMAL256_MAX_PRECISION, DECIMAL256_MAX_SCALE, DataType, Field, FieldRef, Fields,
34-
TimeUnit,
31+
DataType, Field, FieldRef,
32+
Fields, TimeUnit, DECIMAL128_MAX_PRECISION,
33+
DECIMAL128_MAX_SCALE, DECIMAL256_MAX_PRECISION, DECIMAL256_MAX_SCALE, DECIMAL32_MAX_PRECISION, DECIMAL32_MAX_SCALE, DECIMAL64_MAX_PRECISION,
34+
DECIMAL64_MAX_SCALE,
3535
};
3636
use datafusion_common::{
37-
Diagnostic, Result, Span, Spans, exec_err, internal_err, not_impl_err,
38-
plan_datafusion_err, plan_err,
37+
exec_err, internal_err, not_impl_err, plan_datafusion_err, plan_err, Diagnostic, Result,
38+
Span, Spans,
3939
};
4040
use itertools::Itertools;
4141

@@ -528,11 +528,12 @@ enum TypeCategory {
528528
impl From<&DataType> for TypeCategory {
529529
fn from(data_type: &DataType) -> Self {
530530
match data_type {
531-
// Dict is a special type in arrow, we check the value type
531+
// Dict and REE are special types in arrow, we check the value type.
532532
DataType::Dictionary(_, v) => {
533533
let v = v.as_ref();
534534
TypeCategory::from(v)
535535
}
536+
DataType::RunEndEncoded(_, v) => TypeCategory::from(v.data_type()),
536537
_ => {
537538
if data_type.is_numeric() {
538539
return TypeCategory::Numeric;
@@ -709,6 +710,27 @@ fn type_union_resolution_coercion(
709710
None => None,
710711
}
711712
}
713+
(
714+
DataType::RunEndEncoded(lhs_run, lhs_val),
715+
DataType::RunEndEncoded(rhs_run, rhs_val),
716+
) => {
717+
let new_run =
718+
type_union_resolution_coercion(lhs_run.data_type(), rhs_run.data_type())?;
719+
let new_val =
720+
type_union_resolution_coercion(lhs_val.data_type(), rhs_val.data_type())?;
721+
Some(DataType::RunEndEncoded(
722+
Arc::new(lhs_run.as_ref().clone().with_data_type(new_run)),
723+
Arc::new(lhs_val.as_ref().clone().with_data_type(new_val)),
724+
))
725+
}
726+
(DataType::RunEndEncoded(run, val), other)
727+
| (other, DataType::RunEndEncoded(run, val)) => {
728+
let new_val = type_union_resolution_coercion(val.data_type(), other)?;
729+
Some(DataType::RunEndEncoded(
730+
Arc::clone(run),
731+
Arc::new(val.as_ref().clone().with_data_type(new_val)),
732+
))
733+
}
712734
(DataType::Struct(lhs), DataType::Struct(rhs)) => {
713735
if lhs.len() != rhs.len() {
714736
return None;

datafusion/sqllogictest/test_files/coalesce.slt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,19 @@ query T
443443
select coalesce(arrow_cast('', 'Utf8View'), arrow_cast('', 'Dictionary(UInt32, Utf8)'));
444444
----
445445
(empty)
446+
447+
# RunEndEncoded column coalesced with a string literal
448+
statement ok
449+
create table ree_t as
450+
select arrow_cast(c, 'RunEndEncoded("run_ends": non-null Int32, "values": Utf8)') as ree_col
451+
from (values ('hello'), (NULL), ('world')) as t(c);
452+
453+
query ?
454+
select coalesce(ree_col, '__null__') from ree_t;
455+
----
456+
hello
457+
__null__
458+
world
459+
460+
statement ok
461+
drop table ree_t;

0 commit comments

Comments
 (0)