AshinGau commented on code in PR #17960: URL: https://github.com/apache/doris/pull/17960#discussion_r1151308730
########## 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: `capacity` is the number of elements in vector column, and `size` is the memory usage in vector column. We use `int` to store offsets, so the size of offsets should 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