Skip to content

Commit 4ec1e7a

Browse files
committed
add more comments in code
Signed-off-by: Lantao Jin <ltjin@amazon.com>
1 parent 8bf0431 commit 4ec1e7a

4 files changed

Lines changed: 17 additions & 50 deletions

File tree

core/src/main/java/org/opensearch/sql/calcite/utils/PlanUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ static void addIgnoreNullBucketHintToAggregate(RelBuilder relBuilder) {
616616
.build());
617617
}
618618

619+
/** Extract the RexLiteral from the aggregate call if the aggregate call is a LITERAL_AGG. */
619620
static @Nullable RexLiteral getObjectFromLiteralAgg(AggregateCall aggCall) {
620621
if (aggCall.getAggregation().kind == SqlKind.LITERAL_AGG) {
621622
return (RexLiteral)

opensearch/src/main/java/org/opensearch/sql/opensearch/data/value/OpenSearchExprValueFactory.java

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.util.function.BiFunction;
3939
import lombok.Getter;
4040
import lombok.Setter;
41-
import org.apache.commons.lang3.function.TriFunction;
4241
import org.opensearch.OpenSearchParseException;
4342
import org.opensearch.common.time.DateFormatter;
4443
import org.opensearch.common.time.DateFormatters;
@@ -136,52 +135,6 @@ public void extendTypeMapping(Map<String, OpenSearchDataType> typeMapping) {
136135
(c, dt) -> new OpenSearchExprBinaryValue(c.stringValue()))
137136
.build();
138137

139-
private static final Map<ExprType, TriFunction<Content, ExprType, Boolean, ExprValue>>
140-
typeActionMap2 =
141-
new ImmutableMap.Builder<ExprType, TriFunction<Content, ExprType, Boolean, ExprValue>>()
142-
.put(
143-
OpenSearchDataType.of(OpenSearchDataType.MappingType.Integer),
144-
(c, dt, a) -> new ExprIntegerValue(c.intValue()))
145-
.put(
146-
OpenSearchDataType.of(OpenSearchDataType.MappingType.Long),
147-
(c, dt, a) -> new ExprLongValue(c.longValue()))
148-
.put(
149-
OpenSearchDataType.of(OpenSearchDataType.MappingType.Short),
150-
(c, dt, a) -> new ExprShortValue(c.shortValue()))
151-
.put(
152-
OpenSearchDataType.of(OpenSearchDataType.MappingType.Byte),
153-
(c, dt, a) -> new ExprByteValue(c.byteValue()))
154-
.put(
155-
OpenSearchDataType.of(OpenSearchDataType.MappingType.Float),
156-
(c, dt, a) -> new ExprFloatValue(c.floatValue()))
157-
.put(
158-
OpenSearchDataType.of(OpenSearchDataType.MappingType.Double),
159-
(c, dt, a) -> new ExprDoubleValue(c.doubleValue()))
160-
.put(
161-
OpenSearchTextType.of(),
162-
(c, dt, a) -> new OpenSearchExprTextValue(c.stringValue()))
163-
.put(
164-
OpenSearchDataType.of(OpenSearchDataType.MappingType.Keyword),
165-
(c, dt, a) -> new ExprStringValue(c.stringValue()))
166-
.put(
167-
OpenSearchDataType.of(OpenSearchDataType.MappingType.Boolean),
168-
(c, dt, a) -> ExprBooleanValue.of(c.booleanValue()))
169-
// Handles the creation of DATE, TIME & DATETIME
170-
.put(
171-
OpenSearchDateType.of(TIME), OpenSearchExprValueFactory::createOpenSearchDateType)
172-
.put(
173-
OpenSearchDateType.of(DATE), OpenSearchExprValueFactory::createOpenSearchDateType)
174-
.put(
175-
OpenSearchDateType.of(TIMESTAMP),
176-
OpenSearchExprValueFactory::createOpenSearchDateType)
177-
.put(
178-
OpenSearchDateType.of(OpenSearchDataType.MappingType.Ip),
179-
(c, dt, a) -> new ExprIpValue(c.stringValue()))
180-
.put(
181-
OpenSearchDataType.of(OpenSearchDataType.MappingType.Binary),
182-
(c, dt, a) -> new OpenSearchExprBinaryValue(c.stringValue()))
183-
.build();
184-
185138
/** Constructor of OpenSearchExprValueFactory. */
186139
public OpenSearchExprValueFactory(
187140
Map<String, OpenSearchDataType> typeMapping, boolean fieldTypeTolerance) {

opensearch/src/main/java/org/opensearch/sql/opensearch/planner/rules/DedupPushdownRule.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ && dedupColumnsContainRexCall(rexCallsExceptWindow, dedupColumns)) {
141141
LogicalProject childProject = (LogicalProject) relBuilder.peek();
142142

143143
// 3. Push an Aggregate
144+
// We push down a LITERAL_AGG with dedupNumer for converting the dedup command to aggregate:
145+
// (1) Pass the dedupNumer to AggregateAnalyzer.processAggregateCalls()
146+
// (2) Distinguish it from an optimization operator and user defined aggregator.
147+
// (LITERAL_AGG is used in optimization normally, see {@link SqlKind#LITERAL_AGG})
144148
final List<RexNode> newDedupColumns = RexUtil.apply(mappingForDedupColumns, dedupColumns);
145149
relBuilder.aggregate(relBuilder.groupKey(newDedupColumns), relBuilder.literalAgg(dedupNumer));
146150
PlanUtils.addIgnoreNullBucketHintToAggregate(relBuilder);

opensearch/src/main/java/org/opensearch/sql/opensearch/request/AggregateAnalyzer.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ private static Pair<Builder, List<MetricParser>> processAggregateCalls(
335335

336336
for (int i = 0; i < aggCalls.size(); i++) {
337337
AggregateCall aggCall = aggCalls.get(i);
338-
List<RexNode> args = convertAggArgThroughProject(aggCall, project, helper);
338+
List<RexNode> args = convertAggArgThroughProject(aggCall, project);
339339
String aggFieldName = aggFieldNames.get(i);
340340

341341
Pair<AggregationBuilder, MetricParser> builderAndParser =
@@ -347,8 +347,16 @@ private static Pair<Builder, List<MetricParser>> processAggregateCalls(
347347
return Pair.of(metricBuilder, metricParserList);
348348
}
349349

350-
private static List<RexNode> convertAggArgThroughProject(
351-
AggregateCall aggCall, Project project, AggregateAnalyzer.AggregateBuilderHelper helper) {
350+
/**
351+
* Convert aggregate arguments through child project. Normally, just return the rex nodes of
352+
* Project which are included in aggCall expression. If the aggCall is a LITERAL_AGG, it returns
353+
* all rex nodes of Project except WindowFunction.
354+
*
355+
* @param aggCall the aggregate call
356+
* @param project the project
357+
* @return the converted RexNode list
358+
*/
359+
private static List<RexNode> convertAggArgThroughProject(AggregateCall aggCall, Project project) {
352360
return project == null
353361
? List.of()
354362
: PlanUtils.getObjectFromLiteralAgg(aggCall) != null
@@ -546,6 +554,7 @@ yield switch (functionName) {
546554
String.format("Unsupported push-down aggregator %s", aggCall.getAggregation()));
547555
}
548556
Integer dedupNumber = literal.getValueAs(Integer.class);
557+
// Disable fetchSource since TopHitsParser only parses fetchField currently.
549558
TopHitsAggregationBuilder topHitsAggregationBuilder =
550559
AggregationBuilders.topHits(aggFieldName).from(0).fetchSource(false).size(dedupNumber);
551560
args.stream()

0 commit comments

Comments
 (0)