Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions datafusion/physical-expr/src/simplifier/const_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use arrow::record_batch::RecordBatch;
use datafusion_common::tree_node::{Transformed, TreeNode, TreeNodeRecursion};
use datafusion_common::{Result, ScalarValue};
use datafusion_expr_common::columnar_value::ColumnarValue;
use datafusion_physical_expr_common::physical_expr::is_volatile;

use crate::PhysicalExpr;
use crate::expressions::{Column, Literal};
Expand All @@ -43,7 +42,7 @@ use crate::expressions::{Column, Literal};
pub fn simplify_const_expr(
expr: &Arc<dyn PhysicalExpr>,
) -> Result<Transformed<Arc<dyn PhysicalExpr>>> {
if is_volatile(expr) || has_column_references(expr) {
if !can_evaluate_as_constant(expr) {
return Ok(Transformed::no(Arc::clone(expr)));
}

Expand Down Expand Up @@ -73,6 +72,22 @@ pub fn simplify_const_expr(
}
}

fn can_evaluate_as_constant(expr: &Arc<dyn PhysicalExpr>) -> bool {
let mut can_evaluate = true;

expr.apply(|e| {
if e.as_any().is::<Column>() || e.is_volatile_node() {
can_evaluate = false;
Ok(TreeNodeRecursion::Stop)
} else {
Ok(TreeNodeRecursion::Continue)
}
})
.expect("apply should not fail");

can_evaluate
}

/// Create a 1-row dummy RecordBatch for evaluating constant expressions.
///
/// The batch is never actually accessed for data - it's just needed because
Expand Down
4 changes: 2 additions & 2 deletions datafusion/physical-expr/src/simplifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<'a> PhysicalExprSimplifier<'a> {
while count < MAX_LOOP_COUNT {
count += 1;
let result = current_expr.transform(|node| {
#[cfg(test)]
#[cfg(debug_assertions)]
let original_type = node.data_type(schema).unwrap();

// Apply NOT expression simplification first, then unwrap cast optimization,
Expand All @@ -64,7 +64,7 @@ impl<'a> PhysicalExprSimplifier<'a> {
})?
.transform_data(|node| const_evaluator::simplify_const_expr(&node))?;

#[cfg(test)]
#[cfg(debug_assertions)]
assert_eq!(
rewritten.data.data_type(schema).unwrap(),
original_type,
Expand Down