Search in sources :

Example 16 with Expression

use of io.trino.sql.tree.Expression in project trino by trinodb.

the class EqualityInference method getScopedCanonical.

/**
 * Returns a canonical expression that is fully contained by the symbolScope and that is equivalent
 * to the specified expression. Returns null if unable to find a canonical.
 */
@VisibleForTesting
Expression getScopedCanonical(Expression expression, Predicate<Symbol> symbolScope) {
    Expression canonicalIndex = canonicalMap.get(expression);
    if (canonicalIndex == null) {
        return null;
    }
    Collection<Expression> equivalences = equalitySets.get(canonicalIndex);
    if (expression instanceof SymbolReference) {
        boolean inScope = equivalences.stream().filter(SymbolReference.class::isInstance).map(Symbol::from).anyMatch(symbolScope);
        if (!inScope) {
            return null;
        }
    }
    return getCanonical(equivalences.stream().filter(e -> isScoped(e, symbolScope)));
}
Also used : Arrays(java.util.Arrays) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) DeterminismEvaluator.isDeterministic(io.trino.sql.planner.DeterminismEvaluator.isDeterministic) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) DisjointSet(io.trino.util.DisjointSet) LinkedHashSet(java.util.LinkedHashSet) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) NullabilityAnalyzer.mayReturnNullOnNonNullInput(io.trino.sql.planner.NullabilityAnalyzer.mayReturnNullOnNonNullInput) ToIntFunction(java.util.function.ToIntFunction) Set(java.util.Set) Collectors(java.util.stream.Collectors) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) SymbolReference(io.trino.sql.tree.SymbolReference) Metadata(io.trino.metadata.Metadata) ExpressionNodeInliner.replaceExpression(io.trino.sql.planner.ExpressionNodeInliner.replaceExpression) Expression(io.trino.sql.tree.Expression) ExpressionUtils.extractConjuncts(io.trino.sql.ExpressionUtils.extractConjuncts) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) ExpressionNodeInliner.replaceExpression(io.trino.sql.planner.ExpressionNodeInliner.replaceExpression) Expression(io.trino.sql.tree.Expression) SymbolReference(io.trino.sql.tree.SymbolReference) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 17 with Expression

use of io.trino.sql.tree.Expression in project trino by trinodb.

the class ExpressionInterpreter method evaluateConstantExpression.

public static Object evaluateConstantExpression(Expression expression, Type expectedType, PlannerContext plannerContext, Session session, AccessControl accessControl, Map<NodeRef<Parameter>, Expression> parameters) {
    ExpressionAnalyzer analyzer = createConstantAnalyzer(plannerContext, accessControl, session, parameters, WarningCollector.NOOP);
    analyzer.analyze(expression, Scope.create());
    Type actualType = analyzer.getExpressionTypes().get(NodeRef.of(expression));
    if (!new TypeCoercion(plannerContext.getTypeManager()::getType).canCoerce(actualType, expectedType)) {
        throw semanticException(TYPE_MISMATCH, expression, "Cannot cast type %s to %s", actualType.getDisplayName(), expectedType.getDisplayName());
    }
    Map<NodeRef<Expression>, Type> coercions = ImmutableMap.<NodeRef<Expression>, Type>builder().putAll(analyzer.getExpressionCoercions()).put(NodeRef.of(expression), expectedType).buildOrThrow();
    return evaluateConstantExpression(expression, coercions, analyzer.getTypeOnlyCoercions(), plannerContext, session, accessControl, ImmutableSet.of(), parameters);
}
Also used : NodeRef(io.trino.sql.tree.NodeRef) FunctionType(io.trino.type.FunctionType) OperatorType(io.trino.spi.function.OperatorType) CharType(io.trino.spi.type.CharType) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) RowType(io.trino.spi.type.RowType) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) ArrayType(io.trino.spi.type.ArrayType) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) NullIfExpression(io.trino.sql.tree.NullIfExpression) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) InListExpression(io.trino.sql.tree.InListExpression) NotExpression(io.trino.sql.tree.NotExpression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) BindExpression(io.trino.sql.tree.BindExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) IfExpression(io.trino.sql.tree.IfExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) Expression(io.trino.sql.tree.Expression) SimpleCaseExpression(io.trino.sql.tree.SimpleCaseExpression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) LambdaExpression(io.trino.sql.tree.LambdaExpression) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) CanonicalizeExpressionRewriter.canonicalizeExpression(io.trino.sql.planner.iterative.rule.CanonicalizeExpressionRewriter.canonicalizeExpression) ExpressionAnalyzer(io.trino.sql.analyzer.ExpressionAnalyzer) TypeCoercion(io.trino.type.TypeCoercion)

Example 18 with Expression

use of io.trino.sql.tree.Expression in project trino by trinodb.

the class ExpressionInterpreter method evaluateConstantExpression.

public static Object evaluateConstantExpression(Expression expression, Map<NodeRef<Expression>, Type> coercions, Set<NodeRef<Expression>> typeOnlyCoercions, PlannerContext plannerContext, Session session, AccessControl accessControl, Set<NodeRef<Expression>> columnReferences, Map<NodeRef<Parameter>, Expression> parameters) {
    requireNonNull(columnReferences, "columnReferences is null");
    verifyExpressionIsConstant(columnReferences, expression);
    // add coercions
    Expression rewrite = Coercer.addCoercions(expression, coercions, typeOnlyCoercions);
    // redo the analysis since above expression rewriter might create new expressions which do not have entries in the type map
    ExpressionAnalyzer analyzer = createConstantAnalyzer(plannerContext, accessControl, session, parameters, WarningCollector.NOOP);
    analyzer.analyze(rewrite, Scope.create());
    // remove syntax sugar
    rewrite = DesugarAtTimeZoneRewriter.rewrite(rewrite, analyzer.getExpressionTypes(), plannerContext.getMetadata(), session);
    // The optimization above may have rewritten the expression tree which breaks all the identity maps, so redo the analysis
    // to re-analyze coercions that might be necessary
    analyzer = createConstantAnalyzer(plannerContext, accessControl, session, parameters, WarningCollector.NOOP);
    analyzer.analyze(rewrite, Scope.create());
    // expressionInterpreter/optimizer only understands a subset of expression types
    // TODO: remove this when the new expression tree is implemented
    Expression canonicalized = canonicalizeExpression(rewrite, analyzer.getExpressionTypes(), plannerContext, session);
    // The optimization above may have rewritten the expression tree which breaks all the identity maps, so redo the analysis
    // to re-analyze coercions that might be necessary
    analyzer = createConstantAnalyzer(plannerContext, accessControl, session, parameters, WarningCollector.NOOP);
    analyzer.analyze(canonicalized, Scope.create());
    // resolve functions
    Expression resolved = rewriteResolvedFunctions(canonicalized, analyzer.getResolvedFunctions());
    // The optimization above may have rewritten the expression tree which breaks all the identity maps, so redo the analysis
    // to re-analyze coercions that might be necessary
    analyzer = createConstantAnalyzer(plannerContext, accessControl, session, parameters, WarningCollector.NOOP);
    analyzer.analyze(resolved, Scope.create());
    // evaluate the expression
    return new ExpressionInterpreter(resolved, plannerContext, session, analyzer.getExpressionTypes()).evaluate();
}
Also used : NullIfExpression(io.trino.sql.tree.NullIfExpression) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) InListExpression(io.trino.sql.tree.InListExpression) NotExpression(io.trino.sql.tree.NotExpression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) BindExpression(io.trino.sql.tree.BindExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) IfExpression(io.trino.sql.tree.IfExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) Expression(io.trino.sql.tree.Expression) SimpleCaseExpression(io.trino.sql.tree.SimpleCaseExpression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) LambdaExpression(io.trino.sql.tree.LambdaExpression) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) CanonicalizeExpressionRewriter.canonicalizeExpression(io.trino.sql.planner.iterative.rule.CanonicalizeExpressionRewriter.canonicalizeExpression) ExpressionAnalyzer(io.trino.sql.analyzer.ExpressionAnalyzer)

Example 19 with Expression

use of io.trino.sql.tree.Expression in project trino by trinodb.

the class ExpressionInterpreter method evaluate.

public Object evaluate(SymbolResolver inputs) {
    Object result = new Visitor(false).processWithExceptionHandling(expression, inputs);
    verify(!(result instanceof Expression), "Expression interpreter returned an unresolved expression");
    return result;
}
Also used : AstVisitor(io.trino.sql.tree.AstVisitor) NullIfExpression(io.trino.sql.tree.NullIfExpression) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) InListExpression(io.trino.sql.tree.InListExpression) NotExpression(io.trino.sql.tree.NotExpression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) BindExpression(io.trino.sql.tree.BindExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) IfExpression(io.trino.sql.tree.IfExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) Expression(io.trino.sql.tree.Expression) SimpleCaseExpression(io.trino.sql.tree.SimpleCaseExpression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) LambdaExpression(io.trino.sql.tree.LambdaExpression) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) CanonicalizeExpressionRewriter.canonicalizeExpression(io.trino.sql.planner.iterative.rule.CanonicalizeExpressionRewriter.canonicalizeExpression)

Example 20 with Expression

use of io.trino.sql.tree.Expression in project trino by trinodb.

the class DomainTranslator method toPredicate.

private Expression toPredicate(Session session, Domain domain, SymbolReference reference) {
    if (domain.getValues().isNone()) {
        return domain.isNullAllowed() ? new IsNullPredicate(reference) : FALSE_LITERAL;
    }
    if (domain.getValues().isAll()) {
        return domain.isNullAllowed() ? TRUE_LITERAL : new NotExpression(new IsNullPredicate(reference));
    }
    List<Expression> disjuncts = new ArrayList<>();
    disjuncts.addAll(domain.getValues().getValuesProcessor().transform(ranges -> extractDisjuncts(session, domain.getType(), ranges, reference), discreteValues -> extractDisjuncts(session, domain.getType(), discreteValues, reference), allOrNone -> {
        throw new IllegalStateException("Case should not be reachable");
    }));
    // Add nullability disjuncts
    if (domain.isNullAllowed()) {
        disjuncts.add(new IsNullPredicate(reference));
    }
    return combineDisjunctsWithDefault(plannerContext.getMetadata(), disjuncts, TRUE_LITERAL);
}
Also used : IsNullPredicate(io.trino.sql.tree.IsNullPredicate) TableProceduresPropertyManager(io.trino.metadata.TableProceduresPropertyManager) FAIL_ON_NULL(io.trino.spi.function.InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL) ExpressionUtils.combineDisjunctsWithDefault(io.trino.sql.ExpressionUtils.combineDisjunctsWithDefault) InvocationConvention.simpleConvention(io.trino.spi.function.InvocationConvention.simpleConvention) PeekingIterator(com.google.common.collect.PeekingIterator) InterpretedFunctionInvoker(io.trino.sql.InterpretedFunctionInvoker) TypeUtils.isFloatingPointNaN(io.trino.spi.type.TypeUtils.isFloatingPointNaN) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) BooleanLiteral(io.trino.sql.tree.BooleanLiteral) ExpressionUtils.or(io.trino.sql.ExpressionUtils.or) Slices(io.airlift.slice.Slices) Map(java.util.Map) GREATER_THAN(io.trino.sql.tree.ComparisonExpression.Operator.GREATER_THAN) DiscreteValues(io.trino.spi.predicate.DiscreteValues) SqlParser(io.trino.sql.parser.SqlParser) FunctionCall(io.trino.sql.tree.FunctionCall) BetweenPredicate(io.trino.sql.tree.BetweenPredicate) SliceUtf8.getCodePointAt(io.airlift.slice.SliceUtf8.getCodePointAt) SATURATED_FLOOR_CAST(io.trino.spi.function.OperatorType.SATURATED_FLOOR_CAST) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Range(io.trino.spi.predicate.Range) Ranges(io.trino.spi.predicate.Ranges) ResolvedFunction(io.trino.metadata.ResolvedFunction) Domain(io.trino.spi.predicate.Domain) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) FALSE_LITERAL(io.trino.sql.tree.BooleanLiteral.FALSE_LITERAL) Set(java.util.Set) TrinoException(io.trino.spi.TrinoException) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) ValueSet(io.trino.spi.predicate.ValueSet) InPredicate(io.trino.sql.tree.InPredicate) Preconditions.checkState(com.google.common.base.Preconditions.checkState) LESS_THAN_OR_EQUAL(io.trino.sql.tree.ComparisonExpression.Operator.LESS_THAN_OR_EQUAL) SessionPropertyManager(io.trino.metadata.SessionPropertyManager) EQUAL(io.trino.sql.tree.ComparisonExpression.Operator.EQUAL) NOT_EQUAL(io.trino.sql.tree.ComparisonExpression.Operator.NOT_EQUAL) List(java.util.List) SymbolReference(io.trino.sql.tree.SymbolReference) Optional(java.util.Optional) Expression(io.trino.sql.tree.Expression) InListExpression(io.trino.sql.tree.InListExpression) ExpressionUtils.combineConjuncts(io.trino.sql.ExpressionUtils.combineConjuncts) NEVER_NULL(io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.NEVER_NULL) Session(io.trino.Session) PlannerContext(io.trino.sql.PlannerContext) MethodHandle(java.lang.invoke.MethodHandle) Slice(io.airlift.slice.Slice) AllowAllAccessControl(io.trino.security.AllowAllAccessControl) NullableValue(io.trino.spi.predicate.NullableValue) DoubleType(io.trino.spi.type.DoubleType) LikePredicate(io.trino.sql.tree.LikePredicate) Type(io.trino.spi.type.Type) TypeCoercion(io.trino.type.TypeCoercion) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) Collectors.collectingAndThen(java.util.stream.Collectors.collectingAndThen) ArrayList(java.util.ArrayList) Cast(io.trino.sql.tree.Cast) VarcharType(io.trino.spi.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList) IsNotNullPredicate(io.trino.sql.tree.IsNotNullPredicate) NodeRef(io.trino.sql.tree.NodeRef) NotExpression(io.trino.sql.tree.NotExpression) Objects.requireNonNull(java.util.Objects.requireNonNull) NullLiteral(io.trino.sql.tree.NullLiteral) TableProceduresRegistry(io.trino.metadata.TableProceduresRegistry) Iterators.peekingIterator(com.google.common.collect.Iterators.peekingIterator) GREATER_THAN_OR_EQUAL(io.trino.sql.tree.ComparisonExpression.Operator.GREATER_THAN_OR_EQUAL) Nullable(javax.annotation.Nullable) AstVisitor(io.trino.sql.tree.AstVisitor) SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) StringLiteral(io.trino.sql.tree.StringLiteral) OperatorNotFoundException(io.trino.metadata.OperatorNotFoundException) Throwables(com.google.common.base.Throwables) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) TRUE_LITERAL(io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL) LESS_THAN(io.trino.sql.tree.ComparisonExpression.Operator.LESS_THAN) TupleDomain(io.trino.spi.predicate.TupleDomain) SliceUtf8.setCodePointAt(io.airlift.slice.SliceUtf8.setCodePointAt) GENERIC_INTERNAL_ERROR(io.trino.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR) LikeFunctions(io.trino.type.LikeFunctions) Collectors.toList(java.util.stream.Collectors.toList) ExpressionUtils.and(io.trino.sql.ExpressionUtils.and) StatementAnalyzerFactory(io.trino.sql.analyzer.StatementAnalyzerFactory) TablePropertyManager(io.trino.metadata.TablePropertyManager) RealType(io.trino.spi.type.RealType) LogicalExpression(io.trino.sql.tree.LogicalExpression) SliceUtf8.countCodePoints(io.airlift.slice.SliceUtf8.countCodePoints) AnalyzePropertyManager(io.trino.metadata.AnalyzePropertyManager) SortedRangeSet(io.trino.spi.predicate.SortedRangeSet) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) Expression(io.trino.sql.tree.Expression) InListExpression(io.trino.sql.tree.InListExpression) NotExpression(io.trino.sql.tree.NotExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) ArrayList(java.util.ArrayList) IsNullPredicate(io.trino.sql.tree.IsNullPredicate) NotExpression(io.trino.sql.tree.NotExpression)

Aggregations

Expression (io.trino.sql.tree.Expression)257 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)111 ImmutableList (com.google.common.collect.ImmutableList)88 Symbol (io.trino.sql.planner.Symbol)77 ProjectNode (io.trino.sql.planner.plan.ProjectNode)65 PlanNode (io.trino.sql.planner.plan.PlanNode)63 Map (java.util.Map)63 NotExpression (io.trino.sql.tree.NotExpression)62 Test (org.testng.annotations.Test)58 Objects.requireNonNull (java.util.Objects.requireNonNull)57 Assignments (io.trino.sql.planner.plan.Assignments)55 InListExpression (io.trino.sql.tree.InListExpression)55 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)54 SymbolReference (io.trino.sql.tree.SymbolReference)53 Type (io.trino.spi.type.Type)52 List (java.util.List)52 Set (java.util.Set)52 SubscriptExpression (io.trino.sql.tree.SubscriptExpression)48 ImmutableMap (com.google.common.collect.ImmutableMap)47 Optional (java.util.Optional)47