Skip to content

Commit

Permalink
Add mvQuery attribute in IndexQueryDetails (opensearch-project#2946)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomoyuki Morita <[email protected]>
  • Loading branch information
ykmr1224 authored Aug 29, 2024
1 parent 69853fe commit 83bc3d2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class IndexQueryDetails {
// materialized view special case where
// table name and mv name are combined.
private String mvName;
private String mvQuery;
private FlintIndexType indexType;

private IndexQueryDetails() {}
Expand Down Expand Up @@ -73,6 +74,11 @@ public IndexQueryDetailsBuilder mvName(String mvName) {
return this;
}

public IndexQueryDetailsBuilder mvQuery(String mvQuery) {
indexQueryDetails.mvQuery = mvQuery;
return this;
}

public IndexQueryDetailsBuilder indexType(FlintIndexType indexType) {
indexQueryDetails.indexType = indexType;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import lombok.Getter;
import lombok.experimental.UtilityClass;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.tree.ParseTree;
import org.opensearch.sql.common.antlr.CaseInsensitiveCharStream;
import org.opensearch.sql.common.antlr.SyntaxAnalysisErrorListener;
import org.opensearch.sql.common.antlr.SyntaxCheckException;
import org.opensearch.sql.spark.antlr.parser.FlintSparkSqlExtensionsBaseVisitor;
import org.opensearch.sql.spark.antlr.parser.FlintSparkSqlExtensionsLexer;
import org.opensearch.sql.spark.antlr.parser.FlintSparkSqlExtensionsParser;
import org.opensearch.sql.spark.antlr.parser.FlintSparkSqlExtensionsParser.MaterializedViewQueryContext;
import org.opensearch.sql.spark.antlr.parser.SqlBaseLexer;
import org.opensearch.sql.spark.antlr.parser.SqlBaseParser;
import org.opensearch.sql.spark.antlr.parser.SqlBaseParser.IdentifierReferenceContext;
Expand Down Expand Up @@ -353,6 +355,15 @@ public Void visitAlterMaterializedViewStatement(
return super.visitAlterMaterializedViewStatement(ctx);
}

@Override
public Void visitMaterializedViewQuery(MaterializedViewQueryContext ctx) {
int a = ctx.start.getStartIndex();
int b = ctx.stop.getStopIndex();
String query = ctx.start.getInputStream().getText(new Interval(a, b));
indexQueryDetailsBuilder.mvQuery(query);
return super.visitMaterializedViewQuery(ctx);
}

private String propertyKey(FlintSparkSqlExtensionsParser.PropertyKeyContext key) {
if (key.STRING() != null) {
return key.STRING().getText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,22 @@ void testExtractionFromFlintCoveringIndexQueries() {
}
}

@Test
void testExtractionFromCreateMVQuery() {
String mvQuery = "select * from my_glue.default.logs";
String query = "CREATE MATERIALIZED VIEW mv_1 AS " + mvQuery + " WITH (auto_refresh = true)";

assertTrue(SQLQueryUtils.isFlintExtensionQuery(query));
IndexQueryDetails indexQueryDetails = SQLQueryUtils.extractIndexDetails(query);
assertNull(indexQueryDetails.getIndexName());
assertNull(indexQueryDetails.getFullyQualifiedTableName());
assertEquals(mvQuery, indexQueryDetails.getMvQuery());
assertEquals("mv_1", indexQueryDetails.getMvName());
}

@Test
void testExtractionFromFlintMVQuery() {
String[] mvQueries = {
"CREATE MATERIALIZED VIEW mv_1 AS query=select * from my_glue.default.logs WITH"
+ " (auto_refresh = true)",
"DROP MATERIALIZED VIEW mv_1",
"VACUUM MATERIALIZED VIEW mv_1",
"ALTER MATERIALIZED VIEW mv_1 WITH (auto_refresh = false)",
Expand All @@ -200,6 +211,7 @@ void testExtractionFromFlintMVQuery() {

assertNull(indexQueryDetails.getIndexName());
assertNull(fullyQualifiedTableName);
assertNull(indexQueryDetails.getMvQuery());
assertEquals("mv_1", indexQueryDetails.getMvName());
}
}
Expand Down Expand Up @@ -428,8 +440,7 @@ public static IndexQuery index() {
}

public static IndexQuery mv() {
return new IndexQuery(
"CREATE MATERIALIZED VIEW mv_1 AS query=select * from my_glue.default.logs");
return new IndexQuery("CREATE MATERIALIZED VIEW mv_1 AS select * from my_glue.default.logs");
}

public IndexQuery withProperty(String key, String value) {
Expand Down

0 comments on commit 83bc3d2

Please sign in to comment.