@@ -24,10 +24,12 @@ import scala.collection.JavaConverters._
24
24
import org .apache .commons .lang3 .StringUtils
25
25
import org .apache .hive .service .rpc .thrift .{TExecuteStatementReq , TFetchResultsReq , TOpenSessionReq , TStatusCode }
26
26
27
- import org .apache .kyuubi .KYUUBI_VERSION
27
+ import org .apache .kyuubi .{ KYUUBI_VERSION , Utils }
28
28
29
29
trait SparkQueryTests extends HiveJDBCTestHelper {
30
30
31
+ protected lazy val SPARK_ENGINE_MAJOR_MINOR_VERSION : (Int , Int ) = sparkEngineMajorMinorVersion
32
+
31
33
test(" execute statement - select null" ) {
32
34
withJdbcStatement() { statement =>
33
35
val resultSet = statement.executeQuery(" SELECT NULL AS col" )
@@ -175,25 +177,70 @@ trait SparkQueryTests extends HiveJDBCTestHelper {
175
177
test(" execute statement - select daytime interval" ) {
176
178
withJdbcStatement() { statement =>
177
179
Map (
178
- " -interval 2 day" -> " -2 00:00:00.000000000" ,
179
- " -interval 200 day" -> " -200 00:00:00.000000000" ,
180
- " interval 1 day 1 hour" -> " 1 01:00:00.000000000" ,
181
- " interval 1 day 1 hour -60 minutes" -> " 1 00:00:00.000000000" ,
182
- " interval 1 day 1 hour -60 minutes 30 seconds" -> " 1 00:00:30.000000000" ,
180
+ " interval 1 day 1 hour -60 minutes 30 seconds" ->
181
+ Tuple2 (" 1 00:00:30.000000000" , " 1 days 30 seconds" ),
182
+ " interval 30 seconds 12345 milliseconds" ->
183
+ Tuple2 (" 0 00:00:42.345000000" , " 42.345 seconds" ),
184
+ " interval 1 hour 59 minutes 30 seconds 12345 milliseconds" ->
185
+ Tuple2 (" 0 01:59:42.345000000" , " 1 hours 59 minutes 42.345 seconds" ),
186
+ " -interval 2 day" -> Tuple2 (" -2 00:00:00.000000000" , " -2 days" ),
187
+ " interval 59 minutes 30 seconds 12345 milliseconds" ->
188
+ Tuple2 (" 0 00:59:42.345000000" , " 59 minutes 42.345 seconds" ),
189
+ " interval 25 hour" -> Tuple2 (" 1 01:00:00.000000000" , " 25 hours" ),
190
+ " interval 1 hour 62 minutes" -> Tuple2 (" 0 02:02:00.000000000" , " 2 hours 2 minutes" ),
183
191
" interval 1 day 1 hour 59 minutes 30 seconds 12345 milliseconds" ->
184
- " 1 01:59:42.345000000" ).foreach { kv => // value -> result pair
192
+ Tuple2 (" 1 01:59:42.345000000" , " 1 days 1 hours 59 minutes 42.345 seconds" ),
193
+ " interval 1 day 1 hour -60 minutes" -> Tuple2 (" 1 00:00:00.000000000" , " 1 days" ),
194
+ " INTERVAL 30 SECONDS" -> Tuple2 (" 0 00:00:30.000000000" , " 30 seconds" ),
195
+ " interval -60 minutes 30 seconds" ->
196
+ Tuple2 (" -0 00:59:30.000000000" , " -59 minutes -30 seconds" ),
197
+ " -interval 200 day" -> Tuple2 (" -200 00:00:00.000000000" , " -200 days" ),
198
+ " interval 1 hour -60 minutes 30 seconds" -> Tuple2 (" 0 00:00:30.000000000" , " 30 seconds" ),
199
+ " interval 62 minutes" -> Tuple2 (" 0 01:02:00.000000000" , " 1 hours 2 minutes" ),
200
+ " interval 1 day 1 hour" -> Tuple2 (" 1 01:00:00.000000000" , " 1 days 1 hours" )).foreach {
201
+ kv => // value -> result pair
202
+ val resultSet = statement.executeQuery(s " SELECT ${kv._1} AS col " )
203
+ assert(resultSet.next())
204
+ val result = resultSet.getString(" col" )
205
+ val metaData = resultSet.getMetaData
206
+ if (SPARK_ENGINE_MAJOR_MINOR_VERSION ._1 == 3
207
+ && SPARK_ENGINE_MAJOR_MINOR_VERSION ._2 < 2 ) {
208
+ // for spark 3.1 and backwards
209
+ assert(result === kv._2._2)
210
+ assert(metaData.getPrecision(1 ) === Int .MaxValue )
211
+ assert(resultSet.getMetaData.getColumnType(1 ) === java.sql.Types .VARCHAR )
212
+ } else {
213
+ assert(result === kv._2._1)
214
+ assert(metaData.getPrecision(1 ) === 29 )
215
+ assert(resultSet.getMetaData.getColumnType(1 ) === java.sql.Types .OTHER )
216
+ }
217
+ assert(metaData.getScale(1 ) === 0 )
218
+ }
219
+ }
220
+ }
221
+
222
+ test(" execute statement - select year/month interval" ) {
223
+ withJdbcStatement() { statement =>
224
+ Map (
225
+ " INTERVAL 2022 YEAR" -> Tuple2 (" 2022-0" , " 2022 years" ),
226
+ " INTERVAL '2021-07' YEAR TO MONTH" -> Tuple2 (" 2021-7" , " 2021 years 7 months" ),
227
+ " INTERVAL 3 MONTH" -> Tuple2 (" 0-3" , " 3 months" ),
228
+ " INTERVAL 241 MONTH" -> Tuple2 (" 20-1" , " 20 years 1 months" ),
229
+ " INTERVAL -1 year -25 MONTH" -> Tuple2 (" -3-1" , " -3 years -1 months" ),
230
+ " INTERVAL 3 year -25 MONTH" -> Tuple2 (" 0-11" , " 11 months" )).foreach { kv =>
185
231
val resultSet = statement.executeQuery(s " SELECT ${kv._1} AS col " )
186
232
assert(resultSet.next())
187
233
val result = resultSet.getString(" col" )
188
234
val metaData = resultSet.getMetaData
189
- if (result.contains(" days" )) {
235
+ if (SPARK_ENGINE_MAJOR_MINOR_VERSION ._1 == 3
236
+ && SPARK_ENGINE_MAJOR_MINOR_VERSION ._2 < 2 ) {
190
237
// for spark 3.1 and backwards
191
- assert(result.split( " days " ).head.trim === kv._2.split( " " ).head )
238
+ assert(result === kv._2._2 )
192
239
assert(metaData.getPrecision(1 ) === Int .MaxValue )
193
240
assert(resultSet.getMetaData.getColumnType(1 ) === java.sql.Types .VARCHAR )
194
241
} else {
195
- assert(result === kv._2)
196
- assert(metaData.getPrecision(1 ) === 29 )
242
+ assert(result === kv._2._1 )
243
+ assert(metaData.getPrecision(1 ) === 11 )
197
244
assert(resultSet.getMetaData.getColumnType(1 ) === java.sql.Types .OTHER )
198
245
}
199
246
assert(metaData.getScale(1 ) === 0 )
@@ -545,4 +592,15 @@ trait SparkQueryTests extends HiveJDBCTestHelper {
545
592
assert(foundOperationLangItem)
546
593
}
547
594
}
595
+
596
+ def sparkEngineMajorMinorVersion : (Int , Int ) = {
597
+ var sparkRuntimeVer = " "
598
+ withJdbcStatement() { stmt =>
599
+ val result = stmt.executeQuery(" SELECT version()" )
600
+ assert(result.next())
601
+ sparkRuntimeVer = result.getString(1 )
602
+ assert(! result.next())
603
+ }
604
+ Utils .majorMinorVersion(sparkRuntimeVer)
605
+ }
548
606
}
0 commit comments