1818 */
1919package org .apache .iotdb .db .it .query ;
2020
21+ import org .apache .iotdb .isession .SessionConfig ;
2122import org .apache .iotdb .it .env .EnvFactory ;
2223import org .apache .iotdb .it .framework .IoTDBTestRunner ;
2324import org .apache .iotdb .itbase .category .TableClusterIT ;
2425import org .apache .iotdb .itbase .category .TableLocalStandaloneIT ;
26+ import org .apache .iotdb .itbase .env .BaseEnv ;
2527
2628import org .junit .AfterClass ;
2729import org .junit .BeforeClass ;
2830import org .junit .Test ;
2931import org .junit .experimental .categories .Category ;
3032import org .junit .runner .RunWith ;
3133
34+ import java .sql .Connection ;
35+ import java .sql .SQLException ;
36+ import java .sql .Statement ;
37+
3238import static org .apache .iotdb .db .it .utils .TestUtils .prepareTableData ;
3339import static org .apache .iotdb .db .it .utils .TestUtils .tableResultSetEqualTest ;
40+ import static org .junit .Assert .assertEquals ;
41+ import static org .junit .Assert .fail ;
3442
3543@ RunWith (IoTDBTestRunner .class )
3644@ Category ({TableLocalStandaloneIT .class , TableClusterIT .class })
@@ -103,25 +111,83 @@ public static void tearDown() throws Exception {
103111 EnvFactory .getEnv ().cleanClusterEnvironment ();
104112 }
105113
114+ @ Test
115+ public void TestPriority () {
116+ String [] expectedHeader = {"_col0" , "_col1" , "_col2" , "_col3" , "_col4" , "_col5" };
117+ String [] retArray = {"1,1,1.0,1.0,true,1s," };
118+ tableResultSetEqualTest (
119+ "select "
120+ + "first_by(s_int, y_criteria), "
121+ + "first_by(s_long, y_criteria), "
122+ + "first_by(s_float, y_criteria), "
123+ + "first_by(s_double, y_criteria), "
124+ + "first_by(s_bool, y_criteria), "
125+ + "first_by(s_string, y_criteria) "
126+ + "from table_a "
127+ + "where table_a.device='d1'" ,
128+ expectedHeader ,
129+ retArray ,
130+ DATABASE_NAME );
131+ }
132+
133+ @ Test
134+ public void testNoTimeStamp () {
135+
136+ String sql =
137+ "select "
138+ + "first_by(s_int, y_criteria)"
139+ + " from ("
140+ + " select "
141+ + " s_int,"
142+ + " s_long,"
143+ + " s_float,"
144+ + " y_criteria"
145+ + " from table_a"
146+ + ") AS t" ;
147+ try (Connection connection =
148+ EnvFactory .getEnv ()
149+ .getConnection (
150+ SessionConfig .DEFAULT_USER ,
151+ SessionConfig .DEFAULT_PASSWORD ,
152+ BaseEnv .TABLE_SQL_DIALECT )) {
153+ connection .setClientInfo ("time_zone" , "+00:00" );
154+ try (Statement statement = connection .createStatement ()) {
155+ statement .execute ("use " + DATABASE_NAME );
156+ statement .executeQuery (sql );
157+ }
158+ fail ("Missing valid time column, the query should fail" );
159+ } catch (SQLException e ) {
160+ assertEquals (
161+ "701: Missing valid time column. The table must contain either a column with the TIME category or at least one TIMESTAMP column." ,
162+ e .getMessage ());
163+ }
164+ }
165+
106166 @ Test
107167 public void testFirstBy_d1_NoNulls () {
108168 String [] expectedHeader = {"_col0" , "_col1" , "_col2" , "_col3" , "_col4" , "_col5" };
109169 String [] retArray = {"5,5,5.0,5.0,false,5s," };
110170 runTest ("d1" , expectedHeader , retArray );
171+ runTest2 ("d1" , expectedHeader , retArray );
172+ runTest3 ("d1" , expectedHeader , retArray );
111173 }
112174
113175 @ Test
114176 public void testFirstBy_d2_ForwardTracking () {
115177 String [] expectedHeader = {"_col0" , "_col1" , "_col2" , "_col3" , "_col4" , "_col5" };
116178 String [] retArray = {"10,10,10.0,10.0,true,10s," };
117179 runTest ("d2" , expectedHeader , retArray );
180+ runTest2 ("d2" , expectedHeader , retArray );
181+ runTest3 ("d2" , expectedHeader , retArray );
118182 }
119183
120184 @ Test
121185 public void testFirstBy_d3_TargetNull () {
122186 String [] expectedHeader = {"_col0" , "_col1" , "_col2" , "_col3" , "_col4" , "_col5" };
123187 String [] retArray = {"5,5,null,null,null,null," };
124188 runTest ("d3" , expectedHeader , retArray );
189+ runTest2 ("d3" , expectedHeader , retArray );
190+ runTest3 ("d3" , expectedHeader , retArray );
125191 }
126192
127193 @ Test
@@ -130,6 +196,8 @@ public void testFirstBy_d4_AllNullCriteria() {
130196 // Expected: No valid s2 found.
131197 String [] retArray = {"null,null,null,null,null,null," };
132198 runTest ("d4" , expectedHeader , retArray );
199+ runTest2 ("d4" , expectedHeader , retArray );
200+ runTest3 ("d4" , expectedHeader , retArray );
133201 }
134202
135203 @ Test
@@ -138,6 +206,8 @@ public void testFirstBy_d5_AllTimeNull() {
138206 // Expected: The row with y_criteria=NULL is skipped. The row with y_criteria=50 is picked.
139207 String [] retArray = {"50,50,50.0,50.0,false,50s," };
140208 runTest ("d5" , expectedHeader , retArray );
209+ runTest2 ("d5" , expectedHeader , retArray );
210+ runTest3 ("d5" , expectedHeader , retArray );
141211 }
142212
143213 private void runTest (String deviceId , String [] expectedHeader , String [] retArray ) {
@@ -159,4 +229,43 @@ private void runTest(String deviceId, String[] expectedHeader, String[] retArray
159229 retArray ,
160230 DATABASE_NAME );
161231 }
232+
233+ private void runTest2 (String deviceId , String [] expectedHeader , String [] retArray ) {
234+ tableResultSetEqualTest (
235+ "select "
236+ + "first_by(s_int, y_criteria), "
237+ + "first_by(s_long, y_criteria), "
238+ + "first_by(s_float, y_criteria), "
239+ + "first_by(s_double, y_criteria), "
240+ + "first_by(s_bool, y_criteria), "
241+ + "first_by(s_string, y_criteria) "
242+ + "from "
243+ + "(select s_int, s_long, s_float, s_double, s_bool, s_string, y_criteria, time_type "
244+ + "from table_a left join table_b on table_a.time=table_b.time "
245+ + "where table_a.device='"
246+ + deviceId
247+ + "') " ,
248+ expectedHeader ,
249+ retArray ,
250+ DATABASE_NAME );
251+ }
252+
253+ /** Test 3: Raw table query with explicit time_type column, 3 arguments */
254+ private void runTest3 (String deviceId , String [] expectedHeader , String [] retArray ) {
255+ tableResultSetEqualTest (
256+ "select "
257+ + "first_by(s_int, y_criteria, time_type), "
258+ + "first_by(s_long, y_criteria, time_type), "
259+ + "first_by(s_float, y_criteria, time_type), "
260+ + "first_by(s_double, y_criteria, time_type), "
261+ + "first_by(s_bool, y_criteria, time_type), "
262+ + "first_by(s_string, y_criteria, time_type) "
263+ + "from table_a "
264+ + "where table_a.device='"
265+ + deviceId
266+ + "'" ,
267+ expectedHeader ,
268+ retArray ,
269+ DATABASE_NAME );
270+ }
162271}
0 commit comments