Jefffrey commented on code in PR #19311:
URL: https://github.com/apache/datafusion/pull/19311#discussion_r2645869751
##########
datafusion-examples/examples/relation_planner/main.rs:
##########
@@ -90,7 +90,7 @@ async fn main() -> Result<()> {
let example: ExampleKind = std::env::args()
.nth(1)
- .ok_or_else(|| DataFusionError::Execution(format!("Missing argument.
{usage}")))?
+ .unwrap_or_else(|| ExampleKind::All.to_string())
Review Comment:
This doesn't seem consistent with the other example groups
##########
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:
I do wonder if there's a way to compact this signature; for example, having
both `T` and `target_type` seem redundant since they'd need to be compatible
anyway, but off the top of my head I can't think of a way to combine them 🤔
--
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]