leaves12138 commented on code in PR #1846: URL: https://github.com/apache/incubator-paimon/pull/1846#discussion_r1302809775
########## paimon-core/src/main/java/org/apache/paimon/sort/zorder/ZIndexer.java: ########## @@ -0,0 +1,373 @@ +/* + * 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.sort.zorder; + +import org.apache.paimon.data.InternalRow; +import org.apache.paimon.data.Timestamp; +import org.apache.paimon.types.ArrayType; +import org.apache.paimon.types.BigIntType; +import org.apache.paimon.types.BinaryType; +import org.apache.paimon.types.BooleanType; +import org.apache.paimon.types.CharType; +import org.apache.paimon.types.DataField; +import org.apache.paimon.types.DataType; +import org.apache.paimon.types.DataTypeVisitor; +import org.apache.paimon.types.DateType; +import org.apache.paimon.types.DecimalType; +import org.apache.paimon.types.DoubleType; +import org.apache.paimon.types.FloatType; +import org.apache.paimon.types.IntType; +import org.apache.paimon.types.LocalZonedTimestampType; +import org.apache.paimon.types.MapType; +import org.apache.paimon.types.MultisetType; +import org.apache.paimon.types.RowType; +import org.apache.paimon.types.SmallIntType; +import org.apache.paimon.types.TimeType; +import org.apache.paimon.types.TimestampType; +import org.apache.paimon.types.TinyIntType; +import org.apache.paimon.types.VarBinaryType; +import org.apache.paimon.types.VarCharType; +import org.apache.paimon.utils.ByteBuffers; +import org.apache.paimon.utils.ZOrderByteUtils; + +import java.io.Serializable; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import java.util.function.Function; + +import static org.apache.paimon.utils.ZOrderByteUtils.PRIMITIVE_BUFFER_SIZE; + +/** Z-indexer for responsibility to generate z-index. */ +public class ZIndexer implements Serializable { + + private final Set<ZorderBaseFunction> functionSet; + private final int[] fieldsIndex; + private final int totalBytes; + private transient ByteBuffer reuse; + + public ZIndexer(RowType rowType, List<String> orderColumns) { + List<String> fields = rowType.getFieldNames(); + fieldsIndex = new int[orderColumns.size()]; + for (int i = 0; i < fieldsIndex.length; i++) { + int index = fields.indexOf(orderColumns.get(i)); + if (index == -1) { + throw new IllegalArgumentException( + "Can't find column: " + + orderColumns.get(i) + + " in row type fields: " + + fields); + } + fieldsIndex[i] = index; + } + this.functionSet = constructFunctionMap(rowType.getFields()); + this.totalBytes = PRIMITIVE_BUFFER_SIZE * this.fieldsIndex.length; + } + + public void open() { + this.reuse = ByteBuffer.allocate(totalBytes); + functionSet.forEach(ZorderBaseFunction::open); + } + + public int size() { + return totalBytes; + } + + public byte[] index(InternalRow row) { + byte[][] columnBytes = new byte[fieldsIndex.length][]; + + int index = 0; + for (ZorderBaseFunction f : functionSet) { + columnBytes[index++] = f.apply(row); + } + + return ZOrderByteUtils.interleaveBits(columnBytes, totalBytes, reuse); + } + + public Set<ZorderBaseFunction> constructFunctionMap(List<DataField> fields) { + Set<ZorderBaseFunction> zorderFunctionSet = new LinkedHashSet<>(); + // Construct zorderFunctionSet and fill dataTypes, rowFields + for (int fieldIndex = 0; fieldIndex < fieldsIndex.length; fieldIndex++) { + int index = fieldsIndex[fieldIndex]; + DataField field = fields.get(index); + zorderFunctionSet.add(zmapColumnToCalculator(field).setPosition(index)); Review Comment: done ########## paimon-core/src/main/java/org/apache/paimon/sort/zorder/ZIndexer.java: ########## @@ -0,0 +1,373 @@ +/* + * 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.sort.zorder; + +import org.apache.paimon.data.InternalRow; +import org.apache.paimon.data.Timestamp; +import org.apache.paimon.types.ArrayType; +import org.apache.paimon.types.BigIntType; +import org.apache.paimon.types.BinaryType; +import org.apache.paimon.types.BooleanType; +import org.apache.paimon.types.CharType; +import org.apache.paimon.types.DataField; +import org.apache.paimon.types.DataType; +import org.apache.paimon.types.DataTypeVisitor; +import org.apache.paimon.types.DateType; +import org.apache.paimon.types.DecimalType; +import org.apache.paimon.types.DoubleType; +import org.apache.paimon.types.FloatType; +import org.apache.paimon.types.IntType; +import org.apache.paimon.types.LocalZonedTimestampType; +import org.apache.paimon.types.MapType; +import org.apache.paimon.types.MultisetType; +import org.apache.paimon.types.RowType; +import org.apache.paimon.types.SmallIntType; +import org.apache.paimon.types.TimeType; +import org.apache.paimon.types.TimestampType; +import org.apache.paimon.types.TinyIntType; +import org.apache.paimon.types.VarBinaryType; +import org.apache.paimon.types.VarCharType; +import org.apache.paimon.utils.ByteBuffers; +import org.apache.paimon.utils.ZOrderByteUtils; + +import java.io.Serializable; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import java.util.function.Function; + +import static org.apache.paimon.utils.ZOrderByteUtils.PRIMITIVE_BUFFER_SIZE; + +/** Z-indexer for responsibility to generate z-index. */ +public class ZIndexer implements Serializable { + + private final Set<ZorderBaseFunction> functionSet; + private final int[] fieldsIndex; + private final int totalBytes; + private transient ByteBuffer reuse; + + public ZIndexer(RowType rowType, List<String> orderColumns) { + List<String> fields = rowType.getFieldNames(); + fieldsIndex = new int[orderColumns.size()]; + for (int i = 0; i < fieldsIndex.length; i++) { + int index = fields.indexOf(orderColumns.get(i)); + if (index == -1) { + throw new IllegalArgumentException( + "Can't find column: " + + orderColumns.get(i) + + " in row type fields: " + + fields); + } + fieldsIndex[i] = index; + } + this.functionSet = constructFunctionMap(rowType.getFields()); + this.totalBytes = PRIMITIVE_BUFFER_SIZE * this.fieldsIndex.length; + } + + public void open() { + this.reuse = ByteBuffer.allocate(totalBytes); + functionSet.forEach(ZorderBaseFunction::open); + } + + public int size() { + return totalBytes; + } + + public byte[] index(InternalRow row) { + byte[][] columnBytes = new byte[fieldsIndex.length][]; + + int index = 0; + for (ZorderBaseFunction f : functionSet) { + columnBytes[index++] = f.apply(row); + } + + return ZOrderByteUtils.interleaveBits(columnBytes, totalBytes, reuse); + } + + public Set<ZorderBaseFunction> constructFunctionMap(List<DataField> fields) { + Set<ZorderBaseFunction> zorderFunctionSet = new LinkedHashSet<>(); + // Construct zorderFunctionSet and fill dataTypes, rowFields + for (int fieldIndex = 0; fieldIndex < fieldsIndex.length; fieldIndex++) { + int index = fieldsIndex[fieldIndex]; + DataField field = fields.get(index); + zorderFunctionSet.add(zmapColumnToCalculator(field).setPosition(index)); + } + return zorderFunctionSet; + } + + public static ZorderBaseFunction zmapColumnToCalculator(DataField field) { + DataType type = field.type(); + return type.accept(new TypeVisitor()); + } + + /** Type Visitor to generate function map from row column to z-index. */ + public static class TypeVisitor implements DataTypeVisitor<ZorderBaseFunction> { + + @Override + public ZorderBaseFunction visit(CharType charType) { + return new ZorderStringFunction(); + } + + @Override + public ZorderBaseFunction visit(VarCharType varCharType) { + return new ZorderStringFunction(); + } + + @Override + public ZorderBaseFunction visit(BooleanType booleanType) { + return new ZorderBooleanFunction(); + } + + @Override + public ZorderBaseFunction visit(BinaryType binaryType) { + return new ZorderBytesFunction(); + } + + @Override + public ZorderBaseFunction visit(VarBinaryType varBinaryType) { + return new ZorderBytesFunction(); + } + + @Override + public ZorderBaseFunction visit(DecimalType decimalType) { + return new ZorderDecimalFunction(decimalType.getPrecision(), decimalType.getScale()); + } + + @Override + public ZorderBaseFunction visit(TinyIntType tinyIntType) { + return new ZorderTinyIntFunction(); + } + + @Override + public ZorderBaseFunction visit(SmallIntType smallIntType) { + return new ZorderShortFunction(); + } + + @Override + public ZorderBaseFunction visit(IntType intType) { + return new ZorderIntFunction(); + } + + @Override + public ZorderBaseFunction visit(BigIntType bigIntType) { + return new ZorderLongFunction(); + } + + @Override + public ZorderBaseFunction visit(FloatType floatType) { + return new ZorderFloatFunction(); + } + + @Override + public ZorderBaseFunction visit(DoubleType doubleType) { + return new ZorderDoubleFunction(); + } + + @Override + public ZorderBaseFunction visit(DateType dateType) { + return new ZorderDateFunction(); + } + + @Override + public ZorderBaseFunction visit(TimeType timeType) { + return new ZorderTimeFunction(); + } + + @Override + public ZorderBaseFunction visit(TimestampType timestampType) { + return new ZorderTimestampFunction(timestampType.getPrecision()); + } + + @Override + public ZorderBaseFunction visit(LocalZonedTimestampType localZonedTimestampType) { + return new ZorderTimestampFunction(localZonedTimestampType.getPrecision()); + } + + @Override + public ZorderBaseFunction visit(ArrayType arrayType) { + throw new RuntimeException("Unsupported type"); + } + + @Override + public ZorderBaseFunction visit(MultisetType multisetType) { + throw new RuntimeException("Unsupported type"); + } + + @Override + public ZorderBaseFunction visit(MapType mapType) { + throw new RuntimeException("Unsupported type"); + } + + @Override + public ZorderBaseFunction visit(RowType rowType) { + throw new RuntimeException("Unsupported type"); + } + } + + /** BaseFunction to convert row field record to devoted bytes. */ + public abstract static class ZorderBaseFunction Review Comment: done ########## paimon-core/src/main/java/org/apache/paimon/utils/ZOrderByteUtils.java: ########## @@ -0,0 +1,227 @@ +/* + * 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.utils; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; + +/** + * Within Z-Ordering the byte representations of objects being compared must be ordered, this + * requires several types to be transformed when converted to bytes. The goal is to map object's + * whose byte representation are not lexicographically ordered into representations that are + * lexicographically ordered. Bytes produced should be compared lexicographically as unsigned bytes, + * big-endian. + * + * <p>All types except for String are stored within an 8 Byte Buffer + * + * <p>Most of these techniques are derived from + * https://aws.amazon.com/blogs/database/z-order-indexing-for-multifaceted-queries-in-amazon-dynamodb-part-2/ + * + * <p>Most of the content of this class is referenced from Iceberg's ZOrderByteUtils. Review Comment: done -- 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]
