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 java.util.List;
 
 import static com.facebook.presto.byteCode.ParameterizedType.type;
 import static com.facebook.presto.byteCode.expression.ByteCodeExpressions.constantInt;
 import static com.facebook.presto.byteCode.expression.ByteCodeExpressions.lessThan;
 import static com.facebook.presto.byteCode.expression.ByteCodeExpressions.newInstance;
 import static com.facebook.presto.byteCode.instruction.VariableInstruction.incrementVariable;
 import static com.facebook.presto.sql.gen.SqlTypeByteCodeExpression.constantType;
 
         extends ByteCodeExpression
 {
     private static final AtomicLong NEXT_VARIABLE_ID = new AtomicLong();
 
     private final com.facebook.presto.byteCode.Block body;
     private final String oneLineDescription;
 
     public ArrayMapByteCodeExpression(
             Scope scope,
             CallSiteBinder binder,
             ByteCodeExpression arrayType fromType,
             Type toType,
             Function<ByteCodeExpressionByteCodeExpressionmapper)
     {
         super(type(Block.class));
 
          = new com.facebook.presto.byteCode.Block();
 
         Variable blockBuilder = scope.declareVariable(BlockBuilder.class"blockBuilder_" + .getAndIncrement());
         .append(blockBuilder.set(newInstance(VariableWidthBlockBuilder.classnewInstance(BlockBuilderStatus.class), array.invoke("getPositionCount"int.class))));
 
         Variable element = scope.declareVariable(fromType.getJavaType(), "element_" + .getAndIncrement());
         Variable newElement = scope.declareVariable(toType.getJavaType(), "newElement_" + .getAndIncrement());
         Variable position = scope.declareVariable(int.class"position_" + .getAndIncrement());
 
         // get element, apply function, and write new element to block builder
         SqlTypeByteCodeExpression elementTypeConstant = constantType(binderfromType);
         SqlTypeByteCodeExpression newElementTypeConstant = constantType(bindertoType);
         com.facebook.presto.byteCode.Block mapElement = new com.facebook.presto.byteCode.Block()
                 .append(element.set(elementTypeConstant.getValue(arrayposition)))
                 .append(newElement.set(mapper.apply(element)))
                 .append(newElementTypeConstant.writeValue(blockBuildernewElement));
 
         // main loop
         .append(new ForLoop()
                 .initialize(position.set(constantInt(0)))
                 .condition(lessThan(positionarray.invoke("getPositionCount"int.class)))
                 .update(incrementVariable(position, (byte) 1))
                 .body(new IfStatement()
                         .condition(array.invoke("isNull"boolean.classposition))
                         .ifTrue(blockBuilder.invoke("appendNull"BlockBuilder.class).pop())
                         .ifFalse(mapElement)));
 
         // build block
         .append(blockBuilder.invoke("build"Block.class));
 
         // pretty print
          = "arrayMap(" + array + ", element -> " + mapper.apply(element) + ")";
     }
 
     @Override
     public ByteCodeNode getByteCode(MethodGenerationContext generationContext)
     {
         return ;
     }
 
     @Override
     public List<ByteCodeNodegetChildNodes()
    {
        return ImmutableList.of();
    }
    @Override
    protected String formatOneLine()
    {
        return ;
    }
New to GrepCode? Check out our FAQ X