alamb commented on code in PR #19311:
URL: https://github.com/apache/datafusion/pull/19311#discussion_r2617083271
##########
datafusion-examples/examples/relation_planner/table_sample.rs:
##########
@@ -454,40 +470,6 @@ impl RelationPlanner for TableSamplePlanner {
}
}
-/// Parse a SQL expression as a numeric value (supports basic arithmetic).
Review Comment:
@geoffreyclaude I wonder what your thoughts on this PR and the approach of
parse_sql_literal?
##########
datafusion/optimizer/src/simplify_expressions/simplify_sql_literal.rs:
##########
@@ -0,0 +1,253 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Parses and simplifies a SQL expression to a literal of a given type.
+//!
+//! This module provides functionality to parse and simplify static SQL
expressions
+//! used in SQL constructs like `FROM TABLE SAMPLE (10 + 50 * 2)`. If they are
required
+//! in a planning (not an execution) phase, they need to be reduced to
literals of a given type.
+
+use crate::simplify_expressions::ExprSimplifier;
+use arrow::datatypes::DataType;
+use datafusion_common::{
+ DFSchemaRef, DataFusionError, Result, ScalarValue, plan_datafusion_err,
plan_err,
+};
+use datafusion_expr::Expr;
+use datafusion_expr::execution_props::ExecutionProps;
+use datafusion_expr::planner::RelationPlannerContext;
+use datafusion_expr::simplify::SimplifyContext;
+use datafusion_expr::sqlparser::ast;
+use std::sync::Arc;
+
+/// Parse and simplifies a SQL expression to a numeric literal of a given type
`T`.
+///
+/// This function simplifies and coerces the expression, then extracts the
underlying
+/// native type using `TryFrom<ScalarValue>`.
+///
+/// # Arguments
+/// * `expr` - A logical AST expression
+/// * `target_type` - Arrow type to cast the literal to
+/// * `schema` - Schema reference for expression planning
+/// * `context` - `RelationPlannerContext` context
+///
+/// # Returns
+/// A `Result` containing a literal type
+///
+/// # Example
+/// ```ignore
+/// let value: f64 = parse_sql_literal(&expr, &DataType::Float64, &schema,
&mut relPlannerContext)?;
+/// ```
+pub fn parse_sql_literal<T>(
+ expr: &ast::Expr,
+ target_type: &DataType,
+ schema: &DFSchemaRef,
+ context: &mut dyn RelationPlannerContext,
+) -> Result<T>
+where
+ T: TryFrom<ScalarValue, Error = DataFusionError>,
+{
+ match context.sql_to_expr(expr.clone(), &Arc::clone(schema)) {
Review Comment:
it is a lot of ceremony to do this process
However, it also feels like this function is non trivial to use itself (it
has 4 parameters and a type parameter)
I wonder if we added a method like `SessionContext::simplify` to invoke the
simplifier more easily
Then extracting an expr as a particular type might look like
```rust
let expr = ...; // Expr to simplity
// cast to target type and simplify
let cast_expr = SessionContext::simplify(expr.cast(target_type)?);
// Check if it simplified to a type
let Expr::Literal(scalar) = cast_expr else {
// error parsing
};
// convert
```
--
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]