Skip to content

Commit fbfb4ce

Browse files
Fix MvSort checks
1 parent 15439de commit fbfb4ce

File tree

1 file changed

+15
-28
lines changed
  • x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/multivalue

1 file changed

+15
-28
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/multivalue/MvSort.java

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.elasticsearch.xpack.esql.common.Failure;
3535
import org.elasticsearch.xpack.esql.common.Failures;
3636
import org.elasticsearch.xpack.esql.core.expression.Expression;
37-
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
3837
import org.elasticsearch.xpack.esql.core.expression.Literal;
3938
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
4039
import org.elasticsearch.xpack.esql.core.tree.Source;
@@ -48,7 +47,6 @@
4847
import org.elasticsearch.xpack.esql.planner.PlannerUtils;
4948

5049
import java.io.IOException;
51-
import java.util.ArrayList;
5250
import java.util.Arrays;
5351
import java.util.List;
5452

@@ -147,36 +145,25 @@ public boolean foldable() {
147145
return field.foldable() && (order == null || order.foldable());
148146
}
149147

150-
@Override
151-
public Object fold(FoldContext ctx) {
152-
if (order != null && order instanceof Literal == false) {
153-
// if we are here, we know that order is foldable, so we can safely fold it
154-
Literal newOrder = Literal.of(ctx, order);
155-
List<Expression> newChildren = new ArrayList<>();
156-
newChildren.add(field);
157-
newChildren.add(newOrder);
158-
return replaceChildren(newChildren).fold(ctx);
159-
}
160-
return super.fold(ctx);
161-
}
162-
163148
@Override
164149
public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
165150
boolean ordering = true;
166-
if (isValidOrder() == false) {
167-
throw new IllegalArgumentException(
168-
LoggerMessageFormat.format(
169-
null,
170-
INVALID_ORDER_ERROR,
171-
sourceText(),
172-
BytesRefs.toString(ASC.value()),
173-
BytesRefs.toString(DESC.value()),
174-
BytesRefs.toString(order.fold(toEvaluator.foldCtx()))
175-
)
176-
);
177-
}
178151
if (order != null && order.foldable()) {
179-
ordering = BytesRefs.toString(order.fold(toEvaluator.foldCtx())).equalsIgnoreCase(BytesRefs.toString(ASC.value()));
152+
String orderValue = BytesRefs.toString(order.fold(toEvaluator.foldCtx()));
153+
if (orderValue.equalsIgnoreCase(BytesRefs.toString(DESC.value())) == false
154+
&& orderValue.equalsIgnoreCase(BytesRefs.toString(ASC.value())) == false) {
155+
throw new IllegalArgumentException(
156+
LoggerMessageFormat.format(
157+
null,
158+
INVALID_ORDER_ERROR,
159+
sourceText(),
160+
BytesRefs.toString(ASC.value()),
161+
BytesRefs.toString(DESC.value()),
162+
orderValue
163+
)
164+
);
165+
}
166+
ordering = orderValue.equalsIgnoreCase(BytesRefs.toString(ASC.value()));
180167
}
181168

182169
return switch (PlannerUtils.toElementType(field.dataType())) {

0 commit comments

Comments
 (0)