Search in sources :

Example 11 with NodeRef

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

the class TestExpressionInterpreter method optimize.

private static Object optimize(Expression expression) {
    Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypes(TEST_SESSION, METADATA, SQL_PARSER, SYMBOL_TYPES, expression, emptyList(), WarningCollector.NOOP);
    ExpressionInterpreter interpreter = expressionOptimizer(expression, METADATA, TEST_SESSION, expressionTypes);
    return interpreter.optimize(variable -> {
        Symbol symbol = new Symbol(variable.getName());
        Object value = symbolConstant(symbol);
        if (value == null) {
            return symbol.toSymbolReference();
        }
        return value;
    });
}
Also used : NodeRef(com.facebook.presto.sql.tree.NodeRef) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) Symbol(com.facebook.presto.sql.planner.Symbol) RowExpressionInterpreter(com.facebook.presto.sql.planner.RowExpressionInterpreter) ExpressionInterpreter(com.facebook.presto.sql.planner.ExpressionInterpreter) RowExpressionInterpreter.rowExpressionInterpreter(com.facebook.presto.sql.planner.RowExpressionInterpreter.rowExpressionInterpreter)

Example 12 with NodeRef

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

the class SqlFunctionUtils method rewriteLambdaExpression.

private static Expression rewriteLambdaExpression(Expression sqlFunction, Map<String, VariableReferenceExpression> arguments, ExpressionAnalysis functionAnalysis, PlanVariableAllocator variableAllocator) {
    Map<NodeRef<Identifier>, LambdaArgumentDeclaration> lambdaArgumentReferences = functionAnalysis.getLambdaArgumentReferences();
    Map<NodeRef<Expression>, Type> expressionTypes = functionAnalysis.getExpressionTypes();
    // Rewrite reference to LambdaArgumentDeclaration
    Map<NodeRef<LambdaArgumentDeclaration>, VariableReferenceExpression> variables = expressionTypes.entrySet().stream().filter(entry -> entry.getKey().getNode() instanceof LambdaArgumentDeclaration).distinct().collect(toImmutableMap(entry -> NodeRef.of((LambdaArgumentDeclaration) entry.getKey().getNode()), entry -> variableAllocator.newVariable(((LambdaArgumentDeclaration) entry.getKey().getNode()).getName(), entry.getValue(), "lambda")));
    Expression rewritten = ExpressionTreeRewriter.rewriteWith(new ExpressionRewriter<Map<NodeRef<Identifier>, LambdaArgumentDeclaration>>() {

        @Override
        public Expression rewriteLambdaExpression(LambdaExpression node, Map<NodeRef<Identifier>, LambdaArgumentDeclaration> context, ExpressionTreeRewriter<Map<NodeRef<Identifier>, LambdaArgumentDeclaration>> treeRewriter) {
            return new LambdaExpression(node.getArguments().stream().map(argument -> new LambdaArgumentDeclaration(new Identifier(variables.get(NodeRef.of(argument)).getName()))).collect(toImmutableList()), treeRewriter.rewrite(node.getBody(), context));
        }

        @Override
        public Expression rewriteIdentifier(Identifier node, Map<NodeRef<Identifier>, LambdaArgumentDeclaration> context, ExpressionTreeRewriter<Map<NodeRef<Identifier>, LambdaArgumentDeclaration>> treeRewriter) {
            NodeRef<Identifier> ref = NodeRef.of(node);
            if (context.containsKey(ref)) {
                return createSymbolReference(variables.get(NodeRef.of(context.get(ref))));
            }
            return node;
        }
    }, sqlFunction, lambdaArgumentReferences);
    // Rewrite function input referenced in lambda
    rewritten = ExpressionTreeRewriter.rewriteWith(new ExpressionRewriter<Map<String, VariableReferenceExpression>>() {

        @Override
        public Expression rewriteIdentifier(Identifier node, Map<String, VariableReferenceExpression> context, ExpressionTreeRewriter<Map<String, VariableReferenceExpression>> treeRewriter) {
            if (context.containsKey(node.getValue())) {
                return createSymbolReference(context.get(node.getValue()));
            }
            return node;
        }
    }, rewritten, arguments);
    // Desugar lambda capture
    return LambdaCaptureDesugaringRewriter.rewrite(rewritten, variableAllocator);
}
Also used : ExpressionTreeRewriter(com.facebook.presto.sql.tree.ExpressionTreeRewriter) LambdaArgumentDeclaration(com.facebook.presto.sql.tree.LambdaArgumentDeclaration) RowExpressionRewriter(com.facebook.presto.expressions.RowExpressionRewriter) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ExpressionAnalyzer.analyzeSqlFunctionExpression(com.facebook.presto.sql.analyzer.ExpressionAnalyzer.analyzeSqlFunctionExpression) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) Identifier(com.facebook.presto.sql.tree.Identifier) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Map(java.util.Map) LambdaCaptureDesugaringRewriter(com.facebook.presto.sql.planner.iterative.rule.LambdaCaptureDesugaringRewriter) Cast(com.facebook.presto.sql.tree.Cast) SQL(com.facebook.presto.spi.function.FunctionImplementationType.SQL) ENGLISH(java.util.Locale.ENGLISH) Type(com.facebook.presto.common.type.Type) RowExpression(com.facebook.presto.spi.relation.RowExpression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) RowExpressionTreeRewriter(com.facebook.presto.expressions.RowExpressionTreeRewriter) ImmutableMap(com.google.common.collect.ImmutableMap) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) String.format(java.lang.String.format) SqlParser(com.facebook.presto.sql.parser.SqlParser) ExpressionRewriter(com.facebook.presto.sql.tree.ExpressionRewriter) Preconditions.checkState(com.google.common.base.Preconditions.checkState) NodeRef(com.facebook.presto.sql.tree.NodeRef) FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) List(java.util.List) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) SqlFunctionId(com.facebook.presto.spi.function.SqlFunctionId) Expression(com.facebook.presto.sql.tree.Expression) AS_DOUBLE(com.facebook.presto.sql.parser.ParsingOptions.DecimalLiteralTreatment.AS_DOUBLE) AS_DECIMAL(com.facebook.presto.sql.parser.ParsingOptions.DecimalLiteralTreatment.AS_DECIMAL) PlanVariableAllocator(com.facebook.presto.sql.planner.PlanVariableAllocator) Function.identity(java.util.function.Function.identity) Optional(java.util.Optional) ParsingOptions(com.facebook.presto.sql.parser.ParsingOptions) ExpressionAnalysis(com.facebook.presto.sql.analyzer.ExpressionAnalysis) Metadata(com.facebook.presto.metadata.Metadata) SqlInvokedScalarFunctionImplementation(com.facebook.presto.spi.function.SqlInvokedScalarFunctionImplementation) ExpressionTreeUtils.createSymbolReference(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference) ExpressionTreeRewriter(com.facebook.presto.sql.tree.ExpressionTreeRewriter) RowExpressionTreeRewriter(com.facebook.presto.expressions.RowExpressionTreeRewriter) NodeRef(com.facebook.presto.sql.tree.NodeRef) Type(com.facebook.presto.common.type.Type) LambdaArgumentDeclaration(com.facebook.presto.sql.tree.LambdaArgumentDeclaration) Identifier(com.facebook.presto.sql.tree.Identifier) RowExpressionRewriter(com.facebook.presto.expressions.RowExpressionRewriter) ExpressionRewriter(com.facebook.presto.sql.tree.ExpressionRewriter) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ExpressionAnalyzer.analyzeSqlFunctionExpression(com.facebook.presto.sql.analyzer.ExpressionAnalyzer.analyzeSqlFunctionExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) Expression(com.facebook.presto.sql.tree.Expression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression)

Example 13 with NodeRef

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

the class ExpressionInterpreter method evaluateConstantExpression.

public static Object evaluateConstantExpression(Expression expression, Type expectedType, Metadata metadata, Session session, List<Expression> parameters) {
    ExpressionAnalyzer analyzer = createConstantAnalyzer(metadata, session, parameters, WarningCollector.NOOP);
    analyzer.analyze(expression, Scope.create());
    Type actualType = analyzer.getExpressionTypes().get(NodeRef.of(expression));
    if (!metadata.getFunctionAndTypeManager().canCoerce(actualType, expectedType)) {
        throw new SemanticException(SemanticErrorCode.TYPE_MISMATCH, expression, format("Cannot cast type %s to %s", actualType.getTypeSignature(), expectedType.getTypeSignature()));
    }
    Map<NodeRef<Expression>, Type> coercions = ImmutableMap.<NodeRef<Expression>, Type>builder().putAll(analyzer.getExpressionCoercions()).put(NodeRef.of(expression), expectedType).build();
    return evaluateConstantExpression(expression, coercions, analyzer.getTypeOnlyCoercions(), metadata, session, ImmutableSet.of(), parameters);
}
Also used : NodeRef(com.facebook.presto.sql.tree.NodeRef) FunctionImplementationType(com.facebook.presto.spi.function.FunctionImplementationType) TypeUtils.isEnumType(com.facebook.presto.common.type.TypeUtils.isEnumType) ArrayType(com.facebook.presto.common.type.ArrayType) OperatorType(com.facebook.presto.common.function.OperatorType) FunctionType(com.facebook.presto.common.type.FunctionType) RowType(com.facebook.presto.common.type.RowType) LiteralEncoder.isSupportedLiteralType(com.facebook.presto.sql.planner.LiteralEncoder.isSupportedLiteralType) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) Type(com.facebook.presto.common.type.Type) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) SimpleCaseExpression(com.facebook.presto.sql.tree.SimpleCaseExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) CanonicalizeExpressionRewriter.canonicalizeExpression(com.facebook.presto.sql.planner.iterative.rule.CanonicalizeExpressionRewriter.canonicalizeExpression) IfExpression(com.facebook.presto.sql.tree.IfExpression) BindExpression(com.facebook.presto.sql.tree.BindExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) SqlFunctionUtils.getSqlFunctionExpression(com.facebook.presto.sql.relational.SqlFunctionUtils.getSqlFunctionExpression) ArithmeticUnaryExpression(com.facebook.presto.sql.tree.ArithmeticUnaryExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) NullIfExpression(com.facebook.presto.sql.tree.NullIfExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) ExpressionAnalyzer(com.facebook.presto.sql.analyzer.ExpressionAnalyzer) SemanticException(com.facebook.presto.sql.analyzer.SemanticException)

Example 14 with NodeRef

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

the class GroupingOperationRewriter method rewriteGroupingOperation.

public static Expression rewriteGroupingOperation(GroupingOperation expression, List<Set<Integer>> groupingSets, Multimap<NodeRef<Expression>, FieldId> columnReferenceFields, Optional<VariableReferenceExpression> groupIdVariable) {
    requireNonNull(groupIdVariable, "groupIdVariable is null");
    // See SQL:2011:4.16.2 and SQL:2011:6.9.10.
    if (groupingSets.size() == 1) {
        return new LongLiteral("0");
    } else {
        checkState(groupIdVariable.isPresent(), "groupId symbol is missing");
        RelationId relationId = checkAndGetColumnReferenceField(expression.getGroupingColumns().get(0), columnReferenceFields).getRelationId();
        List<Integer> columns = expression.getGroupingColumns().stream().map(NodeRef::of).peek(groupingColumn -> checkState(columnReferenceFields.containsKey(groupingColumn), "the grouping column is not in the columnReferencesField map")).map(columnReferenceFields::get).flatMap(Collection::stream).map(fieldId -> translateFieldToInteger(fieldId, relationId)).collect(toImmutableList());
        List<Expression> groupingResults = groupingSets.stream().map(groupingSet -> String.valueOf(calculateGrouping(groupingSet, columns))).map(LongLiteral::new).collect(toImmutableList());
        // It is necessary to add a 1 to the groupId because the underlying array is indexed starting at 1
        return new SubscriptExpression(new ArrayConstructor(groupingResults), new ArithmeticBinaryExpression(ADD, createSymbolReference(groupIdVariable.get()), new GenericLiteral("BIGINT", "1")));
    }
}
Also used : SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) RelationId(com.facebook.presto.sql.analyzer.RelationId) GroupingOperation(com.facebook.presto.sql.tree.GroupingOperation) Collection(java.util.Collection) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ExpressionTreeUtils.checkAndGetColumnReferenceField(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.checkAndGetColumnReferenceField) Set(java.util.Set) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Multimap(com.google.common.collect.Multimap) Preconditions.checkState(com.google.common.base.Preconditions.checkState) NodeRef(com.facebook.presto.sql.tree.NodeRef) List(java.util.List) ArrayConstructor(com.facebook.presto.sql.tree.ArrayConstructor) GenericLiteral(com.facebook.presto.sql.tree.GenericLiteral) Expression(com.facebook.presto.sql.tree.Expression) ADD(com.facebook.presto.sql.tree.ArithmeticBinaryExpression.Operator.ADD) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) Objects.requireNonNull(java.util.Objects.requireNonNull) FieldId(com.facebook.presto.sql.analyzer.FieldId) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) Optional(java.util.Optional) ExpressionTreeUtils.createSymbolReference(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) RelationId(com.facebook.presto.sql.analyzer.RelationId) Collection(java.util.Collection) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) ArrayConstructor(com.facebook.presto.sql.tree.ArrayConstructor) GenericLiteral(com.facebook.presto.sql.tree.GenericLiteral)

Example 15 with NodeRef

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

the class PlanBuilder method rowExpression.

public RowExpression rowExpression(String sql) {
    Expression expression = expression(sql);
    Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypes(session, metadata, new SqlParser(), getTypes(), expression, ImmutableList.of(), WarningCollector.NOOP);
    return SqlToRowExpressionTranslator.translate(expression, expressionTypes, ImmutableMap.of(), metadata.getFunctionAndTypeManager(), session);
}
Also used : NodeRef(com.facebook.presto.sql.tree.NodeRef) OperatorType(com.facebook.presto.common.function.OperatorType) Type(com.facebook.presto.common.type.Type) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) Expression(com.facebook.presto.sql.tree.Expression) SqlParser(com.facebook.presto.sql.parser.SqlParser)

Aggregations

NodeRef (com.facebook.presto.sql.tree.NodeRef)15 Type (com.facebook.presto.common.type.Type)14 Expression (com.facebook.presto.sql.tree.Expression)9 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)8 RowExpression (com.facebook.presto.spi.relation.RowExpression)6 ExpressionInterpreter (com.facebook.presto.sql.planner.ExpressionInterpreter)5 SqlParser (com.facebook.presto.sql.parser.SqlParser)4 OperatorType (com.facebook.presto.common.function.OperatorType)3 ArrayType (com.facebook.presto.common.type.ArrayType)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 ImmutableMap (com.google.common.collect.ImmutableMap)3 List (java.util.List)3 Session (com.facebook.presto.Session)2 DecimalType.createDecimalType (com.facebook.presto.common.type.DecimalType.createDecimalType)2 FunctionType (com.facebook.presto.common.type.FunctionType)2 RowType (com.facebook.presto.common.type.RowType)2 VarcharType.createVarcharType (com.facebook.presto.common.type.VarcharType.createVarcharType)2 FunctionAssertions.createExpression (com.facebook.presto.operator.scalar.FunctionAssertions.createExpression)2