tarun11Mavani commented on code in PR #16727:
URL: https://github.com/apache/pinot/pull/16727#discussion_r2354407791


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/readers/PinotSegmentColumnReaderFactory.java:
##########
@@ -0,0 +1,151 @@
+/**
+ * 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 java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import org.apache.pinot.segment.spi.IndexSegment;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.ColumnReader;
+import org.apache.pinot.spi.data.readers.ColumnReaderFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * ColumnReaderFactory implementation for immutable Pinot segments.
+ *
+ * <p>This factory creates ColumnReader instances for reading data from Pinot 
segments
+ * in a columnar fashion. It handles:
+ * <ul>
+ *   <li>Creating readers for existing columns in the segment</li>
+ *   <li>Creating default value readers for new columns</li>
+ *   <li>Data type conversions between source and target schemas</li>
+ *   <li>Resource management for all created readers</li>
+ * </ul>
+ */
+public class PinotSegmentColumnReaderFactory implements ColumnReaderFactory {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(PinotSegmentColumnReaderFactory.class);
+
+  private final IndexSegment _indexSegment;
+  private Schema _targetSchema;
+  private final Map<String, ColumnReader> _columnReaders;
+
+  /**
+   * Create a PinotSegmentColumnReaderFactory.
+   *
+   * @param indexSegment Source segment to read from
+   */
+  public PinotSegmentColumnReaderFactory(IndexSegment indexSegment) {
+    _indexSegment = indexSegment;
+    _columnReaders = new HashMap<>();
+  }
+
+  @Override
+  public void init(Schema targetSchema) throws IOException {
+    _targetSchema = targetSchema;
+    LOGGER.info("Initialized PinotSegmentColumnReaderFactory with target 
schema containing {} columns",
+        targetSchema.getPhysicalColumnNames().size());
+  }
+
+  @Override
+  public Set<String> getAvailableColumns() {
+    return _indexSegment.getPhysicalColumnNames();
+  }
+
+  @Override
+  public int getNumDocs() {
+    return _indexSegment.getSegmentMetadata().getTotalDocs();
+  }
+
+  @Override
+  public ColumnReader createColumnReader(String columnName, FieldSpec 
targetFieldSpec) throws IOException {
+    if (_targetSchema == null) {
+      throw new IllegalStateException("Factory not initialized. Call init() 
first.");
+    }
+
+    // Check if we already have a reader for this column
+    ColumnReader existingReader = _columnReaders.get(columnName);
+    if (existingReader != null) {
+      return existingReader;
+    }
+
+    ColumnReader columnReader;
+
+    if (hasColumn(columnName)) {

Review Comment:
   We are skipping VirtualColumns in getAllColumnReaders but this can still be 
called for virtual column from somewhere else. We should skip VirtualColumns 
here as well.



##########
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:
   what if the data type is same but we are transforming from SV to MV?



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/creator/impl/SegmentColumnarIndexCreator.java:
##########
@@ -383,6 +384,46 @@ public void indexColumn(String columnName, @Nullable int[] 
sortedDocIds, IndexSe
     }
   }
 
+  /**
+   * Index a column using a ColumnReader (column-major approach).
+   * This method processes the column values using the iterator pattern from 
ColumnReader.
+   *
+   * @param columnName Name of the column to index
+   * @param columnReader ColumnReader for the column data
+   * @throws IOException if indexing fails
+   */
+  public void indexColumn(String columnName, ColumnReader columnReader) throws 
IOException {

Review Comment:
   Can add this method signature to the interface SegmentCreator. 



##########
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:
   Can define another implementation(CompactedColumnReader) of this in future 
to include validDocIds and sortedColumn to support compaction. This can be used 
to improve compact segment build time for UpsertCompaction and 
UpsertMergeCompact task.



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