-
Notifications
You must be signed in to change notification settings - Fork 204
[Enhancement] Support bare-field join criteria join on <field> shorthand
#5517
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,10 @@ | |
| import org.apache.calcite.rel.core.JoinRelType; | ||
| import org.apache.calcite.rex.RexNode; | ||
| import org.apache.calcite.util.Pair; | ||
| import org.opensearch.sql.ast.expression.And; | ||
| import org.opensearch.sql.ast.expression.Field; | ||
| import org.opensearch.sql.ast.expression.QualifiedName; | ||
| import org.opensearch.sql.ast.expression.UnresolvedExpression; | ||
| import org.opensearch.sql.ast.tree.Join; | ||
| import org.opensearch.sql.ast.tree.Lookup; | ||
| import org.opensearch.sql.calcite.CalcitePlanContext; | ||
|
|
@@ -37,6 +41,24 @@ static JoinRelType translateJoinType(Join.JoinType joinType) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Collects the names of bare single-part-field join criteria (e.g. `on a AND b` -> [a, b]). | ||
| * Returns false for anything else (qualified field, comparison, OR); {@code out} is only valid | ||
| * when this returns true, so callers must check the return before reading it. | ||
| */ | ||
| static boolean collectBareFields(UnresolvedExpression expr, List<String> out) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point. I refactored the code to Optional<List>, so therefore the list is only assembled when every node in the AND-tree is a bare field, so partial state can't exist. I also added JoinAndLookupUtilsTest with 5 cases including mixedLeftSideReturnsEmptyNoPartialState, confirming that And(Compare(...), Field("b")) returns Optional.empty(), not a list containing "b". |
||
| if (expr instanceof And and) { | ||
| return collectBareFields(and.getLeft(), out) && collectBareFields(and.getRight(), out); | ||
| } | ||
| if (expr instanceof Field field | ||
| && field.getField() instanceof QualifiedName qn | ||
| && qn.getParts().size() == 1) { | ||
| out.add(qn.getParts().get(0)); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /* ------For Lookup------ */ | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -729,8 +729,9 @@ sourceFilterArg | |
|
|
||
| // join | ||
| joinCommand | ||
| : JOIN (joinOption)* (fieldList)? right = tableOrSubqueryClause | ||
| | sqlLikeJoinType? JOIN (joinOption)* sideAlias joinHintList? joinCriteria right = tableOrSubqueryClause | ||
| // Criteria alt listed first - so `join on a` reads `on` as the criteria keyword, not a field. | ||
| : sqlLikeJoinType? JOIN (joinOption)* sideAlias joinHintList? joinCriteria right = tableOrSubqueryClause | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as before: the alternative reorder affects every join query's parse, not just the shorthand. Please still call the grammar precedence change out explicitly in the PR description so reviewers don't miss it — the new comment covers the why well, but a note that existing queries were re-verified would close it out.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do-adding a note to the PR description that the reorder affects all join queries' parse path, and that existing behavior is verified by testJoinFieldListStillParsesAsFieldList + testJoinPrefixWithoutCriteriaKeywordIsSyntaxError (158 AstBuilder + 55 CalcitePPLJoin tests). |
||
| | JOIN (joinOption)* (fieldList)? right = tableOrSubqueryClause | ||
| ; | ||
|
|
||
| sqlLikeJoinType | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens with a mixed condition like
on a AND b.x = c.y?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this, collectBareFields returns Optional.empty(), since the Compare node isn't a bare field), so the whole expression falls through to analyzeJoinCondition unchanged. I added two tests (testJoinMixedConditionFallsThrough and testJoinMixedConditionWithInequality) to prove it produces the correct plan via the normal path.