viirya commented on code in PR #60380:
URL: https://github.com/apache/spark/pull/60380#discussion_r4553445531
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/ApplyCharTypePadding.scala:
##########
@@ -102,25 +102,30 @@ object ApplyCharTypePadding extends Rule[LogicalPlan] {
}
/**
- * A DELETE that is not rewritten as a row-level operation deletes with
filters translated from
- * its condition, which the data source compares with the stored values. To
match what a read of
- * the table returns, the condition evaluates the read-side padding of the
columns itself, after
- * its comparisons are padded. The data source can only delete with such a
filter if it supports
- * the padding, e.g. `CHAR_READ_SIDE_PADDING`.
+ * A DELETE that is not rewritten as a row-level operation, and an overwrite
by expression such
+ * as `INSERT INTO ... REPLACE WHERE`, delete with filters translated from
their condition, which
+ * the data source compares with the stored values. To match what a read of
the table returns,
+ * the condition evaluates the read-side padding of the columns itself,
after its comparisons are
+ * padded. The data source can only delete with such a filter if it supports
the padding, e.g.
+ * `CHAR_READ_SIDE_PADDING`.
*/
private def padDeleteConditions(plan: LogicalPlan): LogicalPlan = {
- plan.resolveOperatorsWithPruning(_.containsPattern(DELETE_FROM_TABLE)) {
- case d: DeleteFromTable =>
- d.copy(condition = d.condition.transform {
- // the padded value refers to the attribute without the raw type and
is not padded again
- case attr: AttributeReference if
CharVarcharUtils.getRawType(attr.metadata).isDefined =>
- CharVarcharUtils.addPaddingForScan(attr).transform {
- case a: AttributeReference =>
CharVarcharUtils.cleanAttrMetadata(a)
- }
- })
+ plan.resolveOperatorsWithPruning(_.containsPattern(COMMAND)) {
+ case d: DeleteFromTable => d.copy(condition =
padRawTypedColumns(d.condition))
+ case o: OverwriteByExpression
+ if
!conf.getConf(SQLConf.LEGACY_NO_CHAR_PADDING_IN_OVERWRITE_CONDITION) =>
+ o.copy(deleteExpr = padRawTypedColumns(o.deleteExpr))
}
}
+ private def padRawTypedColumns(condition: Expression): Expression =
condition.transform {
+ // the padded value refers to the attribute without the raw type and is
not padded again
+ case attr: AttributeReference if
CharVarcharUtils.getRawType(attr.metadata).isDefined =>
+ CharVarcharUtils.addPaddingForScan(attr).transform {
Review Comment:
Under `spark.sql.charVarchar.standardSemantics.enabled=true` this wraps the
column in `charTypeReadSideCheck` / `varcharTypeReadSideCheck`, which
`V2ExpressionBuilder` cannot translate. `INSERT OVERWRITE t PARTITION (dep =
'hr')` on a `VARCHAR(8)` or `CHAR(8)` partition column then fails with
```
cannot translate expression to source filter:
(static_invoke(CharVarcharCodegenUtils.varcharTypeReadSideCheck(dep, 8)) <=>
hr)
```
It works without this change, and no connector can opt in, including the
test table with `accept-char-read-side-padding`. VARCHAR has no padding to
apply, so should the overwrite condition skip the wrap under standard
semantics, or at least for VARCHAR? Otherwise the migration guide entry, which
says it applies when standard semantics is off, needs to cover this case too.
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala:
##########
@@ -232,6 +232,56 @@ abstract class DataSourceV2SQLSuite
}
}
+ test("SPARK-61512: REPLACE WHERE with a condition on a CHAR column") {
+ val t = s"${catalogAndNamespace}tbl"
+ withTable(t) {
+ sql(s"CREATE TABLE $t (id bigint, dep char(8)) USING foo PARTITIONED BY
(dep)")
+ withSQLConf(SQLConf.LEGACY_CHAR_VARCHAR_AS_STRING.key -> "true") {
+ sql(s"INSERT INTO $t VALUES (1L, 'hr')")
+ }
+ // a read pads the values stored without padding, so the source must not
delete them by
+ // comparing the stored values with the padded literal, which would not
match
+ checkError(
+ exception = intercept[SparkException] {
+ sql(s"INSERT INTO $t REPLACE WHERE dep = 'hr' VALUES (2L, 'hr')")
+ },
+ condition = "_LEGACY_ERROR_TEMP_2245",
+ parameters = Map("table" -> ".*"),
+ matchPVals = true)
+ checkAnswer(spark.table(t), Row(1L, "hr "))
+ }
+ }
+
+ test("SPARK-61512: REPLACE WHERE with a CHAR condition under the legacy
config") {
+ val t = s"${catalogAndNamespace}tbl"
+ withTable(t) {
+ sql(s"CREATE TABLE $t (id bigint, dep char(8)) USING foo PARTITIONED BY
(dep)")
+ sql(s"INSERT INTO $t VALUES (1L, 'hr')")
Review Comment:
Could this also assert that the same statement fails with
`_LEGACY_ERROR_TEMP_2245` before the config is set? The migration guide
documents that failure on padded data, but no test pins it.
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala:
##########
@@ -232,6 +232,56 @@ abstract class DataSourceV2SQLSuite
}
}
+ test("SPARK-61512: REPLACE WHERE with a condition on a CHAR column") {
+ val t = s"${catalogAndNamespace}tbl"
+ withTable(t) {
+ sql(s"CREATE TABLE $t (id bigint, dep char(8)) USING foo PARTITIONED BY
(dep)")
+ withSQLConf(SQLConf.LEGACY_CHAR_VARCHAR_AS_STRING.key -> "true") {
+ sql(s"INSERT INTO $t VALUES (1L, 'hr')")
+ }
+ // a read pads the values stored without padding, so the source must not
delete them by
+ // comparing the stored values with the padded literal, which would not
match
+ checkError(
+ exception = intercept[SparkException] {
+ sql(s"INSERT INTO $t REPLACE WHERE dep = 'hr' VALUES (2L, 'hr')")
+ },
+ condition = "_LEGACY_ERROR_TEMP_2245",
+ parameters = Map("table" -> ".*"),
+ matchPVals = true)
+ checkAnswer(spark.table(t), Row(1L, "hr "))
+ }
+ }
+
+ test("SPARK-61512: REPLACE WHERE with a CHAR condition under the legacy
config") {
+ val t = s"${catalogAndNamespace}tbl"
+ withTable(t) {
+ sql(s"CREATE TABLE $t (id bigint, dep char(8)) USING foo PARTITIONED BY
(dep)")
+ sql(s"INSERT INTO $t VALUES (1L, 'hr')")
+ // the source compares the stored values with the padded literal, as
before
+ withSQLConf(SQLConf.LEGACY_NO_CHAR_PADDING_IN_OVERWRITE_CONDITION.key ->
"true") {
+ sql(s"INSERT INTO $t REPLACE WHERE dep = 'hr' VALUES (2L, 'hr')")
+ }
+ checkAnswer(spark.table(t), Row(2L, "hr "))
+ }
+ }
+
+ test("SPARK-61512: REPLACE WHERE with a CHAR condition in an OR with a
partition condition") {
Review Comment:
This test passes without the `ApplyCharTypePadding` change. In
`DataSourceV2SQLSuiteV2Filter` it cannot fail, because `supportsPredicates`
rejects every `OR`. Since it guards `toV1`, would it fit better in
`DataSourceV2SQLSuiteV1Filter`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]