gengziyand commented on code in PR #793: URL: https://github.com/apache/tsfile/pull/793#discussion_r3166397155
########## java/tools/src/main/java/org/apache/tsfile/tools/ArrowSourceReader.java: ########## @@ -0,0 +1,298 @@ +/* + * 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.tsfile.tools; + +import org.apache.tsfile.enums.TSDataType; + +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.memory.RootAllocator; +import org.apache.arrow.vector.BigIntVector; +import org.apache.arrow.vector.BitVector; +import org.apache.arrow.vector.FieldVector; +import org.apache.arrow.vector.Float4Vector; +import org.apache.arrow.vector.Float8Vector; +import org.apache.arrow.vector.IntVector; +import org.apache.arrow.vector.VarBinaryVector; +import org.apache.arrow.vector.VarCharVector; +import org.apache.arrow.vector.VectorSchemaRoot; +import org.apache.arrow.vector.ipc.ArrowFileReader; +import org.apache.arrow.vector.ipc.message.ArrowBlock; +import org.apache.arrow.vector.types.pojo.ArrowType; +import org.apache.arrow.vector.types.pojo.Field; +import org.apache.arrow.vector.types.pojo.Schema; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ArrowSourceReader implements SourceReader { + + private static final Logger LOGGER = LoggerFactory.getLogger(ArrowSourceReader.class); + + private final File sourceFile; + private ImportSchema schema; + private BufferAllocator allocator; + private ArrowFileReader arrowReader; + private Schema arrowSchema; + private List<ArrowBlock> recordBatches; + private int currentBatchIndex; + private boolean exhausted; + + private String overrideTableName; + private String overrideTimePrecision; + + public ArrowSourceReader(File sourceFile, ImportSchema schema) { + this.sourceFile = sourceFile; + this.schema = schema; + this.exhausted = false; + this.currentBatchIndex = 0; + } + + public ArrowSourceReader(File sourceFile) { + this.sourceFile = sourceFile; + this.schema = null; + this.exhausted = false; + this.currentBatchIndex = 0; + } + + public void setOverrideTableName(String tableName) { + this.overrideTableName = tableName; + } + + public void setOverrideTimePrecision(String timePrecision) { + this.overrideTimePrecision = timePrecision; + } + + @Override + public ImportSchema inferSchema() { + if (schema != null) { + throw new UnsupportedOperationException("inferSchema() is only available in auto mode"); + } + + try { + ensureReaderOpen(); + + List<String> columnNames = new ArrayList<>(); + List<TSDataType> columnTypes = new ArrayList<>(); + String detectedTimePrecision = null; + + for (Field field : arrowSchema.getFields()) { + String name = field.getName(); + columnNames.add(name); + TSDataType tsType = mapArrowType(field.getType()); + columnTypes.add(tsType); + + if (("time".equals(name) || "TIME".equals(name)) && detectedTimePrecision == null) { + detectedTimePrecision = detectTimestampPrecision(field.getType()); + } + } + + String timeColumn = AutoSchemaInferer.detectTimeColumn(columnNames); + TSDataType[] types = columnTypes.toArray(new TSDataType[0]); + + String tableName = + overrideTableName != null + ? overrideTableName + : AutoSchemaInferer.deriveTableName(sourceFile.getName(), "arrow_data"); + + String timePrecision; + if (overrideTimePrecision != null) { + timePrecision = overrideTimePrecision; + } else if (detectedTimePrecision != null) { + timePrecision = detectedTimePrecision; + } else { + timePrecision = "ms"; + } + + schema = + AutoSchemaInferer.buildAutoSchema( + tableName, timeColumn, columnNames, types, timePrecision); + return schema; + } catch (IOException e) { + throw new RuntimeException("Failed to infer schema from: " + sourceFile.getAbsolutePath(), e); + } + } + + @Override + public SourceBatch readBatch() { + if (exhausted) { + return null; + } + + try { + ensureReaderOpen(); + + if (currentBatchIndex >= recordBatches.size()) { + exhausted = true; + return null; + } + + arrowReader.loadRecordBatch(recordBatches.get(currentBatchIndex)); + currentBatchIndex++; + + VectorSchemaRoot root = arrowReader.getVectorSchemaRoot(); + int rowCount = root.getRowCount(); + if (rowCount == 0) { + if (currentBatchIndex >= recordBatches.size()) { + exhausted = true; + return null; + } + return readBatch(); + } + + List<String> schemaColumnNames = getSchemaColumnNames(); + Map<String, FieldVector> vectorMap = new HashMap<>(); + for (FieldVector vec : root.getFieldVectors()) { + vectorMap.put(vec.getName(), vec); + } + + int numCols = schemaColumnNames.size(); + List<Object[]> rows = new ArrayList<>(rowCount); + + for (int r = 0; r < rowCount; r++) { + Object[] row = new Object[numCols]; + for (int c = 0; c < numCols; c++) { + String colName = schemaColumnNames.get(c); + FieldVector vec = vectorMap.get(colName); + if (vec == null || vec.isNull(r)) { + row[c] = null; + } else { + row[c] = extractValue(vec, r); + } + } + rows.add(row); + } + + return SourceBatch.fromRows(schemaColumnNames, rows); + } catch (IOException e) { + LOGGER.error("Error reading Arrow file: " + sourceFile.getAbsolutePath(), e); + exhausted = true; + return null; + } + } + + @Override + public void close() { + if (arrowReader != null) { + try { + arrowReader.close(); + } catch (IOException e) { + LOGGER.error("Error closing Arrow reader", e); + } + arrowReader = null; + } + if (allocator != null) { + allocator.close(); + allocator = null; + } + } + + private void ensureReaderOpen() throws IOException { + if (arrowReader == null) { + allocator = new RootAllocator(); + arrowReader = new ArrowFileReader(new FileInputStream(sourceFile).getChannel(), allocator); Review Comment: Done. I now keep the FileInputStream as a field in ArrowSourceReader and close it explicitly in close() instead of creating it anonymously from getChannel(). -- 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]
