Skip to content

Commit

Permalink
Add test for primary key join key shouldn't contains relation (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandboat authored Oct 27, 2023
1 parent b995828 commit c69ebd4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private static String getSubquerySql(Model model, List<Relationship> relationshi
// TODO: this should be checked in validator too
primaryKey.getExpression().ifPresent(expression ->
checkArgument(ExpressionRelationshipAnalyzer.getRelationships(parseExpression(expression), mdl, model).isEmpty(),
"primary key expression can't use relation"));
format("found relation in model %s primary key expression", model.getName())));

String joinKeys = relationships.stream()
.map(relationship -> {
Expand All @@ -209,7 +209,7 @@ private static String getSubquerySql(Model model, List<Relationship> relationshi
// TODO: this should be checked in validator too
joinColumn.getExpression().ifPresent(expression ->
checkArgument(ExpressionRelationshipAnalyzer.getRelationships(parseExpression(expression), mdl, model).isEmpty(),
"column in join condition can't use relation"));
format("found relation in relation join condition in %s.%s", model.getName(), joinColumn.getName())));
return format("%s AS \"%s\"", joinColumn.getExpression().orElse(joinColumn.getName()), joinColumn.getName());
})
.collect(joining(","));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public void testCycle()

// TODO: This is not allowed since accio lack of the functionality of analyzing select items in model in sql.
// Currently we treat all columns in models are required, and that cause cycles in generating WITH queries when models reference each other.
assertThatThrownBy(() -> rewrite("SELECT * FROM People", cycle), "")
assertThatThrownBy(() -> rewrite("SELECT * FROM People", cycle))
.hasMessage("found cycle in models");
}

Expand All @@ -260,6 +260,39 @@ public void testNoRewrite()
assertSqlEquals(rewrite("SELECT * FROM foo"), "SELECT * FROM foo");
}

@Test
public void testJoinKeyPrimaryIncludesRelation()
{
AccioMDL mdl = AccioMDL.fromManifest(withDefaultCatalogSchema()
.setRelationships(List.of(
relationship("WishListPeople", List.of("WishList", "People"), ONE_TO_ONE, "WishList.peopleId = People.id")))
.setModels(List.of(
model(
"People",
"SELECT * FROM People",
List.of(
column("id", "STRING", null, false),
column("gift", "STRING", null, false, "wishlist.bookId"),
relationshipColumn("wishlist", "WishList", "WishListPeople")),
"gift"),
model(
"WishList",
"SELECT * FROM WishList",
List.of(
column("id", "STRING", null, false),
column("bookId", "STRING", null, false),
column("peopleId", "STRING", null, false, "people.id"),
relationshipColumn("people", "People", "WishListPeople")),
"id")))
.build());

assertThatThrownBy(() -> rewrite("SELECT * FROM People", mdl))
.hasMessage("found relation in model People primary key expression");

assertThatThrownBy(() -> rewrite("SELECT * FROM WishList", mdl))
.hasMessage("found relation in relation join condition in WishList.peopleId");
}

private String rewrite(String sql)
{
return rewrite(sql, ACCIOMDL);
Expand Down

0 comments on commit c69ebd4

Please sign in to comment.