AHeise commented on code in PR #30239:
URL: https://github.com/apache/flink/pull/30239#discussion_r4783871566
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/materializedtable/EvolutionMaterializedTableMergeContext.java:
##########
@@ -101,7 +107,53 @@ public boolean hasConstraintDefinition() {
@Override
public Schema getMergedSchema() {
- return mergeDeclaredSchema(declaredSchema, querySchema,
dataTypeFactory, OPERATION_NAME);
+ final Schema mergedSchema =
+ mergeDeclaredSchema(
+ declaredSchema, querySchemaToMerge(), dataTypeFactory,
OPERATION_NAME);
+ return declaredSchema == null ? mergedSchema :
withNotNullKeyColumns(mergedSchema);
Review Comment:
Request: making the key columns NOT NULL here, after the merge, lets a
primary key land on a column that can hold NULLs:
- `appendedPrimaryKeyColumnsAreNotNull` stores `k2` as `INT NOT NULL`, but
the query produces it with `NULLIF(k, 0)`. With the default
`table.exec.sink.not-null-enforcer` = `ERROR`, the refresh job fails on the
first `k = 0`. Rows written before the alter have no `k2` at all, which is why
`query()` stores appended columns as nullable.
- Without a new query, a key on a stored nullable column
(`MaterializedTableEvolutionImplTest`, the case at ~392) turns `v` into `BIGINT
NOT NULL`, while the stored query still produces a nullable `v`.
`validateChanges` doesn't check it because there is no query change. With
`query(...)` in the same alter, the same edit is rejected with the "Column
mismatch at position N" message instead.
CoA rejects a nullable key (`DefaultSchemaResolver`: "Invalid primary key
'%s'. Column '%s' is nullable."), and `table-api.md` says for `materializeInto`
that key columns must be produced as `NOT NULL` and declared with `.notNull()`.
From reading the code, SQL `ADD PRIMARY KEY` on an MT doesn't change the column
type either: it emits only `TableChange.add(constraint)`, so `alterTable`
should fail on resolve. The note in `statements.md:597` says otherwise, though,
so please double-check.
Could we drop `withNotNullKeyColumns` and let the resolver reject a nullable
key? A user then declares the column with `.notNull()` in `addSchema`, and the
query must produce it as `NOT NULL`. That is the `materializeInto` contract.
Both tests would flip to expecting the rejection, and the "columns of the
resulting primary key become NOT NULL" sentence in the
`addSchema`/`modifySchema` Javadoc would go.
##########
docs/content/docs/sql/materialized-table/table-api.md:
##########
@@ -158,6 +158,31 @@ as one `CREATE OR ALTER MATERIALIZED TABLE`, with the
effects described in
distribution and the start mode. An alter that changes nothing leaves the
materialized table and
its refresh job untouched. In `explain()`, options that the alter resets still
apply to the sink.
+`addSchema`, `modifySchema`, `dropColumns`, `dropWatermark`, `dropPrimaryKey`
and `dropConstraint`
+change the schema like
+[`ALTER MATERIALIZED TABLE ... ADD`]({{< ref
"docs/sql/materialized-table/statements" >}}#add),
+[`MODIFY`]({{< ref "docs/sql/materialized-table/statements" >}}#modify) and
+[`DROP`]({{< ref "docs/sql/materialized-table/statements" >}}#drop):
+
+```java
+tEnv.fromMaterializedTable("orders_curated")
+ .alter()
+ .addSchema(Schema.newBuilder()
+ .columnByExpression("order_hour", "HOUR(`order_time`)")
+ .build())
+ .modifySchema(Schema.newBuilder()
+ .watermark("order_time",
$("order_time").minus(lit(10).seconds()))
+ .build())
+ .execute();
+```
+
+Unlike the SQL clauses, they also accept physical and persisted metadata
columns, and they combine
Review Comment:
Optional: this holds for ADD, but SQL `MODIFY` already accepts physical
columns (e.g. `MODIFY (c BIGINT AFTER b, ...)` in
`SqlMaterializedTableNodeToOperationConverterTest`), and `dropColumns` never
accepts persisted ones. Maybe: "Unlike `ALTER MATERIALIZED TABLE ... ADD`,
`addSchema` also accepts physical and persisted metadata columns, which the new
query must produce. Persisted columns cannot be dropped." Same in the zh file.
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/MaterializedTableEvolution.java:
##########
@@ -83,6 +86,59 @@ public interface MaterializedTableEvolution extends
Explainable<MaterializedTabl
*/
MaterializedTableEvolution resetOption(String key);
+ /**
+ * Adds the columns, watermark and primary key declared in {@code schema}.
The columns of the
+ * resulting primary key become NOT NULL.
+ *
+ * @param schema The columns, watermark and primary key to add.
+ * @return This evolution.
+ * @throws ValidationException if {@code schema} declares nothing,
declares a column more than
+ * once, or declares indexes or immutable columns
+ */
+ MaterializedTableEvolution addSchema(Schema schema);
Review Comment:
Request: FLIP-616 says for `addSchema`: "Physical and persisted metadata
columns come from the defining query and cannot be added here. Equivalent to
ALTER MATERIALIZED TABLE ... ADD (...)." This PR accepts them when the new
query produces them, and the declared type wins, including a declared NOT NULL
on an appended column, as CoA with a column list does. That seems a reasonable
extension, but could you add a "Deviation from FLIP-616" section to the PR body
and update the FLIP's `addSchema` Javadoc, as in #30207?
While here: compared with the FLIP, the public Javadoc dropped "New columns
are appended" (`addSchema`), and "A modified column keeps its position, and its
comment unless a new one is declared" (`modifySchema`). The implementation does
both; it is only said in the private Javadoc of `withModified`.
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/MaterializedTableEvolution.java:
##########
@@ -94,6 +150,17 @@ public interface MaterializedTableEvolution extends
Explainable<MaterializedTabl
*
* @return A new handle on the altered table.
* @throws ValidationException if the table does not exist or is not a
materialized table
+ * @throws ValidationException if a schema change names a column,
watermark, primary key or
+ * constraint that the schema left by the earlier changes does not
define, or adds one that
+ * it already defines
+ * @throws ValidationException if a schema change changes the kind of a
column
+ * @throws ValidationException if a dropped column is produced by the
query, unless it is a
Review Comment:
Optional: as far as I can trace, a persisted column can never be dropped. If
the query produces it, `checkDroppedColumnsNotProducedByQuery` rejects it. If
the new query omits it,
`AlterMaterializedTableChangeOperation#checkDroppedColumn` rejects the
`DropColumn` with "Dropping of persisted column `%s` is not supported." That
message only shows at `execute()`. This `@throws` reads as if a persisted
column the query doesn't produce could be dropped.
Simpler: reject a stored physical or persisted metadata column eagerly in
`withoutColumns`, and drop `droppedColumns` and
`checkDroppedColumnsNotProducedByQuery` (~15 lines).
`dropColumnsTurnsAComputedColumnTheQueryProducesIntoAPhysicalOne` keeps
passing; `dropColumnsRejectsAColumnAddedAndProducedByTheQuery` would get the
new message. Either way, I'd say here that persisted columns can't be dropped.
--
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]