Search in sources :

Example 1 with HeapIntVector

use of org.apache.flink.table.data.columnar.vector.heap.HeapIntVector in project flink by apache.

the class VectorizedColumnBatchTest method testDictionary.

@Test
public void testDictionary() {
    // all null
    HeapIntVector col = new HeapIntVector(VECTOR_SIZE);
    Integer[] dict = new Integer[2];
    dict[0] = 1998;
    dict[1] = 9998;
    col.setDictionary(new ColumnVectorTest.TestDictionary(dict));
    HeapIntVector heapIntVector = col.reserveDictionaryIds(VECTOR_SIZE);
    for (int i = 0; i < VECTOR_SIZE; i++) {
        heapIntVector.vector[i] = i % 2 == 0 ? 0 : 1;
    }
    VectorizedColumnBatch batch = new VectorizedColumnBatch(new ColumnVector[] { col });
    for (int i = 0; i < VECTOR_SIZE; i++) {
        ColumnarRowData row = new ColumnarRowData(batch, i);
        if (i % 2 == 0) {
            assertEquals(row.getInt(0), 1998);
        } else {
            assertEquals(row.getInt(0), 9998);
        }
    }
}
Also used : HeapIntVector(org.apache.flink.table.data.columnar.vector.heap.HeapIntVector) ColumnarRowData(org.apache.flink.table.data.columnar.ColumnarRowData) Test(org.junit.Test)

Example 2 with HeapIntVector

use of org.apache.flink.table.data.columnar.vector.heap.HeapIntVector in project flink by apache.

the class VectorizedColumnBatchTest method testNull.

@Test
public void testNull() {
    // all null
    HeapIntVector col0 = new HeapIntVector(VECTOR_SIZE);
    for (int i = 0; i < VECTOR_SIZE; i++) {
        col0.setNullAt(i);
    }
    // some null
    HeapIntVector col1 = new HeapIntVector(VECTOR_SIZE);
    for (int i = 0; i < VECTOR_SIZE; i++) {
        if (i % 2 == 0) {
            col1.setNullAt(i);
        } else {
            col1.vector[i] = i;
        }
    }
    VectorizedColumnBatch batch = new VectorizedColumnBatch(new ColumnVector[] { col0, col1 });
    for (int i = 0; i < VECTOR_SIZE; i++) {
        ColumnarRowData row = new ColumnarRowData(batch, i);
        assertTrue(row.isNullAt(0));
        if (i % 2 == 0) {
            assertTrue(row.isNullAt(1));
        } else {
            assertEquals(row.getInt(1), i);
        }
    }
}
Also used : HeapIntVector(org.apache.flink.table.data.columnar.vector.heap.HeapIntVector) ColumnarRowData(org.apache.flink.table.data.columnar.ColumnarRowData) Test(org.junit.Test)

Example 3 with HeapIntVector

use of org.apache.flink.table.data.columnar.vector.heap.HeapIntVector in project flink by apache.

the class ColumnVectorTest method testReserveDictIds.

@Test
public void testReserveDictIds() {
    HeapIntVector vector = new HeapIntVector(SIZE);
    assertTrue(vector.reserveDictionaryIds(2).vector.length >= 2);
    assertTrue(vector.reserveDictionaryIds(5).vector.length >= 5);
    assertTrue(vector.reserveDictionaryIds(2).vector.length >= 2);
}
Also used : HeapIntVector(org.apache.flink.table.data.columnar.vector.heap.HeapIntVector) Test(org.junit.Test)

Example 4 with HeapIntVector

use of org.apache.flink.table.data.columnar.vector.heap.HeapIntVector in project flink by apache.

the class ParquetSplitReaderUtil method createVectorFromConstant.

public static ColumnVector createVectorFromConstant(LogicalType type, Object value, int batchSize) {
    switch(type.getTypeRoot()) {
        case CHAR:
        case VARCHAR:
        case BINARY:
        case VARBINARY:
            HeapBytesVector bsv = new HeapBytesVector(batchSize);
            if (value == null) {
                bsv.fillWithNulls();
            } else {
                bsv.fill(value instanceof byte[] ? (byte[]) value : value.toString().getBytes(StandardCharsets.UTF_8));
            }
            return bsv;
        case BOOLEAN:
            HeapBooleanVector bv = new HeapBooleanVector(batchSize);
            if (value == null) {
                bv.fillWithNulls();
            } else {
                bv.fill((boolean) value);
            }
            return bv;
        case TINYINT:
            HeapByteVector byteVector = new HeapByteVector(batchSize);
            if (value == null) {
                byteVector.fillWithNulls();
            } else {
                byteVector.fill(((Number) value).byteValue());
            }
            return byteVector;
        case SMALLINT:
            HeapShortVector sv = new HeapShortVector(batchSize);
            if (value == null) {
                sv.fillWithNulls();
            } else {
                sv.fill(((Number) value).shortValue());
            }
            return sv;
        case INTEGER:
            HeapIntVector iv = new HeapIntVector(batchSize);
            if (value == null) {
                iv.fillWithNulls();
            } else {
                iv.fill(((Number) value).intValue());
            }
            return iv;
        case BIGINT:
            HeapLongVector lv = new HeapLongVector(batchSize);
            if (value == null) {
                lv.fillWithNulls();
            } else {
                lv.fill(((Number) value).longValue());
            }
            return lv;
        case DECIMAL:
            DecimalType decimalType = (DecimalType) type;
            int precision = decimalType.getPrecision();
            int scale = decimalType.getScale();
            DecimalData decimal = value == null ? null : Preconditions.checkNotNull(DecimalData.fromBigDecimal((BigDecimal) value, precision, scale));
            ColumnVector internalVector;
            if (ParquetSchemaConverter.is32BitDecimal(precision)) {
                internalVector = createVectorFromConstant(new IntType(), decimal == null ? null : (int) decimal.toUnscaledLong(), batchSize);
            } else if (ParquetSchemaConverter.is64BitDecimal(precision)) {
                internalVector = createVectorFromConstant(new BigIntType(), decimal == null ? null : decimal.toUnscaledLong(), batchSize);
            } else {
                internalVector = createVectorFromConstant(new VarBinaryType(), decimal == null ? null : decimal.toUnscaledBytes(), batchSize);
            }
            return new ParquetDecimalVector(internalVector);
        case FLOAT:
            HeapFloatVector fv = new HeapFloatVector(batchSize);
            if (value == null) {
                fv.fillWithNulls();
            } else {
                fv.fill(((Number) value).floatValue());
            }
            return fv;
        case DOUBLE:
            HeapDoubleVector dv = new HeapDoubleVector(batchSize);
            if (value == null) {
                dv.fillWithNulls();
            } else {
                dv.fill(((Number) value).doubleValue());
            }
            return dv;
        case DATE:
            if (value instanceof LocalDate) {
                value = Date.valueOf((LocalDate) value);
            }
            return createVectorFromConstant(new IntType(), value == null ? null : toInternal((Date) value), batchSize);
        case TIMESTAMP_WITHOUT_TIME_ZONE:
            HeapTimestampVector tv = new HeapTimestampVector(batchSize);
            if (value == null) {
                tv.fillWithNulls();
            } else {
                tv.fill(TimestampData.fromLocalDateTime((LocalDateTime) value));
            }
            return tv;
        default:
            throw new UnsupportedOperationException("Unsupported type: " + type);
    }
}
Also used : HeapShortVector(org.apache.flink.table.data.columnar.vector.heap.HeapShortVector) HeapLongVector(org.apache.flink.table.data.columnar.vector.heap.HeapLongVector) LocalDateTime(java.time.LocalDateTime) VarBinaryType(org.apache.flink.table.types.logical.VarBinaryType) HeapByteVector(org.apache.flink.table.data.columnar.vector.heap.HeapByteVector) HeapDoubleVector(org.apache.flink.table.data.columnar.vector.heap.HeapDoubleVector) HeapTimestampVector(org.apache.flink.table.data.columnar.vector.heap.HeapTimestampVector) HeapBytesVector(org.apache.flink.table.data.columnar.vector.heap.HeapBytesVector) HeapIntVector(org.apache.flink.table.data.columnar.vector.heap.HeapIntVector) BigIntType(org.apache.flink.table.types.logical.BigIntType) LocalDate(java.time.LocalDate) HeapBooleanVector(org.apache.flink.table.data.columnar.vector.heap.HeapBooleanVector) ColumnVector(org.apache.flink.table.data.columnar.vector.ColumnVector) WritableColumnVector(org.apache.flink.table.data.columnar.vector.writable.WritableColumnVector) IntType(org.apache.flink.table.types.logical.IntType) BigIntType(org.apache.flink.table.types.logical.BigIntType) DecimalData(org.apache.flink.table.data.DecimalData) DecimalType(org.apache.flink.table.types.logical.DecimalType) HeapFloatVector(org.apache.flink.table.data.columnar.vector.heap.HeapFloatVector)

Example 5 with HeapIntVector

use of org.apache.flink.table.data.columnar.vector.heap.HeapIntVector in project flink by apache.

the class ColumnVectorTest method testInt.

@Test
public void testInt() {
    HeapIntVector vector = new HeapIntVector(SIZE);
    for (int i = 0; i < SIZE; i++) {
        vector.setInt(i, i);
    }
    for (int i = 0; i < SIZE; i++) {
        assertEquals(i, vector.getInt(i));
    }
    vector.fill(22);
    for (int i = 0; i < SIZE; i++) {
        assertEquals(22, vector.getInt(i));
    }
    vector = new HeapIntVector(SIZE);
    vector.setInts(0, SIZE, 22);
    for (int i = 0; i < SIZE; i++) {
        assertEquals(22, vector.getInt(i));
    }
    vector.setDictionary(new TestDictionary(IntStream.range(0, SIZE).boxed().toArray()));
    setRangeDictIds(vector);
    for (int i = 0; i < SIZE; i++) {
        assertEquals(i, vector.getInt(i));
    }
    int[] ints = IntStream.range(0, SIZE).toArray();
    byte[] binary = new byte[SIZE * 8];
    UNSAFE.copyMemory(ints, INT_ARRAY_OFFSET, binary, BYTE_ARRAY_OFFSET, binary.length);
    vector = new HeapIntVector(SIZE);
    vector.setIntsFromBinary(0, SIZE, binary, 0);
    for (int i = 0; i < SIZE; i++) {
        assertEquals(i, vector.getInt(i));
    }
}
Also used : HeapIntVector(org.apache.flink.table.data.columnar.vector.heap.HeapIntVector) Test(org.junit.Test)

Aggregations

HeapIntVector (org.apache.flink.table.data.columnar.vector.heap.HeapIntVector)6 Test (org.junit.Test)5 ColumnarRowData (org.apache.flink.table.data.columnar.ColumnarRowData)3 HeapBooleanVector (org.apache.flink.table.data.columnar.vector.heap.HeapBooleanVector)2 HeapByteVector (org.apache.flink.table.data.columnar.vector.heap.HeapByteVector)2 HeapBytesVector (org.apache.flink.table.data.columnar.vector.heap.HeapBytesVector)2 HeapDoubleVector (org.apache.flink.table.data.columnar.vector.heap.HeapDoubleVector)2 HeapFloatVector (org.apache.flink.table.data.columnar.vector.heap.HeapFloatVector)2 HeapLongVector (org.apache.flink.table.data.columnar.vector.heap.HeapLongVector)2 HeapShortVector (org.apache.flink.table.data.columnar.vector.heap.HeapShortVector)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 DecimalData (org.apache.flink.table.data.DecimalData)1 ColumnarArrayData (org.apache.flink.table.data.columnar.ColumnarArrayData)1 ColumnVector (org.apache.flink.table.data.columnar.vector.ColumnVector)1 HeapTimestampVector (org.apache.flink.table.data.columnar.vector.heap.HeapTimestampVector)1 WritableColumnVector (org.apache.flink.table.data.columnar.vector.writable.WritableColumnVector)1 BigIntType (org.apache.flink.table.types.logical.BigIntType)1 DecimalType (org.apache.flink.table.types.logical.DecimalType)1