1313import static org .opensearch .sql .ast .dsl .AstDSL .function ;
1414import static org .opensearch .sql .ast .dsl .AstDSL .intLiteral ;
1515import static org .opensearch .sql .ast .dsl .AstDSL .relation ;
16+ import static org .opensearch .sql .calcite .plan .OpenSearchConstants .IMPLICIT_FIELD_TIMESTAMP ;
1617
18+ import java .util .List ;
1719import java .util .stream .Stream ;
1820import org .junit .jupiter .api .Test ;
1921import org .junit .jupiter .params .ParameterizedTest ;
2022import org .junit .jupiter .params .provider .Arguments ;
2123import org .junit .jupiter .params .provider .MethodSource ;
2224import org .opensearch .sql .ast .dsl .AstDSL ;
2325import org .opensearch .sql .ast .expression .AggregateFunction ;
26+ import org .opensearch .sql .ast .expression .Argument ;
2427import org .opensearch .sql .ast .expression .Let ;
28+ import org .opensearch .sql .ast .expression .Literal ;
2529import org .opensearch .sql .ast .expression .Span ;
2630import org .opensearch .sql .ast .expression .SpanUnit ;
2731import org .opensearch .sql .ast .expression .UnresolvedExpression ;
32+ import org .opensearch .sql .expression .function .BuiltinFunctionName ;
2833
29- class TimechartTest {
30-
34+ class PerFunctionsTest {
3135 /**
3236 * @return test sources for per_* function test.
3337 */
@@ -57,8 +61,9 @@ void should_transform_per_second_for_different_spans(
5761 multiply ("per_second(bytes)" , 1000.0 ),
5862 timestampdiff (
5963 "MILLISECOND" ,
60- "@timestamp" ,
61- timestampadd (expectedIntervalUnit , spanValue , "@timestamp" )))),
64+ IMPLICIT_FIELD_TIMESTAMP ,
65+ timestampadd (
66+ expectedIntervalUnit , spanValue , IMPLICIT_FIELD_TIMESTAMP )))),
6267 timechart (span (spanValue , spanUnit ), alias ("per_second(bytes)" , sum ("bytes" )))));
6368 }
6469
@@ -76,8 +81,9 @@ void should_transform_per_minute_for_different_spans(
7681 multiply ("per_minute(bytes)" , 60000.0 ),
7782 timestampdiff (
7883 "MILLISECOND" ,
79- "@timestamp" ,
80- timestampadd (expectedIntervalUnit , spanValue , "@timestamp" )))),
84+ IMPLICIT_FIELD_TIMESTAMP ,
85+ timestampadd (
86+ expectedIntervalUnit , spanValue , IMPLICIT_FIELD_TIMESTAMP )))),
8187 timechart (span (spanValue , spanUnit ), alias ("per_minute(bytes)" , sum ("bytes" )))));
8288 }
8389
@@ -95,8 +101,9 @@ void should_transform_per_hour_for_different_spans(
95101 multiply ("per_hour(bytes)" , 3600000.0 ),
96102 timestampdiff (
97103 "MILLISECOND" ,
98- "@timestamp" ,
99- timestampadd (expectedIntervalUnit , spanValue , "@timestamp" )))),
104+ IMPLICIT_FIELD_TIMESTAMP ,
105+ timestampadd (
106+ expectedIntervalUnit , spanValue , IMPLICIT_FIELD_TIMESTAMP )))),
100107 timechart (span (spanValue , spanUnit ), alias ("per_hour(bytes)" , sum ("bytes" )))));
101108 }
102109
@@ -114,8 +121,9 @@ void should_transform_per_day_for_different_spans(
114121 multiply ("per_day(bytes)" , 8.64E7 ),
115122 timestampdiff (
116123 "MILLISECOND" ,
117- "@timestamp" ,
118- timestampadd (expectedIntervalUnit , spanValue , "@timestamp" )))),
124+ IMPLICIT_FIELD_TIMESTAMP ,
125+ timestampadd (
126+ expectedIntervalUnit , spanValue , IMPLICIT_FIELD_TIMESTAMP )))),
119127 timechart (span (spanValue , spanUnit ), alias ("per_day(bytes)" , sum ("bytes" )))));
120128 }
121129
@@ -128,19 +136,27 @@ void should_not_transform_non_per_functions() {
128136
129137 @ Test
130138 void should_preserve_all_fields_during_per_function_transformation () {
131- Timechart original =
132- new Timechart (relation ("logs" ), perSecond ("bytes" ))
133- .span (span (5 , "m" ))
134- .by (field ("status" ))
135- .limit (20 )
136- .useOther (false );
137-
138- Timechart expected =
139- new Timechart (relation ("logs" ), alias ("per_second(bytes)" , sum ("bytes" )))
140- .span (span (5 , "m" ))
141- .by (field ("status" ))
142- .limit (20 )
143- .useOther (false );
139+ Chart original =
140+ Chart .builder ()
141+ .child (relation ("logs" ))
142+ .aggregationFunction (perSecond ("bytes" ))
143+ .rowSplit (span (5 , "m" ))
144+ .columnSplit (field ("status" ))
145+ .arguments (
146+ List .of (
147+ new Argument ("limit" , intLiteral (20 )), new Argument ("useOther" , Literal .FALSE )))
148+ .build ();
149+
150+ Chart expected =
151+ Chart .builder ()
152+ .child (relation ("logs" ))
153+ .aggregationFunction (alias ("per_second(bytes)" , sum ("bytes" )))
154+ .rowSplit (span (5 , "m" ))
155+ .columnSplit (field ("status" ))
156+ .arguments (
157+ List .of (
158+ new Argument ("limit" , intLiteral (20 )), new Argument ("useOther" , Literal .FALSE )))
159+ .build ();
144160
145161 withTimechart (original )
146162 .whenTransformingPerFunction ()
@@ -151,7 +167,9 @@ void should_preserve_all_fields_during_per_function_transformation() {
151167 divide (
152168 multiply ("per_second(bytes)" , 1000.0 ),
153169 timestampdiff (
154- "MILLISECOND" , "@timestamp" , timestampadd ("MINUTE" , 5 , "@timestamp" )))),
170+ "MILLISECOND" ,
171+ IMPLICIT_FIELD_TIMESTAMP ,
172+ timestampadd ("MINUTE" , 5 , IMPLICIT_FIELD_TIMESTAMP )))),
155173 expected ));
156174 }
157175
@@ -161,17 +179,21 @@ private static TransformationAssertion withTimechart(Span spanExpr, AggregateFun
161179 return new TransformationAssertion (timechart (spanExpr , aggFunc ));
162180 }
163181
164- private static TransformationAssertion withTimechart (Timechart timechart ) {
182+ private static TransformationAssertion withTimechart (Chart timechart ) {
165183 return new TransformationAssertion (timechart );
166184 }
167185
168- private static Timechart timechart (Span spanExpr , UnresolvedExpression aggExpr ) {
186+ private static Chart timechart (Span spanExpr , UnresolvedExpression aggExpr ) {
169187 // Set child here because expected object won't call attach below
170- return new Timechart (relation ("t" ), aggExpr ).span (spanExpr ).limit (10 ).useOther (true );
188+ return Chart .builder ()
189+ .child (relation ("t" ))
190+ .aggregationFunction (aggExpr )
191+ .rowSplit (spanExpr )
192+ .build ();
171193 }
172194
173195 private static Span span (int value , String unit ) {
174- return AstDSL .span (field ( "@timestamp" ), intLiteral (value ), SpanUnit .of (unit ));
196+ return AstDSL .span (AstDSL . implicitTimestampField ( ), intLiteral (value ), SpanUnit .of (unit ));
175197 }
176198
177199 private static AggregateFunction perSecond (String fieldName ) {
@@ -209,23 +231,31 @@ private static UnresolvedExpression divide(
209231
210232 private static UnresolvedExpression timestampadd (String unit , int value , String timestampField ) {
211233 return function (
212- "timestampadd" , AstDSL .stringLiteral (unit ), intLiteral (value ), field (timestampField ));
234+ BuiltinFunctionName .TIMESTAMPADD .getName ().getFunctionName (),
235+ AstDSL .stringLiteral (unit ),
236+ intLiteral (value ),
237+ field (timestampField ));
213238 }
214239
215240 private static UnresolvedExpression timestampdiff (
216241 String unit , String startField , UnresolvedExpression end ) {
217- return function ("timestampdiff" , AstDSL .stringLiteral (unit ), field (startField ), end );
242+
243+ return function (
244+ BuiltinFunctionName .TIMESTAMPDIFF .getName ().getFunctionName (),
245+ AstDSL .stringLiteral (unit ),
246+ field (startField ),
247+ end );
218248 }
219249
220- private static UnresolvedPlan eval (Let letExpr , Timechart timechartExpr ) {
250+ private static UnresolvedPlan eval (Let letExpr , Chart timechartExpr ) {
221251 return AstDSL .eval (timechartExpr , letExpr );
222252 }
223253
224254 private static class TransformationAssertion {
225- private final Timechart timechart ;
255+ private final Chart timechart ;
226256 private UnresolvedPlan result ;
227257
228- TransformationAssertion (Timechart timechart ) {
258+ TransformationAssertion (Chart timechart ) {
229259 this .timechart = timechart ;
230260 }
231261
0 commit comments