Search in sources :

Example 6 with IntArrayBlock

use of com.facebook.presto.common.block.IntArrayBlock in project presto by prestodb.

the class TestRowExpressionOptimizer method testCastWithJsonParseOptimization.

@Test
public void testCastWithJsonParseOptimization() {
    FunctionHandle jsonParseFunctionHandle = functionAndTypeManager.lookupFunction("json_parse", fromTypes(VARCHAR));
    // constant
    FunctionHandle jsonCastFunctionHandle = functionAndTypeManager.lookupCast(CAST, JSON.getTypeSignature(), parseTypeSignature("array(integer)"));
    RowExpression jsonCastExpression = new CallExpression(CAST.name(), jsonCastFunctionHandle, new ArrayType(INTEGER), ImmutableList.of(call("json_parse", jsonParseFunctionHandle, JSON, constant(utf8Slice("[1, 2]"), VARCHAR))));
    RowExpression resultExpression = optimize(jsonCastExpression);
    assertInstanceOf(resultExpression, ConstantExpression.class);
    Object resultValue = ((ConstantExpression) resultExpression).getValue();
    assertInstanceOf(resultValue, IntArrayBlock.class);
    assertEquals(toValues(INTEGER, (IntArrayBlock) resultValue), ImmutableList.of(1, 2));
    // varchar to array
    jsonCastFunctionHandle = functionAndTypeManager.lookupCast(CAST, JSON.getTypeSignature(), parseTypeSignature("array(varchar)"));
    jsonCastExpression = call(CAST.name(), jsonCastFunctionHandle, new ArrayType(VARCHAR), ImmutableList.of(call("json_parse", jsonParseFunctionHandle, JSON, field(1, VARCHAR))));
    resultExpression = optimize(jsonCastExpression);
    assertEquals(resultExpression, call(JSON_TO_ARRAY_CAST.name(), functionAndTypeManager.lookupCast(JSON_TO_ARRAY_CAST, VARCHAR.getTypeSignature(), parseTypeSignature("array(varchar)")), new ArrayType(VARCHAR), field(1, VARCHAR)));
    // varchar to map
    jsonCastFunctionHandle = functionAndTypeManager.lookupCast(CAST, JSON.getTypeSignature(), parseTypeSignature("map(integer,varchar)"));
    jsonCastExpression = call(CAST.name(), jsonCastFunctionHandle, mapType(INTEGER, VARCHAR), ImmutableList.of(call("json_parse", jsonParseFunctionHandle, JSON, field(1, VARCHAR))));
    resultExpression = optimize(jsonCastExpression);
    assertEquals(resultExpression, call(JSON_TO_MAP_CAST.name(), functionAndTypeManager.lookupCast(JSON_TO_MAP_CAST, VARCHAR.getTypeSignature(), parseTypeSignature("map(integer, varchar)")), mapType(INTEGER, VARCHAR), field(1, VARCHAR)));
    // varchar to row
    jsonCastFunctionHandle = functionAndTypeManager.lookupCast(CAST, JSON.getTypeSignature(), parseTypeSignature("row(varchar,bigint)"));
    jsonCastExpression = call(CAST.name(), jsonCastFunctionHandle, RowType.anonymous(ImmutableList.of(VARCHAR, BIGINT)), ImmutableList.of(call("json_parse", jsonParseFunctionHandle, JSON, field(1, VARCHAR))));
    resultExpression = optimize(jsonCastExpression);
    assertEquals(resultExpression, call(JSON_TO_ROW_CAST.name(), functionAndTypeManager.lookupCast(JSON_TO_ROW_CAST, VARCHAR.getTypeSignature(), parseTypeSignature("row(varchar,bigint)")), RowType.anonymous(ImmutableList.of(VARCHAR, BIGINT)), field(1, VARCHAR)));
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) IntArrayBlock(com.facebook.presto.common.block.IntArrayBlock) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) CallExpression(com.facebook.presto.spi.relation.CallExpression) Test(org.testng.annotations.Test)

Example 7 with IntArrayBlock

use of com.facebook.presto.common.block.IntArrayBlock in project presto by prestodb.

the class Int32NestedBatchReader method readNestedWithNull.

@Override
protected ColumnChunk readNestedWithNull() throws IOException {
    int maxDefinitionLevel = columnDescriptor.getMaxDefinitionLevel();
    RepetitionLevelDecodingContext repetitionLevelDecodingContext = readRepetitionLevels(nextBatchSize);
    DefinitionLevelDecodingContext definitionLevelDecodingContext = readDefinitionLevels(repetitionLevelDecodingContext.getDLValuesDecoderContexts(), repetitionLevelDecodingContext.getRepetitionLevels().length);
    int[] definitionLevels = definitionLevelDecodingContext.getDefinitionLevels();
    int newBatchSize = 0;
    int batchNonNullCount = 0;
    for (ValuesDecoderContext valuesDecoderContext : definitionLevelDecodingContext.getValuesDecoderContexts()) {
        int nonNullCount = 0;
        int valueCount = 0;
        for (int i = valuesDecoderContext.getStart(); i < valuesDecoderContext.getEnd(); i++) {
            nonNullCount += (definitionLevels[i] == maxDefinitionLevel ? 1 : 0);
            valueCount += (definitionLevels[i] >= maxDefinitionLevel - 1 ? 1 : 0);
        }
        batchNonNullCount += nonNullCount;
        newBatchSize += valueCount;
        valuesDecoderContext.setNonNullCount(nonNullCount);
        valuesDecoderContext.setValueCount(valueCount);
    }
    if (batchNonNullCount == 0) {
        Block block = RunLengthEncodedBlock.create(field.getType(), null, newBatchSize);
        return new ColumnChunk(block, definitionLevels, repetitionLevelDecodingContext.getRepetitionLevels());
    }
    int[] values = new int[newBatchSize];
    boolean[] isNull = new boolean[newBatchSize];
    int offset = 0;
    for (ValuesDecoderContext valuesDecoderContext : definitionLevelDecodingContext.getValuesDecoderContexts()) {
        ((Int32ValuesDecoder) valuesDecoderContext.getValuesDecoder()).readNext(values, offset, valuesDecoderContext.getNonNullCount());
        int valueDestinationIndex = offset + valuesDecoderContext.getValueCount() - 1;
        int valueSourceIndex = offset + valuesDecoderContext.getNonNullCount() - 1;
        int definitionLevelIndex = valuesDecoderContext.getEnd() - 1;
        while (valueDestinationIndex >= offset) {
            if (definitionLevels[definitionLevelIndex] == maxDefinitionLevel) {
                values[valueDestinationIndex--] = values[valueSourceIndex--];
            } else if (definitionLevels[definitionLevelIndex] == maxDefinitionLevel - 1) {
                values[valueDestinationIndex] = 0;
                isNull[valueDestinationIndex] = true;
                valueDestinationIndex--;
            }
            definitionLevelIndex--;
        }
        offset += valuesDecoderContext.getValueCount();
    }
    boolean hasNoNull = batchNonNullCount == newBatchSize;
    Block block = new IntArrayBlock(newBatchSize, hasNoNull ? Optional.empty() : Optional.of(isNull), values);
    return new ColumnChunk(block, definitionLevels, repetitionLevelDecodingContext.getRepetitionLevels());
}
Also used : IntArrayBlock(com.facebook.presto.common.block.IntArrayBlock) Int32ValuesDecoder(com.facebook.presto.parquet.batchreader.decoders.ValuesDecoder.Int32ValuesDecoder) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) IntArrayBlock(com.facebook.presto.common.block.IntArrayBlock) Block(com.facebook.presto.common.block.Block) ColumnChunk(com.facebook.presto.parquet.reader.ColumnChunk)

Example 8 with IntArrayBlock

use of com.facebook.presto.common.block.IntArrayBlock in project presto by prestodb.

the class Int32NestedBatchReader method readNestedNoNull.

@Override
protected ColumnChunk readNestedNoNull() throws IOException {
    int maxDefinitionLevel = columnDescriptor.getMaxDefinitionLevel();
    RepetitionLevelDecodingContext repetitionLevelDecodingContext = readRepetitionLevels(nextBatchSize);
    DefinitionLevelDecodingContext definitionLevelDecodingContext = readDefinitionLevels(repetitionLevelDecodingContext.getDLValuesDecoderContexts(), repetitionLevelDecodingContext.getRepetitionLevels().length);
    int[] definitionLevels = definitionLevelDecodingContext.getDefinitionLevels();
    int newBatchSize = 0;
    for (ValuesDecoderContext valuesDecoderContext : definitionLevelDecodingContext.getValuesDecoderContexts()) {
        int valueCount = 0;
        for (int i = valuesDecoderContext.getStart(); i < valuesDecoderContext.getEnd(); i++) {
            valueCount += (definitionLevels[i] == maxDefinitionLevel ? 1 : 0);
        }
        newBatchSize += valueCount;
        valuesDecoderContext.setNonNullCount(valueCount);
        valuesDecoderContext.setValueCount(valueCount);
    }
    int[] values = new int[newBatchSize];
    int offset = 0;
    for (ValuesDecoderContext valuesDecoderContext : definitionLevelDecodingContext.getValuesDecoderContexts()) {
        ((Int32ValuesDecoder) valuesDecoderContext.getValuesDecoder()).readNext(values, offset, valuesDecoderContext.getNonNullCount());
        offset += valuesDecoderContext.getValueCount();
    }
    Block block = new IntArrayBlock(newBatchSize, Optional.empty(), values);
    return new ColumnChunk(block, definitionLevels, repetitionLevelDecodingContext.getRepetitionLevels());
}
Also used : IntArrayBlock(com.facebook.presto.common.block.IntArrayBlock) Int32ValuesDecoder(com.facebook.presto.parquet.batchreader.decoders.ValuesDecoder.Int32ValuesDecoder) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) IntArrayBlock(com.facebook.presto.common.block.IntArrayBlock) Block(com.facebook.presto.common.block.Block) ColumnChunk(com.facebook.presto.parquet.reader.ColumnChunk)

Example 9 with IntArrayBlock

use of com.facebook.presto.common.block.IntArrayBlock in project presto by prestodb.

the class TestRowExpressionSerde method testArrayLiteral.

@Test
public void testArrayLiteral() {
    RowExpression rowExpression = getRoundTrip("ARRAY [1, 2, 3]", true);
    assertTrue(rowExpression instanceof ConstantExpression);
    Object value = ((ConstantExpression) rowExpression).getValue();
    assertTrue(value instanceof IntArrayBlock);
    IntArrayBlock block = (IntArrayBlock) value;
    assertEquals(block.getPositionCount(), 3);
    assertEquals(block.getInt(0), 1);
    assertEquals(block.getInt(1), 2);
    assertEquals(block.getInt(2), 3);
}
Also used : IntArrayBlock(com.facebook.presto.common.block.IntArrayBlock) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) Test(org.testng.annotations.Test)

Example 10 with IntArrayBlock

use of com.facebook.presto.common.block.IntArrayBlock in project presto by prestodb.

the class FloatSelectiveStreamReader method getBlock.

@Override
public Block getBlock(int[] positions, int positionCount) {
    checkArgument(outputPositionCount > 0, "outputPositionCount must be greater than zero");
    checkState(outputRequired, "This stream reader doesn't produce output");
    checkState(positionCount <= outputPositionCount, "Not enough values");
    checkState(!valuesInUse, "BlockLease hasn't been closed yet");
    if (allNulls) {
        return new RunLengthEncodedBlock(NULL_BLOCK, positionCount);
    }
    boolean includeNulls = nullsAllowed && presentStream != null;
    if (positionCount == outputPositionCount) {
        Block block = new IntArrayBlock(positionCount, Optional.ofNullable(includeNulls ? nulls : null), values);
        nulls = null;
        values = null;
        return block;
    }
    int[] valuesCopy = new int[positionCount];
    boolean[] nullsCopy = null;
    if (includeNulls) {
        nullsCopy = new boolean[positionCount];
    }
    int positionIndex = 0;
    int nextPosition = positions[positionIndex];
    for (int i = 0; i < outputPositionCount; i++) {
        if (outputPositions[i] < nextPosition) {
            continue;
        }
        assert outputPositions[i] == nextPosition;
        valuesCopy[positionIndex] = this.values[i];
        if (nullsCopy != null) {
            nullsCopy[positionIndex] = this.nulls[i];
        }
        positionIndex++;
        if (positionIndex >= positionCount) {
            break;
        }
        nextPosition = positions[positionIndex];
    }
    return new IntArrayBlock(positionCount, Optional.ofNullable(nullsCopy), valuesCopy);
}
Also used : IntArrayBlock(com.facebook.presto.common.block.IntArrayBlock) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) IntArrayBlock(com.facebook.presto.common.block.IntArrayBlock) Block(com.facebook.presto.common.block.Block) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock)

Aggregations

IntArrayBlock (com.facebook.presto.common.block.IntArrayBlock)10 Block (com.facebook.presto.common.block.Block)6 RunLengthEncodedBlock (com.facebook.presto.common.block.RunLengthEncodedBlock)6 ColumnChunk (com.facebook.presto.parquet.reader.ColumnChunk)4 Test (org.testng.annotations.Test)3 Int32ValuesDecoder (com.facebook.presto.parquet.batchreader.decoders.ValuesDecoder.Int32ValuesDecoder)2 ConstantExpression (com.facebook.presto.spi.relation.ConstantExpression)2 RowExpression (com.facebook.presto.spi.relation.RowExpression)2 ParquetDecodingException (org.apache.parquet.io.ParquetDecodingException)2 LongArrayBlock (com.facebook.presto.common.block.LongArrayBlock)1 ShortArrayBlock (com.facebook.presto.common.block.ShortArrayBlock)1 ArrayType (com.facebook.presto.common.type.ArrayType)1 FunctionHandle (com.facebook.presto.spi.function.FunctionHandle)1 CallExpression (com.facebook.presto.spi.relation.CallExpression)1