JingsongLi commented on code in PR #2013:
URL: https://github.com/apache/incubator-paimon/pull/2013#discussion_r1328229717


##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/procedure/CompactProcedure.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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.paimon.flink.procedure;
+
+import org.apache.paimon.catalog.Identifier;
+import org.apache.paimon.flink.action.CompactAction;
+import org.apache.paimon.flink.action.SortCompactAction;
+import org.apache.paimon.flink.utils.StreamExecutionEnvironmentUtils;
+
+import org.apache.flink.api.common.RuntimeExecutionMode;
+import org.apache.flink.configuration.ExecutionOptions;
+import org.apache.flink.configuration.PipelineOptions;
+import org.apache.flink.configuration.ReadableConfig;
+import org.apache.flink.core.execution.JobClient;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.table.procedure.ProcedureContext;
+import org.apache.flink.table.procedures.Procedure;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import static 
org.apache.paimon.flink.action.ActionFactory.parseCommaSeparatedKeyValues;
+
+/**
+ * Compact procedure. Usage:
+ *
+ * <pre><code>
+ *  -- compact a table (tableId should be 'database_name.table_name')
+ *  CALL compact('tableId')
+ *
+ *  -- compact a table with sorting
+ *  CALL compact('tableId', 'order-strategy', 'order-by-columns')
+ *
+ *  -- compact specific partitions ('pt1=A,pt2=a', 'pt1=B,pt2=b', ...)
+ *  -- NOTE: if you don't need sorting but you want specify partitions, use '' 
as placeholder
+ *  CALL compact('tableId', '', '', partition1, partition2, ...)
+ * </code></pre>
+ */
+public class CompactProcedure implements Procedure {
+
+    private final String warehouse;
+    private final Map<String, String> catalogOptions;
+
+    public CompactProcedure(String warehouse, Map<String, String> 
catalogOptions) {
+        this.warehouse = warehouse;
+        this.catalogOptions = catalogOptions;
+    }
+
+    public String[] call(ProcedureContext procedureContext, String tableId) 
throws Exception {
+        return call(procedureContext, tableId, "", "");
+    }
+
+    public String[] call(
+            ProcedureContext procedureContext,
+            String tableId,
+            String orderStrategy,
+            String orderByColumns)
+            throws Exception {
+        return call(procedureContext, tableId, orderStrategy, orderByColumns, 
new String[0]);
+    }
+
+    public String[] call(
+            ProcedureContext procedureContext,
+            String tableId,
+            String orderStrategy,
+            String orderByColumns,
+            String... partitionStrings)
+            throws Exception {
+        Identifier identifier = Identifier.fromString(tableId);
+        CompactAction action;
+        String jobName;
+        if (orderStrategy.isEmpty() && orderByColumns.isEmpty()) {
+            action =
+                    new CompactAction(
+                            warehouse,
+                            identifier.getDatabaseName(),
+                            identifier.getObjectName(),
+                            catalogOptions);
+            jobName = "Compact Job";
+        } else if (!orderStrategy.isEmpty() && !orderByColumns.isEmpty()) {
+            action =
+                    new SortCompactAction(
+                                    warehouse,
+                                    identifier.getDatabaseName(),
+                                    identifier.getObjectName(),
+                                    catalogOptions)
+                            .withOrderStrategy(orderStrategy)
+                            .withOrderColumns(orderByColumns.split(","));
+            jobName = "Sort Compact Job";
+        } else {
+            throw new IllegalArgumentException(
+                    "You must specify 'order strategy' and 'order by columns' 
both.");
+        }
+
+        if (partitionStrings.length != 0) {
+            List<Map<String, String>> partitions = new ArrayList<>();
+            for (String partition : partitionStrings) {
+                partitions.add(parseCommaSeparatedKeyValues(partition));
+            }
+            action.withPartitions(partitions);
+        }
+
+        StreamExecutionEnvironment env = 
procedureContext.getExecutionEnvironment();
+        action.build(env);
+
+        ReadableConfig conf = 
StreamExecutionEnvironmentUtils.getConfiguration(env);
+        String name = conf.getOptional(PipelineOptions.NAME).orElse(jobName);
+        if (conf.get(ExecutionOptions.RUNTIME_MODE) == 
RuntimeExecutionMode.STREAMING) {

Review Comment:
   We should use `TABLE_DML_SYNC` in Flink.



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

Reply via email to