Skip to content

Commit

Permalink
Remove esql-core ParsingException (elastic#118789)
Browse files Browse the repository at this point in the history
* Remove esql-core ParsingException
Move fulltext related classes: querydsl, predicate and tests
  • Loading branch information
astefan authored Dec 17, 2024
1 parent e1d06a8 commit b444697
Show file tree
Hide file tree
Showing 25 changed files with 66 additions and 124 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
import org.elasticsearch.xpack.esql.core.expression.MetadataAttribute;
import org.elasticsearch.xpack.esql.core.expression.predicate.fulltext.MultiMatchQueryPredicate;
import org.elasticsearch.xpack.esql.core.expression.predicate.logical.And;
import org.elasticsearch.xpack.esql.core.expression.predicate.logical.Not;
import org.elasticsearch.xpack.esql.core.expression.predicate.logical.Or;
Expand All @@ -22,7 +21,6 @@
import org.elasticsearch.xpack.esql.core.expression.predicate.regex.WildcardLike;
import org.elasticsearch.xpack.esql.core.querydsl.query.BoolQuery;
import org.elasticsearch.xpack.esql.core.querydsl.query.ExistsQuery;
import org.elasticsearch.xpack.esql.core.querydsl.query.MultiMatchQuery;
import org.elasticsearch.xpack.esql.core.querydsl.query.NotQuery;
import org.elasticsearch.xpack.esql.core.querydsl.query.Query;
import org.elasticsearch.xpack.esql.core.querydsl.query.RegexQuery;
Expand Down Expand Up @@ -71,18 +69,6 @@ private static Query translateField(RegexMatch e, String targetFieldName) {
}
}

public static class MultiMatches extends ExpressionTranslator<MultiMatchQueryPredicate> {

@Override
protected Query asQuery(MultiMatchQueryPredicate q, TranslatorHandler handler) {
return doTranslate(q, handler);
}

public static Query doTranslate(MultiMatchQueryPredicate q, TranslatorHandler handler) {
return new MultiMatchQuery(q.source(), q.query(), q.fields(), q);
}
}

public static class BinaryLogic extends ExpressionTranslator<
org.elasticsearch.xpack.esql.core.expression.predicate.logical.BinaryLogic> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.util.CollectionUtils;
import org.elasticsearch.common.util.FeatureFlag;
import org.elasticsearch.xpack.esql.core.ParsingException;
import org.elasticsearch.xpack.esql.core.QlIllegalArgumentException;
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.expression.function.Function;
Expand Down Expand Up @@ -147,6 +146,7 @@
import org.elasticsearch.xpack.esql.expression.function.scalar.string.ToUpper;
import org.elasticsearch.xpack.esql.expression.function.scalar.string.Trim;
import org.elasticsearch.xpack.esql.expression.function.scalar.util.Delay;
import org.elasticsearch.xpack.esql.parser.ParsingException;
import org.elasticsearch.xpack.esql.session.Configuration;

import java.lang.reflect.Constructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.xpack.esql.action.EsqlCapabilities;
import org.elasticsearch.xpack.esql.core.expression.predicate.fulltext.MatchQueryPredicate;
import org.elasticsearch.xpack.esql.core.expression.predicate.fulltext.MultiMatchQueryPredicate;
import org.elasticsearch.xpack.esql.expression.predicate.fulltext.MatchQueryPredicate;
import org.elasticsearch.xpack.esql.expression.predicate.fulltext.MultiMatchQueryPredicate;

import java.util.ArrayList;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.esql.core.expression.predicate.fulltext;
package org.elasticsearch.xpack.esql.expression.predicate.fulltext;

import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.esql.core.expression.predicate.fulltext;
package org.elasticsearch.xpack.esql.expression.predicate.fulltext;

import org.elasticsearch.common.Strings;
import org.elasticsearch.common.util.Maps;
import org.elasticsearch.xpack.esql.core.ParsingException;
import org.elasticsearch.xpack.esql.core.expression.predicate.fulltext.FullTextPredicate.Operator;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.expression.predicate.fulltext.FullTextPredicate.Operator;
import org.elasticsearch.xpack.esql.parser.ParsingException;

import java.util.LinkedHashMap;
import java.util.Locale;
Expand Down Expand Up @@ -86,7 +86,7 @@ private static String[] splitInTwo(String string, String delimiter) {
return split;
}

static FullTextPredicate.Operator operator(Map<String, String> options, String key) {
static Operator operator(Map<String, String> options, String key) {
String value = options.get(key);
return value != null ? Operator.valueOf(value.toUpperCase(Locale.ROOT)) : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.esql.core.expression.predicate.fulltext;
package org.elasticsearch.xpack.esql.expression.predicate.fulltext;

import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.esql.core.expression.predicate.fulltext;
package org.elasticsearch.xpack.esql.expression.predicate.fulltext;

import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ private <T> T invokeParser(
BiFunction<AstBuilder, ParserRuleContext, T> result
) {
if (query.length() > MAX_LENGTH) {
throw new org.elasticsearch.xpack.esql.core.ParsingException(
"ESQL statement is too large [{} characters > {}]",
query.length(),
MAX_LENGTH
);
throw new ParsingException("ESQL statement is too large [{} characters > {}]", query.length(), MAX_LENGTH);
}
try {
EsqlBaseLexer lexer = new EsqlBaseLexer(CharStreams.fromString(query));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.elasticsearch.common.util.Maps;
import org.elasticsearch.xpack.esql.core.ParsingException;
import org.elasticsearch.xpack.esql.core.tree.Location;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.util.Check;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ParsingException(String message, Exception cause, int line, int charPosit
this.charPositionInLine = charPositionInLine + 1;
}

ParsingException(String message, Object... args) {
public ParsingException(String message, Object... args) {
this(Source.EMPTY, message, args);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.elasticsearch.xpack.esql.core.planner.ExpressionTranslators;
import org.elasticsearch.xpack.esql.core.planner.TranslatorHandler;
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;
import org.elasticsearch.xpack.esql.core.querydsl.query.Query;
import org.elasticsearch.xpack.esql.core.querydsl.query.QueryStringQuery;
Expand All @@ -44,6 +43,7 @@
import org.elasticsearch.xpack.esql.expression.function.scalar.ip.CIDRMatch;
import org.elasticsearch.xpack.esql.expression.function.scalar.spatial.SpatialRelatesFunction;
import org.elasticsearch.xpack.esql.expression.function.scalar.spatial.SpatialRelatesUtils;
import org.elasticsearch.xpack.esql.expression.predicate.fulltext.MultiMatchQueryPredicate;
import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.Equals;
import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.GreaterThan;
import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.GreaterThanOrEqual;
Expand All @@ -53,6 +53,8 @@
import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.LessThanOrEqual;
import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.NotEquals;
import org.elasticsearch.xpack.esql.querydsl.query.KqlQuery;
import org.elasticsearch.xpack.esql.querydsl.query.MatchQuery;
import org.elasticsearch.xpack.esql.querydsl.query.MultiMatchQuery;
import org.elasticsearch.xpack.esql.querydsl.query.SpatialRelatesQuery;
import org.elasticsearch.xpack.versionfield.Version;

Expand Down Expand Up @@ -92,7 +94,7 @@ public final class EsqlExpressionTranslators {
new ExpressionTranslators.IsNotNulls(),
new ExpressionTranslators.Nots(),
new ExpressionTranslators.Likes(),
new ExpressionTranslators.MultiMatches(),
new MultiMatches(),
new MatchFunctionTranslator(),
new QueryStringFunctionTranslator(),
new KqlFunctionTranslator(),
Expand Down Expand Up @@ -537,6 +539,18 @@ private static RangeQuery translate(Range r, TranslatorHandler handler) {
}
}

public static class MultiMatches extends ExpressionTranslator<MultiMatchQueryPredicate> {

@Override
protected Query asQuery(MultiMatchQueryPredicate q, TranslatorHandler handler) {
return doTranslate(q, handler);
}

public static Query doTranslate(MultiMatchQueryPredicate q, TranslatorHandler handler) {
return new MultiMatchQuery(q.source(), q.query(), q.fields(), q);
}
}

public static class MatchFunctionTranslator extends ExpressionTranslator<Match> {
@Override
protected Query asQuery(Match match, TranslatorHandler handler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.esql.core.querydsl.query;
package org.elasticsearch.xpack.esql.querydsl.query;

import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.xpack.esql.core.querydsl.query.Query;
import org.elasticsearch.xpack.esql.core.tree.Source;

import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.esql.core.querydsl.query;
package org.elasticsearch.xpack.esql.querydsl.query;

import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.xpack.esql.core.expression.predicate.fulltext.MultiMatchQueryPredicate;
import org.elasticsearch.xpack.esql.core.querydsl.query.Query;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.expression.predicate.fulltext.MultiMatchQueryPredicate;

import java.util.Map;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.json.JsonXContent;
import org.elasticsearch.xpack.esql.LoadMapping;
import org.elasticsearch.xpack.esql.core.ParsingException;
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.expression.function.EsqlFunctionRegistry;
import org.elasticsearch.xpack.esql.expression.function.FunctionDefinition;
import org.elasticsearch.xpack.esql.index.EsIndex;
import org.elasticsearch.xpack.esql.index.IndexResolution;
import org.elasticsearch.xpack.esql.parser.EsqlParser;
import org.elasticsearch.xpack.esql.parser.ParsingException;
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
import org.elasticsearch.xpack.esql.plan.logical.Row;
import org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter;
Expand Down Expand Up @@ -49,27 +49,27 @@ public class ParsingTests extends ESTestCase {
);

public void testCaseFunctionInvalidInputs() {
assertEquals("1:23: error building [case]: expects at least two arguments", error("row a = 1 | eval x = case()"));
assertEquals("1:23: error building [case]: expects at least two arguments", error("row a = 1 | eval x = case(a)"));
assertEquals("1:23: error building [case]: expects at least two arguments", error("row a = 1 | eval x = case(1)"));
assertEquals("1:22: error building [case]: expects at least two arguments", error("row a = 1 | eval x = case()"));
assertEquals("1:22: error building [case]: expects at least two arguments", error("row a = 1 | eval x = case(a)"));
assertEquals("1:22: error building [case]: expects at least two arguments", error("row a = 1 | eval x = case(1)"));
}

public void testConcatFunctionInvalidInputs() {
assertEquals("1:23: error building [concat]: expects at least two arguments", error("row a = 1 | eval x = concat()"));
assertEquals("1:23: error building [concat]: expects at least two arguments", error("row a = 1 | eval x = concat(a)"));
assertEquals("1:23: error building [concat]: expects at least two arguments", error("row a = 1 | eval x = concat(1)"));
assertEquals("1:22: error building [concat]: expects at least two arguments", error("row a = 1 | eval x = concat()"));
assertEquals("1:22: error building [concat]: expects at least two arguments", error("row a = 1 | eval x = concat(a)"));
assertEquals("1:22: error building [concat]: expects at least two arguments", error("row a = 1 | eval x = concat(1)"));
}

public void testCoalesceFunctionInvalidInputs() {
assertEquals("1:23: error building [coalesce]: expects at least one argument", error("row a = 1 | eval x = coalesce()"));
assertEquals("1:22: error building [coalesce]: expects at least one argument", error("row a = 1 | eval x = coalesce()"));
}

public void testGreatestFunctionInvalidInputs() {
assertEquals("1:23: error building [greatest]: expects at least one argument", error("row a = 1 | eval x = greatest()"));
assertEquals("1:22: error building [greatest]: expects at least one argument", error("row a = 1 | eval x = greatest()"));
}

public void testLeastFunctionInvalidInputs() {
assertEquals("1:23: error building [least]: expects at least one argument", error("row a = 1 | eval x = least()"));
assertEquals("1:22: error building [least]: expects at least one argument", error("row a = 1 | eval x = least()"));
}

/**
Expand Down Expand Up @@ -108,7 +108,7 @@ public void testTooBigQuery() {
while (query.length() < EsqlParser.MAX_LENGTH) {
query.append(", a = CONCAT(a, a)");
}
assertEquals("-1:0: ESQL statement is too large [1000011 characters > 1000000]", error(query.toString()));
assertEquals("-1:-1: ESQL statement is too large [1000011 characters > 1000000]", error(query.toString()));
}

private String functionName(EsqlFunctionRegistry registry, Expression functionCall) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.compute.operator.EvalOperator;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.esql.core.ParsingException;
import org.elasticsearch.xpack.esql.core.QlIllegalArgumentException;
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.expression.function.scalar.ScalarFunction;
Expand All @@ -19,6 +18,7 @@
import org.elasticsearch.xpack.esql.core.tree.SourceTests;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlConfigurationFunction;
import org.elasticsearch.xpack.esql.parser.ParsingException;
import org.elasticsearch.xpack.esql.session.Configuration;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
* 2.0.
*/

package org.elasticsearch.xpack.esql.expression.predicate.operator.fulltext;
package org.elasticsearch.xpack.esql.expression.predicate.fulltext;

import org.elasticsearch.xpack.esql.core.expression.predicate.fulltext.FullTextPredicate;
import org.elasticsearch.xpack.esql.expression.AbstractExpressionSerializationTests;

import java.util.HashMap;
Expand Down
Loading

0 comments on commit b444697

Please sign in to comment.