You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by we...@apache.org on 2026/03/26 08:32:42 UTC

(spark) branch master updated: [SPARK-56206][SQL] Fix case-insensitive duplicate CTE name detection

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

wenchen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new 042440d45613 [SPARK-56206][SQL] Fix case-insensitive duplicate CTE name detection
042440d45613 is described below

commit 042440d4561376c3520cce5dea12672b2ac5c732
Author: ilicmarkodb <ma...@databricks.com>
AuthorDate: Thu Mar 26 16:32:24 2026 +0800

    [SPARK-56206][SQL] Fix case-insensitive duplicate CTE name detection
    
    ### What changes were proposed in this pull request?
    The `DUPLICATED_CTE_NAMES` validation in the parser uses case-sensitive string comparison via `groupBy(_._1)`, so CTE definitions that differ only in case (e.g., `WITH CTE AS (...), cte AS (...)`) bypass the duplicate check.
    
    This is incorrect because SQL identifiers are case-insensitive by default. The downstream Analyzer already treats CTE names case-insensitively (`IdentifierMap` normalizes keys via `toLowerCase(Locale.ROOT)`), so the second definition silently overwrites the first instead of raising an error.
    
    Fix: normalize CTE names to lowercase before grouping in the duplicate check.
    
    ### Why are the changes needed?
    Bug fix.
    
    ### Does this PR introduce _any_ user-facing change?
    Yes. Queries with duplicate CTE names that differ only in case will now correctly raise a `DUPLICATED_CTE_NAMES` error instead of silently succeeding.
    
    ### How was this patch tested?
    New test.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    Yes, Claude
    
    Closes #55009 from ilicmarkodb/fix-case-insensitive-cte-duplicate-check.
    
    Authored-by: ilicmarkodb <ma...@databricks.com>
    Signed-off-by: Wenchen Fan <we...@databricks.com>
---
 .../apache/spark/sql/catalyst/parser/AstBuilder.scala    |  2 +-
 .../spark/sql/catalyst/parser/PlanParserSuite.scala      | 16 +++++++++++++---
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala
index ab9b96104aac..a8f09367ba2f 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala
@@ -758,7 +758,7 @@ class AstBuilder extends DataTypeAstBuilder
       (namedQuery.alias, namedQuery, rowLevelLimit)
     }
     // Check for duplicate names.
-    val duplicates = ctes.groupBy(_._1).filter(_._2.size > 1).keys
+    val duplicates = ctes.groupBy(_._1.toLowerCase(Locale.ROOT)).filter(_._2.size > 1).keys
     if (duplicates.nonEmpty) {
       throw QueryParsingErrors.duplicateCteDefinitionNamesError(
         duplicates.map(toSQLId).mkString(", "), ctx)
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/PlanParserSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/PlanParserSuite.scala
index 4a38110b80f1..b659e8273d12 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/PlanParserSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/PlanParserSuite.scala
@@ -296,15 +296,25 @@ class PlanParserSuite extends AnalysisTest {
       cte(table("cte2").select(star()), false,
         "cte1" -> ((OneRowRelation().select(1), Seq.empty)),
         "cte2" -> ((table("cte1").select(star()), Seq.empty))))
-    val sql = "with cte1 (select 1), cte1 as (select 1 from cte1) select * from cte1"
+    val sql1 = "with cte1 (select 1), cte1 as (select 1 from cte1) select * from cte1"
     checkError(
-      exception = parseException(sql),
+      exception = parseException(sql1),
       condition = "DUPLICATED_CTE_NAMES",
       parameters = Map("duplicateNames" -> "`cte1`"),
       context = ExpectedContext(
-        fragment = sql,
+        fragment = sql1,
         start = 0,
         stop = 68))
+    // Case-insensitive duplicate CTE names should also be detected.
+    val sql2 = "with CTE1 (select 1), cte1 as (select 2) select * from cte1"
+    checkError(
+      exception = parseException(sql2),
+      condition = "DUPLICATED_CTE_NAMES",
+      parameters = Map("duplicateNames" -> "`cte1`"),
+      context = ExpectedContext(
+        fragment = sql2,
+        start = 0,
+        stop = 58))
   }
 
   test("simple select query") {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org