|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.analytics.planner; |
| 10 | + |
| 11 | +import org.apache.calcite.rel.RelCollations; |
| 12 | +import org.apache.calcite.rel.RelFieldCollation; |
| 13 | +import org.apache.calcite.rel.RelNode; |
| 14 | +import org.apache.calcite.sql.type.SqlTypeName; |
| 15 | +import org.opensearch.common.settings.Settings; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +/** |
| 21 | + * Plan-shape tests for {@link org.opensearch.analytics.planner.rules.OpenSearchAggregateShardBucketRule}. |
| 22 | + * |
| 23 | + * <p>Verifies the post-CBO insertion of a shard-side Sort+Limit above the PARTIAL |
| 24 | + * aggregate when {@code index.analytics.shard_bucket_oversampling_factor > 0} |
| 25 | + * for all involved indices. {@code shardSize = ceil(max(LIMIT, 10) * factor) + 10}. |
| 26 | + */ |
| 27 | +public class AggregateShardBucketTests extends PlanShapeTestBase { |
| 28 | + |
| 29 | + /** |
| 30 | + * Default factor 1.5 with multi-shard {@code GROUP BY k ORDER BY COUNT(*) LIMIT 100}. |
| 31 | + * Inserts a shard-side Sort with {@code fetch = ceil(100 * 1.5) + 10 = 160}. |
| 32 | + */ |
| 33 | + public void testDefaultFactor_multiShard() { |
| 34 | + RelNode plan = makeSortLimitOverGroupByCount(/* limit */ 100); |
| 35 | + RelNode result = runPlanner(plan, multiShardContext()); |
| 36 | + assertPlanShape( |
| 37 | + """ |
| 38 | + OpenSearchSort(sort0=[$1], dir0=[ASC], fetch=[100], viableBackends=[[mock-parquet]]) |
| 39 | + OpenSearchAggregate(group=[{0}], cnt=[COUNT()], mode=[FINAL], viableBackends=[[mock-parquet]]) |
| 40 | + OpenSearchExchangeReducer(viableBackends=[[mock-parquet]], exchange=[ExchangeInfo[distributionType=SINGLETON, partitionKeyIndices=[]]]) |
| 41 | + OpenSearchSort(sort0=[$1], dir0=[ASC], fetch=[160], viableBackends=[[mock-parquet]]) |
| 42 | + OpenSearchAggregate(group=[{0}], cnt=[COUNT()], mode=[PARTIAL], viableBackends=[[mock-parquet]]) |
| 43 | + OpenSearchTableScan(table=[[test_index]], viableBackends=[[mock-parquet]]) |
| 44 | + """, |
| 45 | + result |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + /** Factor 0 disables the optimization β pre-existing FINAL+ER+PARTIAL shape, no shard Sort. */ |
| 50 | + public void testFactorZero_disabled() { |
| 51 | + RelNode plan = makeSortLimitOverGroupByCount(100); |
| 52 | + RelNode result = runPlanner(plan, multiShardContextWithFactor(0.0)); |
| 53 | + assertPlanShape( |
| 54 | + """ |
| 55 | + OpenSearchSort(sort0=[$1], dir0=[ASC], fetch=[100], viableBackends=[[mock-parquet]]) |
| 56 | + OpenSearchAggregate(group=[{0}], cnt=[COUNT()], mode=[FINAL], viableBackends=[[mock-parquet]]) |
| 57 | + OpenSearchExchangeReducer(viableBackends=[[mock-parquet]], exchange=[ExchangeInfo[distributionType=SINGLETON, partitionKeyIndices=[]]]) |
| 58 | + OpenSearchAggregate(group=[{0}], cnt=[COUNT()], mode=[PARTIAL], viableBackends=[[mock-parquet]]) |
| 59 | + OpenSearchTableScan(table=[[test_index]], viableBackends=[[mock-parquet]]) |
| 60 | + """, |
| 61 | + result |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + /** Factor 1.0 β no oversampling buffer; {@code shardSize = max(LIMIT, 10) * 1.0 + 10 = 110}. */ |
| 66 | + public void testFactorOne_pureTopK() { |
| 67 | + RelNode plan = makeSortLimitOverGroupByCount(100); |
| 68 | + RelNode result = runPlanner(plan, multiShardContextWithFactor(1.0)); |
| 69 | + assertPlanShape( |
| 70 | + """ |
| 71 | + OpenSearchSort(sort0=[$1], dir0=[ASC], fetch=[100], viableBackends=[[mock-parquet]]) |
| 72 | + OpenSearchAggregate(group=[{0}], cnt=[COUNT()], mode=[FINAL], viableBackends=[[mock-parquet]]) |
| 73 | + OpenSearchExchangeReducer(viableBackends=[[mock-parquet]], exchange=[ExchangeInfo[distributionType=SINGLETON, partitionKeyIndices=[]]]) |
| 74 | + OpenSearchSort(sort0=[$1], dir0=[ASC], fetch=[110], viableBackends=[[mock-parquet]]) |
| 75 | + OpenSearchAggregate(group=[{0}], cnt=[COUNT()], mode=[PARTIAL], viableBackends=[[mock-parquet]]) |
| 76 | + OpenSearchTableScan(table=[[test_index]], viableBackends=[[mock-parquet]]) |
| 77 | + """, |
| 78 | + result |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + /** Factor 3.0 β aggressive oversampling; {@code shardSize = max(100, 10) * 3.0 + 10 = 310}. */ |
| 83 | + public void testFactorThree() { |
| 84 | + RelNode plan = makeSortLimitOverGroupByCount(100); |
| 85 | + RelNode result = runPlanner(plan, multiShardContextWithFactor(3.0)); |
| 86 | + assertPlanShape( |
| 87 | + """ |
| 88 | + OpenSearchSort(sort0=[$1], dir0=[ASC], fetch=[100], viableBackends=[[mock-parquet]]) |
| 89 | + OpenSearchAggregate(group=[{0}], cnt=[COUNT()], mode=[FINAL], viableBackends=[[mock-parquet]]) |
| 90 | + OpenSearchExchangeReducer(viableBackends=[[mock-parquet]], exchange=[ExchangeInfo[distributionType=SINGLETON, partitionKeyIndices=[]]]) |
| 91 | + OpenSearchSort(sort0=[$1], dir0=[ASC], fetch=[310], viableBackends=[[mock-parquet]]) |
| 92 | + OpenSearchAggregate(group=[{0}], cnt=[COUNT()], mode=[PARTIAL], viableBackends=[[mock-parquet]]) |
| 93 | + OpenSearchTableScan(table=[[test_index]], viableBackends=[[mock-parquet]]) |
| 94 | + """, |
| 95 | + result |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + /** Single-shard: aggregate is SINGLE (no FINAL/PARTIAL split), rule pattern doesn't match. */ |
| 100 | + public void testSingleShard_noRewrite() { |
| 101 | + RelNode plan = makeSortLimitOverGroupByCount(100); |
| 102 | + RelNode result = runPlanner(plan, singleShardContext()); |
| 103 | + assertPlanShape(""" |
| 104 | + OpenSearchSort(sort0=[$1], dir0=[ASC], fetch=[100], viableBackends=[[mock-parquet]]) |
| 105 | + OpenSearchAggregate(group=[{0}], cnt=[COUNT()], mode=[SINGLE], viableBackends=[[mock-parquet]]) |
| 106 | + OpenSearchTableScan(table=[[test_index]], viableBackends=[[mock-parquet]]) |
| 107 | + """, result); |
| 108 | + } |
| 109 | + |
| 110 | + /** No LIMIT in the query: rule skips (no coord-required size to derive shardSize from). */ |
| 111 | + public void testNoLimit_noRewrite() { |
| 112 | + RelNode agg = makeAggregate(stubScan(mockTable("test_index", "status", "size")), countStarCall()); |
| 113 | + RelNode plan = org.apache.calcite.rel.logical.LogicalSort.create( |
| 114 | + agg, |
| 115 | + RelCollations.of(new RelFieldCollation(1, RelFieldCollation.Direction.ASCENDING)), |
| 116 | + null, |
| 117 | + null |
| 118 | + ); |
| 119 | + RelNode result = runPlanner(plan, multiShardContext()); |
| 120 | + assertPlanShape( |
| 121 | + """ |
| 122 | + OpenSearchSort(sort0=[$1], dir0=[ASC], viableBackends=[[mock-parquet]]) |
| 123 | + OpenSearchAggregate(group=[{0}], cnt=[COUNT()], mode=[FINAL], viableBackends=[[mock-parquet]]) |
| 124 | + OpenSearchExchangeReducer(viableBackends=[[mock-parquet]], exchange=[ExchangeInfo[distributionType=SINGLETON, partitionKeyIndices=[]]]) |
| 125 | + OpenSearchAggregate(group=[{0}], cnt=[COUNT()], mode=[PARTIAL], viableBackends=[[mock-parquet]]) |
| 126 | + OpenSearchTableScan(table=[[test_index]], viableBackends=[[mock-parquet]]) |
| 127 | + """, |
| 128 | + result |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Sort+Limit over GROUP BY shape: ORDER BY agg ASC LIMIT N. Field 0 = group key, |
| 134 | + * field 1 = COUNT(*). |
| 135 | + */ |
| 136 | + private RelNode makeSortLimitOverGroupByCount(int limit) { |
| 137 | + RelNode agg = makeAggregate(stubScan(mockTable("test_index", "status", "size")), countStarCall()); |
| 138 | + return org.apache.calcite.rel.logical.LogicalSort.create( |
| 139 | + agg, |
| 140 | + RelCollations.of(new RelFieldCollation(1, RelFieldCollation.Direction.ASCENDING)), |
| 141 | + null, |
| 142 | + rexBuilder.makeLiteral(limit, typeFactory.createSqlType(SqlTypeName.INTEGER), true) |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + /** Helper: build a multi-shard context with a custom oversampling factor on test_index. */ |
| 147 | + PlannerContext multiShardContextWithFactor(double factor) { |
| 148 | + return buildContextPerIndex( |
| 149 | + "parquet", |
| 150 | + Map.of("test_index", 2), |
| 151 | + Map.of("test_index", Settings.builder().put("index.analytics.shard_bucket_oversampling_factor", factor).build()), |
| 152 | + intFields(), |
| 153 | + List.of(DATAFUSION, LUCENE) |
| 154 | + ); |
| 155 | + } |
| 156 | +} |
0 commit comments