Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ class MySQLIntegrationSuite extends DockerJDBCIntegrationV2Suite with V2JDBCTest

override def supportsIndex: Boolean = true

override def supportListIndexes: Boolean = true

override def indexOptions: String = "KEY_BLOCK_SIZE=10"

testVarPop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ private[v2] trait V2JDBCTest extends SharedSparkSession with DockerIntegrationFu

def supportsIndex: Boolean = false

def supportListIndexes: Boolean = false

def indexOptions: String = ""

test("SPARK-36895: Test INDEX Using SQL") {
Expand All @@ -219,11 +221,17 @@ private[v2] trait V2JDBCTest extends SharedSparkSession with DockerIntegrationFu
s" The supported Index Types are:"))

sql(s"CREATE index i1 ON $catalogName.new_table USING BTREE (col1)")
assert(jdbcTable.indexExists("i1") == true)
if (supportListIndexes) {
assert(jdbcTable.listIndexes().size == 1)
}

sql(s"CREATE index i2 ON $catalogName.new_table (col2, col3, col5)" +
s" OPTIONS ($indexOptions)")

assert(jdbcTable.indexExists("i1") == true)
assert(jdbcTable.indexExists("i2") == true)
if (supportListIndexes) {
assert(jdbcTable.listIndexes().size == 2)
}

// This should pass without exception
sql(s"CREATE index IF NOT EXISTS i1 ON $catalogName.new_table (col1)")
Expand All @@ -234,10 +242,16 @@ private[v2] trait V2JDBCTest extends SharedSparkSession with DockerIntegrationFu
assert(m.contains("Failed to create index i1 in new_table"))

sql(s"DROP index i1 ON $catalogName.new_table")
sql(s"DROP index i2 ON $catalogName.new_table")

assert(jdbcTable.indexExists("i1") == false)
if (supportListIndexes) {
assert(jdbcTable.listIndexes().size == 1)
}

sql(s"DROP index i2 ON $catalogName.new_table")
assert(jdbcTable.indexExists("i2") == false)
if (supportListIndexes) {
assert(jdbcTable.listIndexes().size == 0)
}

// This should pass without exception
sql(s"DROP index IF EXISTS i1 ON $catalogName.new_table")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import java.util.Locale

import scala.collection.mutable.ArrayBuilder

import org.apache.commons.lang3.StringUtils

import org.apache.spark.sql.AnalysisException
import org.apache.spark.sql.catalyst.SQLConfHelper
import org.apache.spark.sql.catalyst.analysis.{IndexAlreadyExistsException, NoSuchIndexException}
Expand Down Expand Up @@ -217,7 +219,7 @@ private case object MySQLDialect extends JdbcDialect with SQLConfHelper {
// The only property we are building here is `COMMENT` because it's the only one
// we can get from `SHOW INDEXES`.
val properties = new util.Properties();
if (indexComment.nonEmpty) properties.put("COMMENT", indexComment)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to me if Index_comment is not set, MySQL defaults it to empty String?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right! I investigated it carefully.
INDEX_COMMENT nullability is NO, the default value is empty String.

mysql> desc INFORMATION_SCHEMA.STATISTICS;
+---------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+-------+
| TABLE_CATALOG | varchar(512) | NO | | | |
| TABLE_SCHEMA | varchar(64) | NO | | | |
| TABLE_NAME | varchar(64) | NO | | | |
| NON_UNIQUE | bigint(1) | NO | | 0 | |
| INDEX_SCHEMA | varchar(64) | NO | | | |
| INDEX_NAME | varchar(64) | NO | | | |
| SEQ_IN_INDEX | bigint(2) | NO | | 0 | |
| COLUMN_NAME | varchar(64) | NO | | | |
| COLLATION | varchar(1) | YES | | NULL | |
| CARDINALITY | bigint(21) | YES | | NULL | |
| SUB_PART | bigint(3) | YES | | NULL | |
| PACKED | varchar(10) | YES | | NULL | |
| NULLABLE | varchar(3) | NO | | | |
| INDEX_TYPE | varchar(16) | NO | | | |
| COMMENT | varchar(16) | YES | | NULL | |
| INDEX_COMMENT | varchar(1024) | NO | | | |
+---------------+---------------+------+-----+---------+-------+

Copy link
Contributor Author

@panbingkun panbingkun Jul 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will close it, but maybe we need add some UT for it.I will open new pr for it.
FYI: Whether it is necessary to implement createIndex & existIndex & dropIndex & listIndex in H2Dialect ?

I found the above "possible null pointer" when implementing the method: listIndex in H2Dialect(the default value is null instead of empty string). @huaxingao

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated:

new UT for it: #37060

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we need add some UT for it

Sounds good to me. I will take a look at the new PR.

Whether it is necessary to implement createIndex & existIndex & dropIndex & listIndex in H2Dialect ?

It's OK to implement these.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@huaxingao implement for h2 dialect: #37112

if (StringUtils.isNotEmpty(indexComment)) properties.put("COMMENT", indexComment)
val index = new TableIndex(indexName, indexType, Array(FieldReference(colName)),
new util.HashMap[NamedReference, util.Properties](), properties)
indexMap += (indexName -> index)
Expand Down