diff --git a/core/src/main/java/org/opensearch/sql/calcite/plan/LogicalSystemLimit.java b/core/src/main/java/org/opensearch/sql/calcite/plan/LogicalSystemLimit.java index c33854ebe52..6e46b63b976 100644 --- a/core/src/main/java/org/opensearch/sql/calcite/plan/LogicalSystemLimit.java +++ b/core/src/main/java/org/opensearch/sql/calcite/plan/LogicalSystemLimit.java @@ -65,16 +65,15 @@ private LogicalSystemLimit( } public static LogicalSystemLimit create(SystemLimitType type, RelNode input, RexNode fetch) { - return create(type, input, input.getTraitSet().getCollation(), null, fetch); + return create(type, input, null, fetch); } public static LogicalSystemLimit create( - SystemLimitType type, - RelNode input, - RelCollation collation, - @Nullable RexNode offset, - @Nullable RexNode fetch) { + SystemLimitType type, RelNode input, @Nullable RexNode offset, @Nullable RexNode fetch) { RelOptCluster cluster = input.getCluster(); + List collations = input.getTraitSet().getTraits(RelCollationTraitDef.INSTANCE); + // When there exists multiple sets of equivalent collations, we randomly select one + RelCollation collation = collations == null ? null : collations.get(0); collation = RelCollationTraitDef.INSTANCE.canonize(collation); RelTraitSet traitSet = input.getTraitSet().replace(Convention.NONE).replace(collation); return new LogicalSystemLimit(type, cluster, traitSet, input, collation, offset, fetch); diff --git a/integ-test/src/yamlRestTest/resources/rest-api-spec/test/issues/4644.yml b/integ-test/src/yamlRestTest/resources/rest-api-spec/test/issues/4644.yml new file mode 100644 index 00000000000..f04a1fb920d --- /dev/null +++ b/integ-test/src/yamlRestTest/resources/rest-api-spec/test/issues/4644.yml @@ -0,0 +1,88 @@ +setup: + - do: + indices.create: + index: timechart-eval-bug + body: + mappings: + properties: + "@timestamp": + type: date + "user": + type: keyword + - do: + bulk: + index: timechart-eval-bug + refresh: true + body: + - '{"index":{}}' + - '{"@timestamp":"2024-05-01T00:15:00Z","user":"alice"}' + - '{"index":{}}' + - '{"@timestamp":"2024-05-01T00:30:00Z","user":"bob"}' + - '{"index":{}}' + - '{"@timestamp":"2024-05-02T00:45:00Z","user":"alice"}' + - '{"index":{}}' + - '{"@timestamp":"2024-05-03T00:20:00Z","user":"bob"}' + +--- +"timechart with eval on @timestamp field": + - skip: + features: + - headers + - do: + headers: + Content-Type: 'application/json' + ppl: + body: + query: source=timechart-eval-bug | where HOUR(@timestamp) = 0 | timechart span=1d COUNT() by user | eval copyTimestamp = @timestamp + + - match: { total: 4 } + - match: { "schema": [ { "name": "@timestamp", "type": "timestamp" }, { "name": "user", "type": "string" }, { "name": "COUNT()", "type": "bigint" }, { "name": "copyTimestamp", "type": "timestamp" }] } + - match: {"datarows": [["2024-05-01 00:00:00", "alice", 1, "2024-05-01 00:00:00"], ["2024-05-01 00:00:00", "bob", 1, "2024-05-01 00:00:00"], ["2024-05-02 00:00:00", "alice", 1, "2024-05-02 00:00:00"], ["2024-05-03 00:00:00", "bob", 1, "2024-05-03 00:00:00"]]} + +--- +"timechart with eval on by field": + - skip: + features: + - headers + - do: + headers: + Content-Type: 'application/json' + ppl: + body: + query: source=timechart-eval-bug | where HOUR(@timestamp) = 0 | timechart span=1d COUNT() by user | eval copyUser = user + + - match: { total: 4 } + - match: { "schema": [ { "name": "@timestamp", "type": "timestamp" }, { "name": "user", "type": "string" }, { "name": "COUNT()", "type": "bigint" }, { "name": "copyUser", "type": "string" }] } + - match: {"datarows": [["2024-05-01 00:00:00", "alice", 1, "alice"], ["2024-05-01 00:00:00", "bob", 1, "bob"], ["2024-05-02 00:00:00", "alice", 1, "alice"], ["2024-05-03 00:00:00", "bob", 1, "bob"]]} + +--- +"timechart with eval on aggregated field": + - skip: + features: + - headers + - do: + headers: + Content-Type: 'application/json' + ppl: + body: + query: source=timechart-eval-bug | where HOUR(@timestamp) = 0 | timechart span=1d COUNT() by user | eval doubleCount = `COUNT()` * 2 + + - match: { total: 4 } + - match: { "schema": [ { "name": "@timestamp", "type": "timestamp" }, { "name": "user", "type": "string" }, { "name": "COUNT()", "type": "bigint" }, { "name": "doubleCount", "type": "bigint" }] } + - match: {"datarows": [["2024-05-01 00:00:00", "alice", 1, 2], ["2024-05-01 00:00:00", "bob", 1, 2], ["2024-05-02 00:00:00", "alice", 1, 2], ["2024-05-03 00:00:00", "bob", 1, 2]]} + +--- +"timechart with DATE_FORMAT on @timestamp": + - skip: + features: + - headers + - do: + headers: + Content-Type: 'application/json' + ppl: + body: + query: source=timechart-eval-bug | where HOUR(@timestamp) = 0 | timechart span=1d COUNT() by user | eval formatted_date = DATE_FORMAT(@timestamp, 'yyyy-MM-dd') + + - match: { total: 4 } + - match: { "schema": [ { "name": "@timestamp", "type": "timestamp" }, { "name": "user", "type": "string" }, { "name": "COUNT()", "type": "bigint" }, { "name": "formatted_date", "type": "string" }] } + - match: {"datarows": [["2024-05-01 00:00:00", "alice", 1, "2024-05-01"], ["2024-05-01 00:00:00", "bob", 1, "2024-05-01"], ["2024-05-02 00:00:00", "alice", 1, "2024-05-02"], ["2024-05-03 00:00:00", "bob", 1, "2024-05-03"]]}