Skip to content

Conversation

RyanL1997
Copy link
Collaborator

@RyanL1997 RyanL1997 commented Sep 22, 2025

Signed-off-by: Jialiang Liang [email protected]

Description

Better error handling for known limitation of sql JOIN

example:

curl -X POST "localhost:9200/_plugins/_sql" -H 'Content-Type: application/json' -d'
  {
    "query": "SELECT a.gender FROM accounts a JOIN accounts b ON a.account_number = b.account_number GROUP BY a.gender"
  }'


{
  "error": {
    "reason": "Invalid SQL query",
    "details": "JOIN queries do not support aggregations on the joined result. For more information, see https://docs.opensearch.org/latest/search-plugins/sql/limitation/#join-does-not-support-aggregations-on-the-joined-result",
    "type": "SqlParseException"
  },
  "status": 400
}

Related Issues

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added.
  • New PPL command checklist all confirmed.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff or -s.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@RyanL1997 RyanL1997 added enhancement New feature or request SQL labels Sep 22, 2025
@RyanL1997 RyanL1997 changed the title [Enhancement] Better error handling for known limitation of sql JOIN [Enhancement] Add error handling for known limitation of sql JOIN Sep 22, 2025
Signed-off-by: Jialiang Liang <[email protected]>
Copy link
Collaborator

@Swiddis Swiddis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few suggestions -- I think this could be designed better if changing JoinSelect doesn't cause cascading breakage, otherwise this approach is fine.

throws SqlParseException {
String errorMessage =
"JOIN queries do not support aggregations on the joined result. For more information, see"
+ " https://docs.opensearch.org/latest/search-plugins/sql/limitation/#join-does-not-support-aggregations-on-the-joined-result";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Can we fetch the cluster version and use it instead of latest?

If we ever fix this constraint, this error message will be invalid on all the older versions that output it

updateJoinLimit(query.getLimit(), joinSelect);

// todo: throw error feature not supported: no group bys on joins ?
validateJoinWithoutAggregations(query);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (if-minor): Should we move a lot of this logic to the construction of the JoinSelect?

Seems weird to construct the object and then use its setters to fill in everything, instead of just passing in the subquery block and letting it figure itself out. This also would guarantee that this validation is applied everywhere that JoinSelect is constructed.

/** JOIN Aggregation Validation Tests */
private void expectJoinAggregationException() {
thrown.expect(SqlParseException.class);
thrown.expectMessage("JOIN queries do not support aggregations on the joined result.");
Copy link
Collaborator

@Swiddis Swiddis Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: This is a hard-coded copy of the validation's private error message

We should just make it public & reference it like we do with most of the other strings. I've had a few cases now of trying to clarify error messages and getting several pointless test failures for it


@Test
public void joinWithGroupByShouldThrowException() throws SqlParseException {
expectJoinAggregationException();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: Nice idea to make the exception expectation a shared function

I probably would have done a bunch of thrown.expect && thrown.expectMessage(ERROR_MSG) copies, this is nicer

}

@Test
public void joinWithSumFunctionShouldThrowException() throws SqlParseException {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Since we're just leaning on Select.AGGREGATE_FUNCTIONS, we could reduce all these aggregation cases to a single table-driven test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request SQL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Enhancement] Better error handling for JOIN query limitation
2 participants