Skip to content

Commit bf018b7

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

File tree

3 files changed

+43
-51
lines changed

3 files changed

+43
-51
lines changed

core/src/main/java/org/opensearch/sql/ast/tree/MvExpand.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ public MvExpand attach(UnresolvedPlan child) {
3434
return this;
3535
}
3636

37-
@Nullable
38-
public Integer getLimit() {
39-
return limit;
40-
}
41-
4237
@Override
4338
public List<UnresolvedPlan> getChild() {
4439
return this.child == null ? ImmutableList.of() : ImmutableList.of(this.child);

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

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,12 @@ public RelNode visitPatterns(Patterns node, CalcitePlanContext context) {
813813
.toList();
814814
context.relBuilder.aggregate(context.relBuilder.groupKey(groupByList), aggCall);
815815
buildExpandRelNode(
816-
context.relBuilder.field(node.getAlias()), node.getAlias(), node.getAlias(), context);
816+
context.relBuilder.field(node.getAlias()),
817+
node.getAlias(),
818+
node.getAlias(),
819+
null,
820+
context);
821+
817822
flattenParsedPattern(
818823
node.getAlias(),
819824
context.relBuilder.field(node.getAlias()),
@@ -1556,16 +1561,11 @@ public RelNode visitMvExpand(MvExpand node, CalcitePlanContext context) {
15561561
Field arrayField = node.getField();
15571562
RexInputRef arrayFieldRex = (RexInputRef) rexVisitor.analyze(arrayField, context);
15581563

1559-
// buildMvExpandRelNode(arrayFieldRex, arrayField.getField().toString(), null, context);
1560-
15611564
// pass the per-document limit into the builder so it can be applied inside the UNNEST inner
15621565
// query
15631566
buildMvExpandRelNode(
15641567
arrayFieldRex, arrayField.getField().toString(), null, node.getLimit(), context);
15651568

1566-
// if (node.getLimit() != null) {
1567-
// context.relBuilder.limit(0, node.getLimit());
1568-
// }
15691569
return context.relBuilder.peek();
15701570
}
15711571

@@ -2830,7 +2830,7 @@ public RelNode visitExpand(Expand expand, CalcitePlanContext context) {
28302830
RexInputRef arrayFieldRex = (RexInputRef) rexVisitor.analyze(arrayField, context);
28312831
String alias = expand.getAlias();
28322832

2833-
buildExpandRelNode(arrayFieldRex, arrayField.getField().toString(), alias, context);
2833+
buildExpandRelNode(arrayFieldRex, arrayField.getField().toString(), alias, null, context);
28342834

28352835
return context.relBuilder.peek();
28362836
}
@@ -3132,46 +3132,27 @@ private void buildUnnestForLeft(
31323132
}
31333133

31343134
private void buildExpandRelNode(
3135-
RexInputRef arrayFieldRex, String arrayFieldName, String alias, CalcitePlanContext context) {
3136-
3137-
// Capture left node and its schema BEFORE calling build()
3138-
RelNode leftNode = context.relBuilder.peek();
3139-
RelDataType leftRowType = leftNode.getRowType();
3140-
3141-
// Create correlation variable while left is still on the builder stack
3142-
Holder<RexCorrelVariable> correlVariable = Holder.empty();
3143-
context.relBuilder.variable(correlVariable::set);
3144-
3145-
// Create correlated field access while left is still on the builder stack
3146-
// (preserve original expand semantics: use the input RexInputRef index)
3147-
RexNode correlArrayFieldAccess =
3148-
context.relBuilder.field(
3149-
context.rexBuilder.makeCorrel(leftRowType, correlVariable.get().id),
3150-
arrayFieldRex.getIndex());
3151-
3152-
// Materialize leftBuilt (this pops the left from the relBuilder stack)
3153-
RelNode leftBuilt = context.relBuilder.build();
3154-
3155-
// Use unified helper to build right/uncollect + correlate + cleanup
3156-
buildUnnestForLeft(
3157-
leftBuilt,
3158-
leftRowType,
3159-
arrayFieldRex.getIndex(),
3160-
arrayFieldName,
3161-
alias,
3162-
correlVariable,
3163-
correlArrayFieldAccess,
3164-
null,
3165-
context);
3166-
}
3167-
3168-
private void buildMvExpandRelNode(
3169-
RexInputRef arrayFieldRex,
3135+
RexNode arrayFieldRexNode,
31703136
String arrayFieldName,
31713137
String alias,
31723138
Integer mvExpandLimit,
31733139
CalcitePlanContext context) {
31743140

3141+
// Convert incoming RexNode to RexInputRef when possible; otherwise resolve by field name.
3142+
RexInputRef arrayFieldRex;
3143+
if (arrayFieldRexNode instanceof RexInputRef) {
3144+
arrayFieldRex = (RexInputRef) arrayFieldRexNode;
3145+
} else {
3146+
RelDataType currentRowType = context.relBuilder.peek().getRowType();
3147+
RelDataTypeField fld = currentRowType.getField(arrayFieldName, false, false);
3148+
if (fld != null) {
3149+
arrayFieldRex = context.rexBuilder.makeInputRef(currentRowType, fld.getIndex());
3150+
} else {
3151+
throw new IllegalArgumentException(
3152+
"buildExpandRelNode: expected RexInputRef or resolvable field name: " + arrayFieldName);
3153+
}
3154+
}
3155+
31753156
// Capture left node and its schema BEFORE calling build()
31763157
RelNode leftNode = context.relBuilder.peek();
31773158
RelDataType leftRowType = leftNode.getRowType();
@@ -3207,6 +3188,17 @@ private void buildMvExpandRelNode(
32073188
context);
32083189
}
32093190

3191+
private void buildMvExpandRelNode(
3192+
RexInputRef arrayFieldRex,
3193+
String arrayFieldName,
3194+
String alias,
3195+
Integer mvExpandLimit,
3196+
CalcitePlanContext context) {
3197+
3198+
// Delegate to the canonical expand implementation (pass the per-document limit through).
3199+
buildExpandRelNode(arrayFieldRex, arrayFieldName, alias, mvExpandLimit, context);
3200+
}
3201+
32103202
/** Creates an optimized sed call using native Calcite functions */
32113203
private RexNode createOptimizedSedCall(
32123204
RexNode fieldRex, String sedExpression, CalcitePlanContext context) {

docs/user/ppl/cmd/mvexpand.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,19 @@ Output (example)::
154154
| error |
155155
+-------+
156156

157-
Example 5: Large Arrays and Memory Limits
158-
----------------------------------------
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.
157+
Example 5: Large Arrays and Memory / resource limits
158+
----------------------------------------------------
159+
If an array is very large it can trigger engine or cluster resource limits and the query can fail with an error. There is no mvexpand-specific configuration flag that controls resource usage; instead, limits are enforced by the engine and the cluster:
160+
161+
- OpenSearch node-level protections (circuit breakers and JVM/heap safeguards) and request-size protections.
162+
- SQL/PPL execution limits (for example, query timeouts, request-size limits, and engine memory budgets) that apply to the query execution layer.
163+
164+
Behavior of circuit breakers and which operators they protect can vary by release and configuration (some breakers primarily protect memory-heavy operations such as fielddata, aggregations, and certain scan implementations). Because of these distinctions, mvexpand should not be relied on to bypass cluster-level protections — use the command-level ``limit`` to bound per-document expansion and avoid hitting cluster limits.
160165

161166
To avoid failures when expanding large arrays:
162167
- Use the `limit` parameter to restrict the number of expanded values per document (for example: `mvexpand field limit=1000`).
163168
- 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.
169+
- Tune cluster and SQL/PPL execution settings (circuit breakers, request/response size, timeouts, memory limits) appropriate for your deployment. If desired, we can add links to the exact OpenSearch circuit-breaker and SQL/PPL configuration docs for the targeted release.
165170

166171
PPL query::
167172

0 commit comments

Comments
 (0)