Skip to content

Commit

Permalink
ESQL: make cidr_match foldable (#105403)
Browse files Browse the repository at this point in the history
Fixes #105376 Creates two
new ESQL specific base classes for functions, eliminating boilerplate
code from many concrete function implementations.
  • Loading branch information
astefan committed Feb 12, 2024
1 parent 4b5f0a0 commit 9151d2d
Show file tree
Hide file tree
Showing 43 changed files with 148 additions and 429 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/105403.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 105403
summary: "ESQL: make `cidr_match` foldable"
area: ES|QL
type: bug
issues:
- 105376
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,10 @@ row true_bool = null is null, false_bool = null is not null, negated_true = not(
true_bool:boolean | false_bool:boolean | negated_true:boolean | negated_false:boolean
true | false | false | true
;

staticCidrMatch
row x = cidr_match(to_ip("127.0.0.1"), "127.0.1.0/16"), y = cidr_match(to_ip("127.0.0.1"), "127.1.0.0/16") | eval ip = to_ip("127.0.0.1") | eval z = cidr_match(ip, "127.0.0.1");

x:boolean | y:boolean | ip:ip | z:boolean
true |false |127.0.0.1 |true
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.expression.function.scalar;

import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.session.Configuration;
import org.elasticsearch.xpack.ql.tree.Source;

import java.util.List;

public abstract class EsqlConfigurationFunction extends EsqlScalarFunction {

private final Configuration configuration;

protected EsqlConfigurationFunction(Source source, List<Expression> fields, Configuration configuration) {
super(source, fields);
this.configuration = configuration;
}

public Configuration configuration() {
return configuration;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.expression.function.scalar;

import org.elasticsearch.xpack.esql.evaluator.mapper.EvaluatorMapper;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.ql.tree.Source;

import java.util.List;

public abstract class EsqlScalarFunction extends ScalarFunction implements EvaluatorMapper {

protected EsqlScalarFunction(Source source) {
super(source);
}

protected EsqlScalarFunction(Source source, List<Expression> fields) {
super(source, fields);
}

@Override
public Object fold() {
return EvaluatorMapper.super.fold();
}

@Override
public final ScriptTemplate asScript() {
throw new UnsupportedOperationException("functions do not support scripting");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.TypeResolutions;
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.ql.type.DataType;

Expand All @@ -19,7 +17,7 @@

import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isNumeric;

public abstract class UnaryScalarFunction extends ScalarFunction {
public abstract class UnaryScalarFunction extends EsqlScalarFunction {
protected final Expression field;

public UnaryScalarFunction(Source source, Expression field) {
Expand Down Expand Up @@ -63,9 +61,4 @@ public final boolean equals(Object obj) {
UnaryScalarFunction other = (UnaryScalarFunction) obj;
return Objects.equals(other.field, field);
}

@Override
public final ScriptTemplate asScript() {
throw new UnsupportedOperationException("functions do not support scripting");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
import org.elasticsearch.core.Releasable;
import org.elasticsearch.core.Releasables;
import org.elasticsearch.xpack.esql.evaluator.mapper.EvaluatorMapper;
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
import org.elasticsearch.xpack.esql.expression.function.Param;
import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlScalarFunction;
import org.elasticsearch.xpack.esql.planner.PlannerUtils;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.Literal;
import org.elasticsearch.xpack.ql.expression.Nullability;
import org.elasticsearch.xpack.ql.expression.TypeResolutions;
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.ql.tree.NodeInfo;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.ql.type.DataType;
Expand All @@ -39,7 +37,7 @@
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
import static org.elasticsearch.xpack.ql.type.DataTypes.NULL;

public final class Case extends ScalarFunction implements EvaluatorMapper {
public final class Case extends EsqlScalarFunction {
record Condition(Expression condition, Expression value) {}

private final List<Condition> conditions;
Expand Down Expand Up @@ -151,11 +149,6 @@ public Nullability nullable() {
return Nullability.UNKNOWN;
}

@Override
public ScriptTemplate asScript() {
throw new UnsupportedOperationException("functions do not support scripting");
}

@Override
public Expression replaceChildren(List<Expression> newChildren) {
return new Case(source(), newChildren.get(0), newChildren.subList(1, newChildren.size()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@
import org.elasticsearch.compute.ann.Evaluator;
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException;
import org.elasticsearch.xpack.esql.evaluator.mapper.EvaluatorMapper;
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
import org.elasticsearch.xpack.esql.expression.function.Param;
import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlScalarFunction;
import org.elasticsearch.xpack.esql.expression.function.scalar.multivalue.MvMax;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.Expressions;
import org.elasticsearch.xpack.ql.expression.TypeResolutions;
import org.elasticsearch.xpack.ql.expression.function.OptionalArgument;
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.ql.tree.NodeInfo;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.ql.type.DataType;
Expand All @@ -35,7 +33,7 @@
/**
* Returns the maximum value of multiple columns.
*/
public class Greatest extends ScalarFunction implements EvaluatorMapper, OptionalArgument {
public class Greatest extends EsqlScalarFunction implements OptionalArgument {
private DataType dataType;

@FunctionInfo(
Expand Down Expand Up @@ -84,11 +82,6 @@ protected TypeResolution resolveType() {
return TypeResolution.TYPE_RESOLVED;
}

@Override
public ScriptTemplate asScript() {
throw new UnsupportedOperationException();
}

@Override
public Expression replaceChildren(List<Expression> newChildren) {
return new Greatest(source(), newChildren.get(0), newChildren.subList(1, newChildren.size()));
Expand All @@ -104,11 +97,6 @@ public boolean foldable() {
return Expressions.foldable(children());
}

@Override
public Object fold() {
return EvaluatorMapper.super.fold();
}

@Override
public ExpressionEvaluator.Factory toEvaluator(Function<Expression, ExpressionEvaluator.Factory> toEvaluator) {
// force datatype initialization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@
import org.elasticsearch.compute.ann.Evaluator;
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException;
import org.elasticsearch.xpack.esql.evaluator.mapper.EvaluatorMapper;
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
import org.elasticsearch.xpack.esql.expression.function.Param;
import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlScalarFunction;
import org.elasticsearch.xpack.esql.expression.function.scalar.multivalue.MvMin;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.Expressions;
import org.elasticsearch.xpack.ql.expression.TypeResolutions;
import org.elasticsearch.xpack.ql.expression.function.OptionalArgument;
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.ql.tree.NodeInfo;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.ql.type.DataType;
Expand All @@ -35,7 +33,7 @@
/**
* Returns the minimum value of multiple columns.
*/
public class Least extends ScalarFunction implements EvaluatorMapper, OptionalArgument {
public class Least extends EsqlScalarFunction implements OptionalArgument {
private DataType dataType;

@FunctionInfo(
Expand Down Expand Up @@ -84,11 +82,6 @@ protected TypeResolution resolveType() {
return TypeResolution.TYPE_RESOLVED;
}

@Override
public ScriptTemplate asScript() {
throw new UnsupportedOperationException();
}

@Override
public Expression replaceChildren(List<Expression> newChildren) {
return new Least(source(), newChildren.get(0), newChildren.subList(1, newChildren.size()));
Expand All @@ -104,11 +97,6 @@ public boolean foldable() {
return Expressions.foldable(children());
}

@Override
public Object fold() {
return EvaluatorMapper.super.fold();
}

@Override
public ExpressionEvaluator.Factory toEvaluator(Function<Expression, ExpressionEvaluator.Factory> toEvaluator) {
// force datatype initialization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
import org.elasticsearch.core.Releasables;
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException;
import org.elasticsearch.xpack.esql.evaluator.mapper.EvaluatorMapper;
import org.elasticsearch.xpack.esql.expression.function.Warnings;
import org.elasticsearch.xpack.esql.expression.function.scalar.UnaryScalarFunction;
import org.elasticsearch.xpack.esql.type.EsqlDataTypes;
Expand All @@ -41,7 +40,7 @@
/**
* Base class for functions that converts a field into a function-specific type.
*/
public abstract class AbstractConvertFunction extends UnaryScalarFunction implements EvaluatorMapper {
public abstract class AbstractConvertFunction extends UnaryScalarFunction {

// the numeric types convert functions need to handle; the other numeric types are converted upstream to one of these
private static final List<DataType> NUMERIC_TYPES = List.of(
Expand Down Expand Up @@ -101,11 +100,6 @@ interface BuildFactory {

protected abstract Map<DataType, BuildFactory> factories();

@Override
public final Object fold() {
return EvaluatorMapper.super.fold();
}

@Override
public ExpressionEvaluator.Factory toEvaluator(Function<Expression, ExpressionEvaluator.Factory> toEvaluator) {
return evaluator(toEvaluator.apply(field()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
import org.elasticsearch.compute.ann.Evaluator;
import org.elasticsearch.compute.ann.Fixed;
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
import org.elasticsearch.xpack.esql.evaluator.mapper.EvaluatorMapper;
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
import org.elasticsearch.xpack.esql.expression.function.Param;
import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlScalarFunction;
import org.elasticsearch.xpack.ql.InvalidArgumentException;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.function.OptionalArgument;
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.ql.tree.NodeInfo;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.ql.type.DataType;
Expand Down Expand Up @@ -49,7 +47,7 @@
* in multiples of the unit specified in the first argument.
* If the second argument (start) is greater than the third argument (end), then negative values are returned.
*/
public class DateDiff extends ScalarFunction implements OptionalArgument, EvaluatorMapper {
public class DateDiff extends EsqlScalarFunction implements OptionalArgument {

public static final ZoneId UTC = ZoneId.of("Z");

Expand Down Expand Up @@ -188,11 +186,6 @@ protected TypeResolution resolveType() {
return TypeResolution.TYPE_RESOLVED;
}

@Override
public Object fold() {
return EvaluatorMapper.super.fold();
}

@Override
public boolean foldable() {
return unit.foldable() && startTimestamp.foldable() && endTimestamp.foldable();
Expand All @@ -203,11 +196,6 @@ public DataType dataType() {
return DataTypes.INTEGER;
}

@Override
public ScriptTemplate asScript() {
throw new UnsupportedOperationException("functions do not support scripting");
}

@Override
public Expression replaceChildren(List<Expression> newChildren) {
return new DateDiff(source(), newChildren.get(0), newChildren.get(1), newChildren.get(2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
import org.elasticsearch.compute.ann.Evaluator;
import org.elasticsearch.compute.ann.Fixed;
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
import org.elasticsearch.xpack.esql.evaluator.mapper.EvaluatorMapper;
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
import org.elasticsearch.xpack.esql.expression.function.Param;
import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlConfigurationFunction;
import org.elasticsearch.xpack.ql.InvalidArgumentException;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.TypeResolutions;
import org.elasticsearch.xpack.ql.expression.function.scalar.ConfigurationFunction;
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.ql.session.Configuration;
import org.elasticsearch.xpack.ql.tree.NodeInfo;
import org.elasticsearch.xpack.ql.tree.Source;
Expand All @@ -36,7 +34,7 @@
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isDate;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isStringAndExact;

public class DateExtract extends ConfigurationFunction implements EvaluatorMapper {
public class DateExtract extends EsqlConfigurationFunction {

private ChronoField chronoField;

Expand Down Expand Up @@ -116,11 +114,6 @@ public DataType dataType() {
return DataTypes.LONG;
}

@Override
public ScriptTemplate asScript() {
throw new UnsupportedOperationException("functions do not support scripting");
}

@Override
protected TypeResolution resolveType() {
if (childrenResolved() == false) {
Expand All @@ -135,10 +128,4 @@ protected TypeResolution resolveType() {
public boolean foldable() {
return children().get(0).foldable() && children().get(1).foldable();
}

@Override
public Object fold() {
return EvaluatorMapper.super.fold();
}

}
Loading

0 comments on commit 9151d2d

Please sign in to comment.