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


##########
paimon-mosaic/src/main/java/org/apache/paimon/format/mosaic/MosaicSimpleStatsExtractor.java:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.format.mosaic;
+
+import org.apache.paimon.format.SimpleColStats;
+import org.apache.paimon.format.SimpleStatsExtractor;
+import org.apache.paimon.fs.FileIO;
+import org.apache.paimon.fs.Path;
+import org.apache.paimon.mosaic.ColumnStatistics;
+import org.apache.paimon.mosaic.MosaicReader;
+import org.apache.paimon.statistics.SimpleColStatsCollector;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.RowType;
+import org.apache.paimon.utils.Pair;
+
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.paimon.format.mosaic.MosaicObjects.convertStatsValue;
+
+/** Extracts statistics from Mosaic file metadata. */
+public class MosaicSimpleStatsExtractor implements SimpleStatsExtractor {
+
+    private final RowType rowType;
+    private final SimpleColStatsCollector.Factory[] statsCollectors;
+
+    public MosaicSimpleStatsExtractor(
+            RowType rowType, SimpleColStatsCollector.Factory[] 
statsCollectors) {
+        this.rowType = rowType;
+        this.statsCollectors = statsCollectors;
+    }
+
+    @Override
+    public SimpleColStats[] extract(FileIO fileIO, Path path, long length) {
+        try (MosaicInputFileAdapter inputFile = new 
MosaicInputFileAdapter(fileIO, path);
+                BufferAllocator allocator = new RootAllocator();
+                MosaicReader reader = MosaicReader.open(inputFile, length, 
allocator)) {
+            return extractFromStats(reader.numRowGroups(), 
reader::getRowGroupStatistics, null);
+        } catch (IOException e) {
+            throw new RuntimeException("Failed to extract stats from " + path, 
e);
+        }
+    }
+
+    @Override
+    public SimpleColStats[] extract(
+            FileIO fileIO, Path path, long length, @Nullable Object 
writerMetadata) {
+        if (writerMetadata instanceof MosaicWriterMetadata) {
+            MosaicWriterMetadata meta = (MosaicWriterMetadata) writerMetadata;
+            Set<Integer> statsFieldIndices = 
resolveStatsFieldIndices(meta.statsColumnNames());
+            return extractFromStats(
+                    meta.numRowGroups(), meta::getRowGroupStatistics, 
statsFieldIndices);
+        }
+        return extract(fileIO, path, length);
+    }
+
+    @Override
+    public Pair<SimpleColStats[], FileInfo> extractWithFileInfo(
+            FileIO fileIO, Path path, long length) {
+        try (MosaicInputFileAdapter inputFile = new 
MosaicInputFileAdapter(fileIO, path);
+                BufferAllocator allocator = new RootAllocator();
+                MosaicReader reader = MosaicReader.open(inputFile, length, 
allocator)) {
+            int numRowGroups = reader.numRowGroups();
+            SimpleColStats[] stats =
+                    extractFromStats(numRowGroups, 
reader::getRowGroupStatistics, null);
+            long rowCount = 0;
+            for (int rg = 0; rg < numRowGroups; rg++) {
+                rowCount += reader.rowGroupNumRows(rg);
+            }
+            return Pair.of(stats, new FileInfo(rowCount));
+        } catch (IOException e) {
+            throw new RuntimeException("Failed to extract stats from " + path, 
e);
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    private SimpleColStats[] extractFromStats(
+            int numRowGroups,
+            RowGroupStatsProvider statsProvider,
+            @Nullable Set<Integer> statsFieldIndices) {
+        int fieldCount = rowType.getFieldCount();
+        List<String> fieldNames = rowType.getFieldNames();
+        Object[] minValues = new Object[fieldCount];
+        Object[] maxValues = new Object[fieldCount];
+        long[] nullCounts = new long[fieldCount];
+        Set<Integer> seenColumns = new HashSet<>();
+
+        for (int rg = 0; rg < numRowGroups; rg++) {
+            Map<String, ColumnStatistics> statsMap = 
statsProvider.getRowGroupStatistics(rg);
+            for (Map.Entry<String, ColumnStatistics> entry : 
statsMap.entrySet()) {
+                int colIdx = fieldNames.indexOf(entry.getKey());
+                if (colIdx < 0) {
+                    continue;
+                }
+
+                ColumnStatistics stat = entry.getValue();
+                seenColumns.add(colIdx);
+                nullCounts[colIdx] += stat.getNullCount();
+
+                if (stat.hasMinMax()) {
+                    DataType dataType = rowType.getFields().get(colIdx).type();
+                    Object min = convertStatsValue(stat.getMin(), dataType);
+                    Object max = convertStatsValue(stat.getMax(), dataType);
+                    if (min != null) {
+                        if (minValues[colIdx] == null) {
+                            minValues[colIdx] = min;
+                        } else {
+                            if (((Comparable<Object>) 
min).compareTo(minValues[colIdx]) < 0) {

Review Comment:
    returns  for BINARY/VARBINARY stats, but this aggregation path assumes 
every min/max value is . If a binary stats column spans more than one row 
group, the second row group will hit  here. Please either compare through 
Paimon's byte-array-aware comparator (for example ) or do not expose min/max 
for binary columns, and add a multi-row-group binary stats test.



##########
paimon-mosaic/src/main/java/org/apache/paimon/format/mosaic/MosaicWriterFactory.java:
##########
@@ -0,0 +1,61 @@
+/*
+ * 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.format.mosaic;
+
+import org.apache.paimon.format.FileFormatFactory;
+import org.apache.paimon.format.FormatWriter;
+import org.apache.paimon.format.FormatWriterFactory;
+import org.apache.paimon.fs.PositionOutputStream;
+import org.apache.paimon.types.RowType;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/** A factory to create Mosaic {@link FormatWriter}. */
+public class MosaicWriterFactory implements FormatWriterFactory {
+
+    private final RowType rowType;
+    private final FileFormatFactory.FormatContext formatContext;
+    private final List<String> statsColumnNames;
+    private final int numBuckets;
+
+    public MosaicWriterFactory(RowType rowType, 
FileFormatFactory.FormatContext formatContext) {
+        this.rowType = rowType;
+        this.formatContext = formatContext;
+        String statsColumnsValue = 
formatContext.options().get(MosaicFileFormat.STATS_COLUMNS);
+        if (statsColumnsValue == null || statsColumnsValue.trim().isEmpty()) {
+            this.statsColumnNames = new ArrayList<>();
+        } else {
+            this.statsColumnNames =
+                    Arrays.stream(statsColumnsValue.split(","))
+                            .map(String::trim)
+                            .filter(s -> !s.isEmpty())
+                            .collect(Collectors.toList());
+        }
+        this.numBuckets = 
formatContext.options().get(MosaicFileFormat.NUM_BUCKETS);
+    }
+
+    @Override
+    public FormatWriter create(PositionOutputStream out, String compression) 
throws IOException {

Review Comment:
   The  argument from Paimon's writer path is currently ignored.  always uses ' 
default compression (zstd) and only forwards the zstd level, so table options 
like  or  silently have no effect for Mosaic files. Please map supported Paimon 
compression values to Mosaic  (or reject unsupported ones explicitly) and add 
coverage for at least  and .



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