suhuruli commented on code in PR #60462:
URL: https://github.com/apache/spark/pull/60462#discussion_r4594137931


##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/DatasetManager.scala:
##########
@@ -742,7 +783,14 @@ object DatasetManager extends Logging {
       }
       .collect { case (key, Some(value)) => key -> value }
 
-    validatedAndCanonicalizedProps ++ specialProps
+    val declaredPropertyKeys = if (validatedAndCanonicalizedProps.nonEmpty) {

Review Comment:
   Fixed. `materializeTable` now passes the resolved target catalog to 
`resolveTableProperties`, which adds no `pipelines.declaredPropertyKeys` when 
that catalog is a `JDBCTableCatalog`; every other catalog receives the record 
exactly as before, and `evolveTable` is unchanged because a JDBC table never 
reports a record to read. The `declaredPropertyKeys` Scaladoc and the 
`resolveTableProperties` doc now say that tables in a JDBC catalog do not get 
the record and why. The new `MaterializeTablesInJdbcCatalogSuite` materializes 
a view with a declared table property into an H2-backed JDBC catalog that 
records the properties reaching `createTable`, and asserts that the view is 
created, that the recorded properties hold only the declared one, and that H2 
stores the declared comment. H2 rejects an unknown `key value` option after the 
column list (the sql/core `CREATE TABLE with table property` test shows that 
for `ENGINE`), so the view declares `comment`, which the JDBC catalog turns 
into t
 he table comment; before the change the record was the statement's only option 
and the run failed with `TableMaterializationException`. The suite lives in 
`sql/pipelines` with the H2 test dependency the sql/core JDBC catalog tests 
use, added to that module only, and the existing record tests on the recording 
catalog and the session catalog still pass.



##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/JsonStringArrayCodec.scala:
##########
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.pipelines.graph
+
+import scala.util.control.NonFatal
+
+import org.json4s.JsonAST.{JArray, JString}
+import org.json4s.jackson.JsonMethods
+
+/**
+ * Codec for table property values that hold a list of strings as a JSON 
string array, e.g.
+ * `["id","region"]`. Used for 
[[PipelinesTableProperties.declaredPropertyKeys]] and for the
+ * column-name lists [[AutoCdcAuxiliaryTable]] records on auxiliary tables. An 
empty list
+ * round-trips as `[]`; whether an empty list is meaningful is up to the 
caller.
+ */
+private[graph] object JsonStringArrayCodec {
+
+  def serialize(values: Seq[String]): String = {
+    JsonMethods.compact(JArray(values.map(JString(_)).toList))
+  }
+
+  /**
+   * Parses a value written by [[serialize]]. `None` if it is not JSON, or not 
a JSON array whose
+   * elements are all strings, so that callers can treat a malformed value as 
absent.
+   */
+  def parse(raw: String): Option[Seq[String]] = {
+    val parsed = try Some(JsonMethods.parse(raw)) catch { case NonFatal(_) => 
None }

Review Comment:
   Confirmed: on the previous code `["p.a"]x`, `["p.a"]]` and `["p.a"],["p.b"]` 
all parsed to `["p.a"]`, and the new negative test failed against it. `parse` 
now reads through the json4s mapper's `ObjectReader` with 
`FAIL_ON_TRAILING_TOKENS` enabled and keeps the `NonFatal` to `None` mapping, 
so those values are malformed. `JsonStringArrayCodecSuite` gains "parse returns 
None for a JSON array followed by trailing content" for those three inputs and 
a second value, and "parse accepts whitespace before and after the array"; the 
existing escaping round-trip tests still pass. `MaterializeTablesSuite` gains 
"a declared-keys record with trailing content is malformed and removes 
nothing": a record such as `["p.stale"]x` no longer removes `p.stale`, and the 
run rewrites it as `["p.a"]`. `AutoCdcAuxiliaryTableSuite` still passes through 
the shared codec.



##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/PipelinesTableProperties.scala:
##########
@@ -37,6 +37,27 @@ object PipelinesTableProperties {
   val resetAllowed: PipelineTableProperty[Boolean] =
     buildProp("pipelines.reset.allowed", default = true)
 
+  /**
+   * Internal table property recording, as a sorted JSON string array (see
+   * [[JsonStringArrayCodec]]), the keys of the table properties declared in 
the dataset definition
+   * (SQL `TBLPROPERTIES`, Python `table_properties`) as of the latest 
materialization.
+   *
+   * `DatasetManager` writes it, through `resolveTableProperties`, only when 
the definition declares
+   * at least one table property. It covers those declared keys, not the 
`comment` and `provider`
+   * entries derived from the dataset's comment and format. On the next 
materialization,
+   * `evolveTable` removes a recorded key when the definition no longer 
declares it, it is still on

Review Comment:
   I rewrote the `declaredPropertyKeys` Scaladoc and the `evolveTable` `@param 
properties` entry to state the rule as the code applies it. A recorded key is 
removed when the resolved table properties of the definition no longer contain 
it, it is still on the table, and it is not a reserved key other than 
`comment`; the resolved properties include the `comment` and `provider` derived 
from the definition's comment and format, so a comment moved from 
`TBLPROPERTIES` to the comment field stays. The record itself is removed once 
the definition declares no table property, whether or not the keys it listed go 
with it, and a reserved key such as `owner` passed through Python 
`table_properties` is recorded but never removed. The `@param` entry now also 
says that the record is removed once `properties` no longer carries it. No new 
test; the existing suites pass and the module compiles.



##########
docs/sql-migration-guide.md:
##########
@@ -100,6 +100,7 @@ license: |
 - Since Spark 4.4, a CHECK constraint that `ALTER TABLE ... ADD CONSTRAINT` 
adds to a Data Source V2 table, and whose condition reads the current time, 
date, catalog, user and the like, such as `ts <= current_timestamp()` or `db = 
current_database()`, is checked against their values at the time of each write. 
Previously, it was checked against their values at the time of the `ALTER 
TABLE`, unless a write could not read back a predicate for its condition, such 
as one with a lambda function or one that passes a column to `datediff`, `year` 
or `upper` (SPARK-60032). Such a constraint is stored without a predicate, so 
each write parses and analyzes its SQL text under the session of the write, as 
it does for a constraint without a predicate that `CREATE TABLE` adds. A 
setting of the write such as `spark.sql.ansi.enforceReservedKeywords`, which 
reserves `TIME`, or `spark.sql.ansi.doubleQuotedIdentifiers` can then make it 
fail to parse the text or read it differently. A row-level operation
  that copies unchanged rows, such as a `DELETE` or `UPDATE` on a copy-on-write 
table, fails with `CHECK_CONSTRAINT_VIOLATION` on a copied row that no longer 
satisfies the condition. For example, `ts >= current_timestamp() - INTERVAL 30 
DAYS` now fails such a `DELETE` once a copied row is older than 30 days. A 
condition that Spark detects a write could resolve differently is stored as 
before, so a write accepts and rejects the same rows for it as before. Examples 
are conditions that also read a variable, a `CHAR` column, a name with more 
than one part such as a column qualified by the table name, or a constant that 
depends on the session time zone, such as `'2024-01-01 00:00:00'` cast to a 
timestamp. So are conditions with a name such as `current_date` or `user` 
without parentheses, which a write can read as a column name, and conditions 
whose text the parser settings of the `ALTER TABLE`, such as 
`spark.sql.ansi.doubleQuotedIdentifiers` or 
`spark.sql.parser.escapedStringLiterals`, r
 ead differently from their default values. ANSI mode, 
`spark.sql.timestampType` and the session time zone are taken from the session 
for this comparison, as for a generation expression. A condition that reads the 
current date or time only through a typed literal such as `DATE 'today'` is 
also stored as before. An existing constraint keeps its stored predicate until 
it is dropped and added again. `Check.predicate()` is null for a constraint 
stored without a predicate, so a connector that validates or enforces it itself 
has to evaluate `Check.predicateSql()`. To restore the previous behavior, set 
`spark.sql.legacy.freezeCurrentValuesInCheckConstraint` to `true` before adding 
the constraint. A constraint added without it is not changed.
 - Since Spark 4.4, a function created by `CREATE FUNCTION ... AS 'class'` 
whose class implements 
`org.apache.spark.sql.connector.catalog.functions.UnboundFunction` and extends 
either a Hive UDF class (such as `UDF` or `GenericUDF`) or 
`org.apache.spark.sql.expressions.UserDefinedAggregateFunction` runs as a 
DataSource V2 function instead of a Hive UDF or Spark UDAF, including functions 
created in earlier versions. Its result type, argument coercion, and null 
handling are defined by the V2 implementation selected by 
`UnboundFunction.bind`. Hive's `@UDFType` and Spark UDAF's `deterministic` 
property do not control the V2 path. For scalar functions, determinism uses 
`BoundFunction.isDeterministic()` (default `true`) and the arguments' 
determinism. A call with constant arguments can therefore become eligible for 
constant folding during query planning instead of being evaluated per row.
 - Since Spark 4.4, `next_day` and `last_day` fail with `DATETIME_OVERFLOW` 
when the result is later than the maximum `DATE`, `+5881580-07-11`, and `trunc` 
fails with it when the result is earlier than the minimum `DATE`, 
`-5877641-06-23`, regardless of `spark.sql.ansi.enabled`. Previously, these 
functions silently returned a wrong date, in most cases one at the other end of 
the `DATE` range; for example, `last_day(DATE'+5881580-07-11')` returned 
`-5877641-07-12` and `trunc(DATE'-5877641-06-23', 'MONTH')` returned 
`+5881580-06-20`, while `trunc` to `QUARTER` failed with `ARITHMETIC_OVERFLOW`. 
`next_day` and `trunc` to `WEEK` also returned a wrong date for the first days 
of the `DATE` range; for example, `next_day(DATE'-5877641-06-23', 'MO')` and 
`trunc(DATE'-5877641-07-01', 'WEEK')` returned `-5877641-06-25`, a Thursday, 
and now return `-5877641-06-29`.
+- Since Spark 4.4, [Declarative 
Pipelines](declarative-pipelines-programming-guide.html) removes a table 
property from a materialized view or streaming table when the property is 
dropped from the dataset definition (SQL `TBLPROPERTIES` or Python 
`table_properties`). Previously, a dropped property stayed on the table. A 
table whose definition declares at least one table property now carries the 
table property `pipelines.declaredPropertyKeys`, which lists the declared keys. 
A table created before this change gets the record on its first run after 
upgrading whose definition declares a property, and a dropped property is 
removed starting with the run after the one that first recorded the table's 
declared keys. Properties the definition never declared, such as those managed 
by the catalog or set with `ALTER TABLE ... SET TBLPROPERTIES`, are left in 
place. To keep a property on the table, declare it in the dataset definition.

Review Comment:
   The entry now names `pipelines.declaredPropertyKeys` as the record, says 
that a property is removed only when a record written by an earlier run lists 
it, that a property dropped before a table first records its declared keys, 
including one an earlier version left behind, stays on the table and can be 
removed with `ALTER TABLE ... UNSET TBLPROPERTIES`, and that reserved keys 
other than `comment`, such as `owner`, passed through Python `table_properties` 
are never removed. It also notes that tables in a JDBC catalog never get the 
record. The four `table_properties` docstrings in `api.py` and `output.py` now 
state the same conditions and point to the Declarative Pipelines entry in the 
SQL migration guide, so none promises removal on the next run without them. I 
ran `ruff check` and `ruff format --diff` on both files.



-- 
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]

Reply via email to