Skip to content

Commit

Permalink
Register the KQL function in various places.
Browse files Browse the repository at this point in the history
  • Loading branch information
afoucret committed Nov 13, 2024
1 parent 8295038 commit e41327b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.xpack.esql.expression.function.aggregate.FilteredExpression;
import org.elasticsearch.xpack.esql.expression.function.aggregate.Rate;
import org.elasticsearch.xpack.esql.expression.function.fulltext.FullTextFunction;
import org.elasticsearch.xpack.esql.expression.function.fulltext.Kql;
import org.elasticsearch.xpack.esql.expression.function.fulltext.Match;
import org.elasticsearch.xpack.esql.expression.function.fulltext.QueryString;
import org.elasticsearch.xpack.esql.expression.function.grouping.GroupingFunction;
Expand Down Expand Up @@ -684,6 +685,14 @@ private static void checkFullTextQueryFunctions(LogicalPlan plan, Set<Failure> f
qsf -> "[" + qsf.functionName() + "] " + qsf.functionType(),
failures
);
checkCommandsBeforeExpression(
plan,
condition,
Kql.class,
lp -> (lp instanceof Filter || lp instanceof OrderBy || lp instanceof EsRelation),
kqlFunc -> "[" + kqlFunc.functionName() + "] " + kqlFunc.functionType(),
failures
);
checkCommandsBeforeExpression(
plan,
condition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.elasticsearch.xpack.esql.expression.function.aggregate.Top;
import org.elasticsearch.xpack.esql.expression.function.aggregate.Values;
import org.elasticsearch.xpack.esql.expression.function.aggregate.WeightedAvg;
import org.elasticsearch.xpack.esql.expression.function.fulltext.Kql;
import org.elasticsearch.xpack.esql.expression.function.fulltext.Match;
import org.elasticsearch.xpack.esql.expression.function.fulltext.QueryString;
import org.elasticsearch.xpack.esql.expression.function.grouping.Bucket;
Expand Down Expand Up @@ -406,7 +407,8 @@ private static FunctionDefinition[][] snapshotFunctions() {
// This is an experimental function and can be removed without notice.
def(Delay.class, Delay::new, "delay"),
def(Categorize.class, Categorize::new, "categorize"),
def(Rate.class, Rate::withUnresolvedTimestamp, "rate") } };
def(Rate.class, Rate::withUnresolvedTimestamp, "rate"),
def(Kql.class, Kql::new, "kql") } };
}

public EsqlFunctionRegistry snapshotRegistry() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@

import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.xpack.esql.action.EsqlCapabilities;
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.expression.Nullability;
import org.elasticsearch.xpack.esql.core.expression.TypeResolutions;
import org.elasticsearch.xpack.esql.core.expression.function.Function;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
Expand All @@ -30,7 +33,13 @@
*/
public abstract class FullTextFunction extends Function {
public static List<NamedWriteableRegistry.Entry> getNamedWriteables() {
return List.of(QueryString.ENTRY, Match.ENTRY);
List<NamedWriteableRegistry.Entry> writeables = new ArrayList<>(List.of(QueryString.ENTRY, Match.ENTRY));

if (EsqlCapabilities.Cap.KQL_FUNCTION.isEnabled()) {
writeables.add(Kql.ENTRY);
}

return Collections.unmodifiableList(writeables);
}

private final Expression query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.core.util.CollectionUtils;
import org.elasticsearch.xpack.esql.core.util.Queries;
import org.elasticsearch.xpack.esql.expression.function.fulltext.FullTextFunction;
import org.elasticsearch.xpack.esql.expression.function.fulltext.Match;
import org.elasticsearch.xpack.esql.expression.function.fulltext.QueryString;
import org.elasticsearch.xpack.esql.expression.function.scalar.ip.CIDRMatch;
import org.elasticsearch.xpack.esql.expression.function.scalar.spatial.BinarySpatialFunction;
import org.elasticsearch.xpack.esql.expression.function.scalar.spatial.SpatialRelatesFunction;
Expand Down Expand Up @@ -255,10 +255,10 @@ static boolean canPushToSource(Expression exp, LucenePushdownPredicates lucenePu
return canPushSpatialFunctionToSource(spatial, lucenePushdownPredicates);
} else if (exp instanceof StringQueryPredicate) {
return true;
} else if (exp instanceof QueryString) {
return true;
} else if (exp instanceof Match mf) {
return mf.field() instanceof FieldAttribute && DataType.isString(mf.field().dataType());
} else if (exp instanceof FullTextFunction) {
return true;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.xpack.esql.core.planner.ExpressionTranslator;
import org.elasticsearch.xpack.esql.core.planner.ExpressionTranslators;
import org.elasticsearch.xpack.esql.core.planner.TranslatorHandler;
import org.elasticsearch.xpack.esql.core.querydsl.query.KqlQuery;
import org.elasticsearch.xpack.esql.core.querydsl.query.MatchAll;
import org.elasticsearch.xpack.esql.core.querydsl.query.MatchQuery;
import org.elasticsearch.xpack.esql.core.querydsl.query.NotQuery;
Expand All @@ -34,6 +35,7 @@
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.core.util.Check;
import org.elasticsearch.xpack.esql.expression.function.fulltext.Kql;
import org.elasticsearch.xpack.esql.expression.function.fulltext.Match;
import org.elasticsearch.xpack.esql.expression.function.fulltext.QueryString;
import org.elasticsearch.xpack.esql.expression.function.scalar.ip.CIDRMatch;
Expand Down Expand Up @@ -90,6 +92,7 @@ public final class EsqlExpressionTranslators {
new ExpressionTranslators.MultiMatches(),
new MatchFunctionTranslator(),
new QueryStringFunctionTranslator(),
new KqlFunctionTranslator(),
new Scalars()
);

Expand Down Expand Up @@ -539,4 +542,11 @@ protected Query asQuery(QueryString queryString, TranslatorHandler handler) {
return new QueryStringQuery(queryString.source(), queryString.queryAsText(), Map.of(), null);
}
}

public static class KqlFunctionTranslator extends ExpressionTranslator<Kql> {
@Override
protected Query asQuery(Kql kqlFunction, TranslatorHandler handler) {
return new KqlQuery(kqlFunction.source(), kqlFunction.queryAsText());
}
}
}

0 comments on commit e41327b

Please sign in to comment.