Jefffrey commented on code in PR #19311:
URL: https://github.com/apache/datafusion/pull/19311#discussion_r2652315001


##########
datafusion/optimizer/src/simplify_expressions/simplify_sql_literal.rs:
##########
@@ -0,0 +1,247 @@
+// 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::ArrowPrimitiveType;
+use datafusion_common::{
+    DFSchema, 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,
+/// corresponding to an arrow primitive type `T` (for example, Float64Type).
+///
+/// This function simplifies and coerces the expression, then extracts the 
underlying
+/// native type using `TryFrom<ScalarValue>`.
+///
+/// # Arguments
+/// * `expr` - A logical AST expression
+/// * `schema` - Schema reference for expression planning
+/// * `context` - `RelationPlannerContext` context
+///
+/// # Returns
+/// A `Result` containing a literal type
+///
+/// # Example
+/// ```ignore
+/// let value: f64 = parse_sql_literal::<Float64Type>(&expr, &schema, &mut 
relPlannerContext)?;
+/// ```
+pub fn parse_sql_literal<T>(
+    expr: &ast::Expr,
+    context: &mut dyn RelationPlannerContext,
+) -> Result<T::Native>
+where
+    T: ArrowPrimitiveType,
+    <T as ArrowPrimitiveType>::Native: TryFrom<ScalarValue, Error = 
DataFusionError>,

Review Comment:
   ```suggestion
       T::Native: TryFrom<ScalarValue, Error = DataFusionError>,
   ```



##########
datafusion/optimizer/src/simplify_expressions/simplify_sql_literal.rs:
##########
@@ -0,0 +1,247 @@
+// 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::ArrowPrimitiveType;
+use datafusion_common::{
+    DFSchema, 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,
+/// corresponding to an arrow primitive type `T` (for example, Float64Type).
+///
+/// This function simplifies and coerces the expression, then extracts the 
underlying
+/// native type using `TryFrom<ScalarValue>`.
+///
+/// # Arguments
+/// * `expr` - A logical AST expression
+/// * `schema` - Schema reference for expression planning
+/// * `context` - `RelationPlannerContext` context
+///
+/// # Returns
+/// A `Result` containing a literal type

Review Comment:
   Can just remove these sections on arguments & return, they don't really 
provide much information



##########
datafusion/optimizer/src/simplify_expressions/simplify_sql_literal.rs:
##########
@@ -0,0 +1,247 @@
+// 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::ArrowPrimitiveType;
+use datafusion_common::{
+    DFSchema, 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,
+/// corresponding to an arrow primitive type `T` (for example, Float64Type).
+///
+/// This function simplifies and coerces the expression, then extracts the 
underlying
+/// native type using `TryFrom<ScalarValue>`.
+///
+/// # Arguments
+/// * `expr` - A logical AST expression
+/// * `schema` - Schema reference for expression planning
+/// * `context` - `RelationPlannerContext` context
+///
+/// # Returns
+/// A `Result` containing a literal type
+///
+/// # Example
+/// ```ignore
+/// let value: f64 = parse_sql_literal::<Float64Type>(&expr, &schema, &mut 
relPlannerContext)?;
+/// ```
+pub fn parse_sql_literal<T>(
+    expr: &ast::Expr,
+    context: &mut dyn RelationPlannerContext,
+) -> Result<T::Native>
+where
+    T: ArrowPrimitiveType,
+    <T as ArrowPrimitiveType>::Native: TryFrom<ScalarValue, Error = 
DataFusionError>,
+{
+    // Empty schema is sufficient because it parses only literal expressions
+    let schema = DFSchemaRef::new(DFSchema::empty());
+
+    match context.sql_to_expr(expr.clone(), &schema) {

Review Comment:
   This is the only usage of `context`; would it make more sense to pull 
`context` out of this function and only require an `Expr` input? So it would be 
up to the callers to do `context.sql_to_expr()`, which means we can omit need 
for a sql feature in the optimizer crate



-- 
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]

Reply via email to