Skip to content

Commit

Permalink
ESQL: Remove esql-core's sort (elastic#111467)
Browse files Browse the repository at this point in the history
This moves the parts of `Sort` that we use out of esql-core and removes
the rest. And the subclass.
  • Loading branch information
nik9000 authored Jul 30, 2024
1 parent d8df7e4 commit 9639ee2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 146 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import org.elasticsearch.index.IndexMode;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.xpack.esql.core.expression.Attribute;
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
import org.elasticsearch.xpack.esql.core.expression.Order;
import org.elasticsearch.xpack.esql.core.index.EsIndex;
import org.elasticsearch.xpack.esql.core.querydsl.container.Sort;
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
import org.elasticsearch.xpack.esql.core.tree.NodeUtils;
import org.elasticsearch.xpack.esql.core.tree.Source;
Expand Down Expand Up @@ -46,8 +46,8 @@ public class EsQueryExec extends LeafExec implements EstimatesRowSize {
public record FieldSort(FieldAttribute field, Order.OrderDirection direction, Order.NullsPosition nulls) {
public FieldSortBuilder fieldSortBuilder() {
FieldSortBuilder builder = new FieldSortBuilder(field.name());
builder.order(Sort.Direction.from(direction).asOrder());
builder.missing(Sort.Missing.from(nulls).searchOrder());
builder.order(Direction.from(direction).asOrder());
builder.missing(Missing.from(nulls).searchOrder());
builder.unmappedType(field.dataType().esType());
return builder;
}
Expand Down Expand Up @@ -206,4 +206,47 @@ public String nodeString() {
+ estimatedRowSize
+ "]";
}

public enum Direction {
ASC,
DESC;

public static Direction from(Order.OrderDirection dir) {
return dir == null || dir == Order.OrderDirection.ASC ? ASC : DESC;
}

public SortOrder asOrder() {
return this == Direction.ASC ? SortOrder.ASC : SortOrder.DESC;
}
}

public enum Missing {
FIRST("_first"),
LAST("_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(null);

private final String searchOrder;

Missing(String searchOrder) {
this.searchOrder = searchOrder;
}

public static Missing from(Order.NullsPosition pos) {
return switch (pos) {
case FIRST -> FIRST;
case LAST -> LAST;
default -> ANY;
};
}

public String searchOrder() {
return searchOrder;
}
}
}

0 comments on commit 9639ee2

Please sign in to comment.