Search in sources :

Example 6 with FunctionCallBuilder

use of io.prestosql.sql.planner.FunctionCallBuilder in project hetu-core by openlookeng.

the class TransformCorrelatedScalarSubquery method apply.

@Override
public Result apply(LateralJoinNode lateralJoinNode, Captures captures, Context context) {
    PlanNode subquery = context.getLookup().resolve(lateralJoinNode.getSubquery());
    if (!searchFrom(subquery, context.getLookup()).where(EnforceSingleRowNode.class::isInstance).recurseOnlyWhen(ProjectNode.class::isInstance).matches()) {
        return Result.empty();
    }
    PlanNode rewrittenSubquery = searchFrom(subquery, context.getLookup()).where(EnforceSingleRowNode.class::isInstance).recurseOnlyWhen(ProjectNode.class::isInstance).removeFirst();
    Range<Long> subqueryCardinality = extractCardinality(rewrittenSubquery, context.getLookup());
    boolean producesAtMostOneRow = Range.closed(0L, 1L).encloses(subqueryCardinality);
    if (producesAtMostOneRow) {
        boolean producesSingleRow = Range.singleton(1L).encloses(subqueryCardinality);
        return Result.ofPlanNode(new LateralJoinNode(context.getIdAllocator().getNextId(), lateralJoinNode.getInput(), rewrittenSubquery, lateralJoinNode.getCorrelation(), producesSingleRow ? lateralJoinNode.getType() : LEFT, lateralJoinNode.getFilter(), lateralJoinNode.getOriginSubquery()));
    }
    Symbol unique = context.getSymbolAllocator().newSymbol("unique", BigintType.BIGINT);
    LateralJoinNode rewrittenLateralJoinNode = new LateralJoinNode(context.getIdAllocator().getNextId(), new AssignUniqueId(context.getIdAllocator().getNextId(), lateralJoinNode.getInput(), unique), rewrittenSubquery, lateralJoinNode.getCorrelation(), LEFT, lateralJoinNode.getFilter(), lateralJoinNode.getOriginSubquery());
    Symbol isDistinct = context.getSymbolAllocator().newSymbol("is_distinct", BooleanType.BOOLEAN);
    MarkDistinctNode markDistinctNode = new MarkDistinctNode(context.getIdAllocator().getNextId(), rewrittenLateralJoinNode, isDistinct, rewrittenLateralJoinNode.getInput().getOutputSymbols(), Optional.empty());
    FilterNode filterNode = new FilterNode(context.getIdAllocator().getNextId(), markDistinctNode, castToRowExpression(new SimpleCaseExpression(toSymbolReference(isDistinct), ImmutableList.of(new WhenClause(TRUE_LITERAL, TRUE_LITERAL)), Optional.of(new Cast(new FunctionCallBuilder(metadata).setName(QualifiedName.of("fail")).addArgument(INTEGER, new LongLiteral(Integer.toString(SUBQUERY_MULTIPLE_ROWS.toErrorCode().getCode()))).addArgument(VARCHAR, new StringLiteral("Scalar sub-query has returned multiple rows")).build(), BOOLEAN)))));
    return Result.ofPlanNode(new ProjectNode(context.getIdAllocator().getNextId(), filterNode, AssignmentUtils.identityAsSymbolReferences((lateralJoinNode.getOutputSymbols()))));
}
Also used : Cast(io.prestosql.sql.tree.Cast) MarkDistinctNode(io.prestosql.spi.plan.MarkDistinctNode) LongLiteral(io.prestosql.sql.tree.LongLiteral) Symbol(io.prestosql.spi.plan.Symbol) FilterNode(io.prestosql.spi.plan.FilterNode) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) WhenClause(io.prestosql.sql.tree.WhenClause) PlanNode(io.prestosql.spi.plan.PlanNode) AssignUniqueId(io.prestosql.sql.planner.plan.AssignUniqueId) LateralJoinNode(io.prestosql.sql.planner.plan.LateralJoinNode) StringLiteral(io.prestosql.sql.tree.StringLiteral) EnforceSingleRowNode(io.prestosql.sql.planner.plan.EnforceSingleRowNode) ProjectNode(io.prestosql.spi.plan.ProjectNode) FunctionCallBuilder(io.prestosql.sql.planner.FunctionCallBuilder)

Example 7 with FunctionCallBuilder

use of io.prestosql.sql.planner.FunctionCallBuilder in project hetu-core by openlookeng.

the class RewriteSpatialPartitioningAggregation method apply.

@Override
public Result apply(AggregationNode node, Captures captures, Context context) {
    ImmutableMap.Builder<Symbol, Aggregation> aggregations = ImmutableMap.builder();
    Symbol partitionCountSymbol = context.getSymbolAllocator().newSymbol("partition_count", INTEGER);
    ImmutableMap.Builder<Symbol, RowExpression> envelopeAssignments = ImmutableMap.builder();
    for (Map.Entry<Symbol, Aggregation> entry : node.getAggregations().entrySet()) {
        Aggregation aggregation = entry.getValue();
        QualifiedObjectName name = metadata.getFunctionAndTypeManager().getFunctionMetadata(aggregation.getFunctionHandle()).getName();
        Type geometryType = metadata.getType(GEOMETRY_TYPE_SIGNATURE);
        if (name.equals(NAME) && aggregation.getArguments().size() == 1) {
            RowExpression geometry = getOnlyElement(aggregation.getArguments().stream().collect(toImmutableList()));
            Symbol envelopeSymbol = context.getSymbolAllocator().newSymbol("envelope", metadata.getType(GEOMETRY_TYPE_SIGNATURE));
            if (isFunctionNameMatch(geometry, "ST_Envelope")) {
                envelopeAssignments.put(envelopeSymbol, geometry);
            } else {
                envelopeAssignments.put(envelopeSymbol, castToRowExpression(new FunctionCallBuilder(metadata).setName(QualifiedName.of("ST_Envelope")).addArgument(GEOMETRY_TYPE_SIGNATURE, castToExpression(geometry)).build()));
            }
            aggregations.put(entry.getKey(), new Aggregation(new CallExpression(name.getObjectName(), metadata.getFunctionAndTypeManager().lookupFunction(NAME.getObjectName(), fromTypes(geometryType, INTEGER)), context.getSymbolAllocator().getTypes().get(entry.getKey()), ImmutableList.of(castToRowExpression(toSymbolReference(envelopeSymbol)), castToRowExpression(toSymbolReference(partitionCountSymbol))), Optional.empty()), ImmutableList.of(castToRowExpression(toSymbolReference(envelopeSymbol)), castToRowExpression(toSymbolReference(partitionCountSymbol))), false, Optional.empty(), Optional.empty(), aggregation.getMask()));
        } else {
            aggregations.put(entry);
        }
    }
    return Result.ofPlanNode(new AggregationNode(node.getId(), new ProjectNode(context.getIdAllocator().getNextId(), node.getSource(), Assignments.builder().putAll(AssignmentUtils.identityAsSymbolReferences(node.getSource().getOutputSymbols())).put(partitionCountSymbol, castToRowExpression(new LongLiteral(Integer.toString(getHashPartitionCount(context.getSession()))))).putAll(envelopeAssignments.build()).build()), aggregations.build(), node.getGroupingSets(), node.getPreGroupedSymbols(), node.getStep(), node.getHashSymbol(), node.getGroupIdSymbol(), node.getAggregationType(), node.getFinalizeSymbol()));
}
Also used : LongLiteral(io.prestosql.sql.tree.LongLiteral) Symbol(io.prestosql.spi.plan.Symbol) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) RowExpression(io.prestosql.spi.relation.RowExpression) AggregationNode(io.prestosql.spi.plan.AggregationNode) ImmutableMap(com.google.common.collect.ImmutableMap) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) Aggregation(io.prestosql.spi.plan.AggregationNode.Aggregation) Type(io.prestosql.spi.type.Type) ProjectNode(io.prestosql.spi.plan.ProjectNode) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) CallExpression(io.prestosql.spi.relation.CallExpression) FunctionCallBuilder(io.prestosql.sql.planner.FunctionCallBuilder)

Example 8 with FunctionCallBuilder

use of io.prestosql.sql.planner.FunctionCallBuilder in project hetu-core by openlookeng.

the class TestExpressionRewriteRuleSet method testAggregationExpressionNotRewritten.

@Test
public void testAggregationExpressionNotRewritten() {
    FunctionCall nowCall = new FunctionCallBuilder(tester().getMetadata()).setName(QualifiedName.of("now")).build();
    ExpressionRewriteRuleSet functionCallRewriter = new ExpressionRewriteRuleSet((expression, context) -> nowCall);
    tester().assertThat(functionCallRewriter.aggregationExpressionRewrite()).on(p -> p.aggregation(a -> a.globalGrouping().addAggregation(p.symbol("count_1", DateType.DATE), nowCall, ImmutableList.of()).source(p.values()))).doesNotFire();
}
Also used : Symbol(io.prestosql.spi.plan.Symbol) BigintType(io.prestosql.spi.type.BigintType) PlanBuilder.assignment(io.prestosql.sql.planner.iterative.rule.test.PlanBuilder.assignment) BaseRuleTest(io.prestosql.sql.planner.iterative.rule.test.BaseRuleTest) PlanMatchPattern.project(io.prestosql.sql.planner.assertions.PlanMatchPattern.project) ImmutableMap(com.google.common.collect.ImmutableMap) PlanMatchPattern(io.prestosql.sql.planner.assertions.PlanMatchPattern) QualifiedName(io.prestosql.sql.tree.QualifiedName) FunctionCallBuilder(io.prestosql.sql.planner.FunctionCallBuilder) Test(org.testng.annotations.Test) PlanMatchPattern.values(io.prestosql.sql.planner.assertions.PlanMatchPattern.values) VARCHAR(io.prestosql.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) FunctionCall(io.prestosql.sql.tree.FunctionCall) LongLiteral(io.prestosql.sql.tree.LongLiteral) SymbolReference(io.prestosql.sql.tree.SymbolReference) DateType(io.prestosql.spi.type.DateType) PlanBuilder(io.prestosql.sql.planner.iterative.rule.test.PlanBuilder) PlanMatchPattern.filter(io.prestosql.sql.planner.assertions.PlanMatchPattern.filter) PlanMatchPattern.expression(io.prestosql.sql.planner.assertions.PlanMatchPattern.expression) OriginalExpressionUtils(io.prestosql.sql.relational.OriginalExpressionUtils) FunctionCall(io.prestosql.sql.tree.FunctionCall) FunctionCallBuilder(io.prestosql.sql.planner.FunctionCallBuilder) BaseRuleTest(io.prestosql.sql.planner.iterative.rule.test.BaseRuleTest) Test(org.testng.annotations.Test)

Example 9 with FunctionCallBuilder

use of io.prestosql.sql.planner.FunctionCallBuilder in project hetu-core by openlookeng.

the class TestStarTreeAggregationRule method testDoNotFireWhenMatchingCubeNotFound.

@Test
public void testDoNotFireWhenMatchingCubeNotFound() {
    Mockito.when(cubeManager.getCubeProvider(anyString())).then(new Returns(Optional.of(provider)));
    Mockito.when(cubeManager.getMetaStore(anyString())).then(new Returns(Optional.of(cubeMetaStore)));
    TpchTableHandle orders = new TpchTableHandle("orders", 1.0);
    TableHandle tmpOrdersTableHandle = new TableHandle(tester().getCurrentConnectorId(), orders, TpchTransactionHandle.INSTANCE, Optional.of(new TpchTableLayoutHandle(orders, TupleDomain.all())));
    List<CubeMetadata> metadataList = ImmutableList.of(cubeMetadata);
    Mockito.when(cubeMetaStore.getMetadataList(eq("local.sf1.0.orders"))).then(new Returns(metadataList));
    Mockito.when(cubeMetadata.matches(any(CubeStatement.class))).thenReturn(false);
    StarTreeAggregationRule starTreeAggregationRule = new StarTreeAggregationRule(cubeManager, tester().getMetadata());
    tester().assertThat(starTreeAggregationRule).setSystemProperty(ENABLE_STAR_TREE_INDEX, "true").on(p -> p.aggregation(builder -> builder.globalGrouping().addAggregation(p.symbol("count", BIGINT), new FunctionCallBuilder(tester().getMetadata()).setName(QualifiedName.of("count")).build(), ImmutableList.of(BIGINT)).step(SINGLE).source(p.project(Assignments.builder().put(p.symbol("custkey"), OriginalExpressionUtils.castToRowExpression(SymbolUtils.toSymbolReference(p.symbol("custkey")))).build(), p.filter(expression("orderkey=1"), p.tableScan(tmpOrdersTableHandle, ImmutableList.of(p.symbol("orderkey", BIGINT)), ImmutableMap.of(p.symbol("orderkey", BIGINT), new TpchColumnHandle("orderkey", BIGINT)))))))).doesNotFire();
    Mockito.verify(cubeMetaStore, Mockito.atLeastOnce()).getMetadataList(eq("local.sf1.0.orders"));
    Mockito.verify(cubeMetadata, Mockito.atLeastOnce()).matches(any(CubeStatement.class));
}
Also used : CubeStatement(io.hetu.core.spi.cube.CubeStatement) BaseRuleTest(io.prestosql.sql.planner.iterative.rule.test.BaseRuleTest) Arrays(java.util.Arrays) ListMultimap(com.google.common.collect.ListMultimap) TpchColumnHandle(io.prestosql.plugin.tpch.TpchColumnHandle) QualifiedName(io.prestosql.sql.tree.QualifiedName) FunctionCallBuilder(io.prestosql.sql.planner.FunctionCallBuilder) Test(org.testng.annotations.Test) AggregationNode(io.prestosql.spi.plan.AggregationNode) Cast(io.prestosql.sql.tree.Cast) FilterNode(io.prestosql.spi.plan.FilterNode) ENABLE_STAR_TREE_INDEX(io.prestosql.SystemSessionProperties.ENABLE_STAR_TREE_INDEX) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) PlanBuilder.expression(io.prestosql.sql.planner.iterative.rule.test.PlanBuilder.expression) Assert.assertFalse(org.testng.Assert.assertFalse) AggregationNode.singleGroupingSet(io.prestosql.spi.plan.AggregationNode.singleGroupingSet) CubeMetaStore(io.hetu.core.spi.cube.io.CubeMetaStore) SymbolUtils(io.prestosql.sql.planner.SymbolUtils) PlanNodeId(io.prestosql.spi.plan.PlanNodeId) ImmutableMap(com.google.common.collect.ImmutableMap) MetadataManager.createTestMetadataManager(io.prestosql.metadata.MetadataManager.createTestMetadataManager) BeforeClass(org.testng.annotations.BeforeClass) TableScanNode(io.prestosql.spi.plan.TableScanNode) PlanNode(io.prestosql.spi.plan.PlanNode) UUID(java.util.UUID) CubeStatement(io.hetu.core.spi.cube.CubeStatement) ProjectNode(io.prestosql.spi.plan.ProjectNode) Metadata(io.prestosql.metadata.Metadata) FunctionHandle(io.prestosql.spi.function.FunctionHandle) PlanSymbolAllocator(io.prestosql.sql.planner.PlanSymbolAllocator) Matchers.any(org.mockito.Matchers.any) ReuseExchangeOperator(io.prestosql.spi.operator.ReuseExchangeOperator) List(java.util.List) DoubleLiteral(io.prestosql.sql.tree.DoubleLiteral) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Returns(org.mockito.internal.stubbing.answers.Returns) UnionNode(io.prestosql.spi.plan.UnionNode) Optional(java.util.Optional) PlanBuilder(io.prestosql.sql.planner.iterative.rule.test.PlanBuilder) FunctionAndTypeManager(io.prestosql.metadata.FunctionAndTypeManager) OriginalExpressionUtils(io.prestosql.sql.relational.OriginalExpressionUtils) TpchTableLayoutHandle(io.prestosql.plugin.tpch.TpchTableLayoutHandle) TpchTableHandle(io.prestosql.plugin.tpch.TpchTableHandle) TableMetadata(io.prestosql.metadata.TableMetadata) StandardTypes(io.prestosql.spi.type.StandardTypes) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) INTEGER(io.prestosql.spi.type.IntegerType.INTEGER) AbstractMockMetadata.dummyMetadata(io.prestosql.metadata.AbstractMockMetadata.dummyMetadata) TableHandle(io.prestosql.spi.metadata.TableHandle) Matchers.anyString(org.mockito.Matchers.anyString) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) ArrayList(java.util.ArrayList) BaseMatcher(org.hamcrest.BaseMatcher) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) ImmutableList(com.google.common.collect.ImmutableList) SINGLE(io.prestosql.spi.plan.AggregationNode.Step.SINGLE) DateTimeUtils(io.prestosql.spi.util.DateTimeUtils) Session(io.prestosql.Session) DOUBLE(io.prestosql.spi.type.DoubleType.DOUBLE) TpchTransactionHandle(io.prestosql.plugin.tpch.TpchTransactionHandle) DATE(io.prestosql.spi.type.DateType.DATE) CubeProvider(io.prestosql.spi.cube.CubeProvider) Symbol(io.prestosql.spi.plan.Symbol) Description(org.hamcrest.Description) TypeSignatureProvider.fromTypes(io.prestosql.sql.analyzer.TypeSignatureProvider.fromTypes) Assignments(io.prestosql.spi.plan.Assignments) ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) TupleDomain(io.prestosql.spi.predicate.TupleDomain) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) ConnectorTableMetadata(io.prestosql.spi.connector.ConnectorTableMetadata) Expressions(io.prestosql.sql.relational.Expressions) CubeManager(io.prestosql.cube.CubeManager) Mockito(org.mockito.Mockito) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) Matcher(org.hamcrest.Matcher) PlanNodeIdAllocator(io.prestosql.spi.plan.PlanNodeIdAllocator) InputReferenceExpression(io.prestosql.spi.relation.InputReferenceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) FeaturesConfig(io.prestosql.sql.analyzer.FeaturesConfig) CubeMetadata(io.hetu.core.spi.cube.CubeMetadata) Assert.assertTrue(org.testng.Assert.assertTrue) Returns(org.mockito.internal.stubbing.answers.Returns) TpchColumnHandle(io.prestosql.plugin.tpch.TpchColumnHandle) TpchTableHandle(io.prestosql.plugin.tpch.TpchTableHandle) TableHandle(io.prestosql.spi.metadata.TableHandle) TpchTableLayoutHandle(io.prestosql.plugin.tpch.TpchTableLayoutHandle) CubeMetadata(io.hetu.core.spi.cube.CubeMetadata) TpchTableHandle(io.prestosql.plugin.tpch.TpchTableHandle) FunctionCallBuilder(io.prestosql.sql.planner.FunctionCallBuilder) BaseRuleTest(io.prestosql.sql.planner.iterative.rule.test.BaseRuleTest) Test(org.testng.annotations.Test)

Example 10 with FunctionCallBuilder

use of io.prestosql.sql.planner.FunctionCallBuilder in project hetu-core by openlookeng.

the class HashGenerationOptimizer method getHashExpression.

public static Optional<Expression> getHashExpression(Metadata metadata, PlanSymbolAllocator planSymbolAllocator, List<Symbol> symbols) {
    if (symbols.isEmpty()) {
        return Optional.empty();
    }
    Expression result = new GenericLiteral(StandardTypes.BIGINT, String.valueOf(INITIAL_HASH_VALUE));
    for (Symbol symbol : symbols) {
        Expression hashField = new FunctionCallBuilder(metadata).setName(QualifiedName.of(HASH_CODE)).addArgument(planSymbolAllocator.getTypes().get(symbol), new SymbolReference(symbol.getName())).build();
        hashField = new CoalesceExpression(hashField, new LongLiteral(String.valueOf(NULL_HASH_CODE)));
        result = new FunctionCallBuilder(metadata).setName(QualifiedName.of("combine_hash")).addArgument(BIGINT, result).addArgument(BIGINT, hashField).build();
    }
    return Optional.of(result);
}
Also used : CallExpression(io.prestosql.spi.relation.CallExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) OriginalExpressionUtils.isExpression(io.prestosql.sql.relational.OriginalExpressionUtils.isExpression) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Expression(io.prestosql.sql.tree.Expression) LongLiteral(io.prestosql.sql.tree.LongLiteral) Symbol(io.prestosql.spi.plan.Symbol) SymbolReference(io.prestosql.sql.tree.SymbolReference) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) GenericLiteral(io.prestosql.sql.tree.GenericLiteral) FunctionCallBuilder(io.prestosql.sql.planner.FunctionCallBuilder)

Aggregations

FunctionCallBuilder (io.prestosql.sql.planner.FunctionCallBuilder)12 Symbol (io.prestosql.spi.plan.Symbol)10 Test (org.testng.annotations.Test)9 ImmutableMap (com.google.common.collect.ImmutableMap)7 ImmutableList (com.google.common.collect.ImmutableList)6 BaseRuleTest (io.prestosql.sql.planner.iterative.rule.test.BaseRuleTest)6 QualifiedName (io.prestosql.sql.tree.QualifiedName)6 RowExpression (io.prestosql.spi.relation.RowExpression)5 PlanBuilder (io.prestosql.sql.planner.iterative.rule.test.PlanBuilder)5 OriginalExpressionUtils (io.prestosql.sql.relational.OriginalExpressionUtils)5 LongLiteral (io.prestosql.sql.tree.LongLiteral)5 SymbolReference (io.prestosql.sql.tree.SymbolReference)5 TpchColumnHandle (io.prestosql.plugin.tpch.TpchColumnHandle)4 TpchTableHandle (io.prestosql.plugin.tpch.TpchTableHandle)4 TpchTransactionHandle (io.prestosql.plugin.tpch.TpchTransactionHandle)4 QualifiedObjectName (io.prestosql.spi.connector.QualifiedObjectName)4 TableHandle (io.prestosql.spi.metadata.TableHandle)4 AggregationNode (io.prestosql.spi.plan.AggregationNode)4 ProjectNode (io.prestosql.spi.plan.ProjectNode)4 Cast (io.prestosql.sql.tree.Cast)4