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.spi.type.BigintType.BIGINT;
 import static com.facebook.presto.spi.type.BooleanType.BOOLEAN;
 import static com.facebook.presto.spi.type.DateType.DATE;
 import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
 import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
 import static com.facebook.presto.sql.relational.Expressions.call;
 import static com.facebook.presto.sql.relational.Expressions.constant;
 import static com.facebook.presto.sql.relational.Expressions.field;
 import static com.google.common.base.Preconditions.checkState;
 import static java.nio.charset.StandardCharsets.UTF_8;
 
 @Fork(5)
 @Warmup(iterations = 10, time = 500, timeUnit = .)
 @Measurement(iterations = 10, time = 500, timeUnit = .)
 public class BenchmarkPageProcessor
 {
     private static final int EXTENDED_PRICE = 0;
     private static final int DISCOUNT = 1;
     private static final int SHIP_DATE = 2;
     private static final int QUANTITY = 3;
 
     private static final Slice MIN_SHIP_DATE = Slices.copiedBuffer("1994-01-01");
     private static final Slice MAX_SHIP_DATE = Slices.copiedBuffer("1995-01-01");
 
     private Page inputPage;
     private PageProcessor handCodedProcessor;
     private PageProcessor compiledProcessor;
 
     @Setup
     public void setup()
     {
          = createInputPage();
 
          = new Tpch1FilterAndProject();
 
          = new ExpressionCompiler(new MetadataManager()).compilePageProcessor(, ImmutableList.of());
     }
 
     @Benchmark
     public Page handCoded()
     {
         return execute();
     }
 
     @Benchmark
     public Page compiled()
     {
         return execute();
     }
 
    public static void main(String[] args)
            throws RunnerException
    {
        Options options = new OptionsBuilder()
                .verbosity(.)
                .include(".*" + BenchmarkPageProcessor.class.getSimpleName() + ".*")
                .build();
        new Runner(options).run();
    }
    public static Page execute(Page inputPagePageProcessor processor)
    {
        PageBuilder pageBuilder = new PageBuilder(ImmutableList.of());
        int count = processor.process(nullinputPage, 0, inputPage.getPositionCount(), pageBuilder);
        checkState(count == inputPage.getPositionCount());
        return pageBuilder.build();
    }
    public static Page createInputPage()
    {
        PageBuilder pageBuilder = new PageBuilder(ImmutableList.of());
        LineItemGenerator lineItemGenerator = new LineItemGenerator(1, 1, 1);
        Iterator<LineItemiterator = lineItemGenerator.iterator();
        for (int i = 0; i < 10_000; i++) {
            pageBuilder.declarePosition();
            LineItem lineItem = iterator.next();
            .writeDouble(pageBuilder.getBlockBuilder(), lineItem.getExtendedPrice());
            .writeDouble(pageBuilder.getBlockBuilder(), lineItem.getDiscount());
            .writeLong(pageBuilder.getBlockBuilder(), lineItem.getShipDate());
            .writeLong(pageBuilder.getBlockBuilder(), lineItem.getQuantity());
        }
        return pageBuilder.build();
    }
    private static final class Tpch1FilterAndProject
            implements PageProcessor
    {
        @Override
        public int process(ConnectorSession sessionPage pageint startint endPageBuilder pageBuilder)
        {
            Block discountBlock = page.getBlock();
            int position = start;
            for (; position < endposition++) {
                // where shipdate >= '1994-01-01'
                //    and shipdate < '1995-01-01'
                //    and discount >= 0.05
                //    and discount <= 0.07
                //    and quantity < 24;
                if (filter(positiondiscountBlockpage.getBlock(), page.getBlock())) {
                    project(positionpageBuilderpage.getBlock(), discountBlock);
                }
            }
            return position;
        }
        private static void project(int positionPageBuilder pageBuilderBlock extendedPriceBlockBlock discountBlock)
        {
            pageBuilder.declarePosition();
            if (discountBlock.isNull(position) || extendedPriceBlock.isNull(position)) {
                pageBuilder.getBlockBuilder(0).appendNull();
            }
            else {
                .writeDouble(pageBuilder.getBlockBuilder(0), .getDouble(extendedPriceBlockposition) * .getDouble(discountBlockposition));
            }
        }
        private static boolean filter(int positionBlock discountBlockBlock shipDateBlockBlock quantityBlock)
        {
            return !shipDateBlock.isNull(position) && .getSlice(shipDateBlockposition).compareTo() >= 0 &&
                    !shipDateBlock.isNull(position) && .getSlice(shipDateBlockposition).compareTo() < 0 &&
                    !discountBlock.isNull(position) && .getDouble(discountBlockposition) >= 0.05 &&
                    !discountBlock.isNull(position) && .getDouble(discountBlockposition) <= 0.07 &&
                    !quantityBlock.isNull(position) && .getLong(quantityBlockposition) < 24;
        }
    }
    // where shipdate >= '1994-01-01'
    //    and shipdate < '1995-01-01'
    //    and discount >= 0.05
    //    and discount <= 0.07
    //    and quantity < 24;
    private static final RowExpression FILTER = call(new Signature("AND".),
            ,
                    ,
                    field(),
                    constant()),
            call(new Signature("AND".),
                    ,
                            ,
                            field(),
                            constant()),
                    call(new Signature("AND".),
                            ,
                            call(new Signature(..name(), ...),
                                    ,
                                    field(),
                                    constant(0.05, )),
                            call(new Signature("AND".),
                                    ,
                                    call(new Signature(..name(), ...),
                                            ,
                                            field(),
                                            constant(0.07, )),
                                    call(new Signature(..name(), ...),
                                            ,
                                            field(),
                                            constant((long) 24, ))))));
    private static final RowExpression PROJECT = call(
            ,
            field(),
            field());
New to GrepCode? Check out our FAQ X