Skip to content

Commit

Permalink
fix a cte block with same name for many times (#1639)
Browse files Browse the repository at this point in the history
  • Loading branch information
xudong963 authored Jan 23, 2022
1 parent 71757bb commit 4a2453a
Showing 1 changed file with 16 additions and 0 deletions.
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

0 comments on commit 4a2453a

Please sign in to comment.