Apache DataFusion SQL Query Engine
Show current SQL recursion limit in RecursionLimitExceeded error message (#15644)
Lokesh392a9dd60c24
diff --git a/datafusion/sql/src/parser.rs b/datafusion/sql/src/parser.rsindex a5883783dc..27c897f7ad 100644--- a/datafusion/sql/src/parser.rs+++ b/datafusion/sql/src/parser.rs@@ -20,6 +20,7 @@//! This parser implements DataFusion specific statements such as//! `CREATE EXTERNAL TABLE`+use datafusion_common::config::SqlParserOptions;use datafusion_common::DataFusionError;use datafusion_common::{sql_err, Diagnostic, Span};use sqlparser::ast::{ExprWithAlias, OrderByOptions};@@ -284,6 +285,7 @@ fn ensure_not_set<T>(field: &Option<T>, name: &str) -> Result<(), DataFusionErro/// [`Statement`] for a list of this special syntaxpub struct DFParser<'a> {pub parser: Parser<'a>,+ options: SqlParserOptions,}/// Same as `sqlparser`@@ -366,6 +368,10 @@ impl<'a> DFParserBuilder<'a> {parser: Parser::new(self.dialect).with_tokens_with_locations(tokens).with_recursion_limit(self.recursion_limit),+ options: SqlParserOptions {+ recursion_limit: self.recursion_limit,+ ..Default::default()+ },})}}@@ -471,9 +477,7 @@ impl<'a> DFParser<'a> {if let Token::Word(w) = self.parser.peek_nth_token(1).token {// use native parser for COPY INTOif w.keyword == Keyword::INTO {- return Ok(Statement::Statement(Box::from(- self.parser.parse_statement()?,- )));+ return self.parse_and_handle_statement();}}self.parser.next_token(); // COPY@@ -485,17 +489,13 @@ impl<'a> DFParser<'a> {}_ => {// use sqlparser-rs parser- Ok(Statement::Statement(Box::from(- self.parser.parse_statement()?,- )))+ self.parse_and_handle_statement()}}}_ => {// use the native parser- Ok(Statement::Statement(Box::from(- self.parser.parse_statement()?,- )))+ self.parse_and_handle_statement()}}}@@ -513,6 +513,23 @@ impl<'a> DFParser<'a> {Ok(self.parser.parse_expr_with_alias()?)}+ /// Helper method to parse a statement and handle errors consistently, especially for recursion limits+ fn parse_and_handle_statement(&mut self) -> Result<Statement, DataFusionError> {+ self.parser+ .parse_statement()+ .map(|stmt| Statement::Statement(Box::from(stmt)))+ .map_err(|e| match e {+ ParserError::RecursionLimitExceeded => DataFusionError::SQL(+ ParserError::RecursionLimitExceeded,+ Some(format!(+ " (current limit: {})",+ self.options.recursion_limit+ )),+ ),+ other => DataFusionError::SQL(other, None),+ })+ }+/// Parse a SQL `COPY TO` statementpub fn parse_copy(&mut self) -> Result<Statement, DataFusionError> {// parse as a query@@ -1760,6 +1777,9 @@ mod tests {.parse_statements().unwrap_err();- assert_contains!(err.to_string(), "SQL error: RecursionLimitExceeded");+ assert_contains!(+ err.to_string(),+ "SQL error: RecursionLimitExceeded (current limit: 1)"+ );}}