Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support bounds evaluation for temporal data types #14523

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions datafusion-examples/examples/expr_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use arrow::record_batch::RecordBatch;
use datafusion::arrow::datatypes::{DataType, Field, Schema, TimeUnit};
use datafusion::common::stats::Precision;
use datafusion::common::tree_node::{Transformed, TreeNode};
use datafusion::common::{ColumnStatistics, DFSchema};
use datafusion::common::{internal_datafusion_err, ColumnStatistics, DFSchema};
use datafusion::common::{ScalarValue, ToDFSchema};
use datafusion::error::Result;
use datafusion::functions_aggregate::first_last::first_value_udaf;
Expand Down Expand Up @@ -302,10 +302,17 @@ fn boundary_analysis_and_selectivity_demo() -> Result<()> {
distinct_count: Precision::Absent,
};

let field = schema.fields().first().ok_or_else(|| {
internal_datafusion_err!("schema does not have a field at index 0")
})?;

// We can then build our expression boundaries from the column statistics
// allowing the analysis to be more precise.
let initial_boundaries =
vec![ExprBoundaries::try_from_column(&schema, &column_stats, 0)?];
let initial_boundaries = vec![ExprBoundaries::try_from_column(
field.as_ref(),
&column_stats,
0,
)?];

// With the above we can perform the boundary analysis similar to the previous
// example.
Expand Down
11 changes: 11 additions & 0 deletions datafusion/common/src/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,17 @@ impl ScalarValue {
}
}

/// Returns negation for a boolean scalar value
pub fn boolean_negate(&self) -> Result<Self> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can extend arithmetic_negate() with booleans, wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can do that, but I think we should then rename arithmetic_negate to just negate to cause no ambiguity. WDYT?

match self {
ScalarValue::Boolean(None) => Ok(self.clone()),
ScalarValue::Boolean(Some(value)) => Ok(ScalarValue::Boolean(Some(!value))),
value => {
_internal_err!("Can not run boolean negative on scalar value {value:?}")
}
}
}

/// Wrapping addition of `ScalarValue`
///
/// NB: operating on `ScalarValue` directly is not efficient, performance sensitive code
Expand Down
2 changes: 1 addition & 1 deletion datafusion/common/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl Precision<usize> {
/// rows are selected. A selectivity of `0.5` means half the rows are
/// selected. Will always return inexact statistics.
pub fn with_estimated_selectivity(self, selectivity: f64) -> Self {
self.map(|v| ((v as f64 * selectivity).ceil()) as usize)
self.map(|v| (v as f64 * selectivity).ceil() as usize)
.to_inexact()
}
}
Expand Down
9 changes: 4 additions & 5 deletions datafusion/core/tests/physical_optimizer/join_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use datafusion_execution::{RecordBatchStream, SendableRecordBatchStream, TaskCon
use datafusion_expr::Operator;
use datafusion_physical_expr::expressions::col;
use datafusion_physical_expr::expressions::{BinaryExpr, Column, NegativeExpr};
use datafusion_physical_expr::intervals::utils::check_support;
use datafusion_physical_expr::PhysicalExprRef;
use datafusion_physical_expr::{EquivalenceProperties, Partitioning, PhysicalExpr};
use datafusion_physical_optimizer::join_selection::{
Expand Down Expand Up @@ -1071,21 +1070,21 @@ fn check_expr_supported() {
Operator::Plus,
Arc::new(Column::new("a", 0)),
)) as Arc<dyn PhysicalExpr>;
assert!(check_support(&supported_expr, &schema));
assert!(&supported_expr.supports_bounds_evaluation(&schema));
let supported_expr_2 = Arc::new(Column::new("a", 0)) as Arc<dyn PhysicalExpr>;
assert!(check_support(&supported_expr_2, &schema));
assert!(&supported_expr_2.supports_bounds_evaluation(&schema));
let unsupported_expr = Arc::new(BinaryExpr::new(
Arc::new(Column::new("a", 0)),
Operator::Or,
Arc::new(Column::new("a", 0)),
)) as Arc<dyn PhysicalExpr>;
assert!(!check_support(&unsupported_expr, &schema));
assert!(!&unsupported_expr.supports_bounds_evaluation(&schema));
let unsupported_expr_2 = Arc::new(BinaryExpr::new(
Arc::new(Column::new("a", 0)),
Operator::Or,
Arc::new(NegativeExpr::new(Arc::new(Column::new("a", 0)))),
)) as Arc<dyn PhysicalExpr>;
assert!(!check_support(&unsupported_expr_2, &schema));
assert!(!&unsupported_expr_2.supports_bounds_evaluation(&schema));
}

struct TestCase {
Expand Down
3 changes: 3 additions & 0 deletions datafusion/expr-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ datafusion-common = { workspace = true }
indexmap = { workspace = true }
itertools = { workspace = true }
paste = "^1.0"

[dev-dependencies]
arrow-buffer = { workspace = true }
Loading