Search in sources :

Example 6 with PlanVariableAllocator

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

the class RuleAssert method applyRule.

private RuleApplication applyRule() {
    PlanVariableAllocator variableAllocator = new PlanVariableAllocator(types.allVariables());
    Memo memo = new Memo(idAllocator, plan);
    Lookup lookup = Lookup.from(planNode -> Stream.of(memo.resolve(planNode)));
    PlanNode memoRoot = memo.getNode(memo.getRootGroup());
    return inTransaction(session -> applyRule(rule, memoRoot, ruleContext(statsCalculator, costCalculator, variableAllocator, memo, lookup, session)));
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) Lookup(com.facebook.presto.sql.planner.iterative.Lookup) PlanVariableAllocator(com.facebook.presto.sql.planner.PlanVariableAllocator) Memo(com.facebook.presto.sql.planner.iterative.Memo)

Example 7 with PlanVariableAllocator

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

the class TestInlineSqlFunctions method assertInlined.

private void assertInlined(RuleTester tester, String inputSql, String expected, Map<String, Type> variableTypes) {
    Session session = TestingSession.testSessionBuilder().setSystemProperty("inline_sql_functions", "true").build();
    Metadata metadata = tester.getMetadata();
    Expression inputSqlExpression = PlanBuilder.expression(inputSql);
    Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypes(session, metadata, tester.getSqlParser(), viewOf(variableTypes), inputSqlExpression, ImmutableList.of(), WarningCollector.NOOP);
    Expression inlinedExpression = InlineSqlFunctions.InlineSqlFunctionsRewriter.rewrite(inputSqlExpression, session, metadata, new PlanVariableAllocator(variableTypes.entrySet().stream().map(entry -> new VariableReferenceExpression(Optional.empty(), entry.getKey(), entry.getValue())).collect(toImmutableList())), expressionTypes);
    inlinedExpression = ExpressionUtils.rewriteIdentifiersToSymbolReferences(inlinedExpression);
    Expression expectedExpression = PlanBuilder.expression(expected);
    assertEquals(inlinedExpression, expectedExpression);
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) WarningCollector(com.facebook.presto.spi.WarningCollector) Parameter(com.facebook.presto.spi.function.Parameter) SqlInvokedFunctionNamespaceManagerConfig(com.facebook.presto.functionNamespace.SqlInvokedFunctionNamespaceManagerConfig) SQL(com.facebook.presto.spi.function.RoutineCharacteristics.Language.SQL) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Test(org.testng.annotations.Test) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) Map(java.util.Map) PlanBuilder.assignment(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder.assignment) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) PlanMatchPattern.expression(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.expression) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) PlanMatchPattern.project(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.project) ImmutableMap(com.google.common.collect.ImmutableMap) FunctionImplementationType(com.facebook.presto.spi.function.FunctionImplementationType) BigintType(com.facebook.presto.common.type.BigintType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) DETERMINISTIC(com.facebook.presto.spi.function.RoutineCharacteristics.Determinism.DETERMINISTIC) InMemoryFunctionNamespaceManager(com.facebook.presto.functionNamespace.testing.InMemoryFunctionNamespaceManager) RETURNS_NULL_ON_NULL_INPUT(com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause.RETURNS_NULL_ON_NULL_INPUT) PlanVariableAllocator(com.facebook.presto.sql.planner.PlanVariableAllocator) ExpressionAnalyzer.getExpressionTypes(com.facebook.presto.sql.analyzer.ExpressionAnalyzer.getExpressionTypes) Optional(java.util.Optional) TypeProvider.viewOf(com.facebook.presto.sql.planner.TypeProvider.viewOf) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) Assert.assertEquals(org.testng.Assert.assertEquals) ExpressionUtils(com.facebook.presto.sql.ExpressionUtils) TestingSession(com.facebook.presto.testing.TestingSession) BeforeTest(org.testng.annotations.BeforeTest) IntegerType(com.facebook.presto.common.type.IntegerType) ImmutableList(com.google.common.collect.ImmutableList) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) ArrayType(com.facebook.presto.common.type.ArrayType) THRIFT(com.facebook.presto.spi.function.FunctionImplementationType.THRIFT) Type(com.facebook.presto.common.type.Type) RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) Session(com.facebook.presto.Session) INTEGER(com.facebook.presto.common.type.StandardTypes.INTEGER) NodeRef(com.facebook.presto.sql.tree.NodeRef) SqlFunctionExecutors(com.facebook.presto.functionNamespace.execution.SqlFunctionExecutors) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Expression(com.facebook.presto.sql.tree.Expression) FunctionVersion.notVersioned(com.facebook.presto.spi.function.FunctionVersion.notVersioned) CatalogSchemaName(com.facebook.presto.common.CatalogSchemaName) PlanMatchPattern.values(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.values) NoopSqlFunctionExecutor(com.facebook.presto.functionNamespace.execution.NoopSqlFunctionExecutor) RuleTester(com.facebook.presto.sql.planner.iterative.rule.test.RuleTester) Metadata(com.facebook.presto.metadata.Metadata) NodeRef(com.facebook.presto.sql.tree.NodeRef) FunctionImplementationType(com.facebook.presto.spi.function.FunctionImplementationType) BigintType(com.facebook.presto.common.type.BigintType) IntegerType(com.facebook.presto.common.type.IntegerType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Expression(com.facebook.presto.sql.tree.Expression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Metadata(com.facebook.presto.metadata.Metadata) PlanVariableAllocator(com.facebook.presto.sql.planner.PlanVariableAllocator) TestingSession(com.facebook.presto.testing.TestingSession) Session(com.facebook.presto.Session)

Example 8 with PlanVariableAllocator

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

the class TestPlanRemoteProjections method testRemoteOnly.

@Test
void testRemoteOnly() {
    PlanBuilder planBuilder = new PlanBuilder(TEST_SESSION, new PlanNodeIdAllocator(), getMetadata());
    PlanRemotePojections rule = new PlanRemotePojections(getFunctionAndTypeManager());
    List<ProjectionContext> rewritten = rule.planRemoteAssignments(Assignments.builder().put(planBuilder.variable("a"), planBuilder.rowExpression("unittest.memory.remote_foo()")).put(planBuilder.variable("b"), planBuilder.rowExpression("unittest.memory.remote_foo(unittest.memory.remote_foo())")).build(), new PlanVariableAllocator(planBuilder.getTypes().allVariables()));
    assertEquals(rewritten.size(), 2);
    assertEquals(rewritten.get(1).getProjections().size(), 2);
}
Also used : ProjectionContext(com.facebook.presto.sql.planner.iterative.rule.PlanRemotePojections.ProjectionContext) PlanNodeIdAllocator(com.facebook.presto.spi.plan.PlanNodeIdAllocator) PlanVariableAllocator(com.facebook.presto.sql.planner.PlanVariableAllocator) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) Test(org.testng.annotations.Test)

Example 9 with PlanVariableAllocator

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

the class TestPlanRemoteProjections method testSpecialForm.

@Test
void testSpecialForm() {
    PlanBuilder planBuilder = new PlanBuilder(TEST_SESSION, new PlanNodeIdAllocator(), getMetadata());
    planBuilder.variable("x", INTEGER);
    planBuilder.variable("y", INTEGER);
    PlanRemotePojections rule = new PlanRemotePojections(getFunctionAndTypeManager());
    List<ProjectionContext> rewritten = rule.planRemoteAssignments(Assignments.builder().put(planBuilder.variable("a"), planBuilder.rowExpression("unittest.memory.remote_foo(x, y + unittest.memory.remote_foo(x))")).put(planBuilder.variable("b"), planBuilder.rowExpression("x IS NULL OR y IS NULL")).put(planBuilder.variable("c"), planBuilder.rowExpression("IF(abs(unittest.memory.remote_foo()) > 0, x, y)")).put(planBuilder.variable("d"), planBuilder.rowExpression("unittest.memory.remote_foo(x + y, abs(x))")).put(planBuilder.variable("e"), planBuilder.rowExpression("TRUE OR FALSE")).build(), new PlanVariableAllocator(planBuilder.getTypes().allVariables()));
    assertEquals(rewritten.size(), 4);
    assertEquals(rewritten.get(3).getProjections().size(), 5);
}
Also used : ProjectionContext(com.facebook.presto.sql.planner.iterative.rule.PlanRemotePojections.ProjectionContext) PlanNodeIdAllocator(com.facebook.presto.spi.plan.PlanNodeIdAllocator) PlanVariableAllocator(com.facebook.presto.sql.planner.PlanVariableAllocator) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) Test(org.testng.annotations.Test)

Example 10 with PlanVariableAllocator

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

the class TestLambdaCaptureDesugaringRewriter method testRewriteBasicLambda.

@Test
public void testRewriteBasicLambda() {
    final List<VariableReferenceExpression> variables = ImmutableList.of(new VariableReferenceExpression(Optional.empty(), "a", BIGINT), new VariableReferenceExpression(Optional.empty(), "x", BIGINT));
    final PlanVariableAllocator allocator = new PlanVariableAllocator(variables);
    assertEquals(rewrite(expression("x -> a + x"), allocator), new BindExpression(ImmutableList.of(expression("a")), new LambdaExpression(Stream.of("a_0", "x").map(Identifier::new).map(LambdaArgumentDeclaration::new).collect(toList()), expression("a_0 + x"))));
}
Also used : LambdaArgumentDeclaration(com.facebook.presto.sql.tree.LambdaArgumentDeclaration) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) BindExpression(com.facebook.presto.sql.tree.BindExpression) PlanVariableAllocator(com.facebook.presto.sql.planner.PlanVariableAllocator) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) Test(org.testng.annotations.Test)

Aggregations

PlanVariableAllocator (com.facebook.presto.sql.planner.PlanVariableAllocator)22 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)9 Test (org.testng.annotations.Test)8 PlanNodeIdAllocator (com.facebook.presto.spi.plan.PlanNodeIdAllocator)7 RowExpression (com.facebook.presto.spi.relation.RowExpression)6 PlanBuilder (com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder)6 PlanNode (com.facebook.presto.spi.plan.PlanNode)5 Map (java.util.Map)5 Type (com.facebook.presto.common.type.Type)4 SqlParser (com.facebook.presto.sql.parser.SqlParser)4 ProjectionContext (com.facebook.presto.sql.planner.iterative.rule.PlanRemotePojections.ProjectionContext)4 Expression (com.facebook.presto.sql.tree.Expression)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 List (java.util.List)4 Session (com.facebook.presto.Session)3 Metadata (com.facebook.presto.metadata.Metadata)3 LambdaExpression (com.facebook.presto.sql.tree.LambdaExpression)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 Optional (java.util.Optional)3 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)2