Skip to content

Commit

Permalink
feat: Add support for reduction operation in set comparison queries (…
Browse files Browse the repository at this point in the history
…as mentioned in #92)
  • Loading branch information
EpsilonPrime committed Feb 8, 2024
1 parent 4f6ed2f commit 2b70788
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/substrait/textplan/converter/PlanPrinterVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,25 @@ std::any PlanPrinterVisitor::visitSubquerySetComparison(
errorListener_->addError("Did not recognize the subquery comparison.");
return std::string("UNSUPPORTED SUBQUERY");
}
result << "ALL SUBQUERY ";
switch (query.reduction_op()) {
case ::substrait::proto::
Expression_Subquery_SetComparison_ReductionOp_REDUCTION_OP_ANY:
result << "ANY ";
break;
case ::substrait::proto::
Expression_Subquery_SetComparison_ReductionOp_REDUCTION_OP_ALL:
result << "ALL ";
break;
case ::substrait::proto::
Expression_Subquery_SetComparison_ReductionOp_REDUCTION_OP_UNSPECIFIED:
case ::substrait::proto::
Expression_Subquery_SetComparison_ReductionOp_Expression_Subquery_SetComparison_ReductionOp_INT_MIN_SENTINEL_DO_NOT_USE_:
case ::substrait::proto::
Expression_Subquery_SetComparison_ReductionOp_Expression_Subquery_SetComparison_ReductionOp_INT_MAX_SENTINEL_DO_NOT_USE_:
errorListener_->addError("Did not recognize the subquery reduction op.");
return std::string("UNSUPPORTED SUBQUERY");
}
result << "SUBQUERY ";
if (query.has_right()) {
const SymbolInfo* symbol = symbolTable_->lookupSymbolByParentQueryAndType(
currentScope_->sourceLocation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,15 @@ SubstraitPlanSubqueryRelationVisitor::visitExpressionSetComparisonSubquery(
::substrait::proto::Expression, visitExpression(ctx->expression()));
expr.mutable_subquery()->mutable_set_comparison()->set_comparison_op(
comparisonToProto(ctx->COMPARISON()->getText()));
if (ctx->ANY() != nullptr) {
expr.mutable_subquery()->mutable_set_comparison()->set_reduction_op(
::substrait::proto::
Expression_Subquery_SetComparison_ReductionOp_REDUCTION_OP_ANY);
} else {
expr.mutable_subquery()->mutable_set_comparison()->set_reduction_op(
::substrait::proto::
Expression_Subquery_SetComparison_ReductionOp_REDUCTION_OP_ALL);
}
// Next find the relation created in a previous step.
auto symbol =
symbolTable_->lookupSymbolByName(ctx->relation_ref()->getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ EXISTS: 'EXISTS';
UNIQUE: 'UNIQUE';
IN: 'IN';
ALL: 'ALL';
ANY: 'ANY';
COMPARISON: 'EQ'|'NE'|'LT'|'GT'|'LE'|'GE';

VIRTUAL_TABLE: 'VIRTUAL_TABLE';
Expand Down
3 changes: 2 additions & 1 deletion src/substrait/textplan/parser/grammar/SubstraitPlanParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ expression
| SUBQUERY relation_ref # expressionScalarSubquery
| expression_list IN SUBQUERY relation_ref # expressionInPredicateSubquery
| (UNIQUE|EXISTS) IN SUBQUERY relation_ref # expressionSetPredicateSubquery
| expression COMPARISON ALL SUBQUERY relation_ref # expressionSetComparisonSubquery
| expression COMPARISON (ALL|ANY) SUBQUERY relation_ref # expressionSetComparisonSubquery
;

expression_list
Expand Down Expand Up @@ -235,5 +235,6 @@ simple_id
| EMIT
| NAMED
| ALL
| ANY
| COMPARISON
;
2 changes: 1 addition & 1 deletion src/substrait/textplan/parser/tests/TextPlanParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ std::vector<TestCase> getTestCases() {
"1:0 → extraneous input 'relation' expecting {<EOF>, "
"'EXTENSION_SPACE', 'NAMED', 'SCHEMA', 'PIPELINES', 'FILTER', "
"'GROUPING', 'MEASURE', 'SORT', 'COUNT', 'TYPE', 'EMIT', "
"'ALL', COMPARISON, 'SOURCE', 'ROOT', 'NULL', IDENTIFIER}",
"'ALL', 'ANY', COMPARISON, 'SOURCE', 'ROOT', 'NULL', IDENTIFIER}",
"1:24 → mismatched input '{' expecting 'RELATION'",
"1:9 → Unrecognized relation type: notyperelation",
"1:9 → Internal error: Previously encountered symbol "
Expand Down

0 comments on commit 2b70788

Please sign in to comment.