Skip to content

Commit a41b081

Browse files
author
Srikanth Padakanti
committed
Address the PR comments
Signed-off-by: Srikanth Padakanti <[email protected]>
1 parent e584368 commit a41b081

File tree

4 files changed

+39
-147
lines changed

4 files changed

+39
-147
lines changed

core/src/main/java/org/opensearch/sql/calcite/CalciteRelNodeVisitor.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,11 +1556,16 @@ public RelNode visitMvExpand(MvExpand node, CalcitePlanContext context) {
15561556
Field arrayField = node.getField();
15571557
RexInputRef arrayFieldRex = (RexInputRef) rexVisitor.analyze(arrayField, context);
15581558

1559-
buildMvExpandRelNode(arrayFieldRex, arrayField.getField().toString(), null, context);
1559+
// buildMvExpandRelNode(arrayFieldRex, arrayField.getField().toString(), null, context);
15601560

1561-
if (node.getLimit() != null) {
1562-
context.relBuilder.limit(0, node.getLimit());
1563-
}
1561+
// pass the per-document limit into the builder so it can be applied inside the UNNEST inner
1562+
// query
1563+
buildMvExpandRelNode(
1564+
arrayFieldRex, arrayField.getField().toString(), null, node.getLimit(), context);
1565+
1566+
// if (node.getLimit() != null) {
1567+
// context.relBuilder.limit(0, node.getLimit());
1568+
// }
15641569
return context.relBuilder.peek();
15651570
}
15661571

@@ -3083,16 +3088,17 @@ private void buildUnnestForLeft(
30833088
String alias,
30843089
Holder<RexCorrelVariable> correlVariable,
30853090
RexNode correlArrayFieldAccess,
3091+
Integer mvExpandLimit,
30863092
CalcitePlanContext context) {
30873093

3088-
// Build right node: one-row -> project(correlated access) -> uncollect
3089-
RelNode rightNode =
3090-
context
3091-
.relBuilder
3092-
.push(LogicalValues.createOneRow(context.relBuilder.getCluster()))
3093-
.project(List.of(correlArrayFieldAccess), List.of(arrayFieldName))
3094-
.uncollect(List.of(), false)
3095-
.build();
3094+
RelBuilder rb = context.relBuilder;
3095+
rb.push(LogicalValues.createOneRow(rb.getCluster()))
3096+
.project(List.of(correlArrayFieldAccess), List.of(arrayFieldName));
3097+
// apply per-document limit into the inner SELECT if provided
3098+
if (mvExpandLimit != null && mvExpandLimit > 0) {
3099+
rb.limit(0, mvExpandLimit);
3100+
}
3101+
RelNode rightNode = rb.uncollect(List.of(), false).build();
30963102

30973103
// Compute required column ref against leftBuilt's row type (robust)
30983104
RexNode requiredColumnRef =
@@ -3155,11 +3161,16 @@ private void buildExpandRelNode(
31553161
alias,
31563162
correlVariable,
31573163
correlArrayFieldAccess,
3164+
null,
31583165
context);
31593166
}
31603167

31613168
private void buildMvExpandRelNode(
3162-
RexInputRef arrayFieldRex, String arrayFieldName, String alias, CalcitePlanContext context) {
3169+
RexInputRef arrayFieldRex,
3170+
String arrayFieldName,
3171+
String alias,
3172+
Integer mvExpandLimit,
3173+
CalcitePlanContext context) {
31633174

31643175
// Capture left node and its schema BEFORE calling build()
31653176
RelNode leftNode = context.relBuilder.peek();
@@ -3192,6 +3203,7 @@ private void buildMvExpandRelNode(
31923203
alias,
31933204
correlVariable,
31943205
correlArrayFieldAccess,
3206+
mvExpandLimit,
31953207
context);
31963208
}
31973209

docs/user/ppl/cmd/mvexpand.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ Description
2020
- Handles empty, null, and non-array fields gracefully.
2121
- Works as a streaming/distributable command for performance and scalability.
2222

23-
Version
24-
=======
25-
3.3.0
26-
2723
Syntax
2824
======
2925
mvexpand <field> [limit=<int>]
@@ -48,6 +44,10 @@ Limitations
4844
- For empty or null arrays, no rows are returned.
4945
- Large arrays may be subject to resource/memory limits; exceeding them results in an error or warning.
5046

47+
Output ordering and default limit
48+
--------------------------------
49+
If no `limit` is specified, mvexpand expands all elements in the array (there is no implicit per-document cap). Elements are emitted in the same order they appear in the array (array iteration order). If the underlying field does not provide a defined order, the output order is undefined. Use `limit` to bound the number of expanded rows per document and to avoid resource issues on very large arrays.
50+
5151
Examples and Edge Cases
5252
=======================
5353

@@ -156,18 +156,18 @@ Output (example)::
156156

157157
Example 5: Large Arrays and Memory Limits
158158
----------------------------------------
159-
If an array exceeds configured memory/resource limits, mvexpand returns an error.
159+
If an array is very large it can trigger engine/cluster resource limits (memory, circuit-breakers, or query execution limits). Note: this behavior is enforced by the underlying engine and cluster settings, not by a mvexpand-specific configuration.
160160

161-
Input document::
162-
163-
{ "ids": [1, 2, ..., 100000] }
161+
To avoid failures when expanding large arrays:
162+
- Use the `limit` parameter to restrict the number of expanded values per document (for example: `mvexpand field limit=1000`).
163+
- Filter or narrow the input before expanding (use `where` and `fields` to reduce rows and columns).
164+
- Tune cluster and SQL/PPL execution settings (circuit breakers, query size/timeouts, memory limits) appropriate for your deployment.
164165

165166
PPL query::
166167

167168
source=docs | mvexpand ids
168169

169170
Output (example)::
170-
171171
Error: Memory/resource limit exceeded while expanding field 'ids'. Please reduce the array size or specify a limit.
172172

173173
Example 6: Multiple Fields (Limitation)

ppl/src/main/antlr/OpenSearchPPLParser.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ expandCommand
491491
;
492492

493493
mvexpandCommand
494-
: MVEXPAND fieldExpression (LIMIT '=' INTEGER_LITERAL)?
494+
: MVEXPAND fieldExpression (LIMIT EQUAL INTEGER_LITERAL)?
495495
;
496496

497497
flattenCommand

0 commit comments

Comments
 (0)