FrankChen021 commented on code in PR #19379:
URL: https://github.com/apache/druid/pull/19379#discussion_r4206911085


##########
processing/src/main/java/org/apache/druid/segment/transform/ScanTransformer.java:
##########
@@ -0,0 +1,339 @@
+/*
+ * 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.segment.transform;
+
+import org.apache.druid.data.input.InputRow;
+import org.apache.druid.data.input.InputRowListPlusRawValues;
+import org.apache.druid.data.input.ListBasedInputRow;
+import org.apache.druid.data.input.MapBasedInputRow;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.query.DataSource;
+import org.apache.druid.query.QueryContexts;
+import org.apache.druid.query.UnnestDataSource;
+import org.apache.druid.query.scan.ScanQuery;
+import org.apache.druid.segment.ColumnSelectorFactory;
+import org.apache.druid.segment.ColumnValueSelector;
+import org.apache.druid.segment.Cursor;
+import org.apache.druid.segment.CursorBuildSpec;
+import org.apache.druid.segment.CursorFactory;
+import org.apache.druid.segment.CursorHolder;
+import org.apache.druid.segment.Segment;
+import org.apache.druid.segment.SegmentMapFunction;
+import org.apache.druid.segment.VirtualColumn;
+import org.apache.druid.segment.column.ColumnHolder;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.segment.column.RowSignature;
+import org.apache.druid.segment.filter.Filters;
+import org.apache.druid.timeline.SegmentId;
+import org.joda.time.Interval;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ * A {@link BaseTransformer} that processes input rows through a reusable scan 
query cursor pipeline.
+ *
+ * <p>The pipeline is built once at construction: a {@link 
SettableRowCursorFactory} is wrapped by the
+ * scan query's {@link SegmentMapFunction} (e.g., unnest, filter). For each 
input row, the row is set
+ * on the factory and the cursor is {@link Cursor#reset reset} — no per-row 
segment or cursor allocation.
+ *
+ * <p>When the scan query produces zero output rows (e.g., null/missing 
arrays, or filter rejection),
+ * the input row is dropped. This matches native Druid UNNEST / CROSS JOIN 
semantics where
+ * null or empty arrays produce zero rows.
+ *
+ * <p>This class is not thread-safe. Each reader thread should have its own 
instance.
+ */
+public class ScanTransformer implements BaseTransformer
+{
+  private final ScanQuery query;
+  // Field names that must never be promoted to a dimension, regardless of 
whether they show up in this
+  // scan query's result columns. See resolveDimensionColumns() for why this 
can't be derived locally.
+  private final Set<String> dimensionExclusions;
+  private final SettableRowCursorFactory baseCursorFactory;
+  private final CursorHolder cursorHolder;
+  private Cursor cursor;
+
+  ScanTransformer(final ScanQuery scanQuery, final Set<String> 
dimensionExclusions)
+  {
+    this.query = scanQuery.withOverriddenContext(
+        Map.of(QueryContexts.TIMEOUT_KEY, 0)
+    );
+    this.dimensionExclusions = dimensionExclusions;
+
+    final RowSignature broadSignature = RowSignature.builder()
+                                                     
.add(ColumnHolder.TIME_COLUMN_NAME, ColumnType.LONG)
+                                                     .build();
+
+    final CursorBuildSpec cursorBuildSpec = CursorBuildSpec.builder()

Review Comment:
   [P2] Reject unsupported non-time ordering before pagination
   
   **Finding:** A ScanQuery can carry a non-time `orderBy`, but this 
transformer bypasses the normal scan-query execution path and neither validates 
that ordering nor passes it to the cursor build spec. It then applies `offset` 
and `limit` while iterating the cursor, so a transform with `orderBy` plus 
pagination silently ingests rows in natural cursor order; native scan execution 
rejects non-time ordering through `ScanQuery.verifyOrderByForNativeExecution`.
   
   **Suggestion:** Validate the embedded query with 
`ScanQuery.verifyOrderByForNativeExecution` (or explicitly reject non-time 
ordering) before applying offset and limit.



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