|
17 | 17 |
|
18 | 18 | package org.apache.spark.sql.jdbc.v2
|
19 | 19 |
|
| 20 | +import java.util |
| 21 | + |
20 | 22 | import org.apache.log4j.Level
|
21 | 23 |
|
22 | 24 | import org.apache.spark.sql.AnalysisException
|
| 25 | +import org.apache.spark.sql.catalyst.analysis.{IndexAlreadyExistsException, NoSuchIndexException} |
| 26 | +import org.apache.spark.sql.connector.catalog.{Catalogs, Identifier, TableCatalog} |
| 27 | +import org.apache.spark.sql.connector.catalog.index.SupportsIndex |
| 28 | +import org.apache.spark.sql.connector.expressions.{FieldReference, NamedReference} |
23 | 29 | import org.apache.spark.sql.jdbc.DockerIntegrationFunSuite
|
24 | 30 | import org.apache.spark.sql.test.SharedSparkSession
|
25 | 31 | import org.apache.spark.sql.types._
|
@@ -181,12 +187,85 @@ private[v2] trait V2JDBCTest extends SharedSparkSession with DockerIntegrationFu
|
181 | 187 | }
|
182 | 188 | }
|
183 | 189 |
|
184 |
| - def testIndex(tbl: String): Unit = {} |
| 190 | + def supportsIndex: Boolean = false |
| 191 | + def testIndexProperties(jdbcTable: SupportsIndex): Unit = {} |
185 | 192 |
|
186 | 193 | test("SPARK-36913: Test INDEX") {
|
187 |
| - withTable(s"$catalogName.new_table") { |
188 |
| - sql(s"CREATE TABLE $catalogName.new_table(col1 INT, col2 INT, col3 INT, col4 INT, col5 INT)") |
189 |
| - testIndex(s"$catalogName.new_table") |
| 194 | + if (supportsIndex) { |
| 195 | + withTable(s"$catalogName.new_table") { |
| 196 | + sql(s"CREATE TABLE $catalogName.new_table(col1 INT, col2 INT, col3 INT," + |
| 197 | + s" col4 INT, col5 INT)") |
| 198 | + val loaded = Catalogs.load(catalogName, conf) |
| 199 | + val jdbcTable = loaded.asInstanceOf[TableCatalog] |
| 200 | + .loadTable(Identifier.of(Array.empty[String], "new_table")) |
| 201 | + .asInstanceOf[SupportsIndex] |
| 202 | + assert(jdbcTable.indexExists("i1") == false) |
| 203 | + assert(jdbcTable.indexExists("i2") == false) |
| 204 | + |
| 205 | + val properties = new util.Properties(); |
| 206 | + jdbcTable.createIndex("i1", "BTREE", Array(FieldReference("col1")), |
| 207 | + Array.empty[util.Map[NamedReference, util.Properties]], properties) |
| 208 | + |
| 209 | + jdbcTable.createIndex("i2", "", |
| 210 | + Array(FieldReference("col2"), FieldReference("col3"), FieldReference("col5")), |
| 211 | + Array.empty[util.Map[NamedReference, util.Properties]], properties) |
| 212 | + |
| 213 | + assert(jdbcTable.indexExists("i1") == true) |
| 214 | + assert(jdbcTable.indexExists("i2") == true) |
| 215 | + |
| 216 | + var m = intercept[IndexAlreadyExistsException] { |
| 217 | + jdbcTable.createIndex("i1", "", Array(FieldReference("col1")), |
| 218 | + Array.empty[util.Map[NamedReference, util.Properties]], properties) |
| 219 | + }.getMessage |
| 220 | + assert(m.contains("Failed to create index: i1 in new_table")) |
| 221 | + |
| 222 | + var index = jdbcTable.listIndexes() |
| 223 | + assert(index.length == 2) |
| 224 | + |
| 225 | + assert(index(0).indexName.equals("i1")) |
| 226 | + assert(index(0).indexType.equals("BTREE")) |
| 227 | + var cols = index(0).columns |
| 228 | + assert(cols.length == 1) |
| 229 | + assert(cols(0).describe().equals("col1")) |
| 230 | + assert(index(0).properties.size == 0) |
| 231 | + |
| 232 | + assert(index(1).indexName.equals("i2")) |
| 233 | + assert(index(1).indexType.equals("BTREE")) |
| 234 | + cols = index(1).columns |
| 235 | + assert(cols.length == 3) |
| 236 | + assert(cols(0).describe().equals("col2")) |
| 237 | + assert(cols(1).describe().equals("col3")) |
| 238 | + assert(cols(2).describe().equals("col5")) |
| 239 | + assert(index(1).properties.size == 0) |
| 240 | + |
| 241 | + jdbcTable.dropIndex("i1") |
| 242 | + assert(jdbcTable.indexExists("i1") == false) |
| 243 | + assert(jdbcTable.indexExists("i2") == true) |
| 244 | + |
| 245 | + index = jdbcTable.listIndexes() |
| 246 | + assert(index.length == 1) |
| 247 | + |
| 248 | + assert(index(0).indexName.equals("i2")) |
| 249 | + assert(index(0).indexType.equals("BTREE")) |
| 250 | + cols = index(0).columns |
| 251 | + assert(cols.length == 3) |
| 252 | + assert(cols(0).describe().equals("col2")) |
| 253 | + assert(cols(1).describe().equals("col3")) |
| 254 | + assert(cols(2).describe().equals("col5")) |
| 255 | + |
| 256 | + jdbcTable.dropIndex("i2") |
| 257 | + assert(jdbcTable.indexExists("i1") == false) |
| 258 | + assert(jdbcTable.indexExists("i2") == false) |
| 259 | + index = jdbcTable.listIndexes() |
| 260 | + assert(index.length == 0) |
| 261 | + |
| 262 | + m = intercept[NoSuchIndexException] { |
| 263 | + jdbcTable.dropIndex("i2") |
| 264 | + }.getMessage |
| 265 | + assert(m.contains("Failed to drop index: i2")) |
| 266 | + |
| 267 | + testIndexProperties(jdbcTable) |
| 268 | + } |
190 | 269 | }
|
191 | 270 | }
|
192 | 271 | }
|
|
0 commit comments