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