Jackie-Jiang commented on code in PR #16727:
URL: https://github.com/apache/pinot/pull/16727#discussion_r2356677290
##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/recordtransformer/DataTypeTransformer.java:
##########
@@ -128,6 +89,62 @@ public void transform(GenericRow record) {
}
}
+ /**
+ * Convert a single value to the target PinotDataType.
+ * This method can be reused by other components that need single value
conversion.
+ *
+ * @param value The value to convert
+ * @param dest The target PinotDataType
+ * @param columnName The column name (for error reporting)
+ * @return The converted value
+ */
+ public static Object convertValue(@Nullable Object value, PinotDataType
dest, String columnName) {
Review Comment:
(minor) `value` is never `null` (caller ensures that). Let's remove the
`@Nullable` and null check. The return value could actually be `null`, so let's
annotate the return value as `@Nullable`. Also suggest renaming `dest` to
`targetType`
```suggestion
@Nullable
public static Object convertValue(Object value, PinotDataType targetType,
String columnName) {
```
##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/readers/PinotSegmentColumnReaderImpl.java:
##########
@@ -0,0 +1,146 @@
+/**
+ * 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.pinot.segment.local.segment.readers;
+
+import java.io.IOException;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.utils.PinotDataType;
+import org.apache.pinot.segment.local.recordtransformer.DataTypeTransformer;
+import org.apache.pinot.segment.spi.IndexSegment;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.readers.ColumnReader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Implementation of ColumnReader for Pinot segments.
+ *
+ * <p>This class wraps the existing PinotSegmentColumnReader and provides the
ColumnReader interface
+ * for columnar segment building. It handles:
+ * <ul>
+ * <li>Reading column values from Pinot segments</li>
+ * <li>Data type conversions when target schema differs from source</li>
+ * <li>Null value detection</li>
+ * <li>Resource cleanup</li>
+ * </ul>
+ */
+public class PinotSegmentColumnReaderImpl implements ColumnReader {
+ private static final Logger LOGGER =
LoggerFactory.getLogger(PinotSegmentColumnReaderImpl.class);
+
+ private final PinotSegmentColumnReader _segmentColumnReader;
+ private final String _columnName;
+ private final int _numDocs;
+ private final boolean _needsConversion;
+ private final FieldSpec _targetFieldSpec;
+
+ private int _currentIndex;
+ private Object _currentValue;
+ private boolean _currentIsNull;
+
+ /**
+ * Create a PinotSegmentColumnReaderImpl for an existing column in the
segment.
+ *
+ * @param indexSegment Source segment to read from
+ * @param columnName Name of the column
+ * @param targetFieldSpec Target field specification (may differ from source
for data type conversion)
+ */
+ public PinotSegmentColumnReaderImpl(IndexSegment indexSegment, String
columnName, FieldSpec targetFieldSpec) {
+ _segmentColumnReader = new PinotSegmentColumnReader(indexSegment,
columnName);
+ _columnName = columnName;
+ _numDocs = indexSegment.getSegmentMetadata().getTotalDocs();
+ _targetFieldSpec = targetFieldSpec;
+ _currentIndex = 0;
+
+ // Check if data type conversion is needed
+ FieldSpec sourceFieldSpec =
indexSegment.getSegmentMetadata().getSchema().getFieldSpecFor(columnName);
+ _needsConversion = sourceFieldSpec != null
Review Comment:
How about other `RecordTransformer`? Seems the current implementation can
only support `DataTypeTransformer`
##########
pinot-spi/src/main/java/org/apache/pinot/spi/data/readers/ColumnReader.java:
##########
@@ -0,0 +1,83 @@
+/**
+ * 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.pinot.spi.data.readers;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.Serializable;
+import javax.annotation.Nullable;
+
+
+/**
+ * The <code>ColumnReader</code> interface is used to read column data from
various data sources
+ * for columnar segment building. Unlike RecordReader which reads row-by-row,
ColumnReader provides
+ * column-wise access to data, enabling efficient columnar segment creation.
+ *
+ * <p>This interface follows an iterator pattern similar to RecordReader:
+ * <ul>
+ * <li>Sequential iteration over all values in a column using hasNext() and
next()</li>
+ * <li>Null value detection for the current value</li>
+ * <li>Rewind capability to restart iteration</li>
+ * <li>Resource cleanup</li>
+ * </ul>
+ *
+ * <p>Implementations should handle data type conversions, default values for
new columns,
+ * and efficient column-wise data access patterns.
+ */
+public interface ColumnReader extends Closeable, Serializable {
Review Comment:
This is mostly useful when reading from a column major file format such as
Parquet. Can you check if this interface can be used for Parquet reader?
##########
pinot-spi/src/main/java/org/apache/pinot/spi/data/readers/ColumnReader.java:
##########
@@ -0,0 +1,83 @@
+/**
+ * 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.pinot.spi.data.readers;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.Serializable;
+import javax.annotation.Nullable;
+
+
+/**
+ * The <code>ColumnReader</code> interface is used to read column data from
various data sources
+ * for columnar segment building. Unlike RecordReader which reads row-by-row,
ColumnReader provides
+ * column-wise access to data, enabling efficient columnar segment creation.
+ *
+ * <p>This interface follows an iterator pattern similar to RecordReader:
+ * <ul>
+ * <li>Sequential iteration over all values in a column using hasNext() and
next()</li>
+ * <li>Null value detection for the current value</li>
+ * <li>Rewind capability to restart iteration</li>
+ * <li>Resource cleanup</li>
+ * </ul>
+ *
+ * <p>Implementations should handle data type conversions, default values for
new columns,
+ * and efficient column-wise data access patterns.
+ */
+public interface ColumnReader extends Closeable, Serializable {
+
+ /**
+ * Return <code>true</code> if more values remain to be read in this column.
+ * <p>This method should not throw exception. Caller is not responsible for
handling exceptions from this method.
+ */
+ boolean hasNext();
+
+ /**
+ * Get the next value in the column.
+ * <p>This method should be called only if {@link #hasNext()} returns
<code>true</code>. Caller is responsible for
+ * handling exceptions from this method and skip the value if user wants to
continue reading the remaining values.
+ *
+ * @return Next column value, or null if the value is null
+ * @throws IOException If an I/O error occurs while reading
+ */
+ @Nullable
+ Object next() throws IOException;
+
+ /**
+ * Check if the current value (last returned by next()) is null.
+ * <p>This method should be called after {@link #next()} to check if the
returned value represents a null.
+ *
+ * @return true if the current value is null, false otherwise
+ */
+ boolean isNull();
Review Comment:
Why do we need this when `next()` can return `null`?
##########
pinot-spi/src/main/java/org/apache/pinot/spi/data/readers/ColumnReader.java:
##########
@@ -0,0 +1,83 @@
+/**
+ * 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.pinot.spi.data.readers;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.Serializable;
+import javax.annotation.Nullable;
+
+
+/**
+ * The <code>ColumnReader</code> interface is used to read column data from
various data sources
+ * for columnar segment building. Unlike RecordReader which reads row-by-row,
ColumnReader provides
+ * column-wise access to data, enabling efficient columnar segment creation.
+ *
+ * <p>This interface follows an iterator pattern similar to RecordReader:
+ * <ul>
+ * <li>Sequential iteration over all values in a column using hasNext() and
next()</li>
+ * <li>Null value detection for the current value</li>
+ * <li>Rewind capability to restart iteration</li>
+ * <li>Resource cleanup</li>
+ * </ul>
+ *
+ * <p>Implementations should handle data type conversions, default values for
new columns,
+ * and efficient column-wise data access patterns.
+ */
+public interface ColumnReader extends Closeable, Serializable {
+
+ /**
+ * Return <code>true</code> if more values remain to be read in this column.
+ * <p>This method should not throw exception. Caller is not responsible for
handling exceptions from this method.
+ */
+ boolean hasNext();
+
+ /**
+ * Get the next value in the column.
+ * <p>This method should be called only if {@link #hasNext()} returns
<code>true</code>. Caller is responsible for
+ * handling exceptions from this method and skip the value if user wants to
continue reading the remaining values.
+ *
+ * @return Next column value, or null if the value is null
+ * @throws IOException If an I/O error occurs while reading
+ */
+ @Nullable
+ Object next() throws IOException;
Review Comment:
(format) Reformat the changes with [Pinot
Style](https://docs.pinot.apache.org/developers/developers-and-contributors/code-setup#set-up-ide)
--
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]