-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support aggregation pushdown in MySQL connector
- Loading branch information
Showing
5 changed files
with
317 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
presto-mysql/src/main/java/io/prestosql/plugin/mysql/ImplementAvgBigint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.prestosql.plugin.mysql; | ||
|
||
import io.prestosql.matching.Capture; | ||
import io.prestosql.matching.Captures; | ||
import io.prestosql.matching.Pattern; | ||
import io.prestosql.plugin.jdbc.JdbcColumnHandle; | ||
import io.prestosql.plugin.jdbc.JdbcExpression; | ||
import io.prestosql.plugin.jdbc.JdbcTypeHandle; | ||
import io.prestosql.plugin.jdbc.expression.AggregateFunctionRule; | ||
import io.prestosql.spi.connector.AggregateFunction; | ||
import io.prestosql.spi.expression.Variable; | ||
|
||
import java.sql.Types; | ||
import java.util.Optional; | ||
|
||
import static com.google.common.base.Verify.verify; | ||
import static com.google.common.base.Verify.verifyNotNull; | ||
import static io.prestosql.matching.Capture.newCapture; | ||
import static io.prestosql.plugin.jdbc.expression.AggregateFunctionPatterns.basicAggregation; | ||
import static io.prestosql.plugin.jdbc.expression.AggregateFunctionPatterns.expressionType; | ||
import static io.prestosql.plugin.jdbc.expression.AggregateFunctionPatterns.functionName; | ||
import static io.prestosql.plugin.jdbc.expression.AggregateFunctionPatterns.singleInput; | ||
import static io.prestosql.plugin.jdbc.expression.AggregateFunctionPatterns.variable; | ||
import static io.prestosql.spi.type.BigintType.BIGINT; | ||
import static io.prestosql.spi.type.DoubleType.DOUBLE; | ||
import static java.lang.String.format; | ||
|
||
public class ImplementAvgBigint | ||
implements AggregateFunctionRule | ||
{ | ||
private static final Capture<Variable> INPUT = newCapture(); | ||
|
||
@Override | ||
public Pattern<AggregateFunction> getPattern() | ||
{ | ||
return basicAggregation() | ||
.with(functionName().equalTo("avg")) | ||
.with(singleInput().matching( | ||
variable() | ||
.with(expressionType().matching(type -> type == BIGINT)) | ||
.capturedAs(INPUT))); | ||
} | ||
|
||
@Override | ||
public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext context) | ||
{ | ||
Variable input = captures.get(INPUT); | ||
JdbcColumnHandle columnHandle = (JdbcColumnHandle) context.getAssignments().get(input.getName()); | ||
verifyNotNull(columnHandle, "Unbound variable: %s", input); | ||
verify(aggregateFunction.getOutputType() == DOUBLE); | ||
|
||
return Optional.of(new JdbcExpression( | ||
format("avg((%s * 1.0))", columnHandle.toSqlExpression(context.getIdentifierQuote())), | ||
new JdbcTypeHandle(Types.DOUBLE, Optional.of("double"), 0, 0, Optional.empty(), Optional.empty()))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
presto-mysql/src/test/java/io/prestosql/plugin/mysql/TestMySqlClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.prestosql.plugin.mysql; | ||
|
||
import io.prestosql.plugin.jdbc.BaseJdbcConfig; | ||
import io.prestosql.plugin.jdbc.ColumnMapping; | ||
import io.prestosql.plugin.jdbc.JdbcClient; | ||
import io.prestosql.plugin.jdbc.JdbcColumnHandle; | ||
import io.prestosql.plugin.jdbc.JdbcExpression; | ||
import io.prestosql.plugin.jdbc.JdbcTypeHandle; | ||
import io.prestosql.spi.connector.AggregateFunction; | ||
import io.prestosql.spi.connector.ColumnHandle; | ||
import io.prestosql.spi.expression.ConnectorExpression; | ||
import io.prestosql.spi.expression.Variable; | ||
import io.prestosql.spi.type.TypeManager; | ||
import io.prestosql.type.InternalTypeManager; | ||
import org.testng.annotations.Test; | ||
|
||
import java.sql.Types; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static io.prestosql.metadata.MetadataManager.createTestMetadataManager; | ||
import static io.prestosql.spi.type.BigintType.BIGINT; | ||
import static io.prestosql.spi.type.BooleanType.BOOLEAN; | ||
import static io.prestosql.spi.type.DoubleType.DOUBLE; | ||
import static io.prestosql.testing.TestingConnectorSession.SESSION; | ||
import static io.prestosql.testing.assertions.Assert.assertEquals; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
public class TestMySqlClient | ||
{ | ||
private static final TypeManager TYPE_MANAGER = new InternalTypeManager(createTestMetadataManager()); | ||
|
||
private static final JdbcColumnHandle BIGINT_COLUMN = | ||
JdbcColumnHandle.builder() | ||
.setColumnName("c_bigint") | ||
.setColumnType(BIGINT) | ||
.setJdbcTypeHandle(new JdbcTypeHandle(Types.BIGINT, Optional.of("int8"), 0, 0, Optional.empty())) | ||
.build(); | ||
|
||
private static final JdbcColumnHandle DOUBLE_COLUMN = | ||
JdbcColumnHandle.builder() | ||
.setColumnName("c_double") | ||
.setColumnType(DOUBLE) | ||
.setJdbcTypeHandle(new JdbcTypeHandle(Types.DOUBLE, Optional.of("double"), 0, 0, Optional.empty())) | ||
.build(); | ||
|
||
private static final JdbcClient JDBC_CLIENT = new MySqlClient( | ||
new BaseJdbcConfig(), | ||
identity -> { | ||
throw new UnsupportedOperationException(); | ||
}, | ||
TYPE_MANAGER); | ||
|
||
@Test | ||
public void testImplementCount() | ||
{ | ||
Variable bigintVariable = new Variable("v_bigint", BIGINT); | ||
Variable doubleVariable = new Variable("v_double", BIGINT); | ||
Optional<ConnectorExpression> filter = Optional.of(new Variable("a_filter", BOOLEAN)); | ||
|
||
// count(*) | ||
testImplementAggregation( | ||
new AggregateFunction("count", BIGINT, List.of(), List.of(), false, Optional.empty()), | ||
Map.of(), | ||
Optional.of("count(*)")); | ||
|
||
// count(bigint) | ||
testImplementAggregation( | ||
new AggregateFunction("count", BIGINT, List.of(bigintVariable), List.of(), false, Optional.empty()), | ||
Map.of(bigintVariable.getName(), BIGINT_COLUMN), | ||
Optional.of("count(`c_bigint`)")); | ||
|
||
// count(double) | ||
testImplementAggregation( | ||
new AggregateFunction("count", BIGINT, List.of(doubleVariable), List.of(), false, Optional.empty()), | ||
Map.of(doubleVariable.getName(), DOUBLE_COLUMN), | ||
Optional.of("count(`c_double`)")); | ||
|
||
// count(DISTINCT bigint) | ||
testImplementAggregation( | ||
new AggregateFunction("count", BIGINT, List.of(bigintVariable), List.of(), true, Optional.empty()), | ||
Map.of(bigintVariable.getName(), BIGINT_COLUMN), | ||
Optional.empty()); | ||
|
||
// count() FILTER (WHERE ...) | ||
|
||
testImplementAggregation( | ||
new AggregateFunction("count", BIGINT, List.of(), List.of(), false, filter), | ||
Map.of(), | ||
Optional.empty()); | ||
|
||
// count(bigint) FILTER (WHERE ...) | ||
testImplementAggregation( | ||
new AggregateFunction("count", BIGINT, List.of(bigintVariable), List.of(), false, filter), | ||
Map.of(bigintVariable.getName(), BIGINT_COLUMN), | ||
Optional.empty()); | ||
} | ||
|
||
@Test | ||
public void testImplementSum() | ||
{ | ||
Variable bigintVariable = new Variable("v_bigint", BIGINT); | ||
Variable doubleVariable = new Variable("v_double", DOUBLE); | ||
Optional<ConnectorExpression> filter = Optional.of(new Variable("a_filter", BOOLEAN)); | ||
|
||
// sum(bigint) | ||
testImplementAggregation( | ||
new AggregateFunction("sum", BIGINT, List.of(bigintVariable), List.of(), false, Optional.empty()), | ||
Map.of(bigintVariable.getName(), BIGINT_COLUMN), | ||
Optional.of("sum(`c_bigint`)")); | ||
|
||
// sum(double) | ||
testImplementAggregation( | ||
new AggregateFunction("sum", DOUBLE, List.of(doubleVariable), List.of(), false, Optional.empty()), | ||
Map.of(doubleVariable.getName(), DOUBLE_COLUMN), | ||
Optional.of("sum(`c_double`)")); | ||
|
||
// sum(DISTINCT bigint) | ||
testImplementAggregation( | ||
new AggregateFunction("sum", BIGINT, List.of(bigintVariable), List.of(), true, Optional.empty()), | ||
Map.of(bigintVariable.getName(), BIGINT_COLUMN), | ||
Optional.empty()); // distinct not supported | ||
|
||
// sum(bigint) FILTER (WHERE ...) | ||
testImplementAggregation( | ||
new AggregateFunction("sum", BIGINT, List.of(bigintVariable), List.of(), false, filter), | ||
Map.of(bigintVariable.getName(), BIGINT_COLUMN), | ||
Optional.empty()); // filter not supported | ||
} | ||
|
||
private void testImplementAggregation(AggregateFunction aggregateFunction, Map<String, ColumnHandle> assignments, Optional<String> expectedExpression) | ||
{ | ||
Optional<JdbcExpression> result = JDBC_CLIENT.implementAggregation(SESSION, aggregateFunction, assignments); | ||
if (expectedExpression.isEmpty()) { | ||
assertThat(result).isEmpty(); | ||
} | ||
else { | ||
assertThat(result).isPresent(); | ||
assertEquals(result.get().getExpression(), expectedExpression.get()); | ||
Optional<ColumnMapping> columnMapping = JDBC_CLIENT.toPrestoType(SESSION, null, result.get().getJdbcTypeHandle()); | ||
assertTrue(columnMapping.isPresent(), "No mapping for: " + result.get().getJdbcTypeHandle()); | ||
assertEquals(columnMapping.get().getType(), aggregateFunction.getOutputType()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters