Search in sources :

Example 1 with ArrayType

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

the class TypeUtils method prestoNativeToJdbcObject.

private static Object prestoNativeToJdbcObject(ConnectorSession session, Type prestoType, Object prestoNative) {
    if (prestoNative == null) {
        return null;
    }
    if (DOUBLE.equals(prestoType) || BOOLEAN.equals(prestoType) || BIGINT.equals(prestoType)) {
        return prestoNative;
    }
    if (prestoType instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) prestoType;
        if (decimalType.isShort()) {
            BigInteger unscaledValue = BigInteger.valueOf((long) prestoNative);
            return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
        }
        BigInteger unscaledValue = decodeUnscaledValue((Slice) prestoNative);
        return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
    }
    if (REAL.equals(prestoType)) {
        return intBitsToFloat(toIntExact((long) prestoNative));
    }
    if (TINYINT.equals(prestoType)) {
        return SignedBytes.checkedCast((long) prestoNative);
    }
    if (SMALLINT.equals(prestoType)) {
        return Shorts.checkedCast((long) prestoNative);
    }
    if (INTEGER.equals(prestoType)) {
        return toIntExact((long) prestoNative);
    }
    if (DATE.equals(prestoType)) {
        // convert to midnight in default time zone
        long millis = DAYS.toMillis((long) prestoNative);
        return new Date(UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millis));
    }
    if (prestoType instanceof VarcharType || prestoType instanceof CharType) {
        return ((Slice) prestoNative).toStringUtf8();
    }
    if (prestoType instanceof ArrayType) {
        // process subarray of multi-dimensional array
        return getJdbcObjectArray(session, ((ArrayType) prestoType).getElementType(), (Block) prestoNative);
    }
    throw new PrestoException(NOT_SUPPORTED, "Unsupported type: " + prestoType);
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) VarcharType(io.prestosql.spi.type.VarcharType) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) DecimalType(io.prestosql.spi.type.DecimalType) BigInteger(java.math.BigInteger) PrestoException(io.prestosql.spi.PrestoException) CharType(io.prestosql.spi.type.CharType) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) Date(java.sql.Date)

Example 2 with ArrayType

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

the class TypeUtils method jdbcObjectToPrestoNative.

private static Object jdbcObjectToPrestoNative(ConnectorSession session, Object jdbcObject, Type prestoType) {
    if (jdbcObject == null) {
        return null;
    }
    if (BOOLEAN.equals(prestoType) || TINYINT.equals(prestoType) || SMALLINT.equals(prestoType) || INTEGER.equals(prestoType) || BIGINT.equals(prestoType) || DOUBLE.equals(prestoType)) {
        return jdbcObject;
    }
    if (prestoType instanceof ArrayType) {
        return jdbcObjectArrayToBlock(session, ((ArrayType) prestoType).getElementType(), (Object[]) jdbcObject);
    }
    if (prestoType instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) prestoType;
        BigDecimal value = (BigDecimal) jdbcObject;
        if (decimalType.isShort()) {
            return encodeShortScaledValue(value, decimalType.getScale());
        }
        return encodeScaledValue(value, decimalType.getScale());
    }
    if (REAL.equals(prestoType)) {
        return floatToRawIntBits((float) jdbcObject);
    }
    if (DATE.equals(prestoType)) {
        long localMillis = ((Date) jdbcObject).getTime();
        // Convert it to a ~midnight in UTC.
        long utcMillis = ISOChronology.getInstance().getZone().getMillisKeepLocal(UTC, localMillis);
        // convert to days
        return MILLISECONDS.toDays(utcMillis);
    }
    if (TIMESTAMP.equals(prestoType)) {
        Timestamp timestamp = (Timestamp) jdbcObject;
        return timestamp.toLocalDateTime().atZone(ZoneOffset.UTC).toInstant().toEpochMilli();
    }
    if (prestoType instanceof VarcharType) {
        return utf8Slice((String) jdbcObject);
    }
    if (prestoType instanceof CharType) {
        return utf8Slice(CharMatcher.is(' ').trimTrailingFrom((String) jdbcObject));
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported type %s and object type %s", prestoType, jdbcObject.getClass()));
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) PrestoException(io.prestosql.spi.PrestoException) CharType(io.prestosql.spi.type.CharType) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) Date(java.sql.Date)

Example 3 with ArrayType

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

the class TestMapOperators method testElementAt.

@Test
public void testElementAt() {
    // empty map
    assertFunction("element_at(MAP(CAST(ARRAY [] AS ARRAY(BIGINT)), CAST(ARRAY [] AS ARRAY(BIGINT))), 1)", BIGINT, null);
    // missing key
    assertFunction("element_at(MAP(ARRAY [1], ARRAY [1e0]), 2)", DOUBLE, null);
    assertFunction("element_at(MAP(ARRAY [1.0], ARRAY ['a']), 2.0)", createVarcharType(1), null);
    assertFunction("element_at(MAP(ARRAY ['a'], ARRAY [true]), 'b')", BOOLEAN, null);
    assertFunction("element_at(MAP(ARRAY [true], ARRAY [ARRAY [1]]), false)", new ArrayType(INTEGER), null);
    assertFunction("element_at(MAP(ARRAY [ARRAY [1]], ARRAY [1]), ARRAY [2])", INTEGER, null);
    // null value associated with the requested key
    assertFunction("element_at(MAP(ARRAY [1], ARRAY [null]), 1)", UNKNOWN, null);
    assertFunction("element_at(MAP(ARRAY [1.0E0], ARRAY [null]), 1.0E0)", UNKNOWN, null);
    assertFunction("element_at(MAP(ARRAY [TRUE], ARRAY [null]), TRUE)", UNKNOWN, null);
    assertFunction("element_at(MAP(ARRAY ['puppies'], ARRAY [null]), 'puppies')", UNKNOWN, null);
    assertFunction("element_at(MAP(ARRAY [ARRAY [1]], ARRAY [null]), ARRAY [1])", UNKNOWN, null);
    // general tests
    assertFunction("element_at(MAP(ARRAY [1, 3], ARRAY [2, 4]), 3)", INTEGER, 4);
    assertFunction("element_at(MAP(ARRAY [BIGINT '1', 3], ARRAY [BIGINT '2', 4]), 3)", BIGINT, 4L);
    assertFunction("element_at(MAP(ARRAY [1, 3], ARRAY [2, NULL]), 3)", INTEGER, null);
    assertFunction("element_at(MAP(ARRAY [BIGINT '1', 3], ARRAY [2, NULL]), 3)", INTEGER, null);
    assertFunction("element_at(MAP(ARRAY [1, 3], ARRAY [2.0E0, 4.0E0]), 1)", DOUBLE, 2.0);
    assertFunction("element_at(MAP(ARRAY [1.0E0, 2.0E0], ARRAY [ARRAY [1, 2], ARRAY [3]]), 1.0E0)", new ArrayType(INTEGER), ImmutableList.of(1, 2));
    assertFunction("element_at(MAP(ARRAY ['puppies'], ARRAY ['kittens']), 'puppies')", createVarcharType(7), "kittens");
    assertFunction("element_at(MAP(ARRAY [TRUE, FALSE], ARRAY [2, 4]), TRUE)", INTEGER, 2);
    assertFunction("element_at(MAP(ARRAY [ARRAY [1, 2], ARRAY [3]], ARRAY [1e0, 2e0]), ARRAY [1, 2])", DOUBLE, 1.0);
    assertFunction("element_at(MAP(ARRAY ['1', '100'], ARRAY [TIMESTAMP '1970-01-01 00:00:01', TIMESTAMP '2005-09-10 13:00:00']), '1')", TIMESTAMP, sqlTimestampOf(1970, 1, 1, 0, 0, 1, 0));
    assertFunction("element_at(MAP(ARRAY [from_unixtime(1), from_unixtime(100)], ARRAY [1.0E0, 100.0E0]), from_unixtime(1))", DOUBLE, 1.0);
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) Test(org.testng.annotations.Test)

Example 4 with ArrayType

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

the class TestMapOperators method testSubscript.

@Test
public void testSubscript() {
    assertFunction("MAP(ARRAY [1], ARRAY [null])[1]", UNKNOWN, null);
    assertFunction("MAP(ARRAY [1.0E0], ARRAY [null])[1.0E0]", UNKNOWN, null);
    assertFunction("MAP(ARRAY [TRUE], ARRAY [null])[TRUE]", UNKNOWN, null);
    assertFunction("MAP(ARRAY['puppies'], ARRAY [null])['puppies']", UNKNOWN, null);
    assertInvalidFunction("MAP(ARRAY [CAST(null as bigint)], ARRAY [1])", "map key cannot be null");
    assertInvalidFunction("MAP(ARRAY [CAST(null as bigint)], ARRAY [CAST(null as bigint)])", "map key cannot be null");
    assertInvalidFunction("MAP(ARRAY [1,null], ARRAY [null,2])", "map key cannot be null");
    assertFunction("MAP(ARRAY [1, 3], ARRAY [2, 4])[3]", INTEGER, 4);
    assertFunction("MAP(ARRAY [BIGINT '1', 3], ARRAY [BIGINT '2', 4])[3]", BIGINT, 4L);
    assertFunction("MAP(ARRAY [1, 3], ARRAY[2, NULL])[3]", INTEGER, null);
    assertFunction("MAP(ARRAY [BIGINT '1', 3], ARRAY[2, NULL])[3]", INTEGER, null);
    assertFunction("MAP(ARRAY [1, 3], ARRAY [2.0E0, 4.0E0])[1]", DOUBLE, 2.0);
    assertFunction("MAP(ARRAY[1.0E0, 2.0E0], ARRAY[ ARRAY[1, 2], ARRAY[3]])[1.0E0]", new ArrayType(INTEGER), ImmutableList.of(1, 2));
    assertFunction("MAP(ARRAY['puppies'], ARRAY['kittens'])['puppies']", createVarcharType(7), "kittens");
    assertFunction("MAP(ARRAY[TRUE,FALSE],ARRAY[2,4])[TRUE]", INTEGER, 2);
    assertFunction("MAP(ARRAY['1', '100'], ARRAY[TIMESTAMP '1970-01-01 00:00:01', TIMESTAMP '1973-07-08 22:00:01'])['1']", TIMESTAMP, sqlTimestampOf(1970, 1, 1, 0, 0, 1, 0));
    assertFunction("MAP(ARRAY[from_unixtime(1), from_unixtime(100)], ARRAY[1.0E0, 100.0E0])[from_unixtime(1)]", DOUBLE, 1.0);
    assertInvalidFunction("MAP(ARRAY [BIGINT '1'], ARRAY [BIGINT '2'])[3]", "Key not present in map: 3");
    assertInvalidFunction("MAP(ARRAY ['hi'], ARRAY [2])['missing']", "Key not present in map: missing");
    assertFunction("MAP(ARRAY[array[1,1]], ARRAY['a'])[ARRAY[1,1]]", createVarcharType(1), "a");
    assertFunction("MAP(ARRAY[('a', 'b')], ARRAY[ARRAY[100, 200]])[('a', 'b')]", new ArrayType(INTEGER), ImmutableList.of(100, 200));
    assertFunction("MAP(ARRAY[1.0], ARRAY [2.2])[1.0]", createDecimalType(2, 1), decimal("2.2"));
    assertFunction("MAP(ARRAY[000000000000001.00000000000000], ARRAY [2.2])[000000000000001.00000000000000]", createDecimalType(2, 1), decimal("2.2"));
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) Test(org.testng.annotations.Test)

Example 5 with ArrayType

use of io.prestosql.spi.type.ArrayType 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)

Aggregations

ArrayType (io.prestosql.spi.type.ArrayType)187 Test (org.testng.annotations.Test)136 Type (io.prestosql.spi.type.Type)63 RowType (io.prestosql.spi.type.RowType)53 ImmutableList (com.google.common.collect.ImmutableList)45 List (java.util.List)44 ArrayList (java.util.ArrayList)40 Arrays.asList (java.util.Arrays.asList)32 Collections.singletonList (java.util.Collections.singletonList)32 MessageType (org.apache.parquet.schema.MessageType)29 MessageTypeParser.parseMessageType (org.apache.parquet.schema.MessageTypeParser.parseMessageType)27 VarcharType.createUnboundedVarcharType (io.prestosql.spi.type.VarcharType.createUnboundedVarcharType)26 DecimalType.createDecimalType (io.prestosql.spi.type.DecimalType.createDecimalType)25 StructuralTestUtil.mapType (io.prestosql.tests.StructuralTestUtil.mapType)24 MapType (io.prestosql.spi.type.MapType)20 BlockBuilder (io.prestosql.spi.block.BlockBuilder)18 Block (io.prestosql.spi.block.Block)14 VarcharType (io.prestosql.spi.type.VarcharType)14 BigInteger (java.math.BigInteger)13 Map (java.util.Map)12