Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions datafusion/sql/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -284,6 +285,7 @@ fn ensure_not_set<T>(field: &Option<T>, name: &str) -> Result<(), DataFusionErro
/// [`Statement`] for a list of this special syntax
pub struct DFParser<'a> {
pub parser: Parser<'a>,
options: SqlParserOptions,
}

/// Same as `sqlparser`
Expand Down Expand Up @@ -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()
},
})
}
}
Expand Down Expand Up @@ -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 INTO
if 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
Expand All @@ -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()
}
}
}
Expand All @@ -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` statement
pub fn parse_copy(&mut self) -> Result<Statement, DataFusionError> {
// parse as a query
Expand Down Expand Up @@ -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)"
);
}
}