-
Notifications
You must be signed in to change notification settings - Fork 5.5k
A simple sql to sql rewrite for cardinality() #17198
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * 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 com.facebook.presto.sql.planner.iterative.rule; | ||
|
|
||
| import static com.facebook.presto.sql.planner.iterative.rule.SimplifyCardinalityMapRewriter.rewrite; | ||
|
|
||
| public class SimplifyCardinalityMap | ||
| extends ExpressionRewriteRuleSet | ||
| { | ||
| public SimplifyCardinalityMap() | ||
| { | ||
| super((expression, context) -> rewrite(expression)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * 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 com.facebook.presto.sql.planner.iterative.rule; | ||
| import com.facebook.presto.sql.tree.Expression; | ||
| import com.facebook.presto.sql.tree.ExpressionRewriter; | ||
| import com.facebook.presto.sql.tree.ExpressionTreeRewriter; | ||
| import com.facebook.presto.sql.tree.FunctionCall; | ||
| import com.facebook.presto.sql.tree.QualifiedName; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableSet; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Transforms: | ||
| * <pre> | ||
| * - Cardinality(Map_Values(map)) | ||
| * - X | ||
| * </pre> | ||
| * Into: | ||
| * <pre> | ||
| * - Cardinality(map) | ||
| * - X | ||
| * </pre> | ||
| */ | ||
| public class SimplifyCardinalityMapRewriter | ||
| { | ||
| private static final Set<QualifiedName> MAP_FUNCTIONS = ImmutableSet.of(QualifiedName.of("map_values"), QualifiedName.of("map_keys")); | ||
|
|
||
| private SimplifyCardinalityMapRewriter() {} | ||
|
|
||
| public static Expression rewrite(Expression expression) | ||
| { | ||
| return ExpressionTreeRewriter.rewriteWith(new Visitor(), expression); | ||
| } | ||
|
|
||
| private static class Visitor | ||
| extends ExpressionRewriter<Void> | ||
| { | ||
| @Override | ||
| public Expression rewriteFunctionCall(FunctionCall node, Void context, ExpressionTreeRewriter<Void> treeRewriter) | ||
| { | ||
| ImmutableList.Builder<Expression> rewrittenArguments = ImmutableList.builder(); | ||
|
|
||
| if (node.getName().equals(QualifiedName.of("cardinality"))) { | ||
| for (Expression argument : node.getArguments()) { | ||
| if (argument instanceof FunctionCall) { | ||
| FunctionCall functionCall = (FunctionCall) argument; | ||
| if (MAP_FUNCTIONS.contains(functionCall.getName()) && functionCall.getArguments().size() == 1) { | ||
| rewrittenArguments.add(treeRewriter.rewrite(functionCall.getArguments().get(0), context)); | ||
| continue; | ||
| } | ||
| } | ||
| rewrittenArguments.add(treeRewriter.rewrite(argument, context)); | ||
| } | ||
| return newFunctionIfRewritten(node, rewrittenArguments); | ||
| } | ||
| for (Expression argument : node.getArguments()) { | ||
| rewrittenArguments.add(treeRewriter.rewrite(argument, context)); | ||
| } | ||
| return newFunctionIfRewritten(node, rewrittenArguments); | ||
| } | ||
|
|
||
| private Expression newFunctionIfRewritten(FunctionCall node, ImmutableList.Builder<Expression> rewrittenArguments) | ||
| { | ||
| if (!node.getArguments().equals(rewrittenArguments.build())) { | ||
| return new FunctionCall(node.getName(), rewrittenArguments.build()); | ||
| } | ||
| return node; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * 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 com.facebook.presto.sql.planner.iterative.rule; | ||
|
|
||
| import com.facebook.presto.sql.planner.iterative.rule.test.BaseRuleTest; | ||
| import com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import static com.facebook.presto.sql.planner.iterative.rule.SimplifyCardinalityMapRewriter.rewrite; | ||
| import static org.testng.Assert.assertEquals; | ||
|
|
||
| public class TestSimplifyCardinalityMap | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add map_keys tests?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| extends BaseRuleTest | ||
| { | ||
| @Test | ||
| public void testRewriteMapValuesCardinality() | ||
| { | ||
| assertRewritten("cardinality(map_values(m))", "cardinality(m)"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add capital cases and mixed cases like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| } | ||
|
|
||
| @Test | ||
| public void testRewriteMapValuesMixedCasesCardinality() | ||
| { | ||
| assertRewritten("CaRDinality(map_values(m))", "cardinaLITY(m)"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNoRewriteMapValuesCardinality() | ||
| { | ||
| assertRewritten("cardinality(map(ARRAY[1,3], ARRAY[2,4]))", "cardinality(map(ARRAY[1,3], ARRAY[2,4]))"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNestedRewriteMapValuesCardinality() | ||
| { | ||
| assertRewritten( | ||
| "cardinality(map(ARRAY[cardinality(map_values(m_1)),3], ARRAY[2,cardinality(map_values(m_2))]))", | ||
| "cardinality(map(ARRAY[cardinality(m_1),3], ARRAY[2,cardinality(m_2)]))"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNestedRewriteMapKeysCardinality() | ||
| { | ||
| assertRewritten( | ||
| "cardinality(map(ARRAY[cardinality(map_keys(m_1)),3], ARRAY[2,cardinality(map_keys(m_2))]))", | ||
| "cardinality(map(ARRAY[cardinality(m_1),3], ARRAY[2,cardinality(m_2)]))"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAnotherNestedRewriteMapValuesCardinality() | ||
| { | ||
| assertRewritten( | ||
| "cardinality(map(ARRAY[cardinality(map_values(map(ARRAY[1,3], ARRAY[2,4]))),3], ARRAY[2,cardinality(map_values(m_2))]))", | ||
| "cardinality(map(ARRAY[cardinality(map(ARRAY[1,3], ARRAY[2,4])),3], ARRAY[2,cardinality(m_2)]))"); | ||
| } | ||
|
|
||
| private static void assertRewritten(String from, String to) | ||
| { | ||
| assertEquals(rewrite(PlanBuilder.expression(from)), PlanBuilder.expression(to)); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol, this part is tricky. It's recursive so that we have to hold on a lot on building new objects before we can tell if we need rewrite or not. cc: @yuanzhanhku
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that is a valid concern. The current implementation makes a copy of all function argument pointers recursively even if we don't need to change it. It may cause regressions for queries with lots of expressions. That being said, I don't have an easy way to address this. One idea is to have a tree node level bitmap encode what type of nodes are contained in the tree so that we could have a O(1) function to tell if a tree contains a given node type. But this requires some large refactor.