Skip to content

Commit

Permalink
Add support for identity views for input tables in DDlogJooqProvider (#…
Browse files Browse the repository at this point in the history
…1094)

Signed-off-by: Amy Tai <[email protected]>
  • Loading branch information
amytai authored Sep 30, 2021
1 parent 2bbe130 commit 369c7bb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
17 changes: 16 additions & 1 deletion sql/src/main/java/com/vmware/ddlog/DDlogJooqProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
public final class DDlogJooqProvider implements MockDataProvider {
static final boolean debug = false;

private static final String IDENTITY_VIEW_SUFFIX = "_view";
private static final String DDLOG_SOME = "ddlog_std::Some";
private static final String DDLOG_NONE = "ddlog_std::None";
private static final Object[] DEFAULT_BINDING = new Object[0];
Expand All @@ -91,6 +92,7 @@ public final class DDlogJooqProvider implements MockDataProvider {
private final Map<String, Map<String, Field<?>>> tablesToFieldMap = new HashMap<>();
private final Map<String, List<? extends Field<?>>> tablesToPrimaryKeys = new HashMap<>();
private final Map<String, Set<Record>> materializedViews = new ConcurrentHashMap<>();
private final Set<String> inputTableNames = new HashSet<>();
public static boolean trace = false;

public DDlogJooqProvider(final DDlogAPI dDlogAPI, final List<H2SqlStatement> sqlStatements) {
Expand All @@ -115,10 +117,17 @@ public DDlogJooqProvider(final DDlogAPI dDlogAPI, final List<H2SqlStatement> sql
if (table.getPrimaryKey() != null) {
tablesToPrimaryKeys.put(table.getName(), table.getPrimaryKey().getFields());
}
if (table.getType().equals(TableOptions.TableType.TABLE)) {
inputTableNames.add(table.getName());
}
}
}
}

public static String toIdentityViewName(String inputTableName) {
return String.format("%s_%s", inputTableName, IDENTITY_VIEW_SUFFIX);
}

public DSLContext getDslContext() {
return dslContext;
}
Expand Down Expand Up @@ -256,7 +265,13 @@ private MockResult visitSelect(final SqlSelect select) {
) {
return exception("Statement not supported: " + context.sql());
}
final String tableName = ((SqlIdentifier) select.getFrom()).getSimple();

// If the select is on an input table, redirect it to the corresponding view / output relation, if it exists
// The user of this JooqProvider is responsible for creating these identity views
String tableName = ((SqlIdentifier) select.getFrom()).getSimple();
if (inputTableNames.contains(tableName)) {
tableName = DDlogJooqProvider.toIdentityViewName(tableName);
}
try {
final Result<Record> result = fetchTable(tableName);
return new MockResult(1, result);
Expand Down
12 changes: 12 additions & 0 deletions sql/src/test/java/ddlog/JooqProviderTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,18 @@ public void testArrayAggTypes() {
assertTrue(aggResults.contains(arrayAgg3));
}

@Test
public void testIdentityViews() {
create.execute("insert into hosts values ('n1', 10, true)");
create.batch("insert into hosts values ('n54', 18, false)",
"insert into hosts values ('n9', 2, true)").execute();

final Result<Record> readFromInput = create.fetch("select * from hosts");
assertTrue(readFromInput.contains(test1));
assertTrue(readFromInput.contains(test2));
assertTrue(readFromInput.contains(test3));
}

public static <R extends SqlStatement> DDlogAPI compileAndLoad(final List<R> ddl, ToPrestoTranslator<R> translator) throws IOException, DDlogException {
final Translator t = new Translator(null);
ddl.forEach(x -> t.translateSqlStatement(translator.toPresto(x)));
Expand Down
5 changes: 5 additions & 0 deletions sql/src/test/java/ddlog/JooqProviderTestCalcite.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public static void setup() throws IOException, DDlogException {
String checkArrayType = "create view check_array_type as select distinct col3, " +
"ARRAY_AGG(capacity) over (partition by col3) as agg " +
"from base_array_table";

String identityViewName = DDlogJooqProvider.toIdentityViewName("hosts");
String hostidentityView = String.format("create view %s as select distinct * from hosts", identityViewName);

List<String> ddl = new ArrayList<>();
ddl.add(s1);
ddl.add(v2);
Expand All @@ -39,6 +43,7 @@ public static void setup() throws IOException, DDlogException {
ddl.add(checkNotNullColumns);
ddl.add(arrayTable);
ddl.add(checkArrayType);
ddl.add(hostidentityView);

ddlogAPI = compileAndLoad(
ddl.stream().map(CalciteSqlStatement::new).collect(Collectors.toList()),
Expand Down

0 comments on commit 369c7bb

Please sign in to comment.