Skip to content

Commit 4a8850a

Browse files
committed
Support using specified timefield in per functions
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent e07cd19 commit 4a8850a

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

core/src/main/java/org/opensearch/sql/ast/tree/Chart.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,14 @@ private UnresolvedPlan transformPerFunction() {
9797

9898
PerFunction perFunc = perFuncOpt.get();
9999
// For chart, the rowSplit should contain the span information
100-
UnresolvedExpression spanExpr = rowSplit;
101-
if (rowSplit instanceof Alias) {
102-
spanExpr = ((Alias) rowSplit).getDelegated();
103-
}
100+
UnresolvedExpression spanExpr =
101+
rowSplit instanceof Alias ? ((Alias) rowSplit).getDelegated() : rowSplit;
104102
if (!(spanExpr instanceof Span)) {
105103
return this; // Cannot transform without span information
106104
}
107105

108106
Span span = (Span) spanExpr;
109-
Field spanStartTime = AstDSL.implicitTimestampField();
107+
Field spanStartTime = (Field) span.getField();
110108
Function spanEndTime = timestampadd(span.getUnit(), span.getValue(), spanStartTime);
111109
Function spanMillis = timestampdiff(MILLISECOND, spanStartTime, spanEndTime);
112110
final int SECOND_IN_MILLISECOND = 1000;

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalciteTimechartPerFunctionIT.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package org.opensearch.sql.calcite.remote;
77

8+
import static org.opensearch.sql.util.MatcherUtils.closeTo;
89
import static org.opensearch.sql.util.MatcherUtils.rows;
910
import static org.opensearch.sql.util.MatcherUtils.schema;
1011
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows;
@@ -13,6 +14,8 @@
1314
import java.io.IOException;
1415
import org.json.JSONObject;
1516
import org.junit.jupiter.api.Test;
17+
import org.opensearch.sql.common.utils.StringUtils;
18+
import org.opensearch.sql.legacy.TestsConstants;
1619
import org.opensearch.sql.ppl.PPLIntegTestCase;
1720

1821
public class CalciteTimechartPerFunctionIT extends PPLIntegTestCase {
@@ -24,6 +27,7 @@ public void init() throws Exception {
2427
disallowCalciteFallback();
2528

2629
loadIndex(Index.EVENTS_TRAFFIC);
30+
loadIndex(Index.BANK);
2731
}
2832

2933
@Test
@@ -208,4 +212,26 @@ public void testTimechartPerDayWithByClause() throws IOException {
208212
rows("2025-09-08 10:02:00", "server1", 43200.0), // 60 * 720
209213
rows("2025-09-08 10:02:00", "server2", 129600.0)); // 180 * 720
210214
}
215+
216+
@Test
217+
public void testTimechartPerMonthWithSpecifiedSpan() throws IOException {
218+
JSONObject result =
219+
executeQuery(
220+
StringUtils.format(
221+
"source=%s | timechart timefield=birthdate span=1month per_day(balance) by gender",
222+
TestsConstants.TEST_INDEX_BANK));
223+
verifySchema(
224+
result,
225+
schema("birthdate", "timestamp"),
226+
schema("gender", "string"),
227+
schema("per_day(balance)", "double"));
228+
verifyDataRows(
229+
result,
230+
closeTo("2017-10-01 00:00:00", "M", 1265.3225806451612),
231+
closeTo("2017-11-01 00:00:00", "M", 189.53333333333333),
232+
closeTo("2018-06-01 00:00:00", "F", 1094.6),
233+
closeTo("2018-06-01 00:00:00", "M", 547.2666666666667),
234+
closeTo("2018-08-01 00:00:00", "F", 2858.9032258064517),
235+
closeTo("2018-11-01 00:00:00", "M", 139.33333333333334));
236+
}
211237
}

0 commit comments

Comments
 (0)