Search in sources :

Example 1 with EQUAL

use of io.trino.spi.function.OperatorType.EQUAL in project trino by trinodb.

the class NullIfCodeGenerator method generateExpression.

@Override
public BytecodeNode generateExpression(BytecodeGeneratorContext generatorContext) {
    Scope scope = generatorContext.getScope();
    LabelNode notMatch = new LabelNode("notMatch");
    // push first arg on the stack
    Variable firstValue = scope.createTempVariable(first.getType().getJavaType());
    BytecodeBlock block = new BytecodeBlock().comment("check if first arg is null").append(generatorContext.generate(first)).append(ifWasNullPopAndGoto(scope, notMatch, void.class)).dup(first.getType().getJavaType()).putVariable(firstValue);
    BytecodeNode secondValue = generatorContext.generate(second);
    // if (equal(cast(first as <common type>), cast(second as <common type>))
    BytecodeNode equalsCall = generatorContext.generateCall(equalsFunction, ImmutableList.of(firstCast.map(cast -> generatorContext.generateCall(cast, ImmutableList.of(firstValue))).orElse(firstValue), secondCast.map(cast -> generatorContext.generateCall(cast, ImmutableList.of(secondValue))).orElse(secondValue)));
    BytecodeBlock conditionBlock = new BytecodeBlock().append(equalsCall).append(BytecodeUtils.ifWasNullClearPopAndGoto(scope, notMatch, void.class, boolean.class));
    // if first and second are equal, return null
    BytecodeBlock trueBlock = new BytecodeBlock().append(generatorContext.wasNull().set(constantTrue())).pop(first.getType().getJavaType()).pushJavaDefault(first.getType().getJavaType());
    // else return first (which is still on the stack
    block.append(new IfStatement().condition(conditionBlock).ifTrue(trueBlock).ifFalse(notMatch));
    return block;
}
Also used : LabelNode(io.airlift.bytecode.instruction.LabelNode) Variable(io.airlift.bytecode.Variable) ResolvedFunction(io.trino.metadata.ResolvedFunction) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) BytecodeNode(io.airlift.bytecode.BytecodeNode) Scope(io.airlift.bytecode.Scope) IfStatement(io.airlift.bytecode.control.IfStatement) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) RowExpression(io.trino.sql.relational.RowExpression) BytecodeExpressions.constantTrue(io.airlift.bytecode.expression.BytecodeExpressions.constantTrue) LabelNode(io.airlift.bytecode.instruction.LabelNode) BytecodeUtils.ifWasNullPopAndGoto(io.trino.sql.gen.BytecodeUtils.ifWasNullPopAndGoto) Objects.requireNonNull(java.util.Objects.requireNonNull) Optional(java.util.Optional) EQUAL(io.trino.spi.function.OperatorType.EQUAL) SpecialForm(io.trino.sql.relational.SpecialForm) IfStatement(io.airlift.bytecode.control.IfStatement) Variable(io.airlift.bytecode.Variable) Scope(io.airlift.bytecode.Scope) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) BytecodeNode(io.airlift.bytecode.BytecodeNode)

Aggregations

Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 ImmutableList (com.google.common.collect.ImmutableList)1 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)1 BytecodeNode (io.airlift.bytecode.BytecodeNode)1 Scope (io.airlift.bytecode.Scope)1 Variable (io.airlift.bytecode.Variable)1 IfStatement (io.airlift.bytecode.control.IfStatement)1 BytecodeExpressions.constantTrue (io.airlift.bytecode.expression.BytecodeExpressions.constantTrue)1 LabelNode (io.airlift.bytecode.instruction.LabelNode)1 ResolvedFunction (io.trino.metadata.ResolvedFunction)1 EQUAL (io.trino.spi.function.OperatorType.EQUAL)1 BytecodeUtils.ifWasNullPopAndGoto (io.trino.sql.gen.BytecodeUtils.ifWasNullPopAndGoto)1 RowExpression (io.trino.sql.relational.RowExpression)1 SpecialForm (io.trino.sql.relational.SpecialForm)1 List (java.util.List)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1