Search in sources :

Example 1 with NodeRef

use of io.prestosql.sql.tree.NodeRef in project hetu-core by openlookeng.

the class PageProcessorBenchmark method rowExpression.

private RowExpression rowExpression(String value) {
    Expression expression = createExpression(value, METADATA, TypeProvider.copyOf(symbolTypes));
    Map<NodeRef<Expression>, Type> expressionTypes = TYPE_ANALYZER.getTypes(TEST_SESSION, TypeProvider.copyOf(symbolTypes), expression);
    return SqlToRowExpressionTranslator.translate(expression, SCALAR, expressionTypes, sourceLayout, METADATA.getFunctionAndTypeManager(), TEST_SESSION, true);
}
Also used : NodeRef(io.prestosql.sql.tree.NodeRef) Type(io.prestosql.spi.type.Type) FunctionAssertions.createExpression(io.prestosql.operator.scalar.FunctionAssertions.createExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Expression(io.prestosql.sql.tree.Expression)

Example 2 with NodeRef

use of io.prestosql.sql.tree.NodeRef in project hetu-core by openlookeng.

the class FilterStatsCalculator method simplifyExpression.

private Expression simplifyExpression(Session session, Expression predicate, TypeProvider types) {
    // TODO reuse io.prestosql.sql.planner.iterative.rule.SimplifyExpressions.rewrite
    Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypes(session, predicate, types);
    ExpressionInterpreter interpreter = ExpressionInterpreter.expressionOptimizer(predicate, metadata, session, expressionTypes);
    Object value = interpreter.optimize(NoOpSymbolResolver.INSTANCE);
    if (value == null) {
        // Expression evaluates to SQL null, which in Filter is equivalent to false. This assumes the expression is a top-level expression (eg. not in NOT).
        value = false;
    }
    return literalEncoder.toExpression(value, BOOLEAN);
}
Also used : NodeRef(io.prestosql.sql.tree.NodeRef) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) VarcharType(io.prestosql.spi.type.VarcharType) RowExpressionInterpreter(io.prestosql.sql.planner.RowExpressionInterpreter) ExpressionInterpreter(io.prestosql.sql.planner.ExpressionInterpreter)

Example 3 with NodeRef

use of io.prestosql.sql.tree.NodeRef in project hetu-core by openlookeng.

the class QueryPlanner method coerce.

/**
 * Creates a projection with any additional coercions by identity of the provided expressions.
 *
 * @return the new subplan and a mapping of each expression to the symbol representing the coercion or an existing symbol if a coercion wasn't needed
 */
public static PlanAndMappings coerce(PlanBuilder inputSubPlan, List<Expression> expressions, Analysis analysis, PlanNodeIdAllocator idAllocator, PlanSymbolAllocator planSymbolAllocator, TypeCoercion typeCoercion) {
    PlanBuilder subPlan = inputSubPlan;
    Assignments.Builder assignments = Assignments.builder();
    assignments.putAll(AssignmentUtils.identityAsSymbolReferences(subPlan.getRoot().getOutputSymbols()));
    ImmutableMap.Builder<NodeRef<Expression>, Symbol> mappings = ImmutableMap.builder();
    for (Expression expression : expressions) {
        Type coercion = analysis.getCoercion(expression);
        if (coercion != null) {
            Type type = analysis.getType(expression);
            Symbol symbol = planSymbolAllocator.newSymbol(expression, coercion);
            assignments.put(symbol, castToRowExpression(new Cast(subPlan.rewrite(expression), coercion.getTypeSignature().toString(), false, typeCoercion.isTypeOnlyCoercion(type, coercion))));
            mappings.put(NodeRef.of(expression), symbol);
        } else {
            mappings.put(NodeRef.of(expression), subPlan.translate(expression));
        }
    }
    subPlan = subPlan.withNewRoot(new ProjectNode(idAllocator.getNextId(), subPlan.getRoot(), assignments.build()));
    return new PlanAndMappings(subPlan, mappings.build());
}
Also used : Cast(io.prestosql.sql.tree.Cast) NodeRef(io.prestosql.sql.tree.NodeRef) FrameBoundType(io.prestosql.spi.sql.expression.Types.FrameBoundType) Type(io.prestosql.spi.type.Type) WindowFrameType(io.prestosql.spi.sql.expression.Types.WindowFrameType) RelationType(io.prestosql.sql.analyzer.RelationType) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) Expression(io.prestosql.sql.tree.Expression) RowExpression(io.prestosql.spi.relation.RowExpression) Symbol(io.prestosql.spi.plan.Symbol) Assignments(io.prestosql.spi.plan.Assignments) ProjectNode(io.prestosql.spi.plan.ProjectNode) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 4 with NodeRef

use of io.prestosql.sql.tree.NodeRef in project hetu-core by openlookeng.

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 (!new TypeCoercion(metadata::getType).canCoerce(actualType, expectedType)) {
        throw new SemanticException(SemanticErrorCode.TYPE_MISMATCH, expression, String.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(io.prestosql.sql.tree.NodeRef) RowType(io.prestosql.spi.type.RowType) VarcharType.createVarcharType(io.prestosql.spi.type.VarcharType.createVarcharType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) LiteralFunction.isSupportedLiteralType(io.prestosql.metadata.LiteralFunction.isSupportedLiteralType) FunctionType(io.prestosql.spi.type.FunctionType) CanonicalizeExpressionRewriter.canonicalizeExpression(io.prestosql.sql.planner.iterative.rule.CanonicalizeExpressionRewriter.canonicalizeExpression) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) BindExpression(io.prestosql.sql.tree.BindExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) ExpressionAnalyzer(io.prestosql.sql.analyzer.ExpressionAnalyzer) TypeCoercion(io.prestosql.type.TypeCoercion) SemanticException(io.prestosql.sql.analyzer.SemanticException)

Example 5 with NodeRef

use of io.prestosql.sql.tree.NodeRef in project hetu-core by openlookeng.

the class FunctionAssertions method interpret.

private Object interpret(Expression expression, Type expectedType, Session session) {
    Map<NodeRef<Expression>, Type> expressionTypes = typeAnalyzer.getTypes(session, TypeProvider.copyOf(INPUT_TYPES), expression);
    ExpressionInterpreter evaluator = ExpressionInterpreter.expressionInterpreter(expression, metadata, session, expressionTypes);
    Object result = evaluator.evaluate(symbol -> {
        int position = 0;
        int channel = INPUT_MAPPING.get(symbol);
        Type type = INPUT_TYPES.get(symbol);
        Block block = SOURCE_PAGE.getBlock(channel);
        if (block.isNull(position)) {
            return null;
        }
        Class<?> javaType = type.getJavaType();
        if (javaType == boolean.class) {
            return type.getBoolean(block, position);
        } else if (javaType == long.class) {
            return type.getLong(block, position);
        } else if (javaType == double.class) {
            return type.getDouble(block, position);
        } else if (javaType == Slice.class) {
            return type.getSlice(block, position);
        } else if (javaType == Block.class) {
            return type.getObject(block, position);
        } else {
            throw new UnsupportedOperationException("not yet implemented");
        }
    });
    // convert result from stack type to Type ObjectValue
    Block block = Utils.nativeValueToBlock(expectedType, result);
    return expectedType.getObjectValue(session.toConnectorSession(), block, 0);
}
Also used : NodeRef(io.prestosql.sql.tree.NodeRef) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) Slice(io.airlift.slice.Slice) ExpressionInterpreter(io.prestosql.sql.planner.ExpressionInterpreter) BlockAssertions.createSlicesBlock(io.prestosql.block.BlockAssertions.createSlicesBlock) BlockAssertions.createStringsBlock(io.prestosql.block.BlockAssertions.createStringsBlock) BlockAssertions.createBooleansBlock(io.prestosql.block.BlockAssertions.createBooleansBlock) BlockAssertions.createLongsBlock(io.prestosql.block.BlockAssertions.createLongsBlock) BlockAssertions.createTimestampsWithTimezoneBlock(io.prestosql.block.BlockAssertions.createTimestampsWithTimezoneBlock) BlockAssertions.createRowBlock(io.prestosql.block.BlockAssertions.createRowBlock) Block(io.prestosql.spi.block.Block) BlockAssertions.createIntsBlock(io.prestosql.block.BlockAssertions.createIntsBlock) BlockAssertions.createDoublesBlock(io.prestosql.block.BlockAssertions.createDoublesBlock)

Aggregations

NodeRef (io.prestosql.sql.tree.NodeRef)15 Type (io.prestosql.spi.type.Type)14 Expression (io.prestosql.sql.tree.Expression)10 ExpressionInterpreter (io.prestosql.sql.planner.ExpressionInterpreter)7 RowExpression (io.prestosql.spi.relation.RowExpression)6 ImmutableMap (com.google.common.collect.ImmutableMap)4 Symbol (io.prestosql.spi.plan.Symbol)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 OperatorType (io.prestosql.spi.function.OperatorType)3 DecimalType.createDecimalType (io.prestosql.spi.type.DecimalType.createDecimalType)3 LambdaExpression (io.prestosql.sql.tree.LambdaExpression)3 FunctionType (io.prestosql.spi.type.FunctionType)2 RowType (io.prestosql.spi.type.RowType)2 VarbinaryType (io.prestosql.spi.type.VarbinaryType)2 VarcharType (io.prestosql.spi.type.VarcharType)2 VarcharType.createVarcharType (io.prestosql.spi.type.VarcharType.createVarcharType)2 PlanSymbolAllocator (io.prestosql.sql.planner.PlanSymbolAllocator)2 RowExpressionInterpreter (io.prestosql.sql.planner.RowExpressionInterpreter)2 TypeAnalyzer (io.prestosql.sql.planner.TypeAnalyzer)2 CoalesceExpression (io.prestosql.sql.tree.CoalesceExpression)2