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);
}
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);
}
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");
}
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)");
}
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());
}
Aggregations