Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix a cte block with same name for many times #1639

Merged
merged 1 commit into from
Jan 23, 2022
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
16 changes: 16 additions & 0 deletions datafusion/src/sql/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
// Process CTEs from top to bottom
// do not allow self-references
for cte in &with.cte_tables {
// A `WITH` block can't use the same name for many times
let cte_name: &str = cte.alias.name.value.as_ref();
if ctes.contains_key(cte_name) {
return Err(DataFusionError::SQL(ParserError(format!(
"WITH query name {:?} specified more than once",
cte_name
))));
}
// create logical plan & pass backreferencing CTEs
let logical_plan = self.query_to_plan_with_alias(
&cte.query,
Expand Down Expand Up @@ -3882,6 +3890,14 @@ mod tests {
\n TableScan: lineitem projection=None";
quick_test(sql, expected);
}

#[test]
fn cte_use_same_name_multiple_times() {
let sql = "with a as (select * from person), a as (select * from orders) select * from a;";
let expected = "SQL error: ParserError(\"WITH query name \\\"a\\\" specified more than once\")";
let result = logical_plan(sql).err().unwrap();
assert_eq!(expected, format!("{}", result));
}
}

fn parse_sql_number(n: &str) -> Result<Expr> {
Expand Down