-
Notifications
You must be signed in to change notification settings - Fork 14
feat: add support for self-joins with sub-query #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c029bdb
feat: add support for self-joins
GurtejSohi 26932df
chore: address review comments
GurtejSohi 0378c9c
refactor: api changes for join expression
GurtejSohi fb4d86d
style: spotless formatting
GurtejSohi 72cbcff
feat: implementation for sub query joins within the same collection
GurtejSohi ca4d5f8
chore: complete the impl for sub query self join
GurtejSohi 986c619
test: add integration test for self join with sub-query
GurtejSohi a774807
chore: cleaned up code and added docs
GurtejSohi a72ddf6
chore: address review comments
GurtejSohi 9ee78fb
chore: address review comments p2
GurtejSohi 11f5d28
test: add integration test to verify self join with sub-query works f…
GurtejSohi ab72570
chore: update collection data for integration test
GurtejSohi b572d5f
refactor: move inner class in MongoLetClauseBuilder below the outer c…
GurtejSohi f7a0118
Merge branch 'main' into add-support-for-self-joins
GurtejSohi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...store/src/main/java/org/hypertrace/core/documentstore/expression/impl/JoinExpression.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package org.hypertrace.core.documentstore.expression.impl; | ||
|
|
||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Value; | ||
| import org.hypertrace.core.documentstore.expression.type.FilterTypeExpression; | ||
| import org.hypertrace.core.documentstore.expression.type.FromTypeExpression; | ||
| import org.hypertrace.core.documentstore.parser.FilterTypeExpressionVisitor; | ||
| import org.hypertrace.core.documentstore.parser.FromTypeExpressionVisitor; | ||
| import org.hypertrace.core.documentstore.parser.SelectTypeExpressionVisitor; | ||
| import org.hypertrace.core.documentstore.query.Query; | ||
|
|
||
| /** | ||
| * Expression representing a condition for filtering | ||
| * | ||
| * <p>Example: <code> | ||
| * company IN ('Traceable', 'Harness') | ||
| * </code> can be constructed as <code> | ||
| * RelationalExpression.of( IdentifierExpression.of("company"), RelationalOperator.IN, | ||
| * ConstantExpression.ofStrings("Traceable", "Harness")))); | ||
| * </code> | ||
| */ | ||
| @Value | ||
| @Builder(toBuilder = true) | ||
| @AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
| public class JoinExpression implements FromTypeExpression { | ||
| FromTypeExpression left; | ||
|
GurtejSohi marked this conversation as resolved.
Outdated
|
||
| FromTypeExpression right; | ||
| JoinType joinType; // INNER, LEFT, RIGHT, etc. | ||
|
GurtejSohi marked this conversation as resolved.
Outdated
|
||
| FilterTypeExpression onCondition; // The ON clause | ||
|
|
||
| @Override | ||
| public <T> T accept(FromTypeExpressionVisitor visitor) { | ||
| return visitor.visit(this); | ||
| } | ||
| } | ||
|
|
||
9 changes: 9 additions & 0 deletions
9
document-store/src/main/java/org/hypertrace/core/documentstore/expression/impl/JoinType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package org.hypertrace.core.documentstore.expression.impl; | ||
|
|
||
| public enum JoinType { | ||
| INNER, | ||
| LEFT, | ||
| RIGHT, | ||
| FULL, | ||
| CROSS | ||
| } |
26 changes: 26 additions & 0 deletions
26
...c/main/java/org/hypertrace/core/documentstore/expression/impl/SubQueryFromExpression.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package org.hypertrace.core.documentstore.expression.impl; | ||
|
|
||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Value; | ||
| import org.hypertrace.core.documentstore.expression.type.FromTypeExpression; | ||
| import org.hypertrace.core.documentstore.parser.FromTypeExpressionVisitor; | ||
| import org.hypertrace.core.documentstore.query.Query; | ||
|
|
||
| /** | ||
| * Allows embedding an entire Query in the FROM clause. | ||
| */ | ||
| @Value | ||
| @Builder(toBuilder = true) | ||
| @AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
| public class SubQueryFromExpression implements FromTypeExpression { | ||
|
GurtejSohi marked this conversation as resolved.
Outdated
|
||
| Query subQuery; | ||
| String alias; // e.g. "rightTable" | ||
|
|
||
| @Override | ||
| public <T> T accept(FromTypeExpressionVisitor visitor) { | ||
| return visitor.visit(this); | ||
| } | ||
| } | ||
|
|
||
24 changes: 24 additions & 0 deletions
24
.../src/main/java/org/hypertrace/core/documentstore/expression/impl/TableFromExpression.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package org.hypertrace.core.documentstore.expression.impl; | ||
|
|
||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Value; | ||
| import org.hypertrace.core.documentstore.expression.type.FromTypeExpression; | ||
| import org.hypertrace.core.documentstore.parser.FromTypeExpressionVisitor; | ||
|
|
||
| /** | ||
| * A simple wrapper for referencing a physical collection/table in the FROM clause. | ||
| */ | ||
| @Value | ||
| @Builder(toBuilder = true) | ||
| @AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
| public class TableFromExpression implements FromTypeExpression { | ||
|
GurtejSohi marked this conversation as resolved.
Outdated
|
||
| // Currently, this supports joins on the same table/collection only. In the future, we can also add a tableName field here to support joins across tables/collections. | ||
| String alias; // e.g. "leftTable" | ||
|
|
||
| @Override | ||
| public <T> T accept(FromTypeExpressionVisitor visitor) { | ||
| return visitor.visit(this); | ||
| } | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
...ore/src/main/java/org/hypertrace/core/documentstore/parser/FromTypeExpressionVisitor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,16 @@ | ||
| package org.hypertrace.core.documentstore.parser; | ||
|
|
||
| import org.hypertrace.core.documentstore.expression.impl.JoinExpression; | ||
| import org.hypertrace.core.documentstore.expression.impl.SubQueryFromExpression; | ||
| import org.hypertrace.core.documentstore.expression.impl.TableFromExpression; | ||
| import org.hypertrace.core.documentstore.expression.impl.UnnestExpression; | ||
|
|
||
| public interface FromTypeExpressionVisitor { | ||
| <T> T visit(UnnestExpression unnestExpression); | ||
|
|
||
| <T> T visit(JoinExpression joinExpression); | ||
|
|
||
| <T> T visit(TableFromExpression tableFromExpression); | ||
|
|
||
| <T> T visit(SubQueryFromExpression subQueryFromExpression); | ||
| } |
107 changes: 107 additions & 0 deletions
107
...e/src/test/java/org/hypertrace/core/documentstore/expression/impl/JoinExpressionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package org.hypertrace.core.documentstore.expression.impl; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import org.hypertrace.core.documentstore.expression.operators.AggregationOperator; | ||
| import org.hypertrace.core.documentstore.expression.operators.RelationalOperator; | ||
| import org.hypertrace.core.documentstore.expression.type.FilterTypeExpression; | ||
| import org.hypertrace.core.documentstore.expression.type.FromTypeExpression; | ||
| import org.hypertrace.core.documentstore.query.FromClause; | ||
| import org.hypertrace.core.documentstore.query.Query; | ||
| import org.hypertrace.core.documentstore.query.SelectionSpec; | ||
|
|
||
| class JoinExpressionTest { | ||
|
|
||
| /* | ||
| This is the query we want to execute: | ||
| SELECT sa.suite_id, sa.vulnerability_count, sa.scan_run_number | ||
| FROM scan_analytics sa | ||
| JOIN ( | ||
| SELECT suite_id, MAX(scan_run_number) AS latest_scan_run_number | ||
| FROM scan_analytics | ||
| GROUP BY suite_id | ||
| ) latest | ||
| ON sa.suite_id = latest.suite_id | ||
| AND sa.scan_run_number = latest.latest_scan_run_number; | ||
| */ | ||
|
|
||
| void exampleUsage() { | ||
| // The right subquery: | ||
| // SELECT suite_id, MAX(scan_run_number) AS latest_scan_run_number | ||
| // FROM scan_analytics | ||
| // GROUP BY suite_id | ||
| Query subQuery = Query.builder() | ||
| // SELECT | ||
| .addSelection( | ||
| SelectionSpec.of(IdentifierExpression.of("suite_id"), "suite_id") | ||
| ) | ||
| .addSelection( | ||
| SelectionSpec.of( | ||
| AggregateExpression.of(AggregationOperator.MAX, IdentifierExpression.of("scan_run_number")), | ||
| "latest_scan_run_number" | ||
| )) | ||
| // FROM | ||
| .addFromClause( | ||
|
GurtejSohi marked this conversation as resolved.
Outdated
|
||
| TableFromExpression.builder() | ||
| .alias("ignored") // or skip if not needed | ||
| .build() | ||
| ) | ||
| // GROUP BY | ||
| .addAggregation( | ||
| IdentifierExpression.of("suite_id") | ||
| ) | ||
| .build(); | ||
|
|
||
| // The main FROM side: "scan_analytics sa" | ||
| FromTypeExpression leftTable = TableFromExpression.builder() | ||
| .alias("sa") | ||
| .build(); | ||
|
|
||
| // The subquery side: "(...subQuery...) latest" | ||
| FromTypeExpression rightSubQuery = SubQueryFromExpression.builder() | ||
| .subQuery(subQuery) | ||
| .alias("latest") | ||
| .build(); | ||
|
|
||
| // The ON condition: | ||
| // sa.suite_id = latest.suite_id | ||
| // AND sa.scan_run_number = latest.latest_scan_run_number | ||
| FilterTypeExpression onCondition = LogicalExpression.and( | ||
| RelationalExpression.of( | ||
| IdentifierExpression.of("sa.suite_id"), | ||
| RelationalOperator.EQ, | ||
| IdentifierExpression.of("latest.suite_id") | ||
| ), | ||
| RelationalExpression.of( | ||
| IdentifierExpression.of("a.scan_run_number"), | ||
| RelationalOperator.EQ, | ||
| IdentifierExpression.of("latest.latest_scan_run_number") | ||
| ) | ||
| ); | ||
|
|
||
| JoinExpression joinExpression = JoinExpression.builder() | ||
| .left(leftTable) | ||
| .right(rightSubQuery) | ||
| .joinType(JoinType.INNER) | ||
| .onCondition(onCondition) | ||
| .build(); | ||
|
|
||
| // Now build the top-level Query: | ||
| // SELECT sa.suite_id, sa.vulnerability_count, sa.scan_run_number | ||
| Query mainQuery = Query.builder() | ||
| .addSelection( | ||
| SelectionSpec.of(IdentifierExpression.of("sa.suite_id"), "suite_id") | ||
| ) | ||
| .addSelection( | ||
| SelectionSpec.of(IdentifierExpression.of("sa.vulnerability_count"), "vulnerability_count") | ||
| ) | ||
| .addSelection( | ||
| SelectionSpec.of(IdentifierExpression.of("sa.scan_run_number"), "scan_run_number") | ||
| ) | ||
| .addFromClause(joinExpression) | ||
| .build(); | ||
|
|
||
| System.out.println(mainQuery); | ||
|
|
||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.