From 06d124d2766e66ce2491b7af1c9cd3d7854f1973 Mon Sep 17 00:00:00 2001 From: Fang Xing Date: Wed, 17 Jan 2024 18:21:04 -0500 Subject: [PATCH 1/7] Allow both string and datetime as the third and fourth inputs to auto_bucket Committer: Fang Xing --- .../function/scalar/math/AutoBucket.java | 42 +++++++++++++++++-- .../function/scalar/math/AutoBucketTests.java | 3 +- .../xpack/ql/expression/TypeResolutions.java | 2 +- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java index 27abeb44b2ff0..68a8a1d4a462f 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java @@ -30,13 +30,19 @@ import org.elasticsearch.xpack.ql.type.DataTypes; import java.util.List; +import java.util.Locale; import java.util.function.BiFunction; import java.util.function.Function; +import static org.elasticsearch.common.logging.LoggerMessageFormat.format; +import static org.elasticsearch.xpack.ql.expression.Expressions.name; +import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.DEFAULT; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.FIRST; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.FOURTH; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.SECOND; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.THIRD; +import static org.elasticsearch.xpack.ql.expression.TypeResolutions.acceptedTypesForErrorMsg; +import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isDate; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isFoldable; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isInteger; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isNumeric; @@ -113,8 +119,8 @@ public ExpressionEvaluator.Factory toEvaluator(Function isString(e, sourceText(), o)); + return resolveType((e, o) -> isStringOrDate(e, sourceText(), o)); } if (field.dataType().isNumeric()) { return resolveType((e, o) -> isNumeric(e, sourceText(), o)); @@ -214,6 +220,36 @@ private TypeResolution resolveType(BiFunction parameters() { }))); } + @SuppressWarnings("checkstyle:LineLength") private Expression build(Source source, Expression arg) { Literal from; Literal to; if (arg.dataType() == DataTypes.DATETIME) { from = new Literal(Source.EMPTY, new BytesRef("2023-02-01T00:00:00.00Z"), DataTypes.KEYWORD); - to = new Literal(Source.EMPTY, new BytesRef("2023-03-01T00:00:00.00Z"), DataTypes.KEYWORD); + to = new Literal(Source.EMPTY, DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parseMillis("2023-03-01T09:00:00.00Z"), DataTypes.DATETIME); } else { from = new Literal(Source.EMPTY, 0, DataTypes.DOUBLE); to = new Literal(Source.EMPTY, 1000, DataTypes.DOUBLE); diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/TypeResolutions.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/TypeResolutions.java index af7f61d60fdae..4f424560e8bff 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/TypeResolutions.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/TypeResolutions.java @@ -170,7 +170,7 @@ public static TypeResolution isType( ); } - private static String acceptedTypesForErrorMsg(String... acceptedTypes) { + public static String acceptedTypesForErrorMsg(String... acceptedTypes) { StringJoiner sj = new StringJoiner(", "); for (int i = 0; i < acceptedTypes.length - 1; i++) { sj.add(acceptedTypes[i]); From f5c25c54da3710dc8fdd947206e7166e615c5538 Mon Sep 17 00:00:00 2001 From: Fang Xing Date: Fri, 19 Jan 2024 09:12:33 -0500 Subject: [PATCH 2/7] Allow both string and datetime as the third and fourth inputs to auto_bucket --- .../function/scalar/math/AutoBucket.java | 37 +++---------------- .../function/scalar/math/AutoBucketTests.java | 6 ++- .../xpack/ql/expression/TypeResolutions.java | 21 ++++++++++- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java index e8e7e59dfba8a..bea4a9ae7fac3 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java @@ -30,23 +30,17 @@ import org.elasticsearch.xpack.ql.type.DataTypes; import java.util.List; -import java.util.Locale; import java.util.function.BiFunction; import java.util.function.Function; -import static org.elasticsearch.common.logging.LoggerMessageFormat.format; -import static org.elasticsearch.xpack.ql.expression.Expressions.name; -import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.DEFAULT; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.FIRST; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.FOURTH; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.SECOND; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.THIRD; -import static org.elasticsearch.xpack.ql.expression.TypeResolutions.acceptedTypesForErrorMsg; -import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isDate; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isFoldable; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isInteger; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isNumeric; -import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isString; +import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isStringOrDate; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isType; /** @@ -122,7 +116,8 @@ public ExpressionEvaluator.Factory toEvaluator(Function Date: Sun, 21 Jan 2024 13:43:51 -0500 Subject: [PATCH 3/7] Allow both string and datetime as the third and fourth inputs to auto_bucket --- .../function/scalar/math/AutoBucket.java | 1 - .../function/scalar/math/AutoBucketTests.java | 16 +++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java index bea4a9ae7fac3..504285a253749 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java @@ -117,7 +117,6 @@ public ExpressionEvaluator.Factory toEvaluator(Function parameters() { }))); } - @SuppressWarnings("checkstyle:LineLength") private Expression build(Source source, Expression arg) { Literal from; Literal to; if (arg.dataType() == DataTypes.DATETIME) { - from = new Literal(Source.EMPTY, new BytesRef("2023-02-01T00:00:00.00Z"), DataTypes.KEYWORD); - to = new Literal( - Source.EMPTY, - DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parseMillis("2023-03-01T09:00:00.00Z"), - DataTypes.DATETIME - ); + from = fromOrTo("2023-02-01T00:00:00.00Z"); + to = fromOrTo("2023-03-01T09:00:00.00Z"); } else { from = new Literal(Source.EMPTY, 0, DataTypes.DOUBLE); to = new Literal(Source.EMPTY, 1000, DataTypes.DOUBLE); @@ -98,6 +93,13 @@ private Expression build(Source source, Expression arg) { return new AutoBucket(source, arg, new Literal(Source.EMPTY, 50, DataTypes.INTEGER), from, to); } + private Literal fromOrTo(String date) { + if (randomBoolean()) { + return new Literal(Source.EMPTY, new BytesRef(date), randomBoolean() ? DataTypes.KEYWORD : DataTypes.TEXT); + } + return new Literal(Source.EMPTY, DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parseMillis(date), DataTypes.DATETIME); + } + @Override protected DataType expectedType(List argTypes) { if (argTypes.get(0).isNumeric()) { From 663b5c6c7162d466c26c5333d9adb55bb2ae5be5 Mon Sep 17 00:00:00 2001 From: Fang Xing Date: Mon, 22 Jan 2024 17:12:06 -0500 Subject: [PATCH 4/7] Allow both string and datetime as the third and fourth inputs to auto_bucket --- .../function/scalar/math/AutoBucket.java | 32 ++++++++++++------- .../xpack/ql/expression/TypeResolutions.java | 19 ----------- 2 files changed, 20 insertions(+), 31 deletions(-) diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java index 504285a253749..aef488ed00eb6 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.esql.expression.function.scalar.math; -import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.Rounding; +import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.mapper.DateFieldMapper; @@ -20,6 +20,7 @@ import org.elasticsearch.xpack.esql.expression.predicate.operator.arithmetic.Div; import org.elasticsearch.xpack.esql.expression.predicate.operator.arithmetic.Mul; import org.elasticsearch.xpack.ql.expression.Expression; +import org.elasticsearch.xpack.ql.expression.Foldables; import org.elasticsearch.xpack.ql.expression.Literal; import org.elasticsearch.xpack.ql.expression.TypeResolutions; import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction; @@ -40,7 +41,6 @@ import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isFoldable; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isInteger; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isNumeric; -import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isStringOrDate; import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isType; /** @@ -115,8 +115,8 @@ public ExpressionEvaluator.Factory toEvaluator(Function { return DataTypes.isString(exp) || DataTypes.isDateTime(exp); }, + operationName, + paramOrd, + "datetime", + "string" + ); + } + + private long convertToLong(Expression e) { + Object value = Foldables.valueOf(e); + return (e.dataType() == DataTypes.DATETIME) + ? ((Number) value).longValue() + : DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parseMillis(BytesRefs.toString(value)); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/TypeResolutions.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/TypeResolutions.java index a8de8b62c4bbc..af7f61d60fdae 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/TypeResolutions.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/TypeResolutions.java @@ -118,25 +118,6 @@ public static TypeResolution isIPAndExact(Expression e, String operationName, Pa return isExact(e, operationName, paramOrd); } - public static TypeResolution isStringOrDate(Expression e, String operationName, TypeResolutions.ParamOrdinal paramOrd) { - TypeResolution resolution = isDate(e, operationName, paramOrd); - resolution = resolution.unresolved() ? isString(e, operationName, paramOrd) : resolution; - if (resolution.unresolved()) { - return new TypeResolution( - format( - null, - "{} argument of [{}] must be [{}], found value [{}] type [{}]", - paramOrd == null || paramOrd == DEFAULT ? "" : paramOrd.name().toLowerCase(Locale.ROOT) + " ", - operationName, - acceptedTypesForErrorMsg("string", "datetime"), - name(e), - e.dataType().typeName() - ) - ); - } - return resolution; - } - public static TypeResolution isFoldable(Expression e, String operationName, ParamOrdinal paramOrd) { if (e.foldable() == false) { return new TypeResolution( From 6a19d6ea1cd9228f122769011ea29eedb3b11db0 Mon Sep 17 00:00:00 2001 From: Fang Xing Date: Mon, 22 Jan 2024 17:17:23 -0500 Subject: [PATCH 5/7] Allow both string and datetime as the third and fourth inputs to auto_bucket --- .../xpack/esql/expression/function/scalar/math/AutoBucket.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java index aef488ed00eb6..be50e62eadf2a 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java @@ -229,7 +229,7 @@ public static TypeResolution isStringOrDate(Expression e, String operationName, private long convertToLong(Expression e) { Object value = Foldables.valueOf(e); - return (e.dataType() == DataTypes.DATETIME) + return DataTypes.isDateTime(e.dataType()) ? ((Number) value).longValue() : DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parseMillis(BytesRefs.toString(value)); } From 5d21491a07bccaaf408bfc2c8636dffd20b17a55 Mon Sep 17 00:00:00 2001 From: Fang Xing Date: Tue, 23 Jan 2024 13:11:01 -0500 Subject: [PATCH 6/7] Allow both string and datetime as the third and fourth inputs to auto_bucket --- .../src/main/resources/date.csv-spec | 19 +++++++++++++++---- .../src/main/resources/show.csv-spec | 4 ++-- .../function/scalar/math/AutoBucket.java | 12 ++++++------ .../function/scalar/math/AutoBucketTests.java | 6 +++--- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/x-pack/plugin/esql/qa/testFixtures/src/main/resources/date.csv-spec b/x-pack/plugin/esql/qa/testFixtures/src/main/resources/date.csv-spec index 8dd9704fd2d4b..89f7012a399f6 100644 --- a/x-pack/plugin/esql/qa/testFixtures/src/main/resources/date.csv-spec +++ b/x-pack/plugin/esql/qa/testFixtures/src/main/resources/date.csv-spec @@ -315,6 +315,17 @@ from employees | where birth_date > now() | sort emp_no asc | keep emp_no, birth emp_no:integer | birth_date:date ; +autoBucketYearInAgg +FROM employees +| WHERE hire_date >= "1999-01-01T00:00:00Z" +| EVAL bucket = AUTO_BUCKET(hire_date, 5, "1999-01-01T00:00:00Z", NOW()) +| STATS COUNT(*) by bucket +| sort bucket; + +COUNT(*):long | bucket:date +1 | 1999-01-01T00:00:00.000Z +; + autoBucketMonthInAgg // tag::auto_bucket_in_agg[] @@ -910,7 +921,7 @@ docsAutoBucketLast24hr //tag::docsAutoBucketLast24hr[] FROM sample_data | WHERE @timestamp >= NOW() - 1 day and @timestamp < NOW() -| EVAL bucket = AUTO_BUCKET(@timestamp, 25, DATE_FORMAT(NOW() - 1 day), DATE_FORMAT(NOW())) +| EVAL bucket = AUTO_BUCKET(@timestamp, 25, NOW() - 1 day, NOW()) | STATS COUNT(*) BY bucket //end::docsAutoBucketLast24hr[] ; @@ -922,7 +933,7 @@ docsGettingStartedAutoBucket // tag::gs-auto_bucket[] FROM sample_data | KEEP @timestamp -| EVAL bucket = AUTO_BUCKET (@timestamp, 24, "2023-10-23T00:00:00Z", "2023-10-23T23:59:59Z") +| EVAL bucket = AUTO_BUCKET(@timestamp, 24, "2023-10-23T00:00:00Z", NOW()) // end::gs-auto_bucket[] | LIMIT 0 ; @@ -934,7 +945,7 @@ docsGettingStartedAutoBucketStatsBy // tag::gs-auto_bucket-stats-by[] FROM sample_data | KEEP @timestamp, event_duration -| EVAL bucket = AUTO_BUCKET (@timestamp, 24, "2023-10-23T00:00:00Z", "2023-10-23T23:59:59Z") +| EVAL bucket = AUTO_BUCKET(@timestamp, 24, "2023-10-23T00:00:00Z", "2023-10-23T23:59:59Z") | STATS COUNT(*) BY bucket // end::gs-auto_bucket-stats-by[] | SORT bucket @@ -949,7 +960,7 @@ docsGettingStartedAutoBucketStatsByMedian // tag::gs-auto_bucket-stats-by-median[] FROM sample_data | KEEP @timestamp, event_duration -| EVAL bucket = AUTO_BUCKET (@timestamp, 24, "2023-10-23T00:00:00Z", "2023-10-23T23:59:59Z") +| EVAL bucket = AUTO_BUCKET(@timestamp, 24, "2023-10-23T00:00:00Z", "2023-10-23T23:59:59Z") | STATS median_duration = MEDIAN(event_duration) BY bucket // end::gs-auto_bucket-stats-by-median[] | SORT bucket diff --git a/x-pack/plugin/esql/qa/testFixtures/src/main/resources/show.csv-spec b/x-pack/plugin/esql/qa/testFixtures/src/main/resources/show.csv-spec index 754d4a0e156cf..8c8728885d536 100644 --- a/x-pack/plugin/esql/qa/testFixtures/src/main/resources/show.csv-spec +++ b/x-pack/plugin/esql/qa/testFixtures/src/main/resources/show.csv-spec @@ -15,7 +15,7 @@ acos |"double acos(n:double|integer|long|unsigned_long)" asin |"double asin(n:double|integer|long|unsigned_long)"|n |"double|integer|long|unsigned_long" | "" |double | "Inverse sine trigonometric function." | false | false | false atan |"double atan(n:double|integer|long|unsigned_long)" |n |"double|integer|long|unsigned_long" | "" |double | "Inverse tangent trigonometric function." | false | false | false atan2 |"double atan2(y:double|integer|long|unsigned_long, x:double|integer|long|unsigned_long)" |[y, x] |["double|integer|long|unsigned_long", "double|integer|long|unsigned_long"] |["", ""] |double | "The angle between the positive x-axis and the ray from the origin to the point (x , y) in the Cartesian plane." | [false, false] | false | false -auto_bucket |"double|date auto_bucket(field:integer|long|double|date, buckets:integer, from:integer|long|double|date, to:integer|long|double|date)" |[field, buckets, from, to] |["integer|long|double|date", "integer", "integer|long|double|date", "integer|long|double|date"] |["", "", "", ""] | "double|date" | "Creates human-friendly buckets and returns a datetime value for each row that corresponds to the resulting bucket the row falls into." | [false, false, false, false] | false | false +auto_bucket |"double|date auto_bucket(field:integer|long|double|date, buckets:integer, from:integer|long|double|date|string, to:integer|long|double|date|string)" |[field, buckets, from, to] |["integer|long|double|date", "integer", "integer|long|double|date|string", "integer|long|double|date|string"] |["", "", "", ""] | "double|date" | "Creates human-friendly buckets and returns a datetime value for each row that corresponds to the resulting bucket the row falls into." | [false, false, false, false] | false | false avg |"double avg(field:double|integer|long|unsigned_long)" |field |"double|integer|long|unsigned_long" | "" |double | "The average of a numeric field." | false | false | true case |"boolean|cartesian_point|date|double|geo_point|integer|ip|keyword|long|text|unsigned_long|version case(condition:boolean, rest...:boolean|cartesian_point|date|double|geo_point|integer|ip|keyword|long|text|unsigned_long|version)" |[condition, rest] |["boolean", "boolean|cartesian_point|date|double|geo_point|integer|ip|keyword|long|text|unsigned_long|version"] |["", ""] |"boolean|cartesian_point|date|double|geo_point|integer|ip|keyword|long|text|unsigned_long|version" | "Accepts pairs of conditions and values. The function returns the value that belongs to the first condition that evaluates to true." | [false, false] | true | false ceil |"double|integer|long|unsigned_long ceil(n:double|integer|long|unsigned_long)" |n |"double|integer|long|unsigned_long" | "" | "double|integer|long|unsigned_long" | "Round a number up to the nearest integer." | false | false | false @@ -110,7 +110,7 @@ synopsis:keyword "double asin(n:double|integer|long|unsigned_long)" "double atan(n:double|integer|long|unsigned_long)" "double atan2(y:double|integer|long|unsigned_long, x:double|integer|long|unsigned_long)" -"double|date auto_bucket(field:integer|long|double|date, buckets:integer, from:integer|long|double|date, to:integer|long|double|date)" +"double|date auto_bucket(field:integer|long|double|date, buckets:integer, from:integer|long|double|date|string, to:integer|long|double|date|string)" "double avg(field:double|integer|long|unsigned_long)" "boolean|cartesian_point|date|double|geo_point|integer|ip|keyword|long|text|unsigned_long|version case(condition:boolean, rest...:boolean|cartesian_point|date|double|geo_point|integer|ip|keyword|long|text|unsigned_long|version)" "double|integer|long|unsigned_long ceil(n:double|integer|long|unsigned_long)" diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java index be50e62eadf2a..6a8b3f41a9c65 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucket.java @@ -90,8 +90,8 @@ public AutoBucket( Source source, @Param(name = "field", type = { "integer", "long", "double", "date" }) Expression field, @Param(name = "buckets", type = { "integer" }) Expression buckets, - @Param(name = "from", type = { "integer", "long", "double", "date" }) Expression from, - @Param(name = "to", type = { "integer", "long", "double", "date" }) Expression to + @Param(name = "from", type = { "integer", "long", "double", "date", "string" }) Expression from, + @Param(name = "to", type = { "integer", "long", "double", "date", "string" }) Expression to ) { super(source, List.of(field, buckets, from, to)); this.field = field; @@ -115,8 +115,8 @@ public ExpressionEvaluator.Factory toEvaluator(Function { return DataTypes.isString(exp) || DataTypes.isDateTime(exp); }, + exp -> DataTypes.isString(exp) || DataTypes.isDateTime(exp), operationName, paramOrd, "datetime", @@ -227,7 +227,7 @@ public static TypeResolution isStringOrDate(Expression e, String operationName, ); } - private long convertToLong(Expression e) { + private long foldToLong(Expression e) { Object value = Foldables.valueOf(e); return DataTypes.isDateTime(e.dataType()) ? ((Number) value).longValue() diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucketTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucketTests.java index 193906143fb53..013753c801c39 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucketTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/AutoBucketTests.java @@ -84,8 +84,8 @@ private Expression build(Source source, Expression arg) { Literal from; Literal to; if (arg.dataType() == DataTypes.DATETIME) { - from = fromOrTo("2023-02-01T00:00:00.00Z"); - to = fromOrTo("2023-03-01T09:00:00.00Z"); + from = stringOrDateTime("2023-02-01T00:00:00.00Z"); + to = stringOrDateTime("2023-03-01T09:00:00.00Z"); } else { from = new Literal(Source.EMPTY, 0, DataTypes.DOUBLE); to = new Literal(Source.EMPTY, 1000, DataTypes.DOUBLE); @@ -93,7 +93,7 @@ private Expression build(Source source, Expression arg) { return new AutoBucket(source, arg, new Literal(Source.EMPTY, 50, DataTypes.INTEGER), from, to); } - private Literal fromOrTo(String date) { + private Literal stringOrDateTime(String date) { if (randomBoolean()) { return new Literal(Source.EMPTY, new BytesRef(date), randomBoolean() ? DataTypes.KEYWORD : DataTypes.TEXT); } From 49a012ae892da36d34fb0e97e25d81991bbbf6f1 Mon Sep 17 00:00:00 2001 From: Fang Xing Date: Thu, 25 Jan 2024 16:52:15 -0500 Subject: [PATCH 7/7] add skip tag to the 3 failed tests in #104776 --- .../esql/qa/testFixtures/src/main/resources/date.csv-spec | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x-pack/plugin/esql/qa/testFixtures/src/main/resources/date.csv-spec b/x-pack/plugin/esql/qa/testFixtures/src/main/resources/date.csv-spec index c32c4e3e2fd2c..cab9892f9afdf 100644 --- a/x-pack/plugin/esql/qa/testFixtures/src/main/resources/date.csv-spec +++ b/x-pack/plugin/esql/qa/testFixtures/src/main/resources/date.csv-spec @@ -315,7 +315,7 @@ from employees | where birth_date > now() | sort emp_no asc | keep emp_no, birth emp_no:integer | birth_date:date ; -autoBucketYearInAgg +autoBucketYearInAgg#[skip:-8.12.99, reason:date type is supported in 8.13] FROM employees | WHERE hire_date >= "1999-01-01T00:00:00Z" | EVAL bucket = AUTO_BUCKET(hire_date, 5, "1999-01-01T00:00:00Z", NOW()) @@ -917,7 +917,7 @@ FROM employees //end::docsAutoBucketWeeklyHistogram-result[] ; -docsAutoBucketLast24hr +docsAutoBucketLast24hr#[skip:-8.12.99, reason:date type is supported in 8.13] //tag::docsAutoBucketLast24hr[] FROM sample_data | WHERE @timestamp >= NOW() - 1 day and @timestamp < NOW() @@ -929,7 +929,7 @@ FROM sample_data COUNT(*):long | bucket:date ; -docsGettingStartedAutoBucket +docsGettingStartedAutoBucket#[skip:-8.12.99, reason:date type is supported in 8.13] // tag::gs-auto_bucket[] FROM sample_data | KEEP @timestamp