17
17
18
18
package org .apache .spark .sql .jdbc .v2
19
19
20
- import java .util
21
-
22
20
import org .apache .log4j .Level
23
21
24
22
import org .apache .spark .sql .{AnalysisException , DataFrame }
23
+ import org .apache .spark .sql .catalyst .analysis .{IndexAlreadyExistsException , NoSuchIndexException }
25
24
import org .apache .spark .sql .catalyst .plans .logical .{Aggregate , Filter , Sample }
26
25
import org .apache .spark .sql .connector .catalog .{Catalogs , Identifier , TableCatalog }
27
26
import org .apache .spark .sql .connector .catalog .index .SupportsIndex
@@ -191,91 +190,56 @@ private[v2] trait V2JDBCTest extends SharedSparkSession with DockerIntegrationFu
191
190
}
192
191
193
192
def supportsIndex : Boolean = false
194
- def testIndexProperties (jdbcTable : SupportsIndex ): Unit = {}
195
193
196
- test(" SPARK-36913: Test INDEX" ) {
194
+ def indexOptions : String = " "
195
+
196
+ test(" SPARK-36895: Test INDEX Using SQL" ) {
197
197
if (supportsIndex) {
198
198
withTable(s " $catalogName.new_table " ) {
199
199
sql(s " CREATE TABLE $catalogName.new_table(col1 INT, col2 INT, col3 INT, " +
200
- s " col4 INT, col5 INT) " )
200
+ " col4 INT, col5 INT)" )
201
201
val loaded = Catalogs .load(catalogName, conf)
202
202
val jdbcTable = loaded.asInstanceOf [TableCatalog ]
203
203
.loadTable(Identifier .of(Array .empty[String ], " new_table" ))
204
204
.asInstanceOf [SupportsIndex ]
205
205
assert(jdbcTable.indexExists(" i1" ) == false )
206
206
assert(jdbcTable.indexExists(" i2" ) == false )
207
207
208
- val properties = new util.Properties ();
209
208
val indexType = " DUMMY"
210
209
var m = intercept[UnsupportedOperationException ] {
211
- jdbcTable.createIndex(" i1" , indexType, Array (FieldReference (" col1" )),
212
- Array .empty[util.Map [NamedReference , util.Properties ]], properties)
210
+ sql(s " CREATE index i1 ON $catalogName.new_table USING $indexType (col1) " )
213
211
}.getMessage
214
212
assert(m.contains(s " Index Type $indexType is not supported. " +
215
- s " The supported Index Types are: BTREE and HASH " ))
216
-
217
- jdbcTable.createIndex(" i1" , " BTREE" , Array (FieldReference (" col1" )),
218
- Array .empty[util.Map [NamedReference , util.Properties ]], properties)
213
+ s " The supported Index Types are: " ))
219
214
220
- jdbcTable.createIndex( " i2 " , " " ,
221
- Array ( FieldReference ( " col2" ), FieldReference ( " col3" ), FieldReference ( " col5" )),
222
- Array .empty[util. Map [ NamedReference , util. Properties ]], properties )
215
+ sql( s " CREATE index i1 ON $catalogName .new_table USING BTREE (col1) " )
216
+ sql( s " CREATE index i2 ON $catalogName .new_table ( col2, col3, col5) " +
217
+ s " OPTIONS ( $indexOptions ) " )
223
218
224
219
assert(jdbcTable.indexExists(" i1" ) == true )
225
220
assert(jdbcTable.indexExists(" i2" ) == true )
226
221
222
+ // This should pass without exception
223
+ sql(s " CREATE index IF NOT EXISTS i1 ON $catalogName.new_table (col1) " )
224
+
227
225
m = intercept[IndexAlreadyExistsException ] {
228
- jdbcTable.createIndex(" i1" , " " , Array (FieldReference (" col1" )),
229
- Array .empty[util.Map [NamedReference , util.Properties ]], properties)
226
+ sql(s " CREATE index i1 ON $catalogName.new_table (col1) " )
230
227
}.getMessage
231
- assert(m.contains(" Failed to create index: i1 in new_table" ))
232
-
233
- var index = jdbcTable.listIndexes()
234
- assert(index.length == 2 )
235
-
236
- assert(index(0 ).indexName.equals(" i1" ))
237
- assert(index(0 ).indexType.equals(" BTREE" ))
238
- var cols = index(0 ).columns
239
- assert(cols.length == 1 )
240
- assert(cols(0 ).describe().equals(" col1" ))
241
- assert(index(0 ).properties.size == 0 )
242
-
243
- assert(index(1 ).indexName.equals(" i2" ))
244
- assert(index(1 ).indexType.equals(" BTREE" ))
245
- cols = index(1 ).columns
246
- assert(cols.length == 3 )
247
- assert(cols(0 ).describe().equals(" col2" ))
248
- assert(cols(1 ).describe().equals(" col3" ))
249
- assert(cols(2 ).describe().equals(" col5" ))
250
- assert(index(1 ).properties.size == 0 )
251
-
252
- jdbcTable.dropIndex(" i1" )
253
- assert(jdbcTable.indexExists(" i1" ) == false )
254
- assert(jdbcTable.indexExists(" i2" ) == true )
255
-
256
- index = jdbcTable.listIndexes()
257
- assert(index.length == 1 )
228
+ assert(m.contains(" Failed to create index i1 in new_table" ))
258
229
259
- assert(index(0 ).indexName.equals(" i2" ))
260
- assert(index(0 ).indexType.equals(" BTREE" ))
261
- cols = index(0 ).columns
262
- assert(cols.length == 3 )
263
- assert(cols(0 ).describe().equals(" col2" ))
264
- assert(cols(1 ).describe().equals(" col3" ))
265
- assert(cols(2 ).describe().equals(" col5" ))
230
+ sql(s " DROP index i1 ON $catalogName.new_table " )
231
+ sql(s " DROP index i2 ON $catalogName.new_table " )
266
232
267
- jdbcTable.dropIndex(" i2" )
268
233
assert(jdbcTable.indexExists(" i1" ) == false )
269
234
assert(jdbcTable.indexExists(" i2" ) == false )
270
- index = jdbcTable.listIndexes()
271
- assert(index.length == 0 )
235
+
236
+ // This should pass without exception
237
+ sql(s " DROP index IF EXISTS i1 ON $catalogName.new_table " )
272
238
273
239
m = intercept[NoSuchIndexException ] {
274
- jdbcTable.dropIndex( " i2 " )
240
+ sql( s " DROP index i1 ON $catalogName .new_table " )
275
241
}.getMessage
276
- assert(m.contains(" Failed to drop index: i2" ))
277
-
278
- testIndexProperties(jdbcTable)
242
+ assert(m.contains(" Failed to drop index i1 in new_table" ))
279
243
}
280
244
}
281
245
}
@@ -338,23 +302,23 @@ private[v2] trait V2JDBCTest extends SharedSparkSession with DockerIntegrationFu
338
302
assert(samplePushed(df3))
339
303
assert(limitPushed(df3, 2 ))
340
304
assert(columnPruned(df3, " col1" ))
341
- assert(df3.collect().length = = 2 )
305
+ assert(df3.collect().length < = 2 )
342
306
343
307
// sample(... PERCENT) push down + limit push down + column pruning
344
308
val df4 = sql(s " SELECT col1 FROM $catalogName.new_table " +
345
309
" TABLESAMPLE (50 PERCENT) REPEATABLE (12345) LIMIT 2" )
346
310
assert(samplePushed(df4))
347
311
assert(limitPushed(df4, 2 ))
348
312
assert(columnPruned(df4, " col1" ))
349
- assert(df4.collect().length = = 2 )
313
+ assert(df4.collect().length < = 2 )
350
314
351
315
// sample push down + filter push down + limit push down
352
316
val df5 = sql(s " SELECT * FROM $catalogName.new_table " +
353
317
" TABLESAMPLE (BUCKET 6 OUT OF 10) WHERE col1 > 0 LIMIT 2" )
354
318
assert(samplePushed(df5))
355
319
assert(filterPushed(df5))
356
320
assert(limitPushed(df5, 2 ))
357
- assert(df5.collect().length = = 2 )
321
+ assert(df5.collect().length < = 2 )
358
322
359
323
// sample + filter + limit + column pruning
360
324
// sample pushed down, filer/limit not pushed down, column pruned
@@ -365,7 +329,7 @@ private[v2] trait V2JDBCTest extends SharedSparkSession with DockerIntegrationFu
365
329
assert(! filterPushed(df6))
366
330
assert(! limitPushed(df6, 2 ))
367
331
assert(columnPruned(df6, " col1" ))
368
- assert(df6.collect().length = = 2 )
332
+ assert(df6.collect().length < = 2 )
369
333
370
334
// sample + limit
371
335
// Push down order is sample -> filter -> limit
0 commit comments