ColinLeeo commented on code in PR #20729:
URL: https://github.com/apache/druid/pull/20729#discussion_r4403294133


##########
extensions-contrib/tsfile-extensions/pom.xml:
##########
@@ -0,0 +1,98 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+
+  <groupId>org.apache.druid.extensions.contrib</groupId>
+  <artifactId>druid-tsfile-extensions</artifactId>
+  <name>druid-tsfile-extensions</name>
+  <description>Apache TsFile input format for Druid batch 
ingestion</description>
+
+  <parent>
+    <artifactId>druid</artifactId>
+    <groupId>org.apache.druid</groupId>
+    <version>40.0.0-SNAPSHOT</version>
+    <relativePath>../../pom.xml</relativePath>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.druid</groupId>
+      <artifactId>druid-processing</artifactId>
+      <version>${project.parent.version}</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.tsfile</groupId>
+      <artifactId>tsfile</artifactId>
+    </dependency>

Review Comment:
   Registered both artifacts as a new entry in `licenses.yaml` (`module: 
extensions-contrib/druid-tsfile-extensions`, `license_name: Apache License 
version 2.0`, version 2.5.0, libraries `org.apache.tsfile:tsfile` and 
`org.apache.tsfile:common`) with the NOTICE text taken from the jars. Verified 
locally by running `generate-binary-license.py` and 
`generate-binary-notice.py`: both now render a 
`BINARY/EXTENSIONS/druid-tsfile-extensions` section with the two artifacts and 
the notice.
   



##########
extensions-contrib/tsfile-extensions/src/main/java/org/apache/druid/data/input/tsfile/TsFileInputEntityReader.java:
##########
@@ -0,0 +1,371 @@
+/*
+ * 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.druid.data.input.tsfile;
+
+import org.apache.druid.data.input.InputEntity;
+import org.apache.druid.data.input.InputRow;
+import org.apache.druid.data.input.InputRowSchema;
+import org.apache.druid.data.input.IntermediateRowParsingReader;
+import org.apache.druid.data.input.impl.MapInputRowParser;
+import org.apache.druid.error.InvalidInput;
+import org.apache.druid.java.util.common.CloseableIterators;
+import org.apache.druid.java.util.common.parsers.CloseableIterator;
+import org.apache.druid.java.util.common.parsers.ParseException;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.exception.read.ReadProcessException;
+import org.apache.tsfile.exception.write.NoMeasurementException;
+import org.apache.tsfile.exception.write.NoTableException;
+import org.apache.tsfile.file.metadata.TableSchema;
+import org.apache.tsfile.read.query.dataset.ResultSet;
+import org.apache.tsfile.read.v4.ITsFileReader;
+import org.apache.tsfile.read.v4.TsFileReaderBuilder;
+import org.apache.tsfile.write.schema.IMeasurementSchema;
+import org.joda.time.Interval;
+
+import javax.annotation.Nullable;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Reads the rows of a TsFile file with the TsFile table-model reader and 
converts them into
+ * {@link MapBasedInputRow}-compatible maps.
+ * <p>
+ * TsFile stores its own timestamp, which is exposed to Druid under the name 
{@value TsFileInputFormat#TIME_COLUMN_NAME}.
+ * Tag, field, and attribute columns are exposed as-is.
+ */
+public class TsFileInputEntityReader extends 
IntermediateRowParsingReader<Map<String, Object>>
+{
+  private final InputRowSchema inputRowSchema;
+  private final InputEntity source;
+  @Nullable
+  private final File temporaryDirectory;
+  @Nullable
+  private final List<String> tables;
+  @Nullable
+  private final List<String> columns;
+  @Nullable
+  private final Interval interval;
+
+  TsFileInputEntityReader(
+      InputRowSchema inputRowSchema,
+      InputEntity source,
+      @Nullable File temporaryDirectory,
+      @Nullable List<String> tables,
+      @Nullable List<String> columns,
+      @Nullable Interval interval
+  )
+  {
+    this.inputRowSchema = inputRowSchema;
+    this.source = source;
+    this.temporaryDirectory = temporaryDirectory;
+    this.tables = tables;
+    this.columns = columns;
+    this.interval = interval;
+  }
+
+  @Override
+  protected CloseableIterator<Map<String, Object>> intermediateRowIterator() 
throws IOException
+  {
+    final InputEntity.CleanableFile materialized =
+        source.fetch(temporaryDirectory, new 
byte[InputEntity.DEFAULT_FETCH_BUFFER_SIZE]);
+    final ITsFileReader reader;
+    try {
+      reader = new TsFileReaderBuilder().file(materialized.file()).build();
+    }
+    catch (IOException | RuntimeException e) {
+      materialized.close();
+      throw e;
+    }
+
+    final List<String> tableNames;
+    try {
+      tableNames = resolveTableNames(reader);
+    }
+    catch (IOException | RuntimeException e) {
+      reader.close();
+      materialized.close();
+      throw e;
+    }
+
+    return CloseableIterators.wrap(
+        new RowIterator(reader, tableNames),
+        () -> {
+          reader.close();
+          materialized.close();
+        }
+    );
+  }
+
+  @Override
+  protected InputEntity source()
+  {
+    return source;
+  }
+
+  @Override
+  protected List<InputRow> parseInputRows(Map<String, Object> intermediateRow) 
throws ParseException
+  {
+    return Collections.singletonList(MapInputRowParser.parse(inputRowSchema, 
intermediateRow));
+  }
+
+  @Override
+  protected List<Map<String, Object>> toMap(Map<String, Object> 
intermediateRow)
+  {
+    return Collections.singletonList(intermediateRow);
+  }
+
+  private List<String> resolveTableNames(ITsFileReader reader) throws 
IOException
+  {
+    final List<TableSchema> allTables = reader.getAllTableSchema();
+    if (allTables.isEmpty()) {
+      throw InvalidInput.exception(
+          "TsFile[%s] contains no table schema. Only table-model TsFiles are 
supported; tree-model files "
+          + "are not.",
+          source.getUri()
+      );
+    }
+    if (tables == null || tables.isEmpty()) {
+      return 
allTables.stream().map(TableSchema::getTableName).collect(Collectors.toList());
+    }
+
+    final Set<String> available =
+        allTables.stream().map(schema -> 
schema.getTableName().toLowerCase(Locale.ROOT)).collect(Collectors.toSet());
+    for (String table : tables) {
+      if (!available.contains(table.toLowerCase(Locale.ROOT))) {
+        throw InvalidInput.exception(
+            "Table[%s] does not exist in the file. Available tables: %s",
+            table,
+            
allTables.stream().map(TableSchema::getTableName).collect(Collectors.toList())
+        );
+      }
+    }
+    return tables;
+  }
+
+  private List<String> selectColumns(TableSchema tableSchema)
+  {
+    if (columns != null && !columns.isEmpty()) {

Review Comment:
   Fixed. With `columns` omitted, `selectColumns` now applies 
`inputRowSchema.getColumnsFilter()`, which the batch path builds from the 
timestamp column, the dimensions, the transforms and the aggregator inputs. The 
EXTERN path passes `ColumnsFilter.all()`, so there the projection comes from 
the spec's fixed dimensions, which is exactly the external table's column 
signature. Covered by `testProjectionFromColumnsFilter` and 
`testProjectionFromFixedDimensions`.
   
   One TsFile constraint surfaced while doing this: its table reader produces 
no rows when only tag columns are selected, so a tag-only projection would 
silently ingest nothing. That case now falls back to reading every column, and 
`testTagOnlyProjectionFallsBackToAllColumns` covers it.
   



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to