Skip to content

Commit

Permalink
Move ESQL's LOCATE test cases to cases
Browse files Browse the repository at this point in the history
This moves the test cases declared in the tests for ESQL's LOCATE
function to test cases which will cause elastic#106782 to properly generate all
of the available signatures. It also buys us all of testing for
incorrect parameter combinations.
  • Loading branch information
nik9000 committed Apr 9, 2024
1 parent e28ecbb commit 7518367
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 170 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// 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.string;

import java.lang.IllegalArgumentException;
import java.lang.Override;
import java.lang.String;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.compute.data.Block;
import org.elasticsearch.compute.data.BytesRefBlock;
import org.elasticsearch.compute.data.BytesRefVector;
import org.elasticsearch.compute.data.IntBlock;
import org.elasticsearch.compute.data.IntVector;
import org.elasticsearch.compute.data.Page;
import org.elasticsearch.compute.operator.DriverContext;
import org.elasticsearch.compute.operator.EvalOperator;
import org.elasticsearch.core.Releasables;
import org.elasticsearch.xpack.esql.expression.function.Warnings;
import org.elasticsearch.xpack.ql.tree.Source;

/**
* {@link EvalOperator.ExpressionEvaluator} implementation for {@link Locate}.
* This class is generated. Do not edit it.
*/
public final class LocateNoStartEvaluator implements EvalOperator.ExpressionEvaluator {
private final Warnings warnings;

private final EvalOperator.ExpressionEvaluator str;

private final EvalOperator.ExpressionEvaluator substr;

private final DriverContext driverContext;

public LocateNoStartEvaluator(Source source, EvalOperator.ExpressionEvaluator str,
EvalOperator.ExpressionEvaluator substr, DriverContext driverContext) {
this.warnings = new Warnings(source);
this.str = str;
this.substr = substr;
this.driverContext = driverContext;
}

@Override
public Block eval(Page page) {
try (BytesRefBlock strBlock = (BytesRefBlock) str.eval(page)) {
try (BytesRefBlock substrBlock = (BytesRefBlock) substr.eval(page)) {
BytesRefVector strVector = strBlock.asVector();
if (strVector == null) {
return eval(page.getPositionCount(), strBlock, substrBlock);
}
BytesRefVector substrVector = substrBlock.asVector();
if (substrVector == null) {
return eval(page.getPositionCount(), strBlock, substrBlock);
}
return eval(page.getPositionCount(), strVector, substrVector).asBlock();
}
}
}

public IntBlock eval(int positionCount, BytesRefBlock strBlock, BytesRefBlock substrBlock) {
try(IntBlock.Builder result = driverContext.blockFactory().newIntBlockBuilder(positionCount)) {
BytesRef strScratch = new BytesRef();
BytesRef substrScratch = new BytesRef();
position: for (int p = 0; p < positionCount; p++) {
if (strBlock.isNull(p)) {
result.appendNull();
continue position;
}
if (strBlock.getValueCount(p) != 1) {
if (strBlock.getValueCount(p) > 1) {
warnings.registerException(new IllegalArgumentException("single-value function encountered multi-value"));
}
result.appendNull();
continue position;
}
if (substrBlock.isNull(p)) {
result.appendNull();
continue position;
}
if (substrBlock.getValueCount(p) != 1) {
if (substrBlock.getValueCount(p) > 1) {
warnings.registerException(new IllegalArgumentException("single-value function encountered multi-value"));
}
result.appendNull();
continue position;
}
result.appendInt(Locate.process(strBlock.getBytesRef(strBlock.getFirstValueIndex(p), strScratch), substrBlock.getBytesRef(substrBlock.getFirstValueIndex(p), substrScratch)));
}
return result.build();
}
}

public IntVector eval(int positionCount, BytesRefVector strVector, BytesRefVector substrVector) {
try(IntVector.Builder result = driverContext.blockFactory().newIntVectorBuilder(positionCount)) {
BytesRef strScratch = new BytesRef();
BytesRef substrScratch = new BytesRef();
position: for (int p = 0; p < positionCount; p++) {
result.appendInt(Locate.process(strVector.getBytesRef(p, strScratch), substrVector.getBytesRef(p, substrScratch)));
}
return result.build();
}
}

@Override
public String toString() {
return "LocateNoStartEvaluator[" + "str=" + str + ", substr=" + substr + "]";
}

@Override
public void close() {
Releasables.closeExpectNoException(str, substr);
}

static class Factory implements EvalOperator.ExpressionEvaluator.Factory {
private final Source source;

private final EvalOperator.ExpressionEvaluator.Factory str;

private final EvalOperator.ExpressionEvaluator.Factory substr;

public Factory(Source source, EvalOperator.ExpressionEvaluator.Factory str,
EvalOperator.ExpressionEvaluator.Factory substr) {
this.source = source;
this.str = str;
this.substr = substr;
}

@Override
public LocateNoStartEvaluator get(DriverContext context) {
return new LocateNoStartEvaluator(source, str.get(context), substr.get(context), context);
}

@Override
public String toString() {
return "LocateNoStartEvaluator[" + "str=" + str + ", substr=" + substr + "]";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.FIRST;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.SECOND;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.THIRD;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isInteger;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isString;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isType;

/**
* Locate function, given a string 'a' and a substring 'b', it returns the index of the first occurrence of the substring 'b' in 'a'.
Expand Down Expand Up @@ -80,7 +80,7 @@ protected TypeResolution resolveType() {
return resolution;
}

return start == null ? TypeResolution.TYPE_RESOLVED : isInteger(start, sourceText(), THIRD);
return start == null ? TypeResolution.TYPE_RESOLVED : isType(start, dt -> dt == DataTypes.INTEGER, sourceText(), THIRD, "integer");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -896,10 +896,12 @@ protected static String typeErrorMessage(boolean includeOrdinal, List<Set<DataTy
),
Map.entry(Set.of(DataTypes.DOUBLE, DataTypes.NULL), "double"),
Map.entry(Set.of(DataTypes.INTEGER, DataTypes.NULL), "integer"),
Map.entry(Set.of(DataTypes.INTEGER), "integer"),
Map.entry(Set.of(DataTypes.IP, DataTypes.NULL), "ip"),
Map.entry(Set.of(DataTypes.LONG, DataTypes.INTEGER, DataTypes.UNSIGNED_LONG, DataTypes.DOUBLE, DataTypes.NULL), "numeric"),
Map.entry(Set.of(DataTypes.KEYWORD, DataTypes.TEXT, DataTypes.VERSION, DataTypes.NULL), "string or version"),
Map.entry(Set.of(DataTypes.KEYWORD, DataTypes.TEXT, DataTypes.NULL), "string"),
Map.entry(Set.of(DataTypes.KEYWORD, DataTypes.TEXT), "string"),
Map.entry(Set.of(DataTypes.IP, DataTypes.KEYWORD, DataTypes.TEXT, DataTypes.NULL), "ip or string"),
Map.entry(Set.copyOf(Arrays.asList(representableTypes())), "representable"),
Map.entry(Set.copyOf(Arrays.asList(representableNonSpatialTypes())), "representableNonSpatial"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public TestCaseSupplier(List<DataType> types, Supplier<TestCase> supplier) {
this(nameFromTypes(types), types, supplier);
}

static String nameFromTypes(List<DataType> types) {
public static String nameFromTypes(List<DataType> types) {
return types.stream().map(t -> "<" + t.typeName() + ">").collect(Collectors.joining(", "));
}

Expand Down
Loading

0 comments on commit 7518367

Please sign in to comment.