Search in sources :

Example 1 with ConstantExpression

use of io.trino.sql.relational.ConstantExpression in project trino by trinodb.

the class CursorProcessorCompiler method fieldReferenceCompiler.

private static RowExpressionVisitor<BytecodeNode, Scope> fieldReferenceCompiler(Variable cursorVariable) {
    return new RowExpressionVisitor<>() {

        @Override
        public BytecodeNode visitInputReference(InputReferenceExpression node, Scope scope) {
            int field = node.getField();
            Type type = node.getType();
            Variable wasNullVariable = scope.getVariable("wasNull");
            Class<?> javaType = type.getJavaType();
            IfStatement ifStatement = new IfStatement();
            ifStatement.condition().setDescription(format("cursor.get%s(%d)", type, field)).getVariable(cursorVariable).push(field).invokeInterface(RecordCursor.class, "isNull", boolean.class, int.class);
            ifStatement.ifTrue().putVariable(wasNullVariable, true).pushJavaDefault(javaType);
            ifStatement.ifFalse().getVariable(cursorVariable).push(field);
            if (javaType == boolean.class) {
                ifStatement.ifFalse().invokeInterface(RecordCursor.class, "getBoolean", boolean.class, int.class);
            } else if (javaType == long.class) {
                ifStatement.ifFalse().invokeInterface(RecordCursor.class, "getLong", long.class, int.class);
            } else if (javaType == double.class) {
                ifStatement.ifFalse().invokeInterface(RecordCursor.class, "getDouble", double.class, int.class);
            } else if (javaType == Slice.class) {
                ifStatement.ifFalse().invokeInterface(RecordCursor.class, "getSlice", Slice.class, int.class);
            } else {
                ifStatement.ifFalse().invokeInterface(RecordCursor.class, "getObject", Object.class, int.class).checkCast(javaType);
            }
            return ifStatement;
        }

        @Override
        public BytecodeNode visitCall(CallExpression call, Scope scope) {
            throw new UnsupportedOperationException("not yet implemented");
        }

        @Override
        public BytecodeNode visitSpecialForm(SpecialForm specialForm, Scope context) {
            throw new UnsupportedOperationException("not yet implemented");
        }

        @Override
        public BytecodeNode visitConstant(ConstantExpression literal, Scope scope) {
            throw new UnsupportedOperationException("not yet implemented");
        }

        @Override
        public BytecodeNode visitLambda(LambdaDefinitionExpression lambda, Scope context) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitVariableReference(VariableReferenceExpression reference, Scope context) {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : InputReferenceExpression(io.trino.sql.relational.InputReferenceExpression) Variable(io.airlift.bytecode.Variable) RecordCursor(io.trino.spi.connector.RecordCursor) ConstantExpression(io.trino.sql.relational.ConstantExpression) IfStatement(io.airlift.bytecode.control.IfStatement) Type(io.trino.spi.type.Type) Scope(io.airlift.bytecode.Scope) Slice(io.airlift.slice.Slice) VariableReferenceExpression(io.trino.sql.relational.VariableReferenceExpression) RowExpressionVisitor(io.trino.sql.relational.RowExpressionVisitor) CallExpression(io.trino.sql.relational.CallExpression) SpecialForm(io.trino.sql.relational.SpecialForm) LambdaDefinitionExpression(io.trino.sql.relational.LambdaDefinitionExpression)

Example 2 with ConstantExpression

use of io.trino.sql.relational.ConstantExpression in project trino by trinodb.

the class InCodeGenerator method generateExpression.

@Override
public BytecodeNode generateExpression(BytecodeGeneratorContext generatorContext) {
    Type type = valueExpression.getType();
    Class<?> javaType = type.getJavaType();
    SwitchGenerationCase switchGenerationCase = checkSwitchGenerationCase(type, testExpressions);
    MethodHandle equalsMethodHandle = generatorContext.getScalarFunctionInvoker(resolvedEqualsFunction, simpleConvention(NULLABLE_RETURN, NEVER_NULL, NEVER_NULL)).getMethodHandle();
    MethodHandle hashCodeMethodHandle = generatorContext.getScalarFunctionInvoker(resolvedHashCodeFunction, simpleConvention(FAIL_ON_NULL, NEVER_NULL)).getMethodHandle();
    MethodHandle indeterminateMethodHandle = generatorContext.getScalarFunctionInvoker(resolvedIsIndeterminate, simpleConvention(FAIL_ON_NULL, NEVER_NULL)).getMethodHandle();
    ImmutableListMultimap.Builder<Integer, BytecodeNode> hashBucketsBuilder = ImmutableListMultimap.builder();
    ImmutableList.Builder<BytecodeNode> defaultBucket = ImmutableList.builder();
    ImmutableSet.Builder<Object> constantValuesBuilder = ImmutableSet.builder();
    for (RowExpression testValue : testExpressions) {
        BytecodeNode testBytecode = generatorContext.generate(testValue);
        if (isDeterminateConstant(testValue, indeterminateMethodHandle)) {
            ConstantExpression constant = (ConstantExpression) testValue;
            Object object = constant.getValue();
            switch(switchGenerationCase) {
                case DIRECT_SWITCH:
                case SET_CONTAINS:
                    constantValuesBuilder.add(object);
                    break;
                case HASH_SWITCH:
                    try {
                        int hashCode = Long.hashCode((Long) hashCodeMethodHandle.invoke(object));
                        hashBucketsBuilder.put(hashCode, testBytecode);
                    } catch (Throwable throwable) {
                        throw new IllegalArgumentException("Error processing IN statement: error calculating hash code for " + object, throwable);
                    }
                    break;
                default:
                    throw new IllegalArgumentException("Not supported switch generation case: " + switchGenerationCase);
            }
        } else {
            defaultBucket.add(testBytecode);
        }
    }
    ImmutableListMultimap<Integer, BytecodeNode> hashBuckets = hashBucketsBuilder.build();
    ImmutableSet<Object> constantValues = constantValuesBuilder.build();
    LabelNode end = new LabelNode("end");
    LabelNode match = new LabelNode("match");
    LabelNode noMatch = new LabelNode("noMatch");
    LabelNode defaultLabel = new LabelNode("default");
    Scope scope = generatorContext.getScope();
    Variable value = scope.createTempVariable(javaType);
    BytecodeNode switchBlock;
    Variable expression = scope.createTempVariable(int.class);
    SwitchBuilder switchBuilder = new SwitchBuilder().expression(expression);
    switch(switchGenerationCase) {
        case DIRECT_SWITCH:
            // For these types, it's safe to not use Trino HASH_CODE and EQUAL operator.
            for (Object constantValue : constantValues) {
                switchBuilder.addCase(toIntExact((Long) constantValue), jump(match));
            }
            switchBuilder.defaultCase(jump(defaultLabel));
            switchBlock = new BytecodeBlock().comment("lookupSwitch(<stackValue>))").append(new IfStatement().condition(invokeStatic(InCodeGenerator.class, "isInteger", boolean.class, value)).ifFalse(new BytecodeBlock().gotoLabel(defaultLabel))).append(expression.set(value.cast(int.class))).append(switchBuilder.build());
            break;
        case HASH_SWITCH:
            for (Map.Entry<Integer, Collection<BytecodeNode>> bucket : hashBuckets.asMap().entrySet()) {
                Collection<BytecodeNode> testValues = bucket.getValue();
                BytecodeBlock caseBlock = buildInCase(generatorContext, scope, resolvedEqualsFunction, match, defaultLabel, value, testValues, false, resolvedIsIndeterminate);
                switchBuilder.addCase(bucket.getKey(), caseBlock);
            }
            switchBuilder.defaultCase(jump(defaultLabel));
            Binding hashCodeBinding = generatorContext.getCallSiteBinder().bind(hashCodeMethodHandle);
            switchBlock = new BytecodeBlock().comment("lookupSwitch(hashCode(<stackValue>))").getVariable(value).append(invoke(hashCodeBinding, resolvedHashCodeFunction.getSignature())).invokeStatic(Long.class, "hashCode", int.class, long.class).putVariable(expression).append(switchBuilder.build());
            break;
        case SET_CONTAINS:
            Set<?> constantValuesSet = toFastutilHashSet(constantValues, type, hashCodeMethodHandle, equalsMethodHandle);
            Binding constant = generatorContext.getCallSiteBinder().bind(constantValuesSet, constantValuesSet.getClass());
            switchBlock = new BytecodeBlock().comment("inListSet.contains(<stackValue>)").append(new IfStatement().condition(new BytecodeBlock().comment("value").getVariable(value).comment("set").append(loadConstant(constant)).invokeStatic(FastutilSetHelper.class, "in", boolean.class, javaType.isPrimitive() ? javaType : Object.class, constantValuesSet.getClass())).ifTrue(jump(match)));
            break;
        default:
            throw new IllegalArgumentException("Not supported switch generation case: " + switchGenerationCase);
    }
    BytecodeBlock defaultCaseBlock = buildInCase(generatorContext, scope, resolvedEqualsFunction, match, noMatch, value, defaultBucket.build(), true, resolvedIsIndeterminate).setDescription("default");
    BytecodeBlock block = new BytecodeBlock().comment("IN").append(generatorContext.generate(valueExpression)).append(ifWasNullPopAndGoto(scope, end, boolean.class, javaType)).putVariable(value).append(switchBlock).visitLabel(defaultLabel).append(defaultCaseBlock);
    BytecodeBlock matchBlock = new BytecodeBlock().setDescription("match").visitLabel(match).append(generatorContext.wasNull().set(constantFalse())).push(true).gotoLabel(end);
    block.append(matchBlock);
    BytecodeBlock noMatchBlock = new BytecodeBlock().setDescription("noMatch").visitLabel(noMatch).push(false).gotoLabel(end);
    block.append(noMatchBlock);
    block.visitLabel(end);
    return block;
}
Also used : LabelNode(io.airlift.bytecode.instruction.LabelNode) Variable(io.airlift.bytecode.Variable) ImmutableList(com.google.common.collect.ImmutableList) ConstantExpression(io.trino.sql.relational.ConstantExpression) IfStatement(io.airlift.bytecode.control.IfStatement) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) BytecodeNode(io.airlift.bytecode.BytecodeNode) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) RowExpression(io.trino.sql.relational.RowExpression) Type(io.trino.spi.type.Type) Scope(io.airlift.bytecode.Scope) SwitchBuilder(io.airlift.bytecode.control.SwitchStatement.SwitchBuilder) Collection(java.util.Collection) Map(java.util.Map) MethodHandle(java.lang.invoke.MethodHandle)

Example 3 with ConstantExpression

use of io.trino.sql.relational.ConstantExpression in project trino by trinodb.

the class PageFunctionCompiler method compileProjectionInternal.

private Supplier<PageProjection> compileProjectionInternal(RowExpression projection, Optional<String> classNameSuffix) {
    requireNonNull(projection, "projection is null");
    if (projection instanceof InputReferenceExpression) {
        InputReferenceExpression input = (InputReferenceExpression) projection;
        InputPageProjection projectionFunction = new InputPageProjection(input.getField(), input.getType());
        return () -> projectionFunction;
    }
    if (projection instanceof ConstantExpression) {
        ConstantExpression constant = (ConstantExpression) projection;
        ConstantPageProjection projectionFunction = new ConstantPageProjection(constant.getValue(), constant.getType());
        return () -> projectionFunction;
    }
    PageFieldsToInputParametersRewriter.Result result = rewritePageFieldsToInputParameters(projection);
    CallSiteBinder callSiteBinder = new CallSiteBinder();
    // generate Work
    ClassDefinition pageProjectionWorkDefinition = definePageProjectWorkClass(result.getRewrittenExpression(), callSiteBinder, classNameSuffix);
    Class<?> pageProjectionWorkClass;
    try {
        pageProjectionWorkClass = defineClass(pageProjectionWorkDefinition, Work.class, callSiteBinder.getBindings(), getClass().getClassLoader());
    } catch (Exception e) {
        if (Throwables.getRootCause(e) instanceof MethodTooLargeException) {
            throw new TrinoException(COMPILER_ERROR, "Query exceeded maximum columns. Please reduce the number of columns referenced and re-run the query.", e);
        }
        throw new TrinoException(COMPILER_ERROR, e);
    }
    return () -> new GeneratedPageProjection(result.getRewrittenExpression(), isDeterministic(result.getRewrittenExpression()), result.getInputChannels(), constructorMethodHandle(pageProjectionWorkClass, BlockBuilder.class, ConnectorSession.class, Page.class, SelectedPositions.class));
}
Also used : InputReferenceExpression(io.trino.sql.relational.InputReferenceExpression) ConstantPageProjection(io.trino.operator.project.ConstantPageProjection) ConstantExpression(io.trino.sql.relational.ConstantExpression) GeneratedPageProjection(io.trino.operator.project.GeneratedPageProjection) Page(io.trino.spi.Page) ClassDefinition(io.airlift.bytecode.ClassDefinition) TrinoException(io.trino.spi.TrinoException) MethodTooLargeException(org.objectweb.asm.MethodTooLargeException) InputPageProjection(io.trino.operator.project.InputPageProjection) PageFieldsToInputParametersRewriter(io.trino.operator.project.PageFieldsToInputParametersRewriter) MethodTooLargeException(org.objectweb.asm.MethodTooLargeException) SelectedPositions(io.trino.operator.project.SelectedPositions) Work(io.trino.operator.Work) TrinoException(io.trino.spi.TrinoException) ConnectorSession(io.trino.spi.connector.ConnectorSession) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 4 with ConstantExpression

use of io.trino.sql.relational.ConstantExpression in project trino by trinodb.

the class LambdaBytecodeGenerator method variableReferenceCompiler.

private static RowExpressionVisitor<BytecodeNode, Scope> variableReferenceCompiler(Map<String, ParameterAndType> parameterMap) {
    return new RowExpressionVisitor<>() {

        @Override
        public BytecodeNode visitInputReference(InputReferenceExpression node, Scope scope) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitCall(CallExpression call, Scope scope) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitSpecialForm(SpecialForm specialForm, Scope context) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitConstant(ConstantExpression literal, Scope scope) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitLambda(LambdaDefinitionExpression lambda, Scope context) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitVariableReference(VariableReferenceExpression reference, Scope context) {
            ParameterAndType parameterAndType = parameterMap.get(reference.getName());
            Parameter parameter = parameterAndType.getParameter();
            Class<?> type = parameterAndType.getType();
            return new BytecodeBlock().append(parameter).append(unboxPrimitiveIfNecessary(context, type));
        }
    };
}
Also used : InputReferenceExpression(io.trino.sql.relational.InputReferenceExpression) ConstantExpression(io.trino.sql.relational.ConstantExpression) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Scope(io.airlift.bytecode.Scope) VariableReferenceExpression(io.trino.sql.relational.VariableReferenceExpression) RowExpressionVisitor(io.trino.sql.relational.RowExpressionVisitor) Parameter(io.airlift.bytecode.Parameter) CallExpression(io.trino.sql.relational.CallExpression) SpecialForm(io.trino.sql.relational.SpecialForm) LambdaDefinitionExpression(io.trino.sql.relational.LambdaDefinitionExpression)

Example 5 with ConstantExpression

use of io.trino.sql.relational.ConstantExpression in project trino by trinodb.

the class TestExpressionOptimizer method testCastWithJsonParseOptimization.

@Test
public void testCastWithJsonParseOptimization() {
    ResolvedFunction jsonParseFunction = functionResolution.resolveFunction(QualifiedName.of("json_parse"), fromTypes(VARCHAR));
    // constant
    ResolvedFunction jsonCastFunction = functionResolution.getCoercion(JSON, new ArrayType(INTEGER));
    RowExpression jsonCastExpression = new CallExpression(jsonCastFunction, ImmutableList.of(call(jsonParseFunction, constant(utf8Slice("[1, 2]"), VARCHAR))));
    RowExpression resultExpression = optimizer.optimize(jsonCastExpression);
    assertInstanceOf(resultExpression, ConstantExpression.class);
    Object resultValue = ((ConstantExpression) resultExpression).getValue();
    assertInstanceOf(resultValue, IntArrayBlock.class);
    assertEquals(toValues(INTEGER, (IntArrayBlock) resultValue), ImmutableList.of(1, 2));
    // varchar to array
    testCastWithJsonParseOptimization(jsonParseFunction, new ArrayType(VARCHAR), JSON_STRING_TO_ARRAY_NAME);
    // varchar to map
    testCastWithJsonParseOptimization(jsonParseFunction, mapType(INTEGER, VARCHAR), JSON_STRING_TO_MAP_NAME);
    // varchar to row
    testCastWithJsonParseOptimization(jsonParseFunction, RowType.anonymous(ImmutableList.of(VARCHAR, BIGINT)), JSON_STRING_TO_ROW_NAME);
}
Also used : ArrayType(io.trino.spi.type.ArrayType) IntArrayBlock(io.trino.spi.block.IntArrayBlock) ResolvedFunction(io.trino.metadata.ResolvedFunction) ConstantExpression(io.trino.sql.relational.ConstantExpression) RowExpression(io.trino.sql.relational.RowExpression) CallExpression(io.trino.sql.relational.CallExpression) Test(org.testng.annotations.Test)

Aggregations

ConstantExpression (io.trino.sql.relational.ConstantExpression)7 Scope (io.airlift.bytecode.Scope)3 CallExpression (io.trino.sql.relational.CallExpression)3 InputReferenceExpression (io.trino.sql.relational.InputReferenceExpression)3 RowExpression (io.trino.sql.relational.RowExpression)3 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)2 Variable (io.airlift.bytecode.Variable)2 IfStatement (io.airlift.bytecode.control.IfStatement)2 Type (io.trino.spi.type.Type)2 LambdaDefinitionExpression (io.trino.sql.relational.LambdaDefinitionExpression)2 RowExpressionVisitor (io.trino.sql.relational.RowExpressionVisitor)2 SpecialForm (io.trino.sql.relational.SpecialForm)2 VariableReferenceExpression (io.trino.sql.relational.VariableReferenceExpression)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 BytecodeNode (io.airlift.bytecode.BytecodeNode)1 ClassDefinition (io.airlift.bytecode.ClassDefinition)1 Parameter (io.airlift.bytecode.Parameter)1