Search in sources :

Example 11 with TypeProvider

use of com.facebook.presto.sql.planner.TypeProvider in project presto by prestodb.

the class InlineProjections method extractInliningTargets.

private Sets.SetView<VariableReferenceExpression> extractInliningTargets(ProjectNode parent, ProjectNode child, Context context) {
    // candidates for inlining are
    // 1. references to simple constants
    // 2. references to complex expressions that
    // a. are not inputs to try() expressions
    // b. appear only once across all expressions
    // c. are not identity projections
    // which come from the child, as opposed to an enclosing scope.
    Set<VariableReferenceExpression> childOutputSet = ImmutableSet.copyOf(child.getOutputVariables());
    TypeProvider types = context.getVariableAllocator().getTypes();
    Map<VariableReferenceExpression, Long> dependencies = parent.getAssignments().getExpressions().stream().flatMap(expression -> extractInputs(expression, context.getVariableAllocator().getTypes()).stream()).filter(childOutputSet::contains).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    // find references to simple constants
    Set<VariableReferenceExpression> constants = dependencies.keySet().stream().filter(input -> isConstant(child.getAssignments().get(input))).collect(toSet());
    // exclude any complex inputs to TRY expressions. Inlining them would potentially
    // change the semantics of those expressions
    Set<VariableReferenceExpression> tryArguments = parent.getAssignments().getExpressions().stream().flatMap(expression -> extractTryArguments(expression, types).stream()).collect(toSet());
    Set<VariableReferenceExpression> singletons = dependencies.entrySet().stream().filter(// reference appears just once across all expressions in parent project node
    entry -> entry.getValue() == 1).filter(// they are not inputs to TRY. Otherwise, inlining might change semantics
    entry -> !tryArguments.contains(entry.getKey())).filter(// skip identities, otherwise, this rule will keep firing forever
    entry -> !isIdentity(child.getAssignments(), entry.getKey())).map(Map.Entry::getKey).collect(toSet());
    return Sets.union(singletons, constants);
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) Builder(com.facebook.presto.spi.plan.Assignments.Builder) OriginalExpressionUtils(com.facebook.presto.sql.relational.OriginalExpressionUtils) OriginalExpressionUtils.isExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.isExpression) Captures(com.facebook.presto.matching.Captures) Assignments(com.facebook.presto.spi.plan.Assignments) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) AstUtils(com.facebook.presto.sql.util.AstUtils) Function(java.util.function.Function) Pattern(com.facebook.presto.matching.Pattern) OriginalExpressionUtils.castToExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression) Capture(com.facebook.presto.matching.Capture) Literal(com.facebook.presto.sql.tree.Literal) TypeProvider(com.facebook.presto.sql.planner.TypeProvider) TryExpression(com.facebook.presto.sql.tree.TryExpression) Map(java.util.Map) AssignmentUtils.isIdentity(com.facebook.presto.sql.planner.plan.AssignmentUtils.isIdentity) FunctionResolution(com.facebook.presto.sql.relational.FunctionResolution) CallExpression(com.facebook.presto.spi.relation.CallExpression) Collectors.toSet(java.util.stream.Collectors.toSet) VariablesExtractor(com.facebook.presto.sql.planner.VariablesExtractor) RowExpression(com.facebook.presto.spi.relation.RowExpression) ImmutableSet(com.google.common.collect.ImmutableSet) RowExpressionVariableInliner(com.facebook.presto.sql.planner.RowExpressionVariableInliner) Rule(com.facebook.presto.sql.planner.iterative.Rule) Patterns.project(com.facebook.presto.sql.planner.plan.Patterns.project) Set(java.util.Set) Collectors(java.util.stream.Collectors) ExpressionVariableInliner(com.facebook.presto.sql.planner.ExpressionVariableInliner) Sets(com.google.common.collect.Sets) Patterns.source(com.facebook.presto.sql.planner.plan.Patterns.source) List(java.util.List) REMOTE(com.facebook.presto.spi.plan.ProjectNode.Locality.REMOTE) Expression(com.facebook.presto.sql.tree.Expression) DefaultRowExpressionTraversalVisitor(com.facebook.presto.expressions.DefaultRowExpressionTraversalVisitor) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) Capture.newCapture(com.facebook.presto.matching.Capture.newCapture) AssignmentUtils.identityAsSymbolReference(com.facebook.presto.sql.planner.plan.AssignmentUtils.identityAsSymbolReference) ExpressionTreeUtils.createSymbolReference(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) TypeProvider(com.facebook.presto.sql.planner.TypeProvider) Map(java.util.Map)

Example 12 with TypeProvider

use of com.facebook.presto.sql.planner.TypeProvider in project presto by prestodb.

the class TestCostCalculator method calculateCost.

private PlanCostEstimate calculateCost(PlanNode node, CostCalculator costCalculator, StatsCalculator statsCalculator, Map<String, Type> types) {
    TypeProvider typeProvider = TypeProvider.copyOf(types);
    StatsProvider statsProvider = new CachingStatsProvider(statsCalculator, session, typeProvider);
    CostProvider costProvider = new CachingCostProvider(costCalculator, statsProvider, Optional.empty(), session);
    return costProvider.getCost(node);
}
Also used : TypeProvider(com.facebook.presto.sql.planner.TypeProvider)

Example 13 with TypeProvider

use of com.facebook.presto.sql.planner.TypeProvider in project presto by prestodb.

the class TestRowExpressionTranslator method testLessThanOperator.

@Test
public void testLessThanOperator() {
    String untranslated = "col1 < col2";
    TypeProvider typeProvider = TypeProvider.copyOf(ImmutableMap.of("col1", BIGINT, "col2", BIGINT));
    RowExpression specialForm = sqlToRowExpressionTranslator.translate(expression(untranslated), typeProvider);
    TranslatedExpression translatedExpression = translateWith(specialForm, new TestFunctionTranslator(functionAndTypeManager, buildFunctionTranslator(ImmutableSet.of(TestFunctions.class))), emptyMap());
    assertTrue(translatedExpression.getTranslated().isPresent());
    assertEquals(translatedExpression.getTranslated().get(), "col1 LT col2");
}
Also used : TypeProvider(com.facebook.presto.sql.planner.TypeProvider) RowExpression(com.facebook.presto.spi.relation.RowExpression) TranslatedExpression(com.facebook.presto.expressions.translator.TranslatedExpression) Test(org.testng.annotations.Test)

Example 14 with TypeProvider

use of com.facebook.presto.sql.planner.TypeProvider in project presto by prestodb.

the class TestRowExpressionTranslator method testEndToEndFunctionTranslation.

@Test
public void testEndToEndFunctionTranslation() {
    String untranslated = "LN(bitwise_and(1, col1))";
    TypeProvider typeProvider = TypeProvider.copyOf(ImmutableMap.of("col1", BIGINT));
    CallExpression callExpression = (CallExpression) sqlToRowExpressionTranslator.translate(expression(untranslated), typeProvider);
    TranslatedExpression translatedExpression = translateWith(callExpression, new TestFunctionTranslator(functionAndTypeManager, buildFunctionTranslator(ImmutableSet.of(TestFunctions.class))), emptyMap());
    assertTrue(translatedExpression.getTranslated().isPresent());
    assertEquals(translatedExpression.getTranslated().get(), "LNof(1 BITWISE_AND col1)");
}
Also used : TypeProvider(com.facebook.presto.sql.planner.TypeProvider) TranslatedExpression(com.facebook.presto.expressions.translator.TranslatedExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) Test(org.testng.annotations.Test)

Example 15 with TypeProvider

use of com.facebook.presto.sql.planner.TypeProvider in project presto by prestodb.

the class TestRowExpressionTranslator method testIncorrectFunctionSignatureInDefinition.

@Test
public void testIncorrectFunctionSignatureInDefinition() {
    String untranslated = "CEIL(col1)";
    TypeProvider typeProvider = TypeProvider.copyOf(ImmutableMap.of("col1", DOUBLE));
    RowExpression specialForm = sqlToRowExpressionTranslator.translate(expression(untranslated), typeProvider);
    TranslatedExpression translatedExpression = translateWith(specialForm, new TestFunctionTranslator(functionAndTypeManager, buildFunctionTranslator(ImmutableSet.of(TestFunctions.class))), emptyMap());
    assertFalse(translatedExpression.getTranslated().isPresent());
}
Also used : TypeProvider(com.facebook.presto.sql.planner.TypeProvider) RowExpression(com.facebook.presto.spi.relation.RowExpression) TranslatedExpression(com.facebook.presto.expressions.translator.TranslatedExpression) Test(org.testng.annotations.Test)

Aggregations

TypeProvider (com.facebook.presto.sql.planner.TypeProvider)28 RowExpression (com.facebook.presto.spi.relation.RowExpression)17 PlanNode (com.facebook.presto.spi.plan.PlanNode)12 Test (org.testng.annotations.Test)12 ColumnHandle (com.facebook.presto.spi.ColumnHandle)7 Session (com.facebook.presto.Session)6 TranslatedExpression (com.facebook.presto.expressions.translator.TranslatedExpression)6 ConnectorSession (com.facebook.presto.spi.ConnectorSession)6 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)6 List (java.util.List)6 JdbcColumnHandle (com.facebook.presto.plugin.jdbc.JdbcColumnHandle)5 JdbcTableHandle (com.facebook.presto.plugin.jdbc.JdbcTableHandle)5 JdbcTableLayoutHandle (com.facebook.presto.plugin.jdbc.JdbcTableLayoutHandle)5 SchemaTableName (com.facebook.presto.spi.SchemaTableName)5 Expression (com.facebook.presto.sql.tree.Expression)5 TestingConnectorSession (com.facebook.presto.testing.TestingConnectorSession)5 Map (java.util.Map)5 CallExpression (com.facebook.presto.spi.relation.CallExpression)4 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)3 ConstantExpression (com.facebook.presto.spi.relation.ConstantExpression)3