Skip to content
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 @@ -101,6 +101,7 @@
import com.facebook.presto.sql.planner.iterative.rule.RewriteFilterWithExternalFunctionToProject;
import com.facebook.presto.sql.planner.iterative.rule.RewriteSpatialPartitioningAggregation;
import com.facebook.presto.sql.planner.iterative.rule.RuntimeReorderJoinSides;
import com.facebook.presto.sql.planner.iterative.rule.SimplifyCardinalityMap;
import com.facebook.presto.sql.planner.iterative.rule.SimplifyCountOverConstant;
import com.facebook.presto.sql.planner.iterative.rule.SimplifyExpressions;
import com.facebook.presto.sql.planner.iterative.rule.SimplifyRowExpressions;
Expand Down Expand Up @@ -288,6 +289,7 @@ public PlanOptimizers(
.addAll(new DesugarAtTimeZone(metadata, sqlParser).rules())
.addAll(new DesugarCurrentUser().rules())
.addAll(new DesugarTryExpression().rules())
.addAll(new SimplifyCardinalityMap().rules())
.addAll(new DesugarRowSubscript(metadata, sqlParser).rules())
.build()),
new IterativeOptimizer(
Expand Down
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));
}
Comment on lines 70 to 72
Copy link
Copy Markdown

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

Copy link
Copy Markdown
Contributor

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.

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add map_keys tests?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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)");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add capital cases and mixed cases like CaDInaliTy(....) ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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));
}
}