From 92609bf8e5b910a27b422369a3c64763347b1134 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Wed, 16 May 2018 00:01:46 +0300 Subject: [PATCH 1/6] SQL: Whitelist SQL utility class for better scripting Add SQL class for reusing code inside SQL functions within Painless Fix 29832 --- x-pack/plugin/sql/build.gradle | 3 +- .../scalar/datetime/DateTimeFunction.java | 58 +++++++------------ .../scalar/script/ScriptTemplate.java | 3 +- .../function/scalar/whitelist/SqlScripts.java | 22 +++++++ .../sql/plugin/SqlPainlessExtension.java | 33 +++++++++++ ...asticsearch.painless.spi.PainlessExtension | 1 + .../xpack/sql/plugin/sql_whitelist.txt | 12 ++++ .../rest-api-spec/test/sql/whitelist.yml | 27 +++++++++ 8 files changed, 118 insertions(+), 41 deletions(-) create mode 100644 x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/SqlScripts.java create mode 100644 x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPainlessExtension.java create mode 100644 x-pack/plugin/sql/src/main/resources/META-INF/services/org.elasticsearch.painless.spi.PainlessExtension create mode 100644 x-pack/plugin/sql/src/main/resources/org/elasticsearch/xpack/sql/plugin/sql_whitelist.txt create mode 100644 x-pack/plugin/src/test/resources/rest-api-spec/test/sql/whitelist.yml diff --git a/x-pack/plugin/sql/build.gradle b/x-pack/plugin/sql/build.gradle index c52413aa4e1d6..8b406235985c6 100644 --- a/x-pack/plugin/sql/build.gradle +++ b/x-pack/plugin/sql/build.gradle @@ -5,7 +5,7 @@ esplugin { name 'x-pack-sql' description 'The Elasticsearch plugin that powers SQL for Elasticsearch' classname 'org.elasticsearch.xpack.sql.plugin.SqlPlugin' - extendedPlugins = ['x-pack-core'] + extendedPlugins = ['x-pack-core', 'lang-painless'] } configurations { @@ -20,6 +20,7 @@ integTest.enabled = false dependencies { compileOnly "org.elasticsearch.plugin:x-pack-core:${version}" + compileOnly project(':modules:lang-painless') compile project('sql-proto') compile "org.elasticsearch.plugin:aggs-matrix-stats-client:${version}" compile "org.antlr:antlr4-runtime:4.5.3" diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFunction.java index 3b3717b1318df..606728222787b 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFunction.java @@ -20,7 +20,6 @@ import org.elasticsearch.xpack.sql.tree.NodeInfo; import org.elasticsearch.xpack.sql.type.DataType; import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; import java.time.Instant; import java.time.ZoneId; @@ -52,8 +51,18 @@ public abstract class DateTimeFunction extends UnaryScalarFunction { protected final NodeInfo info() { return NodeInfo.create(this, ctorForInfo(), field(), timeZone()); } + protected abstract NodeInfo.NodeCtor2 ctorForInfo(); + @Override + protected TypeResolution resolveType() { + if (field().dataType() == DataType.DATE) { + return TypeResolution.TYPE_RESOLVED; + } + return new TypeResolution("Function [" + functionName() + "] cannot be applied on a non-date expression ([" + + Expressions.name(field()) + "] of type [" + field().dataType().esType + "])"); + } + public TimeZone timeZone() { return timeZone; } @@ -70,18 +79,12 @@ public Object fold() { return null; } - ZonedDateTime time = ZonedDateTime.ofInstant( - Instant.ofEpochMilli(folded.getMillis()), ZoneId.of(timeZone.getID())); - return time.get(chronoField()); + return dateTimeChrono(folded.getMillis(), timeZone.getID(), chronoField().name()); } - @Override - protected TypeResolution resolveType() { - if (field().dataType() == DataType.DATE) { - return TypeResolution.TYPE_RESOLVED; - } - return new TypeResolution("Function [" + functionName() + "] cannot be applied on a non-date expression ([" - + Expressions.name(field()) + "] of type [" + field().dataType().esType + "])"); + public static Integer dateTimeChrono(long millis, String tzId, String chronoName) { + ZonedDateTime time = ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.of(tzId)); + return Integer.valueOf(time.get(ChronoField.valueOf(chronoName))); } @Override @@ -89,28 +92,11 @@ protected ScriptTemplate asScriptFrom(FieldAttribute field) { ParamsBuilder params = paramsBuilder(); String template = null; - if (TimeZone.getTimeZone("UTC").equals(timeZone)) { - // TODO: it would be nice to be able to externalize the extract function and reuse the script across all extractors - template = formatTemplate("doc[{}].value.get" + extractFunction() + "()"); - params.variable(field.name()); - } else { - // TODO ewwww - /* - * This uses the Java 8 time API because Painless doesn't whitelist creation of new - * Joda classes. - * - * The actual script is - * ZonedDateTime.ofInstant(Instant.ofEpochMilli(.value.millis), - * ZoneId.of()).get(ChronoField.get(MONTH_OF_YEAR)) - */ - - template = formatTemplate("ZonedDateTime.ofInstant(Instant.ofEpochMilli(doc[{}].value.millis), " - + "ZoneId.of({})).get(ChronoField.valueOf({}))"); - params.variable(field.name()) - .variable(timeZone.getID()) - .variable(chronoField().name()); - } - + template = formatTemplate("{sql}.dateTimeChrono(doc[{}].value.millis, {}, {})"); + params.variable(field.name()) + .variable(timeZone.getID()) + .variable(chronoField().name()); + return new ScriptTemplate(template, params.build(), dataType()); } @@ -120,10 +106,6 @@ protected ScriptTemplate asScriptFrom(AggregateFunctionAttribute aggregate) { throw new UnsupportedOperationException(); } - protected String extractFunction() { - return getClass().getSimpleName(); - } - /** * Used for generating the painless script version of this function when the time zone is not UTC */ @@ -165,4 +147,4 @@ public boolean equals(Object obj) { public int hashCode() { return Objects.hash(field(), timeZone); } -} +} \ No newline at end of file diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/script/ScriptTemplate.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/script/ScriptTemplate.java index b5f9e7f3f9cfd..316ca00a28199 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/script/ScriptTemplate.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/script/ScriptTemplate.java @@ -8,7 +8,6 @@ import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; import org.elasticsearch.xpack.sql.type.DataType; -import org.elasticsearch.xpack.sql.type.DataTypes; import org.elasticsearch.xpack.sql.util.StringUtils; import java.util.List; @@ -93,6 +92,6 @@ public String toString() { } public static String formatTemplate(String template) { - return template.replace("{}", "params.%s"); + return template.replace("{sql}", "SqlScripts").replace("{}", "params.%s"); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/SqlScripts.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/SqlScripts.java new file mode 100644 index 0000000000000..e8a9bceb1ed7a --- /dev/null +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/SqlScripts.java @@ -0,0 +1,22 @@ +/* + * 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.sql.expression.function.scalar.whitelist; + +import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeFunction; + +/** + * Whitelisted class for SQL scripts. + * Acts as a registry of the various static methods used internally by the scalar functions + * (to simplify the whitelist definition). + */ +public final class SqlScripts { + + private SqlScripts() {} + + public static Integer dateTimeChrono(long millis, String tzId, String chronoName) { + return DateTimeFunction.dateTimeChrono(millis, tzId, chronoName); + } +} diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPainlessExtension.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPainlessExtension.java new file mode 100644 index 0000000000000..d67c3aea4f0a0 --- /dev/null +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPainlessExtension.java @@ -0,0 +1,33 @@ +/* + * 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.sql.plugin; + +import org.elasticsearch.painless.spi.PainlessExtension; +import org.elasticsearch.painless.spi.Whitelist; +import org.elasticsearch.painless.spi.WhitelistLoader; +import org.elasticsearch.script.FilterScript; +import org.elasticsearch.script.ScriptContext; +import org.elasticsearch.script.SearchScript; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import static java.util.Collections.singletonList; + +public class SqlPainlessExtension implements PainlessExtension { + + private static final Whitelist WHITELIST = WhitelistLoader.loadFromResourceFiles(SqlPainlessExtension.class, "sql_whitelist.txt"); + + @Override + public Map, List> getContextWhitelists() { + Map, List> whitelist = new LinkedHashMap<>(); + whitelist.put(FilterScript.CONTEXT, singletonList(WHITELIST)); + whitelist.put(SearchScript.CONTEXT, singletonList(WHITELIST)); + whitelist.put(SearchScript.AGGS_CONTEXT, singletonList(WHITELIST)); + return whitelist; + } +} diff --git a/x-pack/plugin/sql/src/main/resources/META-INF/services/org.elasticsearch.painless.spi.PainlessExtension b/x-pack/plugin/sql/src/main/resources/META-INF/services/org.elasticsearch.painless.spi.PainlessExtension new file mode 100644 index 0000000000000..5f2f571f015d9 --- /dev/null +++ b/x-pack/plugin/sql/src/main/resources/META-INF/services/org.elasticsearch.painless.spi.PainlessExtension @@ -0,0 +1 @@ +org.elasticsearch.xpack.sql.plugin.SqlPainlessExtension \ No newline at end of file diff --git a/x-pack/plugin/sql/src/main/resources/org/elasticsearch/xpack/sql/plugin/sql_whitelist.txt b/x-pack/plugin/sql/src/main/resources/org/elasticsearch/xpack/sql/plugin/sql_whitelist.txt new file mode 100644 index 0000000000000..f1fba2f9bd80c --- /dev/null +++ b/x-pack/plugin/sql/src/main/resources/org/elasticsearch/xpack/sql/plugin/sql_whitelist.txt @@ -0,0 +1,12 @@ +# +# 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 SQL specific utilities available inside SQL scripting + +class org.elasticsearch.xpack.sql.expression.function.scalar.whitelist.SqlScripts { + + Integer dateTimeChrono(long, String, String) +} \ No newline at end of file diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/sql/whitelist.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/sql/whitelist.yml new file mode 100644 index 0000000000000..aad463fd80422 --- /dev/null +++ b/x-pack/plugin/src/test/resources/rest-api-spec/test/sql/whitelist.yml @@ -0,0 +1,27 @@ +--- +"SQL methods available in Painless": + - do: + bulk: + refresh: true + body: + - index: + _index: test + _type: doc + _id: 1 + - str: test1 + int: 1 + - do: + index: test + search: + body: + query: + match_all: {} + script_fields: + sNum1: + script: + source: "return + SqlScripts.dateTimeChrono(doc['int'].value, 'UTC', 'YEAR')" + lang: painless + + - match: { hits.total: 1 } + - match: { hits.hits.0.fields.sNum1.0: 1970 } From b63a3e255f59532d67392f49c2ebd61c93286920 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Thu, 17 May 2018 20:36:34 +0300 Subject: [PATCH 2/6] Remove ANTLR --- .../licenses/antlr4-runtime-4.5.3.jar.sha1 | 1 - .../sql/licenses/antlr4-runtime-LICENSE.txt | 26 ------------------- .../sql/licenses/antlr4-runtime-NOTICE.txt | 0 3 files changed, 27 deletions(-) delete mode 100644 x-pack/plugin/sql/licenses/antlr4-runtime-4.5.3.jar.sha1 delete mode 100644 x-pack/plugin/sql/licenses/antlr4-runtime-LICENSE.txt delete mode 100644 x-pack/plugin/sql/licenses/antlr4-runtime-NOTICE.txt diff --git a/x-pack/plugin/sql/licenses/antlr4-runtime-4.5.3.jar.sha1 b/x-pack/plugin/sql/licenses/antlr4-runtime-4.5.3.jar.sha1 deleted file mode 100644 index 535955b7d6826..0000000000000 --- a/x-pack/plugin/sql/licenses/antlr4-runtime-4.5.3.jar.sha1 +++ /dev/null @@ -1 +0,0 @@ -2609e36f18f7e8d593cc1cddfb2ac776dc96b8e0 \ No newline at end of file diff --git a/x-pack/plugin/sql/licenses/antlr4-runtime-LICENSE.txt b/x-pack/plugin/sql/licenses/antlr4-runtime-LICENSE.txt deleted file mode 100644 index 95d0a2554f686..0000000000000 --- a/x-pack/plugin/sql/licenses/antlr4-runtime-LICENSE.txt +++ /dev/null @@ -1,26 +0,0 @@ -[The "BSD license"] -Copyright (c) 2015 Terence Parr, Sam Harwell -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. The name of the author may not be used to endorse or promote products - derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR -IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES -OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. -IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF -THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/x-pack/plugin/sql/licenses/antlr4-runtime-NOTICE.txt b/x-pack/plugin/sql/licenses/antlr4-runtime-NOTICE.txt deleted file mode 100644 index e69de29bb2d1d..0000000000000 From c60e901e1e4fb107010545267f795b398ddf041d Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Mon, 11 Jun 2018 18:55:32 +0300 Subject: [PATCH 3/6] Rename SqlScripts to InternalSqlScript Added Internal in the name to better indicate that it should _not_ be used externally --- .../expression/function/scalar/script/ScriptTemplate.java | 2 +- .../whitelist/{SqlScripts.java => InternalSqlScripts.java} | 6 +++--- .../org/elasticsearch/xpack/sql/plugin/sql_whitelist.txt | 2 +- .../src/test/resources/rest-api-spec/test/sql/whitelist.yml | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) rename x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/{SqlScripts.java => InternalSqlScripts.java} (79%) diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/script/ScriptTemplate.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/script/ScriptTemplate.java index 316ca00a28199..3cb35aa261f23 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/script/ScriptTemplate.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/script/ScriptTemplate.java @@ -92,6 +92,6 @@ public String toString() { } public static String formatTemplate(String template) { - return template.replace("{sql}", "SqlScripts").replace("{}", "params.%s"); + return template.replace("{sql}", "InternalSqlScripts").replace("{}", "params.%s"); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/SqlScripts.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScripts.java similarity index 79% rename from x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/SqlScripts.java rename to x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScripts.java index e8a9bceb1ed7a..ec158b00c7ab2 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/SqlScripts.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScripts.java @@ -9,12 +9,12 @@ /** * Whitelisted class for SQL scripts. - * Acts as a registry of the various static methods used internally by the scalar functions + * Acts as a registry of the various static methods used internally by the scalar functions * (to simplify the whitelist definition). */ -public final class SqlScripts { +public final class InternalSqlScripts { - private SqlScripts() {} + private InternalSqlScripts() {} public static Integer dateTimeChrono(long millis, String tzId, String chronoName) { return DateTimeFunction.dateTimeChrono(millis, tzId, chronoName); diff --git a/x-pack/plugin/sql/src/main/resources/org/elasticsearch/xpack/sql/plugin/sql_whitelist.txt b/x-pack/plugin/sql/src/main/resources/org/elasticsearch/xpack/sql/plugin/sql_whitelist.txt index f1fba2f9bd80c..596d30ac7a0a8 100644 --- a/x-pack/plugin/sql/src/main/resources/org/elasticsearch/xpack/sql/plugin/sql_whitelist.txt +++ b/x-pack/plugin/sql/src/main/resources/org/elasticsearch/xpack/sql/plugin/sql_whitelist.txt @@ -6,7 +6,7 @@ # This file contains a whitelist for SQL specific utilities available inside SQL scripting -class org.elasticsearch.xpack.sql.expression.function.scalar.whitelist.SqlScripts { +class org.elasticsearch.xpack.sql.expression.function.scalar.whitelist.InternalSqlScripts { Integer dateTimeChrono(long, String, String) } \ No newline at end of file diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/sql/whitelist.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/sql/whitelist.yml index aad463fd80422..9db9dd69d2a8f 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/test/sql/whitelist.yml +++ b/x-pack/plugin/src/test/resources/rest-api-spec/test/sql/whitelist.yml @@ -20,7 +20,7 @@ sNum1: script: source: "return - SqlScripts.dateTimeChrono(doc['int'].value, 'UTC', 'YEAR')" + InternalSqlScripts.dateTimeChrono(doc['int'].value, 'UTC', 'YEAR')" lang: painless - match: { hits.total: 1 } From 5b2a86b4bb122411a7d74463007cbb5d5e8b16d2 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Tue, 12 Jun 2018 17:57:57 +0300 Subject: [PATCH 4/6] Add whitelisting to the script sort context as well --- .../xpack/sql/plugin/SqlPainlessExtension.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPainlessExtension.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPainlessExtension.java index d67c3aea4f0a0..4697f7fdcc402 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPainlessExtension.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPainlessExtension.java @@ -25,9 +25,11 @@ public class SqlPainlessExtension implements PainlessExtension { @Override public Map, List> getContextWhitelists() { Map, List> whitelist = new LinkedHashMap<>(); - whitelist.put(FilterScript.CONTEXT, singletonList(WHITELIST)); - whitelist.put(SearchScript.CONTEXT, singletonList(WHITELIST)); - whitelist.put(SearchScript.AGGS_CONTEXT, singletonList(WHITELIST)); + List list = singletonList(WHITELIST); + whitelist.put(FilterScript.CONTEXT, list); + whitelist.put(SearchScript.AGGS_CONTEXT, list); + whitelist.put(SearchScript.CONTEXT, list); + whitelist.put(SearchScript.SCRIPT_SORT_CONTEXT, list); return whitelist; } } From d32b40b38210f44c6ab848e29bb27c57db781b48 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Tue, 12 Jun 2018 23:49:34 +0300 Subject: [PATCH 5/6] Rename whitelisted class Add a more verbose name to better convey the class intent --- .../sql/expression/function/scalar/script/ScriptTemplate.java | 3 ++- .../{InternalSqlScripts.java => InternalSqlScriptUtils.java} | 4 ++-- .../org/elasticsearch/xpack/sql/plugin/sql_whitelist.txt | 2 +- .../src/test/resources/rest-api-spec/test/sql/whitelist.yml | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) rename x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/{InternalSqlScripts.java => InternalSqlScriptUtils.java} (90%) diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/script/ScriptTemplate.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/script/ScriptTemplate.java index 3cb35aa261f23..35b7680dcca78 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/script/ScriptTemplate.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/script/ScriptTemplate.java @@ -7,6 +7,7 @@ import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; +import org.elasticsearch.xpack.sql.expression.function.scalar.whitelist.InternalSqlScriptUtils; import org.elasticsearch.xpack.sql.type.DataType; import org.elasticsearch.xpack.sql.util.StringUtils; @@ -92,6 +93,6 @@ public String toString() { } public static String formatTemplate(String template) { - return template.replace("{sql}", "InternalSqlScripts").replace("{}", "params.%s"); + return template.replace("{sql}", InternalSqlScriptUtils.class.getSimpleName()).replace("{}", "params.%s"); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScripts.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScriptUtils.java similarity index 90% rename from x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScripts.java rename to x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScriptUtils.java index ec158b00c7ab2..802aa4a7c09bb 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScripts.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScriptUtils.java @@ -12,9 +12,9 @@ * Acts as a registry of the various static methods used internally by the scalar functions * (to simplify the whitelist definition). */ -public final class InternalSqlScripts { +public final class InternalSqlScriptUtils { - private InternalSqlScripts() {} + private InternalSqlScriptUtils() {} public static Integer dateTimeChrono(long millis, String tzId, String chronoName) { return DateTimeFunction.dateTimeChrono(millis, tzId, chronoName); diff --git a/x-pack/plugin/sql/src/main/resources/org/elasticsearch/xpack/sql/plugin/sql_whitelist.txt b/x-pack/plugin/sql/src/main/resources/org/elasticsearch/xpack/sql/plugin/sql_whitelist.txt index 596d30ac7a0a8..8dae4f8c0d1d6 100644 --- a/x-pack/plugin/sql/src/main/resources/org/elasticsearch/xpack/sql/plugin/sql_whitelist.txt +++ b/x-pack/plugin/sql/src/main/resources/org/elasticsearch/xpack/sql/plugin/sql_whitelist.txt @@ -6,7 +6,7 @@ # This file contains a whitelist for SQL specific utilities available inside SQL scripting -class org.elasticsearch.xpack.sql.expression.function.scalar.whitelist.InternalSqlScripts { +class org.elasticsearch.xpack.sql.expression.function.scalar.whitelist.InternalSqlScriptUtils { Integer dateTimeChrono(long, String, String) } \ No newline at end of file diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/sql/whitelist.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/sql/whitelist.yml index 9db9dd69d2a8f..27a3f8ea5107f 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/test/sql/whitelist.yml +++ b/x-pack/plugin/src/test/resources/rest-api-spec/test/sql/whitelist.yml @@ -20,7 +20,7 @@ sNum1: script: source: "return - InternalSqlScripts.dateTimeChrono(doc['int'].value, 'UTC', 'YEAR')" + InternalSqlScriptUtils.dateTimeChrono(doc['int'].value, 'UTC', 'YEAR')" lang: painless - match: { hits.total: 1 } From 83d1bbcc2c1708e053de6b20bd5fe7dea6306af0 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Wed, 13 Jun 2018 18:37:49 +0300 Subject: [PATCH 6/6] Address feedback --- .../sql/plugin/SqlPainlessExtension.java | 4 +-- .../rest-api-spec/test/sql/whitelist.yml | 27 ------------------- 2 files changed, 2 insertions(+), 29 deletions(-) delete mode 100644 x-pack/plugin/src/test/resources/rest-api-spec/test/sql/whitelist.yml diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPainlessExtension.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPainlessExtension.java index 4697f7fdcc402..426d725ac79d8 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPainlessExtension.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPainlessExtension.java @@ -12,7 +12,7 @@ import org.elasticsearch.script.ScriptContext; import org.elasticsearch.script.SearchScript; -import java.util.LinkedHashMap; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -24,7 +24,7 @@ public class SqlPainlessExtension implements PainlessExtension { @Override public Map, List> getContextWhitelists() { - Map, List> whitelist = new LinkedHashMap<>(); + Map, List> whitelist = new HashMap<>(); List list = singletonList(WHITELIST); whitelist.put(FilterScript.CONTEXT, list); whitelist.put(SearchScript.AGGS_CONTEXT, list); diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/sql/whitelist.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/sql/whitelist.yml deleted file mode 100644 index 27a3f8ea5107f..0000000000000 --- a/x-pack/plugin/src/test/resources/rest-api-spec/test/sql/whitelist.yml +++ /dev/null @@ -1,27 +0,0 @@ ---- -"SQL methods available in Painless": - - do: - bulk: - refresh: true - body: - - index: - _index: test - _type: doc - _id: 1 - - str: test1 - int: 1 - - do: - index: test - search: - body: - query: - match_all: {} - script_fields: - sNum1: - script: - source: "return - InternalSqlScriptUtils.dateTimeChrono(doc['int'].value, 'UTC', 'YEAR')" - lang: painless - - - match: { hits.total: 1 } - - match: { hits.hits.0.fields.sNum1.0: 1970 }