Skip to content

Commit

Permalink
Pre-validate Flint index name
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Dai <[email protected]>
  • Loading branch information
dai-chen committed Apr 17, 2024
1 parent 9424a79 commit 77e3369
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ abstract class FlintSparkIndexBuilder(flint: FlintSpark) {
* the index or exception occurred if validation failed
*/
protected def validateIndex(index: FlintSparkIndex): FlintSparkIndex = {
// Pre-validate Flint index name length
require(
index.name().length <= 255,
"Flint index name exceeds the maximum allowed length of 255 characters")

// Pre-validate index refresh options
FlintSparkIndexRefresh
.create(index.name(), index) // TODO: remove first argument?
.validate(flint.spark)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import org.json4s.native.Serialization
import org.opensearch.flint.core.FlintOptions
import org.opensearch.flint.core.storage.FlintOpenSearchClient
import org.opensearch.flint.spark.skipping.FlintSparkSkippingIndex.getSkippingIndexName
import org.scalatest.matchers.must.Matchers.defined
import org.scalatest.matchers.must.Matchers.{defined, have}
import org.scalatest.matchers.should.Matchers.{convertToAnyShouldWrapper, the}

import org.apache.spark.sql.Row
Expand Down Expand Up @@ -249,6 +249,18 @@ class FlintSparkSkippingIndexSqlITSuite extends FlintSparkSuite {
}
}

test("should fail if table name is too long") {
val testTableLong = "x" * 255
withTable(testTableLong) {
sql(s"CREATE TABLE $testTableLong (name STRING) USING JSON")

the[IllegalArgumentException] thrownBy {
sql(s"CREATE SKIPPING INDEX ON $testTableLong (name VALUE_SET)")
} should have message
"requirement failed: Flint index name exceeds the maximum allowed length of 255 characters"
}
}

test("create skipping index if not exists") {
sql(s"""
| CREATE SKIPPING INDEX
Expand Down

0 comments on commit 77e3369

Please sign in to comment.