Weijun-H commented on code in PR #26589:
URL: https://github.com/apache/datafusion/pull/26589#discussion_r4593325064
##########
datafusion/physical-expr/src/expressions/case.rs:
##########
@@ -118,41 +184,80 @@ struct CaseBody {
impl CaseBody {
/// Derives a [ProjectedCaseBody] from this [CaseBody].
- fn project(&self) -> Result<ProjectedCaseBody> {
- // Determine the set of columns that are used in all the expressions
of the case body.
+ fn project(&self, schema: &Schema) -> Result<ProjectedCaseBody> {
+ // Lambda bodies are evaluated against batches of a different shape, so
+ // expressions are only precomputed when the body contains no lambdas.
+ let can_precompute = !self.exprs().any(|e| {
+ e.exists(|e| Ok(e.is::<LambdaExpr>() || e.is::<LambdaVariable>()))
+ .expect("Closure cannot fail")
+ });
+
+ // Unknown leaves may read input columns without exposing a Column
child.
+ // This is checked over all the nodes, independently of precomputation.
+ let supports_projection = !self.exprs().any(|e| {
+ e.exists(|e| {
+ Ok(e.children().is_empty()
+ && !e.is::<Column>()
+ && !e.is::<LambdaVariable>()
+ && !e.is::<Literal>()
+ && !e.is::<ScalarFunctionExpr>())
+ })
+ .expect("Closure cannot fail")
+ });
+
+ // Find the candidates for precomputation and the columns used outside
of them.
+ let mut direct_column_indices = HashSet::<usize>::new();
+ let mut precompute_candidates = IndexSet::<Arc<dyn
PhysicalExpr>>::new();
+ if can_precompute {
+ self.exprs().for_each(|expr| {
+ expr.apply(|expr| {
+ if is_precomputable(expr, schema) {
+ precompute_candidates.insert(Arc::clone(expr));
+ return Ok(TreeNodeRecursion::Jump);
+ }
+ if let Some(column) = expr.downcast_ref::<Column>() {
+ direct_column_indices.insert(column.index());
+ }
+ Ok(TreeNodeRecursion::Continue)
+ })
+ .expect("Closure cannot fail");
+ });
+ }
+
+ // Precomputing only pays off when it spares filtering a column that
is not
+ // otherwise used. Candidates referring only to used columns are
evaluated in place.
+ let precomputed = precompute_candidates
+ .into_iter()
+ .filter(|expr| {
+ collect_columns(expr)
+ .iter()
+ .any(|column|
!direct_column_indices.contains(&column.index()))
+ })
+ .collect::<IndexSet<_>>();
+
+ // Determine the set of columns that are used by the expressions of
the case body
Review Comment:
This second pass can go. A candidate is dropped only when all of its columns
are already in `direct_column_indices`, so the set collected here equals
`direct_column_indices` (plus lambda variables, which only occur when
`can_precompute` is false). Collecting `Column`/`LambdaVariable` indices in the
first pass and filtering candidates against that set gives the same projection
with one traversal and no `HashSet`.
##########
datafusion/physical-expr/src/expressions/case.rs:
##########
@@ -2039,18 +2272,19 @@ mod tests {
&schema,
Arc::new(ConfigOptions::default()),
)?);
- let when = binary(random, Operator::Gt, lit(0.5f64), &schema)?;
+ let when = binary(col("a", &schema)?, Operator::Gt, lit(0i32),
&schema)?;
let expr = CaseExpr::try_new(
None,
- vec![(when, col("b", &schema)?)],
- Some(col("c", &schema)?),
+ vec![(when, random)],
+ Some(cast(col("c", &schema)?, &schema, Float64)?),
Review Comment:
The result check below only asserts the length. Rows 1 and 2 take the
`ELSE`, so asserting `-3.0` and `777.0` there, and non-null values in `[0, 1)`
for rows 0 and 3, would also catch a wrong column mapping.
##########
datafusion/physical-expr/src/expressions/case.rs:
##########
@@ -104,6 +108,68 @@ impl PartialEq for LiteralLookupTable {
impl Eq for LiteralLookupTable {}
+/// A [`ProjectedCaseBody`] derived from the [`CaseBody`] on first evaluation.
+///
+/// Deriving it in [`CaseExpr::try_new`] would make rewrites of nested `CASE`
expressions
+/// exponential in the nesting depth: [`CaseBody::project`] rebuilds the
nested expressions,
+/// each of which would project its own body again.
+#[derive(Debug, Default)]
+struct LazyProjectedBody {
+ projected: OnceLock<ProjectedCaseBody>,
+ /// Whether the single `WHEN` is evaluated against the full input batch
before the
+ /// derived body is used, so that the derived body only needs its `THEN`
and `ELSE`.
+ when_evaluated: bool,
+}
+
+impl LazyProjectedBody {
+ /// For bodies with a single `WHEN` that is evaluated against the full
input batch.
+ fn when_evaluated() -> Self {
+ Self {
+ projected: OnceLock::new(),
+ when_evaluated: true,
+ }
+ }
+
+ fn project(&self, body: &CaseBody, schema: &Schema) ->
Result<&ProjectedCaseBody> {
+ if let Some(projected) = self.projected.get() {
+ return Ok(projected);
+ }
+ // Concurrent first evaluations may each derive the body; one of them
is kept.
+ let projected = if self.when_evaluated {
+ // The derived `WHEN` is never evaluated, so a placeholder keeps
it from
+ // affecting the projection and the precomputed expressions.
+ let placeholder: Arc<dyn PhysicalExpr> =
+ Arc::new(Literal::new(ScalarValue::Boolean(Some(true))));
+ CaseBody {
+ expr: None,
+ when_then_expr: vec![(
+ placeholder,
+ Arc::clone(&body.when_then_expr[0].1),
+ )],
+ else_expr: body.else_expr.clone(),
+ }
+ .project(schema)?
+ } else {
+ body.project(schema)?
+ };
+ Ok(self.projected.get_or_init(|| projected))
+ }
+}
+
+/// The projection is derived from the [`CaseBody`], which is already hashed.
+impl Hash for LazyProjectedBody {
Review Comment:
`CaseExpr` hashes and compares only `body`, and the only use of
`EvalMethod`'s `Hash`/`PartialEq` is one `assert_eq!(expr.eval_method,
EvalMethod::InfallibleExprOrNull)` in the tests. If `EvalMethod` and
`ProjectedCaseBody` derive only `Debug` and that assert uses `matches!`, these
no-op `Hash`/always-true `PartialEq` impls are not needed (the same goes for
`LiteralLookupTable`'s).
##########
datafusion/physical-expr/src/expressions/case.rs:
##########
@@ -162,10 +267,25 @@ impl CaseBody {
.collect::<IndexMap<usize, usize>>();
// Construct the projected body by rewriting each expression from the
original body
- // using the column index mapping.
+ // using the column index mapping. Precomputed expressions are
replaced with
+ // references to the columns appended after the projected ones.
let project = |expr: &Arc<dyn PhysicalExpr>| -> Result<Arc<dyn
PhysicalExpr>> {
Arc::clone(expr)
.transform_down(|e| {
+ if !precomputed.is_empty()
+ && is_precomputable(&e, schema)
Review Comment:
Membership in `precomputed` already implies `is_precomputable`, so a cheap
`e.placement() == ExpressionPlacement::MoveTowardsLeafNodes` check before the
lookup is enough here, instead of re-walking the subtree (including
`crosses_map`) at every node.
##########
datafusion/physical-expr/src/expressions/case.rs:
##########
@@ -2060,6 +2294,539 @@ mod tests {
Ok(())
}
+ /// A batch with columns `x: Int32` and `s: Struct<a: Int32, b: Int32, c:
Utf8>`.
+ fn case_test_batch_with_struct() -> Result<RecordBatch> {
+ let struct_array = StructArray::from(vec![
+ (
+ Arc::new(Field::new("a", DataType::Int32, true)),
+ Arc::new(Int32Array::from(vec![Some(1), Some(2), None,
Some(4)]))
+ as ArrayRef,
+ ),
+ (
+ Arc::new(Field::new("b", DataType::Int32, true)),
+ Arc::new(Int32Array::from(vec![10, 20, 30, 40])) as ArrayRef,
+ ),
+ (
+ Arc::new(Field::new("c", DataType::Utf8, true)),
+ Arc::new(StringArray::from(vec!["p", "q", "r", "s"])) as
ArrayRef,
+ ),
+ ]);
+ let schema = Schema::new(vec![
+ Field::new("x", DataType::Int32, true),
+ Field::new("s", struct_array.data_type().clone(), true),
+ ]);
+ Ok(RecordBatch::try_new(
+ Arc::new(schema),
+ vec![
+ Arc::new(Int32Array::from(vec![100, 200, 300, 400])),
+ Arc::new(struct_array),
+ ],
+ )?)
+ }
+
+ fn get_field_expr(
+ base: Arc<dyn PhysicalExpr>,
+ name: &str,
+ schema: &Schema,
+ ) -> Result<Arc<dyn PhysicalExpr>> {
+ Ok(Arc::new(ScalarFunctionExpr::try_new(
+ datafusion_functions::core::get_field(),
+ vec![base, lit(name)],
+ schema,
+ Arc::new(ConfigOptions::default()),
+ )?))
+ }
+
+ /// Building and evaluating a nested `CASE` must not be exponential in the
nesting
+ /// depth when each level reads a different field.
+ #[test]
+ fn nested_case_with_distinct_struct_fields() -> Result<()> {
+ let fields = (0..=40)
+ .map(|i| Field::new(format!("f{i}"), DataType::Int32, true))
+ .collect::<Vec<_>>();
+ let columns = (0..=40)
+ .map(|i| Arc::new(Int32Array::from(vec![i, i + 1])) as ArrayRef)
+ .collect::<Vec<_>>();
+ let s = StructArray::new(fields.into(), columns, None);
+ let schema = Arc::new(Schema::new(vec![Field::new(
+ "s",
+ s.data_type().clone(),
+ true,
+ )]));
+ let batch = RecordBatch::try_new(Arc::clone(&schema),
vec![Arc::new(s)])?;
+
+ let s = col("s", &schema)?;
+ let mut expr = get_field_expr(Arc::clone(&s), "f0", &schema)?;
+ for i in 1..=40 {
+ let when = binary(
+ get_field_expr(Arc::clone(&s), &format!("f{i}"), &schema)?,
+ Operator::Eq,
+ lit(-1i32),
+ &schema,
+ )?;
+ expr = Arc::new(CaseExpr::try_new(
+ None,
+ vec![(when, lit(1i32))],
+ Some(expr),
+ )?);
+ }
+
+ let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?;
+ assert_eq!(as_int32_array(&result)?, &Int32Array::from(vec![0, 1]));
+ Ok(())
+ }
+
+ /// A batch with a column `s: Struct<a: Int32, m: Map<Utf8, Int32>>`.
+ fn case_test_batch_with_struct_map() -> Result<RecordBatch> {
+ let mut map_builder =
+ MapBuilder::new(None, StringBuilder::new(), Int32Builder::new());
+ for (key, value) in [("k", 1), ("k", 2), ("j", 3)] {
+ map_builder.keys().append_value(key);
+ map_builder.values().append_value(value);
+ map_builder.append(true)?;
+ }
+ let map_array = map_builder.finish();
+ let struct_array = StructArray::from(vec![
+ (
+ Arc::new(Field::new("a", DataType::Int32, true)),
+ Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
+ ),
+ (
+ Arc::new(Field::new("m", map_array.data_type().clone(), true)),
+ Arc::new(map_array) as ArrayRef,
+ ),
+ ]);
+ let schema = Schema::new(vec![Field::new(
+ "s",
+ struct_array.data_type().clone(),
+ true,
+ )]);
+ Ok(RecordBatch::try_new(
+ Arc::new(schema),
+ vec![Arc::new(struct_array)],
+ )?)
+ }
+
+ /// `CASE WHEN s['a'] = 1 THEN <then> WHEN s['a'] = 2 THEN <then> END`
+ fn case_with_struct_map_lookup(
+ then: Arc<dyn PhysicalExpr>,
+ schema: &Schema,
+ ) -> Result<CaseExpr> {
+ let s_a = get_field_expr(col("s", schema)?, "a", schema)?;
+ CaseExpr::try_new(
+ None,
+ vec![
+ (
+ binary(Arc::clone(&s_a), Operator::Eq, lit(1i32), schema)?,
+ Arc::clone(&then),
+ ),
+ (binary(s_a, Operator::Eq, lit(2i32), schema)?, then),
+ ],
+ None,
+ )
+ }
+
+ #[test]
+ fn case_does_not_precompute_flat_map_lookup() -> Result<()> {
+ let batch = case_test_batch_with_struct_map()?;
+ let schema = batch.schema();
+
+ // get_field(s, 'm', 'k')
+ let s_m_k: Arc<dyn PhysicalExpr> =
Arc::new(ScalarFunctionExpr::try_new(
+ datafusion_functions::core::get_field(),
+ vec![col("s", &schema)?, lit("m"), lit("k")],
+ &schema,
+ Arc::new(ConfigOptions::default()),
+ )?);
+ let expr = case_with_struct_map_lookup(s_m_k, &schema)?;
+
+ match &expr.eval_method {
+ EvalMethod::NoExpression(projected) => {
+ let projected = projected.project(&expr.body, &schema)?;
+ assert!(projected.supports_projection);
+ assert_eq!(projected.projection, vec![0]);
+ assert!(projected.precomputed.is_empty());
+ }
+ method => panic!("expected NoExpression, got {method:?}"),
+ }
+
+ let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?;
+ assert_eq!(
+ as_int32_array(&result)?,
+ &Int32Array::from(vec![Some(1), Some(2), None])
+ );
+ Ok(())
+ }
+
+ #[test]
+ fn case_precomputes_map_base_of_nested_lookup() -> Result<()> {
+ let batch = case_test_batch_with_struct_map()?;
+ let schema = batch.schema();
+ let s = col("s", &schema)?;
+ let s_a = get_field_expr(Arc::clone(&s), "a", &schema)?;
+ let s_m = get_field_expr(s, "m", &schema)?;
+
+ // get_field(get_field(s, 'm'), 'k')
+ let s_m_k = get_field_expr(Arc::clone(&s_m), "k", &schema)?;
+ let expr = case_with_struct_map_lookup(s_m_k, &schema)?;
+
+ match &expr.eval_method {
+ EvalMethod::NoExpression(projected) => {
+ let projected = projected.project(&expr.body, &schema)?;
+ assert!(projected.supports_projection);
+ assert!(projected.projection.is_empty());
+ assert_eq!(projected.precomputed, vec![s_a, s_m]);
+ }
+ method => panic!("expected NoExpression, got {method:?}"),
+ }
+
+ let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?;
+ assert_eq!(
+ as_int32_array(&result)?,
+ &Int32Array::from(vec![Some(1), Some(2), None])
+ );
+ Ok(())
+ }
+
+ /// The plan may declare a struct column non-nullable while the batch has
null
+ /// structs, which makes the precomputed fields nullable.
+ #[test]
+ fn case_precomputes_fields_of_null_structs_declared_non_nullable() ->
Result<()> {
+ let fields = arrow::datatypes::Fields::from(vec![
+ Field::new("a", DataType::Int32, false),
+ Field::new("b", DataType::Int32, false),
+ ]);
+ let struct_array = StructArray::new(
+ fields.clone(),
+ vec![
+ Arc::new(Int32Array::from(vec![1, 2, 3])),
+ Arc::new(Int32Array::from(vec![10, 20, 30])),
+ ],
+ Some(arrow::buffer::NullBuffer::from(vec![true, true, false])),
+ );
+ let batch_schema = Arc::new(Schema::new(vec![Field::new(
+ "s",
+ DataType::Struct(fields.clone()),
+ true,
+ )]));
+ let batch = RecordBatch::try_new(batch_schema,
vec![Arc::new(struct_array)])?;
+ let plan_schema =
+ Schema::new(vec![Field::new("s", DataType::Struct(fields),
false)]);
+
+ let s = col("s", &plan_schema)?;
+ let s_a = get_field_expr(Arc::clone(&s), "a", &plan_schema)?;
+ let s_b = get_field_expr(s, "b", &plan_schema)?;
+
+ // CASE WHEN s['a'] = 1 THEN s['b'] WHEN s['a'] > 1 THEN 0 END
+ let expr = CaseExpr::try_new(
+ None,
+ vec![
+ (
+ binary(Arc::clone(&s_a), Operator::Eq, lit(1i32),
&plan_schema)?,
+ Arc::clone(&s_b),
+ ),
+ (
+ binary(Arc::clone(&s_a), Operator::Gt, lit(1i32),
&plan_schema)?,
+ lit(0i32),
+ ),
+ ],
+ None,
+ )?;
+
+ match &expr.eval_method {
+ EvalMethod::NoExpression(projected) => {
+ let projected = projected.project(&expr.body, &plan_schema)?;
+ assert_eq!(projected.precomputed, vec![s_a, s_b]);
+ }
+ method => panic!("expected NoExpression, got {method:?}"),
+ }
+
+ let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?;
+ assert_eq!(
+ as_int32_array(&result)?,
+ &Int32Array::from(vec![Some(10), Some(0), None])
+ );
+ Ok(())
+ }
+
+ #[test]
+ fn case_without_expr_precomputes_struct_fields() -> Result<()> {
+ let batch = case_test_batch_with_struct()?;
+ let metadata =
+ std::collections::HashMap::from([("key".to_string(),
"value".to_string())]);
+ let schema = batch
+ .schema()
+ .as_ref()
+ .clone()
+ .with_metadata(metadata.clone());
+ let batch = batch.with_schema(Arc::new(schema))?;
+ let schema = batch.schema();
+ let s = col("s", &schema)?;
+ let s_a = get_field_expr(Arc::clone(&s), "a", &schema)?;
+ let s_b = get_field_expr(Arc::clone(&s), "b", &schema)?;
+
+ // CASE WHEN s['a'] = 1 THEN s['b'] WHEN s['a'] > 1 THEN x ELSE s['a']
END
+ let expr = CaseExpr::try_new(
+ None,
+ vec![
+ (
+ binary(Arc::clone(&s_a), Operator::Eq, lit(1i32),
&schema)?,
+ Arc::clone(&s_b),
+ ),
+ (
+ binary(Arc::clone(&s_a), Operator::Gt, lit(1i32),
&schema)?,
+ col("x", &schema)?,
+ ),
+ ],
+ Some(Arc::clone(&s_a)),
+ )?;
+
+ match &expr.eval_method {
+ EvalMethod::NoExpression(projected) => {
+ let projected = projected.project(&expr.body, &schema)?;
+ assert!(projected.supports_projection);
+ assert_eq!(projected.projection, vec![0]);
+ assert_eq!(projected.precomputed, vec![s_a, s_b]);
+ let projected_batch = projected
+ .project_batch(&batch)?
+ .expect("batch should be projected");
+ assert_eq!(projected_batch.schema().metadata(), &metadata);
+ }
+ method => panic!("expected NoExpression, got {method:?}"),
+ }
+
+ let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?;
+ let result = as_int32_array(&result)?;
+ let expected = Int32Array::from(vec![Some(10), Some(200), None,
Some(400)]);
+ assert_eq!(&expected, result);
+ Ok(())
+ }
+
+ #[test]
+ fn case_with_expr_precomputes_struct_fields() -> Result<()> {
+ let batch = case_test_batch_with_struct()?;
+ let schema = batch.schema();
+ let s = col("s", &schema)?;
+ let s_a = get_field_expr(Arc::clone(&s), "a", &schema)?;
+ let s_c = get_field_expr(Arc::clone(&s), "c", &schema)?;
+
+ // CASE s['a'] WHEN 1 THEN s['c'] WHEN 4 THEN 'four' END
+ let expr = CaseExpr::try_new(
+ Some(Arc::clone(&s_a)),
+ vec![(lit(1i32), Arc::clone(&s_c)), (lit(4i32), lit("four"))],
+ None,
+ )?;
+
+ match &expr.eval_method {
+ EvalMethod::WithExpression(projected) => {
+ let projected = projected.project(&expr.body, &schema)?;
+ assert!(projected.supports_projection);
+ assert!(projected.projection.is_empty());
+ assert_eq!(projected.precomputed, vec![s_a, s_c]);
+ }
+ method => panic!("expected WithExpression, got {method:?}"),
+ }
+
+ let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?;
+ let result = as_string_array(&result);
+ let expected = StringArray::from(vec![Some("p"), None, None,
Some("four")]);
+ assert_eq!(&expected, result);
+ Ok(())
+ }
+
+ #[test]
+ fn case_expr_or_expr_precomputes_struct_fields() -> Result<()> {
+ let batch = case_test_batch_with_struct()?;
+ let schema = batch.schema();
+ let s = col("s", &schema)?;
+ let s_a = get_field_expr(Arc::clone(&s), "a", &schema)?;
+ let s_b = get_field_expr(Arc::clone(&s), "b", &schema)?;
+
+ // CASE WHEN s['a'] > 1 THEN s['b'] / (s['a'] - 1) ELSE x END
+ let expr = CaseExpr::try_new(
+ None,
+ vec![(
+ binary(Arc::clone(&s_a), Operator::Gt, lit(1i32), &schema)?,
+ binary(
+ Arc::clone(&s_b),
+ Operator::Divide,
+ binary(Arc::clone(&s_a), Operator::Minus, lit(1i32),
&schema)?,
+ &schema,
+ )?,
+ )],
+ Some(col("x", &schema)?),
+ )?;
+
+ match &expr.eval_method {
+ EvalMethod::ExpressionOrExpression(projected) => {
+ let projected = projected.project(&expr.body, &schema)?;
+ assert!(projected.supports_projection);
+ assert_eq!(projected.projection, vec![0]);
+ assert_eq!(projected.precomputed, vec![s_b, s_a]);
+ }
+ method => panic!("expected ExpressionOrExpression, got
{method:?}"),
+ }
+
+ let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?;
+ let result = as_int32_array(&result)?;
+ let expected = Int32Array::from(vec![Some(100), Some(20), Some(300),
Some(13)]);
+ assert_eq!(&expected, result);
+ Ok(())
+ }
+
+ #[test]
+ fn case_expr_or_expr_does_not_precompute_when_only_fields() -> Result<()> {
+ let batch = case_test_batch_with_struct()?;
+ let schema = batch.schema();
+ let s_a = get_field_expr(col("s", &schema)?, "a", &schema)?;
+ let x = col("x", &schema)?;
+
+ // CASE WHEN s['a'] > 1 THEN x ELSE -x END
+ let expr = CaseExpr::try_new(
+ None,
+ vec![(
+ binary(s_a, Operator::Gt, lit(1i32), &schema)?,
+ Arc::clone(&x),
+ )],
+ Some(binary(lit(0i32), Operator::Minus, x, &schema)?),
+ )?;
+
+ match &expr.eval_method {
+ EvalMethod::ExpressionOrExpression(projected) => {
+ let projected = projected.project(&expr.body, &schema)?;
+ assert!(projected.supports_projection);
+ assert_eq!(projected.projection, vec![0]);
+ assert!(projected.precomputed.is_empty());
+ }
+ method => panic!("expected ExpressionOrExpression, got
{method:?}"),
+ }
+
+ let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?;
+ let result = as_int32_array(&result)?;
+ let expected =
+ Int32Array::from(vec![Some(-100), Some(200), Some(-300),
Some(400)]);
+ assert_eq!(&expected, result);
+ Ok(())
+ }
+
+ #[test]
+ fn case_with_custom_column_and_struct_fields() -> Result<()> {
+ let batch = case_test_batch_with_struct()?;
+ let schema = batch.schema();
+ let s = col("s", &schema)?;
+ let s_a = get_field_expr(Arc::clone(&s), "a", &schema)?;
+ let s_b = get_field_expr(Arc::clone(&s), "b", &schema)?;
+
+ // CASE WHEN s['a'] = 1 THEN x WHEN s['a'] > 1 THEN s['b'] END,
+ // with `x` read by a leaf that is opaque to projection
+ let expr = CaseExpr::try_new(
+ None,
+ vec![
+ (
+ binary(Arc::clone(&s_a), Operator::Eq, lit(1i32),
&schema)?,
+ Arc::new(CustomColumn::new("x", 0)),
+ ),
+ (
+ binary(Arc::clone(&s_a), Operator::Gt, lit(1i32),
&schema)?,
+ Arc::clone(&s_b),
+ ),
+ ],
+ None,
+ )?;
+
+ match &expr.eval_method {
+ EvalMethod::NoExpression(projected) => {
+ let projected = projected.project(&expr.body, &schema)?;
+ assert!(!projected.supports_projection);
+ }
+ method => panic!("expected NoExpression, got {method:?}"),
+ }
+
+ let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?;
+ let result = as_int32_array(&result)?;
+ let expected = Int32Array::from(vec![Some(100), Some(20), None,
Some(40)]);
+ assert_eq!(&expected, result);
+ Ok(())
+ }
+
+ #[test]
+ fn case_does_not_precompute_fields_of_used_struct() -> Result<()> {
+ let batch = case_test_batch_with_struct()?;
+ let schema = batch.schema();
+ let s = col("s", &schema)?;
+ let s_a = get_field_expr(Arc::clone(&s), "a", &schema)?;
+
+ // CASE WHEN s['a'] = 1 THEN s WHEN s['a'] = 2 THEN NULL END
+ let expr = CaseExpr::try_new(
+ None,
+ vec![
+ (
+ binary(Arc::clone(&s_a), Operator::Eq, lit(1i32),
&schema)?,
+ Arc::clone(&s),
+ ),
+ (
+ binary(Arc::clone(&s_a), Operator::Eq, lit(2i32),
&schema)?,
+ lit(ScalarValue::try_new_null(&s.data_type(&schema)?)?),
+ ),
+ ],
+ None,
+ )?;
+
+ match &expr.eval_method {
+ EvalMethod::NoExpression(projected) => {
+ let projected = projected.project(&expr.body, &schema)?;
+ assert!(projected.supports_projection);
+ assert_eq!(projected.projection, vec![1]);
+ assert!(projected.precomputed.is_empty());
+ }
+ method => panic!("expected NoExpression, got {method:?}"),
+ }
+
+ let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?;
+ let b = as_int32_array(result.as_struct().column(1))?;
+ assert_eq!(b.value(0), 10);
Review Comment:
These two asserts check one child value and the null count only. Could this
compare the whole expected struct array?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]