|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.opensearch.planner.physical; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | +import org.apache.calcite.adapter.enumerable.AggImplementor; |
| 10 | +import org.apache.calcite.adapter.enumerable.EnumerableAggregateBase; |
| 11 | +import org.apache.calcite.adapter.enumerable.EnumerableConvention; |
| 12 | +import org.apache.calcite.adapter.enumerable.EnumerableRel; |
| 13 | +import org.apache.calcite.adapter.enumerable.EnumerableRelImplementor; |
| 14 | +import org.apache.calcite.adapter.enumerable.RexImpTable; |
| 15 | +import org.apache.calcite.plan.RelOptCluster; |
| 16 | +import org.apache.calcite.plan.RelOptCost; |
| 17 | +import org.apache.calcite.plan.RelOptPlanner; |
| 18 | +import org.apache.calcite.plan.RelTraitSet; |
| 19 | +import org.apache.calcite.rel.InvalidRelException; |
| 20 | +import org.apache.calcite.rel.RelNode; |
| 21 | +import org.apache.calcite.rel.core.AggregateCall; |
| 22 | +import org.apache.calcite.rel.hint.RelHint; |
| 23 | +import org.apache.calcite.rel.metadata.RelMetadataQuery; |
| 24 | +import org.apache.calcite.util.ImmutableBitSet; |
| 25 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 26 | + |
| 27 | +/** |
| 28 | + * The enumerable aggregate physical implementation for OpenSearch nested aggregation. |
| 29 | + * https://docs.opensearch.org/latest/aggregations/bucket/nested/ |
| 30 | + */ |
| 31 | +public class EnumerableNestedAggregate extends EnumerableAggregateBase implements EnumerableRel { |
| 32 | + |
| 33 | + public EnumerableNestedAggregate( |
| 34 | + RelOptCluster cluster, |
| 35 | + RelTraitSet traitSet, |
| 36 | + List<RelHint> hints, |
| 37 | + RelNode input, |
| 38 | + ImmutableBitSet groupSet, |
| 39 | + @Nullable List<ImmutableBitSet> groupSets, |
| 40 | + List<AggregateCall> aggCalls) |
| 41 | + throws InvalidRelException { |
| 42 | + super(cluster, traitSet, hints, input, groupSet, groupSets, aggCalls); |
| 43 | + assert getConvention() instanceof EnumerableConvention; |
| 44 | + |
| 45 | + for (AggregateCall aggCall : aggCalls) { |
| 46 | + if (aggCall.isDistinct()) { |
| 47 | + throw new InvalidRelException("distinct aggregation not supported"); |
| 48 | + } |
| 49 | + if (aggCall.distinctKeys != null) { |
| 50 | + throw new InvalidRelException("within-distinct aggregation not supported"); |
| 51 | + } |
| 52 | + AggImplementor implementor2 = RexImpTable.INSTANCE.get(aggCall.getAggregation(), false); |
| 53 | + if (implementor2 == null) { |
| 54 | + throw new InvalidRelException("aggregation " + aggCall.getAggregation() + " not supported"); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public EnumerableNestedAggregate copy( |
| 61 | + RelTraitSet traitSet, |
| 62 | + RelNode input, |
| 63 | + ImmutableBitSet groupSet, |
| 64 | + @Nullable List<ImmutableBitSet> groupSets, |
| 65 | + List<AggregateCall> aggCalls) { |
| 66 | + try { |
| 67 | + return new EnumerableNestedAggregate( |
| 68 | + getCluster(), traitSet, getHints(), input, groupSet, groupSets, aggCalls); |
| 69 | + } catch (InvalidRelException e) { |
| 70 | + // Semantic error not possible. Must be a bug. Convert to |
| 71 | + // internal error. |
| 72 | + throw new AssertionError(e); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Result implement(EnumerableRelImplementor implementor, Prefer pref) { |
| 78 | + throw new RuntimeException(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public @Nullable RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery mq) { |
| 83 | + return super.computeSelfCost(planner, mq).multiplyBy(.9); |
| 84 | + } |
| 85 | +} |
0 commit comments