-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow SymbolsExtractor to accept RowExpression
- Loading branch information
Showing
3 changed files
with
161 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
68 changes: 68 additions & 0 deletions
68
...rc/main/java/com/facebook/presto/sql/relational/DefaultRowExpressionTraversalVisitor.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,68 @@ | ||
/* | ||
* 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.relational; | ||
|
||
import com.facebook.presto.spi.relation.CallExpression; | ||
import com.facebook.presto.spi.relation.ConstantExpression; | ||
import com.facebook.presto.spi.relation.InputReferenceExpression; | ||
import com.facebook.presto.spi.relation.LambdaDefinitionExpression; | ||
import com.facebook.presto.spi.relation.RowExpressionVisitor; | ||
import com.facebook.presto.spi.relation.SpecialFormExpression; | ||
import com.facebook.presto.spi.relation.VariableReferenceExpression; | ||
|
||
/** | ||
* The default visitor serves as a template for "consumer-like" tree traversal. | ||
* {@param context} is the consumer to apply customized actions on the visiting RowExpression. | ||
*/ | ||
public class DefaultRowExpressionTraversalVisitor<C> | ||
implements RowExpressionVisitor<Void, C> | ||
{ | ||
@Override | ||
public Void visitInputReference(InputReferenceExpression input, C context) | ||
{ | ||
return null; | ||
} | ||
|
||
@Override | ||
public Void visitCall(CallExpression call, C context) | ||
{ | ||
call.getArguments().forEach(argument -> argument.accept(this, context)); | ||
return null; | ||
} | ||
|
||
@Override | ||
public Void visitConstant(ConstantExpression literal, C context) | ||
{ | ||
return null; | ||
} | ||
|
||
@Override | ||
public Void visitLambda(LambdaDefinitionExpression lambda, C context) | ||
{ | ||
return null; | ||
} | ||
|
||
@Override | ||
public Void visitVariableReference(VariableReferenceExpression reference, C context) | ||
{ | ||
return null; | ||
} | ||
|
||
@Override | ||
public Void visitSpecialForm(SpecialFormExpression specialForm, C context) | ||
{ | ||
specialForm.getArguments().forEach(argument -> argument.accept(this, context)); | ||
return null; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
presto-main/src/test/java/com/facebook/presto/sql/planner/TestSymbolExtractor.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,65 @@ | ||
/* | ||
* 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; | ||
|
||
import com.facebook.presto.metadata.Metadata; | ||
import com.facebook.presto.metadata.MetadataManager; | ||
import com.facebook.presto.spi.relation.RowExpression; | ||
import com.facebook.presto.spi.type.Type; | ||
import com.facebook.presto.sql.TestingRowExpressionTranslator; | ||
import com.facebook.presto.sql.parser.ParsingOptions; | ||
import com.facebook.presto.sql.parser.SqlParser; | ||
import com.facebook.presto.sql.tree.Expression; | ||
import com.google.common.collect.ImmutableMap; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static com.facebook.presto.spi.type.BigintType.BIGINT; | ||
import static com.facebook.presto.sql.ExpressionUtils.rewriteIdentifiersToSymbolReferences; | ||
import static com.facebook.presto.sql.planner.SymbolsExtractor.extractAll; | ||
import static com.facebook.presto.sql.planner.SymbolsExtractor.extractUnique; | ||
import static com.google.common.collect.ImmutableList.toImmutableList; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
public class TestSymbolExtractor | ||
{ | ||
private static final Metadata METADATA = MetadataManager.createTestMetadataManager(); | ||
private static final TestingRowExpressionTranslator TRANSLATOR = new TestingRowExpressionTranslator(METADATA); | ||
private static final Map<Symbol, Type> SYMBOL_TYPES = ImmutableMap.of(new Symbol("a"), BIGINT, new Symbol("b"), BIGINT, new Symbol("c"), BIGINT); | ||
|
||
@Test | ||
public void testSimple() | ||
{ | ||
assertSymbols("a > b"); | ||
assertSymbols("a + b > c"); | ||
assertSymbols("sin(a) - b"); | ||
assertSymbols("sin(a) + cos(a) - b"); | ||
assertSymbols("sin(a) + cos(a) + a - b"); | ||
assertSymbols("COALESCE(a, b, 1)"); | ||
assertSymbols("a IN (a, b, c)"); | ||
assertSymbols("transform(sequence(1, 5), a -> a + b)"); | ||
assertSymbols("bigint '1'"); | ||
} | ||
|
||
private static void assertSymbols(String expression) | ||
{ | ||
Expression expected = rewriteIdentifiersToSymbolReferences(new SqlParser().createExpression(expression, new ParsingOptions())); | ||
RowExpression actual = TRANSLATOR.translate(expected, TypeProvider.copyOf(SYMBOL_TYPES)); | ||
assertEquals(extractUnique(expected), extractUnique(actual)); | ||
assertEquals( | ||
extractAll(expected).stream().sorted().collect(toImmutableList()), | ||
extractAll(actual).stream().map(variable -> new Symbol(variable.getName())).sorted().collect(toImmutableList())); | ||
} | ||
} |