use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.
the class TestSimplifyExpressions method assertSimplifies.
private static void assertSimplifies(String expression, String expected) {
Expression actualExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(expression));
Expression expectedExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(expected));
assertEquals(normalize(simplifyExpressions(actualExpression)), normalize(expectedExpression));
}
use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.
the class TestExpressionEquivalence method assertEquivalent.
private static void assertEquivalent(@Language("SQL") String left, @Language("SQL") String right) {
Expression leftExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(left));
Expression rightExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(right));
Set<Symbol> symbols = extractUnique(ImmutableList.of(leftExpression, rightExpression));
Map<Symbol, Type> types = symbols.stream().collect(toMap(identity(), TestExpressionEquivalence::generateType));
assertTrue(EQUIVALENCE.areExpressionsEquivalent(TEST_SESSION, leftExpression, rightExpression, types), String.format("Expected (%s) and (%s) to be equivalent", left, right));
assertTrue(EQUIVALENCE.areExpressionsEquivalent(TEST_SESSION, rightExpression, leftExpression, types), String.format("Expected (%s) and (%s) to be equivalent", right, left));
}
use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.
the class TestExpressionEquivalence method assertNotEquivalent.
private static void assertNotEquivalent(@Language("SQL") String left, @Language("SQL") String right) {
Expression leftExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(left));
Expression rightExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(right));
Set<Symbol> symbols = extractUnique(ImmutableList.of(leftExpression, rightExpression));
Map<Symbol, Type> types = symbols.stream().collect(toMap(identity(), TestExpressionEquivalence::generateType));
assertFalse(EQUIVALENCE.areExpressionsEquivalent(TEST_SESSION, leftExpression, rightExpression, types), String.format("Expected (%s) and (%s) to not be equivalent", left, right));
assertFalse(EQUIVALENCE.areExpressionsEquivalent(TEST_SESSION, rightExpression, leftExpression, types), String.format("Expected (%s) and (%s) to not be equivalent", right, left));
}
use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.
the class FunctionAssertions method compileScanFilterProject.
private SourceOperatorFactory compileScanFilterProject(Expression filter, Expression projection, ExpressionCompiler compiler) {
filter = new SymbolToInputRewriter(INPUT_MAPPING).rewrite(filter);
projection = new SymbolToInputRewriter(INPUT_MAPPING).rewrite(projection);
IdentityLinkedHashMap<Expression, Type> expressionTypes = getExpressionTypesFromInput(TEST_SESSION, metadata, SQL_PARSER, INPUT_TYPES, ImmutableList.of(filter, projection), emptyList());
try {
Supplier<CursorProcessor> cursorProcessor = compiler.compileCursorProcessor(toRowExpression(filter, expressionTypes), ImmutableList.of(toRowExpression(projection, expressionTypes)), SOURCE_ID);
Supplier<PageProcessor> pageProcessor = compiler.compilePageProcessor(toRowExpression(filter, expressionTypes), ImmutableList.of(toRowExpression(projection, expressionTypes)));
return new ScanFilterAndProjectOperator.ScanFilterAndProjectOperatorFactory(0, new PlanNodeId("test"), SOURCE_ID, PAGE_SOURCE_PROVIDER, cursorProcessor, pageProcessor, ImmutableList.of(), ImmutableList.of(expressionTypes.get(projection)));
} catch (Throwable e) {
if (e instanceof UncheckedExecutionException) {
e = e.getCause();
}
throw new RuntimeException("Error compiling " + projection + ": " + e.getMessage(), e);
}
}
use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.
the class FunctionAssertions method createExpression.
public static Expression createExpression(String expression, Metadata metadata, Map<Symbol, Type> symbolTypes) {
Expression parsedExpression = SQL_PARSER.createExpression(expression);
parsedExpression = rewriteIdentifiersToSymbolReferences(parsedExpression);
final ExpressionAnalysis analysis = analyzeExpressionsWithSymbols(TEST_SESSION, metadata, SQL_PARSER, symbolTypes, ImmutableList.of(parsedExpression), emptyList(), false);
Expression rewrittenExpression = ExpressionTreeRewriter.rewriteWith(new ExpressionRewriter<Void>() {
@Override
public Expression rewriteExpression(Expression node, Void context, ExpressionTreeRewriter<Void> treeRewriter) {
Expression rewrittenExpression = treeRewriter.defaultRewrite(node, context);
// cast expression if coercion is registered
Type coercion = analysis.getCoercion(node);
if (coercion != null) {
rewrittenExpression = new Cast(rewrittenExpression, coercion.getTypeSignature().toString(), false, analysis.isTypeOnlyCoercion(node));
}
return rewrittenExpression;
}
@Override
public Expression rewriteDereferenceExpression(DereferenceExpression node, Void context, ExpressionTreeRewriter<Void> treeRewriter) {
if (analysis.getColumnReferences().contains(node)) {
return rewriteExpression(node, context, treeRewriter);
}
Expression rewrittenExpression = treeRewriter.defaultRewrite(node, context);
// cast expression if coercion is registered
Type coercion = analysis.getCoercion(node);
if (coercion != null) {
rewrittenExpression = new Cast(rewrittenExpression, coercion.getTypeSignature().toString());
}
return rewrittenExpression;
}
}, parsedExpression);
return canonicalizeExpression(rewrittenExpression);
}
Aggregations