gengziyand commented on code in PR #793: URL: https://github.com/apache/tsfile/pull/793#discussion_r3166441615
########## java/tools/src/main/java/org/apache/tsfile/tools/CsvSourceReader.java: ########## @@ -0,0 +1,290 @@ +/* + * 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.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.List; + +public class CsvSourceReader implements SourceReader { + + private static final Logger LOGGER = LoggerFactory.getLogger(CsvSourceReader.class); + private static final long DEFAULT_CHUNK_SIZE = 256L * 1024 * 1024; + + private final File sourceFile; + private ImportSchema schema; + private final long chunkSizeBytes; + private final String separator; + + private BufferedReader reader; + private String[] columnNames; + private boolean headerConsumed; + private boolean exhausted; + + private List<Object[]> bufferedSampleRows; + private String overrideTableName; + private String overrideTimePrecision; + + public CsvSourceReader(File sourceFile, ImportSchema schema) { + this(sourceFile, schema, DEFAULT_CHUNK_SIZE); + } + + public CsvSourceReader(File sourceFile, ImportSchema schema, long chunkSizeBytes) { + this.sourceFile = sourceFile; + this.schema = schema; + this.chunkSizeBytes = chunkSizeBytes; + this.separator = schema.getSeparator(); + this.headerConsumed = false; + this.exhausted = false; + } + + public CsvSourceReader(File sourceFile, String separator) { + this(sourceFile, separator, DEFAULT_CHUNK_SIZE); + } + + public CsvSourceReader(File sourceFile, String separator, long chunkSizeBytes) { + this.sourceFile = sourceFile; + this.schema = null; + this.chunkSizeBytes = chunkSizeBytes; + this.separator = separator != null ? separator : ","; + this.headerConsumed = false; + this.exhausted = false; + } + + 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 (no schema provided)"); + } + + try { + ensureReaderOpen(); + + String headerLine = reader.readLine(); + if (headerLine == null) { + throw new IllegalArgumentException("CSV file is empty: " + sourceFile.getAbsolutePath()); + } + columnNames = splitLine(headerLine); + headerConsumed = true; + + List<String> colNameList = new ArrayList<>(columnNames.length); + for (String name : columnNames) { + colNameList.add(name); + } + + bufferedSampleRows = new ArrayList<>(); + for (int i = 0; i < AutoSchemaInferer.DEFAULT_SAMPLE_SIZE; i++) { + String line = reader.readLine(); + if (line == null) { + exhausted = true; + break; + } + bufferedSampleRows.add(parseLineAutoMode(line)); + } + + String timeColumn = AutoSchemaInferer.detectTimeColumn(colNameList); + TSDataType[] types = + AutoSchemaInferer.inferColumnTypes( + colNameList, + bufferedSampleRows, + timeColumn, + AutoSchemaInferer.DEFAULT_CSV_NULL_TOKENS); + + String tableName = + overrideTableName != null + ? overrideTableName + : AutoSchemaInferer.deriveTableName(sourceFile.getName(), "csv_data"); + String timePrecision = overrideTimePrecision != null ? overrideTimePrecision : "ms"; + + schema = + AutoSchemaInferer.buildAutoSchema( + tableName, timeColumn, colNameList, types, timePrecision); + schema.setNullFormat("\\N"); + + return schema; + } catch (IOException e) { + throw new RuntimeException("Failed to infer schema from: " + sourceFile.getAbsolutePath(), e); + } + } + + @Override + public SourceBatch readBatch() { + boolean hasBuffered = bufferedSampleRows != null && !bufferedSampleRows.isEmpty(); + if (exhausted && !hasBuffered) { + return null; + } + + try { + ensureReaderOpen(); + + if (schema.isHasHeader() && !headerConsumed) { + String headerLine = reader.readLine(); + if (headerLine == null) { + exhausted = true; + return null; + } + columnNames = splitLine(headerLine); + validateColumnCount(); + headerConsumed = true; + } else if (!headerConsumed) { + columnNames = buildColumnNamesFromSchema(); + headerConsumed = true; + } + + List<Object[]> rows = new ArrayList<>(); + long currentSize = 0; + + if (hasBuffered) { + rows.addAll(bufferedSampleRows); + bufferedSampleRows = null; + } + + if (!exhausted) { + String line; + while ((line = reader.readLine()) != null) { + byte[] lineBytes = line.getBytes(StandardCharsets.UTF_8); + long lineSize = lineBytes.length; + + if (currentSize > 0 && currentSize + lineSize > chunkSizeBytes) { Review Comment: Fixed. I added the missing currentSize update before returning the batch at the chunk boundary. -- 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]
