Skip to content

Commit

Permalink
Implement SHOW VIEWS support (#20413)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fly-Style committed Jan 18, 2022
1 parent 418666a commit 7e7f00b
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 12 deletions.
2 changes: 2 additions & 0 deletions hazelcast-sql/src/main/codegen/config.fmpp
Expand Up @@ -61,6 +61,7 @@ data: {
"SINK"
"SNAPSHOT"
"SUSPEND"
"VIEWS"
]

# List of keywords from "keywords" section that are not reserved.
Expand Down Expand Up @@ -398,6 +399,7 @@ data: {
"SINK"
"SNAPSHOT"
"SUSPEND"
"VIEWS"
]

# List of non-reserved keywords to add;
Expand Down
2 changes: 2 additions & 0 deletions hazelcast-sql/src/main/codegen/includes/parserImpls.ftl
Expand Up @@ -581,6 +581,8 @@ SqlShowStatement SqlShowStatement() :
<SHOW>
(
[ <EXTERNAL> ] <MAPPINGS> { target = ShowStatementTarget.MAPPINGS; }
|
<VIEWS> { target = ShowStatementTarget.VIEWS; }
|
<JOBS> { target = ShowStatementTarget.JOBS; }
)
Expand Down
Expand Up @@ -284,15 +284,24 @@ SqlResult execute(DropViewPlan plan) {

SqlResult execute(ShowStatementPlan plan) {
Stream<String> rows;
if (plan.getShowTarget() == ShowStatementTarget.MAPPINGS) {
rows = catalog.getMappingNames().stream();
} else {
assert plan.getShowTarget() == ShowStatementTarget.JOBS;
NodeEngine nodeEngine = getNodeEngine(hazelcastInstance);
JetServiceBackend jetServiceBackend = nodeEngine.getService(JetServiceBackend.SERVICE_NAME);
rows = jetServiceBackend.getJobRepository().getJobRecords().stream()
.map(record -> record.getConfig().getName())
.filter(Objects::nonNull);

switch (plan.getShowTarget()) {
case MAPPINGS:
rows = catalog.getMappingNames().stream();
break;
case VIEWS:
rows = catalog.getViewNames().stream();
break;
case JOBS:
assert plan.getShowTarget() == ShowStatementTarget.JOBS;
NodeEngine nodeEngine = getNodeEngine(hazelcastInstance);
JetServiceBackend jetServiceBackend = nodeEngine.getService(JetServiceBackend.SERVICE_NAME);
rows = jetServiceBackend.getJobRepository().getJobRecords().stream()
.map(record -> record.getConfig().getName())
.filter(Objects::nonNull);
break;
default:
throw new AssertionError("Unsupported SHOW statement target.");
}
SqlRowMetadata metadata = new SqlRowMetadata(singletonList(new SqlColumnMetadata("name", VARCHAR, false)));
InternalSerializationService serializationService = Util.getSerializationService(hazelcastInstance);
Expand Down
Expand Up @@ -31,6 +31,7 @@
public class SqlShowStatement extends SqlCall {

public static final SqlSpecialOperator SHOW_MAPPINGS = new SqlSpecialOperator("SHOW EXTERNAL MAPPINGS", SqlKind.OTHER);
public static final SqlSpecialOperator SHOW_VIEWS = new SqlSpecialOperator("SHOW VIEWS", SqlKind.OTHER);
public static final SqlSpecialOperator SHOW_JOBS = new SqlSpecialOperator("SHOW JOBS", SqlKind.OTHER);

private final ShowStatementTarget target;
Expand Down Expand Up @@ -62,10 +63,11 @@ public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
}

/**
* The argument of the SHOW command (e.g. SHOW MAPPINGS, SHOW JOBS).
* The argument of the SHOW command (e.g. SHOW MAPPINGS, SHOW VIEWS, SHOW JOBS).
*/
public enum ShowStatementTarget {
MAPPINGS(SHOW_MAPPINGS),
VIEWS(SHOW_VIEWS),
JOBS(SHOW_JOBS);

private final SqlSpecialOperator operator;
Expand Down
Expand Up @@ -138,7 +138,6 @@ public void removeMapping(String name, boolean ifExists) {
public Collection<String> getMappingNames() {
return tableStorage.mappingNames();
}

// endregion

// region view
Expand All @@ -163,6 +162,11 @@ public void removeView(String name, boolean ifExists) {
}
}

@Nonnull
public Collection<String> getViewNames() {
return tableStorage.viewNames();
}

// endregion

@Nonnull
Expand Down
Expand Up @@ -108,6 +108,14 @@ Collection<String> mappingNames() {
.collect(Collectors.toList());
}

Collection<String> viewNames() {
return storage().values()
.stream()
.filter(v -> v instanceof View)
.map(v -> ((View) v).name())
.collect(Collectors.toList());
}

void registerListener(EntryListener<String, Object> listener) {
// do not try to implicitly create ReplicatedMap
// TODO: perform this check in a single place i.e. SqlService ?
Expand Down
Expand Up @@ -227,6 +227,7 @@ public final class UnsupportedOperationVisitor extends SqlBasicVisitor<Void> {
// Extensions
SUPPORTED_OPERATORS.add(SqlOption.OPERATOR);
SUPPORTED_OPERATORS.add(SqlShowStatement.SHOW_MAPPINGS);
SUPPORTED_OPERATORS.add(SqlShowStatement.SHOW_VIEWS);
SUPPORTED_OPERATORS.add(SqlShowStatement.SHOW_JOBS);

SUPPORTED_OPERATORS.add(HazelcastSqlOperatorTable.GENERATE_SERIES);
Expand Down
Expand Up @@ -95,6 +95,21 @@ public void when_implicitMapping_then_notVisible() {
assertRowsOrdered("show mappings", emptyList());
}

@Test
public void when_showViews_empty() {
assertRowsOrdered("show views", emptyList());
}

@Test
public void test_showViews() {
List<String> viewNames = IntStream.range(0, 5).mapToObj(i -> "v" + i).collect(toList());
for (String viewName: viewNames) {
sqlService.execute("create view " + viewName + " AS SELECT 1");
}

assertRowsOrdered("show views", Util.toList(viewNames, Row::new));
}

@Test
public void test_showJobsEmpty() {
assertRowsOrdered("show jobs", emptyList());
Expand Down
Expand Up @@ -36,6 +36,8 @@
*/
public class SqlInfoSchemaTest extends SqlTestSupport {

private static final String LE = System.lineSeparator();

private static SqlService sqlService;

private final String mappingName = randomName();
Expand Down Expand Up @@ -106,7 +108,7 @@ public void test_views() {
"hazelcast",
"public",
viewName,
"SELECT \"" + mappingName + "\".\"__key\", \"" + mappingName + "\".\"__value\"\n" +
"SELECT \"" + mappingName + "\".\"__key\", \"" + mappingName + "\".\"__value\"" + LE +
"FROM \"hazelcast\".\"public\".\"" + mappingName + "\" AS \"" + mappingName + '"',
"NONE",
"NO",
Expand Down

0 comments on commit 7e7f00b

Please sign in to comment.