Skip to content

[SPARK] Handle special characters correctly in validateDataSkippingType #4531

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

Merged
merged 3 commits into from
May 14, 2025
Merged
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 @@ -47,6 +47,7 @@ import org.apache.spark.sql.catalyst.catalog.CatalogTable
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.parser.{AbstractSqlParser, AstBuilder, ParseException, ParserUtils}
import org.apache.spark.sql.catalyst.parser.SqlBaseParser.MultipartIdentifierListContext
import org.apache.spark.sql.catalyst.util.quoteIfNeeded
import org.apache.spark.sql.functions._
import org.apache.spark.sql.functions.lit
import org.apache.spark.sql.internal.SQLConf
Expand Down Expand Up @@ -477,10 +478,18 @@ object StatisticsCollection extends DeltaCommand {
insideStruct: Boolean = false): Unit = dataType match {
case s: StructType =>
s.foreach { field =>
validateDataSkippingType(name + "." + field.name, field.dataType, columnPaths,
insideStruct = true)
// we need to make sure we quote the field if needed otherwise we will not handle
// column names with special characters correctly.
validateDataSkippingType(name + "." +
quoteIfNeeded(field.name), field.dataType, columnPaths, insideStruct = true)
}
case SkippingEligibleDataType(_) =>
if (insideStruct) {
// If this is inside the struct we are already quoting the nested field name.
columnPaths.append(name)
} else {
columnPaths.append(quoteIfNeeded(name))
}
case SkippingEligibleDataType(_) => columnPaths.append(name)
case _ if insideStruct =>
logWarning(s"Data skipping is not supported for column $name of type $dataType")
columnPaths.append(name)
Expand Down Expand Up @@ -517,7 +526,15 @@ object StatisticsCollection extends DeltaCommand {
transformSchema(schema, Some(columnName.head)) {
case (`prefixPath`, struct @ StructType(_), _) =>
val columnField = struct(columnName.head)
validateDataSkippingType(columnAttribute.name, columnField.dataType, visitedColumns)
// We need to figure out if the column is top-level column
// or a column inside a struct, we support collecting null count stats
// on nested columns part of a struct.
val fieldInsideStruct = prefixPath.size > 0
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix this bug as well, we were not correctly handling nested columns in the root schema

validateDataSkippingType(
columnAttribute.name,
columnField.dataType,
visitedColumns,
insideStruct = fieldInsideStruct)
struct
case (_, other, _) => other
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,32 +526,6 @@ class StatsCollectionSuite
)
}
}

test(s"Delta statistic column: invalid data type $invalidType in nested column") {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

withTable(tableName1, tableName2) {
val columnName = "c2.c21"
val exceptOne = intercept[DeltaIllegalArgumentException] {
sql(
s"create table $tableName1 (c1 long, c2 STRUCT<c20:INT, c21:$invalidType>) " +
s"using delta TBLPROPERTIES('delta.dataSkippingStatsColumns' = 'c2.c21')"
)
}
assert(
exceptOne.getErrorClass == "DELTA_COLUMN_DATA_SKIPPING_NOT_SUPPORTED_TYPE" &&
exceptOne.getMessageParametersArray.toSeq == Seq(columnName, typename)
)
sql(s"create table $tableName2 (c1 long, c2 STRUCT<c20:INT, c21:$invalidType>) using delta")
val exceptThree = interceptWithUnwrapping[DeltaIllegalArgumentException] {
sql(
s"ALTER TABLE $tableName2 SET TBLPROPERTIES('delta.dataSkippingStatsColumns'='c2.c21')"
)
}
assert(
exceptThree.getErrorClass == "DELTA_COLUMN_DATA_SKIPPING_NOT_SUPPORTED_TYPE" &&
exceptThree.getMessageParametersArray.toSeq == Seq(columnName, typename)
)
}
}
}

test(s"Delta statistic column: mix case column name") {
Expand Down Expand Up @@ -858,6 +832,21 @@ class StatsCollectionSuite
}
}

test("handle special nested characters in column name") {
withTable("t") {
sql(
s"create table t (`|` long, c struct<s struct<a int, b INT>, `s.a` int>) using delta " +
s"TBLPROPERTIES('delta.columnMapping.mode' = 'name')")
// Have this test to make sure there are no collisions with c.`s.a` and c.s.a.
sql(
s"ALTER TABLE t SET TBLPROPERTIES('delta.dataSkippingStatsColumns' = 'c')")
val deltaLog = DeltaLog.forTable(spark, TableIdentifier("t"))
val tblProperty = DeltaConfigs.DATA_SKIPPING_STATS_COLUMNS
.fromMetaData(deltaLog.update().metadata)
assert(tblProperty.get == "c")
}
}

private def recordsScanned(df: DataFrame): Long = {
val scan = df.queryExecution.executedPlan.find {
case FileScanExecNode(_) => true
Expand Down
Loading