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.operator.aggregation;
 
 
 import java.util.Set;
 
 import static com.facebook.presto.block.BlockAssertions.createDoublesBlock;
 import static com.facebook.presto.block.BlockAssertions.createStringsBlock;
 import static com.facebook.presto.operator.aggregation.AggregationTestUtils.assertAggregation;
 import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
 import static com.facebook.presto.spi.type.TypeSignature.parseTypeSignature;
 import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
 import static com.facebook.presto.util.ImmutableCollectors.toImmutableSet;
 import static io.airlift.slice.SizeOf.SIZE_OF_DOUBLE;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
 
 public class TestMaxByAggregation
 {
     private static final MetadataManager metadata = MetadataManager.createTestMetadataManager();
 
     @BeforeClass
     public void setup()
     {
     }
 
     @Test
     public void testAllRegistered()
     {
         Set<TypeorderableTypes = .getTypeManager()
                 .getTypes().stream()
                 .filter(Type::isOrderable)
                 .collect(toImmutableSet());
 
         for (Type keyType : orderableTypes) {
             for (Type valueType : .getTypeManager().getTypes()) {
                 assertNotNull(.getExactFunction(new Signature("max_by"valueType.getTypeSignature(), valueType.getTypeSignature(), keyType.getTypeSignature())));
             }
         }
     }
 
     @Test
     public void testNull()
             throws Exception
     {
         assertAggregation(
                 doubleDouble,
                 1.0,
                 null,
                 createPage(
                         new Double[] {1.0, null},
                         new Double[] {1.0, 2.0}));
     }
 
     @Test
     public void testDoubleDouble()
             throws Exception
     {
         assertAggregation(
                 doubleDouble,
                 1.0,
                 null,
                 createPage(
                         new Double[] {null},
                         new Double[] {null}),
                 createPage(
                        new Double[] {null},
                        new Double[] {null}));
        assertAggregation(
                doubleDouble,
                1.0,
                2.0,
                createPage(
                        new Double[] {3.0, 2.0},
                        new Double[] {1.0, 1.5}),
                createPage(
                        new Double[] {null},
                        new Double[] {null}));
    }
    @Test
    public void testDoubleVarchar()
            throws Exception
    {
        assertAggregation(
                doubleVarchar,
                1.0,
                "a",
                createPage(
                        new String[] {"z""a"},
                        new Double[] {1.0, 2.0}),
                createPage(
                        new String[] {null},
                        new Double[] {null}));
        assertAggregation(
                doubleVarchar,
                1.0,
                "hi",
                createPage(
                        new String[] {"zz""hi"},
                        new Double[] {0.0, 1.0}),
                createPage(
                        new String[] {null"a"},
                        new Double[] {null, -1.0}));
    }
    @Test
    public void testStateDeserializer()
            throws Exception
    {
        String[] keys = new String[] {"loooooong string""short string"};
        double[] values = new double[] { 3.14, 2.71 };
        MaxOrMinByStateSerializer serializer = new MaxOrMinByStateSerializer();
        BlockBuilder builder = new VariableWidthBlockBuilder(new BlockBuilderStatus());
        for (int i = 0; i < keys.lengthi++) {
            serializer.serialize(makeState(keys[i], values[i]), builder);
        }
        Block serialized = builder.build();
        for (int i = 0; i < keys.lengthi++) {
            MaxOrMinByState deserialized = new MaxOrMinByStateFactory().createSingleState();
            serializer.deserialize(serializedideserialized);
            assertEquals(.getSlice(deserialized.getKey(), 0), Slices.utf8Slice(keys[i]));
            assertEquals(.getDouble(deserialized.getValue(), 0), values[i]);
        }
    }
    private static MaxOrMinByState makeState(String keydouble value)
    {
        MaxOrMinByState result = new MaxOrMinByStateFactory().createSingleState();
        result.setKey(createStringsBlock(key));
        result.setValue(createDoublesBlock(value));
        return result;
    }
    private static Page createPage(Double[] valuesDouble[] keys)
    {
        return new Page(createDoublesBlock(values), createDoublesBlock(keys));
    }
    private static Page createPage(String[] valuesDouble[] keys)
    {
        return new Page(createStringsBlock(values), createDoublesBlock(keys));
    }
    private static class CustomDoubleType
            extends AbstractFixedWidthType
    {
        public static final CustomDoubleType CUSTOM_DOUBLE = new CustomDoubleType();
        public static final String NAME = "custom_double";
        private CustomDoubleType()
        {
            super(parseTypeSignature(), double.class);
        }
        @Override
        public boolean isComparable()
        {
            return true;
        }
        @Override
        public boolean isOrderable()
        {
            return true;
        }
        @Override
        public Object getObjectValue(ConnectorSession sessionBlock blockint position)
        {
            if (block.isNull(position)) {
                return null;
            }
            return block.getDouble(position, 0);
        }
        @Override
        public boolean equalTo(Block leftBlockint leftPositionBlock rightBlockint rightPosition)
        {
            long leftValue = leftBlock.getLong(leftPosition, 0);
            long rightValue = rightBlock.getLong(rightPosition, 0);
            return leftValue == rightValue;
        }
        @Override
        public int hash(Block blockint position)
        {
            long value = block.getLong(position, 0);
            return (int) (value ^ (value >>> 32));
        }
        @Override
        public int compareTo(Block leftBlockint leftPositionBlock rightBlockint rightPosition)
        {
            double leftValue = leftBlock.getDouble(leftPosition, 0);
            double rightValue = rightBlock.getDouble(rightPosition, 0);
            return Double.compare(leftValuerightValue);
        }
        @Override
        public void appendTo(Block blockint positionBlockBuilder blockBuilder)
        {
            if (block.isNull(position)) {
                blockBuilder.appendNull();
            }
            else {
                blockBuilder.writeDouble(block.getDouble(position, 0)).closeEntry();
            }
        }
        @Override
        public double getDouble(Block blockint position)
        {
            return block.getDouble(position, 0);
        }
        @Override
        public void writeDouble(BlockBuilder blockBuilderdouble value)
        {
            blockBuilder.writeDouble(value).closeEntry();
        }
    }
New to GrepCode? Check out our FAQ X