Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.sql.gen;
import static com.facebook.presto.byteCode.expression.ByteCodeExpressions.constantTrue;
public class NullIfCodeGenerator
        implements ByteCodeGenerator
    @Override
    public ByteCodeNode generateExpression(Signature signatureByteCodeGeneratorContext generatorContextType returnTypeList<RowExpressionarguments)
    {
        Scope scope = generatorContext.getScope();
        RowExpression first = arguments.get(0);
        RowExpression second = arguments.get(1);
        LabelNode notMatch = new LabelNode("notMatch");
        // push first arg on the stack
        Block block = new Block()
                .comment("check if first arg is null")
                .append(generatorContext.generate(first))
                .append(ByteCodeUtils.ifWasNullPopAndGoto(scopenotMatchvoid.class));
        Type firstType = first.getType();
        Type secondType = second.getType();
        // this is a hack! We shouldn't be determining type coercions at this point, but there's no way
        // around it in the current expression AST
        Type commonType = FunctionRegistry.getCommonSuperType(firstTypesecondType).get();
        // if (equal(cast(first as <common type>), cast(second as <common type>))
        FunctionInfo equalsFunction = generatorContext.getRegistry().resolveOperator(., ImmutableList.of(firstTypesecondType));
        ByteCodeNode equalsCall = generatorContext.generateCall(
                equalsFunction,
                ImmutableList.of(
                        cast(generatorContextnew Block().dup(firstType.getJavaType()), firstTypecommonType),
                        cast(generatorContextgeneratorContext.generate(second), secondTypecommonType)));
        Block conditionBlock = new Block()
                .append(equalsCall)
                .append(ByteCodeUtils.ifWasNullClearPopAndGoto(scopenotMatchvoid.classboolean.class));
        // if first and second are equal, return null
        Block trueBlock = new Block()
                .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;
    }
    private ByteCodeNode cast(ByteCodeGeneratorContext generatorContextByteCodeNode argumentType fromTypeType toType)
    {
        FunctionInfo function = generatorContext
            .getRegistry()
            .getCoercion(fromTypetoType);
        // TODO: do we need a full function call? (nullability checks, etc)
        return generatorContext.generateCall(function, ImmutableList.of(argument));
    }
New to GrepCode? Check out our FAQ X