dongjoon-hyun commented on code in PR #60380:
URL: https://github.com/apache/spark/pull/60380#discussion_r4553943156
##########
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:
Thank you. I saw the same for `CHAR(8)` and `VARCHAR(8)`, and also for
`REPLACE WHERE dep IS NULL`. The overwrite condition now skips the wrap under
standard semantics, as without this change, so the migration guide entry stays
scoped to standard semantics being off. I added a test of a static partition
overwrite on a `CHAR` column under standard semantics.
##########
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:
Thank you. I moved it to `DataSourceV2SQLSuiteV1Filter`.
##########
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:
Thank you. The test now asserts `_LEGACY_ERROR_TEMP_2245` before the config
is set.
--
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]