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


##########
paimon-core/src/main/java/org/apache/paimon/index/PartitionIndex.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.index;
+
+import org.apache.paimon.data.BinaryRow;
+import org.apache.paimon.manifest.IndexManifestEntry;
+import org.apache.paimon.utils.Int2ShortHashMap;
+import org.apache.paimon.utils.IntIterator;
+
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+
+import static org.apache.paimon.index.HashIndexFile.HASH_INDEX;
+
+/** Bucket Index Per Partition. */
+public class PartitionIndex {
+
+    public final Int2ShortHashMap hash2Bucket;
+
+    public final Map<Integer, Long> bucketInformation;
+
+    private final long targetBucketRowNumber;
+
+    public boolean accessed;
+
+    public long lastAccessedCommitIdentifier;
+
+    public PartitionIndex(
+            Int2ShortHashMap hash2Bucket,
+            Map<Integer, Long> bucketInformation,
+            long targetBucketRowNumber) {
+        this.hash2Bucket = hash2Bucket;
+        this.bucketInformation = bucketInformation;
+        this.targetBucketRowNumber = targetBucketRowNumber;
+        this.lastAccessedCommitIdentifier = Long.MIN_VALUE;
+        this.accessed = true;
+    }
+
+    public int assign(int hash) {

Review Comment:
   It is better to remove this method, let invoker to define a filter function.



##########
paimon-core/src/main/java/org/apache/paimon/table/Table.java:
##########
@@ -58,6 +58,8 @@ public interface Table extends Serializable {
     /** Optional comment of this table. */
     Optional<String> comment();
 
+    boolean isFileStoreTable();

Review Comment:
   `Table` is the top level API. Regarding the write path, maybe we need a new 
design that doesn't need to be exposed here yet.
   I think we can not expose this method now. We can just try cast to 
`FileStoreTable`.



##########
paimon-core/src/main/java/org/apache/paimon/table/sink/DynamicBucketRow.java:
##########
@@ -26,8 +26,10 @@
 import org.apache.paimon.data.Timestamp;
 import org.apache.paimon.types.RowKind;
 
+import java.io.Serializable;
+
 /** An {@link InternalRow} wraps another {@link InternalRow} with bucket. */
-public class DynamicBucketRow implements InternalRow {
+public class DynamicBucketRow implements InternalRow, Serializable {

Review Comment:
   I am concerns about kryo serialization here...
   
   Maybe we can just use `Tuple2<Row, Integer>` in `SparkWrite`?



##########
paimon-core/src/main/java/org/apache/paimon/index/PartitionIndex.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.index;
+
+import org.apache.paimon.data.BinaryRow;
+import org.apache.paimon.manifest.IndexManifestEntry;
+import org.apache.paimon.utils.Int2ShortHashMap;
+import org.apache.paimon.utils.IntIterator;
+
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+
+import static org.apache.paimon.index.HashIndexFile.HASH_INDEX;
+
+/** Bucket Index Per Partition. */
+public class PartitionIndex {
+
+    public final Int2ShortHashMap hash2Bucket;
+
+    public final Map<Integer, Long> bucketInformation;
+
+    private final long targetBucketRowNumber;
+
+    public boolean accessed;
+
+    public long lastAccessedCommitIdentifier;
+
+    public PartitionIndex(
+            Int2ShortHashMap hash2Bucket,
+            Map<Integer, Long> bucketInformation,
+            long targetBucketRowNumber) {
+        this.hash2Bucket = hash2Bucket;
+        this.bucketInformation = bucketInformation;
+        this.targetBucketRowNumber = targetBucketRowNumber;
+        this.lastAccessedCommitIdentifier = Long.MIN_VALUE;
+        this.accessed = true;
+    }
+
+    public int assign(int hash) {
+        return assign(hash, (bucket) -> Boolean.TRUE);
+    }
+
+    public int assign(int hash, Function<Integer, Boolean> bucketFilterFunc) {

Review Comment:
   A `java.util.function.IntPredicate` ?



##########
paimon-spark/paimon-spark-common/src/main/java/org/apache/paimon/spark/SparkWrite.java:
##########
@@ -75,6 +114,34 @@ public InsertableRelation toInsertableRelation() {
         };
     }
 
+    private boolean isDynamicBucketTable() {
+        return table.isFileStoreTable()
+                && ((FileStoreTable) table).bucketMode() == BucketMode.DYNAMIC;
+    }
+
+    private static Iterator<InternalRow> assignBucket(
+            FileStoreTable fileStoreTable, BatchWriteBuilder writeBuilder, 
Iterator<Row> rows) {
+        long targetBucketRowNumber = 
fileStoreTable.coreOptions().dynamicBucketTargetRowNum();
+        List<InternalRow> dynamicBucketRows = new ArrayList<>();

Review Comment:
   I think it is better to return a lazy Iterator for Spark 
`RDD.mapPartitions`, otherwise there is a risk of OOM



##########
paimon-core/src/main/java/org/apache/paimon/index/PartitionIndex.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.index;
+
+import org.apache.paimon.data.BinaryRow;
+import org.apache.paimon.manifest.IndexManifestEntry;
+import org.apache.paimon.utils.Int2ShortHashMap;
+import org.apache.paimon.utils.IntIterator;
+
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+
+import static org.apache.paimon.index.HashIndexFile.HASH_INDEX;
+
+/** Bucket Index Per Partition. */
+public class PartitionIndex {
+
+    public final Int2ShortHashMap hash2Bucket;
+
+    public final Map<Integer, Long> bucketInformation;
+
+    private final long targetBucketRowNumber;
+
+    public boolean accessed;
+
+    public long lastAccessedCommitIdentifier;
+
+    public PartitionIndex(
+            Int2ShortHashMap hash2Bucket,
+            Map<Integer, Long> bucketInformation,
+            long targetBucketRowNumber) {
+        this.hash2Bucket = hash2Bucket;
+        this.bucketInformation = bucketInformation;
+        this.targetBucketRowNumber = targetBucketRowNumber;
+        this.lastAccessedCommitIdentifier = Long.MIN_VALUE;
+        this.accessed = true;
+    }
+
+    public int assign(int hash) {
+        return assign(hash, (bucket) -> Boolean.TRUE);
+    }
+
+    public int assign(int hash, Function<Integer, Boolean> bucketFilterFunc) {
+        accessed = true;
+
+        // 1. is it a key that has appeared before
+        if (hash2Bucket.containsKey(hash)) {
+            return hash2Bucket.get(hash);
+        }
+
+        // 2. find bucket from existing buckets
+        for (Integer bucket : bucketInformation.keySet()) {
+            if (bucketFilterFunc.apply(bucket)) {
+                // it is my bucket
+                Long number = bucketInformation.get(bucket);
+                if (number < targetBucketRowNumber) {
+                    bucketInformation.put(bucket, number + 1);
+                    hash2Bucket.put(hash, bucket.shortValue());
+                    return bucket;
+                }
+            }
+        }
+
+        // 3. create a new bucket
+        for (int i = 0; i < Short.MAX_VALUE; i++) {
+            if (bucketFilterFunc.apply(i) && 
!bucketInformation.containsKey(i)) {
+                hash2Bucket.put(hash, (short) i);
+                bucketInformation.put(i, 1L);
+                return i;
+            }
+        }
+
+        @SuppressWarnings("OptionalGetWithoutIsPresent")
+        int maxBucket =
+                
bucketInformation.keySet().stream().mapToInt(Integer::intValue).max().getAsInt();
+        throw new RuntimeException(
+                String.format(
+                        "To more bucket %s, you should increase target bucket 
row number %s.",
+                        maxBucket, targetBucketRowNumber));
+    }
+
+    public static PartitionIndex loadIndex(

Review Comment:
   It is better to remove this method, let invoker to define a filter 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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to