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
26 changes: 19 additions & 7 deletions datafusion/physical-expr/src/simplifier/const_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use arrow::array::new_null_array;
use arrow::datatypes::{DataType, Field, Schema};
use arrow::record_batch::RecordBatch;
use datafusion_common::tree_node::{Transformed, TreeNode, TreeNodeRecursion};
use datafusion_common::{Result, ScalarValue};
use datafusion_common::{Result, ScalarValue, internal_datafusion_err};
use datafusion_expr_common::columnar_value::ColumnarValue;

use crate::PhysicalExpr;
Expand Down Expand Up @@ -53,7 +53,7 @@ pub fn simplify_const_expr(
}

// Evaluate the expression
match expr.evaluate(&batch) {
match expr.evaluate(batch) {
Ok(ColumnarValue::Scalar(scalar)) => {
Ok(Transformed::yes(Arc::new(Literal::new(scalar))))
}
Expand Down Expand Up @@ -146,11 +146,23 @@ pub(crate) fn simplify_const_expr_immediate(
/// that only contain literals, the batch content is irrelevant.
///
/// This is the same approach used in the logical expression `ConstEvaluator`.
pub(crate) fn create_dummy_batch() -> Result<RecordBatch> {
// RecordBatch requires at least one column
let dummy_schema = Arc::new(Schema::new(vec![Field::new("_", DataType::Null, true)]));
let col = new_null_array(&DataType::Null, 1);
Ok(RecordBatch::try_new(dummy_schema, vec![col])?)
pub(crate) fn create_dummy_batch() -> Result<&'static RecordBatch> {
static DUMMY_BATCH: std::sync::OnceLock<Result<RecordBatch>> =

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to compute the result once and then reuse it each time rather than create a new batch and array each time

std::sync::OnceLock::new();
DUMMY_BATCH
.get_or_init(|| {
// RecordBatch requires at least one column
let dummy_schema =
Arc::new(Schema::new(vec![Field::new("_", DataType::Null, true)]));
let col = new_null_array(&DataType::Null, 1);
Ok(RecordBatch::try_new(dummy_schema, vec![col])?)
})
.as_ref()
.map_err(|e| {
internal_datafusion_err!(
"Failed to create dummy batch for constant expression evaluation: {e}"
)
})
}

fn can_evaluate_as_constant(expr: &Arc<dyn PhysicalExpr>) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion datafusion/physical-expr/src/simplifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<'a> PhysicalExprSimplifier<'a> {
let rewritten = not::simplify_not_expr(node, schema)?
.transform_data(|node| unwrap_cast_in_comparison(node, schema))?
.transform_data(|node| {
const_evaluator::simplify_const_expr_immediate(node, &batch)
const_evaluator::simplify_const_expr_immediate(node, batch)
})?;

#[cfg(debug_assertions)]
Expand Down
Loading