Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-40697][SQL][FOLLOWUP] Read-side char padding should only be applied if necessary #38479

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ object CharVarcharUtils extends Logging {
Seq(Literal(f.name), processStringForCharVarchar(
GetStructField(expr, i, Some(f.name)), f.dataType, charFuncName, varcharFuncName))
})
if (expr.nullable) {
if (struct.valExprs.forall(_.isInstanceOf[GetStructField])) {
// No field needs char/varchar processing, just return the original expression.
expr
} else if (expr.nullable) {
If(IsNull(expr), Literal(null, struct.dataType), struct)
} else {
struct
Expand All @@ -225,11 +228,18 @@ object CharVarcharUtils extends Logging {
processStringForCharVarcharInArray(expr, et, containsNull, charFuncName, varcharFuncName)

case MapType(kt, vt, valueContainsNull) =>
val keys = MapKeys(expr)
val newKeys = processStringForCharVarcharInArray(
MapKeys(expr), kt, containsNull = false, charFuncName, varcharFuncName)
keys, kt, containsNull = false, charFuncName, varcharFuncName)
val values = MapValues(expr)
val newValues = processStringForCharVarcharInArray(
MapValues(expr), vt, valueContainsNull, charFuncName, varcharFuncName)
MapFromArrays(newKeys, newValues)
values, vt, valueContainsNull, charFuncName, varcharFuncName)
if (newKeys.fastEquals(keys) && newValues.fastEquals(values)) {
// If map key/value does not need char/varchar processing, return the original expression.
expr
} else {
MapFromArrays(newKeys, newValues)
}

case _ => expr
}
Expand All @@ -242,10 +252,13 @@ object CharVarcharUtils extends Logging {
charFuncName: Option[String],
varcharFuncName: Option[String]): Expression = {
val param = NamedLambdaVariable("x", replaceCharVarcharWithString(et), containsNull)
val func = LambdaFunction(
processStringForCharVarchar(param, et, charFuncName, varcharFuncName),
Seq(param))
ArrayTransform(arr, func)
val funcBody = processStringForCharVarchar(param, et, charFuncName, varcharFuncName)
if (funcBody.fastEquals(param)) {
// If array element does not need char/varchar processing, return the original expression.
arr
} else {
ArrayTransform(arr, LambdaFunction(funcBody, Seq(param)))
}
}

def addPaddingForScan(attr: Attribute): Expression = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
package org.apache.spark.sql

import org.apache.spark.{SparkConf, SparkException}
import org.apache.spark.sql.catalyst.expressions.Attribute
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
import org.apache.spark.sql.connector.SchemaRequiredDataSource
import org.apache.spark.sql.connector.catalog.InMemoryPartitionTableCatalog
Expand Down Expand Up @@ -862,6 +864,26 @@ class FileSourceCharVarcharTestSuite extends CharVarcharTestSuite with SharedSpa
}
}
}

test("SPARK-40697: read-side char padding should only be applied if necessary") {
withTable("t") {
sql(
s"""
|CREATE TABLE t (
| c1 CHAR(5),
| c2 STRUCT<i VARCHAR(5)>,
| c3 ARRAY<VARCHAR(5)>,
| c4 MAP<INT, VARCHAR(5)>
|) USING $format
|""".stripMargin)
spark.read.table("t").queryExecution.analyzed.foreach {
case Project(projectList, _) =>
assert(projectList.length == 4)
assert(projectList.drop(1).forall(_.isInstanceOf[Attribute]))
case _ =>
}
}
}
}

class DSV2CharVarcharTestSuite extends CharVarcharTestSuite
Expand Down