theirix commented on code in PR #19311: URL: https://github.com/apache/datafusion/pull/19311#discussion_r2646037566
########## 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: Agree. I've found a way to use an `ArrowPrimitiveType` and derive a native type from this trait. Now it looks much more compact with three parameters. -- 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]
