Search in sources :

Example 1 with SymbolReference

use of com.facebook.presto.sql.tree.SymbolReference in project presto by prestodb.

the class SymbolAliases method get.

public SymbolReference get(String alias) {
    alias = toKey(alias);
    SymbolReference result = map.get(alias);
    /*
         * It's still kind of an open question if the right combination of anyTree() and
         * a sufficiently complex and/or ambiguous plan might make throwing here a
         * theoretically incorrect thing to do.
         *
         * If you run into a case that you think justifies changing this, please consider
         * that it's already pretty hard to determine if a failure is because the test
         * is written incorrectly or because the actual plan really doesn't match a
         * correctly written test. Having this throw makes it a lot easier to track down
         * missing aliases in incorrect plans.
         */
    checkState(result != null, format("missing expression for alias %s", alias));
    return result;
}
Also used : SymbolReference(com.facebook.presto.sql.tree.SymbolReference)

Example 2 with SymbolReference

use of com.facebook.presto.sql.tree.SymbolReference in project presto by prestodb.

the class TestExpressionVerifier method test.

@Test
public void test() {
    Expression actual = expression("NOT(orderkey = 3 AND custkey = 3 AND orderkey < 10)");
    SymbolAliases symbolAliases = SymbolAliases.builder().put("X", new SymbolReference("orderkey")).put("Y", new SymbolReference("custkey")).build();
    ExpressionVerifier verifier = new ExpressionVerifier(symbolAliases);
    assertTrue(verifier.process(actual, expression("NOT(X = 3 AND Y = 3 AND X < 10)")));
    assertThrows(() -> verifier.process(actual, expression("NOT(X = 3 AND Y = 3 AND Z < 10)")));
    assertFalse(verifier.process(actual, expression("NOT(X = 3 AND X = 3 AND X < 10)")));
}
Also used : Expression(com.facebook.presto.sql.tree.Expression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) Test(org.testng.annotations.Test)

Example 3 with SymbolReference

use of com.facebook.presto.sql.tree.SymbolReference in project presto by prestodb.

the class TestCountConstantOptimizer method testCountConstantOptimizer.

@Test
public void testCountConstantOptimizer() throws Exception {
    CountConstantOptimizer optimizer = new CountConstantOptimizer();
    PlanNodeIdAllocator planNodeIdAllocator = new PlanNodeIdAllocator();
    Symbol countAggregationSymbol = new Symbol("count");
    Signature countAggregationSignature = new Signature("count", FunctionKind.AGGREGATE, parseTypeSignature(StandardTypes.BIGINT), parseTypeSignature(StandardTypes.BIGINT));
    ImmutableMap<Symbol, FunctionCall> aggregations = ImmutableMap.of(countAggregationSymbol, new FunctionCall(QualifiedName.of("count"), ImmutableList.of(new SymbolReference("expr"))));
    ImmutableMap<Symbol, Signature> functions = ImmutableMap.of(countAggregationSymbol, countAggregationSignature);
    ValuesNode valuesNode = new ValuesNode(planNodeIdAllocator.getNextId(), ImmutableList.of(new Symbol("col")), ImmutableList.of(ImmutableList.of()));
    AggregationNode eligiblePlan = new AggregationNode(planNodeIdAllocator.getNextId(), new ProjectNode(planNodeIdAllocator.getNextId(), valuesNode, Assignments.of(new Symbol("expr"), new LongLiteral("42"))), aggregations, functions, ImmutableMap.of(), ImmutableList.of(ImmutableList.of()), AggregationNode.Step.INTERMEDIATE, Optional.empty(), Optional.empty());
    assertTrue(((AggregationNode) optimizer.optimize(eligiblePlan, TEST_SESSION, ImmutableMap.of(), new SymbolAllocator(), new PlanNodeIdAllocator())).getAggregations().get(countAggregationSymbol).getArguments().isEmpty());
    AggregationNode ineligiblePlan = new AggregationNode(planNodeIdAllocator.getNextId(), new ProjectNode(planNodeIdAllocator.getNextId(), valuesNode, Assignments.of(new Symbol("expr"), new FunctionCall(QualifiedName.of("function"), ImmutableList.of(new Identifier("x"))))), aggregations, functions, ImmutableMap.of(), ImmutableList.of(ImmutableList.of()), AggregationNode.Step.INTERMEDIATE, Optional.empty(), Optional.empty());
    assertFalse(((AggregationNode) optimizer.optimize(ineligiblePlan, TEST_SESSION, ImmutableMap.of(), new SymbolAllocator(), new PlanNodeIdAllocator())).getAggregations().get(countAggregationSymbol).getArguments().isEmpty());
}
Also used : SymbolAllocator(com.facebook.presto.sql.planner.SymbolAllocator) ValuesNode(com.facebook.presto.sql.planner.plan.ValuesNode) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) Symbol(com.facebook.presto.sql.planner.Symbol) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) Identifier(com.facebook.presto.sql.tree.Identifier) PlanNodeIdAllocator(com.facebook.presto.sql.planner.PlanNodeIdAllocator) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 4 with SymbolReference

use of com.facebook.presto.sql.tree.SymbolReference in project presto by prestodb.

the class DomainTranslator method extractDisjuncts.

private static List<Expression> extractDisjuncts(Type type, DiscreteValues discreteValues, SymbolReference reference) {
    List<Expression> values = discreteValues.getValues().stream().map(object -> toExpression(object, type)).collect(toList());
    // If values is empty, then the equatableValues was either ALL or NONE, both of which should already have been checked for
    checkState(!values.isEmpty());
    Expression predicate;
    if (values.size() == 1) {
        predicate = new ComparisonExpression(EQUAL, reference, getOnlyElement(values));
    } else {
        predicate = new InPredicate(reference, new InListExpression(values));
    }
    if (!discreteValues.isWhiteList()) {
        predicate = new NotExpression(predicate);
    }
    return ImmutableList.of(predicate);
}
Also used : ComparisonExpressionType(com.facebook.presto.sql.tree.ComparisonExpressionType) Block(com.facebook.presto.spi.block.Block) GREATER_THAN(com.facebook.presto.sql.tree.ComparisonExpressionType.GREATER_THAN) GREATER_THAN_OR_EQUAL(com.facebook.presto.sql.tree.ComparisonExpressionType.GREATER_THAN_OR_EQUAL) IsNullPredicate(com.facebook.presto.sql.tree.IsNullPredicate) PeekingIterator(com.google.common.collect.PeekingIterator) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Map(java.util.Map) NullableValue(com.facebook.presto.spi.predicate.NullableValue) BetweenPredicate(com.facebook.presto.sql.tree.BetweenPredicate) NotExpression(com.facebook.presto.sql.tree.NotExpression) EQUAL(com.facebook.presto.sql.tree.ComparisonExpressionType.EQUAL) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) ImmutableMap(com.google.common.collect.ImmutableMap) Collections.emptyList(java.util.Collections.emptyList) SortedRangeSet(com.facebook.presto.spi.predicate.SortedRangeSet) Marker(com.facebook.presto.spi.predicate.Marker) SqlParser(com.facebook.presto.sql.parser.SqlParser) Preconditions.checkState(com.google.common.base.Preconditions.checkState) TRUE_LITERAL(com.facebook.presto.sql.tree.BooleanLiteral.TRUE_LITERAL) LESS_THAN(com.facebook.presto.sql.tree.ComparisonExpressionType.LESS_THAN) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) BooleanLiteral(com.facebook.presto.sql.tree.BooleanLiteral) NullLiteral(com.facebook.presto.sql.tree.NullLiteral) ExpressionUtils.combineDisjunctsWithDefault(com.facebook.presto.sql.ExpressionUtils.combineDisjunctsWithDefault) Domain(com.facebook.presto.spi.predicate.Domain) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) List(java.util.List) Utils(com.facebook.presto.spi.predicate.Utils) InPredicate(com.facebook.presto.sql.tree.InPredicate) Optional(java.util.Optional) Ranges(com.facebook.presto.spi.predicate.Ranges) ExpressionUtils.combineConjuncts(com.facebook.presto.sql.ExpressionUtils.combineConjuncts) ExpressionUtils.and(com.facebook.presto.sql.ExpressionUtils.and) ArrayList(java.util.ArrayList) InListExpression(com.facebook.presto.sql.tree.InListExpression) ImmutableList(com.google.common.collect.ImmutableList) ValueSet(com.facebook.presto.spi.predicate.ValueSet) Type(com.facebook.presto.spi.type.Type) Objects.requireNonNull(java.util.Objects.requireNonNull) Cast(com.facebook.presto.sql.tree.Cast) Iterators.peekingIterator(com.google.common.collect.Iterators.peekingIterator) Nullable(javax.annotation.Nullable) ExpressionUtils.or(com.facebook.presto.sql.ExpressionUtils.or) FALSE_LITERAL(com.facebook.presto.sql.tree.BooleanLiteral.FALSE_LITERAL) Session(com.facebook.presto.Session) Signature(com.facebook.presto.metadata.Signature) AstVisitor(com.facebook.presto.sql.tree.AstVisitor) Signature.internalOperator(com.facebook.presto.metadata.Signature.internalOperator) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) IsNotNullPredicate(com.facebook.presto.sql.tree.IsNotNullPredicate) SATURATED_FLOOR_CAST(com.facebook.presto.spi.function.OperatorType.SATURATED_FLOOR_CAST) DiscreteValues(com.facebook.presto.spi.predicate.DiscreteValues) Range(com.facebook.presto.spi.predicate.Range) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LiteralInterpreter.toExpression(com.facebook.presto.sql.planner.LiteralInterpreter.toExpression) LESS_THAN_OR_EQUAL(com.facebook.presto.sql.tree.ComparisonExpressionType.LESS_THAN_OR_EQUAL) NOT_EQUAL(com.facebook.presto.sql.tree.ComparisonExpressionType.NOT_EQUAL) Collectors.toList(java.util.stream.Collectors.toList) ExpressionAnalyzer(com.facebook.presto.sql.analyzer.ExpressionAnalyzer) Expression(com.facebook.presto.sql.tree.Expression) FunctionInvoker(com.facebook.presto.sql.FunctionInvoker) IdentityLinkedHashMap(com.facebook.presto.util.maps.IdentityLinkedHashMap) Metadata(com.facebook.presto.metadata.Metadata) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LiteralInterpreter.toExpression(com.facebook.presto.sql.planner.LiteralInterpreter.toExpression) Expression(com.facebook.presto.sql.tree.Expression) InListExpression(com.facebook.presto.sql.tree.InListExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) InPredicate(com.facebook.presto.sql.tree.InPredicate)

Example 5 with SymbolReference

use of com.facebook.presto.sql.tree.SymbolReference in project presto by prestodb.

the class DomainTranslator method toPredicate.

public static Expression toPredicate(TupleDomain<Symbol> tupleDomain) {
    if (tupleDomain.isNone()) {
        return FALSE_LITERAL;
    }
    ImmutableList.Builder<Expression> conjunctBuilder = ImmutableList.builder();
    for (Map.Entry<Symbol, Domain> entry : tupleDomain.getDomains().get().entrySet()) {
        Symbol symbol = entry.getKey();
        SymbolReference reference = symbol.toSymbolReference();
        conjunctBuilder.add(toPredicate(entry.getValue(), reference));
    }
    return combineConjuncts(conjunctBuilder.build());
}
Also used : NotExpression(com.facebook.presto.sql.tree.NotExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LiteralInterpreter.toExpression(com.facebook.presto.sql.planner.LiteralInterpreter.toExpression) Expression(com.facebook.presto.sql.tree.Expression) ImmutableList(com.google.common.collect.ImmutableList) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) Domain(com.facebook.presto.spi.predicate.Domain) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) IdentityLinkedHashMap(com.facebook.presto.util.maps.IdentityLinkedHashMap)

Aggregations

SymbolReference (com.facebook.presto.sql.tree.SymbolReference)13 Expression (com.facebook.presto.sql.tree.Expression)8 ImmutableList (com.google.common.collect.ImmutableList)5 Signature (com.facebook.presto.metadata.Signature)4 Cast (com.facebook.presto.sql.tree.Cast)4 IdentityLinkedHashMap (com.facebook.presto.util.maps.IdentityLinkedHashMap)4 Map (java.util.Map)4 Domain (com.facebook.presto.spi.predicate.Domain)3 TupleDomain (com.facebook.presto.spi.predicate.TupleDomain)3 Type (com.facebook.presto.spi.type.Type)3 LiteralInterpreter.toExpression (com.facebook.presto.sql.planner.LiteralInterpreter.toExpression)3 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)3 Session (com.facebook.presto.Session)2 Metadata (com.facebook.presto.metadata.Metadata)2 Signature.internalOperator (com.facebook.presto.metadata.Signature.internalOperator)2 Block (com.facebook.presto.spi.block.Block)2 SATURATED_FLOOR_CAST (com.facebook.presto.spi.function.OperatorType.SATURATED_FLOOR_CAST)2 DiscreteValues (com.facebook.presto.spi.predicate.DiscreteValues)2 Marker (com.facebook.presto.spi.predicate.Marker)2 NullableValue (com.facebook.presto.spi.predicate.NullableValue)2