-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract CanonicalizeExpressionRewriter
- Loading branch information
Showing
3 changed files
with
180 additions
and
99 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
...n/java/com/facebook/presto/sql/planner/iterative/rule/CanonicalizeExpressionRewriter.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,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()); | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
...va/com/facebook/presto/sql/planner/iterative/rule/TestCanonicalizeExpressionRewriter.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,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); | ||
} | ||
} |