This is an automated email from the ASF dual-hosted git repository.
maxgekk pushed a commit to branch branch-4.0
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-4.0 by this push:
new 3df89a4a2515 [SPARK-50847][SQL] Deny ApplyCharTypePadding from
applying on specific In expressions
3df89a4a2515 is described below
commit 3df89a4a25155eeddfc4a5b6a74ba34f64885db0
Author: mihailoale-db <[email protected]>
AuthorDate: Mon Jan 20 11:16:22 2025 +0200
[SPARK-50847][SQL] Deny ApplyCharTypePadding from applying on specific In
expressions
### What changes were proposed in this pull request?
`ApplyCharTypePadding` rule shouldn't be applied to `In` expressions where
elements of the `In.list` are not `StringType`s and in this PR we fix it.
### Why are the changes needed?
Users should get a proper `DATATYPE_MISMATCH.DATA_DIFF_TYPES` error message
instead of an `java.lang.ClassCastException` internal one.
### Does this PR introduce _any_ user-facing change?
Users will now get a proper message instead of an internal one.
### How was this patch tested?
Added test.
### Was this patch authored or co-authored using generative AI tooling?
No.
Closes #49526 from mihailoale-db/mihailo-aleksic_data/fixinpadding.
Authored-by: mihailoale-db <[email protected]>
Signed-off-by: Max Gekk <[email protected]>
(cherry picked from commit 29c77ddac93a4a9cfbae69c7effd2015e1f93cee)
Signed-off-by: Max Gekk <[email protected]>
---
.../analysis/ApplyCharTypePaddingHelper.scala | 2 +-
.../org/apache/spark/sql/CharVarcharTestSuite.scala | 19 +++++++++++++++++++
2 files changed, 20 insertions(+), 1 deletion(-)
diff --git
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ApplyCharTypePaddingHelper.scala
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ApplyCharTypePaddingHelper.scala
index 54f9abe0b9f1..8df977c80921 100644
---
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ApplyCharTypePaddingHelper.scala
+++
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ApplyCharTypePaddingHelper.scala
@@ -99,7 +99,7 @@ object ApplyCharTypePaddingHelper {
.getOrElse(b)
case i @ In(e @ AttrOrOuterRef(attr), list)
- if attr.dataType == StringType && list.forall(_.foldable) =>
+ if i.resolved && attr.dataType == StringType &&
list.forall(_.foldable) =>
CharVarcharUtils
.getRawType(attr.metadata)
.flatMap {
diff --git
a/sql/core/src/test/scala/org/apache/spark/sql/CharVarcharTestSuite.scala
b/sql/core/src/test/scala/org/apache/spark/sql/CharVarcharTestSuite.scala
index 47ebd387e89a..6a96431d5708 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/CharVarcharTestSuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/CharVarcharTestSuite.scala
@@ -19,6 +19,7 @@ package org.apache.spark.sql
import org.apache.spark.{SparkConf, SparkRuntimeException}
import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.sql.catalyst.expressions.Cast.toSQLId
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
import org.apache.spark.sql.catalyst.plans.logical.Project
import org.apache.spark.sql.catalyst.util.CharVarcharUtils
@@ -779,6 +780,24 @@ trait CharVarcharTestSuite extends QueryTest with
SQLTestUtils {
}
}
}
+
+ test("SPARK-50847: Deny ApplyCharTypePadding from applying on specific In
expressions") {
+ withTable("mytable") {
+ sql(s"CREATE TABLE mytable(col CHAR(10)) USING $format")
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql("SELECT * FROM mytable where col IN (ARRAY('a'))")
+ },
+ condition = "DATATYPE_MISMATCH.DATA_DIFF_TYPES",
+ parameters = Map(
+ "functionName" -> toSQLId("in"),
+ "dataType" -> "[\"STRING\", \"ARRAY<STRING>\"]",
+ "sqlExpr" -> s""""(col IN (array(a)))""""
+ ),
+ queryContext = Array(ExpectedContext(fragment = "IN (ARRAY('a'))",
start = 32, stop = 46))
+ )
+ }
+ }
}
// Some basic char/varchar tests which doesn't rely on table implementation.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]