From 78bbfb098cf762d847fa1837a36132b00e211e81 Mon Sep 17 00:00:00 2001 From: Chen Dai Date: Fri, 10 May 2024 11:12:11 -0700 Subject: [PATCH] Fix show materialized view bug Signed-off-by: Chen Dai --- .../mv/FlintSparkMaterializedViewAstBuilder.scala | 12 +++++++----- .../spark/FlintSparkCoveringIndexSqlITSuite.scala | 2 +- .../spark/FlintSparkMaterializedViewSqlITSuite.scala | 6 ++++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/flint-spark-integration/src/main/scala/org/opensearch/flint/spark/sql/mv/FlintSparkMaterializedViewAstBuilder.scala b/flint-spark-integration/src/main/scala/org/opensearch/flint/spark/sql/mv/FlintSparkMaterializedViewAstBuilder.scala index 65ee45577..b0cc6a3a7 100644 --- a/flint-spark-integration/src/main/scala/org/opensearch/flint/spark/sql/mv/FlintSparkMaterializedViewAstBuilder.scala +++ b/flint-spark-integration/src/main/scala/org/opensearch/flint/spark/sql/mv/FlintSparkMaterializedViewAstBuilder.scala @@ -69,13 +69,15 @@ trait FlintSparkMaterializedViewAstBuilder extends FlintSparkSqlExtensionsVisito val catalogDbName = ctx.catalogDb.parts .map(part => part.getText) - .mkString("_") - val indexNamePattern = s"flint_${catalogDbName}_*" + .mkString(".") + val indexNamePattern = s"flint_${catalogDbName.replace('.', '_')}_*" flint .describeIndexes(indexNamePattern) - .collect { case mv: FlintSparkMaterializedView => - // MV name must be qualified when metadata created - Row(mv.mvName.split('.').drop(2).mkString(".")) + .collect { + // Ensure index is a MV within the given catalog and database + case mv: FlintSparkMaterializedView if mv.mvName.startsWith(s"$catalogDbName.") => + // MV name must be qualified when metadata created + Row(mv.mvName.split('.').drop(2).mkString(".")) } } } diff --git a/integ-test/src/test/scala/org/opensearch/flint/spark/FlintSparkCoveringIndexSqlITSuite.scala b/integ-test/src/test/scala/org/opensearch/flint/spark/FlintSparkCoveringIndexSqlITSuite.scala index d4eeb43d0..371c6ca2f 100644 --- a/integ-test/src/test/scala/org/opensearch/flint/spark/FlintSparkCoveringIndexSqlITSuite.scala +++ b/integ-test/src/test/scala/org/opensearch/flint/spark/FlintSparkCoveringIndexSqlITSuite.scala @@ -339,7 +339,7 @@ class FlintSparkCoveringIndexSqlITSuite extends FlintSparkSuite { deleteTestIndex(getFlintIndexName("idx_address", testTable), getSkippingIndexName(testTable)) } - test("show covering index on source table with same prefix") { + test("show covering index on source table with the same prefix") { flint .coveringIndex() .name(testIndex) diff --git a/integ-test/src/test/scala/org/opensearch/flint/spark/FlintSparkMaterializedViewSqlITSuite.scala b/integ-test/src/test/scala/org/opensearch/flint/spark/FlintSparkMaterializedViewSqlITSuite.scala index 906523696..922c078b1 100644 --- a/integ-test/src/test/scala/org/opensearch/flint/spark/FlintSparkMaterializedViewSqlITSuite.scala +++ b/integ-test/src/test/scala/org/opensearch/flint/spark/FlintSparkMaterializedViewSqlITSuite.scala @@ -265,6 +265,12 @@ class FlintSparkMaterializedViewSqlITSuite extends FlintSparkSuite { checkAnswer(sql(s"SHOW MATERIALIZED VIEW IN spark_catalog.other"), Seq.empty) } + test("show materialized view in database with the same prefix") { + flint.materializedView().name("spark_catalog.default.mv1").query(testQuery).create() + flint.materializedView().name("spark_catalog.default_test.mv2").query(testQuery).create() + checkAnswer(sql(s"SHOW MATERIALIZED VIEW IN spark_catalog.default"), Seq(Row("mv1"))) + } + test("should return emtpy when show materialized views in empty database") { checkAnswer(sql(s"SHOW MATERIALIZED VIEW IN spark_catalog.other"), Seq.empty) }