Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sum(distinct) pushdown for jdbc connectors #16460

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -29,15 +29,16 @@
import java.util.function.Function;

import static io.trino.matching.Capture.newCapture;
import static io.trino.plugin.base.aggregation.AggregateFunctionPatterns.basicAggregation;
import static io.trino.plugin.base.aggregation.AggregateFunctionPatterns.functionName;
import static io.trino.plugin.base.aggregation.AggregateFunctionPatterns.hasFilter;
import static io.trino.plugin.base.aggregation.AggregateFunctionPatterns.hasSortOrder;
import static io.trino.plugin.base.aggregation.AggregateFunctionPatterns.singleArgument;
import static io.trino.plugin.base.aggregation.AggregateFunctionPatterns.variable;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

/**
* Implements {@code sum(x)}
* Implements {@code sum([DISTINCT] x)}
*/
public class ImplementSum
implements AggregateFunctionRule<JdbcExpression, ParameterizedExpression>
Expand All @@ -54,7 +55,9 @@ public ImplementSum(Function<DecimalType, Optional<JdbcTypeHandle>> decimalTypeH
@Override
public Pattern<AggregateFunction> getPattern()
{
return basicAggregation()
return Pattern.typeOf(AggregateFunction.class)
.with(hasSortOrder().equalTo(false))
.with(hasFilter().equalTo(false))
chenjian2664 marked this conversation as resolved.
Show resolved Hide resolved
.with(functionName().equalTo("sum"))
.with(singleArgument().matching(variable().capturedAs(ARGUMENT)));
}
Expand All @@ -81,8 +84,9 @@ else if (aggregateFunction.getOutputType() instanceof DecimalType) {
}

ParameterizedExpression rewrittenArgument = context.rewriteExpression(argument).orElseThrow();
String function = aggregateFunction.isDistinct() ? "sum(DISTINCT %s)" : "sum(%s)";
chenjian2664 marked this conversation as resolved.
Show resolved Hide resolved
return Optional.of(new JdbcExpression(
format("sum(%s)", rewrittenArgument.expression()),
format(function, rewrittenArgument.expression()),
rewrittenArgument.parameters(),
resultTypeHandle));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ public void testCaseSensitiveAggregationPushdown()

boolean supportsPushdownWithVarcharInequality = hasBehavior(SUPPORTS_PREDICATE_PUSHDOWN_WITH_VARCHAR_INEQUALITY);
boolean supportsCountDistinctPushdown = hasBehavior(SUPPORTS_AGGREGATION_PUSHDOWN_COUNT_DISTINCT);
boolean supportsSumDistinctPushdown = hasBehavior(SUPPORTS_AGGREGATION_PUSHDOWN);

PlanMatchPattern aggregationOverTableScan = node(AggregationNode.class, node(TableScanNode.class));
PlanMatchPattern groupingAggregationOverTableScan = node(AggregationNode.class, node(ProjectNode.class, node(TableScanNode.class)));
Expand All @@ -325,7 +326,7 @@ public void testCaseSensitiveAggregationPushdown()
"(a_string varchar(1), a_char char(1), a_bigint bigint)",
ImmutableList.of(
"'A', 'A', 1",
"'B', 'B', 2",
"'B', 'B', 1",
chenjian2664 marked this conversation as resolved.
Show resolved Hide resolved
"'a', 'a', 3",
"'b', 'b', 4"))) {
// case-sensitive functions prevent pushdown
Expand Down Expand Up @@ -388,17 +389,36 @@ public void testCaseSensitiveAggregationPushdown()
supportsPushdownWithVarcharInequality && supportsCountDistinctPushdown,
node(ExchangeNode.class, node(AggregationNode.class, anyTree(node(TableScanNode.class)))))
.skippingTypesCheck()
.matches("VALUES (BIGINT '4', BIGINT '4')");
.matches("VALUES (BIGINT '4', BIGINT '3')");

assertConditionallyPushedDown(getSession(),
"SELECT count(DISTINCT a_char), count(DISTINCT a_bigint) FROM " + table.getName(),
supportsPushdownWithVarcharInequality && supportsCountDistinctPushdown,
node(ExchangeNode.class, node(AggregationNode.class, anyTree(node(TableScanNode.class)))))
.skippingTypesCheck()
.matches("VALUES (BIGINT '4', BIGINT '4')");
.matches("VALUES (BIGINT '4', BIGINT '3')");

assertConditionallyPushedDown(getSession(),
"SELECT count(DISTINCT a_string), sum(DISTINCT a_bigint) FROM " + table.getName(),
supportsPushdownWithVarcharInequality && supportsSumDistinctPushdown,
node(ExchangeNode.class, node(AggregationNode.class, anyTree(node(TableScanNode.class)))))
.skippingTypesCheck()
.matches(sumDistinctAggregationPushdownExpectedResult());
chenjian2664 marked this conversation as resolved.
Show resolved Hide resolved

assertConditionallyPushedDown(getSession(),
"SELECT count(DISTINCT a_char), sum(DISTINCT a_bigint) FROM " + table.getName(),
supportsPushdownWithVarcharInequality && supportsSumDistinctPushdown,
node(ExchangeNode.class, node(AggregationNode.class, anyTree(node(TableScanNode.class)))))
.skippingTypesCheck()
.matches(sumDistinctAggregationPushdownExpectedResult());
}
}

protected String sumDistinctAggregationPushdownExpectedResult()
{
return "VALUES (BIGINT '4', BIGINT '8')";
}

@Test
public void testAggregationWithUnsupportedResultType()
{
Expand Down Expand Up @@ -453,12 +473,22 @@ public void testDistinctAggregationPushdown()
"SELECT count(DISTINCT regionkey), sum(nationkey) FROM nation",
chenjian2664 marked this conversation as resolved.
Show resolved Hide resolved
hasBehavior(SUPPORTS_AGGREGATION_PUSHDOWN_COUNT_DISTINCT),
node(MarkDistinctNode.class, node(ExchangeNode.class, node(ExchangeNode.class, node(ProjectNode.class, node(TableScanNode.class))))));
assertConditionallyPushedDown(
withMarkDistinct,
"SELECT sum(DISTINCT regionkey), sum(DISTINCT nationkey) FROM nation",
hasBehavior(SUPPORTS_PREDICATE_PUSHDOWN),
node(MarkDistinctNode.class, node(ExchangeNode.class, node(ExchangeNode.class, node(ProjectNode.class, node(TableScanNode.class))))));
// distinct aggregation and a non-distinct aggregation
assertConditionallyPushedDown(
withMarkDistinct,
"SELECT count(DISTINCT regionkey), count(DISTINCT nationkey) FROM nation",
hasBehavior(SUPPORTS_AGGREGATION_PUSHDOWN_COUNT_DISTINCT),
node(MarkDistinctNode.class, node(ExchangeNode.class, node(ExchangeNode.class, node(ProjectNode.class, node(TableScanNode.class))))));
assertConditionallyPushedDown(
withMarkDistinct,
"SELECT sum(DISTINCT regionkey), count(nationkey) FROM nation",
hasBehavior(SUPPORTS_PREDICATE_PUSHDOWN),
node(MarkDistinctNode.class, node(ExchangeNode.class, node(ExchangeNode.class, node(ProjectNode.class, node(TableScanNode.class))))));

Session withoutMarkDistinct = Session.builder(getSession())
.setSystemProperty(USE_MARK_DISTINCT, "false")
Expand All @@ -479,12 +509,23 @@ public void testDistinctAggregationPushdown()
"SELECT count(DISTINCT regionkey), count(DISTINCT nationkey) FROM nation",
hasBehavior(SUPPORTS_AGGREGATION_PUSHDOWN_COUNT_DISTINCT),
node(AggregationNode.class, node(ExchangeNode.class, node(ExchangeNode.class, node(TableScanNode.class)))));
assertConditionallyPushedDown(
withoutMarkDistinct,
"SELECT sum(DISTINCT regionkey), sum(DISTINCT nationkey) FROM nation",
hasBehavior(SUPPORTS_PREDICATE_PUSHDOWN),
node(AggregationNode.class, node(ExchangeNode.class, node(ExchangeNode.class, node(TableScanNode.class)))));

// distinct aggregation and a non-distinct aggregation
assertConditionallyPushedDown(
withoutMarkDistinct,
"SELECT count(DISTINCT regionkey), sum(nationkey) FROM nation",
hasBehavior(SUPPORTS_AGGREGATION_PUSHDOWN_COUNT_DISTINCT),
node(AggregationNode.class, node(ExchangeNode.class, node(ExchangeNode.class, node(TableScanNode.class)))));
assertConditionallyPushedDown(
withoutMarkDistinct,
"SELECT sum(DISTINCT regionkey), sum(nationkey) FROM nation",
hasBehavior(SUPPORTS_PREDICATE_PUSHDOWN),
node(AggregationNode.class, node(ExchangeNode.class, node(ExchangeNode.class, node(TableScanNode.class)))));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ public void testImplementSum()
testImplementAggregation(
new AggregateFunction("sum", BIGINT, List.of(bigintVariable), List.of(), true, Optional.empty()),
Map.of(bigintVariable.getName(), BIGINT_COLUMN),
Optional.empty()); // distinct not supported
Optional.of("sum(DISTINCT `c_bigint`)"));

// sum(DISTINCT double)
testImplementAggregation(
new AggregateFunction("sum", DOUBLE, List.of(doubleVariable), List.of(), true, Optional.empty()),
Map.of(doubleVariable.getName(), DOUBLE_COLUMN),
Optional.of("sum(DISTINCT `c_double`)"));

// sum(bigint) FILTER (WHERE ...)
testImplementAggregation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,13 @@ public void testImplementSum()
testImplementAggregation(
new AggregateFunction("sum", BIGINT, List.of(bigintVariable), List.of(), true, Optional.empty()),
Map.of(bigintVariable.getName(), BIGINT_COLUMN),
Optional.empty()); // distinct not supported
Optional.of("sum(DISTINCT `c_bigint`)"));

// sum(DISTINCT double)
testImplementAggregation(
new AggregateFunction("sum", DOUBLE, List.of(doubleVariable), List.of(), true, Optional.empty()),
Map.of(doubleVariable.getName(), DOUBLE_COLUMN),
Optional.of("sum(DISTINCT `c_double`)"));

// sum(bigint) FILTER (WHERE ...)
testImplementAggregation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ public void testImplementSum()
testImplementAggregation(
new AggregateFunction("sum", BIGINT, List.of(bigintVariable), List.of(), true, Optional.empty()),
Map.of(bigintVariable.getName(), BIGINT_COLUMN),
Optional.empty()); // distinct not supported
Optional.of("sum(DISTINCT `c_bigint`)"));

// sum(DISTINCT double)
testImplementAggregation(
new AggregateFunction("sum", DOUBLE, List.of(bigintVariable), List.of(), true, Optional.empty()),
Map.of(bigintVariable.getName(), DOUBLE_COLUMN),
Optional.of("sum(DISTINCT `c_double`)"));

// sum(bigint) FILTER (WHERE ...)
testImplementAggregation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,12 @@ protected void verifyColumnNameLengthFailurePermissible(Throwable e)
assertThat(e).hasMessage("ORA-00972: identifier is too long\n");
}

@Override
protected String sumDistinctAggregationPushdownExpectedResult()
{
return "VALUES (BIGINT '4', DECIMAL '8')";
}

private void predicatePushdownTest(String oracleType, String oracleLiteral, String operator, String filterLiteral)
{
String tableName = ("test_pdown_" + oracleType.replaceAll("[^a-zA-Z0-9]", ""))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,13 @@ public void testImplementSum()
testImplementAggregation(
new AggregateFunction("sum", BIGINT, List.of(bigintVariable), List.of(), true, Optional.empty()),
Map.of(bigintVariable.getName(), BIGINT_COLUMN),
Optional.empty()); // distinct not supported
Optional.of("sum(DISTINCT \"c_bigint\")"));

// sum(DISTINCT double)
testImplementAggregation(
new AggregateFunction("sum", DOUBLE, List.of(bigintVariable), List.of(), true, Optional.empty()),
Map.of(bigintVariable.getName(), DOUBLE_COLUMN),
Optional.of("sum(DISTINCT \"c_double\")"));

// sum(bigint) FILTER (WHERE ...)
testImplementAggregation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ public void testImplementSum()
testImplementAggregation(
new AggregateFunction("sum", BIGINT, List.of(bigintVariable), List.of(), true, Optional.empty()),
Map.of(bigintVariable.getName(), BIGINT_COLUMN),
Optional.empty()); // distinct not supported
Optional.of("sum(DISTINCT \"c_bigint\")"));

// sum(DISTINCT double)
testImplementAggregation(
new AggregateFunction("sum", DOUBLE, List.of(doubleVariable), List.of(), true, Optional.empty()),
Map.of(doubleVariable.getName(), DOUBLE_COLUMN),
Optional.of("sum(DISTINCT \"c_double\")"));

// sum(bigint) FILTER (WHERE ...)
testImplementAggregation(
Expand Down