theirix commented on code in PR #19311: URL: https://github.com/apache/datafusion/pull/19311#discussion_r2617305187
########## 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: That's true. Its purpose is to simplify expression and convert to a native type at the same time, hence the five parameters and the things behind the scenes (simplify, coerce, arrow cast and arrow-to-native cast) to shift it from client code. For other usages of the optimiser's method - I agree that simplification could be done more easily in the client code via facade methods in context, as you suggested. -- 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]
