Skip to content

Commit fab3b71

Browse files
authored
feat[expr-common]: add REE arithmetic coercion for numeric and decimal (#21179)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #21178 ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> Fix planning error ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> Match arms for REE types ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Yes ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> Queries that would previously return an error on REE columns now pass. <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> Signed-off-by: Alfonso Subiotto Marques <alfonso.subiotto@polarsignals.com>
1 parent 97172e2 commit fab3b71

3 files changed

Lines changed: 74 additions & 2 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,16 @@ fn math_decimal_coercion(
378378
let (lhs_type, value_type) = math_decimal_coercion(lhs_type, value_type)?;
379379
Some((lhs_type, value_type))
380380
}
381+
(RunEndEncoded(_, field), _) => {
382+
let (value_type, rhs_type) =
383+
math_decimal_coercion(field.data_type(), rhs_type)?;
384+
Some((value_type, rhs_type))
385+
}
386+
(_, RunEndEncoded(_, field)) => {
387+
let (lhs_type, value_type) =
388+
math_decimal_coercion(lhs_type, field.data_type())?;
389+
Some((lhs_type, value_type))
390+
}
381391
(
382392
Null,
383393
Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
@@ -1414,6 +1424,15 @@ fn mathematics_numerical_coercion(
14141424
(_, Dictionary(_, value_type)) => {
14151425
mathematics_numerical_coercion(lhs_type, value_type)
14161426
}
1427+
(RunEndEncoded(_, lhs_field), RunEndEncoded(_, rhs_field)) => {
1428+
mathematics_numerical_coercion(lhs_field.data_type(), rhs_field.data_type())
1429+
}
1430+
(RunEndEncoded(_, field), _) => {
1431+
mathematics_numerical_coercion(field.data_type(), rhs_type)
1432+
}
1433+
(_, RunEndEncoded(_, field)) => {
1434+
mathematics_numerical_coercion(lhs_type, field.data_type())
1435+
}
14171436
_ => numerical_coercion(lhs_type, rhs_type),
14181437
}
14191438
}
@@ -1493,6 +1512,15 @@ fn both_numeric_or_null_and_numeric(lhs_type: &DataType, rhs_type: &DataType) ->
14931512
(_, Dictionary(_, value_type)) => {
14941513
lhs_type.is_numeric() && value_type.is_numeric()
14951514
}
1515+
(RunEndEncoded(_, lhs_field), RunEndEncoded(_, rhs_field)) => {
1516+
lhs_field.data_type().is_numeric() && rhs_field.data_type().is_numeric()
1517+
}
1518+
(RunEndEncoded(_, field), _) => {
1519+
field.data_type().is_numeric() && rhs_type.is_numeric()
1520+
}
1521+
(_, RunEndEncoded(_, field)) => {
1522+
lhs_type.is_numeric() && field.data_type().is_numeric()
1523+
}
14961524
_ => lhs_type.is_numeric() && rhs_type.is_numeric(),
14971525
}
14981526
}

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
// under the License.
1717

1818
use super::*;
19+
use DataType::*;
20+
21+
fn ree(value_type: DataType) -> DataType {
22+
RunEndEncoded(
23+
Arc::new(Field::new("run_ends", Int32, false)),
24+
Arc::new(Field::new("values", value_type, false)),
25+
)
26+
}
1927

2028
#[test]
2129
fn test_ree_type_coercion() {
22-
use DataType::*;
23-
2430
let lhs_type = RunEndEncoded(
2531
Arc::new(Field::new("run_ends", Int8, false)),
2632
Arc::new(Field::new("values", Int32, false)),
@@ -97,3 +103,29 @@ fn test_ree_type_coercion() {
97103
Some(rhs_type.clone())
98104
);
99105
}
106+
107+
#[test]
108+
fn test_ree_arithmetic_coercion() -> Result<()> {
109+
test_coercion_binary_rule!(ree(Int64), Int64, Operator::Plus, Int64);
110+
test_coercion_binary_rule!(Int64, ree(Int64), Operator::Multiply, Int64);
111+
test_coercion_binary_rule!(ree(Int32), ree(Int64), Operator::Plus, Int64);
112+
113+
// Decimal unwrapping through math_decimal_coercion
114+
let (lhs, rhs) =
115+
BinaryTypeCoercer::new(&ree(Decimal128(10, 2)), &Operator::Plus, &Int32)
116+
.get_input_types()?;
117+
assert_eq!(lhs, Decimal128(10, 2));
118+
assert_eq!(rhs, Decimal128(10, 0));
119+
120+
let (lhs, rhs) =
121+
BinaryTypeCoercer::new(&Int32, &Operator::Plus, &ree(Decimal128(10, 2)))
122+
.get_input_types()?;
123+
assert_eq!(lhs, Decimal128(10, 0));
124+
assert_eq!(rhs, Decimal128(10, 2));
125+
126+
let result =
127+
BinaryTypeCoercer::new(&ree(Utf8), &Operator::Plus, &Int32).get_input_types();
128+
assert!(result.is_err());
129+
130+
Ok(())
131+
}

datafusion/sqllogictest/test_files/run_end_encoded.slt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,15 @@ FROM sensor_readings;
5555
----
5656
sensor_A
5757
sensor_B
58+
59+
# Test arithmetic on REE columns (numeric and decimal value types)
60+
query IR rowsort
61+
SELECT
62+
arrow_cast(temperature, 'RunEndEncoded("run_ends": non-null Int32, "values": Int64)') + 1 AS t_plus_one,
63+
arrow_cast(arrow_cast(temperature, 'Decimal128(10, 2)'), 'RunEndEncoded("run_ends": non-null Int32, "values": Decimal128(10, 2))') + 1 AS t_dec_plus_one
64+
FROM sensor_readings;
65+
----
66+
21 21
67+
23 23
68+
24 24
69+
25 25

0 commit comments

Comments
 (0)