Search in sources :

Example 96 with ArrayType

use of io.trino.spi.type.ArrayType in project trino by trinodb.

the class TestArrayOperators method testSequenceDateTimeDayToSecond.

@Test
public void testSequenceDateTimeDayToSecond() throws ParseException {
    assertFunction("SEQUENCE(date '2016-04-12', date '2016-04-14', interval '1' day)", new ArrayType(DATE), ImmutableList.of(sqlDate("2016-04-12"), sqlDate("2016-04-13"), sqlDate("2016-04-14")));
    assertFunction("SEQUENCE(date '2016-04-14', date '2016-04-12', interval '-1' day)", new ArrayType(DATE), ImmutableList.of(sqlDate("2016-04-14"), sqlDate("2016-04-13"), sqlDate("2016-04-12")));
    assertFunction("SEQUENCE(date '2016-04-12', date '2016-04-16', interval '2' day)", new ArrayType(DATE), ImmutableList.of(sqlDate("2016-04-12"), sqlDate("2016-04-14"), sqlDate("2016-04-16")));
    assertFunction("SEQUENCE(date '2016-04-16', date '2016-04-12', interval '-2' day)", new ArrayType(DATE), ImmutableList.of(sqlDate("2016-04-16"), sqlDate("2016-04-14"), sqlDate("2016-04-12")));
    assertFunction("SEQUENCE(timestamp '2016-04-16 01:00:10', timestamp '2016-04-16 01:07:00', interval '3' minute)", new ArrayType(createTimestampType(0)), ImmutableList.of(sqlTimestampOf(0, 2016, 4, 16, 1, 0, 10, 0), sqlTimestampOf(0, 2016, 4, 16, 1, 3, 10, 0), sqlTimestampOf(0, 2016, 4, 16, 1, 6, 10, 0)));
    assertFunction("SEQUENCE(timestamp '2016-04-16 01:10:10', timestamp '2016-04-16 01:03:00', interval '-3' minute)", new ArrayType(createTimestampType(0)), ImmutableList.of(sqlTimestampOf(0, 2016, 4, 16, 1, 10, 10, 0), sqlTimestampOf(0, 2016, 4, 16, 1, 7, 10, 0), sqlTimestampOf(0, 2016, 4, 16, 1, 4, 10, 0)));
    assertFunction("SEQUENCE(timestamp '2016-04-16 01:00:10', timestamp '2016-04-16 01:01:00', interval '20' second)", new ArrayType(createTimestampType(0)), ImmutableList.of(sqlTimestampOf(0, 2016, 4, 16, 1, 0, 10, 0), sqlTimestampOf(0, 2016, 4, 16, 1, 0, 30, 0), sqlTimestampOf(0, 2016, 4, 16, 1, 0, 50, 0)));
    assertFunction("SEQUENCE(timestamp '2016-04-16 01:01:10', timestamp '2016-04-16 01:00:20', interval '-20' second)", new ArrayType(createTimestampType(0)), ImmutableList.of(sqlTimestampOf(0, 2016, 4, 16, 1, 1, 10, 0), sqlTimestampOf(0, 2016, 4, 16, 1, 0, 50, 0), sqlTimestampOf(0, 2016, 4, 16, 1, 0, 30, 0)));
    assertFunction("SEQUENCE(timestamp '2016-04-16 01:00:10', timestamp '2016-04-18 01:01:00', interval '19' hour)", new ArrayType(createTimestampType(0)), ImmutableList.of(sqlTimestampOf(0, 2016, 4, 16, 1, 0, 10, 0), sqlTimestampOf(0, 2016, 4, 16, 20, 0, 10, 0), sqlTimestampOf(0, 2016, 4, 17, 15, 0, 10, 0)));
    assertFunction("SEQUENCE(timestamp '2016-04-16 01:00:10', timestamp '2016-04-14 01:00:20', interval '-19' hour)", new ArrayType(createTimestampType(0)), ImmutableList.of(sqlTimestampOf(0, 2016, 4, 16, 1, 0, 10, 0), sqlTimestampOf(0, 2016, 4, 15, 6, 0, 10, 0), sqlTimestampOf(0, 2016, 4, 14, 11, 0, 10, 0)));
    // failure modes
    assertInvalidFunction("SEQUENCE(date '2016-04-12', date '2016-04-14', interval '-1' day)", "sequence stop value should be greater than or equal to start value if step is greater than zero otherwise stop should be less than or equal to start");
    assertInvalidFunction("SEQUENCE(date '2016-04-14', date '2016-04-12', interval '1' day)", "sequence stop value should be greater than or equal to start value if step is greater than zero otherwise stop should be less than or equal to start");
    assertInvalidFunction("SEQUENCE(date '2000-04-14', date '2030-04-12', interval '1' day)", "result of sequence function must not have more than 10000 entries");
    assertInvalidFunction("SEQUENCE(date '2018-01-01', date '2018-01-04', interval '18' hour)", "sequence step must be a day interval if start and end values are dates");
    assertInvalidFunction("SEQUENCE(timestamp '2016-04-16 01:00:10', timestamp '2016-04-16 01:01:00', interval '-20' second)", "sequence stop value should be greater than or equal to start value if step is greater than zero otherwise stop should be less than or equal to start");
    assertInvalidFunction("SEQUENCE(timestamp '2016-04-16 01:10:10', timestamp '2016-04-16 01:01:00', interval '20' second)", "sequence stop value should be greater than or equal to start value if step is greater than zero otherwise stop should be less than or equal to start");
    assertInvalidFunction("SEQUENCE(timestamp '2016-04-16 01:00:10', timestamp '2016-04-16 09:01:00', interval '1' second)", "result of sequence function must not have more than 10000 entries");
}
Also used : ArrayType(io.trino.spi.type.ArrayType) Test(org.testng.annotations.Test)

Example 97 with ArrayType

use of io.trino.spi.type.ArrayType in project trino by trinodb.

the class TestArrayOperators method testArrayMax.

@Test
public void testArrayMax() {
    assertFunction("ARRAY_MAX(ARRAY [])", UNKNOWN, null);
    assertFunction("ARRAY_MAX(ARRAY [NULL])", UNKNOWN, null);
    assertFunction("ARRAY_MAX(ARRAY [NaN()])", DOUBLE, NaN);
    assertFunction("ARRAY_MAX(ARRAY [CAST(NaN() AS REAL)])", REAL, Float.NaN);
    assertFunction("ARRAY_MAX(ARRAY [NULL, NULL, NULL])", UNKNOWN, null);
    assertFunction("ARRAY_MAX(ARRAY [NaN(), NaN(), NaN()])", DOUBLE, NaN);
    assertFunction("ARRAY_MAX(ARRAY [CAST(NaN() AS REAL), CAST(NaN() AS REAL)])", REAL, Float.NaN);
    assertFunction("ARRAY_MAX(ARRAY [NULL, 2, 3])", INTEGER, null);
    assertFunction("ARRAY_MAX(ARRAY [NaN(), 2, 3])", DOUBLE, 3.0);
    assertFunction("ARRAY_MAX(ARRAY [2, NaN(), 3])", DOUBLE, 3.0);
    assertFunction("ARRAY_MAX(ARRAY [2, 3, NaN()])", DOUBLE, 3.0);
    assertFunction("ARRAY_MAX(ARRAY [NULL, NaN(), 1])", DOUBLE, null);
    assertFunction("ARRAY_MAX(ARRAY [NaN(), NULL, 3.0])", DOUBLE, null);
    assertFunction("ARRAY_MAX(ARRAY [1.0E0, NULL, 3])", DOUBLE, null);
    assertFunction("ARRAY_MAX(ARRAY [1.0, NaN(), 3])", DOUBLE, 3.0);
    assertFunction("ARRAY_MAX(ARRAY [CAST(NaN() AS REAL), REAL '2', REAL '3'])", REAL, 3.0f);
    assertFunction("ARRAY_MAX(ARRAY [REAL '2', CAST(NaN() AS REAL), REAL '3'])", REAL, 3.0f);
    assertFunction("ARRAY_MAX(ARRAY [REAL '2', REAL '3', CAST(NaN() AS REAL)])", REAL, 3.0f);
    assertFunction("ARRAY_MAX(ARRAY [NULL, CAST(NaN() AS REAL), REAL '1'])", REAL, null);
    assertFunction("ARRAY_MAX(ARRAY [CAST(NaN() AS REAL), NULL, REAL '3'])", REAL, null);
    assertFunction("ARRAY_MAX(ARRAY [REAL '1', NULL, REAL '3'])", REAL, null);
    assertFunction("ARRAY_MAX(ARRAY [REAL '1', CAST(NaN() AS REAL), REAL '3'])", REAL, 3.0f);
    assertFunction("ARRAY_MAX(ARRAY ['1', '2', NULL])", createVarcharType(1), null);
    assertFunction("ARRAY_MAX(ARRAY [3, 2, 1])", INTEGER, 3);
    assertFunction("ARRAY_MAX(ARRAY [1, 2, 3])", INTEGER, 3);
    assertFunction("ARRAY_MAX(ARRAY [BIGINT '1', 2, 3])", BIGINT, 3L);
    assertFunction("ARRAY_MAX(ARRAY [1, 2.0E0, 3])", DOUBLE, 3.0);
    assertFunction("ARRAY_MAX(ARRAY [ARRAY[1, 2], ARRAY[3]])", new ArrayType(INTEGER), ImmutableList.of(3));
    assertFunction("ARRAY_MAX(ARRAY [1.0E0, 2.5E0, 3.0E0])", DOUBLE, 3.0);
    assertFunction("ARRAY_MAX(ARRAY ['puppies', 'kittens'])", createVarcharType(7), "puppies");
    assertFunction("ARRAY_MAX(ARRAY [TRUE, FALSE])", BOOLEAN, true);
    assertFunction("ARRAY_MAX(ARRAY [NULL, FALSE])", BOOLEAN, null);
    assertFunction("ARRAY_MAX(ARRAY [TIMESTAMP '2020-05-10 12:34:56.123456789', TIMESTAMP '1111-05-10 12:34:56.123456789'])", createTimestampType(9), timestamp(9, "2020-05-10 12:34:56.123456789"));
    assertDecimalFunction("ARRAY_MAX(ARRAY [2.1, 2.2, 2.3])", decimal("2.3", createDecimalType(2, 1)));
    assertDecimalFunction("ARRAY_MAX(ARRAY [2.111111222111111114111, 2.22222222222222222, 2.222222222222223])", decimal("2.222222222222223000000", createDecimalType(22, 21)));
    assertDecimalFunction("ARRAY_MAX(ARRAY [1.9, 2, 2.3])", decimal("0000000002.3", createDecimalType(11, 1)));
    assertDecimalFunction("ARRAY_MAX(ARRAY [2.22222222222222222, 2.3])", decimal("2.30000000000000000", createDecimalType(18, 17)));
    assertFunction("ARRAY_MAX(ARRAY [ROW(NaN()), ROW(2), ROW(3)])", anonymousRow(DOUBLE), ImmutableList.of(3.0));
    assertFunction("ARRAY_MAX(ARRAY [ROW(2), ROW(NaN()), ROW(3)])", anonymousRow(DOUBLE), ImmutableList.of(3.0));
    assertFunction("ARRAY_MAX(ARRAY [ROW(2), ROW(3), ROW(NaN())])", anonymousRow(DOUBLE), ImmutableList.of(3.0));
    assertFunction("ARRAY_MAX(ARRAY [NULL, ROW(NaN()), ROW(1)])", anonymousRow(DOUBLE), null);
    assertFunction("ARRAY_MAX(ARRAY [ROW(NaN()), NULL, ROW(3.0)])", anonymousRow(DOUBLE), null);
    assertFunction("ARRAY_MAX(ARRAY [ROW(1.0E0), NULL, ROW(3)])", anonymousRow(DOUBLE), null);
    assertFunction("ARRAY_MAX(ARRAY [ROW(1.0), ROW(NaN()), ROW(3)])", anonymousRow(DOUBLE), ImmutableList.of(3.0));
    assertFunction("ARRAY_MAX(ARRAY [ROW(CAST(NaN() AS REAL)), ROW(REAL '2'), ROW(REAL '3')])", anonymousRow(REAL), ImmutableList.of(3.0f));
    assertFunction("ARRAY_MAX(ARRAY [ROW(REAL '2'), ROW(CAST(NaN() AS REAL)), ROW(REAL '3')])", anonymousRow(REAL), ImmutableList.of(3.0f));
    assertFunction("ARRAY_MAX(ARRAY [ROW(REAL '2'), ROW(REAL '3'), ROW(CAST(NaN() AS REAL))])", anonymousRow(REAL), ImmutableList.of(3.0f));
    assertFunction("ARRAY_MAX(ARRAY [NULL, ROW(CAST(NaN() AS REAL)), ROW(REAL '1')])", anonymousRow(REAL), null);
    assertFunction("ARRAY_MAX(ARRAY [ROW(CAST(NaN() AS REAL)), NULL, ROW(REAL '3')])", anonymousRow(REAL), null);
    assertFunction("ARRAY_MAX(ARRAY [ROW(REAL '1'), NULL, ROW(REAL '3')])", anonymousRow(REAL), null);
    assertFunction("ARRAY_MAX(ARRAY [ROW(REAL '1'), ROW(CAST(NaN() AS REAL)), ROW(REAL '3')])", anonymousRow(REAL), ImmutableList.of(3.0f));
    assertFunction("ARRAY_MAX(ARRAY [ARRAY[NaN()], ARRAY[2], ARRAY[3]])", new ArrayType(DOUBLE), ImmutableList.of(3.0));
    assertFunction("ARRAY_MAX(ARRAY [ARRAY[2], ARRAY[NaN()], ARRAY[3]])", new ArrayType(DOUBLE), ImmutableList.of(3.0));
    assertFunction("ARRAY_MAX(ARRAY [ARRAY[2], ARRAY[3], ARRAY[NaN()]])", new ArrayType(DOUBLE), ImmutableList.of(3.0));
    assertFunction("ARRAY_MAX(ARRAY [NULL, ARRAY[NaN()], ARRAY[1]])", new ArrayType(DOUBLE), null);
    assertFunction("ARRAY_MAX(ARRAY [ARRAY[NaN()], NULL, ARRAY[3.0]])", new ArrayType(DOUBLE), null);
    assertFunction("ARRAY_MAX(ARRAY [ARRAY[1.0E0], NULL, ARRAY[3]])", new ArrayType(DOUBLE), null);
    assertFunction("ARRAY_MAX(ARRAY [ARRAY[1.0], ARRAY[NaN()], ARRAY[3]])", new ArrayType(DOUBLE), ImmutableList.of(3.0));
    assertFunction("ARRAY_MAX(ARRAY [ARRAY[CAST(NaN() AS REAL)], ARRAY[REAL '2'], ARRAY[REAL '3']])", new ArrayType(REAL), ImmutableList.of(3.0f));
    assertFunction("ARRAY_MAX(ARRAY [ARRAY[REAL '2'], ARRAY[CAST(NaN() AS REAL)], ARRAY[REAL '3']])", new ArrayType(REAL), ImmutableList.of(3.0f));
    assertFunction("ARRAY_MAX(ARRAY [ARRAY[REAL '2'], ARRAY[REAL '3'], ARRAY[CAST(NaN() AS REAL)]])", new ArrayType(REAL), ImmutableList.of(3.0f));
    assertFunction("ARRAY_MAX(ARRAY [NULL, ARRAY[CAST(NaN() AS REAL)], ARRAY[REAL '1']])", new ArrayType(REAL), null);
    assertFunction("ARRAY_MAX(ARRAY [ARRAY[CAST(NaN() AS REAL)], NULL, ARRAY[REAL '3']])", new ArrayType(REAL), null);
    assertFunction("ARRAY_MAX(ARRAY [ARRAY[REAL '1'], NULL, ARRAY[REAL '3']])", new ArrayType(REAL), null);
    assertFunction("ARRAY_MAX(ARRAY [ARRAY[REAL '1'], ARRAY[CAST(NaN() AS REAL)], ARRAY[REAL '3']])", new ArrayType(REAL), ImmutableList.of(3.0f));
}
Also used : ArrayType(io.trino.spi.type.ArrayType) Test(org.testng.annotations.Test)

Example 98 with ArrayType

use of io.trino.spi.type.ArrayType in project trino by trinodb.

the class AbstractTestType method getNonNullValueForType.

/**
 * @return a non-null value, represented in native container type
 */
private static Object getNonNullValueForType(Type type) {
    if (type.getJavaType() == boolean.class) {
        return true;
    }
    if (type.getJavaType() == long.class) {
        return 1L;
    }
    if (type.getJavaType() == double.class) {
        return 1.0;
    }
    if (type.getJavaType() == Slice.class) {
        return Slices.utf8Slice("_");
    }
    if (type instanceof ArrayType) {
        ArrayType arrayType = (ArrayType) type;
        Type elementType = arrayType.getElementType();
        Object elementNonNullValue = getNonNullValueForType(elementType);
        return arrayBlockOf(elementType, elementNonNullValue);
    }
    if (type instanceof MapType) {
        MapType mapType = (MapType) type;
        Type keyType = mapType.getKeyType();
        Type valueType = mapType.getValueType();
        Object keyNonNullValue = getNonNullValueForType(keyType);
        Object valueNonNullValue = getNonNullValueForType(valueType);
        Map<?, ?> map = ImmutableMap.of(keyNonNullValue, valueNonNullValue);
        return mapBlockOf(keyType, valueType, map);
    }
    if (type instanceof RowType) {
        RowType rowType = (RowType) type;
        List<Type> elementTypes = rowType.getTypeParameters();
        Object[] elementNonNullValues = elementTypes.stream().map(AbstractTestType::getNonNullValueForType).toArray(Object[]::new);
        return toRow(elementTypes, elementNonNullValues);
    }
    throw new IllegalStateException("Unsupported Java type " + type.getJavaType() + " (for type " + type + ")");
}
Also used : ArrayType(io.trino.spi.type.ArrayType) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) Type(io.trino.spi.type.Type) MapType(io.trino.spi.type.MapType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType)

Example 99 with ArrayType

use of io.trino.spi.type.ArrayType in project trino by trinodb.

the class StructuralTestUtil method appendToBlockBuilder.

public static void appendToBlockBuilder(Type type, Object element, BlockBuilder blockBuilder) {
    Class<?> javaType = type.getJavaType();
    if (element == null) {
        blockBuilder.appendNull();
    } else if (type instanceof ArrayType && element instanceof Iterable<?>) {
        BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
        for (Object subElement : (Iterable<?>) element) {
            appendToBlockBuilder(type.getTypeParameters().get(0), subElement, subBlockBuilder);
        }
        blockBuilder.closeEntry();
    } else if (type instanceof RowType && element instanceof Iterable<?>) {
        BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
        int field = 0;
        for (Object subElement : (Iterable<?>) element) {
            appendToBlockBuilder(type.getTypeParameters().get(field), subElement, subBlockBuilder);
            field++;
        }
        blockBuilder.closeEntry();
    } else if (type instanceof MapType && element instanceof Map<?, ?>) {
        BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
        for (Map.Entry<?, ?> entry : ((Map<?, ?>) element).entrySet()) {
            appendToBlockBuilder(type.getTypeParameters().get(0), entry.getKey(), subBlockBuilder);
            appendToBlockBuilder(type.getTypeParameters().get(1), entry.getValue(), subBlockBuilder);
        }
        blockBuilder.closeEntry();
    } else if (javaType == boolean.class) {
        type.writeBoolean(blockBuilder, (Boolean) element);
    } else if (javaType == long.class) {
        if (element instanceof SqlDecimal) {
            type.writeLong(blockBuilder, ((SqlDecimal) element).getUnscaledValue().longValue());
        } else if (REAL.equals(type)) {
            type.writeLong(blockBuilder, floatToRawIntBits(((Number) element).floatValue()));
        } else {
            type.writeLong(blockBuilder, ((Number) element).longValue());
        }
    } else if (javaType == double.class) {
        type.writeDouble(blockBuilder, ((Number) element).doubleValue());
    } else if (javaType == Slice.class) {
        if (element instanceof String) {
            type.writeSlice(blockBuilder, Slices.utf8Slice(element.toString()));
        } else if (element instanceof byte[]) {
            type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) element));
        } else {
            type.writeSlice(blockBuilder, (Slice) element);
        }
    } else {
        if (element instanceof SqlDecimal) {
            type.writeObject(blockBuilder, Int128.valueOf(((SqlDecimal) element).getUnscaledValue()));
        } else if (element instanceof BigDecimal) {
            type.writeObject(blockBuilder, Decimals.valueOf((BigDecimal) element));
        } else {
            type.writeObject(blockBuilder, element);
        }
    }
}
Also used : RowType(io.trino.spi.type.RowType) SqlDecimal(io.trino.spi.type.SqlDecimal) MapType(io.trino.spi.type.MapType) BigDecimal(java.math.BigDecimal) ArrayType(io.trino.spi.type.ArrayType) Slice(io.airlift.slice.Slice) Map(java.util.Map) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 100 with ArrayType

use of io.trino.spi.type.ArrayType in project trino by trinodb.

the class OrcTester method testListRoundTrip.

private void testListRoundTrip(Type type, List<?> readValues) throws Exception {
    Type arrayType = arrayType(type);
    // values in simple list
    testRoundTripType(arrayType, readValues.stream().map(OrcTester::toHiveList).collect(toList()));
    if (structuralNullTestsEnabled) {
        // values and nulls in simple list
        testRoundTripType(arrayType, insertNullEvery(5, readValues).stream().map(OrcTester::toHiveList).collect(toList()));
        // all null values in simple list
        testRoundTripType(arrayType, readValues.stream().map(value -> toHiveList(null)).collect(toList()));
    }
}
Also used : TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) OrcType(io.trino.orc.metadata.OrcType) MapType(io.trino.spi.type.MapType) VarbinaryType(io.trino.spi.type.VarbinaryType) CharType(io.trino.spi.type.CharType) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) DecimalType(io.trino.spi.type.DecimalType) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType)

Aggregations

ArrayType (io.trino.spi.type.ArrayType)289 Test (org.testng.annotations.Test)205 Type (io.trino.spi.type.Type)92 RowType (io.trino.spi.type.RowType)86 ImmutableList (com.google.common.collect.ImmutableList)66 List (java.util.List)62 ArrayList (java.util.ArrayList)59 MapType (io.trino.spi.type.MapType)43 Arrays.asList (java.util.Arrays.asList)36 Collections.singletonList (java.util.Collections.singletonList)34 VarcharType (io.trino.spi.type.VarcharType)32 VarcharType.createUnboundedVarcharType (io.trino.spi.type.VarcharType.createUnboundedVarcharType)32 BlockBuilder (io.trino.spi.block.BlockBuilder)31 MessageType (org.apache.parquet.schema.MessageType)31 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)30 MessageTypeParser.parseMessageType (org.apache.parquet.schema.MessageTypeParser.parseMessageType)27 DecimalType (io.trino.spi.type.DecimalType)26 StructuralTestUtil.mapType (io.trino.testing.StructuralTestUtil.mapType)24 Block (io.trino.spi.block.Block)23 Map (java.util.Map)23