Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #242 from elastic/main
🤖 ESQL: Merge upstream
- Loading branch information
Showing
58 changed files
with
1,651 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 89895 | ||
summary: Initial code to support binary expression scripts | ||
area: "Infra/Scripting" | ||
type: enhancement | ||
issues: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 90017 | ||
summary: Fix Fields API Caching Regression | ||
area: Infra/Scripting | ||
type: regression | ||
issues: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 90035 | ||
summary: Add a TSID global ordinal to `TimeSeriesIndexSearcher` | ||
area: TSDB | ||
type: feature | ||
issues: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...ssion/src/main/java/org/elasticsearch/script/expression/ExpressionDoubleValuesScript.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.script.expression; | ||
|
||
import org.apache.lucene.expressions.Bindings; | ||
import org.apache.lucene.expressions.Expression; | ||
import org.apache.lucene.search.DoubleValues; | ||
import org.apache.lucene.search.DoubleValuesSource; | ||
import org.apache.lucene.search.Rescorer; | ||
import org.apache.lucene.search.SortField; | ||
import org.elasticsearch.script.DoubleValuesScript; | ||
|
||
import java.util.function.Function; | ||
|
||
/** | ||
* A factory for a custom compiled {@link Expression} scripts | ||
* <p> | ||
* Instead of an execution result, we return a wrapper to an {@link Expression} object, which | ||
* can be used for all supported double values operations. | ||
*/ | ||
public class ExpressionDoubleValuesScript implements DoubleValuesScript.Factory { | ||
private final Expression exprScript; | ||
|
||
ExpressionDoubleValuesScript(Expression e) { | ||
this.exprScript = e; | ||
} | ||
|
||
@Override | ||
public DoubleValuesScript newInstance() { | ||
return new DoubleValuesScript() { | ||
@Override | ||
public double execute() { | ||
return exprScript.evaluate(new DoubleValues[0]); | ||
} | ||
|
||
@Override | ||
public double evaluate(DoubleValues[] functionValues) { | ||
return exprScript.evaluate(functionValues); | ||
} | ||
|
||
@Override | ||
public DoubleValuesSource getDoubleValuesSource(Function<String, DoubleValuesSource> sourceProvider) { | ||
return exprScript.getDoubleValuesSource(new Bindings() { | ||
@Override | ||
public DoubleValuesSource getDoubleValuesSource(String name) { | ||
return sourceProvider.apply(name); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public SortField getSortField(Function<String, DoubleValuesSource> sourceProvider, boolean reverse) { | ||
return exprScript.getSortField(new Bindings() { | ||
@Override | ||
public DoubleValuesSource getDoubleValuesSource(String name) { | ||
return sourceProvider.apply(name); | ||
} | ||
}, reverse); | ||
} | ||
|
||
@Override | ||
public Rescorer getRescorer(Function<String, DoubleValuesSource> sourceProvider) { | ||
return exprScript.getRescorer(new Bindings() { | ||
@Override | ||
public DoubleValuesSource getDoubleValuesSource(String name) { | ||
return sourceProvider.apply(name); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public String sourceText() { | ||
return exprScript.sourceText; | ||
} | ||
|
||
@Override | ||
public String[] variables() { | ||
return exprScript.variables; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
.../src/test/java/org/elasticsearch/script/expression/ExpressionDoubleValuesScriptTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.script.expression; | ||
|
||
import org.apache.lucene.expressions.SimpleBindings; | ||
import org.apache.lucene.search.DoubleValues; | ||
import org.apache.lucene.search.DoubleValuesSource; | ||
import org.apache.lucene.search.SortField; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.script.DoubleValuesScript; | ||
import org.elasticsearch.script.Script; | ||
import org.elasticsearch.script.ScriptException; | ||
import org.elasticsearch.script.ScriptModule; | ||
import org.elasticsearch.script.ScriptService; | ||
import org.elasticsearch.script.ScriptType; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.io.IOException; | ||
import java.text.ParseException; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
/** | ||
* Tests {@link ExpressionDoubleValuesScript} through the {@link ScriptService} | ||
*/ | ||
public class ExpressionDoubleValuesScriptTests extends ESTestCase { | ||
private ExpressionScriptEngine engine; | ||
private ScriptService scriptService; | ||
|
||
@Override | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
|
||
engine = new ExpressionScriptEngine(); | ||
scriptService = new ScriptService(Settings.EMPTY, Map.of("expression", engine), ScriptModule.CORE_CONTEXTS, () -> 1L); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private DoubleValuesScript compile(String expression) { | ||
var script = new Script(ScriptType.INLINE, "expression", expression, Collections.emptyMap()); | ||
return scriptService.compile(script, DoubleValuesScript.CONTEXT).newInstance(); | ||
} | ||
|
||
public void testCompileError() { | ||
ScriptException e = expectThrows(ScriptException.class, () -> compile("10 * log(10)")); | ||
assertTrue(e.getCause() instanceof ParseException); | ||
assertEquals("Invalid expression '10 * log(10)': Unrecognized function call (log).", e.getCause().getMessage()); | ||
} | ||
|
||
public void testEvaluate() { | ||
var expression = compile("10 * log10(10)"); | ||
assertEquals("10 * log10(10)", expression.sourceText()); | ||
assertEquals(10.0, expression.evaluate(new DoubleValues[0]), 0.00001); | ||
assertEquals(10.0, expression.execute(), 0.00001); | ||
|
||
expression = compile("20 * log10(a)"); | ||
assertEquals("20 * log10(a)", expression.sourceText()); | ||
assertEquals(20.0, expression.evaluate(new DoubleValues[] { DoubleValues.withDefault(DoubleValues.EMPTY, 10.0) }), 0.00001); | ||
} | ||
|
||
public void testDoubleValuesSource() throws IOException { | ||
SimpleBindings bindings = new SimpleBindings(); | ||
bindings.add("popularity", DoubleValuesSource.constant(5)); | ||
|
||
var expression = compile("10 * log10(popularity)"); | ||
var doubleValues = expression.getDoubleValuesSource((name) -> bindings.getDoubleValuesSource(name)); | ||
assertEquals("expr(10 * log10(popularity))", doubleValues.toString()); | ||
var values = doubleValues.getValues(null, null); | ||
assertTrue(values.advanceExact(0)); | ||
assertEquals(6, (int) values.doubleValue()); | ||
|
||
var sortField = expression.getSortField((name) -> bindings.getDoubleValuesSource(name), false); | ||
assertEquals("expr(10 * log10(popularity))", sortField.getField()); | ||
assertEquals(SortField.Type.CUSTOM, sortField.getType()); | ||
assertFalse(sortField.getReverse()); | ||
|
||
var rescorer = expression.getRescorer((name) -> bindings.getDoubleValuesSource(name)); | ||
assertNotNull(rescorer); | ||
} | ||
|
||
} |
Oops, something went wrong.