AHeise commented on code in PR #29973:
URL: https://github.com/apache/flink/pull/29973#discussion_r4600605660
##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/runtime/stream/sql/MaterializedTableTableApiITCase.java:
##########
@@ -408,6 +411,57 @@ void testCreateMaterializedTableFailedInContinuousMode() {
assertThat(catalog.tableExists(USERS_SHOPS.toObjectPath())).isFalse();
}
+ @Test
+ void testRegularTableAndViewAtThePathAreRejected() throws Exception {
+ tEnv.createTable(
+ "existing_table",
+ TableDescriptor.forConnector("datagen")
+ .schema(Schema.newBuilder().column("id",
DataTypes.BIGINT()).build())
+ .build());
+ tEnv.createView("existing_view", tEnv.from("datagenSource"));
+ final Set<JobID> jobsBefore = listJobIds();
+
+ assertThatThrownBy(
+ () ->
+ usersShopsQuery()
+ .materializeInto("existing_table")
+ .execute(CreateMode.failIfExists()))
+ .isInstanceOf(ValidationException.class)
+ .extracting(FlinkAssertions::chainOfCauses,
FlinkAssertions.STREAM_THROWABLE)
+ .extracting(Throwable::getMessage)
+ .containsExactly(
+ "Could not execute CreateTable in path
`test_catalog`.`test_db`.`existing_table`",
+ "Table (or view) test_db.existing_table already exists
in Catalog test_catalog.");
+ assertThatThrownBy(
+ () ->
+ usersShopsQuery()
+ .materializeInto("existing_view")
+ .execute(CreateMode.failIfExists()))
+ .isInstanceOf(ValidationException.class)
+ .extracting(FlinkAssertions::chainOfCauses,
FlinkAssertions.STREAM_THROWABLE)
+ .extracting(Throwable::getMessage)
+ .containsExactly(
+ "Could not execute CreateTable in path
`test_catalog`.`test_db`.`existing_view`",
+ "Table (or view) test_db.existing_view already exists
in Catalog test_catalog.");
+ // A regular table under createOrAlter() is rejected before planning.
Review Comment:
Nit: the comment reads as describing the next assertion, but that one checks
a view. Maybe "A view under createOrAlter() is rejected by the factory. The
regular-table case is rejected before planning and covered in
`MaterializedPipelineImplTest`."
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/CreateMode.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.flink.table.api;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.util.Preconditions;
+
+import java.util.Objects;
+
+/**
+ * Controls what executing a materialized table declaration does when an
object already exists at
+ * its path. See {@link MaterializedPipeline#execute(CreateMode)}.
+ */
+@PublicEvolving
+public class CreateMode {
+
+ /** The kind of a {@link CreateMode}. */
+ @PublicEvolving
+ public enum Kind {
+ CREATE_OR_ALTER,
+ FAIL_IF_EXISTS
+ }
+
+ private static final CreateMode CREATE_OR_ALTER = new
CreateMode(Kind.CREATE_OR_ALTER);
+ private static final CreateMode FAIL_IF_EXISTS = new
CreateMode(Kind.FAIL_IF_EXISTS);
+
+ private final Kind kind;
+
+ private CreateMode(Kind kind) {
+ this.kind = Preconditions.checkNotNull(kind, "Kind must not be null.");
+ }
+
+ /**
+ * Creates the materialized table, or alters an existing materialized
table to this declaration.
+ * This is the default, used by {@link MaterializedPipeline#execute()}.
See there for what an
+ * existing materialized table keeps.
+ *
+ * <p>Fails with a {@link ValidationException} if a regular table, a view,
or a temporary table
+ * or view exists at the path.
+ *
+ * <p>Equivalent to {@code CREATE OR ALTER MATERIALIZED TABLE}.
+ */
+ public static CreateMode createOrAlter() {
+ return CREATE_OR_ALTER;
+ }
+
+ /**
+ * Creates the materialized table. Fails with a {@link
ValidationException} if a materialized
+ * table, a regular table, a view, or a temporary table or view exists at
the path. The existing
+ * object is not changed, and no refresh job is started.
+ *
+ * <p>Equivalent to {@code CREATE MATERIALIZED TABLE}.
+ */
+ public static CreateMode failIfExists() {
Review Comment:
Should we also offer an `ignoreIfExists()` mode, probably as a follow-up?
`createTable`, `createView` and `createModel` all take `ignoreIfExists`, and a
"create once, never touch again" deployment can't be expressed with the two
modes today: `createOrAlter()` restarts the refresh job on every run and
`failIfExists()` makes the rerun fail. It would return the handle of the
existing materialized table without changing it.
The SQL grammar has no `CREATE MATERIALIZED TABLE IF NOT EXISTS` (the MT
rule never calls `IfNotExistsOpt()`), so this would need a FLIP amendment, and
maybe the SQL clause too for parity. We'd also have to decide whether an object
of another kind at the path is ignored or rejected.
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/CreateMode.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.flink.table.api;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.util.Preconditions;
+
+import java.util.Objects;
+
+/**
+ * Controls what executing a materialized table declaration does when an
object already exists at
+ * its path. See {@link MaterializedPipeline#execute(CreateMode)}.
+ */
+@PublicEvolving
+public class CreateMode {
Review Comment:
Nit: could this be `final`, like `RefreshStrategy` in #29936 and
`InsertConflictStrategy`? The private constructor already prevents subclassing,
so it's only for consistency.
--
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]