Skip to content
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import org.opensearch.sql.ast.tree.Lookup;
import org.opensearch.sql.ast.tree.ML;
import org.opensearch.sql.ast.tree.Multisearch;
import org.opensearch.sql.ast.tree.MvCombine;
import org.opensearch.sql.ast.tree.Paginate;
import org.opensearch.sql.ast.tree.Parse;
import org.opensearch.sql.ast.tree.Patterns;
Expand Down Expand Up @@ -533,6 +534,11 @@ public LogicalPlan visitAddColTotals(AddColTotals node, AnalysisContext context)
throw getOnlyForCalciteException("addcoltotals");
}

@Override
public LogicalPlan visitMvCombine(MvCombine node, AnalysisContext context) {
throw getOnlyForCalciteException("mvcombine");
}

/** Build {@link ParseExpression} to context and skip to child nodes. */
@Override
public LogicalPlan visitParse(Parse node, AnalysisContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.opensearch.sql.ast.tree.Lookup;
import org.opensearch.sql.ast.tree.ML;
import org.opensearch.sql.ast.tree.Multisearch;
import org.opensearch.sql.ast.tree.MvCombine;
import org.opensearch.sql.ast.tree.Paginate;
import org.opensearch.sql.ast.tree.Parse;
import org.opensearch.sql.ast.tree.Patterns;
Expand Down Expand Up @@ -461,4 +462,8 @@ public T visitAddTotals(AddTotals node, C context) {
public T visitAddColTotals(AddColTotals node, C context) {
return visitChildren(node, context);
}

public T visitMvCombine(MvCombine node, C context) {
return visitChildren(node, context);
}
}
13 changes: 13 additions & 0 deletions core/src/main/java/org/opensearch/sql/ast/dsl/AstDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.opensearch.sql.ast.tree.Head;
import org.opensearch.sql.ast.tree.Limit;
import org.opensearch.sql.ast.tree.MinSpanBin;
import org.opensearch.sql.ast.tree.MvCombine;
import org.opensearch.sql.ast.tree.Parse;
import org.opensearch.sql.ast.tree.Patterns;
import org.opensearch.sql.ast.tree.Project;
Expand Down Expand Up @@ -468,6 +469,18 @@ public static List<Argument> defaultDedupArgs() {
argument("consecutive", booleanLiteral(false)));
}

public static MvCombine mvcombine(Field field) {
return new MvCombine(field, null, false);
}

public static MvCombine mvcombine(Field field, String delim) {
return new MvCombine(field, delim, false);
}

public static MvCombine mvcombine(Field field, String delim, boolean nomv) {
return new MvCombine(field, delim, nomv);
}

public static List<Argument> sortOptions() {
return exprList(argument("desc", booleanLiteral(false)));
}
Expand Down
47 changes: 47 additions & 0 deletions core/src/main/java/org/opensearch/sql/ast/tree/MvCombine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.sql.ast.tree;

import com.google.common.collect.ImmutableList;
import java.util.List;
import javax.annotation.Nullable;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.opensearch.sql.ast.AbstractNodeVisitor;
import org.opensearch.sql.ast.expression.Field;

@Getter
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class MvCombine extends UnresolvedPlan {

private final Field field;
private final String delim;
@Nullable private UnresolvedPlan child;
private final boolean nomv;

public MvCombine(Field field, @Nullable String delim, boolean nomv) {
this.field = field;
this.delim = (delim == null) ? " " : delim;
this.nomv = nomv;
}

public MvCombine attach(UnresolvedPlan child) {
this.child = child;
return this;
}

@Override
public List<UnresolvedPlan> getChild() {
return child == null ? ImmutableList.of() : ImmutableList.of(child);
}

@Override
public <T, C> T accept(AbstractNodeVisitor<T, C> nodeVisitor, C context) {
return nodeVisitor.visitMvCombine(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
import org.opensearch.sql.ast.tree.Lookup.OutputStrategy;
import org.opensearch.sql.ast.tree.ML;
import org.opensearch.sql.ast.tree.Multisearch;
import org.opensearch.sql.ast.tree.MvCombine;
import org.opensearch.sql.ast.tree.Paginate;
import org.opensearch.sql.ast.tree.Parse;
import org.opensearch.sql.ast.tree.Patterns;
Expand Down Expand Up @@ -3116,6 +3117,109 @@ public RelNode visitExpand(Expand expand, CalcitePlanContext context) {
return context.relBuilder.peek();
}

@Override
public RelNode visitMvCombine(MvCombine node, CalcitePlanContext context) {
// 1) Lower child first
visitChildren(node, context);

final RelBuilder relBuilder = context.relBuilder;
final RexBuilder rexBuilder = context.rexBuilder;

final RelNode input = relBuilder.peek();
final List<String> inputFieldNames = input.getRowType().getFieldNames();

// 2) Accept delim (default is single space).
// NOTE: delim only affects single-value rendering when nomv=true.
final String delim = node.getDelim();

// 3) Resolve target field to an input ref (must be a direct field reference)
final Field targetField = node.getField();
final RexNode targetRex = rexVisitor.analyze(targetField, context);
if (!(targetRex instanceof RexInputRef)) {
throw new SemanticCheckException(
"mvcombine target must be a direct field reference, but got: " + targetField);
}
final int targetIndex = ((RexInputRef) targetRex).getIndex();
final String targetName = inputFieldNames.get(targetIndex);

// 4) Group key = all fields except target
final List<RexNode> groupExprs = new ArrayList<>();
for (int i = 0; i < inputFieldNames.size(); i++) {
if (i == targetIndex) continue;
groupExprs.add(relBuilder.field(i));
}

// 5) Aggregate target values: COLLECT => MULTISET
final RelBuilder.AggCall aggCall =
relBuilder
.aggregateCall(SqlStdOperatorTable.COLLECT, relBuilder.field(targetIndex))
.as(targetName);

relBuilder.aggregate(relBuilder.groupKey(groupExprs), aggCall);

// 6) Restore original output column order AND cast MULTISET -> ARRAY using RexBuilder
// After aggregate => [group fields..., targetAgg(multiset)]
final int aggPos = groupExprs.size();

final RelDataType elemType = input.getRowType().getFieldList().get(targetIndex).getType();
final RelDataType arrayType = relBuilder.getTypeFactory().createArrayType(elemType, -1);

final List<RexNode> projections = new ArrayList<>(inputFieldNames.size());
final List<String> projNames = new ArrayList<>(inputFieldNames.size());

int groupPos = 0;
for (int i = 0; i < inputFieldNames.size(); i++) {
projNames.add(inputFieldNames.get(i));
if (i == targetIndex) {
final RexNode multisetRef = relBuilder.field(aggPos); // MULTISET
projections.add(rexBuilder.makeCast(arrayType, multisetRef)); // ARRAY
} else {
projections.add(relBuilder.field(groupPos));
groupPos++;
}
}
relBuilder.project(projections, projNames, /* force= */ true);

// 7) nomv=true converts multivalue output to a single delimited string.
// arrayToString in this engine supports only String/ByteString, so cast elements to STRING
// first.
if (node.isNomv()) {
final List<RexNode> nomvProjections = new ArrayList<>(inputFieldNames.size());
final List<String> nomvNames = new ArrayList<>(inputFieldNames.size());

// Build ARRAY<VARCHAR> type once
final RelDataType stringType =
relBuilder
.getTypeFactory()
.createSqlType(org.apache.calcite.sql.type.SqlTypeName.VARCHAR);
final RelDataType stringArrayType =
relBuilder.getTypeFactory().createArrayType(stringType, -1);

for (int i = 0; i < inputFieldNames.size(); i++) {
final String name = inputFieldNames.get(i);
nomvNames.add(name);

if (i == targetIndex) {
// At this point relBuilder.field(i) is ARRAY<elemType>. Cast to ARRAY<VARCHAR> so
// ARRAY_TO_STRING works.
final RexNode stringArray = rexBuilder.makeCast(stringArrayType, relBuilder.field(i));

nomvProjections.add(
relBuilder.call(
org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_TO_STRING,
stringArray,
relBuilder.literal(delim)));
} else {
nomvProjections.add(relBuilder.field(i));
}
}

relBuilder.project(nomvProjections, nomvNames, /* force= */ true);
}

return relBuilder.peek();
}

@Override
public RelNode visitValues(Values values, CalcitePlanContext context) {
if (values.getValues() == null || values.getValues().isEmpty()) {
Expand Down
1 change: 1 addition & 0 deletions docs/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"user/ppl/cmd/sort.md",
"user/ppl/cmd/spath.md",
"user/ppl/cmd/stats.md",
"user/ppl/cmd/mvcombine.md",
"user/ppl/cmd/streamstats.md",
"user/ppl/cmd/subquery.md",
"user/ppl/cmd/syntax.md",
Expand Down
3 changes: 2 additions & 1 deletion docs/user/dql/metadata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Example 1: Show All Indices Information
SQL query::

os> SHOW TABLES LIKE '%'
fetched rows / total rows = 23/23
fetched rows / total rows = 24/24
+----------------+-------------+-------------------+------------+---------+----------+------------+-----------+---------------------------+----------------+
| TABLE_CAT | TABLE_SCHEM | TABLE_NAME | TABLE_TYPE | REMARKS | TYPE_CAT | TYPE_SCHEM | TYPE_NAME | SELF_REFERENCING_COL_NAME | REF_GENERATION |
|----------------+-------------+-------------------+------------+---------+----------+------------+-----------+---------------------------+----------------|
Expand All @@ -48,6 +48,7 @@ SQL query::
| docTestCluster | null | events_many_hosts | BASE TABLE | null | null | null | null | null | null |
| docTestCluster | null | events_null | BASE TABLE | null | null | null | null | null | null |
| docTestCluster | null | json_test | BASE TABLE | null | null | null | null | null | null |
| docTestCluster | null | mvcombine_data | BASE TABLE | null | null | null | null | null | null |
| docTestCluster | null | nested | BASE TABLE | null | null | null | null | null | null |
| docTestCluster | null | nyc_taxi | BASE TABLE | null | null | null | null | null | null |
| docTestCluster | null | occupation | BASE TABLE | null | null | null | null | null | null |
Expand Down
Loading