Skip to content

Commit

Permalink
Extract CanonicalizeExpressionRewriter
Browse files Browse the repository at this point in the history
  • Loading branch information
Szymon Matejczyk authored and kokosing committed Jul 25, 2017
1 parent 90c4a68 commit 4456400
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* 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.CurrentTime;
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.Extract;
import com.facebook.presto.sql.tree.FunctionCall;
import com.facebook.presto.sql.tree.IfExpression;
import com.facebook.presto.sql.tree.IsNotNullPredicate;
import com.facebook.presto.sql.tree.IsNullPredicate;
import com.facebook.presto.sql.tree.NotExpression;
import com.facebook.presto.sql.tree.QualifiedName;
import com.facebook.presto.sql.tree.SearchedCaseExpression;
import com.facebook.presto.sql.tree.WhenClause;
import com.google.common.collect.ImmutableList;

import java.util.Optional;

public class CanonicalizeExpressionRewriter
extends ExpressionRewriter<Void>
{
@Override
public Expression rewriteIsNotNullPredicate(IsNotNullPredicate node, Void context, ExpressionTreeRewriter<Void> treeRewriter)
{
Expression value = treeRewriter.rewrite(node.getValue(), context);
return new NotExpression(new IsNullPredicate(value));
}

@Override
public Expression rewriteIfExpression(IfExpression node, Void context, ExpressionTreeRewriter<Void> treeRewriter)
{
Expression condition = treeRewriter.rewrite(node.getCondition(), context);
Expression trueValue = treeRewriter.rewrite(node.getTrueValue(), context);

Optional<Expression> falseValue = node.getFalseValue()
.map((value) -> treeRewriter.rewrite(value, context));

return new SearchedCaseExpression(ImmutableList.of(new WhenClause(condition, trueValue)), falseValue);
}

@Override
public Expression rewriteCurrentTime(CurrentTime node, Void context, ExpressionTreeRewriter<Void> treeRewriter)
{
if (node.getPrecision() != null) {
throw new UnsupportedOperationException("not yet implemented: non-default precision");
}

switch (node.getType()) {
case DATE:
return new FunctionCall(QualifiedName.of("current_date"), ImmutableList.of());
case TIME:
return new FunctionCall(QualifiedName.of("current_time"), ImmutableList.of());
case LOCALTIME:
return new FunctionCall(QualifiedName.of("localtime"), ImmutableList.of());
case TIMESTAMP:
return new FunctionCall(QualifiedName.of("current_timestamp"), ImmutableList.of());
case LOCALTIMESTAMP:
return new FunctionCall(QualifiedName.of("localtimestamp"), ImmutableList.of());
default:
throw new UnsupportedOperationException("not yet implemented: " + node.getType());
}
}

@Override
public Expression rewriteExtract(Extract node, Void context, ExpressionTreeRewriter<Void> treeRewriter)
{
Expression value = treeRewriter.rewrite(node.getExpression(), context);

switch (node.getField()) {
case YEAR:
return new FunctionCall(QualifiedName.of("year"), ImmutableList.of(value));
case QUARTER:
return new FunctionCall(QualifiedName.of("quarter"), ImmutableList.of(value));
case MONTH:
return new FunctionCall(QualifiedName.of("month"), ImmutableList.of(value));
case WEEK:
return new FunctionCall(QualifiedName.of("week"), ImmutableList.of(value));
case DAY:
case DAY_OF_MONTH:
return new FunctionCall(QualifiedName.of("day"), ImmutableList.of(value));
case DAY_OF_WEEK:
case DOW:
return new FunctionCall(QualifiedName.of("day_of_week"), ImmutableList.of(value));
case DAY_OF_YEAR:
case DOY:
return new FunctionCall(QualifiedName.of("day_of_year"), ImmutableList.of(value));
case YEAR_OF_WEEK:
case YOW:
return new FunctionCall(QualifiedName.of("year_of_week"), ImmutableList.of(value));
case HOUR:
return new FunctionCall(QualifiedName.of("hour"), ImmutableList.of(value));
case MINUTE:
return new FunctionCall(QualifiedName.of("minute"), ImmutableList.of(value));
case SECOND:
return new FunctionCall(QualifiedName.of("second"), ImmutableList.of(value));
case TIMEZONE_MINUTE:
return new FunctionCall(QualifiedName.of("timezone_minute"), ImmutableList.of(value));
case TIMEZONE_HOUR:
return new FunctionCall(QualifiedName.of("timezone_hour"), ImmutableList.of(value));
}

throw new UnsupportedOperationException("not yet implemented: " + node.getField());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.facebook.presto.sql.planner.PlanNodeIdAllocator;
import com.facebook.presto.sql.planner.Symbol;
import com.facebook.presto.sql.planner.SymbolAllocator;
import com.facebook.presto.sql.planner.iterative.rule.CanonicalizeExpressionRewriter;
import com.facebook.presto.sql.planner.plan.Assignments;
import com.facebook.presto.sql.planner.plan.FilterNode;
import com.facebook.presto.sql.planner.plan.JoinNode;
Expand All @@ -26,20 +27,8 @@
import com.facebook.presto.sql.planner.plan.SimplePlanRewriter;
import com.facebook.presto.sql.planner.plan.TableScanNode;
import com.facebook.presto.sql.tree.BooleanLiteral;
import com.facebook.presto.sql.tree.CurrentTime;
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.Extract;
import com.facebook.presto.sql.tree.FunctionCall;
import com.facebook.presto.sql.tree.IfExpression;
import com.facebook.presto.sql.tree.IsNotNullPredicate;
import com.facebook.presto.sql.tree.IsNullPredicate;
import com.facebook.presto.sql.tree.NotExpression;
import com.facebook.presto.sql.tree.QualifiedName;
import com.facebook.presto.sql.tree.SearchedCaseExpression;
import com.facebook.presto.sql.tree.WhenClause;
import com.google.common.collect.ImmutableList;

import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -129,91 +118,4 @@ public PlanNode visitTableScan(TableScanNode node, RewriteContext<Void> context)
originalConstraint);
}
}

private static class CanonicalizeExpressionRewriter
extends ExpressionRewriter<Void>
{
@Override
public Expression rewriteIsNotNullPredicate(IsNotNullPredicate node, Void context, ExpressionTreeRewriter<Void> treeRewriter)
{
Expression value = treeRewriter.rewrite(node.getValue(), context);
return new NotExpression(new IsNullPredicate(value));
}

@Override
public Expression rewriteIfExpression(IfExpression node, Void context, ExpressionTreeRewriter<Void> treeRewriter)
{
Expression condition = treeRewriter.rewrite(node.getCondition(), context);
Expression trueValue = treeRewriter.rewrite(node.getTrueValue(), context);

Optional<Expression> falseValue = node.getFalseValue()
.map((value) -> treeRewriter.rewrite(value, context));

return new SearchedCaseExpression(ImmutableList.of(new WhenClause(condition, trueValue)), falseValue);
}

@Override
public Expression rewriteCurrentTime(CurrentTime node, Void context, ExpressionTreeRewriter<Void> treeRewriter)
{
if (node.getPrecision() != null) {
throw new UnsupportedOperationException("not yet implemented: non-default precision");
}

switch (node.getType()) {
case DATE:
return new FunctionCall(QualifiedName.of("current_date"), ImmutableList.of());
case TIME:
return new FunctionCall(QualifiedName.of("current_time"), ImmutableList.of());
case LOCALTIME:
return new FunctionCall(QualifiedName.of("localtime"), ImmutableList.of());
case TIMESTAMP:
return new FunctionCall(QualifiedName.of("current_timestamp"), ImmutableList.of());
case LOCALTIMESTAMP:
return new FunctionCall(QualifiedName.of("localtimestamp"), ImmutableList.of());
default:
throw new UnsupportedOperationException("not yet implemented: " + node.getType());
}
}

@Override
public Expression rewriteExtract(Extract node, Void context, ExpressionTreeRewriter<Void> treeRewriter)
{
Expression value = treeRewriter.rewrite(node.getExpression(), context);

switch (node.getField()) {
case YEAR:
return new FunctionCall(QualifiedName.of("year"), ImmutableList.of(value));
case QUARTER:
return new FunctionCall(QualifiedName.of("quarter"), ImmutableList.of(value));
case MONTH:
return new FunctionCall(QualifiedName.of("month"), ImmutableList.of(value));
case WEEK:
return new FunctionCall(QualifiedName.of("week"), ImmutableList.of(value));
case DAY:
case DAY_OF_MONTH:
return new FunctionCall(QualifiedName.of("day"), ImmutableList.of(value));
case DAY_OF_WEEK:
case DOW:
return new FunctionCall(QualifiedName.of("day_of_week"), ImmutableList.of(value));
case DAY_OF_YEAR:
case DOY:
return new FunctionCall(QualifiedName.of("day_of_year"), ImmutableList.of(value));
case YEAR_OF_WEEK:
case YOW:
return new FunctionCall(QualifiedName.of("year_of_week"), ImmutableList.of(value));
case HOUR:
return new FunctionCall(QualifiedName.of("hour"), ImmutableList.of(value));
case MINUTE:
return new FunctionCall(QualifiedName.of("minute"), ImmutableList.of(value));
case SECOND:
return new FunctionCall(QualifiedName.of("second"), ImmutableList.of(value));
case TIMEZONE_MINUTE:
return new FunctionCall(QualifiedName.of("timezone_minute"), ImmutableList.of(value));
case TIMEZONE_HOUR:
return new FunctionCall(QualifiedName.of("timezone_hour"), ImmutableList.of(value));
}

throw new UnsupportedOperationException("not yet implemented: " + node.getField());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.PlanBuilder;
import com.facebook.presto.sql.tree.Expression;
import com.facebook.presto.sql.tree.ExpressionTreeRewriter;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;

public class TestCanonicalizeExpressionRewriter
{
private static final CanonicalizeExpressionRewriter rewriter = new CanonicalizeExpressionRewriter();

@Test
public void testRewriteIsNotNullPredicate()
{
assertRewritten("x is NOT NULL", "NOT (x IS NULL)");
}

@Test
public void testRewriteIfExpression()
{
assertRewritten("IF(x = 0, 0, 1)", "CASE WHEN x = 0 THEN 0 ELSE 1 END");
}

@Test
public void testRewriteCurrentTime()
{
assertRewritten("CURRENT_TIME", "\"current_time\"()");
}

@Test
public void testRewriteYearExtract()
throws Exception
{
assertRewritten("EXTRACT(YEAR FROM '2017-07-20')", "year('2017-07-20')");
}

private static void assertRewritten(String from, String to)
{
assertEquals(rewrite(PlanBuilder.expression(from)), PlanBuilder.expression(to));
}

private static Expression rewrite(Expression expression)
{
return ExpressionTreeRewriter.rewriteWith(rewriter, expression);
}
}

0 comments on commit 4456400

Please sign in to comment.