-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-37343][SQL] Implement createIndex, IndexExists and dropIndex in JDBC (Postgres dialect) #34673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-37343][SQL] Implement createIndex, IndexExists and dropIndex in JDBC (Postgres dialect) #34673
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ import java.util | |
| import java.util.Locale | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| import scala.collection.JavaConverters._ | ||
| import scala.util.Try | ||
| import scala.util.control.NonFatal | ||
|
|
||
|
|
@@ -38,6 +39,7 @@ import org.apache.spark.sql.catalyst.parser.CatalystSqlParser | |
| import org.apache.spark.sql.catalyst.util.{CaseInsensitiveMap, DateTimeUtils, GenericArrayData} | ||
| import org.apache.spark.sql.catalyst.util.DateTimeUtils.{instantToMicros, localDateToDays, toJavaDate, toJavaTimestamp} | ||
| import org.apache.spark.sql.connector.catalog.TableChange | ||
| import org.apache.spark.sql.connector.catalog.index.SupportsIndex | ||
| import org.apache.spark.sql.connector.catalog.index.TableIndex | ||
| import org.apache.spark.sql.connector.expressions.NamedReference | ||
| import org.apache.spark.sql.errors.{QueryCompilationErrors, QueryExecutionErrors} | ||
|
|
@@ -1025,7 +1027,7 @@ object JdbcUtils extends Logging with SQLConfHelper { | |
| options: JDBCOptions): Unit = { | ||
| val dialect = JdbcDialects.get(options.url) | ||
| executeStatement(conn, options, | ||
| dialect.createIndex(indexName, tableName, columns, columnsProperties, properties)) | ||
| dialect.createIndex(indexName, tableName, columns, columnsProperties, properties, options)) | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1078,26 +1080,50 @@ object JdbcUtils extends Logging with SQLConfHelper { | |
| */ | ||
| def checkIfIndexExists( | ||
| conn: Connection, | ||
| indexName: String, | ||
| sql: String, | ||
| indexColumnName: String, | ||
| options: JDBCOptions): Boolean = { | ||
| val statement = conn.createStatement | ||
| try { | ||
| statement.setQueryTimeout(options.queryTimeout) | ||
| val rs = statement.executeQuery(sql) | ||
| while (rs.next()) { | ||
| val retrievedIndexName = rs.getString(indexColumnName) | ||
| if (conf.resolver(retrievedIndexName, indexName)) { | ||
| return true | ||
| } | ||
| } | ||
| false | ||
| rs.next | ||
| } catch { | ||
| case _: Exception => | ||
| logWarning("Cannot retrieved index info.") | ||
| false | ||
| } finally { | ||
| statement.close() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Process index properties and return tuple of indexType and list of the other index properties. | ||
| */ | ||
| def processIndexProperties( | ||
| properties: util.Map[String, String], | ||
| options: JDBCOptions | ||
| ): (String, Array[String]) = { | ||
| val dialect = JdbcDialects.get(options.url) | ||
| var indexType = "" | ||
| var indexPropertyList: Array[String] = Array.empty | ||
|
||
|
|
||
| if (!properties.isEmpty) { | ||
| properties.asScala.foreach { case (k, v) => | ||
| if (k.equals(SupportsIndex.PROP_TYPE)) { | ||
| if (v.equalsIgnoreCase("BTREE") || v.equalsIgnoreCase("HASH")) { | ||
| indexType = s"USING $v" | ||
| } else { | ||
| throw new UnsupportedOperationException(s"Index Type $v is not supported." + | ||
| " The supported Index Types are: BTREE and HASH") | ||
|
||
| } | ||
| } else { | ||
| indexPropertyList = indexPropertyList :+ dialect.convertPropertyPairToString(k, v) | ||
| } | ||
| } | ||
| } | ||
| (indexType, indexPropertyList) | ||
| } | ||
|
||
|
|
||
| def executeQuery(conn: Connection, options: JDBCOptions, sql: String): ResultSet = { | ||
| val statement = conn.createStatement | ||
| try { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -306,7 +306,8 @@ abstract class JdbcDialect extends Serializable with Logging{ | |
| tableName: String, | ||
| columns: Array[NamedReference], | ||
| columnsProperties: util.Map[NamedReference, util.Map[String, String]], | ||
| properties: util.Map[String, String]): String = { | ||
| properties: util.Map[String, String], | ||
| options: JDBCOptions): String = { | ||
| throw new UnsupportedOperationException("createIndex is not supported") | ||
| } | ||
|
|
||
|
|
@@ -358,6 +359,13 @@ abstract class JdbcDialect extends Serializable with Logging{ | |
| new AnalysisException(message, cause = Some(e)) | ||
| } | ||
|
|
||
| /** | ||
| * Convert key-value property pair to string | ||
| */ | ||
| def convertPropertyPairToString(key: String, value: String): String = { | ||
|
||
| s"$key $value" | ||
| } | ||
|
|
||
| /** | ||
| * returns the LIMIT clause for the SELECT statement | ||
| */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: combine this with the next line