Skip to content
Merged
Changes from 3 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
64 changes: 54 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,22 @@ 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
.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),
});
}
}
self.parser.next_token(); // COPY
Expand All @@ -485,17 +504,39 @@ impl<'a> DFParser<'a> {
}
_ => {
// use sqlparser-rs parser
Ok(Statement::Statement(Box::from(
self.parser.parse_statement()?,
)))
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),
})
}
}
}
_ => {
// use the native parser
Ok(Statement::Statement(Box::from(
self.parser.parse_statement()?,
)))
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),
})
}
}
}
Expand Down Expand Up @@ -1760,6 +1801,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)"
);
}
}