|
66 | 66 | import com.google.common.collect.ImmutableList; |
67 | 67 | import com.google.common.collect.ImmutableSet; |
68 | 68 |
|
| 69 | +import java.util.ArrayList; |
69 | 70 | import java.util.Collection; |
70 | 71 | import java.util.HashMap; |
71 | 72 | import java.util.LinkedHashMap; |
@@ -400,7 +401,11 @@ private Pair<LogicalPlan, Optional<Expression>> addApply(SubqueryExpr subquery, |
400 | 401 | Optional<MarkJoinSlotReference> markJoinSlot = subqueryToMarkJoinSlot.get(subquery); |
401 | 402 | boolean needAddScalarSubqueryOutputToProjects = isConjunctContainsScalarSubqueryOutput( |
402 | 403 | subquery, conjunct, isProject, singleSubquery); |
403 | | - boolean needRuntimeAssertCount = false; |
| 404 | + // for scalar subquery, we need ensure it output at most 1 row |
| 405 | + // by doing that, we add an aggregate function any_value() to the project list |
| 406 | + // we use needRuntimeAnyValue to indicate if any_value() is needed |
| 407 | + // if needRuntimeAnyValue is true, we will add it to the project list |
| 408 | + boolean needRuntimeAnyValue = false; |
404 | 409 | NamedExpression oldSubqueryOutput = subquery.getQueryPlan().getOutput().get(0); |
405 | 410 | Slot countSlot = null; |
406 | 411 | Slot anyValueSlot = null; |
@@ -450,19 +455,25 @@ private Pair<LogicalPlan, Optional<Expression>> addApply(SubqueryExpr subquery, |
450 | 455 | // select (select t2.c1 from t2 where t2.c2 = t1.c2) from t1; |
451 | 456 | // the original output of the correlate subquery is t2.c1, after adding a scalar agg, it will be |
452 | 457 | // select (select count(*), any_value(t2.c1) from t2 where t2.c2 = t1.c2) from t1; |
453 | | - Alias countAlias = new Alias(new Count()); |
454 | 458 | Alias anyValueAlias = new Alias(new AnyValue(oldSubqueryOutput)); |
455 | | - LogicalAggregate<Plan> aggregate = new LogicalAggregate<>(ImmutableList.of(), |
456 | | - ImmutableList.of(countAlias, anyValueAlias), subquery.getQueryPlan()); |
457 | | - countSlot = countAlias.toSlot(); |
| 459 | + LogicalAggregate<Plan> aggregate; |
| 460 | + if (((ScalarSubquery) subquery).limitOneIsEliminated()) { |
| 461 | + aggregate = new LogicalAggregate<>(ImmutableList.of(), |
| 462 | + ImmutableList.of(anyValueAlias), subquery.getQueryPlan()); |
| 463 | + } else { |
| 464 | + Alias countAlias = new Alias(new Count()); |
| 465 | + countSlot = countAlias.toSlot(); |
| 466 | + aggregate = new LogicalAggregate<>(ImmutableList.of(), |
| 467 | + ImmutableList.of(countAlias, anyValueAlias), subquery.getQueryPlan()); |
| 468 | + } |
458 | 469 | anyValueSlot = anyValueAlias.toSlot(); |
459 | 470 | subquery = subquery.withSubquery(aggregate); |
460 | 471 | if (conjunct.isPresent()) { |
461 | 472 | Map<Expression, Expression> replaceMap = new HashMap<>(); |
462 | 473 | replaceMap.put(oldSubqueryOutput, anyValueSlot); |
463 | 474 | newConjunct = Optional.of(ExpressionUtils.replace(conjunct.get(), replaceMap)); |
464 | 475 | } |
465 | | - needRuntimeAssertCount = true; |
| 476 | + needRuntimeAnyValue = true; |
466 | 477 | } |
467 | 478 | } |
468 | 479 | LogicalApply.SubQueryType subQueryType; |
@@ -493,21 +504,33 @@ private Pair<LogicalPlan, Optional<Expression>> addApply(SubqueryExpr subquery, |
493 | 504 | projects.addAll(childPlan.getOutput()); |
494 | 505 | // markJoinSlotReference |
495 | 506 | markJoinSlot.map(projects::add); |
| 507 | + LogicalProject logicalProject; |
496 | 508 | if (needAddScalarSubqueryOutputToProjects) { |
497 | | - if (needRuntimeAssertCount) { |
| 509 | + if (needRuntimeAnyValue) { |
498 | 510 | // if we create a new subquery in previous step, we need add the any_value() and assert_true() |
499 | 511 | // into the project list. So BE will use assert_true to check if the subquery return only 1 row |
500 | 512 | projects.add(anyValueSlot); |
501 | | - projects.add(new Alias(new AssertTrue( |
502 | | - ExpressionUtils.or(new IsNull(countSlot), |
503 | | - new LessThanEqual(countSlot, new IntegerLiteral(1))), |
504 | | - new VarcharLiteral("correlate scalar subquery must return only 1 row")))); |
| 513 | + if (countSlot != null) { |
| 514 | + List<NamedExpression> upperProjects = new ArrayList<>(); |
| 515 | + upperProjects.addAll(projects.build()); |
| 516 | + projects.add(new Alias(new AssertTrue( |
| 517 | + ExpressionUtils.or(new IsNull(countSlot), |
| 518 | + new LessThanEqual(countSlot, new IntegerLiteral(1))), |
| 519 | + new VarcharLiteral("correlate scalar subquery must return only 1 row")))); |
| 520 | + logicalProject = new LogicalProject(projects.build(), newApply); |
| 521 | + logicalProject = new LogicalProject(upperProjects, logicalProject); |
| 522 | + } else { |
| 523 | + logicalProject = new LogicalProject(projects.build(), newApply); |
| 524 | + } |
505 | 525 | } else { |
506 | 526 | projects.add(oldSubqueryOutput); |
| 527 | + logicalProject = new LogicalProject(projects.build(), newApply); |
507 | 528 | } |
| 529 | + } else { |
| 530 | + logicalProject = new LogicalProject(projects.build(), newApply); |
508 | 531 | } |
509 | 532 |
|
510 | | - return Pair.of(new LogicalProject(projects.build(), newApply), newConjunct); |
| 533 | + return Pair.of(logicalProject, newConjunct); |
511 | 534 | } |
512 | 535 |
|
513 | 536 | private boolean isConjunctContainsScalarSubqueryOutput( |
|
0 commit comments