morningman commented on code in PR #17960: URL: https://github.com/apache/doris/pull/17960#discussion_r1150843897
########## fe/java-udf/src/main/java/org/apache/doris/jni/JniScanner.java: ########## @@ -0,0 +1,99 @@ +// 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.doris.jni; + +import org.apache.doris.jni.vec.ColumnType; +import org.apache.doris.jni.vec.ColumnValue; +import org.apache.doris.jni.vec.ScanPredicate; +import org.apache.doris.jni.vec.VectorTable; + +import java.io.IOException; + +public abstract class JniScanner { + protected VectorTable vectorTable; + protected String[] fields; + protected ColumnType[] types; + protected ScanPredicate[] predicates; + protected int batchSize; + + // Initialize JniScanner + public abstract void open() throws IOException; + + // Close JniScanner and release resources + public abstract void close() throws IOException; + + // Scan data and save as vector table + public abstract int getNext() throws IOException; + + protected void initTableInfo(ColumnType[] requiredTypes, String[] requiredFields, ScanPredicate[] predicates, + int batchSize) { + this.types = requiredTypes; + this.fields = requiredFields; + this.predicates = predicates; + this.batchSize = batchSize; + } + + protected void appendData(int index, ColumnValue value) { + vectorTable.appendData(index, value); + } + + protected int getBatchSize() { + return batchSize; + } + + public VectorTable getTable() { + return vectorTable; + } + + public long getNextBatchMeta() throws IOException { Review Comment: Looks like this `getNextBatchMeta` is the public method, and `getNext()` should be a `protected` method? ########## fe/java-udf/src/main/java/org/apache/doris/jni/vec/VectorColumn.java: ########## @@ -0,0 +1,573 @@ +// 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.doris.jni.vec; + +import org.apache.doris.jni.utils.OffHeap; +import org.apache.doris.jni.utils.TypeNativeBytes; +import org.apache.doris.jni.vec.ColumnType.Type; + +import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.List; + +/** + * Reference to Apache Spark + * see <a href="https://github.com/apache/spark/blob/master/sql/core/src/main/java/org/apache/spark/sql/execution/vectorized/WritableColumnVector.java">WritableColumnVector</a> + */ +public class VectorColumn { + // String is stored as array<byte> + // The default string length to initialize the capacity. + private static final int DEFAULT_STRING_LENGTH = 4; + + // NullMap column address + private long nullMap; + // Data column address + private long data; + + // For String / Array / Map. + private long offsets; + // Number of elements in vector column + private int capacity; + // Upper limit for the maximum capacity for this column. + private static final int MAX_CAPACITY = Integer.MAX_VALUE - 15; + private final ColumnType columnType; + + private int numNulls; + + private int appendIndex; + + // For nested column type: String / Array/ Map / Struct + private VectorColumn[] childColumns; + + public VectorColumn(ColumnType columnType, int capacity) { + this.columnType = columnType; + this.capacity = 0; + this.nullMap = 0; + this.data = 0; + this.offsets = 0; + this.numNulls = 0; + this.appendIndex = 0; + if (columnType.isComplexType()) { + List<ColumnType> children = columnType.getChildTypes(); + childColumns = new VectorColumn[children.size()]; + for (int i = 0; i < children.size(); ++i) { + childColumns[i] = new VectorColumn(children.get(i), capacity); + } + } else if (columnType.isStringType()) { + childColumns = new VectorColumn[1]; + childColumns[0] = new VectorColumn(new ColumnType("#data", Type.BYTE), capacity * DEFAULT_STRING_LENGTH); + } + + reserveCapacity(capacity); + } + + public long nullMapAddress() { + return nullMap; + } + + public long dataAddress() { + return data; + } + + public long offsetAddress() { + return offsets; + } + + /** + * Release columns and meta information + */ + public void close() { + if (childColumns != null) { + for (int i = 0; i < childColumns.length; i++) { + childColumns[i].close(); + childColumns[i] = null; + } + childColumns = null; + } + + if (nullMap != 0) { + OffHeap.freeMemory(nullMap); + } + if (data != 0) { + OffHeap.freeMemory(data); + } + if (offsets != 0) { + OffHeap.freeMemory(offsets); + } + nullMap = 0; + data = 0; + offsets = 0; + capacity = 0; + numNulls = 0; + appendIndex = 0; + } + + private void throwReserveException(int requiredCapacity, Throwable cause) { + String message = "Cannot reserve enough bytes in off heap memory (" + + (requiredCapacity >= 0 ? "requested " + requiredCapacity + " bytes" : "integer overflow)."); + throw new RuntimeException(message, cause); + } + + private void reserve(int requiredCapacity) { + if (requiredCapacity < 0) { + throwReserveException(requiredCapacity, null); + } else if (requiredCapacity > capacity) { + int newCapacity = (int) Math.min(MAX_CAPACITY, requiredCapacity * 2L); + if (requiredCapacity <= newCapacity) { + try { + reserveCapacity(newCapacity); + } catch (OutOfMemoryError outOfMemoryError) { + throwReserveException(requiredCapacity, outOfMemoryError); + } + } else { + // overflow + throwReserveException(requiredCapacity, null); + } + } + } + + private void reserveCapacity(int newCapacity) { + long oldCapacity = capacity; + long oldOffsetSize = capacity * 4L; + long newOffsetSize = newCapacity * 4L; + long typeSize = columnType.getTypeSize(); + if (columnType.isUnsupported()) { + // do nothing + return; + } else if (typeSize != -1) { + this.data = OffHeap.reallocateMemory(data, oldCapacity * typeSize, newCapacity * typeSize); + } else if (columnType.isStringType()) { + this.offsets = OffHeap.reallocateMemory(offsets, oldOffsetSize, newOffsetSize); + } else { + throw new RuntimeException("Unhandled type: " + columnType); + } + // todo: support complex type + this.nullMap = OffHeap.reallocateMemory(nullMap, oldCapacity, newCapacity); + OffHeap.setMemory(nullMap + oldCapacity, (byte) 0, newCapacity - oldCapacity); + capacity = newCapacity; + } + + public void reset() { + if (childColumns != null) { + for (VectorColumn c : childColumns) { + c.reset(); + } + } + appendIndex = 0; + if (numNulls > 0) { + putNotNulls(0, capacity); + numNulls = 0; + } + } + + public boolean isNullAt(int rowId) { + return OffHeap.getByte(null, nullMap + rowId) == 1; + } + + public boolean hasNull() { + return numNulls > 0; + } + + private void putNotNulls(int rowId, int count) { + if (!hasNull()) { + return; + } + long offset = nullMap + rowId; + for (int i = 0; i < count; ++i, ++offset) { + OffHeap.putByte(null, offset, (byte) 0); + } + } + + public int appendNull(ColumnType.Type typeValue) { + reserve(appendIndex + 1); + putNull(appendIndex); + // append default value + switch (typeValue) { + case BOOLEAN: + return appendBoolean(false); + case TINYINT: + return appendByte((byte) 0); + case SMALLINT: + return appendShort((short) 0); + case INT: + return appendInt(0); Review Comment: Why can we use 0 as null? ########## fe/java-udf/src/main/java/org/apache/doris/jni/vec/VectorTable.java: ########## @@ -0,0 +1,98 @@ +// 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.doris.jni.vec; + +import org.apache.doris.jni.vec.ColumnType.Type; + +/** + * Store a batch of data as vector table. + */ +public class VectorTable { + public VectorColumn[] columns; Review Comment: Should be private? ########## be/src/vec/exec/jni_connector.h: ########## @@ -0,0 +1,308 @@ +// 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. + +#pragma once + +#include <jni.h> + +#include "runtime/runtime_state.h" +#include "util/jni-util.h" + +namespace doris::vectorized { + +/** + * Connector to java jni scanner, which should extend org.apache.doris.jni.JniScanner + */ +class JniConnector { +public: + /** + * The predicates that can be pushed down to java side. + * Reference to java class org.apache.doris.jni.vec.ScanPredicate + */ + template <typename CppType> + struct ScanPredicate { + ScanPredicate() = default; + ~ScanPredicate() = default; + const std::string column_name; + SQLFilterOp op; + std::vector<const CppType*> values; + int scale; + + ScanPredicate(const std::string column_name) : column_name(std::move(column_name)) {} + + ScanPredicate(const ScanPredicate& other) { + column_name = other.column_name; + op = other.op; + for (auto v : other.values) { + values.emplace_back(v); + } + scale = other.scale; + } + + int length() { + int len = 4 + column_name.size() + 4 + 4 + 4; Review Comment: Add some commend to explain these number ########## fe/java-udf/src/main/java/org/apache/doris/jni/vec/VectorColumn.java: ########## @@ -0,0 +1,573 @@ +// 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.doris.jni.vec; + +import org.apache.doris.jni.utils.OffHeap; +import org.apache.doris.jni.utils.TypeNativeBytes; +import org.apache.doris.jni.vec.ColumnType.Type; + +import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.List; + +/** + * Reference to Apache Spark + * see <a href="https://github.com/apache/spark/blob/master/sql/core/src/main/java/org/apache/spark/sql/execution/vectorized/WritableColumnVector.java">WritableColumnVector</a> + */ +public class VectorColumn { + // String is stored as array<byte> + // The default string length to initialize the capacity. + private static final int DEFAULT_STRING_LENGTH = 4; + + // NullMap column address + private long nullMap; + // Data column address + private long data; + + // For String / Array / Map. + private long offsets; + // Number of elements in vector column + private int capacity; + // Upper limit for the maximum capacity for this column. + private static final int MAX_CAPACITY = Integer.MAX_VALUE - 15; + private final ColumnType columnType; + + private int numNulls; + + private int appendIndex; + + // For nested column type: String / Array/ Map / Struct + private VectorColumn[] childColumns; + + public VectorColumn(ColumnType columnType, int capacity) { + this.columnType = columnType; + this.capacity = 0; + this.nullMap = 0; + this.data = 0; + this.offsets = 0; + this.numNulls = 0; + this.appendIndex = 0; + if (columnType.isComplexType()) { + List<ColumnType> children = columnType.getChildTypes(); + childColumns = new VectorColumn[children.size()]; + for (int i = 0; i < children.size(); ++i) { + childColumns[i] = new VectorColumn(children.get(i), capacity); + } + } else if (columnType.isStringType()) { + childColumns = new VectorColumn[1]; + childColumns[0] = new VectorColumn(new ColumnType("#data", Type.BYTE), capacity * DEFAULT_STRING_LENGTH); + } + + reserveCapacity(capacity); + } + + public long nullMapAddress() { + return nullMap; + } + + public long dataAddress() { + return data; + } + + public long offsetAddress() { + return offsets; + } + + /** + * Release columns and meta information + */ + public void close() { + if (childColumns != null) { + for (int i = 0; i < childColumns.length; i++) { + childColumns[i].close(); + childColumns[i] = null; + } + childColumns = null; + } + + if (nullMap != 0) { + OffHeap.freeMemory(nullMap); + } + if (data != 0) { + OffHeap.freeMemory(data); + } + if (offsets != 0) { + OffHeap.freeMemory(offsets); + } + nullMap = 0; + data = 0; + offsets = 0; + capacity = 0; + numNulls = 0; + appendIndex = 0; + } + + private void throwReserveException(int requiredCapacity, Throwable cause) { + String message = "Cannot reserve enough bytes in off heap memory (" + + (requiredCapacity >= 0 ? "requested " + requiredCapacity + " bytes" : "integer overflow)."); + throw new RuntimeException(message, cause); + } + + private void reserve(int requiredCapacity) { + if (requiredCapacity < 0) { + throwReserveException(requiredCapacity, null); + } else if (requiredCapacity > capacity) { + int newCapacity = (int) Math.min(MAX_CAPACITY, requiredCapacity * 2L); + if (requiredCapacity <= newCapacity) { + try { + reserveCapacity(newCapacity); + } catch (OutOfMemoryError outOfMemoryError) { + throwReserveException(requiredCapacity, outOfMemoryError); + } + } else { + // overflow + throwReserveException(requiredCapacity, null); + } + } + } + + private void reserveCapacity(int newCapacity) { + long oldCapacity = capacity; + long oldOffsetSize = capacity * 4L; Review Comment: Why multiply 4? -- 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: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org