This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git


The following commit(s) were added to refs/heads/master by this push:
     new 4a2453a  fix a cte block with same name for many times (#1639)
4a2453a is described below

commit 4a2453a291a33bf3864509cdcca183f94fdba306
Author: xudong.w <[email protected]>
AuthorDate: Sun Jan 23 18:06:21 2022 +0800

    fix a cte block with same name for many times (#1639)
---
 datafusion/src/sql/planner.rs | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/datafusion/src/sql/planner.rs b/datafusion/src/sql/planner.rs
index 8be5d7c..f060044 100644
--- a/datafusion/src/sql/planner.rs
+++ b/datafusion/src/sql/planner.rs
@@ -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,
@@ -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> {

Reply via email to