@@ -86,29 +86,15 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
8686 }
8787
8888 let skip = match skip {
89- Some ( skip_expr) => {
90- let expr = self . sql_to_expr (
91- skip_expr. value ,
92- input. schema ( ) ,
93- & mut PlannerContext :: new ( ) ,
94- ) ?;
95- let n = get_constant_result ( & expr, "OFFSET" ) ?;
96- convert_usize_with_check ( n, "OFFSET" )
97- }
89+ Some ( skip_expr) => self . get_constant_usize_result ( skip_expr. value , input. schema ( ) , "OFFSET" ) ,
9890 _ => Ok ( 0 ) ,
9991 } ?;
10092
10193 let fetch = match fetch {
10294 Some ( limit_expr)
10395 if limit_expr != sqlparser:: ast:: Expr :: Value ( Value :: Null ) =>
10496 {
105- let expr = self . sql_to_expr (
106- limit_expr,
107- input. schema ( ) ,
108- & mut PlannerContext :: new ( ) ,
109- ) ?;
110- let n = get_constant_result ( & expr, "LIMIT" ) ?;
111- Some ( convert_usize_with_check ( n, "LIMIT" ) ?)
97+ Some ( self . get_constant_usize_result ( limit_expr, input. schema ( ) , "LIMIT" ) ?)
11298 }
11399 _ => None ,
114100 } ;
@@ -156,6 +142,28 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
156142 _ => Ok ( plan) ,
157143 }
158144 }
145+
146+ /// Retrieves the constant usize result of an SQL expression, evaluating it if possible.
147+ ///
148+ /// This function takes an SQL expression, a related scheme and an argument name as input and returns
149+ /// a `Result<usize>` indicating either the constant result of the expression or an
150+ /// error if the expression cannot be evaluated.
151+ ///
152+ /// # Arguments
153+ ///
154+ /// * `expr` - An `SQLExpr` representing the expression to evaluate.
155+ /// * `schema` - A related DataFusion schema to apply while converting an `expr` into a logical expression.
156+ /// * `arg_name` - The name of the argument for error messages.
157+ ///
158+ /// # Returns
159+ ///
160+ /// * `Result<usize>` - An `Ok` variant containing the constant result if evaluation is successful,
161+ /// or an `Err` variant containing an error message if evaluation fails.
162+ pub ( super ) fn get_constant_usize_result ( & self , expr : SQLExpr , schema : & datafusion_common:: DFSchema , arg_name : & str ) -> Result < usize > {
163+ let expr = self . sql_to_expr ( expr, schema, & mut PlannerContext :: new ( ) ) ?;
164+ let value = get_constant_result ( & expr, arg_name) ?;
165+ convert_usize_with_check ( value, arg_name)
166+ }
159167}
160168
161169/// Retrieves the constant result of an expression, evaluating it if possible.
0 commit comments