Search in sources :

Example 26 with Type

use of io.prestosql.spi.type.Type in project hetu-core by openlookeng.

the class OrcTester method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else {
        if (BOOLEAN.equals(type)) {
            type.writeBoolean(blockBuilder, (Boolean) value);
        } else if (TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type)) {
            type.writeLong(blockBuilder, ((Number) value).longValue());
        } else if (Decimals.isShortDecimal(type)) {
            type.writeLong(blockBuilder, ((SqlDecimal) value).toBigDecimal().unscaledValue().longValue());
        } else if (Decimals.isLongDecimal(type)) {
            type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) value).toBigDecimal().unscaledValue()));
        } else if (DOUBLE.equals(type)) {
            type.writeDouble(blockBuilder, ((Number) value).doubleValue());
        } else if (REAL.equals(type)) {
            float floatValue = ((Number) value).floatValue();
            type.writeLong(blockBuilder, Float.floatToIntBits(floatValue));
        } else if (type instanceof VarcharType) {
            Slice slice = truncateToLength(utf8Slice((String) value), type);
            type.writeSlice(blockBuilder, slice);
        } else if (type instanceof CharType) {
            Slice slice = truncateToLengthAndTrimSpaces(utf8Slice((String) value), type);
            type.writeSlice(blockBuilder, slice);
        } else if (VARBINARY.equals(type)) {
            type.writeSlice(blockBuilder, Slices.wrappedBuffer(((SqlVarbinary) value).getBytes()));
        } else if (DATE.equals(type)) {
            long days = ((SqlDate) value).getDays();
            type.writeLong(blockBuilder, days);
        } else if (TIMESTAMP.equals(type)) {
            long millis = ((SqlTimestamp) value).getMillis();
            type.writeLong(blockBuilder, millis);
        } else {
            String baseType = type.getTypeSignature().getBase();
            if (StandardTypes.ARRAY.equals(baseType)) {
                List<?> array = (List<?>) value;
                Type elementType = type.getTypeParameters().get(0);
                BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
                for (Object elementValue : array) {
                    writeValue(elementType, arrayBlockBuilder, elementValue);
                }
                blockBuilder.closeEntry();
            } else if (StandardTypes.MAP.equals(baseType)) {
                Map<?, ?> map = (Map<?, ?>) value;
                Type keyType = type.getTypeParameters().get(0);
                Type valueType = type.getTypeParameters().get(1);
                BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
                for (Entry<?, ?> entry : map.entrySet()) {
                    writeValue(keyType, mapBlockBuilder, entry.getKey());
                    writeValue(valueType, mapBlockBuilder, entry.getValue());
                }
                blockBuilder.closeEntry();
            } else if (StandardTypes.ROW.equals(baseType)) {
                List<?> array = (List<?>) value;
                List<Type> fieldTypes = type.getTypeParameters();
                BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
                for (int fieldId = 0; fieldId < fieldTypes.size(); fieldId++) {
                    Type fieldType = fieldTypes.get(fieldId);
                    writeValue(fieldType, rowBlockBuilder, array.get(fieldId));
                }
                blockBuilder.closeEntry();
            } else {
                throw new IllegalArgumentException("Unsupported type " + type);
            }
        }
    }
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) SqlDecimal(io.prestosql.spi.type.SqlDecimal) SqlTimestamp(io.prestosql.spi.type.SqlTimestamp) VarbinaryType(io.prestosql.spi.type.VarbinaryType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) Entry(java.util.Map.Entry) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) Arrays.asList(java.util.Arrays.asList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) CharType(io.prestosql.spi.type.CharType) Map(java.util.Map) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Example 27 with Type

use of io.prestosql.spi.type.Type in project hetu-core by openlookeng.

the class TestOrcBloomFilters method testExtractValuesFromSingleDomain.

@Test
public void testExtractValuesFromSingleDomain() {
    Map<Type, Object> testValues = ImmutableMap.<Type, Object>builder().put(BOOLEAN, true).put(INTEGER, 1234L).put(SMALLINT, 789L).put(TINYINT, 77L).put(DATE, 901L).put(TIMESTAMP, 987654L).put(BIGINT, 4321L).put(DOUBLE, 0.123).put(REAL, (long) (floatToIntBits(0.456f))).put(VARCHAR, wrappedBuffer(TEST_STRING)).build();
    for (Map.Entry<Type, Object> testValue : testValues.entrySet()) {
        Domain predicateDomain = Domain.singleValue(testValue.getKey(), testValue.getValue());
        Optional<Collection<Object>> discreteValues = extractDiscreteValues(predicateDomain.getValues());
        assertTrue(discreteValues.isPresent());
        Collection<Object> objects = discreteValues.get();
        assertEquals(objects.size(), 1);
        assertEquals(objects.iterator().next(), testValue.getValue());
    }
}
Also used : Type(io.prestosql.spi.type.Type) RealType(io.prestosql.spi.type.RealType) Collection(java.util.Collection) Domain(io.prestosql.spi.predicate.Domain) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.testng.annotations.Test)

Example 28 with Type

use of io.prestosql.spi.type.Type in project hetu-core by openlookeng.

the class TestMapOperators method testMapEntries.

@Test
public void testMapEntries() {
    Type unknownEntryType = entryType(UNKNOWN, UNKNOWN);
    assertFunction("map_entries(null)", unknownEntryType, null);
    assertFunction("map_entries(MAP(ARRAY[], null))", unknownEntryType, null);
    assertFunction("map_entries(MAP(null, ARRAY[]))", unknownEntryType, null);
    assertFunction("map_entries(MAP(ARRAY[1, 2, 3], null))", entryType(INTEGER, UNKNOWN), null);
    assertFunction("map_entries(MAP(null, ARRAY[1, 2, 3]))", entryType(UNKNOWN, INTEGER), null);
    assertFunction("map_entries(MAP(ARRAY[], ARRAY[]))", unknownEntryType, ImmutableList.of());
    assertFunction("map_entries(MAP(ARRAY[1], ARRAY['x']))", entryType(INTEGER, createVarcharType(1)), ImmutableList.of(ImmutableList.of(1, "x")));
    assertFunction("map_entries(MAP(ARRAY[1, 2], ARRAY['x', 'y']))", entryType(INTEGER, createVarcharType(1)), ImmutableList.of(ImmutableList.of(1, "x"), ImmutableList.of(2, "y")));
    assertFunction("map_entries(MAP(ARRAY['x', 'y'], ARRAY[ARRAY[1, 2], ARRAY[3, 4]]))", entryType(createVarcharType(1), new ArrayType(INTEGER)), ImmutableList.of(ImmutableList.of("x", ImmutableList.of(1, 2)), ImmutableList.of("y", ImmutableList.of(3, 4))));
    assertFunction("map_entries(MAP(ARRAY[ARRAY[1.0E0, 2.0E0], ARRAY[3.0E0, 4.0E0]], ARRAY[5.0E0, 6.0E0]))", entryType(new ArrayType(DOUBLE), DOUBLE), ImmutableList.of(ImmutableList.of(ImmutableList.of(1.0, 2.0), 5.0), ImmutableList.of(ImmutableList.of(3.0, 4.0), 6.0)));
    assertFunction("map_entries(MAP(ARRAY['x', 'y'], ARRAY[MAP(ARRAY[1], ARRAY[2]), MAP(ARRAY[3], ARRAY[4])]))", entryType(createVarcharType(1), mapType(INTEGER, INTEGER)), ImmutableList.of(ImmutableList.of("x", ImmutableMap.of(1, 2)), ImmutableList.of("y", ImmutableMap.of(3, 4))));
    assertFunction("map_entries(MAP(ARRAY[MAP(ARRAY[1], ARRAY[2]), MAP(ARRAY[3], ARRAY[4])], ARRAY['x', 'y']))", entryType(mapType(INTEGER, INTEGER), createVarcharType(1)), ImmutableList.of(ImmutableList.of(ImmutableMap.of(1, 2), "x"), ImmutableList.of(ImmutableMap.of(3, 4), "y")));
    // null values
    List<Object> expectedEntries = ImmutableList.of(asList("x", null), asList("y", null));
    assertFunction("map_entries(MAP(ARRAY['x', 'y'], ARRAY[null, null]))", entryType(createVarcharType(1), UNKNOWN), expectedEntries);
    assertCachedInstanceHasBoundedRetainedSize("map_entries(MAP(ARRAY[1, 2], ARRAY['x', 'y']))");
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) MapType(io.prestosql.spi.type.MapType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) DecimalType.createDecimalType(io.prestosql.spi.type.DecimalType.createDecimalType) ArrayType(io.prestosql.spi.type.ArrayType) StructuralTestUtil.mapType(io.prestosql.util.StructuralTestUtil.mapType) VarcharType.createVarcharType(io.prestosql.spi.type.VarcharType.createVarcharType) SqlType(io.prestosql.spi.function.SqlType) Test(org.testng.annotations.Test)

Example 29 with Type

use of io.prestosql.spi.type.Type in project hetu-core by openlookeng.

the class TestMergeSortedPages method testMultipleStreams.

@Test
public void testMultipleStreams() {
    List<Type> types = ImmutableList.of(INTEGER, INTEGER, INTEGER);
    MaterializedResult actual = mergeSortedPages(types, ImmutableList.of(0, 1), ImmutableList.of(ASC_NULLS_FIRST, DESC_NULLS_FIRST), ImmutableList.of(rowPagesBuilder(types).row(1, 1, 2).pageBreak().pageBreak().row(8, 1, 1).row(19, 1, 3).row(27, 1, 4).row(41, 2, 5).pageBreak().row(55, 1, 2).row(89, 1, 3).row(100, 2, 6).row(100, 2, 8).row(101, 1, 4).row(202, 1, 3).row(399, 2, 2).pageBreak().row(400, 1, 1).row(401, 1, 7).pageBreak().row(402, 1, 6).build(), rowPagesBuilder(types).pageBreak().row(2, 1, 2).row(8, 1, 1).row(19, 1, 3).row(25, 1, 4).row(26, 2, 5).pageBreak().row(56, 1, 2).row(66, 1, 3).row(77, 1, 4).row(88, 1, 3).row(99, 1, 1).pageBreak().row(99, 2, 2).row(100, 1, 7).build(), rowPagesBuilder(types).row(8, 1, 1).row(88, 1, 3).pageBreak().row(89, 1, 3).pageBreak().row(90, 1, 3).pageBreak().row(91, 1, 4).row(92, 2, 5).pageBreak().row(93, 1, 2).row(94, 1, 3).row(95, 1, 4).row(97, 1, 3).row(98, 2, 2).row(100, 1, 7).build()));
    MaterializedResult expected = resultBuilder(TEST_SESSION, types).row(1, 1, 2).row(2, 1, 2).row(8, 1, 1).row(8, 1, 1).row(8, 1, 1).row(19, 1, 3).row(19, 1, 3).row(25, 1, 4).row(26, 2, 5).row(27, 1, 4).row(41, 2, 5).row(55, 1, 2).row(56, 1, 2).row(66, 1, 3).row(77, 1, 4).row(88, 1, 3).row(88, 1, 3).row(89, 1, 3).row(89, 1, 3).row(90, 1, 3).row(91, 1, 4).row(92, 2, 5).row(93, 1, 2).row(94, 1, 3).row(95, 1, 4).row(97, 1, 3).row(98, 2, 2).row(99, 1, 1).row(99, 2, 2).row(100, 2, 6).row(100, 2, 8).row(100, 1, 7).row(100, 1, 7).row(101, 1, 4).row(202, 1, 3).row(399, 2, 2).row(400, 1, 1).row(401, 1, 7).row(402, 1, 6).build();
    assertEquals(actual, expected);
}
Also used : Type(io.prestosql.spi.type.Type) OperatorAssertion.toMaterializedResult(io.prestosql.operator.OperatorAssertion.toMaterializedResult) MaterializedResult(io.prestosql.testing.MaterializedResult) Test(org.testng.annotations.Test)

Example 30 with Type

use of io.prestosql.spi.type.Type in project hetu-core by openlookeng.

the class TestMergeSortedPages method testMergeSortYieldingProgresses.

@Test
public void testMergeSortYieldingProgresses() {
    DriverYieldSignal yieldSignal = new DriverYieldSignal();
    yieldSignal.forceYieldForTesting();
    List<Type> types = ImmutableList.of(INTEGER);
    WorkProcessor<Page> mergedPages = MergeSortedPages.mergeSortedPages(ImmutableList.of(WorkProcessor.fromIterable(rowPagesBuilder(types).build())), new SimplePageWithPositionComparator(types, ImmutableList.of(0), ImmutableList.of(DESC_NULLS_LAST)), ImmutableList.of(0), types, (pageBuilder, pageWithPosition) -> pageBuilder.isFull(), false, newSimpleAggregatedMemoryContext().newAggregatedMemoryContext(), yieldSignal);
    // yield signal is on
    assertFalse(mergedPages.process());
    // processor finishes computations (yield signal is still on, but previous process() call yielded)
    assertTrue(mergedPages.process());
    assertTrue(mergedPages.isFinished());
}
Also used : Type(io.prestosql.spi.type.Type) DriverYieldSignal(io.prestosql.operator.DriverYieldSignal) Page(io.prestosql.spi.Page) SimplePageWithPositionComparator(io.prestosql.operator.SimplePageWithPositionComparator) Test(org.testng.annotations.Test)

Aggregations

Type (io.prestosql.spi.type.Type)764 Test (org.testng.annotations.Test)260 ImmutableList (com.google.common.collect.ImmutableList)230 List (java.util.List)222 ArrayList (java.util.ArrayList)212 ArrayType (io.prestosql.spi.type.ArrayType)200 RowType (io.prestosql.spi.type.RowType)194 VarcharType (io.prestosql.spi.type.VarcharType)159 VarcharType.createUnboundedVarcharType (io.prestosql.spi.type.VarcharType.createUnboundedVarcharType)150 Map (java.util.Map)150 Block (io.prestosql.spi.block.Block)133 ImmutableMap (com.google.common.collect.ImmutableMap)131 Page (io.prestosql.spi.Page)129 PrestoException (io.prestosql.spi.PrestoException)123 DecimalType (io.prestosql.spi.type.DecimalType)118 HashMap (java.util.HashMap)115 Optional (java.util.Optional)113 DecimalType.createDecimalType (io.prestosql.spi.type.DecimalType.createDecimalType)112 Slice (io.airlift.slice.Slice)88 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)87