You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "lsyldliu (via GitHub)" <gi...@apache.org> on 2024/04/30 08:28:28 UTC

[PR] [FLINK-35195][table] Support the execution of create materialized table in continuous refresh mode [flink]

lsyldliu opened a new pull request, #24750:
URL: https://github.com/apache/flink/pull/24750

   ## What is the purpose of the change
   
   *Support the execution of create materialized table in continuous refresh mode*
   
   
   ## Brief change log
   
     - *Support the execution of create materialized table in continuous refresh mode*
   
   
   ## Verifying this change
   
   This change added tests and can be verified as follows:
   
     - *Added integration tests in MaterializedTableStatementITCase*
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): (no)
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (no)
     - The serializers: (no)
     - The runtime per-record code paths (performance sensitive): (no)
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (no)
     - The S3 file system connector: (no)
   
   ## Documentation
   
     - Does this pull request introduce a new feature? (yes)
     - If yes, how is the feature documented? (docs)
   


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-35195][table] Support the execution of create materialized table in continuous refresh mode [flink]

Posted by "lsyldliu (via GitHub)" <gi...@apache.org>.
lsyldliu commented on PR #24750:
URL: https://github.com/apache/flink/pull/24750#issuecomment-2095912485

   > May I ask how to find the list of issues/requests I can contribute to?
   
   You can find it in jira: https://issues.apache.org/jira/projects/FLINK/issues/


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-35195][table] Support the execution of create materialized table in continuous refresh mode [flink]

Posted by "lsyldliu (via GitHub)" <gi...@apache.org>.
lsyldliu commented on code in PR #24750:
URL: https://github.com/apache/flink/pull/24750#discussion_r1591779149


##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/operations/materializedtable/AlterMaterializedTableChangeOperation.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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.operations.materializedtable;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.internal.TableResultImpl;
+import org.apache.flink.table.api.internal.TableResultInternal;
+import org.apache.flink.table.catalog.CatalogMaterializedTable;
+import org.apache.flink.table.catalog.ObjectIdentifier;
+import org.apache.flink.table.catalog.TableChange;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/** Alter dynamic table with new table definition and table changes represents the modification. */
+@Internal
+public class AlterMaterializedTableChangeOperation extends AlterMaterializedTableOperation {
+
+    private final List<TableChange.MaterializedTableChange> tableChanges;
+    private final CatalogMaterializedTable catalogMaterializedTable;
+
+    public AlterMaterializedTableChangeOperation(
+            ObjectIdentifier tableIdentifier,
+            List<TableChange.MaterializedTableChange> tableChanges,

Review Comment:
   Good catch, I think it is necessary.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-35195][table] Support the execution of create materialized table in continuous refresh mode [flink]

Posted by "lsyldliu (via GitHub)" <gi...@apache.org>.
lsyldliu merged PR #24750:
URL: https://github.com/apache/flink/pull/24750


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-35195][table] Support the execution of create materialized table in continuous refresh mode [flink]

Posted by "lsyldliu (via GitHub)" <gi...@apache.org>.
lsyldliu commented on code in PR #24750:
URL: https://github.com/apache/flink/pull/24750#discussion_r1591784050


##########
flink-table/flink-sql-gateway/src/main/java/org/apache/flink/table/gateway/service/operation/OperationExecutor.java:
##########
@@ -143,11 +145,14 @@ public class OperationExecutor {
 
     private final ClusterClientServiceLoader clusterClientServiceLoader;
 
+    private final MaterializedTableManager materializedTableManager;
+
     @VisibleForTesting
     public OperationExecutor(SessionContext context, Configuration executionConfig) {
         this.sessionContext = context;
         this.executionConfig = executionConfig;
         this.clusterClientServiceLoader = new DefaultClusterClientServiceLoader();
+        this.materializedTableManager = new MaterializedTableManager(this);

Review Comment:
   What do you see as the problem with circular references? I was mainly thinking about getting a global object because we needed to pass in conf information for the workflow scheduler for subsequent full refresh mode. Of course, this way also makes sense to me.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-35195][table] Support the execution of create materialized table in continuous refresh mode [flink]

Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #24750:
URL: https://github.com/apache/flink/pull/24750#issuecomment-2084709081

   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "9970141f26ce36804d528727728eb23648a1201f",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "9970141f26ce36804d528727728eb23648a1201f",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 9970141f26ce36804d528727728eb23648a1201f UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-35195][table] Support the execution of create materialized table in continuous refresh mode [flink]

Posted by "lsyldliu (via GitHub)" <gi...@apache.org>.
lsyldliu commented on code in PR #24750:
URL: https://github.com/apache/flink/pull/24750#discussion_r1591862269


##########
flink-table/flink-sql-gateway/src/main/java/org/apache/flink/table/gateway/service/operation/OperationExecutor.java:
##########
@@ -143,11 +145,14 @@ public class OperationExecutor {
 
     private final ClusterClientServiceLoader clusterClientServiceLoader;
 
+    private final MaterializedTableManager materializedTableManager;
+
     @VisibleForTesting
     public OperationExecutor(SessionContext context, Configuration executionConfig) {
         this.sessionContext = context;
         this.executionConfig = executionConfig;
         this.clusterClientServiceLoader = new DefaultClusterClientServiceLoader();
+        this.materializedTableManager = new MaterializedTableManager(this);

Review Comment:
   I have changed MaterializedTableManager to as static util class.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-35195][table] Support the execution of create materialized table in continuous refresh mode [flink]

Posted by "hackergin (via GitHub)" <gi...@apache.org>.
hackergin commented on code in PR #24750:
URL: https://github.com/apache/flink/pull/24750#discussion_r1591948579


##########
flink-table/flink-sql-gateway/src/main/java/org/apache/flink/table/gateway/service/materializedtable/MaterializedTableManager.java:
##########
@@ -0,0 +1,182 @@
+/*
+ * 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.gateway.service.materializedtable;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.catalog.CatalogMaterializedTable;
+import org.apache.flink.table.catalog.ObjectIdentifier;
+import org.apache.flink.table.catalog.TableChange;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.gateway.api.operation.OperationHandle;
+import org.apache.flink.table.gateway.api.results.ResultSet;
+import org.apache.flink.table.gateway.service.operation.OperationExecutor;
+import org.apache.flink.table.gateway.service.result.ResultFetcher;
+import org.apache.flink.table.gateway.service.utils.SqlExecutionException;
+import org.apache.flink.table.operations.materializedtable.AlterMaterializedTableChangeOperation;
+import org.apache.flink.table.operations.materializedtable.CreateMaterializedTableOperation;
+import org.apache.flink.table.operations.materializedtable.DropMaterializedTableOperation;
+import org.apache.flink.table.operations.materializedtable.MaterializedTableOperation;
+import org.apache.flink.table.refresh.ContinuousRefreshHandler;
+import org.apache.flink.table.refresh.ContinuousRefreshHandlerSerializer;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.apache.flink.api.common.RuntimeExecutionMode.STREAMING;
+import static org.apache.flink.configuration.DeploymentOptions.TARGET;
+import static org.apache.flink.configuration.ExecutionOptions.RUNTIME_MODE;
+import static org.apache.flink.configuration.PipelineOptions.NAME;
+import static org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions.CHECKPOINTING_INTERVAL;
+import static org.apache.flink.table.api.internal.TableResultInternal.TABLE_RESULT_OK;
+
+/** Manager is responsible for execute the {@link MaterializedTableOperation}. */
+@Internal
+public class MaterializedTableManager {
+
+    private static final Logger LOG = LoggerFactory.getLogger(MaterializedTableManager.class);
+
+    public static ResultFetcher callMaterializedTableOperation(
+            OperationExecutor operationExecutor,
+            OperationHandle handle,
+            MaterializedTableOperation op,
+            String statement) {
+        if (op instanceof CreateMaterializedTableOperation) {
+            return callCreateMaterializedTableOperation(
+                    operationExecutor, handle, (CreateMaterializedTableOperation) op);
+        }
+        throw new SqlExecutionException(
+                String.format(
+                        "Unsupported Operation %s for materialized table.", op.asSummaryString()));
+    }
+
+    private static ResultFetcher callCreateMaterializedTableOperation(
+            OperationExecutor operationExecutor,
+            OperationHandle handle,
+            CreateMaterializedTableOperation createMaterializedTableOperation) {
+        CatalogMaterializedTable materializedTable =
+                createMaterializedTableOperation.getCatalogMaterializedTable();
+        if (CatalogMaterializedTable.RefreshMode.CONTINUOUS == materializedTable.getRefreshMode()) {
+            createMaterializedInContinuousMode(
+                    operationExecutor, handle, createMaterializedTableOperation);
+        } else {
+            throw new SqlExecutionException(
+                    "Only support create materialized table in continuous refresh mode currently.");

Review Comment:
   The wording here is a bit strange.  How about change it to .  `Currently, we only support creating materialized tables with continuous refresh mode.`



##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/utils/MaterializedTableUtils.java:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.planner.utils;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.sql.parser.ddl.SqlRefreshMode;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.catalog.CatalogMaterializedTable;
+
+import org.apache.calcite.sql.SqlIntervalLiteral;
+import org.apache.calcite.sql.type.SqlTypeFamily;
+
+import java.time.Duration;
+
+/** The utils for materialized table. */
+@Internal
+public class MaterializedTableUtils {
+
+    public static Duration getMaterializedTableFreshness(SqlIntervalLiteral sqlIntervalLiteral) {
+        if (sqlIntervalLiteral.signum() < 0) {
+            throw new ValidationException(
+                    "Materialized table freshness doesn't support negative value.");
+        }
+        if (sqlIntervalLiteral.getTypeName().getFamily() != SqlTypeFamily.INTERVAL_DAY_TIME) {
+            throw new ValidationException(
+                    "Materialized table freshness only support SECOND, MINUTE, HOUR, DAY as the time unit.");
+        }
+
+        SqlIntervalLiteral.IntervalValue intervalValue =
+                sqlIntervalLiteral.getValueAs(SqlIntervalLiteral.IntervalValue.class);
+        long interval = Long.parseLong(intervalValue.getIntervalLiteral());
+        switch (intervalValue.getIntervalQualifier().typeName()) {
+            case INTERVAL_DAY:
+                return Duration.ofDays(interval);
+            case INTERVAL_HOUR:
+                return Duration.ofHours(interval);
+            case INTERVAL_MINUTE:
+                return Duration.ofMinutes(interval);
+            case INTERVAL_SECOND:
+                return Duration.ofSeconds(interval);
+            default:
+                throw new ValidationException(
+                        "Materialized table freshness only support SECOND, MINUTE, HOUR, DAY as the time unit.");
+        }
+    }
+
+    public static CatalogMaterializedTable.LogicalRefreshMode deriveLogicalRefreshMode(
+            SqlRefreshMode sqlRefreshMode) {
+        if (sqlRefreshMode == null) {
+            return CatalogMaterializedTable.LogicalRefreshMode.AUTOMATIC;
+        }
+
+        switch (sqlRefreshMode) {
+            case FULL:
+                return CatalogMaterializedTable.LogicalRefreshMode.FULL;
+            case CONTINUOUS:
+                return CatalogMaterializedTable.LogicalRefreshMode.CONTINUOUS;
+            default:
+                throw new ValidationException(
+                        String.format("Unsupported logical refresh mode: %s.", sqlRefreshMode));
+        }
+    }
+
+    public static CatalogMaterializedTable.RefreshMode deriveRefreshMode(

Review Comment:
   Add doc too. 



##########
flink-table/flink-sql-gateway/src/main/java/org/apache/flink/table/gateway/service/materializedtable/MaterializedTableManager.java:
##########
@@ -0,0 +1,182 @@
+/*
+ * 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.gateway.service.materializedtable;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.catalog.CatalogMaterializedTable;
+import org.apache.flink.table.catalog.ObjectIdentifier;
+import org.apache.flink.table.catalog.TableChange;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.gateway.api.operation.OperationHandle;
+import org.apache.flink.table.gateway.api.results.ResultSet;
+import org.apache.flink.table.gateway.service.operation.OperationExecutor;
+import org.apache.flink.table.gateway.service.result.ResultFetcher;
+import org.apache.flink.table.gateway.service.utils.SqlExecutionException;
+import org.apache.flink.table.operations.materializedtable.AlterMaterializedTableChangeOperation;
+import org.apache.flink.table.operations.materializedtable.CreateMaterializedTableOperation;
+import org.apache.flink.table.operations.materializedtable.DropMaterializedTableOperation;
+import org.apache.flink.table.operations.materializedtable.MaterializedTableOperation;
+import org.apache.flink.table.refresh.ContinuousRefreshHandler;
+import org.apache.flink.table.refresh.ContinuousRefreshHandlerSerializer;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.apache.flink.api.common.RuntimeExecutionMode.STREAMING;
+import static org.apache.flink.configuration.DeploymentOptions.TARGET;
+import static org.apache.flink.configuration.ExecutionOptions.RUNTIME_MODE;
+import static org.apache.flink.configuration.PipelineOptions.NAME;
+import static org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions.CHECKPOINTING_INTERVAL;
+import static org.apache.flink.table.api.internal.TableResultInternal.TABLE_RESULT_OK;
+
+/** Manager is responsible for execute the {@link MaterializedTableOperation}. */
+@Internal
+public class MaterializedTableManager {
+
+    private static final Logger LOG = LoggerFactory.getLogger(MaterializedTableManager.class);
+
+    public static ResultFetcher callMaterializedTableOperation(
+            OperationExecutor operationExecutor,
+            OperationHandle handle,
+            MaterializedTableOperation op,
+            String statement) {
+        if (op instanceof CreateMaterializedTableOperation) {
+            return callCreateMaterializedTableOperation(
+                    operationExecutor, handle, (CreateMaterializedTableOperation) op);
+        }
+        throw new SqlExecutionException(
+                String.format(
+                        "Unsupported Operation %s for materialized table.", op.asSummaryString()));
+    }
+
+    private static ResultFetcher callCreateMaterializedTableOperation(
+            OperationExecutor operationExecutor,
+            OperationHandle handle,
+            CreateMaterializedTableOperation createMaterializedTableOperation) {
+        CatalogMaterializedTable materializedTable =
+                createMaterializedTableOperation.getCatalogMaterializedTable();
+        if (CatalogMaterializedTable.RefreshMode.CONTINUOUS == materializedTable.getRefreshMode()) {
+            createMaterializedInContinuousMode(
+                    operationExecutor, handle, createMaterializedTableOperation);
+        } else {
+            throw new SqlExecutionException(
+                    "Only support create materialized table in continuous refresh mode currently.");
+        }
+        // Just return ok for unify different refresh job info of continuous and full mode, user
+        // should get the refresh job info via desc table.
+        return ResultFetcher.fromTableResult(handle, TABLE_RESULT_OK, false);
+    }
+
+    private static void createMaterializedInContinuousMode(
+            OperationExecutor operationExecutor,
+            OperationHandle handle,
+            CreateMaterializedTableOperation createMaterializedTableOperation) {
+        // create materialized table first
+        operationExecutor.callExecutableOperation(handle, createMaterializedTableOperation);
+
+        ObjectIdentifier materializedTableIdentifier =
+                createMaterializedTableOperation.getTableIdentifier();
+        CatalogMaterializedTable catalogMaterializedTable =
+                createMaterializedTableOperation.getCatalogMaterializedTable();
+
+        // Set job name, runtime mode, checkpoint interval
+        // TODO: Set minibatch related optimization options.
+        Configuration customConfig = new Configuration();
+        String jobName =
+                String.format(
+                        "Materialized_table_%s_continuous_refresh_job",
+                        materializedTableIdentifier.asSerializableString());
+        customConfig.set(NAME, jobName);
+        customConfig.set(RUNTIME_MODE, STREAMING);
+        customConfig.set(CHECKPOINTING_INTERVAL, catalogMaterializedTable.getFreshness());
+
+        String insertStatement =
+                String.format(
+                        "INSERT INTO %s %s",
+                        materializedTableIdentifier, catalogMaterializedTable.getDefinitionQuery());
+        try {
+            // submit flink streaming job
+            ResultFetcher resultFetcher =
+                    operationExecutor.executeStatement(handle, insertStatement);
+
+            // get execution.target and jobId, currently doesn't support yarn and k8s, so doesn't
+            // get clusterId
+            List<RowData> results = fetchAllResults(resultFetcher);
+            String jobId = results.get(0).getString(0).toString();
+            String executeTarget =
+                    operationExecutor.getSessionContext().getSessionConf().get(TARGET);
+            ContinuousRefreshHandler continuousRefreshHandler =
+                    new ContinuousRefreshHandler(executeTarget, jobId);
+            byte[] serializedBytes =
+                    ContinuousRefreshHandlerSerializer.INSTANCE.serialize(continuousRefreshHandler);
+
+            // update RefreshHandler to Catalog
+            CatalogMaterializedTable updatedMaterializedTable =
+                    catalogMaterializedTable.copy(
+                            CatalogMaterializedTable.RefreshStatus.ACTIVATED,
+                            continuousRefreshHandler.asSummaryString(),
+                            serializedBytes);
+            List<TableChange> tableChanges = new ArrayList<>();
+            tableChanges.add(
+                    TableChange.modifyRefreshStatus(
+                            CatalogMaterializedTable.RefreshStatus.ACTIVATED));
+            tableChanges.add(
+                    TableChange.modifyRefreshHandler(
+                            continuousRefreshHandler.asSummaryString(), serializedBytes));
+
+            AlterMaterializedTableChangeOperation alterMaterializedTableChangeOperation =
+                    new AlterMaterializedTableChangeOperation(
+                            materializedTableIdentifier, tableChanges, updatedMaterializedTable);
+            operationExecutor.callExecutableOperation(
+                    handle, alterMaterializedTableChangeOperation);
+        } catch (Exception e) {
+            // drop materialized table while submit flink streaming job occur exception. Thus, weak

Review Comment:
   ```suggestion
               // drop materialized table while submitting flink streaming job occur exception. Thus, weak
   ```



##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/utils/MaterializedTableUtils.java:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.planner.utils;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.sql.parser.ddl.SqlRefreshMode;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.catalog.CatalogMaterializedTable;
+
+import org.apache.calcite.sql.SqlIntervalLiteral;
+import org.apache.calcite.sql.type.SqlTypeFamily;
+
+import java.time.Duration;
+
+/** The utils for materialized table. */
+@Internal
+public class MaterializedTableUtils {
+
+    public static Duration getMaterializedTableFreshness(SqlIntervalLiteral sqlIntervalLiteral) {
+        if (sqlIntervalLiteral.signum() < 0) {
+            throw new ValidationException(
+                    "Materialized table freshness doesn't support negative value.");
+        }
+        if (sqlIntervalLiteral.getTypeName().getFamily() != SqlTypeFamily.INTERVAL_DAY_TIME) {
+            throw new ValidationException(
+                    "Materialized table freshness only support SECOND, MINUTE, HOUR, DAY as the time unit.");
+        }
+
+        SqlIntervalLiteral.IntervalValue intervalValue =
+                sqlIntervalLiteral.getValueAs(SqlIntervalLiteral.IntervalValue.class);
+        long interval = Long.parseLong(intervalValue.getIntervalLiteral());
+        switch (intervalValue.getIntervalQualifier().typeName()) {
+            case INTERVAL_DAY:
+                return Duration.ofDays(interval);
+            case INTERVAL_HOUR:
+                return Duration.ofHours(interval);
+            case INTERVAL_MINUTE:
+                return Duration.ofMinutes(interval);
+            case INTERVAL_SECOND:
+                return Duration.ofSeconds(interval);
+            default:
+                throw new ValidationException(
+                        "Materialized table freshness only support SECOND, MINUTE, HOUR, DAY as the time unit.");
+        }
+    }
+
+    public static CatalogMaterializedTable.LogicalRefreshMode deriveLogicalRefreshMode(

Review Comment:
   We can add some documentation to this function.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-35195][table] Support the execution of create materialized table in continuous refresh mode [flink]

Posted by "hackergin (via GitHub)" <gi...@apache.org>.
hackergin commented on code in PR #24750:
URL: https://github.com/apache/flink/pull/24750#discussion_r1591725527


##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/operations/materializedtable/AlterMaterializedTableChangeOperation.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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.operations.materializedtable;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.internal.TableResultImpl;
+import org.apache.flink.table.api.internal.TableResultInternal;
+import org.apache.flink.table.catalog.CatalogMaterializedTable;
+import org.apache.flink.table.catalog.ObjectIdentifier;
+import org.apache.flink.table.catalog.TableChange;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/** Alter dynamic table with new table definition and table changes represents the modification. */
+@Internal
+public class AlterMaterializedTableChangeOperation extends AlterMaterializedTableOperation {
+
+    private final List<TableChange.MaterializedTableChange> tableChanges;
+    private final CatalogMaterializedTable catalogMaterializedTable;
+
+    public AlterMaterializedTableChangeOperation(
+            ObjectIdentifier tableIdentifier,
+            List<TableChange.MaterializedTableChange> tableChanges,

Review Comment:
   The tableChanges here may be a regular tableChange such as SetOption, so I think it  we should change type to `List<TableChange>`?



##########
flink-table/flink-sql-gateway/src/main/java/org/apache/flink/table/gateway/service/materializedtable/MaterializedTableManager.java:
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.gateway.service.materializedtable;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.catalog.CatalogMaterializedTable;
+import org.apache.flink.table.catalog.ObjectIdentifier;
+import org.apache.flink.table.catalog.TableChange;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.gateway.api.operation.OperationHandle;
+import org.apache.flink.table.gateway.api.results.ResultSet;
+import org.apache.flink.table.gateway.service.operation.OperationExecutor;
+import org.apache.flink.table.gateway.service.result.ResultFetcher;
+import org.apache.flink.table.gateway.service.utils.SqlExecutionException;
+import org.apache.flink.table.operations.materializedtable.AlterMaterializedTableChangeOperation;
+import org.apache.flink.table.operations.materializedtable.CreateMaterializedTableOperation;
+import org.apache.flink.table.operations.materializedtable.DropMaterializedTableOperation;
+import org.apache.flink.table.operations.materializedtable.MaterializedTableOperation;
+import org.apache.flink.table.refresh.ContinuousRefreshHandler;
+import org.apache.flink.table.refresh.ContinuousRefreshHandlerSerializer;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.apache.flink.api.common.RuntimeExecutionMode.STREAMING;
+import static org.apache.flink.configuration.DeploymentOptions.TARGET;
+import static org.apache.flink.configuration.ExecutionOptions.RUNTIME_MODE;
+import static org.apache.flink.configuration.PipelineOptions.NAME;
+import static org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions.CHECKPOINTING_INTERVAL;
+import static org.apache.flink.table.api.internal.TableResultInternal.TABLE_RESULT_OK;
+
+/** Manager is responsible for execute the {@link MaterializedTableOperation}. */
+@Internal
+public class MaterializedTableManager {
+
+    private static final Logger LOG = LoggerFactory.getLogger(MaterializedTableManager.class);
+
+    private final OperationExecutor operationExecutor;
+
+    public MaterializedTableManager(OperationExecutor operationExecutor) {
+        this.operationExecutor = operationExecutor;
+    }
+
+    public ResultFetcher callMaterializedTableOperation(
+            OperationHandle handle, MaterializedTableOperation op, String statement) {
+        if (op instanceof CreateMaterializedTableOperation) {
+            return callCreateMaterializedTableOperation(
+                    handle, (CreateMaterializedTableOperation) op);
+        }
+        throw new SqlExecutionException(
+                String.format(
+                        "Unsupported Operation %s for materialized table.", op.asSummaryString()));
+    }
+
+    private ResultFetcher callCreateMaterializedTableOperation(
+            OperationHandle handle,
+            CreateMaterializedTableOperation createMaterializedTableOperation) {
+        CatalogMaterializedTable materializedTable =
+                createMaterializedTableOperation.getCatalogMaterializedTable();
+        if (CatalogMaterializedTable.RefreshMode.CONTINUOUS == materializedTable.getRefreshMode()) {
+            createMaterializedInContinuousMode(handle, createMaterializedTableOperation);
+        } else {
+            throw new SqlExecutionException(
+                    "Only support create materialized table in continuous refresh mode currently.");
+        }
+        // Just return ok for unify different refresh job info of continuous and full mode, user
+        // should get the refresh job info via desc table.
+        return ResultFetcher.fromTableResult(handle, TABLE_RESULT_OK, false);
+    }
+
+    private void createMaterializedInContinuousMode(
+            OperationHandle handle,
+            CreateMaterializedTableOperation createMaterializedTableOperation) {
+        // create materialized table first
+        operationExecutor.callExecutableOperation(handle, createMaterializedTableOperation);
+
+        ObjectIdentifier materializedTableIdentifier =
+                createMaterializedTableOperation.getTableIdentifier();
+        CatalogMaterializedTable catalogMaterializedTable =
+                createMaterializedTableOperation.getCatalogMaterializedTable();
+
+        // Set job name, runtime mode, checkpoint interval
+        // TODO: Set minibatch related optimization options.
+        Configuration customConfig = new Configuration();
+        String jobName =
+                String.format(
+                        "Materialized_table_%s_continuous_refresh_job",
+                        materializedTableIdentifier.asSerializableString());
+        customConfig.set(NAME, jobName);
+        customConfig.set(RUNTIME_MODE, STREAMING);
+        customConfig.set(CHECKPOINTING_INTERVAL, catalogMaterializedTable.getFreshness());
+
+        String insertStatement =
+                String.format(
+                        "INSERT INTO %s %s",
+                        materializedTableIdentifier, catalogMaterializedTable.getDefinitionQuery());
+        try {
+            // submit flink streaming job
+            ResultFetcher resultFetcher =
+                    operationExecutor.executeStatement(handle, insertStatement);
+
+            // get execution.target and jobId, currently doesn't support yarn and k8s, so doesn't
+            // get clusterId
+            List<RowData> results = fetchAllResults(resultFetcher);
+            String jobId = results.get(0).getString(0).toString();
+            String executeTarget =
+                    operationExecutor.getSessionContext().getSessionConf().get(TARGET);
+            ContinuousRefreshHandler continuousRefreshHandler =
+                    new ContinuousRefreshHandler(executeTarget, jobId);
+            byte[] serializedBytes =
+                    ContinuousRefreshHandlerSerializer.INSTANCE.serialize(continuousRefreshHandler);
+
+            // update RefreshHandler to Catalog
+            CatalogMaterializedTable updatedMaterializedTable =
+                    catalogMaterializedTable.copy(
+                            CatalogMaterializedTable.RefreshStatus.ACTIVATED,
+                            continuousRefreshHandler.asSummaryString(),
+                            serializedBytes);
+            List<TableChange.MaterializedTableChange> tableChanges = new ArrayList<>();
+            tableChanges.add(
+                    TableChange.modifyRefreshStatus(
+                            CatalogMaterializedTable.RefreshStatus.ACTIVATED));
+            tableChanges.add(
+                    TableChange.modifyRefreshHandler(
+                            continuousRefreshHandler.asSummaryString(), serializedBytes));
+
+            AlterMaterializedTableChangeOperation alterDynamicTableChangeOperation =

Review Comment:
   ```suggestion
               AlterMaterializedTableChangeOperation alterMaterializedTableChangeOperation =
   ```



##########
flink-table/flink-sql-gateway/src/main/java/org/apache/flink/table/gateway/service/operation/OperationExecutor.java:
##########
@@ -143,11 +145,14 @@ public class OperationExecutor {
 
     private final ClusterClientServiceLoader clusterClientServiceLoader;
 
+    private final MaterializedTableManager materializedTableManager;
+
     @VisibleForTesting
     public OperationExecutor(SessionContext context, Configuration executionConfig) {
         this.sessionContext = context;
         this.executionConfig = executionConfig;
         this.clusterClientServiceLoader = new DefaultClusterClientServiceLoader();
+        this.materializedTableManager = new MaterializedTableManager(this);

Review Comment:
   It seems that there may be a circular reference here? how about passing it in when calling callMaterializedTable?



##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/operations/materializedtable/AlterMaterializedTableChangeOperation.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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.operations.materializedtable;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.internal.TableResultImpl;
+import org.apache.flink.table.api.internal.TableResultInternal;
+import org.apache.flink.table.catalog.CatalogMaterializedTable;
+import org.apache.flink.table.catalog.ObjectIdentifier;
+import org.apache.flink.table.catalog.TableChange;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/** Alter dynamic table with new table definition and table changes represents the modification. */

Review Comment:
   ```suggestion
   /** Alter materialized table with new table definition and table changes represents the modification. */
   ```



##########
flink-table/flink-sql-gateway/src/main/java/org/apache/flink/table/gateway/service/materializedtable/MaterializedTableManager.java:
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.gateway.service.materializedtable;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.catalog.CatalogMaterializedTable;
+import org.apache.flink.table.catalog.ObjectIdentifier;
+import org.apache.flink.table.catalog.TableChange;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.gateway.api.operation.OperationHandle;
+import org.apache.flink.table.gateway.api.results.ResultSet;
+import org.apache.flink.table.gateway.service.operation.OperationExecutor;
+import org.apache.flink.table.gateway.service.result.ResultFetcher;
+import org.apache.flink.table.gateway.service.utils.SqlExecutionException;
+import org.apache.flink.table.operations.materializedtable.AlterMaterializedTableChangeOperation;
+import org.apache.flink.table.operations.materializedtable.CreateMaterializedTableOperation;
+import org.apache.flink.table.operations.materializedtable.DropMaterializedTableOperation;
+import org.apache.flink.table.operations.materializedtable.MaterializedTableOperation;
+import org.apache.flink.table.refresh.ContinuousRefreshHandler;
+import org.apache.flink.table.refresh.ContinuousRefreshHandlerSerializer;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.apache.flink.api.common.RuntimeExecutionMode.STREAMING;
+import static org.apache.flink.configuration.DeploymentOptions.TARGET;
+import static org.apache.flink.configuration.ExecutionOptions.RUNTIME_MODE;
+import static org.apache.flink.configuration.PipelineOptions.NAME;
+import static org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions.CHECKPOINTING_INTERVAL;
+import static org.apache.flink.table.api.internal.TableResultInternal.TABLE_RESULT_OK;
+
+/** Manager is responsible for execute the {@link MaterializedTableOperation}. */
+@Internal
+public class MaterializedTableManager {
+
+    private static final Logger LOG = LoggerFactory.getLogger(MaterializedTableManager.class);
+
+    private final OperationExecutor operationExecutor;
+
+    public MaterializedTableManager(OperationExecutor operationExecutor) {

Review Comment:
   When deserializing RefreshHandler, we may need a classloader. I think we can also pass sessionState to MaterializedTableManager?



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-35195][table] Support the execution of create materialized table in continuous refresh mode [flink]

Posted by "h12567 (via GitHub)" <gi...@apache.org>.
h12567 commented on PR #24750:
URL: https://github.com/apache/flink/pull/24750#issuecomment-2087873976

   May I ask how to find the list of issues/requests I can contribute to?


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-35195][table] Support the execution of create materialized table in continuous refresh mode [flink]

Posted by "lsyldliu (via GitHub)" <gi...@apache.org>.
lsyldliu commented on code in PR #24750:
URL: https://github.com/apache/flink/pull/24750#discussion_r1591779606


##########
flink-table/flink-sql-gateway/src/main/java/org/apache/flink/table/gateway/service/materializedtable/MaterializedTableManager.java:
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.gateway.service.materializedtable;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.catalog.CatalogMaterializedTable;
+import org.apache.flink.table.catalog.ObjectIdentifier;
+import org.apache.flink.table.catalog.TableChange;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.gateway.api.operation.OperationHandle;
+import org.apache.flink.table.gateway.api.results.ResultSet;
+import org.apache.flink.table.gateway.service.operation.OperationExecutor;
+import org.apache.flink.table.gateway.service.result.ResultFetcher;
+import org.apache.flink.table.gateway.service.utils.SqlExecutionException;
+import org.apache.flink.table.operations.materializedtable.AlterMaterializedTableChangeOperation;
+import org.apache.flink.table.operations.materializedtable.CreateMaterializedTableOperation;
+import org.apache.flink.table.operations.materializedtable.DropMaterializedTableOperation;
+import org.apache.flink.table.operations.materializedtable.MaterializedTableOperation;
+import org.apache.flink.table.refresh.ContinuousRefreshHandler;
+import org.apache.flink.table.refresh.ContinuousRefreshHandlerSerializer;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.apache.flink.api.common.RuntimeExecutionMode.STREAMING;
+import static org.apache.flink.configuration.DeploymentOptions.TARGET;
+import static org.apache.flink.configuration.ExecutionOptions.RUNTIME_MODE;
+import static org.apache.flink.configuration.PipelineOptions.NAME;
+import static org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions.CHECKPOINTING_INTERVAL;
+import static org.apache.flink.table.api.internal.TableResultInternal.TABLE_RESULT_OK;
+
+/** Manager is responsible for execute the {@link MaterializedTableOperation}. */
+@Internal
+public class MaterializedTableManager {
+
+    private static final Logger LOG = LoggerFactory.getLogger(MaterializedTableManager.class);
+
+    private final OperationExecutor operationExecutor;
+
+    public MaterializedTableManager(OperationExecutor operationExecutor) {

Review Comment:
   We can get `sessionState` from operationExecutor directly.



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org