JingsongLi commented on code in PR #3384:
URL: https://github.com/apache/paimon/pull/3384#discussion_r1610932034


##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sorter/TableSortInfo.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.sorter;
+
+import org.apache.paimon.flink.FlinkConnectorOptions;
+
+import java.util.Collections;
+import java.util.List;
+
+import static 
org.apache.flink.shaded.guava31.com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * {@link TableSortInfo} is used to indicate the configuration details for 
table data sorting. This
+ * includes information about which columns to sort by, the sorting strategy 
(e.g., order, Z-order),
+ * whether to sort within each cluster, and sample sizes for local and global 
sample nodes.
+ */
+public class TableSortInfo {
+
+    private final List<String> sortColumns;
+
+    private final String sortStrategy;
+
+    private final boolean sortInCluster;
+
+    private final int rangeNumber;
+
+    private final int sinkParallelism;
+
+    private final int localSampleSize;
+
+    private final int globalSampleSize;
+
+    private TableSortInfo(
+            List<String> sortColumns,
+            String sortStrategy,
+            boolean sortInCluster,
+            int rangeNumber,
+            int sinkParallelism,
+            int localSampleSize,
+            int globalSampleSize) {
+        checkArgument(!sortColumns.isEmpty(), "Sort columns cannot be empty");

Review Comment:
   Don't need to check here, this is a private constructor.
   You can check in the Builder.build.



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/FlinkSinkBuilder.java:
##########
@@ -119,8 +133,86 @@ public FlinkSinkBuilder inputBounded(boolean bounded) {
         return this;
     }
 
+    /** Set the table sort info. */
+    public FlinkSinkBuilder setTableSortInfo(
+            String sortColumnsString,

Review Comment:
   respect option keys, sortColumnsString => clusterColumns



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/FlinkSinkBuilder.java:
##########
@@ -119,8 +133,86 @@ public FlinkSinkBuilder inputBounded(boolean bounded) {
         return this;
     }
 
+    /** Set the table sort info. */
+    public FlinkSinkBuilder setTableSortInfo(

Review Comment:
   `clusteringIfPossible`?



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sorter/TableSortInfo.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.sorter;
+
+import org.apache.paimon.flink.FlinkConnectorOptions;
+
+import java.util.Collections;
+import java.util.List;
+
+import static 
org.apache.flink.shaded.guava31.com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * {@link TableSortInfo} is used to indicate the configuration details for 
table data sorting. This
+ * includes information about which columns to sort by, the sorting strategy 
(e.g., order, Z-order),
+ * whether to sort within each cluster, and sample sizes for local and global 
sample nodes.
+ */
+public class TableSortInfo {
+
+    private final List<String> sortColumns;
+
+    private final String sortStrategy;
+
+    private final boolean sortInCluster;
+
+    private final int rangeNumber;
+
+    private final int sinkParallelism;
+
+    private final int localSampleSize;
+
+    private final int globalSampleSize;
+
+    private TableSortInfo(
+            List<String> sortColumns,
+            String sortStrategy,
+            boolean sortInCluster,
+            int rangeNumber,
+            int sinkParallelism,
+            int localSampleSize,
+            int globalSampleSize) {
+        checkArgument(!sortColumns.isEmpty(), "Sort columns cannot be empty");
+        checkArgument(!sortStrategy.isEmpty(), "Sort strategy cannot be 
empty");
+        checkArgument(rangeNumber > 0, "Range number must be positive");
+        checkArgument(
+                sinkParallelism > 0,
+                "The sink parallelism must be specified when sorting the table 
data. Please set it using the key: "
+                        + FlinkConnectorOptions.SINK_PARALLELISM.key());
+        checkArgument(localSampleSize > 0, "Local sample size must be 
positive");
+        checkArgument(globalSampleSize > 0, "Global sample size must be 
positive");
+        this.sortColumns = sortColumns;
+        this.sortStrategy = sortStrategy;
+        this.sortInCluster = sortInCluster;
+        this.rangeNumber = rangeNumber;
+        this.sinkParallelism = sinkParallelism;
+        this.localSampleSize = localSampleSize;
+        this.globalSampleSize = globalSampleSize;
+    }
+
+    public List<String> getSortColumns() {
+        return sortColumns;
+    }
+
+    public String getSortStrategy() {
+        return sortStrategy;
+    }
+
+    public boolean isSortInCluster() {
+        return sortInCluster;
+    }
+
+    public int getRangeNumber() {
+        return rangeNumber;
+    }
+
+    public int getLocalSampleSize() {
+        return localSampleSize;
+    }
+
+    public int getGlobalSampleSize() {
+        return globalSampleSize;
+    }
+
+    public int getSinkParallelism() {
+        return sinkParallelism;
+    }
+
+    /** Builder for {@link TableSortInfo}. */
+    public static class Builder {
+
+        private List<String> sortColumns = Collections.emptyList();
+
+        private String sortStrategy = "order";
+
+        private boolean sortInCluster = true;
+
+        private int rangeNumber = -1;
+
+        private int sinkParallelism = -1;
+
+        private int localSampleSize = -1;
+
+        private int globalSampleSize = -1;
+
+        public Builder setSortColumns(List<String> sortColumns) {
+            checkArgument(!sortColumns.isEmpty(), "Sort columns cannot be 
empty");
+            this.sortColumns = sortColumns;
+            return this;
+        }
+
+        public Builder setSortStrategy(String sortStrategy) {
+            checkArgument(!sortStrategy.isEmpty(), "Sort strategy cannot be 
empty");
+            this.sortStrategy = sortStrategy;
+            return this;
+        }
+
+        public Builder setSortInCluster(boolean sortInCluster) {
+            this.sortInCluster = sortInCluster;
+            return this;
+        }
+
+        public Builder setRangeNumber(int rangeNumber) {
+            checkArgument(rangeNumber > 0, "Range number must be positive");
+            this.rangeNumber = rangeNumber;
+            return this;
+        }
+
+        public Builder setSinkParallelism(int sinkParallelism) {
+            checkArgument(
+                    sinkParallelism > 0,
+                    "The sink parallelism must be specified when sorting the 
table data. Please set it using the key: "

Review Comment:
   use `checkArgument(boolean expression, String errorMessageTemplate, 
@CheckForNull Object... errorMessageArgs)`.



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/shuffle/RangeShuffle.java:
##########
@@ -154,20 +156,35 @@ public static <T> DataStream<Tuple2<T, RowData>> 
rangeShuffleByKey(
                                         BasicTypeInfo.INT_TYPE_INFO, 
input.getOutputType()),
                                 input.getParallelism());
 
-        // 4. Remove the partition id. (shuffle according range partition)
-        return new DataStream<>(
-                inputDataStream.getExecutionEnvironment(),
-                new OneInputTransformation<>(
-                        new PartitionTransformation<>(
-                                preparePartition,
-                                new CustomPartitionerWrapper<>(
-                                        new 
AssignRangeIndexOperator.RangePartitioner(rangeNum),
-                                        new 
AssignRangeIndexOperator.Tuple2KeySelector<>()),
-                                StreamExchangeMode.BATCH),
-                        "REMOVE KEY",
-                        new RemoveRangeIndexOperator<>(),
-                        input.getOutputType(),
-                        outParallelism));
+        PartitionTransformation<Tuple2<Integer, Tuple2<T, RowData>>> 
customPartitioner =
+                new PartitionTransformation<>(
+                        preparePartition,
+                        new CustomPartitionerWrapper<>(
+                                new 
AssignRangeIndexOperator.RangePartitioner(rangeNum),
+                                new 
AssignRangeIndexOperator.Tuple2KeySelector<>()),
+                        StreamExchangeMode.BATCH);
+
+        // 4. Remove the range index or both range index and key. (shuffle 
according range
+        // partition)
+        if (outputWithKey) {

Review Comment:
   You don't need to change here.
   You can just output with key, and introduce a RemoveKeyOperator.
   
   It should have no impact to performance if it is chaining to upstream 
operator.



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/FlinkSinkBuilder.java:
##########
@@ -119,8 +133,86 @@ public FlinkSinkBuilder inputBounded(boolean bounded) {
         return this;
     }
 
+    /** Set the table sort info. */
+    public FlinkSinkBuilder setTableSortInfo(
+            String sortColumnsString,
+            String sortStrategy,
+            boolean sortInCluster,
+            int sampleFactor) {
+        // 1. The table sort will be ignored if the sort columns are not 
specified.
+        if (sortColumnsString == null || sortColumnsString.isEmpty()) {
+            return this;
+        }
+        // 2. Check the table type.
+        checkState(
+                table.bucketMode().equals(BUCKET_UNAWARE),
+                "Clustering only supports bucket unaware table without primary 
keys.");
+        // 3. Check the sort columns.
+        List<String> sortColumns = Arrays.asList(sortColumnsString.split(","));
+        List<String> fieldNames = table.schema().fieldNames();
+        checkState(
+                new HashSet<>(fieldNames).containsAll(new 
HashSet<>(sortColumns)),
+                String.format(
+                        "Field names %s should contains all clustering column 
names %s.",
+                        fieldNames, sortColumns));
+        // 4. Check the execution mode.
+        checkState(input != null, "The input stream should be specified 
earlier.");
+        if (boundedInput == null) {
+            boundedInput = !FlinkSink.isStreaming(input);
+        }
+        checkState(boundedInput, "The clustering should be executed under 
batch mode.");

Review Comment:
   Here we should discuss semantics, maybe we can just not sort in streaming 
mode?



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sorter/TableSortInfo.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.sorter;
+
+import org.apache.paimon.flink.FlinkConnectorOptions;
+
+import java.util.Collections;
+import java.util.List;
+
+import static 
org.apache.flink.shaded.guava31.com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * {@link TableSortInfo} is used to indicate the configuration details for 
table data sorting. This
+ * includes information about which columns to sort by, the sorting strategy 
(e.g., order, Z-order),
+ * whether to sort within each cluster, and sample sizes for local and global 
sample nodes.
+ */
+public class TableSortInfo {
+
+    private final List<String> sortColumns;
+
+    private final String sortStrategy;
+
+    private final boolean sortInCluster;
+
+    private final int rangeNumber;
+
+    private final int sinkParallelism;
+
+    private final int localSampleSize;
+
+    private final int globalSampleSize;
+
+    private TableSortInfo(
+            List<String> sortColumns,
+            String sortStrategy,
+            boolean sortInCluster,
+            int rangeNumber,
+            int sinkParallelism,
+            int localSampleSize,
+            int globalSampleSize) {
+        checkArgument(!sortColumns.isEmpty(), "Sort columns cannot be empty");
+        checkArgument(!sortStrategy.isEmpty(), "Sort strategy cannot be 
empty");
+        checkArgument(rangeNumber > 0, "Range number must be positive");
+        checkArgument(
+                sinkParallelism > 0,
+                "The sink parallelism must be specified when sorting the table 
data. Please set it using the key: "
+                        + FlinkConnectorOptions.SINK_PARALLELISM.key());
+        checkArgument(localSampleSize > 0, "Local sample size must be 
positive");
+        checkArgument(globalSampleSize > 0, "Global sample size must be 
positive");
+        this.sortColumns = sortColumns;
+        this.sortStrategy = sortStrategy;
+        this.sortInCluster = sortInCluster;
+        this.rangeNumber = rangeNumber;
+        this.sinkParallelism = sinkParallelism;
+        this.localSampleSize = localSampleSize;
+        this.globalSampleSize = globalSampleSize;
+    }
+
+    public List<String> getSortColumns() {
+        return sortColumns;
+    }
+
+    public String getSortStrategy() {
+        return sortStrategy;
+    }
+
+    public boolean isSortInCluster() {
+        return sortInCluster;
+    }
+
+    public int getRangeNumber() {
+        return rangeNumber;
+    }
+
+    public int getLocalSampleSize() {
+        return localSampleSize;
+    }
+
+    public int getGlobalSampleSize() {
+        return globalSampleSize;
+    }
+
+    public int getSinkParallelism() {
+        return sinkParallelism;
+    }
+
+    /** Builder for {@link TableSortInfo}. */
+    public static class Builder {
+
+        private List<String> sortColumns = Collections.emptyList();
+
+        private String sortStrategy = "order";
+
+        private boolean sortInCluster = true;
+
+        private int rangeNumber = -1;
+
+        private int sinkParallelism = -1;
+
+        private int localSampleSize = -1;
+
+        private int globalSampleSize = -1;
+
+        public Builder setSortColumns(List<String> sortColumns) {
+            checkArgument(!sortColumns.isEmpty(), "Sort columns cannot be 
empty");
+            this.sortColumns = sortColumns;
+            return this;
+        }
+
+        public Builder setSortStrategy(String sortStrategy) {
+            checkArgument(!sortStrategy.isEmpty(), "Sort strategy cannot be 
empty");
+            this.sortStrategy = sortStrategy;
+            return this;
+        }
+
+        public Builder setSortInCluster(boolean sortInCluster) {
+            this.sortInCluster = sortInCluster;
+            return this;
+        }
+
+        public Builder setRangeNumber(int rangeNumber) {
+            checkArgument(rangeNumber > 0, "Range number must be positive");

Review Comment:
   move all check to `build`.



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