Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@
import org.apache.calcite.util.Holder;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.opensearch.sql.ast.AbstractNodeVisitor;
import org.opensearch.sql.ast.Node;
import org.opensearch.sql.ast.expression.AllFields;
import org.opensearch.sql.ast.expression.Argument;
import org.opensearch.sql.ast.expression.Compare;
import org.opensearch.sql.ast.expression.Field;
import org.opensearch.sql.ast.expression.Let;
import org.opensearch.sql.ast.expression.Not;
import org.opensearch.sql.ast.expression.QualifiedName;
import org.opensearch.sql.ast.expression.UnresolvedExpression;
import org.opensearch.sql.ast.expression.subquery.ExistsSubquery;
Expand Down Expand Up @@ -111,31 +110,38 @@ public RelNode visitFilter(Filter node, CalcitePlanContext context) {
return context.relBuilder.peek();
}

private boolean containsExistsSubquery(Object condition) {
if (condition instanceof ExistsSubquery) {
private boolean containsExistsSubquery(Node expr) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

How about combining this 2 flags together? Will we use them separately in specific cases?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

if (expr == null) {
return false;
}
if (expr instanceof ExistsSubquery) {
return true;
}
if (condition instanceof Not n) {
return containsExistsSubquery(n.getExpression());
if (expr instanceof Let l) {
return containsExistsSubquery(l.getExpression());
}
if (condition instanceof Compare c) {
return containsExistsSubquery(c.getLeft()) || containsExistsSubquery(c.getRight());
for (Node child : expr.getChild()) {
if (containsExistsSubquery(child)) {
return true;
}
}
return false;
}

private boolean containsScalarSubquery(Object expr) {
private boolean containsScalarSubquery(Node expr) {
if (expr == null) {
return false;
}
if (expr instanceof ScalarSubquery) {
return true;
}
if (expr instanceof Not n) {
return containsScalarSubquery(n.getExpression());
}
if (expr instanceof Let l) {
return containsScalarSubquery(l.getExpression());
}
if (expr instanceof Compare c) {
return containsScalarSubquery(c.getLeft()) || containsScalarSubquery(c.getRight());
for (Node child : expr.getChild()) {
if (containsScalarSubquery(child)) {
return true;
}
}
return false;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should we support more cases than Not, Let and Compare?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure, addressed in latest commit, please check it again

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.junit.Test;
import org.opensearch.client.Request;

public class CalcitePPLScalaSubqueryIT extends CalcitePPLIntegTestCase {
public class CalcitePPLScalarSubqueryIT extends CalcitePPLIntegTestCase {

@Override
public void init() throws IOException {
Expand Down Expand Up @@ -244,7 +244,7 @@ public void testDisjunctiveCorrelatedScalarSubquery() {
}

@Test
public void testTwoCorrelatedScalarSubqueriesInOr() {
public void testTwoUncorrelatedScalarSubqueriesInOr() {
JSONObject result =
executeQuery(
String.format(
Expand All @@ -262,6 +262,31 @@ public void testTwoCorrelatedScalarSubqueriesInOr() {
verifyDataRows(result, rows(1002, "John"), rows(1006, "Tommy"));
}

@Test
public void testTwoCorrelatedScalarSubqueriesInOr() {
JSONObject result =
executeQuery(
String.format(
"""
source = %s
| where id = [
source = %s | where id = uid | stats max(uid)
] OR id = [
source = %s | sort uid | where department = 'DATA' | stats min(uid)
]
| fields id, name
""",
TEST_INDEX_WORKER, TEST_INDEX_WORK_INFORMATION, TEST_INDEX_WORK_INFORMATION));
verifySchema(result, schema("id", "integer"), schema("name", "string"));
verifyDataRows(
result,
rows(1000, "Jake"),
rows(1002, "John"),
rows(1003, "David"),
rows(1005, "Jane"),
rows(1006, "Tommy"));
}

@Test
public void testNestedScalarSubquery() {
JSONObject result =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public void testTwoScalarSubqueriesInOr() {
+ " LogicalSort(sort0=[$2], dir0=[DESC])\n"
+ " LogicalFilter(condition=[>($1, 1000.0E0:DOUBLE)])\n"
+ " LogicalTableScan(table=[[scott, SALGRADE]])\n"
+ "})))])\n"
+ "})))], variablesSet=[[$cor0]])\n"
+ " LogicalTableScan(table=[[scott, EMP]])\n";
verifyLogical(root, expectedLogical);

Expand Down
Loading