This is an automated email from the ASF dual-hosted git repository.
dongjoon-hyun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/master by this push:
new 077f3f265f73 [SPARK-61461][SQL] Fix internal error when deleting from
a DSv2 table with a CHAR column without row-level operation support
077f3f265f73 is described below
commit 077f3f265f734910d67e755df2f6bc80dadee905
Author: Dongjoon Hyun <[email protected]>
AuthorDate: Wed Nov 11 13:45:55 2026 -0800
[SPARK-61461][SQL] Fix internal error when deleting from a DSv2 table with
a CHAR column without row-level operation support
### What changes were proposed in this pull request?
This PR fixes `ApplyCharTypePadding` so that a `DELETE` that is not
rewritten into a row-level operation no longer fails with `[INTERNAL_ERROR]
Unexpected table relation: Project [...]` when the table has a `CHAR` column.
- The target table of a `DeleteFromTable` is no longer wrapped in the
read-side padding `Project`, because that plan never reads the table.
- The condition of the `DeleteFromTable` evaluates the read-side padding of
its `CHAR` columns itself, after its comparisons are padded, so it matches what
a read of the table returns.
### Why are the changes needed?
`DeleteFromTable` stays as is for tables that support deletes only with
filters, and for `DELETE` without `WHERE` on a `TruncatableTable`. Since
read-side padding was added in 3.4.0 (SPARK-40697), `ApplyCharTypePadding`
wraps the relation of such a plan in a `Project`, and `DataSourceV2Strategy`
only expects a scan relation there. So every `DELETE` on such a table with a
`CHAR` column fails, whatever the condition and whether or not the values are
stored with padding:
```sql
CREATE TABLE cat.t (id INT, dep CHAR(8)) PARTITIONED BY (id, dep);
DELETE FROM cat.t WHERE id = 1;
-- [INTERNAL_ERROR] Unexpected table relation: Project [id#8,
static_invoke(CharVarcharCodegenUtils.readSidePadding(dep#9, 8)) AS dep#12] ...
```
A `DELETE` on a V1 table with a `CHAR` column hits the same internal error
instead of `UNSUPPORTED_FEATURE.TABLE_OPERATION`.
A comparison with a `CHAR` column cannot simply be translated by attribute
name, as it was before 3.4.0: the data source would compare the padded literal
with the stored values and silently skip the values stored without padding.
With this PR the filter keeps the padding, and the delete fails with the
existing "Cannot delete from table" error unless the data source accepts it,
e.g. `CHAR_READ_SIDE_PADDING` from SPARK-60479. This is the same shape as the
error that `spark.sql.readSideC [...]
### Does this PR introduce _any_ user-facing change?
Yes. For a V2 table with a `CHAR` column that is deleted from with filters:
- `DELETE` with a condition that does not use a `CHAR` column, `DELETE`
without `WHERE`, and `IS [NOT] NULL` on a `CHAR` column now succeed instead of
failing with `INTERNAL_ERROR`.
- A comparison with a `CHAR` column fails with `Cannot delete from table
... where [(CHAR_READ_SIDE_PADDING(dep, 8, ' ')) = 'hr ']` instead of
`INTERNAL_ERROR`, unless the data source accepts that filter. With
`spark.sql.charVarchar.standardSemantics.enabled`, it fails with `cannot
translate expression to source filter`.
- `DELETE` on a V1 table with a `CHAR` column fails with
`UNSUPPORTED_FEATURE.TABLE_OPERATION` instead of `INTERNAL_ERROR`.
### How was this patch tested?
Added two tests to `DeleteFromTests`, which run against the V1 and V2
filter catalogs of `DataSourceV2SQLSuite`. All four runs fail on master and
pass with this PR. Each branch of the change was also disabled in turn, and
every variant made the tests fail.
```
build/sbt 'sql/testOnly *DataSourceV2SQLSuite* *CharVarcharTestSuite
*DeleteFromTableSuite *EnhancedDeleteFilterSuite *UpdateTableSuite
*MergeInto*Suite *DataSourceV2DataFrameSuite *PlanResolutionSuite'
```
passes (29 suites, 3475 tests), as does `sql/scalastyle` and
`sql/Test/scalastyle`.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
Closes #60344 from dongjoon-hyun/filter-only-delete-char.
Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
skip-checks: true
---
.../datasources/ApplyCharTypePadding.scala | 33 +++++++++++++++++++---
.../spark/sql/connector/DeleteFromTests.scala | 31 +++++++++++++++++++-
2 files changed, 59 insertions(+), 5 deletions(-)
diff --git
a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/ApplyCharTypePadding.scala
b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/ApplyCharTypePadding.scala
index a1cc576b2b23..65d1f98d6f49 100644
---
a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/ApplyCharTypePadding.scala
+++
b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/ApplyCharTypePadding.scala
@@ -22,8 +22,10 @@ import java.util.concurrent.atomic.AtomicBoolean
import org.apache.spark.internal.LogKeys
import org.apache.spark.sql.catalyst.analysis.ApplyCharTypePaddingHelper
import org.apache.spark.sql.catalyst.catalog.HiveTableRelation
-import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.catalyst.expressions.AttributeReference
+import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable,
LogicalPlan}
import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.DELETE_FROM_TABLE
import org.apache.spark.sql.catalyst.util.CharVarcharUtils
import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation
import org.apache.spark.sql.internal.SQLConf
@@ -61,7 +63,9 @@ object ApplyCharTypePadding extends Rule[LogicalPlan] {
}
if (conf.readSideCharPadding || conf.charVarcharStandardSemantics) {
- val newPlan = plan.resolveOperatorsUpWithNewOutput {
+ // A DELETE that is not rewritten as a row-level operation does not read
its table, so the
+ // table is not padded, and the condition gets the padding instead, see
padDeleteConditions.
+ val newPlan = plan.transformUpWithNewOutput({
case r: LogicalRelation =>
ApplyCharTypePaddingHelper.readSidePadding(r, () =>
r.copy(output = r.output.map(CharVarcharUtils.cleanAttrMetadata)))
@@ -74,11 +78,32 @@ object ApplyCharTypePadding extends Rule[LogicalPlan] {
val cleanedPartCols =
r.partitionCols.map(CharVarcharUtils.cleanAttrMetadata)
r.copy(dataCols = cleanedDataCols, partitionCols = cleanedPartCols)
})
- }
- ApplyCharTypePaddingHelper.paddingForStringComparison(newPlan,
padCharCol = false)
+ }, skipCond = p => p.analyzed || p.isInstanceOf[DeleteFromTable],
canGetOutput = _.resolved)
+ padDeleteConditions(
+ ApplyCharTypePaddingHelper.paddingForStringComparison(newPlan,
padCharCol = false))
} else {
ApplyCharTypePaddingHelper.paddingForStringComparison(
plan, padCharCol =
!conf.getConf(SQLConf.LEGACY_NO_CHAR_PADDING_IN_PREDICATE))
}
}
+
+ /**
+ * 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`.
+ */
+ 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)
+ }
+ })
+ }
+ }
}
diff --git
a/sql/core/src/test/scala/org/apache/spark/sql/connector/DeleteFromTests.scala
b/sql/core/src/test/scala/org/apache/spark/sql/connector/DeleteFromTests.scala
index 26f64ceb33fe..90abb0e9e30a 100644
---
a/sql/core/src/test/scala/org/apache/spark/sql/connector/DeleteFromTests.scala
+++
b/sql/core/src/test/scala/org/apache/spark/sql/connector/DeleteFromTests.scala
@@ -18,7 +18,7 @@
package org.apache.spark.sql.connector
import org.apache.spark.sql._
-import org.apache.spark.sql.internal.SQLConf.V2_SESSION_CATALOG_IMPLEMENTATION
+import org.apache.spark.sql.internal.SQLConf.{LEGACY_CHAR_VARCHAR_AS_STRING,
V2_SESSION_CATALOG_IMPLEMENTATION}
import org.apache.spark.sql.sources.SimpleScanSource
/**
@@ -99,6 +99,35 @@ trait DeleteFromTests extends DatasourceV2SQLBase {
}
}
+ test("SPARK-61461: DeleteFrom with v2 filtering: table with a CHAR column") {
+ val t = s"${catalogAndNamespace}tbl"
+ withTable(t) {
+ sql(s"CREATE TABLE $t (id bigint, data char(3), p int) USING foo
PARTITIONED BY (id, p)")
+ sql(s"INSERT INTO $t VALUES (2L, 'a', 2), (2L, 'b', 3), (3L, 'c', 3)")
+ sql(s"DELETE FROM $t WHERE id = 2")
+ checkAnswer(spark.table(t), Seq(
+ Row(3, "c ", 3)))
+ }
+ }
+
+ test("SPARK-61461: DeleteFrom with v2 filtering: condition on a CHAR
column") {
+ val t = s"${catalogAndNamespace}tbl"
+ withTable(t) {
+ sql(s"CREATE TABLE $t (id bigint, p char(3)) USING foo PARTITIONED BY
(id, p)")
+ // 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
+ withSQLConf(LEGACY_CHAR_VARCHAR_AS_STRING.key -> "true") {
+ sql(s"INSERT INTO $t VALUES (2L, 'a'), (3L, 'b')")
+ }
+ val exc = intercept[AnalysisException] {
+ sql(s"DELETE FROM $t WHERE p = 'a'")
+ }
+
+ assert(spark.table(t).count() === 2)
+ assert(exc.getMessage.contains(s"Cannot delete from table $t"))
+ }
+ }
+
test("DeleteFrom: DELETE is only supported with v2 tables") {
// use the default v2 session catalog.
spark.conf.set(V2_SESSION_CATALOG_IMPLEMENTATION, "builtin")
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]