Skip to content

Commit

Permalink
ESQL: Port TopN to NamedWriteable (elastic#111466)
Browse files Browse the repository at this point in the history
This ports `TopN` to `NamedWriteable` to line up with the rest of ESQL.
It also moves most references of `esql.core.expression.Order` to
`esql.expression.Order` to make serialization line up a little better.
We will compress those two classes into one soon.
  • Loading branch information
nik9000 authored Jul 31, 2024
1 parent 3b8970c commit b3e8dae
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public static List<PlanNameRegistry.Entry> namedTypeEntries() {
of(LogicalPlan.class, MvExpand.class, PlanNamedTypes::writeMvExpand, PlanNamedTypes::readMvExpand),
of(LogicalPlan.class, OrderBy.class, PlanNamedTypes::writeOrderBy, PlanNamedTypes::readOrderBy),
of(LogicalPlan.class, Project.class, PlanNamedTypes::writeProject, PlanNamedTypes::readProject),
of(LogicalPlan.class, TopN.class, PlanNamedTypes::writeTopN, PlanNamedTypes::readTopN)
of(LogicalPlan.class, TopN.ENTRY)
);
return declared;
}
Expand Down Expand Up @@ -748,22 +748,6 @@ static void writeProject(PlanStreamOutput out, Project project) throws IOExcepti
out.writeNamedWriteableCollection(project.projections());
}

static TopN readTopN(PlanStreamInput in) throws IOException {
return new TopN(
Source.readFrom(in),
in.readLogicalPlanNode(),
in.readCollectionAsList(org.elasticsearch.xpack.esql.expression.Order::new),
in.readNamedWriteable(Expression.class)
);
}

static void writeTopN(PlanStreamOutput out, TopN topN) throws IOException {
Source.EMPTY.writeTo(out);
out.writeLogicalPlanNode(topN.child());
out.writeCollection(topN.order());
out.writeNamedWriteable(topN.limit());
}

// -- ancillary supporting classes of plan nodes, etc

static EsQueryExec.FieldSort readFieldSort(PlanStreamInput in) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.elasticsearch.xpack.esql.core.expression.Literal;
import org.elasticsearch.xpack.esql.core.expression.MetadataAttribute;
import org.elasticsearch.xpack.esql.core.expression.NamedExpression;
import org.elasticsearch.xpack.esql.core.expression.Order;
import org.elasticsearch.xpack.esql.core.expression.TypedAttribute;
import org.elasticsearch.xpack.esql.core.expression.function.scalar.UnaryScalarFunction;
import org.elasticsearch.xpack.esql.core.expression.predicate.Predicates;
Expand All @@ -48,6 +47,7 @@
import org.elasticsearch.xpack.esql.core.util.Queries;
import org.elasticsearch.xpack.esql.core.util.Queries.Clause;
import org.elasticsearch.xpack.esql.core.util.StringUtils;
import org.elasticsearch.xpack.esql.expression.Order;
import org.elasticsearch.xpack.esql.expression.function.aggregate.Count;
import org.elasticsearch.xpack.esql.expression.function.aggregate.SpatialAggregateFunction;
import org.elasticsearch.xpack.esql.expression.function.scalar.ip.CIDRMatch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
import org.elasticsearch.xpack.esql.core.expression.NameId;
import org.elasticsearch.xpack.esql.core.expression.NamedExpression;
import org.elasticsearch.xpack.esql.core.expression.Order;
import org.elasticsearch.xpack.esql.core.expression.ReferenceAttribute;
import org.elasticsearch.xpack.esql.core.rule.ParameterizedRule;
import org.elasticsearch.xpack.esql.core.rule.ParameterizedRuleExecutor;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.expression.Order;
import org.elasticsearch.xpack.esql.expression.function.aggregate.AggregateFunction;
import org.elasticsearch.xpack.esql.optimizer.rules.AddDefaultTopN;
import org.elasticsearch.xpack.esql.optimizer.rules.BooleanFunctionEqualsElimination;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

package org.elasticsearch.xpack.esql.optimizer.rules;

import org.elasticsearch.xpack.esql.core.expression.Order;
import org.elasticsearch.xpack.esql.expression.Order;
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
import org.elasticsearch.xpack.esql.plan.logical.OrderBy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package org.elasticsearch.xpack.esql.optimizer.rules;

import org.elasticsearch.xpack.esql.core.expression.ExpressionSet;
import org.elasticsearch.xpack.esql.core.expression.Order;
import org.elasticsearch.xpack.esql.expression.Order;
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
import org.elasticsearch.xpack.esql.plan.logical.OrderBy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import org.elasticsearch.xpack.esql.core.expression.Alias;
import org.elasticsearch.xpack.esql.core.expression.Attribute;
import org.elasticsearch.xpack.esql.core.expression.Order;
import org.elasticsearch.xpack.esql.expression.Order;
import org.elasticsearch.xpack.esql.plan.logical.Eval;
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
import org.elasticsearch.xpack.esql.plan.logical.OrderBy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
import org.elasticsearch.xpack.esql.core.expression.Literal;
import org.elasticsearch.xpack.esql.core.expression.MetadataAttribute;
import org.elasticsearch.xpack.esql.core.expression.NamedExpression;
import org.elasticsearch.xpack.esql.core.expression.Order;
import org.elasticsearch.xpack.esql.core.expression.UnresolvedAttribute;
import org.elasticsearch.xpack.esql.core.expression.UnresolvedStar;
import org.elasticsearch.xpack.esql.core.parser.ParserUtils;
import org.elasticsearch.xpack.esql.core.plan.TableIdentifier;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.core.util.Holder;
import org.elasticsearch.xpack.esql.expression.Order;
import org.elasticsearch.xpack.esql.expression.UnresolvedNamePattern;
import org.elasticsearch.xpack.esql.expression.function.UnresolvedFunction;
import org.elasticsearch.xpack.esql.parser.EsqlBaseParser.MetadataOptionContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public abstract class LogicalPlan extends QueryPlan<LogicalPlan> implements Resolvable {
public static List<NamedWriteableRegistry.Entry> getNamedWriteables() {
return List.of(LocalRelation.ENTRY, Lookup.ENTRY, Join.ENTRY);
return List.of(LocalRelation.ENTRY, Lookup.ENTRY, Join.ENTRY, TopN.ENTRY);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
package org.elasticsearch.xpack.esql.plan.logical;

import org.elasticsearch.xpack.esql.core.capabilities.Resolvables;
import org.elasticsearch.xpack.esql.core.expression.Order;
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.expression.Order;

import java.util.List;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@

package org.elasticsearch.xpack.esql.plan.logical;

import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.esql.core.capabilities.Resolvables;
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.expression.Order;
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.expression.Order;
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
import org.elasticsearch.xpack.esql.io.stream.PlanStreamOutput;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

public class TopN extends UnaryPlan {
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(LogicalPlan.class, "TopN", TopN::new);

private final List<Order> order;
private final Expression limit;
Expand All @@ -27,6 +34,28 @@ public TopN(Source source, LogicalPlan child, List<Order> order, Expression limi
this.limit = limit;
}

private TopN(StreamInput in) throws IOException {
this(
Source.readFrom((PlanStreamInput) in),
((PlanStreamInput) in).readLogicalPlanNode(),
in.readCollectionAsList(Order::new),
in.readNamedWriteable(Expression.class)
);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
Source.EMPTY.writeTo(out);
((PlanStreamOutput) out).writeLogicalPlanNode(child());
out.writeCollection(order);
out.writeNamedWriteable(limit);
}

@Override
public String getWriteableName() {
return ENTRY.name;
}

@Override
public boolean expressionsResolved() {
return limit.resolved() && Resolvables.resolved(order);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

package org.elasticsearch.xpack.esql.plan.physical;

import org.elasticsearch.xpack.esql.core.expression.Order;
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.expression.Order;

import java.util.List;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
package org.elasticsearch.xpack.esql.plan.physical;

import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.expression.Order;
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.expression.Order;

import java.util.List;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
import java.io.IOException;

public class OrderSerializationTests extends AbstractExpressionSerializationTests<Order> {
public static Order randomOrder() {
return new Order(randomSource(), randomChild(), randomDirection(), randomNulls());
}

@Override
protected Order createTestInstance() {
return new Order(randomSource(), randomChild(), randomDirection(), randomNulls());
return randomOrder();
}

private static org.elasticsearch.xpack.esql.core.expression.Order.OrderDirection randomDirection() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.elasticsearch.xpack.esql.core.expression.Literal;
import org.elasticsearch.xpack.esql.core.expression.MetadataAttribute;
import org.elasticsearch.xpack.esql.core.expression.NamedExpression;
import org.elasticsearch.xpack.esql.core.expression.Order;
import org.elasticsearch.xpack.esql.core.expression.predicate.logical.And;
import org.elasticsearch.xpack.esql.core.expression.predicate.logical.Not;
import org.elasticsearch.xpack.esql.core.expression.predicate.logical.Or;
Expand All @@ -51,6 +50,7 @@
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.core.type.EsField;
import org.elasticsearch.xpack.esql.enrich.ResolvedEnrichPolicy;
import org.elasticsearch.xpack.esql.expression.Order;
import org.elasticsearch.xpack.esql.expression.function.EsqlFunctionRegistry;
import org.elasticsearch.xpack.esql.expression.function.aggregate.AggregateFunction;
import org.elasticsearch.xpack.esql.expression.function.aggregate.Count;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.plan.logical;

import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.expression.AbstractExpressionSerializationTests;
import org.elasticsearch.xpack.esql.expression.Order;
import org.elasticsearch.xpack.esql.expression.OrderSerializationTests;

import java.io.IOException;
import java.util.List;

public class TopNSerializationTests extends AbstractLogicalPlanSerializationTests<TopN> {
public static TopN randomTopN(int depth) {
Source source = randomSource();
LogicalPlan child = randomChild(depth);
List<Order> order = randomOrders();
Expression limit = AbstractExpressionSerializationTests.randomChild();
return new TopN(source, child, order, limit);
}

private static List<Order> randomOrders() {
return randomList(1, 10, OrderSerializationTests::randomOrder);
}

@Override
protected TopN createTestInstance() {
return randomTopN(0);
}

@Override
protected TopN mutateInstance(TopN instance) throws IOException {
Source source = instance.source();
LogicalPlan child = instance.child();
List<Order> order = instance.order();
Expression limit = instance.limit();
switch (between(0, 2)) {
case 0 -> child = randomValueOtherThan(child, () -> randomChild(0));
case 1 -> order = randomValueOtherThan(order, TopNSerializationTests::randomOrders);
case 2 -> limit = randomValueOtherThan(limit, AbstractExpressionSerializationTests::randomChild);
}
return new TopN(source, child, order, limit);
}

@Override
protected boolean alwaysEmptySource() {
return true;
}
}

0 comments on commit b3e8dae

Please sign in to comment.