spark-test commented on code in PR #60462:
URL: https://github.com/apache/spark/pull/60462#discussion_r4584763630
##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/PipelinesTableProperties.scala:
##########
@@ -37,6 +40,36 @@ object PipelinesTableProperties {
val resetAllowed: PipelineTableProperty[Boolean] =
buildProp("pipelines.reset.allowed", default = true)
+ /**
+ * Internal table property recording, as a JSON string array, the keys of
the table properties
+ * declared in the dataset definition as of the latest materialization. On
the next
+ * materialization, keys recorded here but no longer declared are removed
from the table, while
Review Comment:
**Nit (P3):** This sentence and the new `evolveTable` comment
(DatasetManager.scala:702-703) describe the record by intent rather than by the
conditions the code applies. A maintainer relying on them would get these wrong:
- `evolveTable` also requires the key to still be on the table and skips
every key in `CatalogV2Util.TABLE_RESERVED_PROPERTIES`
(DatasetManager.scala:708-711). A `comment` declared in `TBLPROPERTIES`, or an
`owner` declared in Python `table_properties`, is recorded but stays on the
table when dropped.
- A key becomes removable once a definition declares it, not once the
pipeline writes it. A catalog-managed key that a definition re-declares with
its current value is recorded without any `SetProperty`, and is removed once
the declaration is dropped.
- "The record is written by this alter" holds only for graph datasets.
`materializeAuxiliaryTable` calls the same `evolveTable` with
`auxiliaryTableSpec.properties` (DatasetManager.scala:527-535), which never
include the record.
Both comments stay accurate if they state the presence check, the
reserved-key exemption, ownership by declaration, and which callers pass the
record.
**Verification:**
- **Inspection:** Read against resolveTableProperties and evolveTable, the
declaredPropertyKeys Scaladoc and the evolveTable comment each state the
conditions the code applies: when the record is written, the presence check,
the reserved-key exemption, ownership by declaration, and which callers pass
the record.
##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/DatasetManager.scala:
##########
@@ -694,18 +696,27 @@ object DatasetManager extends Logging {
val existingProperties = existingTable.properties()
- // TODO (SPARK-57670): Property removal is intentionally not handled here:
a property dropped
- // from the table definition between runs is left in place rather than
actually removed from the
- // corresponding catalog table entry. Removing it reliably is hard because
we cannot distinguish
- // a user-declared property the user dropped from a catalog/engine-managed
property (e.g. the
- // non-reserved `clusteringColumns`, or arbitrary catalog-internal keys)
that must never be
- // removed, and there is no record of which keys the pipeline previously
set.
+ // Only remove properties that a previous materialization recorded as
declared in the table
+ // definition. Every other existing property may be catalog/engine-managed
(e.g. the
+ // non-reserved `clusteringColumns`, or arbitrary catalog-internal keys)
and must never be
+ // removed. A table without the record (e.g. one materialized before it
was introduced) has
+ // nothing removed; the record is written by this alter and used from the
next run on.
+ val previouslyDeclaredKeys =
+
Option(existingProperties.get(PipelinesTableProperties.declaredPropertyKeys))
+ .flatMap(PipelinesTableProperties.parsePropertyKeys)
+ .getOrElse(Seq.empty)
+ val propertiesToRemove = previouslyDeclaredKeys.distinct.collect {
+ case k if !properties.contains(k) && existingProperties.containsKey(k) &&
+ !CatalogV2Util.TABLE_RESERVED_PROPERTIES.contains(k) =>
+ TableChange.removeProperty(k)
+ }
+
val propertiesToSet = properties.collect {
case (k, v) if !Option(existingProperties.get(k)).contains(v) =>
TableChange.setProperty(k, v)
}
- val allTableChanges = columnChanges ++ propertiesToSet
+ val allTableChanges = columnChanges ++ propertiesToRemove ++
propertiesToSet
Review Comment:
**Non-blocking (P2):** On a Hive-metastore-backed `spark_catalog`, this
alter now clears the table comment and default collation on runs that never
altered the table before. That includes tables in the default parquet format,
which load as a `V1Table`.
`V1Table` reports `comment` from the `CatalogTable` field, so this method
treats an unchanged comment as already set and never re-sends it.
`V2SessionCatalog.alterTable`, however, derives `comment` and `collation` only
from `applyPropertiesChanges(catalogTable.properties, ...)`
(V2SessionCatalog.scala:294-298). `HiveClientImpl` moves both keys out of the
raw properties when it reads the table (HiveClientImpl.scala:544-558). So any
alter with a property change that doesn't set them stores `comment = None`. The
base branch reaches this only when a declared value changes or is added. This
PR adds two more cases:
- the first run over every table materialized before this change, which
writes `pipelines.declaredPropertyKeys` even when the definition is unchanged;
- runs whose definition only drops a recorded key.
By my reading of the code, a streaming table created by Spark 4.3.0 with
`COMMENT 'daily facts'` loses that comment on its first run after upgrading,
and gets it back only on the next run, when the definition sets it again. A
comment added outside the pipeline with `COMMENT ON TABLE`, or a table's
default collation, is lost permanently. On the base branch, a table whose
definition is unchanged is never altered, so both comments stay. The in-memory
session catalog keeps `comment` in the raw properties and is not affected. No
new test checks a comment across these alters in `HiveMaterializeTablesSuite`.
**Recommended change:** Make V2SessionCatalog.alterTable keep the table's
existing comment and default collation unless the requested changes set or
remove that key, the rule the v1 ALTER TABLE ... SET and UNSET TBLPROPERTIES
commands already follow and the fallback the method already applies to the
owner, and cover it with session-catalog alterTable tests and a Declarative
Pipelines materialization test, run in the Hive variant, that checks a table
comment across the bootstrap and removal alters.
**Why this works:** When no SetProperty or RemoveProperty in the change set
names comment, the new definition takes catalogTable.comment, and likewise
catalogTable.collation for collation; when one does, the value comes from the
properties after the changes, as today, so setting or removing either key keeps
working. The external catalog then receives the comment and collation the table
already had, and the Hive metastore keeps them.
**Scope:** Preserve an untouched table comment and default collation in the
session catalog's DSv2 alterTable, with catalog-level and pipeline-level tests.
**Compatibility:** Altering comment or collation through an explicit
SetProperty or RemoveProperty, and column-only alters, behave exactly as on the
base branch.
**Risks:** On a Hive-metastore-backed session catalog, COMMENT ON TABLE and
ALTER TABLE ... DEFAULT COLLATION stop clearing the other key, which they do
today; that corrects silent metadata loss, and a change that names comment or
collation still sets or removes it.
**Constraints:** Setting or removing comment or collation through a
SetProperty or RemoveProperty in the same alterTable must still take effect,
including COMMENT ON TABLE ... IS NULL.
**Exclusions:**
- Comments and collations that earlier property alters already cleared on
existing tables are not restored; the fix only stops further loss.
**Success:** A property alterTable on a session-catalog table, whether
Hive-metastore-backed or created by SQL DDL with a COMMENT clause, keeps the
table's comment and default collation when no change sets or removes them. On a
Hive-metastore-backed session catalog, the first run over a pipeline table
without the declared-keys record and a run whose definition only drops a
recorded property both leave the table comment in place, whether the definition
declares it or it was set outside the pipeline. A change that sets or removes
comment or collation still sets or removes it.
**Verification:**
- **Regression:** On a Hive-metastore-backed session catalog, a pipeline
table with a definition-declared comment and another whose comment was set
outside the pipeline both keep their comments after a run that bootstraps the
declared-keys record and after a run that only drops a recorded property.
- **Behavior:** A session-catalog alterTable that only sets or removes an
unrelated property keeps a comment and default collation that live only in the
table's dedicated fields.
- **Compatibility:** A session-catalog alterTable that sets or removes the
comment or collation key still changes it, as COMMENT ON TABLE, its IS NULL
form, and ALTER TABLE ... DEFAULT COLLATION require.
##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/PipelinesTableProperties.scala:
##########
@@ -37,6 +40,36 @@ object PipelinesTableProperties {
val resetAllowed: PipelineTableProperty[Boolean] =
buildProp("pipelines.reset.allowed", default = true)
+ /**
+ * Internal table property recording, as a JSON string array, the keys of
the table properties
+ * declared in the dataset definition as of the latest materialization. On
the next
+ * materialization, keys recorded here but no longer declared are removed
from the table, while
+ * properties the pipeline never set (e.g. catalog-managed ones) are left
untouched.
+ *
+ * This key is not registered as a user-settable property, so
[[validateAndCanonicalize]] drops
+ * it if a user declares it.
+ */
+ val declaredPropertyKeys: String = s"${pipelinesPrefix}declaredPropertyKeys"
+
+ /** Serializes property keys for [[declaredPropertyKeys]]. */
+ def serializePropertyKeys(keys: Seq[String]): String = {
Review Comment:
**Nit (P3):** This codec repeats
`AutoCdcAuxiliaryTable.serializeColumnNames` and `parseColumnNames`
(AutoCdcAuxiliaryTable.scala:79-95) line for line, as public members where the
originals are `private[graph]`. `AutoCdcAuxiliaryTableSuite` pins the
original's round trip and its `None` result for malformed input, but nothing
tests this copy. No `MaterializeTablesSuite` case uses a malformed record or a
key that needs escaping. Every multi-key assertion declares its keys already
sorted, in Maps of at most four entries, which iterate in insertion order.
By my reading, each of these edits keeps every test green:
- Dropping the size check: a hand-edited `["p.a", 1]` record then drives
removal of `p.a`.
- Dropping the `try`: a non-JSON record then fails the table's
materialization.
- Dropping `.sorted` at DatasetManager.scala:759: reordering a SQL
definition's `TBLPROPERTIES` then alters an otherwise unchanged table.
The record decides which properties a later run deletes, so its encoding
needs tests that fail when it breaks. Sharing one package-private codec with
the AutoCDC properties would also bring the record under the existing AutoCDC
tests.
**Verification:**
- **Regression:** Materializing a table that declares a property and whose
record was replaced with a value that is not JSON, or with a JSON array mixing
strings and numbers, succeeds, removes no property, and leaves a well-formed
record.
- **Regression:** Declaring the same keys in a different order between runs,
with values and schema unchanged, issues no alterTable, and the record lists
the keys sorted.
- **Behavior:** Codec round-trip and malformed-input tests exercise the
shared codec the record uses, including keys with JSON-escaped and separator
characters.
##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/DatasetManager.scala:
##########
@@ -742,7 +754,11 @@ object DatasetManager extends Logging {
}
.collect { case (key, Some(value)) => key -> value }
- validatedAndCanonicalizedProps ++ specialProps
+ val declaredPropertyKeys = PipelinesTableProperties.declaredPropertyKeys ->
+ PipelinesTableProperties.serializePropertyKeys(
+ validatedAndCanonicalizedProps.keys.toSeq.sorted)
+
+ validatedAndCanonicalizedProps ++ specialProps + declaredPropertyKeys
Review Comment:
**Non-blocking (P2):** The record is added even when
`validatedAndCanonicalizedProps` is empty, so a definition with no table
properties now sends `pipelines.declaredPropertyKeys -> []` to the catalog.
`evolveTable` reads an absent record and `[]` the same way
(`getOrElse(Seq.empty)`), so the empty record never drives a removal. It does
change what catalogs receive:
- **JDBC catalogs.** `JDBCTableCatalog.createTable` appends every property
except comment, provider, owner, and location to `JDBC_CREATE_TABLE_OPTIONS`
(JDBCTableCatalog.scala:192-216), and the dialect appends those options
verbatim after the column list (JdbcDialects.scala:279-280). For `CREATE
MATERIALIZED VIEW pg.public.mv AS SELECT 1 AS id` in a JDBC catalog, the
database would receive `CREATE TABLE ... (...) pipelines.declaredPropertyKeys
[]`, which is not valid SQL. The database rejects it, not Spark, so this
follows from reading the code rather than from a run. On the base branch the
first run creates and fills the table; later refreshes already fail there at
`TRUNCATE`.
- **Every catalog.** Each table materialized before this change whose
definition declares no property now gets
`alterTable(SetProperty(pipelines.declaredPropertyKeys, "[]"))` on its next
run, where the base branch issued no alter. On a Hive-metastore session
catalog, that alter also clears the table comment.
Writing the record only when the definition declares at least one table
property avoids both.
**Verification:**
- **Behavior:** Creating a table whose definition declares no table property
passes no pipelines.declaredPropertyKeys to the catalog's createTable, and the
created table does not list it.
- **Regression:** A table that declares no property and carries no record,
standing in for one materialized before this change, is re-materialized with an
unchanged definition and schema without any alterTable.
- **Behavior:** Dropping every declared property removes those properties
and the record in a single alterTable, and a further unchanged run records no
alterTable.
##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/DatasetManager.scala:
##########
@@ -694,18 +696,27 @@ object DatasetManager extends Logging {
val existingProperties = existingTable.properties()
- // TODO (SPARK-57670): Property removal is intentionally not handled here:
a property dropped
- // from the table definition between runs is left in place rather than
actually removed from the
- // corresponding catalog table entry. Removing it reliably is hard because
we cannot distinguish
- // a user-declared property the user dropped from a catalog/engine-managed
property (e.g. the
- // non-reserved `clusteringColumns`, or arbitrary catalog-internal keys)
that must never be
- // removed, and there is no record of which keys the pipeline previously
set.
+ // Only remove properties that a previous materialization recorded as
declared in the table
+ // definition. Every other existing property may be catalog/engine-managed
(e.g. the
+ // non-reserved `clusteringColumns`, or arbitrary catalog-internal keys)
and must never be
+ // removed. A table without the record (e.g. one materialized before it
was introduced) has
+ // nothing removed; the record is written by this alter and used from the
next run on.
+ val previouslyDeclaredKeys =
+
Option(existingProperties.get(PipelinesTableProperties.declaredPropertyKeys))
+ .flatMap(PipelinesTableProperties.parsePropertyKeys)
+ .getOrElse(Seq.empty)
+ val propertiesToRemove = previouslyDeclaredKeys.distinct.collect {
+ case k if !properties.contains(k) && existingProperties.containsKey(k) &&
+ !CatalogV2Util.TABLE_RESERVED_PROPERTIES.contains(k) =>
Review Comment:
**Non-blocking (P2):** `TABLE_RESERVED_PROPERTIES` includes `comment`, and a
definition can declare `comment` as an ordinary table property:
- `cleanTableProperties` keeps it in `TBLPROPERTIES` ("It's safe to set
whatever table comment", AstBuilder.scala:6200-6201), and `ALTER TABLE ...
UNSET TBLPROPERTIES` can remove it.
- Spark Connect passes Python `table_properties` through unfiltered
(PipelinesHandler.scala:291).
`resolveTableProperties` records it like any other key, but this guard never
removes it. By my reading of the code, `CREATE MATERIALIZED VIEW mv
TBLPROPERTIES ('comment' = 'old') AS SELECT 1 AS id`, rerun without the
`TBLPROPERTIES` clause, keeps the comment `'old'`, while a dropped `p.stale` is
removed. That is the stale-property behavior this PR fixes for every other
declared key.
Spark SQL rejects the other reserved keys in `TBLPROPERTIES`, so narrowing
the exemption to them (every key in the list except `comment`) would match the
parser's rule. A comment the definition still supplies through its `COMMENT`
field would stay, because it remains among the declared properties. No test
declares a reserved key and then drops it, so removing or narrowing the guard
would not fail any test.
**Verification:**
- **Regression:** A table defined with a comment table property and no
comment field loses that comment on the run after the property is dropped, on
the recording catalog and on the session catalog in both the default and Hive
variants.
- **Behavior:** A table whose definition moves its comment from the table
properties to the comment field keeps that comment.
- **Regression:** A structural reserved key declared as a table property and
then dropped stays on the table, so removing or widening the guard fails a test.
--
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]