leaves12138 commented on code in PR #7354:
URL: https://github.com/apache/paimon/pull/7354#discussion_r2910283544


##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/btree/BTreeIndexTopoBuilder.java:
##########
@@ -83,78 +90,165 @@ public static void buildIndex(
             indexBuilder = 
indexBuilder.withPartitionPredicate(partitionPredicate);
         }
 
-        List<DataSplit> splits = indexBuilder.scan();
-        Range range = calcRowRange(splits);
-        if (splits.isEmpty() || range == null) {
+        List<DataSplit> splits = 
splitByContiguousRowRange(indexBuilder.scan());
+        if (splits.isEmpty()) {
+            return;
+        }
+        Map<BinaryRow, Map<Range, List<DataSplit>>> partitionRangeSplits =
+                groupSplitsByRange(splits);
+        if (partitionRangeSplits.isEmpty()) {
             return;
         }
 
-        // 2. Select necessary columns (partition keys + index field + ROW_ID)
-        List<String> selectedColumns = new ArrayList<>(table.partitionKeys());
+        // 2. Select necessary columns (index field + ROW_ID)
+        List<String> selectedColumns = new ArrayList<>();
         selectedColumns.add(indexColumn);
 
         RowType readType = 
SpecialFields.rowTypeWithRowId(table.rowType().project(selectedColumns));
+        int indexFieldPos = readType.getFieldIndex(indexColumn);
+        int rowIdPos = readType.getFieldIndex(SpecialFields.ROW_ID.name());
+        DataType indexFieldType = readType.getTypeAt(indexFieldPos);
 
-        // 3. Calculate parallelism and sort
+        // 3. Calculate maximum parallelism bound
         long recordsPerRange = 
userOptions.get(BTreeIndexOptions.BTREE_INDEX_RECORDS_PER_RANGE);
-        int parallelism = Math.max((int) (range.count() / recordsPerRange), 1);
         int maxParallelism = 
userOptions.get(BTreeIndexOptions.BTREE_INDEX_BUILD_MAX_PARALLELISM);
-        parallelism = Math.min(parallelism, maxParallelism);
 
-        // 4. Create source from splits and select columns
-        DataStream<DataSplit> sourceStream =
-                env.fromData(new JavaTypeInfo<>(DataSplit.class), 
splits.toArray(new DataSplit[0]))
-                        .name("Global Index Source")
-                        .setParallelism(1);
-
-        ReadBuilder readBuilder = 
table.newReadBuilder().withReadType(readType);
-        DataStream<RowData> rowDataStream =
-                sourceStream
-                        .transform(
-                                "Read Data",
-                                
InternalTypeInfo.of(LogicalTypeConversion.toLogicalType(readType)),
-                                new ReadDataOperator(readBuilder))
-                        .setParallelism(parallelism);
-
-        // 5. Sort data using TableSorter style
-        // Configure sort info similar to SortCompactAction
+        // 4. Build one topology per contiguous row range
         CoreOptions coreOptions = table.coreOptions();
-        TableSortInfo sortInfo =
-                new TableSortInfo.Builder()
-                        .setSortColumns(selectedColumns)
-                        .setSortStrategy(CoreOptions.OrderType.ORDER)
-                        .setSinkParallelism(parallelism)
-                        .setLocalSampleSize(parallelism * 
coreOptions.getLocalSampleMagnification())
-                        .setGlobalSampleSize(parallelism * 1000)
-                        .setRangeNumber(parallelism * 10)
-                        .build();
-
-        // Use TableSorter for sorting
-        TableSorter sorter =
-                TableSorter.getSorter(env, rowDataStream, coreOptions, 
readType, sortInfo);
-        DataStream<RowData> sortedStream = sorter.sort();
-
-        // 6. Build index for each partition
-        DataStream<Committable> commitMessages =
-                sortedStream
-                        .transform(
-                                "write-btree-index",
-                                new CommittableTypeInfo(),
-                                new WriteIndexOperator(range, indexBuilder))
-                        .setParallelism(parallelism);
-
-        // 7. Commit all commit messages
-        commit(table, commitMessages);
+        ReadBuilder readBuilder = 
table.newReadBuilder().withReadType(readType);
+        List<String> sortColumns = new ArrayList<>();
+        sortColumns.add(indexColumn);
+        DataStream<Committable> allCommitMessages = null;
+        int partitionFieldSize = table.partitionKeys().size();
+        BinaryRowSerializer binaryRowSerializer = new 
BinaryRowSerializer(partitionFieldSize);
+        for (Map.Entry<BinaryRow, Map<Range, List<DataSplit>>> partitionEntry :
+                partitionRangeSplits.entrySet()) {
+            BinaryRow partition = partitionEntry.getKey();
+            for (Map.Entry<Range, List<DataSplit>> entry : 
partitionEntry.getValue().entrySet()) {
+                Range range = entry.getKey();
+                List<DataSplit> rangeSplits = entry.getValue();
+                if (rangeSplits.isEmpty()) {
+                    continue;
+                }
+                int parallelism = Math.max((int) (range.count() / 
recordsPerRange), 1);
+                parallelism = Math.min(parallelism, maxParallelism);
+
+                DataStream<DataSplit> sourceStream =
+                        env.fromData(
+                                        new JavaTypeInfo<>(DataSplit.class),
+                                        rangeSplits.toArray(new DataSplit[0]))
+                                .name("Global Index Source " + " range=" + 
range)
+                                .setParallelism(1);
+
+                DataStream<RowData> rowDataStream =
+                        sourceStream
+                                .transform(
+                                        "Read Data " + range,
+                                        InternalTypeInfo.of(
+                                                
LogicalTypeConversion.toLogicalType(readType)),
+                                        new ReadDataOperator(readBuilder))
+                                .setParallelism(parallelism);
+
+                TableSortInfo sortInfo =
+                        new TableSortInfo.Builder()
+                                .setSortColumns(sortColumns)
+                                .setSortStrategy(CoreOptions.OrderType.ORDER)
+                                .setSinkParallelism(parallelism)
+                                .setLocalSampleSize(
+                                        parallelism * 
coreOptions.getLocalSampleMagnification())
+                                .setGlobalSampleSize(parallelism * 1000)
+                                .setRangeNumber(parallelism * 10)
+                                .build();
+
+                TableSorter sorter =
+                        TableSorter.getSorter(env, rowDataStream, coreOptions, 
readType, sortInfo);
+                DataStream<RowData> sortedStream = sorter.sort();
+
+                DataStream<Committable> commitMessages =
+                        sortedStream
+                                .transform(
+                                        "write-btree-index " + range,
+                                        new CommittableTypeInfo(),
+                                        new WriteIndexOperator(
+                                                range,
+                                                partitionFieldSize,
+                                                
binaryRowSerializer.serializeToBytes(partition),
+                                                indexBuilder,
+                                                indexFieldPos,
+                                                rowIdPos,
+                                                indexFieldType))
+                                .setParallelism(parallelism);
+                allCommitMessages =
+                        allCommitMessages == null
+                                ? commitMessages
+                                : allCommitMessages.union(commitMessages);
+            }
+        }
+        if (allCommitMessages != null) {
+            commit(table, allCommitMessages);
+        }
 
         env.execute("Create btree global index for table: " + table.name());
     }
 
+    private static Map<BinaryRow, Map<Range, List<DataSplit>>> 
groupSplitsByRange(

Review Comment:
   OK



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/btree/BTreeIndexTopoBuilder.java:
##########
@@ -83,78 +90,165 @@ public static void buildIndex(
             indexBuilder = 
indexBuilder.withPartitionPredicate(partitionPredicate);
         }
 
-        List<DataSplit> splits = indexBuilder.scan();
-        Range range = calcRowRange(splits);
-        if (splits.isEmpty() || range == null) {
+        List<DataSplit> splits = 
splitByContiguousRowRange(indexBuilder.scan());
+        if (splits.isEmpty()) {
+            return;
+        }
+        Map<BinaryRow, Map<Range, List<DataSplit>>> partitionRangeSplits =
+                groupSplitsByRange(splits);
+        if (partitionRangeSplits.isEmpty()) {
             return;
         }
 
-        // 2. Select necessary columns (partition keys + index field + ROW_ID)
-        List<String> selectedColumns = new ArrayList<>(table.partitionKeys());
+        // 2. Select necessary columns (index field + ROW_ID)
+        List<String> selectedColumns = new ArrayList<>();
         selectedColumns.add(indexColumn);
 
         RowType readType = 
SpecialFields.rowTypeWithRowId(table.rowType().project(selectedColumns));
+        int indexFieldPos = readType.getFieldIndex(indexColumn);
+        int rowIdPos = readType.getFieldIndex(SpecialFields.ROW_ID.name());
+        DataType indexFieldType = readType.getTypeAt(indexFieldPos);
 
-        // 3. Calculate parallelism and sort
+        // 3. Calculate maximum parallelism bound
         long recordsPerRange = 
userOptions.get(BTreeIndexOptions.BTREE_INDEX_RECORDS_PER_RANGE);
-        int parallelism = Math.max((int) (range.count() / recordsPerRange), 1);
         int maxParallelism = 
userOptions.get(BTreeIndexOptions.BTREE_INDEX_BUILD_MAX_PARALLELISM);
-        parallelism = Math.min(parallelism, maxParallelism);
 
-        // 4. Create source from splits and select columns
-        DataStream<DataSplit> sourceStream =
-                env.fromData(new JavaTypeInfo<>(DataSplit.class), 
splits.toArray(new DataSplit[0]))
-                        .name("Global Index Source")
-                        .setParallelism(1);
-
-        ReadBuilder readBuilder = 
table.newReadBuilder().withReadType(readType);
-        DataStream<RowData> rowDataStream =
-                sourceStream
-                        .transform(
-                                "Read Data",
-                                
InternalTypeInfo.of(LogicalTypeConversion.toLogicalType(readType)),
-                                new ReadDataOperator(readBuilder))
-                        .setParallelism(parallelism);
-
-        // 5. Sort data using TableSorter style
-        // Configure sort info similar to SortCompactAction
+        // 4. Build one topology per contiguous row range
         CoreOptions coreOptions = table.coreOptions();
-        TableSortInfo sortInfo =
-                new TableSortInfo.Builder()
-                        .setSortColumns(selectedColumns)
-                        .setSortStrategy(CoreOptions.OrderType.ORDER)
-                        .setSinkParallelism(parallelism)
-                        .setLocalSampleSize(parallelism * 
coreOptions.getLocalSampleMagnification())
-                        .setGlobalSampleSize(parallelism * 1000)
-                        .setRangeNumber(parallelism * 10)
-                        .build();
-
-        // Use TableSorter for sorting
-        TableSorter sorter =
-                TableSorter.getSorter(env, rowDataStream, coreOptions, 
readType, sortInfo);
-        DataStream<RowData> sortedStream = sorter.sort();
-
-        // 6. Build index for each partition
-        DataStream<Committable> commitMessages =
-                sortedStream
-                        .transform(
-                                "write-btree-index",
-                                new CommittableTypeInfo(),
-                                new WriteIndexOperator(range, indexBuilder))
-                        .setParallelism(parallelism);
-
-        // 7. Commit all commit messages
-        commit(table, commitMessages);
+        ReadBuilder readBuilder = 
table.newReadBuilder().withReadType(readType);
+        List<String> sortColumns = new ArrayList<>();
+        sortColumns.add(indexColumn);
+        DataStream<Committable> allCommitMessages = null;
+        int partitionFieldSize = table.partitionKeys().size();
+        BinaryRowSerializer binaryRowSerializer = new 
BinaryRowSerializer(partitionFieldSize);
+        for (Map.Entry<BinaryRow, Map<Range, List<DataSplit>>> partitionEntry :
+                partitionRangeSplits.entrySet()) {
+            BinaryRow partition = partitionEntry.getKey();
+            for (Map.Entry<Range, List<DataSplit>> entry : 
partitionEntry.getValue().entrySet()) {
+                Range range = entry.getKey();

Review Comment:
   OK



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