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


##########
paimon-common/src/main/java/org/apache/paimon/statistics/TruncateStats.java:
##########
@@ -0,0 +1,127 @@
+/*
+ *
+ *  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.statistics;
+
+import org.apache.paimon.data.BinaryString;
+import org.apache.paimon.data.serializer.Serializer;
+import org.apache.paimon.format.FieldStats;
+import org.apache.paimon.utils.Preconditions;
+
+import javax.annotation.Nullable;
+
+import java.util.regex.Pattern;
+
+/**
+ * The truncate stats which will report null count, truncated min/max value. 
Currently, truncation
+ * only performs on the {@link BinaryString} value.
+ */
+public class TruncateStats extends AbstractStats {
+
+    public static final Pattern TRUNCATE_PATTERN = 
Pattern.compile("TRUNCATE\\((\\d+)\\)");
+
+    private final int length;
+
+    public TruncateStats(int length) {
+        Preconditions.checkArgument(length > 0, "Truncate length should larger 
than zero.");
+        this.length = length;
+    }
+
+    public int getLength() {
+        return length;
+    }
+
+    @Override
+    public void collect(Object field, Serializer<Object> fieldSerializer) {
+        if (field == null) {
+            nullCount++;
+            return;
+        }
+
+        // TODO use comparator for not comparable types and extract this logic 
to a util class
+        if (!(field instanceof Comparable)) {
+            return;
+        }
+
+        Comparable<Object> c = (Comparable<Object>) field;
+        if (minValue == null || c.compareTo(minValue) < 0) {
+            minValue = truncateMin(field, fieldSerializer);
+        }
+        if (maxValue == null || c.compareTo(maxValue) > 0) {
+            maxValue = truncateMax(field, fieldSerializer);
+        }
+    }
+
+    @Override
+    public FieldStats convert(FieldStats source) {
+        return new FieldStats(
+                truncateMin(source.minValue(), null),
+                truncateMax(source.maxValue(), null),
+                source.nullCount());
+    }
+
+    /** @return a truncated value less or equal than the old value. */
+    private Object truncateMin(Object field, @Nullable Serializer<Object> 
fieldSerializer) {
+        if (field == null) {
+            return null;
+        }
+        if (field instanceof BinaryString) {
+            return ((BinaryString) field).substring(0, length);
+        } else {
+            return fieldSerializer == null ? field : 
fieldSerializer.copy(field);
+        }
+    }
+
+    /** @return a value greater or equal than the old value. */
+    private Object truncateMax(Object field, @Nullable Serializer<Object> 
fieldSerializer) {
+        if (field == null) {
+            return null;
+        }
+        if (field instanceof BinaryString) {
+            BinaryString original = ((BinaryString) field);
+            BinaryString truncated = original.substring(0, length);
+
+            // No need to increment if the input length is under the truncate 
length
+            if (original.getSizeInBytes() == truncated.getSizeInBytes()) {
+                return field;
+            }
+
+            StringBuilder truncatedStringBuilder = new 
StringBuilder(truncated.toString());
+
+            // Try incrementing the code points from the end
+            for (int i = length - 1; i >= 0; i--) {
+                // Get the offset in the truncated string buffer where the 
number of unicode
+                // characters = i
+                int offsetByCodePoint = 
truncatedStringBuilder.offsetByCodePoints(0, i);

Review Comment:
   Is there some ut cases for this?



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