Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Support Top/Rare Command In PPL #720

Merged
merged 18 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -31,6 +31,7 @@
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Eval;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Filter;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Project;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.RareTopN;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Relation;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Rename;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Sort;
Expand All @@ -51,6 +52,7 @@
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalFilter;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalPlan;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalProject;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalRareTopN;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalRelation;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalRemove;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalRename;
Expand Down Expand Up @@ -173,6 +175,39 @@ public LogicalPlan visitAggregation(Aggregation node, AnalysisContext context) {
return new LogicalAggregation(child, aggregators, groupBys);
}

/**
* Build {@link LogicalRareTopN}.
*/
@Override
public LogicalPlan visitRareTopN(RareTopN node, AnalysisContext context) {
final LogicalPlan child = node.getChild().get(0).accept(this, context);

ImmutableList.Builder<Expression> groupbyBuilder = new ImmutableList.Builder<>();
for (UnresolvedExpression expr : node.getGroupExprList()) {
groupbyBuilder.add(expressionAnalyzer.analyze(expr, context));
}
ImmutableList<Expression> groupBys = groupbyBuilder.build();

ImmutableList.Builder<Expression> fieldsBuilder = new ImmutableList.Builder<>();
for (Field f : node.getFields()) {
fieldsBuilder.add(expressionAnalyzer.analyze(f, context));
}
ImmutableList<Expression> fields = fieldsBuilder.build();

// new context
context.push();
TypeEnvironment newEnv = context.peek();
groupBys.forEach(group -> newEnv.define(new Symbol(Namespace.FIELD_NAME,
group.toString()), group.type()));
fields.forEach(field -> newEnv.define(new Symbol(Namespace.FIELD_NAME,
field.toString()), field.type()));

List<Argument> options = node.getNoOfResults();
Integer noOfResults = (Integer) options.get(0).getValue().getValue();

return new LogicalRareTopN(child, node.getCommandType(), noOfResults, fields, groupBys);
}

/**
* Build {@link LogicalProject} or {@link LogicalRemove} from {@link Field}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Eval;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Filter;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Project;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.RareTopN;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Relation;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Rename;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Sort;
Expand Down Expand Up @@ -178,6 +179,10 @@ public T visitDedupe(Dedupe node, C context) {
return visitChildren(node, context);
}

public T visitRareTopN(RareTopN node, C context) {
return visitChildren(node, context);
}

public T visitValues(Values node, C context) {
return visitChildren(node, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Eval;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Filter;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Project;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.RareTopN;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.RareTopN.CommandType;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Relation;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Rename;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Sort;
Expand Down Expand Up @@ -296,4 +298,14 @@ public static Sort sort(UnresolvedPlan input, List<Argument> options, Field... s
public static Dedupe dedupe(UnresolvedPlan input, List<Argument> options, Field... fields) {
return new Dedupe(input, options, Arrays.asList(fields));
}

public static List<Argument> defaultTopArgs() {
return exprList(argument("noOfResults", intLiteral(10)));
}

public static RareTopN rareTopN(UnresolvedPlan input, CommandType commandType,
List<Argument> noOfResults, List<UnresolvedExpression> groupList, Field... fields) {
return new RareTopN(input, commandType, noOfResults, Arrays.asList(fields), groupList)
.attach(input);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amazon.opendistroforelasticsearch.sql.ast.tree;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing the license header.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added


import com.amazon.opendistroforelasticsearch.sql.ast.AbstractNodeVisitor;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Argument;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Field;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.UnresolvedExpression;
import java.util.Collections;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;

/**
* AST node represent RareTopN operation.
*/
@Getter
@Setter
@ToString
@EqualsAndHashCode(callSuper = false)
@RequiredArgsConstructor
@AllArgsConstructor
public class RareTopN extends UnresolvedPlan {

private UnresolvedPlan child;
private final CommandType commandType;
private final List<Argument> noOfResults;
private final List<Field> fields;
private final List<UnresolvedExpression> groupExprList;

@Override
public RareTopN attach(UnresolvedPlan child) {
this.child = child;
return this;
}

@Override
public List<UnresolvedPlan> getChild() {
return Collections.singletonList(this.child);
}

@Override
public <T, C> T accept(AbstractNodeVisitor<T, C> nodeVisitor, C context) {
return nodeVisitor.visitRareTopN(this, context);
}

public enum CommandType {
TOP,
RARE
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalPlan;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalPlanNodeVisitor;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalProject;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalRareTopN;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalRelation;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalRemove;
import com.amazon.opendistroforelasticsearch.sql.planner.logical.LogicalRename;
Expand All @@ -34,6 +35,7 @@
import com.amazon.opendistroforelasticsearch.sql.planner.physical.FilterOperator;
import com.amazon.opendistroforelasticsearch.sql.planner.physical.PhysicalPlan;
import com.amazon.opendistroforelasticsearch.sql.planner.physical.ProjectOperator;
import com.amazon.opendistroforelasticsearch.sql.planner.physical.RareTopNOperator;
import com.amazon.opendistroforelasticsearch.sql.planner.physical.RemoveOperator;
import com.amazon.opendistroforelasticsearch.sql.planner.physical.RenameOperator;
import com.amazon.opendistroforelasticsearch.sql.planner.physical.SortOperator;
Expand All @@ -51,6 +53,17 @@
*/
public class DefaultImplementor<C> extends LogicalPlanNodeVisitor<PhysicalPlan, C> {

@Override
public PhysicalPlan visitRareTopN(LogicalRareTopN node, C context) {
return new RareTopNOperator(
visitChild(node, context),
node.getCommandType(),
node.getNoOfResults(),
node.getFieldList(),
node.getGroupByList()
);
}

@Override
public PhysicalPlan visitDedupe(LogicalDedupe node, C context) {
return new DedupeOperator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.amazon.opendistroforelasticsearch.sql.planner.logical;

import com.amazon.opendistroforelasticsearch.sql.ast.tree.RareTopN.CommandType;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Sort.SortOption;
import com.amazon.opendistroforelasticsearch.sql.expression.Expression;
import com.amazon.opendistroforelasticsearch.sql.expression.LiteralExpression;
Expand All @@ -33,6 +34,7 @@
*/
@UtilityClass
public class LogicalPlanDSL {

public static LogicalPlan aggregation(
LogicalPlan input, List<Aggregator> aggregatorList, List<Expression> groupByList) {
return new LogicalAggregation(input, aggregatorList, groupByList);
Expand Down Expand Up @@ -83,6 +85,16 @@ public static LogicalPlan dedupe(
input, Arrays.asList(fields), allowedDuplication, keepEmpty, consecutive);
}

public static LogicalPlan rareTopN(LogicalPlan input, CommandType commandType,
List<Expression> groupByList, Expression... fields) {
return rareTopN(input, commandType, 10, groupByList, fields);
}

public static LogicalPlan rareTopN(LogicalPlan input, CommandType commandType, int noOfResults,
List<Expression> groupByList, Expression... fields) {
return new LogicalRareTopN(input, commandType, noOfResults, Arrays.asList(fields), groupByList);
}

@SafeVarargs
public LogicalPlan values(List<LiteralExpression>... values) {
return new LogicalValues(Arrays.asList(values));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @param <C> context type.
*/
public abstract class LogicalPlanNodeVisitor<R, C> {

protected R visitNode(LogicalPlan plan, C context) {
return null;
}
Expand Down Expand Up @@ -65,4 +66,9 @@ public R visitSort(LogicalSort plan, C context) {
public R visitValues(LogicalValues plan, C context) {
return visitNode(plan, context);
}

public R visitRareTopN(LogicalRareTopN plan, C context) {
return visitNode(plan, context);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amazon.opendistroforelasticsearch.sql.planner.logical;
rupal-bq marked this conversation as resolved.
Show resolved Hide resolved

import com.amazon.opendistroforelasticsearch.sql.ast.tree.RareTopN.CommandType;
import com.amazon.opendistroforelasticsearch.sql.expression.Expression;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

/**
* Logical Rare and TopN Plan.
*/
@Getter
@ToString
@EqualsAndHashCode(callSuper = false)
@RequiredArgsConstructor
public class LogicalRareTopN extends LogicalPlan {

private final LogicalPlan child;
private final CommandType commandType;
private final Integer noOfResults;
private final List<Expression> fieldList;
private final List<Expression> groupByList;

@Override
public List<LogicalPlan> getChild() {
return Collections.singletonList(child);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a different method we use ImmutableList.of() here. Let's be consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced ImmutableList.of(child) in RareTopN.java with Collections.singletonList(child)

}

@Override
public <R, C> R accept(LogicalPlanNodeVisitor<R, C> visitor, C context) {
return visitor.visitRareTopN(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.amazon.opendistroforelasticsearch.sql.planner.physical;

import com.amazon.opendistroforelasticsearch.sql.ast.tree.RareTopN.CommandType;
import com.amazon.opendistroforelasticsearch.sql.ast.tree.Sort.SortOption;
import com.amazon.opendistroforelasticsearch.sql.expression.Expression;
import com.amazon.opendistroforelasticsearch.sql.expression.LiteralExpression;
Expand Down Expand Up @@ -80,6 +81,18 @@ public static DedupeOperator dedupe(
input, Arrays.asList(expressions), allowedDuplication, keepEmpty, consecutive);
}

public static RareTopNOperator rareTopN(PhysicalPlan input, CommandType commandType,
List<Expression> groups, Expression... expressions) {
return new RareTopNOperator(input, commandType, Arrays.asList(expressions), groups);
}

public static RareTopNOperator rareTopN(PhysicalPlan input, CommandType commandType,
int noOfResults,
List<Expression> groups, Expression... expressions) {
return new RareTopNOperator(input, commandType, noOfResults, Arrays.asList(expressions),
groups);
}

@SafeVarargs
public ValuesOperator values(List<LiteralExpression>... values) {
return new ValuesOperator(Arrays.asList(values));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@ public R visitValues(ValuesOperator node, C context) {
public R visitSort(SortOperator node, C context) {
return visitNode(node, context);
}

public R visitRareTopN(RareTopNOperator node, C context) {
return visitNode(node, context);
}
}
Loading