|
14 | 14 | import static org.opensearch.sql.ast.tree.Sort.SortOption.DEFAULT_DESC; |
15 | 15 | import static org.opensearch.sql.ast.tree.Sort.SortOrder.ASC; |
16 | 16 | import static org.opensearch.sql.ast.tree.Sort.SortOrder.DESC; |
17 | | -import static org.opensearch.sql.calcite.utils.PlanUtils.ROW_NUMBER_COLUMN_FOR_DEDUP; |
18 | | -import static org.opensearch.sql.calcite.utils.PlanUtils.ROW_NUMBER_COLUMN_FOR_JOIN_MAX_DEDUP; |
| 17 | +import static org.opensearch.sql.calcite.plan.rule.PPLDedupConvertRule.buildDedupNotNull; |
| 18 | +import static org.opensearch.sql.calcite.plan.rule.PPLDedupConvertRule.buildDedupOrNull; |
19 | 19 | import static org.opensearch.sql.calcite.utils.PlanUtils.ROW_NUMBER_COLUMN_FOR_MAIN; |
20 | 20 | import static org.opensearch.sql.calcite.utils.PlanUtils.ROW_NUMBER_COLUMN_FOR_RARE_TOP; |
21 | 21 | import static org.opensearch.sql.calcite.utils.PlanUtils.ROW_NUMBER_COLUMN_FOR_STREAMSTATS; |
|
146 | 146 | import org.opensearch.sql.ast.tree.Values; |
147 | 147 | import org.opensearch.sql.ast.tree.Window; |
148 | 148 | import org.opensearch.sql.calcite.plan.AliasFieldsWrappable; |
149 | | -import org.opensearch.sql.calcite.plan.LogicalSystemLimit; |
150 | | -import org.opensearch.sql.calcite.plan.LogicalSystemLimit.SystemLimitType; |
151 | 149 | import org.opensearch.sql.calcite.plan.OpenSearchConstants; |
| 150 | +import org.opensearch.sql.calcite.plan.rel.LogicalSystemLimit; |
| 151 | +import org.opensearch.sql.calcite.plan.rel.LogicalSystemLimit.SystemLimitType; |
152 | 152 | import org.opensearch.sql.calcite.utils.BinUtils; |
153 | 153 | import org.opensearch.sql.calcite.utils.JoinAndLookupUtils; |
154 | 154 | import org.opensearch.sql.calcite.utils.PPLHintUtils; |
@@ -1387,7 +1387,7 @@ public RelNode visitJoin(Join node, CalcitePlanContext context) { |
1387 | 1387 | : duplicatedFieldNames.stream() |
1388 | 1388 | .map(a -> (RexNode) context.relBuilder.field(a)) |
1389 | 1389 | .toList(); |
1390 | | - buildDedupNotNull(context, dedupeFields, allowedDuplication, true); |
| 1390 | + buildDedupNotNull(context.relBuilder, dedupeFields, allowedDuplication); |
1391 | 1391 | } |
1392 | 1392 | // add LogicalSystemLimit after dedup |
1393 | 1393 | addSysLimitForJoinSubsearch(context); |
@@ -1445,7 +1445,7 @@ public RelNode visitJoin(Join node, CalcitePlanContext context) { |
1445 | 1445 | List<RexNode> dedupeFields = |
1446 | 1446 | getRightColumnsInJoinCriteria(context.relBuilder, joinCondition); |
1447 | 1447 |
|
1448 | | - buildDedupNotNull(context, dedupeFields, allowedDuplication, true); |
| 1448 | + buildDedupNotNull(context.relBuilder, dedupeFields, allowedDuplication); |
1449 | 1449 | } |
1450 | 1450 | // add LogicalSystemLimit after dedup |
1451 | 1451 | addSysLimitForJoinSubsearch(context); |
@@ -1622,81 +1622,13 @@ public RelNode visitDedupe(Dedupe node, CalcitePlanContext context) { |
1622 | 1622 | List<RexNode> dedupeFields = |
1623 | 1623 | node.getFields().stream().map(f -> rexVisitor.analyze(f, context)).toList(); |
1624 | 1624 | if (keepEmpty) { |
1625 | | - buildDedupOrNull(context, dedupeFields, allowedDuplication); |
| 1625 | + buildDedupOrNull(context.relBuilder, dedupeFields, allowedDuplication); |
1626 | 1626 | } else { |
1627 | | - buildDedupNotNull(context, dedupeFields, allowedDuplication, false); |
| 1627 | + buildDedupNotNull(context.relBuilder, dedupeFields, allowedDuplication); |
1628 | 1628 | } |
1629 | 1629 | return context.relBuilder.peek(); |
1630 | 1630 | } |
1631 | 1631 |
|
1632 | | - private static void buildDedupOrNull( |
1633 | | - CalcitePlanContext context, List<RexNode> dedupeFields, Integer allowedDuplication) { |
1634 | | - /* |
1635 | | - * | dedup 2 a, b keepempty=true |
1636 | | - * LogicalProject(...) |
1637 | | - * +- LogicalFilter(condition=[OR(IS NULL(a), IS NULL(b), <=(_row_number_dedup_, 1))]) |
1638 | | - * +- LogicalProject(..., _row_number_dedup_=[ROW_NUMBER() OVER (PARTITION BY a, b ORDER BY a, b)]) |
1639 | | - * +- ... |
1640 | | - */ |
1641 | | - RexNode rowNumber = |
1642 | | - context |
1643 | | - .relBuilder |
1644 | | - .aggregateCall(SqlStdOperatorTable.ROW_NUMBER) |
1645 | | - .over() |
1646 | | - .partitionBy(dedupeFields) |
1647 | | - .rowsTo(RexWindowBounds.CURRENT_ROW) |
1648 | | - .as(ROW_NUMBER_COLUMN_FOR_DEDUP); |
1649 | | - context.relBuilder.projectPlus(rowNumber); |
1650 | | - RexNode _row_number_dedup_ = context.relBuilder.field(ROW_NUMBER_COLUMN_FOR_DEDUP); |
1651 | | - // Filter (isnull('a) OR isnull('b) OR '_row_number_dedup_ <= n) |
1652 | | - context.relBuilder.filter( |
1653 | | - context.relBuilder.or( |
1654 | | - context.relBuilder.or(dedupeFields.stream().map(context.relBuilder::isNull).toList()), |
1655 | | - context.relBuilder.lessThanOrEqual( |
1656 | | - _row_number_dedup_, context.relBuilder.literal(allowedDuplication)))); |
1657 | | - // DropColumns('_row_number_dedup_) |
1658 | | - context.relBuilder.projectExcept(_row_number_dedup_); |
1659 | | - } |
1660 | | - |
1661 | | - private static void buildDedupNotNull( |
1662 | | - CalcitePlanContext context, |
1663 | | - List<RexNode> dedupeFields, |
1664 | | - Integer allowedDuplication, |
1665 | | - boolean fromJoinMaxOption) { |
1666 | | - /* |
1667 | | - * | dedup 2 a, b keepempty=false |
1668 | | - * LogicalProject(...) |
1669 | | - * +- LogicalFilter(condition=[<=(_row_number_dedup_, n)])) |
1670 | | - * +- LogicalProject(..., _row_number_dedup_=[ROW_NUMBER() OVER (PARTITION BY a, b ORDER BY a, b)]) |
1671 | | - * +- LogicalFilter(condition=[AND(IS NOT NULL(a), IS NOT NULL(b))]) |
1672 | | - * +- ... |
1673 | | - */ |
1674 | | - // Filter (isnotnull('a) AND isnotnull('b)) |
1675 | | - String rowNumberAlias = |
1676 | | - fromJoinMaxOption ? ROW_NUMBER_COLUMN_FOR_JOIN_MAX_DEDUP : ROW_NUMBER_COLUMN_FOR_DEDUP; |
1677 | | - context.relBuilder.filter( |
1678 | | - context.relBuilder.and(dedupeFields.stream().map(context.relBuilder::isNotNull).toList())); |
1679 | | - // Window [row_number() windowspecdefinition('a, 'b, 'a ASC NULLS FIRST, 'b ASC NULLS FIRST, |
1680 | | - // specifiedwindowoundedpreceding$(), currentrow$())) AS _row_number_dedup_], ['a, 'b], ['a ASC |
1681 | | - // NULLS FIRST, 'b ASC NULLS FIRST] |
1682 | | - RexNode rowNumber = |
1683 | | - context |
1684 | | - .relBuilder |
1685 | | - .aggregateCall(SqlStdOperatorTable.ROW_NUMBER) |
1686 | | - .over() |
1687 | | - .partitionBy(dedupeFields) |
1688 | | - .rowsTo(RexWindowBounds.CURRENT_ROW) |
1689 | | - .as(rowNumberAlias); |
1690 | | - context.relBuilder.projectPlus(rowNumber); |
1691 | | - RexNode rowNumberField = context.relBuilder.field(rowNumberAlias); |
1692 | | - // Filter ('_row_number_dedup_ <= n) |
1693 | | - context.relBuilder.filter( |
1694 | | - context.relBuilder.lessThanOrEqual( |
1695 | | - rowNumberField, context.relBuilder.literal(allowedDuplication))); |
1696 | | - // DropColumns('_row_number_dedup_) |
1697 | | - context.relBuilder.projectExcept(rowNumberField); |
1698 | | - } |
1699 | | - |
1700 | 1632 | @Override |
1701 | 1633 | public RelNode visitWindow(Window node, CalcitePlanContext context) { |
1702 | 1634 | visitChildren(node, context); |
|
0 commit comments