Search in sources :

Example 6 with BlockPositionEqual

use of io.trino.type.BlockTypeOperators.BlockPositionEqual in project trino by trinodb.

the class MapConcatFunction method specialize.

@Override
protected ScalarFunctionImplementation specialize(BoundSignature boundSignature) {
    if (boundSignature.getArity() < 2) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "There must be two or more concatenation arguments to " + FUNCTION_NAME);
    }
    MapType mapType = (MapType) boundSignature.getReturnType();
    Type keyType = mapType.getKeyType();
    BlockPositionEqual keyEqual = blockTypeOperators.getEqualOperator(keyType);
    BlockPositionHashCode keyHashCode = blockTypeOperators.getHashCodeOperator(keyType);
    MethodHandleAndConstructor methodHandleAndConstructor = generateVarArgsToArrayAdapter(Block.class, Block.class, boundSignature.getArity(), MethodHandles.insertArguments(METHOD_HANDLE, 0, mapType, keyEqual, keyHashCode), USER_STATE_FACTORY.bindTo(mapType));
    return new ChoicesScalarFunctionImplementation(boundSignature, FAIL_ON_NULL, nCopies(boundSignature.getArity(), NEVER_NULL), methodHandleAndConstructor.getMethodHandle(), Optional.of(methodHandleAndConstructor.getConstructor()));
}
Also used : BlockPositionEqual(io.trino.type.BlockTypeOperators.BlockPositionEqual) Type(io.trino.spi.type.Type) MapType(io.trino.spi.type.MapType) TypeSignature.mapType(io.trino.spi.type.TypeSignature.mapType) MethodHandleAndConstructor(io.trino.sql.gen.VarArgsToArrayAdapterGenerator.MethodHandleAndConstructor) TrinoException(io.trino.spi.TrinoException) BlockPositionHashCode(io.trino.type.BlockTypeOperators.BlockPositionHashCode) MapType(io.trino.spi.type.MapType)

Example 7 with BlockPositionEqual

use of io.trino.type.BlockTypeOperators.BlockPositionEqual in project trino by trinodb.

the class SimplePagesHashStrategy method positionEqualsRowIgnoreNulls.

@Override
public boolean positionEqualsRowIgnoreNulls(int leftBlockIndex, int leftPosition, int rightPosition, Page rightPage) {
    for (int i = 0; i < hashChannels.size(); i++) {
        BlockPositionEqual equalOperator = equalOperators.get(i);
        Block leftBlock = channels.get(hashChannels.get(i)).get(leftBlockIndex);
        Block rightBlock = rightPage.getBlock(i);
        if (!equalOperator.equal(leftBlock, leftPosition, rightBlock, rightPosition)) {
            return false;
        }
    }
    return true;
}
Also used : BlockPositionEqual(io.trino.type.BlockTypeOperators.BlockPositionEqual) Block(io.trino.spi.block.Block)

Example 8 with BlockPositionEqual

use of io.trino.type.BlockTypeOperators.BlockPositionEqual in project trino by trinodb.

the class Histogram method specialize.

@Override
public AggregationMetadata specialize(BoundSignature boundSignature) {
    Type keyType = boundSignature.getArgumentTypes().get(0);
    BlockPositionEqual keyEqual = blockTypeOperators.getEqualOperator(keyType);
    BlockPositionHashCode keyHashCode = blockTypeOperators.getHashCodeOperator(keyType);
    Type outputType = boundSignature.getReturnType();
    HistogramStateSerializer stateSerializer = new HistogramStateSerializer(outputType);
    MethodHandle inputFunction = INPUT_FUNCTION.bindTo(keyType);
    MethodHandle outputFunction = OUTPUT_FUNCTION.bindTo(outputType);
    return new AggregationMetadata(inputFunction, Optional.empty(), Optional.of(COMBINE_FUNCTION), outputFunction, ImmutableList.of(new AccumulatorStateDescriptor<>(HistogramState.class, stateSerializer, new HistogramStateFactory(keyType, keyEqual, keyHashCode, EXPECTED_SIZE_FOR_HASHING))));
}
Also used : BlockPositionEqual(io.trino.type.BlockTypeOperators.BlockPositionEqual) Type(io.trino.spi.type.Type) TypeSignature.mapType(io.trino.spi.type.TypeSignature.mapType) AccumulatorStateDescriptor(io.trino.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) AggregationMetadata(io.trino.operator.aggregation.AggregationMetadata) BlockPositionHashCode(io.trino.type.BlockTypeOperators.BlockPositionHashCode) MethodHandle(java.lang.invoke.MethodHandle)

Example 9 with BlockPositionEqual

use of io.trino.type.BlockTypeOperators.BlockPositionEqual in project trino by trinodb.

the class MultimapAggregationFunction method specialize.

@Override
public AggregationMetadata specialize(BoundSignature boundSignature) {
    Type keyType = boundSignature.getArgumentType(0);
    BlockPositionEqual keyEqual = blockTypeOperators.getEqualOperator(keyType);
    BlockPositionHashCode keyHashCode = blockTypeOperators.getHashCodeOperator(keyType);
    Type valueType = boundSignature.getArgumentType(1);
    MultimapAggregationStateSerializer stateSerializer = new MultimapAggregationStateSerializer(keyType, valueType);
    return new AggregationMetadata(INPUT_FUNCTION, Optional.empty(), Optional.of(COMBINE_FUNCTION), MethodHandles.insertArguments(OUTPUT_FUNCTION, 0, keyType, keyEqual, keyHashCode, valueType), ImmutableList.of(new AccumulatorStateDescriptor<>(MultimapAggregationState.class, stateSerializer, new MultimapAggregationStateFactory(keyType, valueType))));
}
Also used : BlockPositionEqual(io.trino.type.BlockTypeOperators.BlockPositionEqual) Type(io.trino.spi.type.Type) TypeSignature.arrayType(io.trino.spi.type.TypeSignature.arrayType) ArrayType(io.trino.spi.type.ArrayType) TypeSignature.rowType(io.trino.spi.type.TypeSignature.rowType) TypeSignature.mapType(io.trino.spi.type.TypeSignature.mapType) AccumulatorStateDescriptor(io.trino.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) AggregationMetadata(io.trino.operator.aggregation.AggregationMetadata) BlockPositionHashCode(io.trino.type.BlockTypeOperators.BlockPositionHashCode)

Example 10 with BlockPositionEqual

use of io.trino.type.BlockTypeOperators.BlockPositionEqual in project trino by trinodb.

the class TestJoinCompiler method testSingleChannel.

@Test(dataProvider = "hashEnabledValues")
public void testSingleChannel(boolean hashEnabled) {
    List<Type> joinTypes = ImmutableList.of(VARCHAR);
    List<Integer> joinChannels = Ints.asList(0);
    // compile a single channel hash strategy
    PagesHashStrategyFactory pagesHashStrategyFactory = joinCompiler.compilePagesHashStrategyFactory(joinTypes, joinChannels);
    // create hash strategy with a single channel blocks -- make sure there is some overlap in values
    List<Block> channel = ImmutableList.of(BlockAssertions.createStringSequenceBlock(10, 20), BlockAssertions.createStringSequenceBlock(20, 30), BlockAssertions.createStringSequenceBlock(15, 25));
    OptionalInt hashChannel = OptionalInt.empty();
    List<List<Block>> channels = ImmutableList.of(channel);
    if (hashEnabled) {
        ImmutableList.Builder<Block> hashChannelBuilder = ImmutableList.builder();
        for (Block block : channel) {
            hashChannelBuilder.add(TypeTestUtils.getHashBlock(joinTypes, block));
        }
        hashChannel = OptionalInt.of(1);
        channels = ImmutableList.of(channel, hashChannelBuilder.build());
    }
    PagesHashStrategy hashStrategy = pagesHashStrategyFactory.createPagesHashStrategy(channels, hashChannel);
    // verify channel count
    assertEquals(hashStrategy.getChannelCount(), 1);
    BlockTypeOperators blockTypeOperators = new BlockTypeOperators();
    BlockPositionEqual equalOperator = blockTypeOperators.getEqualOperator(VARCHAR);
    BlockPositionIsDistinctFrom distinctFromOperator = blockTypeOperators.getDistinctFromOperator(VARCHAR);
    BlockPositionHashCode hashCodeOperator = blockTypeOperators.getHashCodeOperator(VARCHAR);
    // verify hashStrategy is consistent with equals and hash code from block
    for (int leftBlockIndex = 0; leftBlockIndex < channel.size(); leftBlockIndex++) {
        Block leftBlock = channel.get(leftBlockIndex);
        PageBuilder pageBuilder = new PageBuilder(ImmutableList.of(VARCHAR));
        for (int leftBlockPosition = 0; leftBlockPosition < leftBlock.getPositionCount(); leftBlockPosition++) {
            // hash code of position must match block hash
            assertEquals(hashStrategy.hashPosition(leftBlockIndex, leftBlockPosition), hashCodeOperator.hashCodeNullSafe(leftBlock, leftBlockPosition));
            // position must be equal to itself
            assertTrue(hashStrategy.positionEqualsPositionIgnoreNulls(leftBlockIndex, leftBlockPosition, leftBlockIndex, leftBlockPosition));
            // check equality of every position against every other position in the block
            for (int rightBlockIndex = 0; rightBlockIndex < channel.size(); rightBlockIndex++) {
                Block rightBlock = channel.get(rightBlockIndex);
                for (int rightBlockPosition = 0; rightBlockPosition < rightBlock.getPositionCount(); rightBlockPosition++) {
                    boolean expected = equalOperator.equalNullSafe(leftBlock, leftBlockPosition, rightBlock, rightBlockPosition);
                    boolean expectedNotDistinct = !distinctFromOperator.isDistinctFrom(leftBlock, leftBlockPosition, rightBlock, rightBlockPosition);
                    assertEquals(hashStrategy.positionEqualsRow(leftBlockIndex, leftBlockPosition, rightBlockPosition, new Page(rightBlock)), expected);
                    assertEquals(hashStrategy.positionNotDistinctFromRow(leftBlockIndex, leftBlockPosition, rightBlockPosition, new Page(rightBlock)), expectedNotDistinct);
                    assertEquals(hashStrategy.rowEqualsRow(leftBlockPosition, new Page(leftBlock), rightBlockPosition, new Page(rightBlock)), expected);
                    assertEquals(hashStrategy.rowNotDistinctFromRow(leftBlockPosition, new Page(leftBlock), rightBlockPosition, new Page(rightBlock)), expectedNotDistinct);
                    assertEquals(hashStrategy.positionEqualsRowIgnoreNulls(leftBlockIndex, leftBlockPosition, rightBlockPosition, new Page(rightBlock)), expected);
                    assertEquals(hashStrategy.positionEqualsPositionIgnoreNulls(leftBlockIndex, leftBlockPosition, rightBlockIndex, rightBlockPosition), expected);
                    assertEquals(hashStrategy.positionEqualsPosition(leftBlockIndex, leftBlockPosition, rightBlockIndex, rightBlockPosition), expected);
                    assertEquals(hashStrategy.positionNotDistinctFromPosition(leftBlockIndex, leftBlockPosition, rightBlockIndex, rightBlockPosition), expectedNotDistinct);
                }
            }
            // check equality of every position against every other position in the block cursor
            for (int rightBlockIndex = 0; rightBlockIndex < channel.size(); rightBlockIndex++) {
                Block rightBlock = channel.get(rightBlockIndex);
                for (int rightBlockPosition = 0; rightBlockPosition < rightBlock.getPositionCount(); rightBlockPosition++) {
                    boolean expected = equalOperator.equalNullSafe(leftBlock, leftBlockPosition, rightBlock, rightBlockPosition);
                    boolean expectedNotDistinct = !distinctFromOperator.isDistinctFrom(leftBlock, leftBlockPosition, rightBlock, rightBlockPosition);
                    assertEquals(hashStrategy.positionEqualsRow(leftBlockIndex, leftBlockPosition, rightBlockPosition, new Page(rightBlock)), expected);
                    assertEquals(hashStrategy.positionNotDistinctFromRow(leftBlockIndex, leftBlockPosition, rightBlockPosition, new Page(rightBlock)), expectedNotDistinct);
                    assertEquals(hashStrategy.rowEqualsRow(leftBlockPosition, new Page(leftBlock), rightBlockPosition, new Page(rightBlock)), expected);
                    assertEquals(hashStrategy.rowNotDistinctFromRow(leftBlockPosition, new Page(leftBlock), rightBlockPosition, new Page(rightBlock)), expectedNotDistinct);
                    assertEquals(hashStrategy.positionEqualsRowIgnoreNulls(leftBlockIndex, leftBlockPosition, rightBlockPosition, new Page(rightBlock)), expected);
                    assertEquals(hashStrategy.positionEqualsPositionIgnoreNulls(leftBlockIndex, leftBlockPosition, rightBlockIndex, rightBlockPosition), expected);
                    assertEquals(hashStrategy.positionEqualsPosition(leftBlockIndex, leftBlockPosition, rightBlockIndex, rightBlockPosition), expected);
                    assertEquals(hashStrategy.positionNotDistinctFromPosition(leftBlockIndex, leftBlockPosition, rightBlockIndex, rightBlockPosition), expectedNotDistinct);
                }
            }
            // write position to output block
            pageBuilder.declarePosition();
            hashStrategy.appendTo(leftBlockIndex, leftBlockPosition, pageBuilder, 0);
        }
        // verify output block matches
        assertBlockEquals(VARCHAR, pageBuilder.build().getBlock(0), leftBlock);
    }
}
Also used : PagesHashStrategyFactory(io.trino.sql.gen.JoinCompiler.PagesHashStrategyFactory) ImmutableList(com.google.common.collect.ImmutableList) Page(io.trino.spi.Page) OptionalInt(java.util.OptionalInt) PageBuilder(io.trino.spi.PageBuilder) BlockPositionEqual(io.trino.type.BlockTypeOperators.BlockPositionEqual) Type(io.trino.spi.type.Type) PagesHashStrategy(io.trino.operator.PagesHashStrategy) SimplePagesHashStrategy(io.trino.operator.SimplePagesHashStrategy) BlockTypeOperators(io.trino.type.BlockTypeOperators) BlockPositionIsDistinctFrom(io.trino.type.BlockTypeOperators.BlockPositionIsDistinctFrom) Block(io.trino.spi.block.Block) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BlockPositionHashCode(io.trino.type.BlockTypeOperators.BlockPositionHashCode) Test(org.testng.annotations.Test)

Aggregations

BlockPositionEqual (io.trino.type.BlockTypeOperators.BlockPositionEqual)11 Type (io.trino.spi.type.Type)7 BlockPositionHashCode (io.trino.type.BlockTypeOperators.BlockPositionHashCode)7 TypeSignature.mapType (io.trino.spi.type.TypeSignature.mapType)6 AccumulatorStateDescriptor (io.trino.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor)4 MapType (io.trino.spi.type.MapType)4 Block (io.trino.spi.block.Block)3 MethodHandle (java.lang.invoke.MethodHandle)3 AggregationMetadata (io.trino.operator.aggregation.AggregationMetadata)2 KeyValuePairStateSerializer (io.trino.operator.aggregation.state.KeyValuePairStateSerializer)2 KeyValuePairsStateFactory (io.trino.operator.aggregation.state.KeyValuePairsStateFactory)2 BlockTypeOperators (io.trino.type.BlockTypeOperators)2 ImmutableList (com.google.common.collect.ImmutableList)1 PagesHashStrategy (io.trino.operator.PagesHashStrategy)1 SimplePagesHashStrategy (io.trino.operator.SimplePagesHashStrategy)1 Page (io.trino.spi.Page)1 PageBuilder (io.trino.spi.PageBuilder)1 TrinoException (io.trino.spi.TrinoException)1 BlockBuilder (io.trino.spi.block.BlockBuilder)1 ArrayType (io.trino.spi.type.ArrayType)1