huaxingao commented on a change in pull request #34673:
URL: https://github.com/apache/spark/pull/34673#discussion_r758025776
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/jdbc/PostgresDialect.scala
##########
@@ -164,4 +172,78 @@ private object PostgresDialect extends JdbcDialect {
s"TABLESAMPLE BERNOULLI" +
s" (${(sample.upperBound - sample.lowerBound) * 100}) REPEATABLE
(${sample.seed})"
}
+
+ // CREATE INDEX syntax
+ // https://www.postgresql.org/docs/14/sql-createindex.html
+ override def createIndex(
+ indexName: String,
+ tableName: String,
+ columns: Array[NamedReference],
+ columnsProperties: util.Map[NamedReference, util.Map[String, String]],
+ properties: util.Map[String, String]): String = {
+ val columnList = columns.map(col => quoteIdentifier(col.fieldNames.head))
+ var indexPropertiesStr: String = ""
+ var hasIndexProperties: Boolean = false
+ var indexType = ""
+
+ if (!properties.isEmpty) {
+ var indexPropertyList: Array[String] = Array.empty
+ 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 {
+ hasIndexProperties = true
+ indexPropertyList = indexPropertyList :+ s"$k = $v"
+ }
+ }
+ if (hasIndexProperties) {
+ indexPropertiesStr += "WITH (" + indexPropertyList.mkString(", ") + ")"
+ }
+ }
+
+ s"CREATE INDEX ${quoteIdentifier(indexName)} ON
${quoteIdentifier(tableName)}" +
+ s" $indexType (${columnList.mkString(", ")}) $indexPropertiesStr"
+ }
+
+ // SHOW INDEX syntax
+ // https://www.postgresql.org/docs/14/view-pg-indexes.html
+ override def indexExists(
+ conn: Connection,
+ indexName: String,
+ tableName: String,
+ options: JDBCOptions): Boolean = {
+ val sql = s"SELECT * FROM pg_indexes WHERE tablename = '$tableName'"
+ try {
+ JdbcUtils.checkIfIndexExists(conn, indexName, sql, "indexname", options)
Review comment:
How about moving the try catch to `JdbcUtils` too? So here we have
```
val sql = s"SELECT * FROM pg_indexes WHERE tablename =
${quoteIdentifier(tableName)} AND" +
s" indexname = '$indexName'"
JdbcUtils.checkIfIndexExists(conn, sql, options)
```
In `JDBCUtils`, we have
```
def checkIfIndexExists(
conn: Connection,
sql: String,
options: JDBCOptions): Boolean = {
val statement = conn.createStatement
try {
statement.setQueryTimeout(options.queryTimeout)
val rs = statement.executeQuery(sql)
rs.next
} catch {
case _: Exception =>
logWarning("Cannot retrieved index info.")
false
} finally {
statement.close()
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]