EQL: Fix custom scripting for functions (#53935) (#54114)

Improve separation of scripting between EQL and SQL by delegating common
methods to QL. The context detection is determined based on the package
to avoid having repetitive class hierarchies.
The Painless whitelists have been improved so that the declaring class
is used instead of the inherited one.

Relates #53688

(cherry picked from commit 6d46033e736c64ac9255c5d6964600d2a931430a)

EQL: Add Substring function with Python semantics (#53688)

Does not reuse substring from SQL due to the difference in semantics and
the accepted arguments.
Currently it is missing full integration tests as, due to the usage of
scripting, requires an actual integration test against a proper cluster
(and likely its own QA project).

(cherry picked from commit f58680bad33d5ce4139157a69a4d9f5f286bc3c4)
This commit is contained in:
Costin Leau 2020-03-24 20:54:19 +02:00 committed by GitHub
parent b21b7fb09b
commit 68f74cf593
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
40 changed files with 870 additions and 215 deletions

View File

@ -881,34 +881,6 @@ file where opcode=0 and indexOf(file_name, 'explorer.', 0) == 0'''
expected_event_ids = [88, 92]
description = "check substring ranges"
[[queries]]
query = '''
file where serial_event_id=88 and substring(file_name, 0, 4) == 'expl'
'''
expected_event_ids = [88]
description = "check substring ranges"
[[queries]]
query = '''
file where serial_event_id=88 and substring(file_name, 1, 3) == 'xp'
'''
expected_event_ids = [88]
description = "chaeck substring ranges"
[[queries]]
query = '''
file where serial_event_id=88 and substring(file_name, -4) == '.exe'
'''
expected_event_ids = [88]
description = "check substring ranges"
[[queries]]
query = '''
file where serial_event_id=88 and substring(file_name, -4, -1) == '.ex'
'''
expected_event_ids = [88]
description = "check substring ranges"
[[queries]]
query = '''
process where add(serial_event_id, 0) == 1 and add(0, 1) == serial_event_id'''

View File

@ -10,9 +10,11 @@ import org.elasticsearch.xpack.ql.common.Failure;
import org.elasticsearch.xpack.ql.expression.Attribute;
import org.elasticsearch.xpack.ql.expression.NamedExpression;
import org.elasticsearch.xpack.ql.expression.UnresolvedAttribute;
import org.elasticsearch.xpack.ql.expression.function.Function;
import org.elasticsearch.xpack.ql.expression.function.FunctionDefinition;
import org.elasticsearch.xpack.ql.expression.function.FunctionRegistry;
import org.elasticsearch.xpack.ql.expression.function.UnresolvedFunction;
import org.elasticsearch.xpack.ql.plan.logical.LogicalPlan;
import org.elasticsearch.xpack.ql.rule.Rule;
import org.elasticsearch.xpack.ql.rule.RuleExecutor;
import java.util.ArrayList;
@ -35,7 +37,8 @@ public class Analyzer extends RuleExecutor<LogicalPlan> {
@Override
protected Iterable<RuleExecutor<LogicalPlan>.Batch> batches() {
Batch resolution = new Batch("Resolution",
new ResolveRefs());
new ResolveRefs(),
new ResolveFunctions());
return asList(resolution);
}
@ -52,7 +55,7 @@ public class Analyzer extends RuleExecutor<LogicalPlan> {
return plan;
}
private static class ResolveRefs extends AnalyzeRule<LogicalPlan> {
private static class ResolveRefs extends AnalyzerRule<LogicalPlan> {
@Override
protected LogicalPlan rule(LogicalPlan plan) {
@ -87,20 +90,34 @@ public class Analyzer extends RuleExecutor<LogicalPlan> {
}
}
abstract static class AnalyzeRule<SubPlan extends LogicalPlan> extends Rule<SubPlan, LogicalPlan> {
// transformUp (post-order) - that is first children and then the node
// but with a twist; only if the tree is not resolved or analyzed
@Override
public final LogicalPlan apply(LogicalPlan plan) {
return plan.transformUp(t -> t.analyzed() || skipResolved() && t.resolved() ? t : rule(t), typeToken());
}
private class ResolveFunctions extends AnalyzerRule<LogicalPlan> {
@Override
protected abstract LogicalPlan rule(SubPlan plan);
protected LogicalPlan rule(LogicalPlan plan) {
return plan.transformExpressionsUp(e -> {
if (e instanceof UnresolvedFunction) {
UnresolvedFunction uf = (UnresolvedFunction) e;
protected boolean skipResolved() {
return true;
if (uf.analyzed()) {
return uf;
}
String name = uf.name();
if (uf.childrenResolved() == false) {
return uf;
}
String functionName = functionRegistry.resolveAlias(name);
if (functionRegistry.functionExists(functionName) == false) {
return uf.missing(functionName, functionRegistry.listFunctions());
}
FunctionDefinition def = functionRegistry.resolveFunction(functionName);
Function f = uf.buildResolved(null, def);
return f;
}
return e;
});
}
}
}

View File

@ -12,6 +12,7 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.xpack.eql.analysis.Analyzer;
import org.elasticsearch.xpack.eql.analysis.PreAnalyzer;
import org.elasticsearch.xpack.eql.analysis.Verifier;
import org.elasticsearch.xpack.eql.expression.function.EqlFunctionRegistry;
import org.elasticsearch.xpack.eql.optimizer.Optimizer;
import org.elasticsearch.xpack.eql.parser.ParserParams;
import org.elasticsearch.xpack.eql.planner.Planner;
@ -44,7 +45,7 @@ public class PlanExecutor {
this.writableRegistry = writeableRegistry;
this.indexResolver = indexResolver;
this.functionRegistry = null;
this.functionRegistry = new EqlFunctionRegistry();
this.metrics = new Metrics();

View File

@ -6,10 +6,30 @@
package org.elasticsearch.xpack.eql.expression.function;
import org.elasticsearch.xpack.eql.expression.function.scalar.string.Substring;
import org.elasticsearch.xpack.ql.expression.function.FunctionDefinition;
import org.elasticsearch.xpack.ql.expression.function.FunctionRegistry;
import java.util.Locale;
public class EqlFunctionRegistry extends FunctionRegistry {
public EqlFunctionRegistry() {
super(functions());
}
private static FunctionDefinition[][] functions() {
return new FunctionDefinition[][] {
// Scalar functions
// String
new FunctionDefinition[] {
def(Substring.class, Substring::new, "substring"),
},
};
}
@Override
protected String normalize(String name) {
return name.toLowerCase(Locale.ROOT);
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.eql.expression.function.scalar.string;
import org.elasticsearch.common.Strings;
import static org.elasticsearch.common.Strings.hasLength;
final class StringUtils {
private StringUtils() {}
/**
* Returns a substring using the Python slice semantics, meaning
* start and end can be negative
*/
static String substringSlice(String string, int start, int end) {
if (hasLength(string) == false) {
return string;
}
int length = string.length();
// handle first negative values
if (start < 0) {
start += length;
}
if (start < 0) {
start = 0;
}
if (end < 0) {
end += length;
}
if (end < 0) {
end = 0;
} else if (end > length) {
end = length;
}
if (start >= end) {
return org.elasticsearch.xpack.ql.util.StringUtils.EMPTY;
}
return Strings.substring(string, start, end);
}
}

View File

@ -0,0 +1,130 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.eql.expression.function.scalar.string;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.Expressions;
import org.elasticsearch.xpack.ql.expression.Expressions.ParamOrdinal;
import org.elasticsearch.xpack.ql.expression.FieldAttribute;
import org.elasticsearch.xpack.ql.expression.Literal;
import org.elasticsearch.xpack.ql.expression.function.OptionalArgument;
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.ql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.ql.expression.gen.script.Scripts;
import org.elasticsearch.xpack.ql.tree.NodeInfo;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.ql.type.DataType;
import org.elasticsearch.xpack.ql.type.DataTypes;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import static java.lang.String.format;
import static org.elasticsearch.xpack.eql.expression.function.scalar.string.SubstringFunctionProcessor.doProcess;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isInteger;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isStringAndExact;
import static org.elasticsearch.xpack.ql.expression.gen.script.ParamsBuilder.paramsBuilder;
/**
* EQL specific substring function - similar to the one in Python.
* Note this is different than the one in SQL.
*/
public class Substring extends ScalarFunction implements OptionalArgument {
private final Expression source, start, end;
public Substring(Source source, Expression src, Expression start, Expression end) {
super(source, Arrays.asList(src, start, end != null ? end : new Literal(source, null, DataTypes.NULL)));
this.source = src;
this.start = start;
this.end = arguments().get(2);
}
@Override
protected TypeResolution resolveType() {
if (!childrenResolved()) {
return new TypeResolution("Unresolved children");
}
TypeResolution sourceResolution = isStringAndExact(source, sourceText(), ParamOrdinal.FIRST);
if (sourceResolution.unresolved()) {
return sourceResolution;
}
TypeResolution startResolution = isInteger(start, sourceText(), ParamOrdinal.SECOND);
if (startResolution.unresolved()) {
return startResolution;
}
return isInteger(end, sourceText(), ParamOrdinal.THIRD);
}
@Override
protected Pipe makePipe() {
return new SubstringFunctionPipe(source(), this, Expressions.pipe(source), Expressions.pipe(start), Expressions.pipe(end));
}
@Override
public boolean foldable() {
return source.foldable() && start.foldable() && end.foldable();
}
@Override
public Object fold() {
return doProcess(source.fold(), start.fold(), end.fold());
}
@Override
protected NodeInfo<? extends Expression> info() {
return NodeInfo.create(this, Substring::new, source, start, end);
}
@Override
public ScriptTemplate asScript() {
ScriptTemplate sourceScript = asScript(source);
ScriptTemplate startScript = asScript(start);
ScriptTemplate endScript = asScript(end);
return asScriptFrom(sourceScript, startScript, endScript);
}
protected ScriptTemplate asScriptFrom(ScriptTemplate sourceScript, ScriptTemplate startScript, ScriptTemplate endScript) {
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{eql}.%s(%s,%s,%s)"),
"substring",
sourceScript.template(),
startScript.template(),
endScript.template()),
paramsBuilder()
.script(sourceScript.params())
.script(startScript.params())
.script(endScript.params())
.build(), dataType());
}
@Override
public ScriptTemplate scriptWithField(FieldAttribute field) {
return new ScriptTemplate(processScript(Scripts.DOC_VALUE),
paramsBuilder().variable(field.exactAttribute().name()).build(),
dataType());
}
@Override
public DataType dataType() {
return DataTypes.KEYWORD;
}
@Override
public Expression replaceChildren(List<Expression> newChildren) {
if (newChildren.size() != 3) {
throw new IllegalArgumentException("expected [3] children but received [" + newChildren.size() + "]");
}
return new Substring(source(), newChildren.get(0), newChildren.get(1), newChildren.get(2));
}
}

View File

@ -0,0 +1,111 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.eql.expression.function.scalar.string;
import org.elasticsearch.xpack.ql.execution.search.QlSourceBuilder;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.ql.tree.NodeInfo;
import org.elasticsearch.xpack.ql.tree.Source;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class SubstringFunctionPipe extends Pipe {
private final Pipe source, start, end;
public SubstringFunctionPipe(Source source, Expression expression, Pipe src, Pipe start, Pipe end) {
super(source, expression, Arrays.asList(src, start, end));
this.source = src;
this.start = start;
this.end = end;
}
@Override
public final Pipe replaceChildren(List<Pipe> newChildren) {
if (newChildren.size() != 3) {
throw new IllegalArgumentException("expected [3] children but received [" + newChildren.size() + "]");
}
return replaceChildren(newChildren.get(0), newChildren.get(1), newChildren.get(2));
}
@Override
public final Pipe resolveAttributes(AttributeResolver resolver) {
Pipe newSource = source.resolveAttributes(resolver);
Pipe newStart = start.resolveAttributes(resolver);
Pipe newEnd = end.resolveAttributes(resolver);
if (newSource == source && newStart == start && newEnd == end) {
return this;
}
return replaceChildren(newSource, newStart, newEnd);
}
@Override
public boolean supportedByAggsOnlyQuery() {
return source.supportedByAggsOnlyQuery() && start.supportedByAggsOnlyQuery() && end.supportedByAggsOnlyQuery();
}
@Override
public boolean resolved() {
return source.resolved() && start.resolved() && end.resolved();
}
protected Pipe replaceChildren(Pipe newSource, Pipe newStart, Pipe newEnd) {
return new SubstringFunctionPipe(source(), expression(), newSource, newStart, newEnd);
}
@Override
public final void collectFields(QlSourceBuilder sourceBuilder) {
source.collectFields(sourceBuilder);
start.collectFields(sourceBuilder);
end.collectFields(sourceBuilder);
}
@Override
protected NodeInfo<SubstringFunctionPipe> info() {
return NodeInfo.create(this, SubstringFunctionPipe::new, expression(), source, start, end);
}
@Override
public SubstringFunctionProcessor asProcessor() {
return new SubstringFunctionProcessor(source.asProcessor(), start.asProcessor(), end.asProcessor());
}
public Pipe src() {
return source;
}
public Pipe start() {
return start;
}
public Pipe end() {
return end;
}
@Override
public int hashCode() {
return Objects.hash(source, start, end);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
SubstringFunctionPipe other = (SubstringFunctionPipe) obj;
return Objects.equals(source, other.source)
&& Objects.equals(start, other.start)
&& Objects.equals(end, other.end);
}
}

View File

@ -0,0 +1,108 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.eql.expression.function.scalar.string;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.eql.EqlIllegalArgumentException;
import org.elasticsearch.xpack.ql.expression.gen.processor.Processor;
import java.io.IOException;
import java.util.Objects;
public class SubstringFunctionProcessor implements Processor {
public static final String NAME = "ssub";
private final Processor source, start, end;
public SubstringFunctionProcessor(Processor source, Processor start, Processor end) {
this.source = source;
this.start = start;
this.end = end;
}
public SubstringFunctionProcessor(StreamInput in) throws IOException {
source = in.readNamedWriteable(Processor.class);
start = in.readNamedWriteable(Processor.class);
end = in.readNamedWriteable(Processor.class);
}
@Override
public final void writeTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(source);
out.writeNamedWriteable(start);
out.writeNamedWriteable(end);
}
@Override
public Object process(Object input) {
return doProcess(source.process(input), start.process(input), end.process(input));
}
public static Object doProcess(Object source, Object start, Object end) {
if (source == null) {
return null;
}
if (!(source instanceof String || source instanceof Character)) {
throw new EqlIllegalArgumentException("A string/char is required; received [{}]", source);
}
if (start == null) {
return source;
}
if ((start instanceof Number) == false) {
throw new EqlIllegalArgumentException("A number is required; received [{}]", start);
}
if (end != null && (end instanceof Number) == false) {
throw new EqlIllegalArgumentException("A number is required; received [{}]", end);
}
String str = source.toString();
int startIndex = ((Number) start).intValue();
int endIndex = end == null ? str.length() : ((Number) end).intValue();
return StringUtils.substringSlice(str, startIndex, endIndex);
}
protected Processor source() {
return source;
}
protected Processor start() {
return start;
}
protected Processor end() {
return end;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
SubstringFunctionProcessor other = (SubstringFunctionProcessor) obj;
return Objects.equals(source(), other.source())
&& Objects.equals(start(), other.start())
&& Objects.equals(end(), other.end());
}
@Override
public int hashCode() {
return Objects.hash(source(), start(), end());
}
@Override
public String getWriteableName() {
return NAME;
}
}

View File

@ -0,0 +1,24 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.eql.expression.function.scalar.whitelist;
import org.elasticsearch.xpack.eql.expression.function.scalar.string.SubstringFunctionProcessor;
import org.elasticsearch.xpack.ql.expression.function.scalar.whitelist.InternalQlScriptUtils;
/*
* Whitelisted class for EQL scripts.
* Acts as a registry of the various static methods used <b>internally</b> by the scalar functions
* (to simplify the whitelist definition).
*/
public class InternalEqlScriptUtils extends InternalQlScriptUtils {
InternalEqlScriptUtils() {}
public static String substring(String s, Number start, Number end) {
return (String) SubstringFunctionProcessor.doProcess(s, start, end);
}
}

View File

@ -0,0 +1,41 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.eql.plugin;
import org.elasticsearch.painless.spi.PainlessExtension;
import org.elasticsearch.painless.spi.Whitelist;
import org.elasticsearch.painless.spi.WhitelistLoader;
import org.elasticsearch.script.AggregationScript;
import org.elasticsearch.script.BucketAggregationSelectorScript;
import org.elasticsearch.script.FieldScript;
import org.elasticsearch.script.FilterScript;
import org.elasticsearch.script.NumberSortScript;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.StringSortScript;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static java.util.Collections.singletonList;
public class EqlPainlessExtension implements PainlessExtension {
private static final Whitelist WHITELIST = WhitelistLoader.loadFromResourceFiles(EqlPainlessExtension.class, "eql_whitelist.txt");
@Override
public Map<ScriptContext<?>, List<Whitelist>> getContextWhitelists() {
Map<ScriptContext<?>, List<Whitelist>> whitelist = new HashMap<>();
List<Whitelist> list = singletonList(WHITELIST);
whitelist.put(FilterScript.CONTEXT, list);
whitelist.put(AggregationScript.CONTEXT, list);
whitelist.put(FieldScript.CONTEXT, list);
whitelist.put(NumberSortScript.CONTEXT, list);
whitelist.put(StringSortScript.CONTEXT, list);
whitelist.put(BucketAggregationSelectorScript.CONTEXT, list);
return whitelist;
}
}

View File

@ -0,0 +1 @@
org.elasticsearch.xpack.eql.plugin.EqlPainlessExtension

View File

@ -0,0 +1,59 @@
#
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License;
# you may not use this file except in compliance with the Elastic License.
#
# This file contains a whitelist for EQL specific utilities and classes available inside EQL scripting
#### Classes
class org.elasticsearch.xpack.ql.expression.function.scalar.whitelist.InternalQlScriptUtils {
#
# Utilities
#
def docValue(java.util.Map, String)
boolean nullSafeFilter(Boolean)
double nullSafeSortNumeric(Number)
String nullSafeSortString(Object)
#
# Comparison
#
Boolean eq(Object, Object)
Boolean nulleq(Object, Object)
Boolean neq(Object, Object)
Boolean lt(Object, Object)
Boolean lte(Object, Object)
Boolean gt(Object, Object)
Boolean gte(Object, Object)
Boolean in(Object, java.util.List)
#
# Logical
#
Boolean and(Boolean, Boolean)
Boolean or(Boolean, Boolean)
Boolean not(Boolean)
Boolean isNull(Object)
Boolean isNotNull(Object)
#
# Regex
#
Boolean regex(String, String)
#
# Math
#
Number neg(Number)
}
class org.elasticsearch.xpack.eql.expression.function.scalar.whitelist.InternalEqlScriptUtils {
#
# ASCII Functions
#
String substring(String, Number, Number)
}

View File

@ -131,8 +131,6 @@ public class VerifierTests extends ESTestCase {
// Test the known EQL functions that are not supported
public void testFunctionVerificationUnknown() {
assertEquals("1:26: Unknown function [substring]",
error("foo where user_domain == substring('abcdfeg', 0, 5)"));
assertEquals("1:25: Unknown function [endsWith]",
error("file where opcode=0 and endsWith(file_name, 'loREr.exe')"));
assertEquals("1:25: Unknown function [startsWith]",
@ -143,7 +141,7 @@ public class VerifierTests extends ESTestCase {
error("file where opcode=0 and indexOf(file_name, 'plore') == 2"));
assertEquals("1:15: Unknown function [add]",
error("process where add(serial_event_id, 0) == 1"));
assertEquals("1:15: Unknown function [subtract]",
assertEquals("1:15: Unknown function [subtract], did you mean [substring]?",
error("process where subtract(serial_event_id, -5) == 6"));
assertEquals("1:15: Unknown function [multiply]",
error("process where multiply(6, serial_event_id) == 30"));

View File

@ -0,0 +1,75 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.eql.expression.function.scalar.string;
import org.elasticsearch.test.ESTestCase;
import static org.elasticsearch.xpack.eql.expression.function.scalar.string.StringUtils.substringSlice;
public class StringUtilsTests extends ESTestCase {
public void testSubstringSlicePositive() {
String str = randomAlphaOfLength(10);
assertEquals(str.substring(1, 7), substringSlice(str, 1, 7));
}
public void testSubstringSliceNegative() {
String str = randomAlphaOfLength(10);
assertEquals(str.substring(5, 9), substringSlice(str, -5, -1));
}
public void testSubstringSliceNegativeOverLength() {
String str = randomAlphaOfLength(10);
assertEquals("", substringSlice(str, -15, -11));
}
public void testSubstringSlicePositiveOverLength() {
String str = randomAlphaOfLength(10);
assertEquals("", substringSlice(str, 11, 14));
}
public void testSubstringHigherEndThanStartNegative() {
String str = randomAlphaOfLength(10);
assertEquals("", substringSlice(str, -20, -11));
}
public void testSubstringRandomSlicePositive() {
String str = randomAlphaOfLength(10);
int start = randomInt(5);
int end = start + randomInt(3);
assertEquals(str.substring(start, end), substringSlice(str, start, end));
}
public void testSubstringRandomSliceNegative() {
String str = randomAlphaOfLength(10);
int end = 1 + randomInt(3);
int start = end + randomInt(5);
assertEquals(str.substring(10 - start, 10 - end), substringSlice(str, -start, -end));
}
public void testStartNegativeHigherThanLength() {
String str = randomAlphaOfLength(10);
int start = 10 + randomInt(10);
assertEquals(str.substring(0, 10 - 1), substringSlice(str, -start, -1));
}
public void testEndHigherThanLength() {
String str = randomAlphaOfLength(10);
int end = 10 + randomInt(10);
assertEquals(str, substringSlice(str, 0, end));
}
public void testSubstringRandomSliceSameStartEnd() {
String str = randomAlphaOfLength(10);
int start = randomInt();
assertEquals("", substringSlice(str, start, start));
}
public void testNullValue() {
assertNull(substringSlice(null, 0, 0));
}
}

View File

@ -64,3 +64,11 @@ process where process_path == "*\\red_ttp\\wininit.*" and opcode in (0,1,2,3)
"term":{"opcode":{"value":1
"term":{"opcode":{"value":2
"term":{"opcode":{"value":3
substringFunction
process where substring(file_name, -4) == '.exe'
"script":{"source":"InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.eq(
InternalEqlScriptUtils.substring(InternalQlScriptUtils.docValue(doc,params.v0),params.v1,params.v2),params.v3))",
"params":{"v0":"file_name.keyword","v1":-4,"v2":null,"v3":".exe"}

View File

@ -86,8 +86,12 @@ public class FunctionRegistry {
return def;
}
protected String normalize(String name) {
return name.toUpperCase(Locale.ROOT);
}
public String resolveAlias(String alias) {
String upperCase = alias.toUpperCase(Locale.ROOT);
String upperCase = normalize(alias);
return aliases.getOrDefault(upperCase, upperCase);
}
@ -102,7 +106,7 @@ public class FunctionRegistry {
public Collection<FunctionDefinition> listFunctions(String pattern) {
// It is worth double checking if we need this copy. These are immutable anyway.
Pattern p = Strings.hasText(pattern) ? Pattern.compile(pattern.toUpperCase(Locale.ROOT)) : null;
Pattern p = Strings.hasText(pattern) ? Pattern.compile(normalize(pattern)) : null;
return defs.entrySet().stream()
.filter(e -> p == null || p.matcher(e.getKey()).matches())
.map(e -> new FunctionDefinition(e.getKey(), emptyList(),

View File

@ -9,6 +9,7 @@ import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.ql.expression.gen.script.Scripts;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.ql.util.Check;
import java.util.Arrays;
import java.util.List;
@ -16,6 +17,7 @@ import java.util.Locale;
public abstract class BinaryScalarFunction extends ScalarFunction {
private static final int PKG_LENGTH = "org.elasticsearch.xpack.".length();
private final Expression left, right;
protected BinaryScalarFunction(Source source, Expression left, Expression right) {
@ -56,7 +58,10 @@ public abstract class BinaryScalarFunction extends ScalarFunction {
}
protected ScriptTemplate asScriptFrom(ScriptTemplate leftScript, ScriptTemplate rightScript) {
return Scripts.binaryMethod(scriptMethodName(), leftScript, rightScript, dataType());
String prefix = getClass().getPackage().getName().substring(PKG_LENGTH);
int index = prefix.indexOf('.');
Check.isTrue(index > 0, "invalid package {}", prefix);
return Scripts.binaryMethod("{" + prefix.substring(0, index) + "}", scriptMethodName(), leftScript, rightScript, dataType());
}
protected String scriptMethodName() {

View File

@ -123,7 +123,7 @@ public abstract class ScalarFunction extends Function {
}
protected ScriptTemplate scriptWithField(FieldAttribute field) {
return new ScriptTemplate(processScript("doc[{}].value"),
return new ScriptTemplate(processScript(Scripts.DOC_VALUE),
paramsBuilder().variable(field.name()).build(),
dataType());
}

View File

@ -9,12 +9,17 @@ package org.elasticsearch.xpack.ql.expression.function.scalar.whitelist;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.xpack.ql.expression.predicate.logical.BinaryLogicProcessor.BinaryLogicOperation;
import org.elasticsearch.xpack.ql.expression.predicate.logical.NotProcessor;
import org.elasticsearch.xpack.ql.expression.predicate.nulls.CheckNullProcessor.CheckNullOperation;
import org.elasticsearch.xpack.ql.expression.predicate.operator.arithmetic.UnaryArithmeticProcessor.UnaryArithmeticOperation;
import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.BinaryComparisonProcessor.BinaryComparisonOperation;
import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.InProcessor;
import org.elasticsearch.xpack.ql.expression.predicate.regex.RegexProcessor.RegexOperation;
import org.elasticsearch.xpack.ql.util.StringUtils;
import java.util.List;
import java.util.Map;
public abstract class InternalQlScriptUtils {
public class InternalQlScriptUtils {
//
// Utilities
@ -79,6 +84,10 @@ public abstract class InternalQlScriptUtils {
return BinaryComparisonOperation.GTE.apply(left, right);
}
public static Boolean in(Object value, List<Object> values) {
return InProcessor.apply(value, values);
}
public static Boolean and(Boolean left, Boolean right) {
return BinaryLogicOperation.AND.apply(left, right);
}
@ -90,4 +99,27 @@ public abstract class InternalQlScriptUtils {
public static Boolean not(Boolean expression) {
return NotProcessor.apply(expression);
}
public static Boolean isNull(Object expression) {
return CheckNullOperation.IS_NULL.apply(expression);
}
public static Boolean isNotNull(Object expression) {
return CheckNullOperation.IS_NOT_NULL.apply(expression);
}
//
// Regex
//
public static Boolean regex(String value, String pattern) {
// TODO: this needs to be improved to avoid creating the pattern on every call
return RegexOperation.match(value, pattern);
}
//
// Math
//
public static Number neg(Number value) {
return UnaryArithmeticOperation.NEGATE.apply(value);
}
}

View File

@ -25,16 +25,21 @@ import static org.elasticsearch.xpack.ql.expression.gen.script.ParamsBuilder.par
public final class Scripts {
public static final String DOC_VALUE = "doc[{}].value";
public static final String QL_SCRIPTS = "{ql}";
public static final String EQL_SCRIPTS = "{eql}";
public static final String SQL_SCRIPTS = "{sql}";
public static final String PARAM = "{}";
// FIXME: this needs to be either renamed (drop Sql) or find a pluggable approach (through ScriptWeaver)
public static final String INTERNAL_SCRIPT_UTILS = "InternalSqlScriptUtils";
public static final String INTERNAL_QL_SCRIPT_UTILS = "InternalQlScriptUtils";
public static final String INTERNAL_EQL_SCRIPT_UTILS = "InternalEqlScriptUtils";
public static final String INTERNAL_SQL_SCRIPT_UTILS = "InternalSqlScriptUtils";
private Scripts() {}
static final Map<Pattern, String> FORMATTING_PATTERNS = unmodifiableMap(Stream.of(
new SimpleEntry<>(DOC_VALUE, SQL_SCRIPTS + ".docValue(doc,{})"),
new SimpleEntry<>(SQL_SCRIPTS, INTERNAL_SCRIPT_UTILS),
new SimpleEntry<>(DOC_VALUE, QL_SCRIPTS + ".docValue(doc,{})"),
new SimpleEntry<>(QL_SCRIPTS, INTERNAL_QL_SCRIPT_UTILS),
new SimpleEntry<>(EQL_SCRIPTS, INTERNAL_EQL_SCRIPT_UTILS),
new SimpleEntry<>(SQL_SCRIPTS, INTERNAL_SQL_SCRIPT_UTILS),
new SimpleEntry<>(PARAM, "params.%s"))
.collect(toMap(e -> Pattern.compile(e.getKey(), Pattern.LITERAL), Map.Entry::getValue, (a, b) -> a, LinkedHashMap::new)));
@ -56,7 +61,7 @@ public final class Scripts {
public static ScriptTemplate nullSafeFilter(ScriptTemplate script) {
return new ScriptTemplate(formatTemplate(
format(Locale.ROOT, "{sql}.nullSafeFilter(%s)", script.template())),
format(Locale.ROOT, "{ql}.nullSafeFilter(%s)", script.template())),
script.params(),
DataTypes.BOOLEAN);
}
@ -64,22 +69,23 @@ public final class Scripts {
public static ScriptTemplate nullSafeSort(ScriptTemplate script) {
String methodName = script.outputType().isNumeric() ? "nullSafeSortNumeric" : "nullSafeSortString";
return new ScriptTemplate(formatTemplate(
format(Locale.ROOT, "{sql}.%s(%s)", methodName, script.template())),
format(Locale.ROOT, "{ql}.%s(%s)", methodName, script.template())),
script.params(),
script.outputType());
}
public static ScriptTemplate and(ScriptTemplate left, ScriptTemplate right) {
return binaryMethod("and", left, right, DataTypes.BOOLEAN);
return binaryMethod("{ql}", "and", left, right, DataTypes.BOOLEAN);
}
public static ScriptTemplate or(ScriptTemplate left, ScriptTemplate right) {
return binaryMethod("or", left, right, DataTypes.BOOLEAN);
return binaryMethod("{ql}", "or", left, right, DataTypes.BOOLEAN);
}
public static ScriptTemplate binaryMethod(String methodName, ScriptTemplate leftScript, ScriptTemplate rightScript,
public static ScriptTemplate binaryMethod(String prefix, String methodName, ScriptTemplate leftScript, ScriptTemplate rightScript,
DataType dataType) {
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s)"),
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("%s.%s(%s,%s)"),
formatTemplate(prefix),
methodName,
leftScript.template(),
rightScript.template()),

View File

@ -129,7 +129,7 @@ public class Range extends ScalarFunction {
ScriptTemplate upperScript = asScript(upper);
String template = formatTemplate(format(Locale.ROOT, "{sql}.and({sql}.%s(%s, %s), {sql}.%s(%s, %s))",
String template = formatTemplate(format(Locale.ROOT, "{ql}.and({ql}.%s(%s, %s), {ql}.%s(%s, %s))",
includeLower() ? "gte" : "gt",
valueScript.template(),
lowerScript.template(),

View File

@ -54,7 +54,7 @@ public class Not extends UnaryScalarFunction {
@Override
public String processScript(String script) {
return Scripts.formatTemplate(Scripts.SQL_SCRIPTS + ".not(" + script + ")");
return Scripts.formatTemplate(Scripts.QL_SCRIPTS + ".not(" + script + ")");
}
@Override

View File

@ -45,7 +45,7 @@ public class IsNotNull extends UnaryScalarFunction implements Negatable<UnarySca
@Override
public String processScript(String script) {
return Scripts.formatTemplate(Scripts.SQL_SCRIPTS + ".isNotNull(" + script + ")");
return Scripts.formatTemplate(Scripts.QL_SCRIPTS + ".isNotNull(" + script + ")");
}
@Override

View File

@ -45,7 +45,7 @@ public class IsNull extends UnaryScalarFunction implements Negatable<UnaryScalar
@Override
public String processScript(String script) {
return Scripts.formatTemplate(Scripts.SQL_SCRIPTS + ".isNull(" + script + ")");
return Scripts.formatTemplate(Scripts.QL_SCRIPTS + ".isNull(" + script + ")");
}
@Override

View File

@ -53,7 +53,7 @@ public class Neg extends UnaryScalarFunction {
@Override
public String processScript(String script) {
return Scripts.formatTemplate(Scripts.SQL_SCRIPTS + ".neg(" + script + ")");
return Scripts.formatTemplate(Scripts.QL_SCRIPTS + ".neg(" + script + ")");
}
@Override

View File

@ -95,7 +95,7 @@ public class In extends ScalarFunction {
List<Object> values = new ArrayList<>(new LinkedHashSet<>(foldAndConvertListOfValues(list, value.dataType())));
return new ScriptTemplate(
formatTemplate(format("{sql}.","in({}, {})", leftScript.template())),
formatTemplate(format("{ql}.","in({}, {})", leftScript.template())),
paramsBuilder()
.script(leftScript.params())
.variable(values)

View File

@ -6,6 +6,7 @@
package org.elasticsearch.xpack.sql.qa.rest;
import com.fasterxml.jackson.core.io.JsonStringEncoder;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
@ -721,7 +722,7 @@ public abstract class RestSqlTestCase extends BaseRestSqlTestCase implements Err
@SuppressWarnings("unchecked")
Map<String, Object> termsScript = (Map<String, Object>) terms.get("script");
assertEquals(3, termsScript.size());
assertEquals("InternalSqlScriptUtils.abs(InternalSqlScriptUtils.docValue(doc,params.v0))", termsScript.get("source"));
assertEquals("InternalSqlScriptUtils.abs(InternalQlScriptUtils.docValue(doc,params.v0))", termsScript.get("source"));
assertEquals("painless", termsScript.get("lang"));
@SuppressWarnings("unchecked")
@ -770,7 +771,7 @@ public abstract class RestSqlTestCase extends BaseRestSqlTestCase implements Err
@SuppressWarnings("unchecked")
Map<String, Object> filterScript = (Map<String, Object>) bucketSelector.get("script");
assertEquals(3, filterScript.size());
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(params.a0,params.v0))",
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a0,params.v0))",
filterScript.get("source"));
assertEquals("painless", filterScript.get("lang"));
@SuppressWarnings("unchecked")

View File

@ -8,7 +8,6 @@ package org.elasticsearch.xpack.sql.expression.function;
import org.elasticsearch.xpack.ql.expression.function.FunctionDefinition;
import org.elasticsearch.xpack.ql.expression.function.FunctionRegistry;
import org.elasticsearch.xpack.ql.expression.function.aggregate.Count;
import org.elasticsearch.xpack.ql.expression.predicate.operator.arithmetic.Mod;
import org.elasticsearch.xpack.sql.expression.function.aggregate.Avg;
import org.elasticsearch.xpack.sql.expression.function.aggregate.First;
import org.elasticsearch.xpack.sql.expression.function.aggregate.Kurtosis;
@ -111,6 +110,7 @@ import org.elasticsearch.xpack.sql.expression.predicate.conditional.IfNull;
import org.elasticsearch.xpack.sql.expression.predicate.conditional.Iif;
import org.elasticsearch.xpack.sql.expression.predicate.conditional.Least;
import org.elasticsearch.xpack.sql.expression.predicate.conditional.NullIf;
import org.elasticsearch.xpack.sql.expression.predicate.operator.arithmetic.Mod;
public class SqlFunctionRegistry extends FunctionRegistry {

View File

@ -10,6 +10,7 @@ import org.elasticsearch.xpack.ql.expression.Expressions.ParamOrdinal;
import org.elasticsearch.xpack.ql.expression.FieldAttribute;
import org.elasticsearch.xpack.ql.expression.function.scalar.BinaryScalarFunction;
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.ql.expression.gen.script.Scripts;
import org.elasticsearch.xpack.ql.tree.Source;
import java.util.Locale;
@ -66,7 +67,7 @@ public abstract class BinaryStringFunction<T,R> extends BinaryScalarFunction {
@Override
public ScriptTemplate scriptWithField(FieldAttribute field) {
return new ScriptTemplate(processScript("doc[{}].value"),
return new ScriptTemplate(processScript(Scripts.DOC_VALUE),
paramsBuilder().variable(field.exactAttribute().name()).build(),
dataType());
}

View File

@ -13,6 +13,7 @@ import org.elasticsearch.xpack.ql.expression.Nullability;
import org.elasticsearch.xpack.ql.expression.function.scalar.BinaryScalarFunction;
import org.elasticsearch.xpack.ql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.ql.expression.gen.script.Scripts;
import org.elasticsearch.xpack.ql.tree.NodeInfo;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.ql.type.DataType;
@ -79,7 +80,7 @@ public class Concat extends BinaryScalarFunction {
@Override
public ScriptTemplate scriptWithField(FieldAttribute field) {
return new ScriptTemplate(processScript("doc[{}].value"),
return new ScriptTemplate(processScript(Scripts.DOC_VALUE),
paramsBuilder().variable(field.exactAttribute().name()).build(),
dataType());
}

View File

@ -12,6 +12,7 @@ import org.elasticsearch.xpack.ql.expression.FieldAttribute;
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.ql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.ql.expression.gen.script.Scripts;
import org.elasticsearch.xpack.ql.tree.NodeInfo;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.ql.type.DataType;
@ -121,7 +122,7 @@ public class Insert extends ScalarFunction {
@Override
public ScriptTemplate scriptWithField(FieldAttribute field) {
return new ScriptTemplate(processScript("doc[{}].value"),
return new ScriptTemplate(processScript(Scripts.DOC_VALUE),
paramsBuilder().variable(field.exactAttribute().name()).build(),
dataType());
}

View File

@ -13,6 +13,7 @@ import org.elasticsearch.xpack.ql.expression.function.OptionalArgument;
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.ql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.ql.expression.gen.script.Scripts;
import org.elasticsearch.xpack.ql.tree.NodeInfo;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.ql.type.DataType;
@ -123,7 +124,7 @@ public class Locate extends ScalarFunction implements OptionalArgument {
@Override
public ScriptTemplate scriptWithField(FieldAttribute field) {
return new ScriptTemplate(processScript("doc[{}].value"),
return new ScriptTemplate(processScript(Scripts.DOC_VALUE),
paramsBuilder().variable(field.exactAttribute().name()).build(),
dataType());
}

View File

@ -12,6 +12,7 @@ import org.elasticsearch.xpack.ql.expression.FieldAttribute;
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.ql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.ql.expression.gen.script.Scripts;
import org.elasticsearch.xpack.ql.tree.NodeInfo;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.ql.type.DataType;
@ -108,7 +109,7 @@ public class Replace extends ScalarFunction {
@Override
public ScriptTemplate scriptWithField(FieldAttribute field) {
return new ScriptTemplate(processScript("doc[{}].value"),
return new ScriptTemplate(processScript(Scripts.DOC_VALUE),
paramsBuilder().variable(field.exactAttribute().name()).build(),
dataType());
}

View File

@ -5,6 +5,8 @@
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import static org.elasticsearch.common.Strings.hasLength;
abstract class StringFunctionUtils {
/**
@ -20,11 +22,13 @@ abstract class StringFunctionUtils {
return s;
}
if (start < 0)
if (start < 0) {
start = 0;
}
if (start + 1 > s.length() || length < 0)
if (start + 1 > s.length() || length < 0) {
return "";
}
return (start + length > s.length()) ? s.substring(start) : s.substring(start, start + length);
}
@ -66,10 +70,4 @@ abstract class StringFunctionUtils {
}
return sb.toString();
}
private static boolean hasLength(String s) {
return (s != null && s.length() > 0);
}
}

View File

@ -11,6 +11,7 @@ import org.elasticsearch.xpack.ql.expression.FieldAttribute;
import org.elasticsearch.xpack.ql.expression.function.scalar.UnaryScalarFunction;
import org.elasticsearch.xpack.ql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.ql.expression.gen.script.Scripts;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.ql.util.StringUtils;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.StringProcessor.StringOperation;
@ -56,7 +57,7 @@ public abstract class UnaryStringFunction extends UnaryScalarFunction {
@Override
public ScriptTemplate scriptWithField(FieldAttribute field) {
//TODO change this to use _source instead of the exact form (aka field.keyword for text fields)
return new ScriptTemplate(processScript("doc[{}].value"),
return new ScriptTemplate(processScript(Scripts.DOC_VALUE),
paramsBuilder().variable(field.exactAttribute().name()).build(),
dataType());
}

View File

@ -11,6 +11,7 @@ import org.elasticsearch.xpack.ql.expression.FieldAttribute;
import org.elasticsearch.xpack.ql.expression.function.scalar.UnaryScalarFunction;
import org.elasticsearch.xpack.ql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.ql.expression.gen.script.Scripts;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.StringProcessor.StringOperation;
@ -57,7 +58,7 @@ public abstract class UnaryStringIntFunction extends UnaryScalarFunction {
@Override
public ScriptTemplate scriptWithField(FieldAttribute field) {
return new ScriptTemplate(processScript("doc[{}].value"),
return new ScriptTemplate(processScript(Scripts.DOC_VALUE),
paramsBuilder().variable(field.name()).build(),
dataType());
}

View File

@ -9,10 +9,6 @@ import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.script.JodaCompatibleZonedDateTime;
import org.elasticsearch.xpack.ql.expression.function.scalar.whitelist.InternalQlScriptUtils;
import org.elasticsearch.xpack.ql.expression.predicate.nulls.CheckNullProcessor.CheckNullOperation;
import org.elasticsearch.xpack.ql.expression.predicate.operator.arithmetic.UnaryArithmeticProcessor.UnaryArithmeticOperation;
import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.InProcessor;
import org.elasticsearch.xpack.ql.expression.predicate.regex.RegexProcessor.RegexOperation;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateAddProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateDiffProcessor;
@ -66,23 +62,6 @@ public class InternalSqlScriptUtils extends InternalQlScriptUtils {
InternalSqlScriptUtils() {}
//
// Logical
//
public static Boolean isNull(Object expression) {
return CheckNullOperation.IS_NULL.apply(expression);
}
public static Boolean isNotNull(Object expression) {
return CheckNullOperation.IS_NOT_NULL.apply(expression);
}
public static Boolean in(Object value, List<Object> values) {
return InProcessor.apply(value, values);
}
//
// Conditional
//
@ -106,14 +85,6 @@ public class InternalSqlScriptUtils extends InternalQlScriptUtils {
return NullIfProcessor.apply(left, right);
}
//
// Regex
//
public static Boolean regex(String value, String pattern) {
// TODO: this needs to be improved to avoid creating the pattern on every call
return RegexOperation.match(value, pattern);
}
//
// Math
//
@ -133,10 +104,6 @@ public class InternalSqlScriptUtils extends InternalQlScriptUtils {
return SqlBinaryArithmeticOperation.MUL.apply(left, right);
}
public static Number neg(Number value) {
return UnaryArithmeticOperation.NEGATE.apply(value);
}
public static Object sub(Object left, Object right) {
return SqlBinaryArithmeticOperation.SUB.apply(left, right);
}

View File

@ -21,10 +21,6 @@ class java.time.OffsetTime {
}
class org.elasticsearch.xpack.ql.expression.function.scalar.whitelist.InternalQlScriptUtils {
}
class org.elasticsearch.xpack.sql.expression.function.scalar.whitelist.InternalSqlScriptUtils {
#
# Utilities
#
@ -54,6 +50,19 @@ class org.elasticsearch.xpack.sql.expression.function.scalar.whitelist.InternalS
Boolean isNull(Object)
Boolean isNotNull(Object)
#
# Regex
#
Boolean regex(String, String)
#
# Math
#
Number neg(Number)
}
class org.elasticsearch.xpack.sql.expression.function.scalar.whitelist.InternalSqlScriptUtils {
#
# Conditional
#

View File

@ -219,7 +219,8 @@ public class QueryFolderTests extends ESTestCase {
assertEquals(EsQueryExec.class, p.getClass());
EsQueryExec ee = (EsQueryExec) p;
assertTrue(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", "").contains(
"\"script\":{\"source\":\"InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(params.a0,params.v0))\"," +
"\"script\":{\"source\":\"InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a0,params.v0))\","
+
"\"lang\":\"painless\",\"params\":{\"v0\":10}},"));
assertEquals(2, ee.output().size());
assertThat(ee.output().get(0).toString(), startsWith("test.keyword{f}#"));
@ -292,7 +293,7 @@ public class QueryFolderTests extends ESTestCase {
EsQueryExec ee = (EsQueryExec) p;
assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""),
endsWith("{\"script\":{" +
"\"source\":\"InternalSqlScriptUtils.gt(InternalSqlScriptUtils.docValue(doc,params.v0),params.v1)\"," +
"\"source\":\"InternalQlScriptUtils.gt(InternalQlScriptUtils.docValue(doc,params.v0),params.v1)\"," +
"\"lang\":\"painless\",\"params\":{\"v0\":\"int\",\"v1\":10}},\"missing_bucket\":true," +
"\"value_type\":\"boolean\",\"order\":\"asc\"}}}]}}}"));
assertEquals(2, ee.output().size());
@ -306,7 +307,7 @@ public class QueryFolderTests extends ESTestCase {
EsQueryExec ee = (EsQueryExec) p;
assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""),
endsWith("{\"script\":{" +
"\"source\":\"InternalSqlScriptUtils.add(InternalSqlScriptUtils.docValue(doc,params.v0),params.v1)\"," +
"\"source\":\"InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0),params.v1)\"," +
"\"lang\":\"painless\",\"params\":{\"v0\":\"int\",\"v1\":10}},\"missing_bucket\":true," +
"\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}"));
assertEquals(2, ee.output().size());
@ -320,7 +321,7 @@ public class QueryFolderTests extends ESTestCase {
EsQueryExec ee = (EsQueryExec) p;
assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""),
endsWith("{\"script\":{" +
"\"source\":\"InternalSqlScriptUtils.sin(InternalSqlScriptUtils.docValue(doc,params.v0))\"," +
"\"source\":\"InternalSqlScriptUtils.sin(InternalQlScriptUtils.docValue(doc,params.v0))\"," +
"\"lang\":\"painless\",\"params\":{\"v0\":\"int\"}},\"missing_bucket\":true," +
"\"value_type\":\"double\",\"order\":\"asc\"}}}]}}}"));
assertEquals(2, ee.output().size());
@ -334,7 +335,7 @@ public class QueryFolderTests extends ESTestCase {
EsQueryExec ee = (EsQueryExec) p;
assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""),
endsWith("{\"script\":{" +
"\"source\":\"InternalSqlScriptUtils.lcase(InternalSqlScriptUtils.docValue(doc,params.v0))\"," +
"\"source\":\"InternalSqlScriptUtils.lcase(InternalQlScriptUtils.docValue(doc,params.v0))\"," +
"\"lang\":\"painless\",\"params\":{\"v0\":\"keyword\"}},\"missing_bucket\":true," +
"\"value_type\":\"string\",\"order\":\"asc\"}}}]}}}"));
assertEquals(2, ee.output().size());
@ -348,7 +349,7 @@ public class QueryFolderTests extends ESTestCase {
EsQueryExec ee = (EsQueryExec) p;
assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""),
endsWith("{\"script\":{\"source\":\"InternalSqlScriptUtils.cast(" +
"InternalSqlScriptUtils.docValue(doc,params.v0),params.v1)\"," +
"InternalQlScriptUtils.docValue(doc,params.v0),params.v1)\"," +
"\"lang\":\"painless\",\"params\":{\"v0\":\"keyword\",\"v1\":\"IP\"}}," +
"\"missing_bucket\":true,\"value_type\":\"ip\",\"order\":\"asc\"}}}]}}}"));
assertEquals(2, ee.output().size());
@ -362,7 +363,7 @@ public class QueryFolderTests extends ESTestCase {
EsQueryExec ee = (EsQueryExec) p;
assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""),
endsWith("{\"script\":{" +
"\"source\":\"InternalSqlScriptUtils.add(InternalSqlScriptUtils.docValue(doc,params.v0)," +
"\"source\":\"InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0)," +
"InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2))\",\"lang\":\"painless\",\"params\":{" +
"\"v0\":\"date\",\"v1\":\"P1Y2M\",\"v2\":\"INTERVAL_YEAR_TO_MONTH\"}},\"missing_bucket\":true," +
"\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}"));

View File

@ -389,8 +389,8 @@ public class QueryTranslatorTests extends ESTestCase {
assertNull(translation.aggFilter);
assertTrue(translation.query instanceof ScriptQuery);
ScriptQuery sc = (ScriptQuery) translation.query;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(InternalSqlScriptUtils.dateAdd(" +
"params.v0,InternalSqlScriptUtils.docValue(doc,params.v1),InternalSqlScriptUtils.docValue(doc,params.v2)," +
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(InternalSqlScriptUtils.dateAdd(" +
"params.v0,InternalQlScriptUtils.docValue(doc,params.v1),InternalQlScriptUtils.docValue(doc,params.v2)," +
"params.v3),InternalSqlScriptUtils.asDateTime(params.v4)))",
sc.script().toString());
assertEquals("[{v=quarter}, {v=int}, {v=date}, {v=Z}, {v=2018-09-04T00:00:00.000Z}]", sc.script().params().toString());
@ -406,8 +406,8 @@ public class QueryTranslatorTests extends ESTestCase {
assertNull(translation.aggFilter);
assertTrue(translation.query instanceof ScriptQuery);
ScriptQuery sc = (ScriptQuery) translation.query;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(InternalSqlScriptUtils.dateDiff(" +
"params.v0,InternalSqlScriptUtils.docValue(doc,params.v1),InternalSqlScriptUtils.docValue(doc,params.v2)," +
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(InternalSqlScriptUtils.dateDiff(" +
"params.v0,InternalQlScriptUtils.docValue(doc,params.v1),InternalQlScriptUtils.docValue(doc,params.v2)," +
"params.v3),InternalSqlScriptUtils.asDateTime(params.v4)))",
sc.script().toString());
assertEquals("[{v=week}, {v=date}, {v=date}, {v=Z}, {v=2018-09-04T00:00:00.000Z}]", sc.script().params().toString());
@ -423,8 +423,8 @@ public class QueryTranslatorTests extends ESTestCase {
assertNull(translation.aggFilter);
assertTrue(translation.query instanceof ScriptQuery);
ScriptQuery sc = (ScriptQuery) translation.query;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(InternalSqlScriptUtils.dateTrunc(" +
"params.v0,InternalSqlScriptUtils.docValue(doc,params.v1),params.v2),InternalSqlScriptUtils.asDateTime(params.v3)))",
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(InternalSqlScriptUtils.dateTrunc(" +
"params.v0,InternalQlScriptUtils.docValue(doc,params.v1),params.v2),InternalSqlScriptUtils.asDateTime(params.v3)))",
sc.script().toString());
assertEquals("[{v=month}, {v=date}, {v=Z}, {v=2018-09-04T00:00:00.000Z}]", sc.script().params().toString());
}
@ -439,8 +439,8 @@ public class QueryTranslatorTests extends ESTestCase {
assertNull(translation.aggFilter);
assertTrue(translation.query instanceof ScriptQuery);
ScriptQuery sc = (ScriptQuery) translation.query;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(InternalSqlScriptUtils.datePart(" +
"params.v0,InternalSqlScriptUtils.docValue(doc,params.v1),params.v2),InternalSqlScriptUtils.asDateTime(params.v3)))",
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(InternalSqlScriptUtils.datePart(" +
"params.v0,InternalQlScriptUtils.docValue(doc,params.v1),params.v2),InternalSqlScriptUtils.asDateTime(params.v3)))",
sc.script().toString());
assertEquals("[{v=month}, {v=date}, {v=Z}, {v=2018-09-04T00:00:00.000Z}]", sc.script().params().toString());
}
@ -556,8 +556,8 @@ public class QueryTranslatorTests extends ESTestCase {
GroupingContext groupingContext = QueryFolder.FoldAggregate.groupBy(((Aggregate) p).groupings());
assertNotNull(groupingContext);
ScriptTemplate scriptTemplate = groupingContext.tail.script();
assertEquals("InternalSqlScriptUtils.caseFunction([InternalSqlScriptUtils.regex(InternalSqlScriptUtils.docValue(" +
"doc,params.v0),params.v1),params.v2,InternalSqlScriptUtils.regex(InternalSqlScriptUtils.docValue(" +
assertEquals("InternalSqlScriptUtils.caseFunction([InternalSqlScriptUtils.regex(InternalQlScriptUtils.docValue("
+ "doc,params.v0),params.v1),params.v2,InternalSqlScriptUtils.regex(InternalQlScriptUtils.docValue(" +
"doc,params.v3),params.v4),params.v5,params.v6])",
scriptTemplate.toString());
assertEquals("[{v=keyword}, {v=^.*foo.*$}, {v=1}, {v=keyword}, {v=.*bar.*}, {v=2}, {v=3}]",
@ -573,9 +573,9 @@ public class QueryTranslatorTests extends ESTestCase {
QueryTranslation translation = QueryTranslator.toQuery(condition, false);
assertTrue(translation.query instanceof ScriptQuery);
ScriptQuery sc = (ScriptQuery) translation.query;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.not(" +
"InternalSqlScriptUtils.eq(InternalSqlScriptUtils.position(" +
"params.v0,InternalSqlScriptUtils.docValue(doc,params.v1)),params.v2)))",
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.not(" +
"InternalQlScriptUtils.eq(InternalSqlScriptUtils.position(" +
"params.v0,InternalQlScriptUtils.docValue(doc,params.v1)),params.v2)))",
sc.script().toString());
assertEquals("[{v=x}, {v=keyword}, {v=0}]", sc.script().params().toString());
}
@ -604,8 +604,8 @@ public class QueryTranslatorTests extends ESTestCase {
QueryTranslation translation = QueryTranslator.toQuery(condition, false);
assertTrue(translation.query instanceof ScriptQuery);
ScriptQuery sc = (ScriptQuery) translation.query;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.isNull(" +
"InternalSqlScriptUtils.position(params.v0,InternalSqlScriptUtils.docValue(doc,params.v1))))",
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.isNull(" +
"InternalSqlScriptUtils.position(params.v0,InternalQlScriptUtils.docValue(doc,params.v1))))",
sc.script().toString());
assertEquals("[{v=x}, {v=keyword}]", sc.script().params().toString());
}
@ -632,8 +632,8 @@ public class QueryTranslatorTests extends ESTestCase {
QueryTranslation translation = QueryTranslator.toQuery(condition, false);
assertTrue(translation.query instanceof ScriptQuery);
ScriptQuery sc = (ScriptQuery) translation.query;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.isNotNull(" +
"InternalSqlScriptUtils.position(params.v0,InternalSqlScriptUtils.docValue(doc,params.v1))))",
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.isNotNull(" +
"InternalSqlScriptUtils.position(params.v0,InternalQlScriptUtils.docValue(doc,params.v1))))",
sc.script().toString());
assertEquals("[{v=x}, {v=keyword}]", sc.script().params().toString());
}
@ -646,7 +646,8 @@ public class QueryTranslatorTests extends ESTestCase {
QueryTranslation translation = QueryTranslator.toQuery(condition, true);
assertNull(translation.query);
AggFilter aggFilter = translation.aggFilter;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.isNull(params.a0))",
assertEquals(
"InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.isNull(params.a0))",
aggFilter.scriptTemplate().toString());
assertThat(aggFilter.scriptTemplate().params().toString(), startsWith("[{a=max(int)"));
}
@ -659,7 +660,8 @@ public class QueryTranslatorTests extends ESTestCase {
QueryTranslation translation = QueryTranslator.toQuery(condition, true);
assertNull(translation.query);
AggFilter aggFilter = translation.aggFilter;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.isNotNull(params.a0))",
assertEquals(
"InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.isNotNull(params.a0))",
aggFilter.scriptTemplate().toString());
assertThat(aggFilter.scriptTemplate().params().toString(), startsWith("[{a=max(int)"));
}
@ -676,17 +678,17 @@ public class QueryTranslatorTests extends ESTestCase {
String aggName = aggBuilder.getSubAggregations().iterator().next().getName();
String havingName = aggBuilder.getPipelineAggregations().iterator().next().getName();
assertThat(aggBuilder.toString(), containsString("{\"terms\":{\"script\":{\"source\":\"" +
"InternalSqlScriptUtils.coalesce([InternalSqlScriptUtils.docValue(doc,params.v0)])\",\"lang\":\"painless\"" +
"InternalSqlScriptUtils.coalesce([InternalQlScriptUtils.docValue(doc,params.v0)])\",\"lang\":\"painless\"" +
",\"params\":{\"v0\":\"int\"}},\"missing_bucket\":true,\"value_type\":\"long\",\"order\":\"asc\"}}}]}," +
"\"aggregations\":{\"" + aggName + "\":{\"max\":{\"field\":\"date\"}},\"" + havingName + "\":" +
"{\"bucket_selector\":{\"buckets_path\":{\"a0\":\"" + aggName + "\"},\"script\":{\"source\":\"" +
"InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(InternalSqlScriptUtils.coalesce(" +
"InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(InternalSqlScriptUtils.coalesce(" +
"[params.a0]),InternalSqlScriptUtils.asDateTime(params.v0)))\",\"lang\":\"painless\",\"params\":" +
"{\"v0\":\"2020-01-01T00:00:00.000Z\"}}"));
assertTrue(esQExec.queryContainer().query() instanceof ScriptQuery);
ScriptQuery sq = (ScriptQuery) esQExec.queryContainer().query();
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(" +
"InternalSqlScriptUtils.coalesce([InternalSqlScriptUtils.docValue(doc,params.v0)]),params.v1))",
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(" +
"InternalSqlScriptUtils.coalesce([InternalQlScriptUtils.docValue(doc,params.v0)]),params.v1))",
sq.script().toString());
assertEquals("[{v=int}, {v=10}]", sq.script().params().toString());
}
@ -743,8 +745,8 @@ public class QueryTranslatorTests extends ESTestCase {
assertNull(translation.aggFilter);
assertTrue(translation.query instanceof ScriptQuery);
ScriptQuery sc = (ScriptQuery) translation.query;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.in(" +
"InternalSqlScriptUtils.power(InternalSqlScriptUtils.docValue(doc,params.v0),params.v1), params.v2))",
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.in(" +
"InternalSqlScriptUtils.power(InternalQlScriptUtils.docValue(doc,params.v0),params.v1), params.v2))",
sc.script().toString());
assertEquals("[{v=int}, {v=2}, {v=[10.0, null, 20.0]}]", sc.script().params().toString());
}
@ -757,7 +759,7 @@ public class QueryTranslatorTests extends ESTestCase {
QueryTranslation translation = QueryTranslator.toQuery(condition, true);
assertNull(translation.query);
AggFilter aggFilter = translation.aggFilter;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.in(params.a0, params.v0))",
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.in(params.a0, params.v0))",
aggFilter.scriptTemplate().toString());
assertThat(aggFilter.scriptTemplate().params().toString(), startsWith("[{a=max(int)"));
assertThat(aggFilter.scriptTemplate().params().toString(), endsWith(", {v=[10, 20]}]"));
@ -771,7 +773,7 @@ public class QueryTranslatorTests extends ESTestCase {
QueryTranslation translation = QueryTranslator.toQuery(condition, true);
assertNull(translation.query);
AggFilter aggFilter = translation.aggFilter;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.in(params.a0, params.v0))",
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.in(params.a0, params.v0))",
aggFilter.scriptTemplate().toString());
assertThat(aggFilter.scriptTemplate().params().toString(), startsWith("[{a=max(int)"));
assertThat(aggFilter.scriptTemplate().params().toString(), endsWith(", {v=[10]}]"));
@ -786,7 +788,7 @@ public class QueryTranslatorTests extends ESTestCase {
QueryTranslation translation = QueryTranslator.toQuery(condition, true);
assertNull(translation.query);
AggFilter aggFilter = translation.aggFilter;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.in(params.a0, params.v0))",
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.in(params.a0, params.v0))",
aggFilter.scriptTemplate().toString());
assertThat(aggFilter.scriptTemplate().params().toString(), startsWith("[{a=max(int)"));
assertThat(aggFilter.scriptTemplate().params().toString(), endsWith(", {v=[10, null, 20, 30]}]"));
@ -804,7 +806,7 @@ public class QueryTranslatorTests extends ESTestCase {
QueryTranslation translation = QueryTranslator.toQuery(condition, true);
assertNull(translation.query);
AggFilter aggFilter = translation.aggFilter;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(InternalSqlScriptUtils." +
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(InternalSqlScriptUtils." +
operation.name().toLowerCase(Locale.ROOT) + "(params.a0),params.v0))",
aggFilter.scriptTemplate().toString());
assertThat(aggFilter.scriptTemplate().params().toString(), startsWith("[{a=max(int)"));
@ -826,7 +828,8 @@ public class QueryTranslatorTests extends ESTestCase {
GroupingContext groupingContext = QueryFolder.FoldAggregate.groupBy(((Aggregate) p).groupings());
assertNotNull(groupingContext);
ScriptTemplate scriptTemplate = groupingContext.tail.script();
assertEquals("InternalSqlScriptUtils.round(InternalSqlScriptUtils.dateTimeChrono(InternalSqlScriptUtils.docValue(doc,params.v0), "
assertEquals(
"InternalSqlScriptUtils.round(InternalSqlScriptUtils.dateTimeChrono(InternalQlScriptUtils.docValue(doc,params.v0), "
+ "params.v1, params.v2),params.v3)",
scriptTemplate.toString());
assertEquals("[{v=date}, {v=Z}, {v=YEAR}, {v=null}]", scriptTemplate.params().toString());
@ -850,7 +853,8 @@ public class QueryTranslatorTests extends ESTestCase {
GroupingContext groupingContext = QueryFolder.FoldAggregate.groupBy(((Aggregate) p).groupings());
assertNotNull(groupingContext);
ScriptTemplate scriptTemplate = groupingContext.tail.script();
assertEquals("InternalSqlScriptUtils.round(InternalSqlScriptUtils.dateTimeChrono(InternalSqlScriptUtils.docValue(doc,params.v0), "
assertEquals(
"InternalSqlScriptUtils.round(InternalSqlScriptUtils.dateTimeChrono(InternalQlScriptUtils.docValue(doc,params.v0), "
+ "params.v1, params.v2),params.v3)",
scriptTemplate.toString());
assertEquals("[{v=date}, {v=Z}, {v=YEAR}, {v=-2}]", scriptTemplate.params().toString());
@ -864,7 +868,7 @@ public class QueryTranslatorTests extends ESTestCase {
QueryTranslation translation = QueryTranslator.toQuery(condition, true);
assertNull(translation.query);
AggFilter aggFilter = translation.aggFilter;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(InternalSqlScriptUtils.abs" +
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(InternalSqlScriptUtils.abs" +
"(params.a0),params.v0))",
aggFilter.scriptTemplate().toString());
assertThat(aggFilter.scriptTemplate().params().toString(), startsWith("[{a=MAX(int)"));
@ -880,7 +884,7 @@ public class QueryTranslatorTests extends ESTestCase {
QueryTranslation translation = QueryTranslator.toQuery(condition, true);
assertNull(translation.query);
AggFilter aggFilter = translation.aggFilter;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.eq(" +
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.eq(" +
"InternalSqlScriptUtils.stAswkt(InternalSqlScriptUtils.geoDocValue(doc,params.v0))," +
"params.v1)" +
")",
@ -897,9 +901,9 @@ public class QueryTranslatorTests extends ESTestCase {
QueryTranslation translation = QueryTranslator.toQuery(condition, true);
assertNull(translation.query);
AggFilter aggFilter = translation.aggFilter;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(" +
"InternalSqlScriptUtils.eq(InternalSqlScriptUtils.stWktToSql(" +
"InternalSqlScriptUtils.docValue(doc,params.v0)),InternalSqlScriptUtils.stWktToSql(params.v1)))",
assertEquals("InternalQlScriptUtils.nullSafeFilter(" +
"InternalQlScriptUtils.eq(InternalSqlScriptUtils.stWktToSql(" +
"InternalQlScriptUtils.docValue(doc,params.v0)),InternalSqlScriptUtils.stWktToSql(params.v1)))",
aggFilter.scriptTemplate().toString());
assertEquals("[{v=keyword}, {v=POINT (10.0 20.0)}]", aggFilter.scriptTemplate().params().toString());
}
@ -916,8 +920,8 @@ public class QueryTranslatorTests extends ESTestCase {
assertNull(translation.aggFilter);
assertTrue(translation.query instanceof ScriptQuery);
ScriptQuery sc = (ScriptQuery) translation.query;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(" +
"InternalSqlScriptUtils." + operatorFunction + "(" +
assertEquals("InternalQlScriptUtils.nullSafeFilter(" +
"InternalQlScriptUtils." + operatorFunction + "(" +
"InternalSqlScriptUtils.stDistance(" +
"InternalSqlScriptUtils.geoDocValue(doc,params.v0),InternalSqlScriptUtils.stWktToSql(params.v1)),params.v2))",
sc.script().toString());
@ -952,7 +956,7 @@ public class QueryTranslatorTests extends ESTestCase {
assertNull(translation.aggFilter);
assertThat(translation.query, instanceOf(ScriptQuery.class));
ScriptQuery sc = (ScriptQuery) translation.query;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.eq(InternalSqlScriptUtils.st" + dim + "(" +
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.eq(InternalSqlScriptUtils.st" + dim + "(" +
"InternalSqlScriptUtils.geoDocValue(doc,params.v0)),params.v1))",
sc.script().toString());
assertEquals("[{v=point}, {v=10}]", sc.script().params().toString());
@ -968,7 +972,7 @@ public class QueryTranslatorTests extends ESTestCase {
assertNull(translation.aggFilter);
assertThat(translation.query, instanceOf(ScriptQuery.class));
ScriptQuery sc = (ScriptQuery) translation.query;
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.eq(InternalSqlScriptUtils.stGeometryType(" +
assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.eq(InternalSqlScriptUtils.stGeometryType(" +
"InternalSqlScriptUtils.geoDocValue(doc,params.v0)),params.v1))",
sc.script().toString());
assertEquals("[{v=point}, {v=POINT}]", sc.script().params().toString());
@ -982,7 +986,8 @@ public class QueryTranslatorTests extends ESTestCase {
GroupingContext groupingContext = QueryFolder.FoldAggregate.groupBy(((Aggregate) p).groupings());
assertNotNull(groupingContext);
ScriptTemplate scriptTemplate = groupingContext.tail.script();
assertEquals("InternalSqlScriptUtils.coalesce([InternalSqlScriptUtils.docValue(doc,params.v0),params.v1])",
assertEquals(
"InternalSqlScriptUtils.coalesce([InternalQlScriptUtils.docValue(doc,params.v0),params.v1])",
scriptTemplate.toString());
assertEquals("[{v=int}, {v=10}]", scriptTemplate.params().toString());
}
@ -995,7 +1000,8 @@ public class QueryTranslatorTests extends ESTestCase {
GroupingContext groupingContext = QueryFolder.FoldAggregate.groupBy(((Aggregate) p).groupings());
assertNotNull(groupingContext);
ScriptTemplate scriptTemplate = groupingContext.tail.script();
assertEquals("InternalSqlScriptUtils.nullif(InternalSqlScriptUtils.docValue(doc,params.v0),params.v1)",
assertEquals(
"InternalSqlScriptUtils.nullif(InternalQlScriptUtils.docValue(doc,params.v0),params.v1)",
scriptTemplate.toString());
assertEquals("[{v=int}, {v=10}]", scriptTemplate.params().toString());
}
@ -1008,8 +1014,8 @@ public class QueryTranslatorTests extends ESTestCase {
GroupingContext groupingContext = QueryFolder.FoldAggregate.groupBy(((Aggregate) p).groupings());
assertNotNull(groupingContext);
ScriptTemplate scriptTemplate = groupingContext.tail.script();
assertEquals("InternalSqlScriptUtils.caseFunction([InternalSqlScriptUtils.gt(InternalSqlScriptUtils.docValue(" + "" +
"doc,params.v0),params.v1),params.v2,InternalSqlScriptUtils.gt(InternalSqlScriptUtils.docValue(doc,params.v3)," +
assertEquals("InternalSqlScriptUtils.caseFunction([InternalQlScriptUtils.gt(InternalQlScriptUtils.docValue(" + ""
+ "doc,params.v0),params.v1),params.v2,InternalQlScriptUtils.gt(InternalQlScriptUtils.docValue(doc,params.v3)," +
"params.v4),params.v5,params.v6])",
scriptTemplate.toString());
assertEquals("[{v=int}, {v=10}, {v=foo}, {v=int}, {v=20}, {v=bar}, {v=default}]", scriptTemplate.params().toString());
@ -1023,8 +1029,8 @@ public class QueryTranslatorTests extends ESTestCase {
GroupingContext groupingContext = QueryFolder.FoldAggregate.groupBy(((Aggregate) p).groupings());
assertNotNull(groupingContext);
ScriptTemplate scriptTemplate = groupingContext.tail.script();
assertEquals("InternalSqlScriptUtils.caseFunction([InternalSqlScriptUtils.gt(" +
"InternalSqlScriptUtils.docValue(doc,params.v0),params.v1),params.v2,params.v3])",
assertEquals("InternalSqlScriptUtils.caseFunction([InternalQlScriptUtils.gt(" +
"InternalQlScriptUtils.docValue(doc,params.v0),params.v1),params.v2,params.v3])",
scriptTemplate.toString());
assertEquals("[{v=int}, {v=20}, {v=foo}, {v=bar}]", scriptTemplate.params().toString());
}
@ -1155,7 +1161,7 @@ public class QueryTranslatorTests extends ESTestCase {
assertEquals(DATETIME, eqe.output().get(0).dataType());
assertThat(eqe.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""),
endsWith("\"date_histogram\":{\"script\":{\"source\":\"InternalSqlScriptUtils.add(" +
"InternalSqlScriptUtils.docValue(doc,params.v0),InternalSqlScriptUtils.intervalDayTime(params.v1,params.v2))\"," +
"InternalQlScriptUtils.docValue(doc,params.v0),InternalSqlScriptUtils.intervalDayTime(params.v1,params.v2))\"," +
"\"lang\":\"painless\",\"params\":{\"v0\":\"date\",\"v1\":\"PT120H\",\"v2\":\"INTERVAL_DAY\"}}," +
"\"missing_bucket\":true,\"value_type\":\"long\",\"order\":\"asc\"," +
"\"calendar_interval\":\"1d\",\"time_zone\":\"Z\"}}}]}}}"));
@ -1170,7 +1176,7 @@ public class QueryTranslatorTests extends ESTestCase {
assertEquals(INTEGER, eqe.output().get(0).dataType());
assertThat(eqe.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""),
endsWith("\"date_histogram\":{\"script\":{\"source\":\"InternalSqlScriptUtils.cast(" +
"InternalSqlScriptUtils.add(InternalSqlScriptUtils.docValue(doc,params.v0)," +
"InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0)," +
"InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2)),params.v3)\"," +
"\"lang\":\"painless\",\"params\":{\"v0\":\"date\",\"v1\":\"P5M\",\"v2\":\"INTERVAL_MONTH\"," +
"\"v3\":\"DATE\"}},\"missing_bucket\":true,\"value_type\":\"long\",\"order\":\"asc\"," +
@ -1185,8 +1191,9 @@ public class QueryTranslatorTests extends ESTestCase {
assertEquals("YEAR(date)", eqe.output().get(0).qualifiedName());
assertEquals(INTEGER, eqe.output().get(0).dataType());
assertThat(eqe.queryContainer().toString().replaceAll("\\s+", ""),
endsWith("\"sort\":[{\"_script\":{\"script\":{\"source\":\"InternalSqlScriptUtils.nullSafeSortNumeric(" +
"InternalSqlScriptUtils.dateTimeChrono(InternalSqlScriptUtils.docValue(doc,params.v0)," +
endsWith("\"sort\":[{\"_script\":{\"script\":{\"source\":\"InternalQlScriptUtils.nullSafeSortNumeric("
+
"InternalSqlScriptUtils.dateTimeChrono(InternalQlScriptUtils.docValue(doc,params.v0)," +
"params.v1,params.v2))\",\"lang\":\"painless\",\"params\":{\"v0\":\"date\",\"v1\":\"Z\"," +
"\"v2\":\"YEAR\"}},\"type\":\"number\",\"order\":\"asc\"}}]}"));
}
@ -1309,17 +1316,17 @@ public class QueryTranslatorTests extends ESTestCase {
+ "\"a4\":\"_count\","
+ "\"a5\":\"" + avgInt.getName() + "\"},"
+ "\"script\":{\"source\":\""
+ "InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.and("
+ "InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.and("
+ "InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.and("
+ "InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.and("
+ "InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.and("
+ "InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(params.a0,params.v0)),"
+ "InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(params.a1,params.v1)))),"
+ "InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(params.a2,params.v2)))),"
+ "InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(params.a3,params.v3)))),"
+ "InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(params.a4,params.v4)))),"
+ "InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(params.a5,params.v5))))\","
+ "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and("
+ "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and("
+ "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and("
+ "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and("
+ "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and("
+ "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a0,params.v0)),"
+ "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a1,params.v1)))),"
+ "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a2,params.v2)))),"
+ "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a3,params.v3)))),"
+ "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a4,params.v4)))),"
+ "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a5,params.v5))))\","
+ "\"lang\":\"painless\",\"params\":{\"v0\":3,\"v1\":32,\"v2\":1,\"v3\":2,\"v4\":5,\"v5\":50000}},"
+ "\"gap_policy\":\"skip\"}}}}}"));
}
@ -1335,7 +1342,7 @@ public class QueryTranslatorTests extends ESTestCase {
((EsQueryExec) p).queryContainer().aggs().asAggBuilder().toString()
.replaceAll("\\s+", ""),
endsWith("{\"source\":\"InternalSqlScriptUtils.cast(InternalSqlScriptUtils.abs(InternalSqlScriptUtils.dateTimeChrono" +
"(InternalSqlScriptUtils.docValue(doc,params.v0),params.v1,params.v2)),params.v3)\",\"lang\":\"painless\"," +
"(InternalQlScriptUtils.docValue(doc,params.v0),params.v1,params.v2)),params.v3)\",\"lang\":\"painless\"," +
"\"params\":{\"v0\":\"date\",\"v1\":\"Z\",\"v2\":\"YEAR\",\"v3\":\"LONG\"}},\"missing_bucket\":true," +
"\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}")
);
@ -1352,7 +1359,7 @@ public class QueryTranslatorTests extends ESTestCase {
((EsQueryExec) p).queryContainer().aggs().asAggBuilder().toString()
.replaceAll("\\s+", ""),
endsWith("{\"source\":\"InternalSqlScriptUtils.cast(InternalSqlScriptUtils.abs(InternalSqlScriptUtils.dateTimeChrono" +
"(InternalSqlScriptUtils.docValue(doc,params.v0),params.v1,params.v2)),params.v3)\",\"lang\":\"painless\"," +
"(InternalQlScriptUtils.docValue(doc,params.v0),params.v1,params.v2)),params.v3)\",\"lang\":\"painless\"," +
"\"params\":{\"v0\":\"date\",\"v1\":\"Z\",\"v2\":\"YEAR\",\"v3\":\"LONG\"}},\"missing_bucket\":true," +
"\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}")
);
@ -1369,7 +1376,7 @@ public class QueryTranslatorTests extends ESTestCase {
((EsQueryExec) p).queryContainer().aggs().asAggBuilder().toString()
.replaceAll("\\s+", ""),
endsWith("{\"source\":\"InternalSqlScriptUtils.cast(InternalSqlScriptUtils.abs(InternalSqlScriptUtils.dateTimeChrono" +
"(InternalSqlScriptUtils.docValue(doc,params.v0),params.v1,params.v2)),params.v3)\",\"lang\":\"painless\"," +
"(InternalQlScriptUtils.docValue(doc,params.v0),params.v1,params.v2)),params.v3)\",\"lang\":\"painless\"," +
"\"params\":{\"v0\":\"date\",\"v1\":\"Z\",\"v2\":\"YEAR\",\"v3\":\"LONG\"}},\"missing_bucket\":true," +
"\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}")
);
@ -1388,7 +1395,7 @@ public class QueryTranslatorTests extends ESTestCase {
((EsQueryExec) p).queryContainer().aggs().asAggBuilder().toString()
.replaceAll("\\s+", ""),
endsWith("{\"source\":\"InternalSqlScriptUtils.cast(InternalSqlScriptUtils.abs(InternalSqlScriptUtils.dateTimeChrono" +
"(InternalSqlScriptUtils.docValue(doc,params.v0),params.v1,params.v2)),params.v3)\",\"lang\":\"painless\"," +
"(InternalQlScriptUtils.docValue(doc,params.v0),params.v1,params.v2)),params.v3)\",\"lang\":\"painless\"," +
"\"params\":{\"v0\":\"date\",\"v1\":\"Z\",\"v2\":\"YEAR\",\"v3\":\"LONG\"}},\"missing_bucket\":true," +
"\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}")
);
@ -1404,7 +1411,7 @@ public class QueryTranslatorTests extends ESTestCase {
((EsQueryExec) p).queryContainer().aggs().asAggBuilder().toString()
.replaceAll("\\s+", ""),
endsWith("{\"source\":\"InternalSqlScriptUtils.dateTimeChrono(" +
"InternalSqlScriptUtils.docValue(doc,params.v0),params.v1,params.v2)\",\"lang\":\"painless\"," +
"InternalQlScriptUtils.docValue(doc,params.v0),params.v1,params.v2)\",\"lang\":\"painless\"," +
"\"params\":{\"v0\":\"date\",\"v1\":\"Z\",\"v2\":\"HOUR_OF_DAY\"}},\"missing_bucket\":true," +
"\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}")
);
@ -1423,7 +1430,7 @@ public class QueryTranslatorTests extends ESTestCase {
((EsQueryExec) p).queryContainer().aggs().asAggBuilder().toString()
.replaceAll("\\s+", ""),
endsWith("{\"source\":\"InternalSqlScriptUtils.cast(InternalSqlScriptUtils.abs(InternalSqlScriptUtils.dateTimeChrono" +
"(InternalSqlScriptUtils.docValue(doc,params.v0),params.v1,params.v2)),params.v3)\",\"lang\":\"painless\"," +
"(InternalQlScriptUtils.docValue(doc,params.v0),params.v1,params.v2)),params.v3)\",\"lang\":\"painless\"," +
"\"params\":{\"v0\":\"date\",\"v1\":\"Z\",\"v2\":\"YEAR\",\"v3\":\"LONG\"}},\"missing_bucket\":true," +
"\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}")
);
@ -1438,7 +1445,7 @@ public class QueryTranslatorTests extends ESTestCase {
((EsQueryExec) p).queryContainer().aggs().asAggBuilder().toString()
.replaceAll("\\s+", ""),
endsWith("{\"source\":\"InternalSqlScriptUtils.dateTimeChrono(" +
"InternalSqlScriptUtils.docValue(doc,params.v0),params.v1,params.v2)\",\"lang\":\"painless\"," +
"InternalQlScriptUtils.docValue(doc,params.v0),params.v1,params.v2)\",\"lang\":\"painless\"," +
"\"params\":{\"v0\":\"date\",\"v1\":\"Z\",\"v2\":\"MINUTE_OF_HOUR\"}}," +
"\"missing_bucket\":true,\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}")
);
@ -1456,7 +1463,7 @@ public class QueryTranslatorTests extends ESTestCase {
((EsQueryExec) p).queryContainer().aggs().asAggBuilder().toString()
.replaceAll("\\s+", ""),
endsWith("{\"source\":\"InternalSqlScriptUtils.cast(InternalSqlScriptUtils.abs(InternalSqlScriptUtils.dateTimeChrono" +
"(InternalSqlScriptUtils.docValue(doc,params.v0),params.v1,params.v2)),params.v3)\",\"lang\":\"painless\"," +
"(InternalQlScriptUtils.docValue(doc,params.v0),params.v1,params.v2)),params.v3)\",\"lang\":\"painless\"," +
"\"params\":{\"v0\":\"date\",\"v1\":\"Z\",\"v2\":\"YEAR\",\"v3\":\"LONG\"}},\"missing_bucket\":true," +
"\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}")
);
@ -1472,7 +1479,8 @@ public class QueryTranslatorTests extends ESTestCase {
assertThat(
((EsQueryExec) p).queryContainer().aggs().asAggBuilder().toString()
.replaceAll("\\s+", ""),
endsWith("{\"script\":{\"source\":\"InternalSqlScriptUtils.mul(params.v0,InternalSqlScriptUtils.docValue(doc,params.v1))\"," +
endsWith("{\"script\":{\"source\":\"InternalSqlScriptUtils.mul(params.v0,InternalQlScriptUtils.docValue(doc,params.v1))\","
+
"\"lang\":\"painless\",\"params\":{\"v0\":3.141592653589793,\"v1\":\"int\"}},\"missing_bucket\":true," +
"\"value_type\":\"double\",\"order\":\"asc\"}}}]}}}")
);
@ -1489,7 +1497,8 @@ public class QueryTranslatorTests extends ESTestCase {
assertThat(
((EsQueryExec) p).queryContainer().aggs().asAggBuilder().toString()
.replaceAll("\\s+", ""),
endsWith("{\"script\":{\"source\":\"InternalSqlScriptUtils.mul(params.v0,InternalSqlScriptUtils.docValue(doc,params.v1))" +
endsWith("{\"script\":{\"source\":\"InternalSqlScriptUtils.mul(params.v0,InternalQlScriptUtils.docValue(doc,params.v1))"
+
"\",\"lang\":\"painless\",\"params\":{\"v0\":3.141592653589793,\"v1\":\"int\"}},\"missing_bucket\":true," +
"\"value_type\":\"double\",\"order\":\"asc\"}}}]}}}")
);
@ -1505,7 +1514,7 @@ public class QueryTranslatorTests extends ESTestCase {
assertThat(
((EsQueryExec) p).queryContainer().aggs().asAggBuilder().toString()
.replaceAll("\\s+", ""),
endsWith("{\"source\":\"InternalSqlScriptUtils.gt(InternalSqlScriptUtils.docValue(doc,params.v0),params.v1)\"," +
endsWith("{\"source\":\"InternalQlScriptUtils.gt(InternalQlScriptUtils.docValue(doc,params.v0),params.v1)\"," +
"\"lang\":\"painless\",\"params\":{\"v0\":\"int\",\"v1\":3}}," +
"\"missing_bucket\":true,\"value_type\":\"boolean\",\"order\":\"asc\"}}}]}}}")
);
@ -1523,7 +1532,8 @@ public class QueryTranslatorTests extends ESTestCase {
assertThat(
((EsQueryExec) p).queryContainer().aggs().asAggBuilder().toString()
.replaceAll("\\s+", ""),
endsWith("{\"script\":{\"source\":\"InternalSqlScriptUtils.mul(params.v0,InternalSqlScriptUtils.docValue(doc,params.v1))" +
endsWith("{\"script\":{\"source\":\"InternalSqlScriptUtils.mul(params.v0,InternalQlScriptUtils.docValue(doc,params.v1))"
+
"\",\"lang\":\"painless\",\"params\":{\"v0\":3.141592653589793,\"v1\":\"int\"}},\"missing_bucket\":true," +
"\"value_type\":\"double\",\"order\":\"asc\"}}}]}}}")
);
@ -1537,7 +1547,7 @@ public class QueryTranslatorTests extends ESTestCase {
assertThat(
((EsQueryExec) p).queryContainer().aggs().asAggBuilder().toString()
.replaceAll("\\s+", ""),
endsWith("{\"source\":\"InternalSqlScriptUtils.mul(params.v0,InternalSqlScriptUtils.docValue(doc,params.v1))\"," +
endsWith("{\"source\":\"InternalSqlScriptUtils.mul(params.v0,InternalQlScriptUtils.docValue(doc,params.v1))\"," +
"\"lang\":\"painless\",\"params\":{\"v0\":3.141592653589793,\"v1\":\"int\"}}," +
"\"missing_bucket\":true,\"value_type\":\"double\",\"order\":\"asc\"}}}]}}}")
);
@ -1551,8 +1561,8 @@ public class QueryTranslatorTests extends ESTestCase {
assertThat(
((EsQueryExec) p).queryContainer().aggs().asAggBuilder().toString()
.replaceAll("\\s+", ""),
endsWith("{\"source\":\"InternalSqlScriptUtils.add(InternalSqlScriptUtils.docValue(doc,params.v0)," +
"InternalSqlScriptUtils.intervalDayTime(params.v1,params.v2))\"," +
endsWith("{\"source\":\"InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0)," +
"InternalSqlScriptUtils.intervalDayTime(params.v1,params.v2))\"," +
"\"lang\":\"painless\",\"params\":{\"v0\":\"date\",\"v1\":\"PT24H\",\"v2\":\"INTERVAL_DAY\"}}," +
"\"missing_bucket\":true,\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}")
);
@ -1568,8 +1578,8 @@ public class QueryTranslatorTests extends ESTestCase {
assertThat(
((EsQueryExec) p).queryContainer().toString()
.replaceAll("\\s+", ""),
endsWith("\"sort\":[{\"_script\":{\"script\":{\"source\":\"InternalSqlScriptUtils.nullSafeSortString(InternalSqlScriptUtils" +
".cast(InternalSqlScriptUtils.docValue(doc,params.v0),params.v1))\",\"lang\":\"painless\"," +
endsWith("\"sort\":[{\"_script\":{\"script\":{\"source\":\"InternalQlScriptUtils.nullSafeSortString(InternalSqlScriptUtils" +
".cast(InternalQlScriptUtils.docValue(doc,params.v0),params.v1))\",\"lang\":\"painless\"," +
"\"params\":{\"v0\":\"date\",\"v1\":\"TIME\"}},\"type\":\"string\",\"order\":\"asc\"}},{\"int\":{\"order\":\"asc\"," +
"\"missing\":\"_last\",\"unmapped_type\":\"integer\"}}]}")
);
@ -1711,9 +1721,9 @@ public class QueryTranslatorTests extends ESTestCase {
assertEquals(EsQueryExec.class, p.getClass());
EsQueryExec eqe = (EsQueryExec) p;
assertThat(eqe.queryContainer().toString().replaceAll("\\s+", ""), containsString(
"\"script\":{\"script\":{\"source\":\"InternalSqlScriptUtils.nullSafeFilter("
+ "InternalSqlScriptUtils.gt(InternalSqlScriptUtils.add(InternalSqlScriptUtils.docValue(doc,params.v0),"
+ "InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2)),InternalSqlScriptUtils.asDateTime(params.v3)))\","
"\"script\":{\"script\":{\"source\":\"InternalQlScriptUtils.nullSafeFilter("
+ "InternalQlScriptUtils.gt(InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0),"
+ "InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2)),InternalSqlScriptUtils.asDateTime(params.v3)))\","
+ "\"lang\":\"painless\","
+ "\"params\":{\"v0\":\"date\",\"v1\":\"P1Y\",\"v2\":\"INTERVAL_YEAR\",\"v3\":\"2019-03-11T12:34:56.000Z\"}},"));
}
@ -1728,8 +1738,8 @@ public class QueryTranslatorTests extends ESTestCase {
EsQueryExec eqe = (EsQueryExec) p;
assertThat(eqe.queryContainer().toString().replaceAll("\\s+", ""), containsString(
"{\"terms\":{\"script\":{\"source\":\"InternalSqlScriptUtils.dateTimeChrono("
+ "InternalSqlScriptUtils.add(InternalSqlScriptUtils.docValue(doc,params.v0),"
+ "InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2)),params.v3,params.v4)\","
+ "InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0),"
+ "InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2)),params.v3,params.v4)\","
+ "\"lang\":\"painless\",\"params\":{\"v0\":\"date\",\"v1\":\"P1Y\",\"v2\":\"INTERVAL_YEAR\","
+ "\"v3\":\"Z\",\"v4\":\"" + randomFunction.chronoField().name() + "\"}},\"missing_bucket\":true,"
+ "\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}}"));
@ -1747,8 +1757,8 @@ public class QueryTranslatorTests extends ESTestCase {
EsQueryExec eqe = (EsQueryExec) p;
assertThat(eqe.queryContainer().toString().replaceAll("\\s+", ""), containsString(
"{\"terms\":{\"script\":{\"source\":\"InternalSqlScriptUtils." + scriptMethods[pos]
+ "(InternalSqlScriptUtils.add(InternalSqlScriptUtils.docValue(doc,params.v0),"
+ "InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2)),params.v3)\",\"lang\":\"painless\","
+ "(InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0),"
+ "InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2)),params.v3)\",\"lang\":\"painless\","
+ "\"params\":{\"v0\":\"date\",\"v1\":\"P1Y\",\"v2\":\"INTERVAL_YEAR\",\"v3\":\"Z\"}},\"missing_bucket\":true,"));
}
@ -1770,7 +1780,7 @@ public class QueryTranslatorTests extends ESTestCase {
assertTrue("Should be tracking hits", eqe.queryContainer().shouldTrackHits());
assertEquals(1, eqe.output().size());
assertThat(eqe.queryContainer().toString().replaceAll("\\s+", ""), containsString(
"\"script\":{\"source\":\"InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(params.a0,params.v0))\","
"\"script\":{\"source\":\"InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a0,params.v0))\","
+ "\"lang\":\"painless\",\"params\":{\"v0\":0}}"));
}
}