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 2022/06/15 08:03:19 UTC

[spark] branch branch-3.3 updated: [SPARK-39355][SQL] Single column uses quoted to construct UnresolvedAttribute

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

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


The following commit(s) were added to refs/heads/branch-3.3 by this push:
     new 2078838adff [SPARK-39355][SQL] Single column uses quoted to construct UnresolvedAttribute
2078838adff is described below

commit 2078838adff6730bdb6db5337ee67f2efaf9153e
Author: sychen <sy...@ctrip.com>
AuthorDate: Wed Jun 15 16:02:46 2022 +0800

    [SPARK-39355][SQL] Single column uses quoted to construct UnresolvedAttribute
    
    ### What changes were proposed in this pull request?
    Use `UnresolvedAttribute.quoted` in `Alias.toAttribute` to avoid calling `UnresolvedAttribute.apply` causing `ParseException`.
    
    ### Why are the changes needed?
    ```sql
    SELECT *
    FROM (
        SELECT '2022-06-01' AS c1
    ) a
    WHERE c1 IN (
        SELECT date_add('2022-06-01', 0)
    );
    ```
    ```
    Error in query:
    mismatched input '(' expecting {<EOF>, '.', '-'}(line 1, pos 8)
    == SQL ==
    date_add(2022-06-01, 0)
    --------^^^
    ```
    
    ### Does this PR introduce _any_ user-facing change?
    No
    
    ### How was this patch tested?
    add UT
    
    Closes #36740 from cxzl25/SPARK-39355.
    
    Authored-by: sychen <sy...@ctrip.com>
    Signed-off-by: Wenchen Fan <we...@databricks.com>
    (cherry picked from commit 8731cb875d075b68e4e0cb1d1eb970725eab9cf9)
    Signed-off-by: Wenchen Fan <we...@databricks.com>
---
 .../expressions/aggregate/interfaces.scala         |  2 +-
 .../catalyst/expressions/namedExpressions.scala    |  2 +-
 .../scala/org/apache/spark/sql/SubquerySuite.scala | 28 ++++++++++++++++++++++
 3 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/interfaces.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/interfaces.scala
index f97293dc9b4..e60c07b0d82 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/interfaces.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/interfaces.scala
@@ -117,7 +117,7 @@ case class AggregateExpression(
     // This is a bit of a hack.  Really we should not be constructing this container and reasoning
     // about datatypes / aggregation mode until after we have finished analysis and made it to
     // planning.
-    UnresolvedAttribute(aggregateFunction.toString)
+    UnresolvedAttribute.quoted(aggregateFunction.toString)
   }
 
   def filterAttributes: AttributeSet = filter.map(_.references).getOrElse(AttributeSet.empty)
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/namedExpressions.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/namedExpressions.scala
index 47cdf21a872..145f371301f 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/namedExpressions.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/namedExpressions.scala
@@ -193,7 +193,7 @@ case class Alias(child: Expression, name: String)(
     if (resolved) {
       AttributeReference(name, child.dataType, child.nullable, metadata)(exprId, qualifier)
     } else {
-      UnresolvedAttribute(name)
+      UnresolvedAttribute.quoted(name)
     }
   }
 
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala
index 5a1ea6ea29e..e8ddf93afc3 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala
@@ -2185,4 +2185,32 @@ class SubquerySuite extends QueryTest with SharedSparkSession with AdaptiveSpark
       }
     }
   }
+
+  test("SPARK-39355: Single column uses quoted to construct UnresolvedAttribute") {
+    checkAnswer(
+      sql("""
+            |SELECT *
+            |FROM (
+            |    SELECT '2022-06-01' AS c1
+            |) a
+            |WHERE c1 IN (
+            |     SELECT date_add('2022-06-01', 0)
+            |)
+            |""".stripMargin),
+      Row("2022-06-01"))
+    checkAnswer(
+      sql("""
+            |SELECT *
+            |FROM (
+            |    SELECT '2022-06-01' AS c1
+            |) a
+            |WHERE c1 IN (
+            |    SELECT date_add(a.c1.k1, 0)
+            |    FROM (
+            |        SELECT named_struct('k1', '2022-06-01') AS c1
+            |    ) a
+            |)
+            |""".stripMargin),
+      Row("2022-06-01"))
+  }
 }


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