xiangfu0 commented on code in PR #19311:
URL: https://github.com/apache/pinot/pull/19311#discussion_r4486999259


##########
pinot-query-planner/src/main/java/org/apache/pinot/calcite/rel/rules/PinotMatchExchangeNodeInsertRule.java:
##########
@@ -0,0 +1,156 @@
+/**
+ * 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.calcite.rel.rules;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import javax.annotation.Nullable;
+import org.apache.calcite.plan.Context;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelCollations;
+import org.apache.calcite.rel.RelDistributions;
+import org.apache.calcite.rel.RelFieldCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Exchange;
+import org.apache.calcite.rel.core.Match;
+import org.apache.calcite.tools.RelBuilderFactory;
+import org.apache.pinot.calcite.rel.logical.PinotLogicalExchange;
+import org.apache.pinot.calcite.rel.logical.PinotLogicalSortExchange;
+import org.apache.pinot.query.context.PlannerContext;
+import org.apache.pinot.spi.exception.QueryErrorCode;
+import org.apache.pinot.spi.exception.QueryException;
+import 
org.apache.pinot.spi.utils.CommonConstants.Broker.Request.QueryOptionKey;
+import org.immutables.value.Value;
+
+
+/// Special rule for Pinot, this rule is fixed to always insert an exchange 
below the MATCH_RECOGNIZE node, mirroring
+/// what [PinotWindowExchangeNodeInsertRule] does for `WINDOW`.
+///
+/// Row pattern recognition is only correct when every row of a `PARTITION BY` 
partition is evaluated by the same
+/// worker, in `ORDER BY` order. The rule therefore inserts a 
[PinotLogicalSortExchange] hash-distributed on the
+/// `PARTITION BY` keys, sorted on the receiver side.
+///
+/// **The receiver-side collation is `(partitionKeys..., orderByKeys...)`, not 
just the `ORDER BY` keys.** Prepending
+/// the partition keys makes rows arrive clustered by partition, so the 
runtime operator can match and flush one
+/// partition at a time and release its buffer at each partition boundary. 
Sorting on the `ORDER BY` keys alone would
+/// interleave partitions and force the operator to buffer a 
`Map<partitionKey, List<row>>` for the whole input until
+/// end-of-stream. An `ORDER BY` key that is also a partition key is dropped 
from the tail: it is constant inside a
+/// partition, so re-sorting on it there is a no-op.
+///
+/// Sorting happens on the receiver only. Sender-side sorting is not 
implemented for exchanges yet (see the same
+/// note in [PinotWindowExchangeNodeInsertRule]).
+///
+/// A query with no `PARTITION BY` is rejected, because hashing on zero keys 
sends the entire table to one worker.
+/// Set the [QueryOptionKey#ALLOW_MATCH_RECOGNIZE_WITHOUT_PARTITION_BY] query 
option to opt into that plan.
+///
+/// TODO(#19395): Support sender-side sorting so the receiver can k-way merge 
already sorted streams instead of
+/// re-sorting.
[email protected]
+public class PinotMatchExchangeNodeInsertRule extends 
RelRule<PinotMatchExchangeNodeInsertRule.Config> {
+  public static final PinotMatchExchangeNodeInsertRule INSTANCE =
+      new PinotMatchExchangeNodeInsertRule(PinotRuleUtils.PINOT_REL_FACTORY);
+
+  public PinotMatchExchangeNodeInsertRule(RelBuilderFactory factory) {
+    this(ImmutablePinotMatchExchangeNodeInsertRule.Config.builder()
+        .relBuilderFactory(factory)
+        .operandSupplier(b0 -> b0.operand(Match.class).anyInputs())
+        .build());
+  }
+
+  private PinotMatchExchangeNodeInsertRule(Config config) {
+    super(config);
+  }
+
+  @Override
+  public boolean matches(RelOptRuleCall call) {
+    Match match = call.rel(0);
+    return !PinotRuleUtils.isExchange(match.getInput());
+  }
+
+  @Override
+  public void onMatch(RelOptRuleCall call) {
+    Match match = call.rel(0);
+    List<Integer> partitionKeys = match.getPartitionKeys().toList();
+    if (partitionKeys.isEmpty() && !isMatchWithoutPartitionByAllowed(call)) {
+      throw new QueryException(QueryErrorCode.QUERY_PLANNING,
+          "MATCH_RECOGNIZE without a PARTITION BY clause is not allowed 
because the whole table has to be routed to "
+              + "a single worker, which does not scale and can exhaust that 
worker's memory. Add a PARTITION BY "
+              + "clause on a column with enough distinct values, or set the 
query option '"
+              + QueryOptionKey.ALLOW_MATCH_RECOGNIZE_WITHOUT_PARTITION_BY + 
"=true' to accept single-worker "
+              + "execution.");
+    }
+
+    RelNode input = match.getInput();
+    RelCollation collation = clusterByPartitionKeys(partitionKeys, 
match.getOrderKeys());
+    Exchange exchange;
+    if (collation.getFieldCollations().isEmpty()) {
+      // Neither PARTITION BY nor ORDER BY: nothing to sort on, so a plain 
exchange onto a single worker is enough.
+      exchange = PinotLogicalExchange.create(input, 
RelDistributions.hash(partitionKeys));
+    } else {
+      exchange = PinotLogicalSortExchange.create(input, 
RelDistributions.hash(partitionKeys), collation, false, true);

Review Comment:
   Done: the rule now puts an explicit full `LogicalSort` over a hash exchange 
that does not sort on receive, the same shape as the window rule, and 
`matches()` recognizes its own output so it does not re-fire. MATCH now reads 
from a FullSortOperator, which samples resource usage per input block and hands 
rows over in bounded blocks; MatchRecognizeSortInputTest checks that operator 
chain and that partitions split across sort blocks match the same. The rule 
Javadoc, MatchOperator's class doc and the `maxRowsInMatchPartition` doc now 
say the sort buffers the worker's whole MATCH input and that the partition 
limit does not bound it.
   
   _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
   



##########
pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/match/MatchTape.java:
##########
@@ -0,0 +1,187 @@
+/**
+ * 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.query.runtime.operator.match;
+
+import it.unimi.dsi.fastutil.ints.IntArrayList;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.pinot.query.planner.logical.RexExpression;
+import org.apache.pinot.query.planner.plannode.PatternSymbol;
+
+
+/// The classifier tape of the match currently being explored: which pattern 
variable each row of the candidate match
+/// is mapped to, plus everything the MEASURES and DEFINE expressions need in 
order to navigate it.
+///
+/// ## Append only, O(1) backtrack
+///
+/// The tape grows by one entry every time the matcher consumes a row 
([#push]) and shrinks by one every time it
+/// backtracks over that row ([#pop]). Both are O(1): [#pop] only decrements 
lengths, it never rebuilds an
+/// index. The per symbol position lists are maintained the same way, which 
makes [#lastRow] and
+/// [#firstRow] O(1) as well, so logical navigation does not degrade into a 
scan of the match.
+///
+/// ## Rows are addressed by partition index
+///
+/// All row indexes handed out by this class are indexes into the partition, 
not into the match, because physical
+/// navigation (`PREV` / `NEXT`) may leave the match while staying inside the 
partition. The invariant
+/// `getEndPos() == getStartPos() + getLength()` always holds: the rows of a 
match are contiguous.
+///
+/// ## Running semantics
+///
+/// While the matcher is exploring, the tape holds only the rows matched so 
far, so a DEFINE predicate evaluated
+/// through it automatically sees SQL:2016 *running* semantics. The candidate 
row is pushed *before* its own
+/// predicate is evaluated, which is what makes `PREV(A.col, 0)` inside 
`DEFINE A` resolve to the current
+/// row. Once the match is complete the tape holds all of its rows, so the 
same lookups give *final* semantics for
+/// MEASURES.
+///
+/// Not thread safe: one instance belongs to one [PartitionMatcher].
+public class MatchTape {
+  /// Returned by [#lastRow] and [#firstRow] when no such row exists.
+  public static final int NO_ROW = -1;
+
+  private final List<PatternSymbol> _patternSymbols;
+  /// Partition row indexes mapped to each symbol, in ascending order; index 
is the symbol ordinal.
+  private final IntArrayList[] _rowsBySymbol;
+
+  private List<Object[]> _partitionRows = List.of();
+  private int _startPos;
+  private int[] _labels = new int[16];
+  private int _length;
+  private long _matchNumber;
+
+  public MatchTape(List<PatternSymbol> patternSymbols) {
+    _patternSymbols = patternSymbols;
+    _rowsBySymbol = new IntArrayList[patternSymbols.size()];
+    for (int i = 0; i < _rowsBySymbol.length; i++) {
+      _rowsBySymbol[i] = new IntArrayList();
+    }
+  }
+
+  /// Starts a new match attempt at `startPos` of `partitionRows`. 
`matchNumber` is the value
+  /// `MATCH_NUMBER()` reports, which SQL:2016 assigns before the match is 
known to succeed.
+  public void reset(List<Object[]> partitionRows, int startPos, long 
matchNumber) {
+    _partitionRows = partitionRows;
+    _startPos = startPos;
+    _length = 0;
+    _matchNumber = matchNumber;
+    for (IntArrayList rows : _rowsBySymbol) {
+      rows.clear();
+    }

Review Comment:
   Fixed: the tape replaces its per-variable lists once their combined capacity 
exceeds twice the label array (at least 64K positions), so retention follows 
the longest match attempt instead of variables times partition size, and 
teardown releases all of it. MatchTapeTest runs 16 large partitions, each 
mapped to a different variable, and checks the retained capacity stays bounded.
   
   _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
   



##########
pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RelToPlanNodeConverter.java:
##########
@@ -681,6 +703,342 @@ private WindowNode convertLogicalWindow(LogicalWindow 
node) {
         aggCalls, windowFrameType, lowerBound, upperBound, 
fromRexWindowExclusion(windowGroup.exclude), constants);
   }
 
+  /// Converts Calcite's [Match] (SQL:2016 MATCH_RECOGNIZE) into a [MatchNode].
+  ///
+  /// The main job is lowering Calcite's encoding of the `PATTERN` clause - a 
`RexCall` tree of
+  /// PATTERN_CONCAT / PATTERN_ALTER / PATTERN_QUANTIFIER over string literals 
- into the explicit
+  /// [RowPattern] tree, and binding every pattern variable reference to an 
ordinal in the symbol table so that
+  /// the operator never string-matches variable names.
+  ///
+  /// [org.apache.pinot.query.validate.MatchRecognizeValidator] runs on the 
`SqlNode` tree before
+  /// conversion: it rewrites an omitted `AFTER MATCH` to `SKIP PAST LAST ROW` 
(the SQL:2016 default, not
+  /// Calcite's `SKIP TO NEXT ROW`) and rejects the constructs that are not 
supported yet. No default is
+  /// re-applied here. The rejections repeated below are a safety net: 
[MatchNode] cannot represent those
+  /// constructs, so dropping one silently would return wrong rows.
+  private MatchNode convertLogicalMatch(Match node) {
+    if (node.isAllRows()) {
+      throw new QueryException(QueryErrorCode.QUERY_PLANNING,
+          "ALL ROWS PER MATCH is not supported yet in MATCH_RECOGNIZE. Use ONE 
ROW PER MATCH (the default) and "
+              + "expose the per-match values you need through the MEASURES 
clause.");
+    }
+    if (!node.getSubsets().isEmpty()) {
+      throw new QueryException(QueryErrorCode.QUERY_PLANNING,
+          "SUBSET is not supported yet in MATCH_RECOGNIZE. Remove the SUBSET 
clause and reference the individual "
+              + "pattern variables directly.");
+    }
+    if (node.getInterval() != null) {
+      throw new QueryException(QueryErrorCode.QUERY_PLANNING,
+          "WITHIN is not supported yet in MATCH_RECOGNIZE. Remove the WITHIN 
clause and bound the match with a "
+              + "predicate in the DEFINE clause instead.");
+    }
+    if (node.getMeasures().isEmpty()) {
+      // Calcite's SqlValidatorImpl.validateMatchRecognize sets the row type 
of a measures-less MATCH_RECOGNIZE to the
+      // whole input row type, which is not the ONE ROW PER MATCH shape at 
all. MatchRecognizeValidator already
+      // rejects this on the SqlNode tree; this is the safety net, because the 
output schema below would otherwise be
+      // silently wrong rather than merely unsupported.
+      throw new QueryException(QueryErrorCode.QUERY_PLANNING,
+          "MATCH_RECOGNIZE requires a MEASURES clause. ONE ROW PER MATCH emits 
the PARTITION BY columns plus the "
+              + "MEASURES, so add at least one measure, e.g. MEASURES 
MATCH_NUMBER() AS mno.");
+    }
+
+    validateMatchPartitionKeyTypes(node);
+    for (Map.Entry<String, RexNode> measure : node.getMeasures().entrySet()) {
+      validateMatchAggregateOperandTypes(measure.getValue(), measure.getKey());
+    }
+
+    // Order the symbol table by first appearance in PATTERN so the ordinals 
are deterministic and independent of the
+    // iteration order of Calcite's pattern definition map.
+    Map<String, Integer> symbolOrdinals = new LinkedHashMap<>();
+    collectPatternSymbols(node.getPattern(), symbolOrdinals);
+    Map<String, RexNode> patternDefinitions = node.getPatternDefinitions();
+    // A DEFINE for a variable that never appears in PATTERN is dead code, but 
keep it in the table rather than
+    // dropping it silently.
+    for (String name : patternDefinitions.keySet()) {
+      symbolOrdinals.putIfAbsent(name, symbolOrdinals.size());
+    }
+
+    List<PatternSymbol> patternSymbols = new 
ArrayList<>(symbolOrdinals.size());
+    for (String name : symbolOrdinals.keySet()) {
+      RexNode definition = patternDefinitions.get(name);
+      // Per SQL:2016 a pattern variable with no DEFINE entry matches every 
row; that is modeled as a null definition.
+      patternSymbols.add(new PatternSymbol(name,
+          definition != null ? toBoundRexExpression(definition, 
symbolOrdinals) : null));
+    }
+
+    List<MatchNode.Measure> measures = new 
ArrayList<>(node.getMeasures().size());
+    for (Map.Entry<String, RexNode> measure : node.getMeasures().entrySet()) {
+      measures.add(
+          new MatchNode.Measure(measure.getKey(), 
toBoundRexExpression(measure.getValue(), symbolOrdinals)));
+    }
+
+    RowPattern pattern =
+        withAnchors(toRowPattern(node.getPattern(), symbolOrdinals), 
node.isStrictStart(), node.isStrictEnd());
+
+    RexNode after = node.getAfter();
+    MatchNode.AfterMatchSkipMode afterMatchSkipMode = 
toAfterMatchSkipMode(after);
+    // Only SKIP TO FIRST / SKIP TO LAST have a target variable; the other 
modes keep NO_SKIP_TO_SYMBOL, which must
+    // stay distinguishable from the legitimate ordinal 0.
+    boolean hasSkipTarget = afterMatchSkipMode == 
MatchNode.AfterMatchSkipMode.TO_FIRST
+        || afterMatchSkipMode == MatchNode.AfterMatchSkipMode.TO_LAST;
+    int afterMatchSkipToSymbolOrdinal =
+        hasSkipTarget ? toAfterMatchSkipToSymbolOrdinal((RexCall) after, 
symbolOrdinals)
+            : MatchNode.NO_SKIP_TO_SYMBOL;
+
+    return new MatchNode(DEFAULT_STAGE_ID, toDataSchema(node.getRowType()), 
NodeHint.EMPTY,
+        convertInputs(node.getInputs()), patternSymbols, pattern, measures, 
partitionKeysInSourceOrder(node),
+        node.getOrderKeys().getFieldCollations(), afterMatchSkipMode, 
afterMatchSkipToSymbolOrdinal,
+        MatchNode.RowsPerMatchMode.ONE_ROW_PER_MATCH);
+  }
+
+  private static void validateMatchPartitionKeyTypes(Match node) {
+    List<RelDataTypeField> inputFields = 
node.getInput().getRowType().getFieldList();
+    for (int partitionKey : node.getPartitionKeys()) {
+      RelDataTypeField field = inputFields.get(partitionKey);
+      if (field.getType().getSqlTypeName() == SqlTypeName.ARRAY) {
+        throw new QueryException(QueryErrorCode.QUERY_PLANNING,
+            "MATCH_RECOGNIZE does not support multi-value or ARRAY PARTITION 
BY columns: '" + field.getName()
+                + "'. Partition by a single-value column instead.");
+      }
+    }
+  }
+
+  private static void validateMatchAggregateOperandTypes(RexNode expression, 
String measureName) {
+    if (!(expression instanceof RexCall)) {
+      return;
+    }
+    RexCall call = (RexCall) expression;
+    if (call.getOperator() instanceof SqlAggFunction) {
+      for (RexNode operand : call.getOperands()) {
+        if (operand.getType().getSqlTypeName() == SqlTypeName.ARRAY) {
+          throw new QueryException(QueryErrorCode.QUERY_PLANNING,
+              "MATCH_RECOGNIZE measure '" + measureName + "' applies aggregate 
'" + call.getOperator().getName()
+                  + "' to a multi-value or ARRAY operand, which is not 
supported. Aggregate a single-value column "
+                  + "instead.");
+        }
+      }
+    }
+    for (RexNode operand : call.getOperands()) {
+      validateMatchAggregateOperandTypes(operand, measureName);
+    }
+  }
+
+  /// The input column indexes of the `PARTITION BY` keys, in `PARTITION BY` 
**source** order.
+  ///
+  /// `MatchOperator` emits the partition columns positionally in this order, 
so it has to agree with the output row
+  /// type. [Match#getPartitionKeys()] is an `ImmutableBitSet` and has lost 
the order; [PinotLogicalMatch] recovered it
+  /// right after `SqlToRelConverter`, while the input field names still 
matched the `PARTITION BY` names.
+  private static List<Integer> partitionKeysInSourceOrder(Match node) {
+    if (node instanceof PinotLogicalMatch) {
+      return ((PinotLogicalMatch) node).getPartitionKeysInSourceOrder();
+    }
+    // Any other Match only knows its keys as a set, which is the source order 
only for a single partition column.
+    int numPartitionFields = node.getRowType().getFieldCount() - 
node.getMeasures().size();
+    Preconditions.checkState(numPartitionFields <= 1,
+        "MATCH_RECOGNIZE with %s PARTITION BY columns must be planned as a 
PinotLogicalMatch to keep their order, "
+            + "got: %s", numPartitionFields, node);
+    return node.getPartitionKeys().asList();
+  }
+
+  /// Registers every pattern variable of `pattern` in `symbolOrdinals`, in 
order of first appearance.
+  private static void collectPatternSymbols(RexNode pattern, Map<String, 
Integer> symbolOrdinals) {
+    if (pattern instanceof RexLiteral) {
+      symbolOrdinals.putIfAbsent(RexLiteral.stringValue(pattern), 
symbolOrdinals.size());
+      return;
+    }
+    if (pattern instanceof RexCall) {
+      for (RexNode operand : patternChildren((RexCall) pattern)) {
+        collectPatternSymbols(operand, symbolOrdinals);
+      }
+    }
+  }
+
+  /// The sub-pattern operands of a pattern call. All operands are 
sub-patterns except for a quantifier, whose last
+  /// three operands are the bounds and the reluctant flag rather than pattern 
variables.
+  private static List<RexNode> patternChildren(RexCall call) {
+    return call.getKind() == SqlKind.PATTERN_QUANTIFIER ? 
call.getOperands().subList(0, 1) : call.getOperands();
+  }
+
+  private RowPattern toRowPattern(RexNode pattern, Map<String, Integer> 
symbolOrdinals) {
+    if (pattern instanceof RexLiteral) {
+      String name = RexLiteral.stringValue(pattern);
+      Integer ordinal = symbolOrdinals.get(name);
+      Preconditions.checkState(ordinal != null, "Unknown MATCH_RECOGNIZE 
pattern variable: %s", name);
+      return new RowPattern.Symbol(ordinal);
+    }
+    Preconditions.checkState(pattern instanceof RexCall, "Unsupported 
MATCH_RECOGNIZE PATTERN node: %s", pattern);
+    RexCall call = (RexCall) pattern;
+    switch (call.getKind()) {
+      case PATTERN_CONCAT:
+        return new RowPattern.Concat(flattenRowPatterns(call, symbolOrdinals));
+      case PATTERN_ALTER:
+        return new RowPattern.Alternate(flattenRowPatterns(call, 
symbolOrdinals));
+      case PATTERN_QUANTIFIER:
+        return toQuantifier(call, symbolOrdinals);
+      case PATTERN_PERMUTE:
+        throw new QueryException(QueryErrorCode.QUERY_PLANNING,
+            "PERMUTE is not supported yet in the MATCH_RECOGNIZE PATTERN 
clause. Expand the permutation into an "
+                + "explicit alternation, e.g. PATTERN ((A B) | (B A)).");
+      case PATTERN_EXCLUDED:
+        throw new QueryException(QueryErrorCode.QUERY_PLANNING,
+            "Pattern exclusions '{- -}' are not supported yet in the 
MATCH_RECOGNIZE PATTERN clause. Remove the "
+                + "exclusion; note that exclusions only affect ALL ROWS PER 
MATCH output, which is also not "
+                + "supported yet.");
+      default:
+        throw new QueryException(QueryErrorCode.QUERY_PLANNING,
+            "Unsupported MATCH_RECOGNIZE PATTERN construct: '" + call + "'.");
+    }
+  }
+
+  /// Flattens the left-deep binary tree Calcite builds for `A B C` and `A | B 
| C` into the operands of a
+  /// single n-ary node. Both operators are associative and the operand order 
is preserved, so the SQL:2016
+  /// leftmost-alternative-wins rule is unaffected.
+  private List<RowPattern> flattenRowPatterns(RexCall call, Map<String, 
Integer> symbolOrdinals) {
+    List<RowPattern> children = new ArrayList<>(call.getOperands().size());
+    flattenRowPatterns(call, call.getKind(), symbolOrdinals, children);
+    return children;
+  }
+
+  private void flattenRowPatterns(RexNode pattern, SqlKind kind, Map<String, 
Integer> symbolOrdinals,
+      List<RowPattern> children) {
+    if (pattern instanceof RexCall && pattern.getKind() == kind) {
+      for (RexNode operand : ((RexCall) pattern).getOperands()) {
+        flattenRowPatterns(operand, kind, symbolOrdinals, children);
+      }
+    } else {
+      children.add(toRowPattern(pattern, symbolOrdinals));
+    }
+  }
+
+  private RowPattern toQuantifier(RexCall call, Map<String, Integer> 
symbolOrdinals) {
+    List<RexNode> operands = call.getOperands();
+    Preconditions.checkState(operands.size() == 4,
+        "Expecting 4 operands in MATCH_RECOGNIZE pattern quantifier, got: %s", 
operands.size());
+    RowPattern child = toRowPattern(operands.get(0), symbolOrdinals);
+    // Calcite writes -1 for an unspecified bound: `*`, `+` and `{n,}` leave 
the maximum open, `{,m}` leaves the
+    // minimum open. SQL:2016 reads an omitted minimum as 0, while an omitted 
maximum stays unbounded.
+    int minRepeat = toQuantifierBound(operands.get(1), call);
+    int maxRepeat = toQuantifierBound(operands.get(2), call);
+    if (minRepeat == UNSPECIFIED_QUANTIFIER_BOUND) {
+      minRepeat = 0;
+    }
+    if (maxRepeat != RowPattern.Quantifier.UNBOUNDED && maxRepeat < minRepeat) 
{
+      throw new QueryException(QueryErrorCode.QUERY_PLANNING,
+          "MATCH_RECOGNIZE pattern quantifier '" + call + "' has an upper 
bound (" + maxRepeat
+              + ") smaller than its lower bound (" + minRepeat + "), so it can 
never match.");
+    }
+    // The reluctant forms (`*?`, `+?`, `??`, `{n,m}?`) prefer the shortest 
match.
+    return new RowPattern.Quantifier(child, minRepeat, maxRepeat, 
!RexLiteral.booleanValue(operands.get(3)));
+  }
+
+  private int toQuantifierBound(RexNode bound, RexCall call) {
+    int value = RexExpressionUtils.getValueAsInt(bound);
+    if (value < UNSPECIFIED_QUANTIFIER_BOUND) {
+      throw new QueryException(QueryErrorCode.QUERY_PLANNING,
+          "MATCH_RECOGNIZE pattern quantifier '" + call + "' has a negative 
repetition bound: " + value + ".");
+    }
+    if (value > MAX_PATTERN_QUANTIFIER_BOUND) {
+      throw new QueryException(QueryErrorCode.QUERY_PLANNING,
+          "MATCH_RECOGNIZE pattern quantifier bound " + value + " exceeds the 
maximum supported bound of "
+              + MAX_PATTERN_QUANTIFIER_BOUND + ": '" + call + "'. Every 
repetition expands into pattern matcher "
+              + "state, so rewrite the pattern with a smaller repetition 
count, or use an unbounded quantifier "
+              + "such as '*' or '+' together with a DEFINE predicate that 
bounds the match.");

Review Comment:
   Removed the cap instead. The counter loop does not grow with the bound, the 
counter cannot pass partition rows + 1, and the step, row and backtracking 
limits already bound the work, so 10,000 only rejected valid patterns. Tests 
cover bounds up to `Integer.MAX_VALUE`, including `DOWN{1,100000} 
UP{1,2147483647}` end to end.
   
   _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
   



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