Skip to content

Commit

Permalink
ESQL: Squash Order
Browse files Browse the repository at this point in the history
This merges the esql-core version of `Order` into the esql-proper
version of `Order`. Now that we've forked from ql we can simplify this
so you only have to read one thing.
  • Loading branch information
nik9000 committed Jul 11, 2024
1 parent 3e7f7f4 commit be80511
Show file tree
Hide file tree
Showing 26 changed files with 130 additions and 232 deletions.

This file was deleted.

This file was deleted.

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

package org.elasticsearch.xpack.esql.analysis;

import org.elasticsearch.xpack.esql.core.analyzer.TableInfo;
import org.elasticsearch.xpack.esql.plan.logical.Enrich;
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
import org.elasticsearch.xpack.esql.plan.logical.UnresolvedRelation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

package org.elasticsearch.xpack.esql.core.analyzer;
package org.elasticsearch.xpack.esql.analysis;

import org.elasticsearch.xpack.esql.core.plan.TableIdentifier;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,127 @@
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.expression.Nullability;
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;

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

public class Order extends org.elasticsearch.xpack.esql.core.expression.Order {
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isExact;

public class Order extends Expression {
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "Order", Order::new);

public enum OrderDirection {
ASC,
DESC
}

public enum NullsPosition {
FIRST,
LAST,
/**
* Nulls position has not been specified by the user and an appropriate default will be used.
*
* The default values are chosen such that it stays compatible with previous behavior. Unfortunately, this results in
* inconsistencies across different types of queries (see https://github.com/elastic/elasticsearch/issues/77068).
*/
ANY;
}

private final Expression child;
private final OrderDirection direction;
private final NullsPosition nulls;

public Order(Source source, Expression child, OrderDirection direction, NullsPosition nulls) {
super(source, child, direction, nulls);
super(source, List.of(child));
this.child = child;
this.direction = direction;
this.nulls = nulls == null ? NullsPosition.ANY : nulls;
}

public Order(StreamInput in) throws IOException {
this(
Source.readFrom((PlanStreamInput) in),
in.readNamedWriteable(Expression.class),
in.readEnum(org.elasticsearch.xpack.esql.core.expression.Order.OrderDirection.class),
in.readEnum(org.elasticsearch.xpack.esql.core.expression.Order.NullsPosition.class)
in.readEnum(Order.OrderDirection.class),
in.readEnum(Order.NullsPosition.class)
);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
Source.EMPTY.writeTo(out);
out.writeNamedWriteable(child());
out.writeEnum(direction());
out.writeEnum(nullsPosition());
out.writeNamedWriteable(child);
out.writeEnum(direction);
out.writeEnum(nulls);
}

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

public Expression child() {
return child;
}

public OrderDirection direction() {
return direction;
}

public NullsPosition nullsPosition() {
return nulls;
}

@Override
protected TypeResolution resolveType() {
if (DataType.isString(child().dataType())) {
if (DataType.isString(child.dataType())) {
return TypeResolution.TYPE_RESOLVED;
}
return super.resolveType();
return isExact(child, "ORDER BY cannot be applied to field of data type [{}]: {}");
}

@Override
public DataType dataType() {
return child.dataType();
}

@Override
public Order replaceChildren(List<Expression> newChildren) {
return new Order(source(), newChildren.get(0), direction(), nullsPosition());
return new Order(source(), newChildren.get(0), direction, nulls);
}

@Override
protected NodeInfo<Order> info() {
return NodeInfo.create(this, Order::new, child, direction, nulls);
}

@Override
public Nullability nullable() {
return Nullability.FALSE;
}

@Override
protected NodeInfo<org.elasticsearch.xpack.esql.core.expression.Order> info() {
return NodeInfo.create(this, Order::new, child(), direction(), nullsPosition());
public int hashCode() {
return Objects.hash(child, direction, nulls);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}

if (obj == null || getClass() != obj.getClass()) {
return false;
}

Order other = (Order) obj;
return Objects.equals(direction, other.direction) && Objects.equals(nulls, other.nulls) && Objects.equals(child, other.child);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
import org.elasticsearch.xpack.esql.core.expression.NamedExpression;
import org.elasticsearch.xpack.esql.core.expression.Order;
import org.elasticsearch.xpack.esql.core.index.EsIndex;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.EsField;
import org.elasticsearch.xpack.esql.expression.Order;
import org.elasticsearch.xpack.esql.plan.logical.Aggregate;
import org.elasticsearch.xpack.esql.plan.logical.Dissect;
import org.elasticsearch.xpack.esql.plan.logical.Dissect.Parser;
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 @@ -47,6 +46,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 @@ -15,10 +15,10 @@
import org.elasticsearch.xpack.esql.core.expression.AttributeMap;
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.expression.Expressions;
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.expression.Order;
import org.elasticsearch.xpack.esql.optimizer.rules.AddDefaultTopN;
import org.elasticsearch.xpack.esql.optimizer.rules.BooleanFunctionEqualsElimination;
import org.elasticsearch.xpack.esql.optimizer.rules.BooleanSimplification;
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
Loading

0 comments on commit be80511

Please sign in to comment.