Search in sources :

Example 6 with ArrayType

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

the class TestRowOperators method testFieldAccessor.

@Test
public void testFieldAccessor() {
    assertFunction("CAST(row(1, CAST(NULL AS DOUBLE)) AS ROW(col0 integer, col1 double)).col1", DOUBLE, null);
    assertFunction("CAST(row(TRUE, CAST(NULL AS BOOLEAN)) AS ROW(col0 boolean, col1 boolean)).col1", BOOLEAN, null);
    assertFunction("CAST(row(TRUE, CAST(NULL AS ARRAY<INTEGER>)) AS ROW(col0 boolean, col1 array(integer))).col1", new ArrayType(INTEGER), null);
    assertFunction("CAST(row(1.0E0, CAST(NULL AS VARCHAR)) AS ROW(col0 double, col1 varchar)).col1", createUnboundedVarcharType(), null);
    assertFunction("CAST(row(1, 2) AS ROW(col0 integer, col1 integer)).col0", INTEGER, 1);
    assertFunction("CAST(row(1, 'kittens') AS ROW(col0 integer, col1 varchar)).col1", createUnboundedVarcharType(), "kittens");
    assertFunction("CAST(row(1, 2) AS ROW(col0 integer, col1 integer)).\"col1\"", INTEGER, 2);
    assertFunction("CAST(array[row(1, 2)] AS array(row(col0 integer, col1 integer)))[1].col1", INTEGER, 2);
    assertFunction("CAST(row(FALSE, ARRAY [1, 2], MAP(ARRAY[1, 3], ARRAY[2.0E0, 4.0E0])) AS ROW(col0 boolean , col1 array(integer), col2 map(integer, double))).col1", new ArrayType(INTEGER), ImmutableList.of(1, 2));
    assertFunction("CAST(row(FALSE, ARRAY [1, 2], MAP(ARRAY[1, 3], ARRAY[2.0E0, 4.0E0])) AS ROW(col0 boolean , col1 array(integer), col2 map(integer, double))).col2", mapType(INTEGER, DOUBLE), ImmutableMap.of(1, 2.0, 3, 4.0));
    assertFunction("CAST(row(1.0E0, ARRAY[row(31, 4.1E0), row(32, 4.2E0)], row(3, 4.0E0)) AS ROW(col0 double, col1 array(row(col0 integer, col1 double)), col2 row(col0 integer, col1 double))).col1[2].col0", INTEGER, 32);
    // Using ROW constructor
    assertFunction("CAST(ROW(1, 2) AS ROW(a BIGINT, b DOUBLE)).a", BIGINT, 1L);
    assertFunction("CAST(ROW(1, 2) AS ROW(a BIGINT, b DOUBLE)).b", DOUBLE, 2.0);
    assertFunction("CAST(ROW(CAST(ROW('aa') AS ROW(a VARCHAR))) AS ROW(a ROW(a VARCHAR))).a.a", createUnboundedVarcharType(), "aa");
    assertFunction("CAST(ROW(ROW('ab')) AS ROW(a ROW(b VARCHAR))).a.b", VARCHAR, "ab");
    assertFunction("CAST(ROW(ARRAY[NULL]) AS ROW(a ARRAY(BIGINT))).a", new ArrayType(BIGINT), asList((Integer) null));
    // Row type is not case sensitive
    assertFunction("CAST(ROW(1) AS ROW(A BIGINT)).A", BIGINT, 1L);
    assertDecimalFunction("CAST(row(1.0, 123123123456.6549876543) AS ROW(col0 decimal(2,1), col1 decimal(22,10))).col0", decimal("1.0"));
    assertDecimalFunction("CAST(row(1.0, 123123123456.6549876543) AS ROW(col0 decimal(2,1), col1 decimal(22,10))).col1", decimal("123123123456.6549876543"));
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) Test(org.testng.annotations.Test)

Example 7 with ArrayType

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

the class TestJsonOperators method testCastWithJsonParse.

@Test
public void testCastWithJsonParse() {
    // the test is to make sure ExpressionOptimizer works with cast + json_parse
    assertCastWithJsonParse("[[1,1], [2,2]]", "ARRAY<ARRAY<INTEGER>>", new ArrayType(new ArrayType(INTEGER)), ImmutableList.of(ImmutableList.of(1, 1), ImmutableList.of(2, 2)));
    assertInvalidCastWithJsonParse("[1, \"abc\"]", "ARRAY<INTEGER>", "Cannot cast to array(integer). Cannot cast 'abc' to INT\n[1, \"abc\"]");
    // Since we will not reformat the JSON string before parse and cast with the optimization,
    // these extra whitespaces in JSON string is to make sure the cast will work in such cases.
    assertCastWithJsonParse("{\"a\"\n:1,  \"b\":\t2}", "MAP<VARCHAR,INTEGER>", mapType(VARCHAR, INTEGER), ImmutableMap.of("a", 1, "b", 2));
    assertInvalidCastWithJsonParse("{\"[1, 1]\":[2, 2]}", "MAP<ARRAY<INTEGER>,ARRAY<INTEGER>>", "Cannot cast JSON to map(array(integer),array(integer))");
    assertInvalidCastWithJsonParse("{true: false, false:false}", "MAP<BOOLEAN,BOOLEAN>", "Cannot cast to map(boolean,boolean).\n{true: false, false:false}");
    assertCastWithJsonParse("{\"a\"  \n  :1,  \"b\":  \t  [2, 3]}", "ROW(a INTEGER, b ARRAY<INTEGER>)", RowType.from(ImmutableList.of(RowType.field("a", INTEGER), RowType.field("b", new ArrayType(INTEGER)))), ImmutableList.of(1, ImmutableList.of(2, 3)));
    assertCastWithJsonParse("[  1,  [2, 3]  ]", "ROW(INTEGER, ARRAY<INTEGER>)", RowType.anonymous(ImmutableList.of(INTEGER, new ArrayType(INTEGER))), ImmutableList.of(1, ImmutableList.of(2, 3)));
    assertInvalidCastWithJsonParse("{\"a\" :1,  \"b\": {} }", "ROW(a INTEGER, b ARRAY<INTEGER>)", "Cannot cast to row(a integer,b array(integer)). Expected a json array, but got {\n{\"a\" :1,  \"b\": {} }");
    assertInvalidCastWithJsonParse("[  1,  {}  ]", "ROW(INTEGER, ARRAY<INTEGER>)", "Cannot cast to row(integer,array(integer)). Expected a json array, but got {\n[  1,  {}  ]");
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) Test(org.testng.annotations.Test)

Example 8 with ArrayType

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

the class TestExpressionOptimizer 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))), Optional.empty());
    RowExpression resultExpression = optimizer.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 = optimizer.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 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 = optimizer.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)));
    // 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 = optimizer.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)));
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) IntArrayBlock(io.prestosql.spi.block.IntArrayBlock) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) RowExpression(io.prestosql.spi.relation.RowExpression) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) FunctionHandle(io.prestosql.spi.function.FunctionHandle) CallExpression(io.prestosql.spi.relation.CallExpression) Test(org.testng.annotations.Test)

Example 9 with ArrayType

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

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.prestosql.spi.type.ArrayType) UnknownType(io.prestosql.spi.type.UnknownType) MapType(io.prestosql.spi.type.MapType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) RowType(io.prestosql.spi.type.RowType) MapType(io.prestosql.spi.type.MapType)

Example 10 with ArrayType

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

the class TestArrayOperators method testElementAt.

@Test
public void testElementAt() {
    assertInvalidFunction("ELEMENT_AT(ARRAY [], 0)", "SQL array indices start at 1");
    assertInvalidFunction("ELEMENT_AT(ARRAY [1, 2, 3], 0)", "SQL array indices start at 1");
    assertFunction("ELEMENT_AT(ARRAY [], 1)", UNKNOWN, null);
    assertFunction("ELEMENT_AT(ARRAY [], -1)", UNKNOWN, null);
    assertFunction("ELEMENT_AT(ARRAY [1, 2, 3], 4)", INTEGER, null);
    assertFunction("ELEMENT_AT(ARRAY [1, 2, 3], -4)", INTEGER, null);
    assertFunction("ELEMENT_AT(ARRAY [NULL], 1)", UNKNOWN, null);
    assertFunction("ELEMENT_AT(ARRAY [NULL], -1)", UNKNOWN, null);
    assertFunction("ELEMENT_AT(ARRAY [NULL, NULL, NULL], 3)", UNKNOWN, null);
    assertFunction("ELEMENT_AT(ARRAY [NULL, NULL, NULL], -1)", UNKNOWN, null);
    assertFunction("1 + ELEMENT_AT(ARRAY [2, 1, 3], 2)", INTEGER, 2);
    assertFunction("10000000000 + ELEMENT_AT(ARRAY [2, 1, 3], -2)", BIGINT, 10000000001L);
    assertFunction("ELEMENT_AT(ARRAY [2, 1, 3], 2)", INTEGER, 1);
    assertFunction("ELEMENT_AT(ARRAY [2, 1, 3], -2)", INTEGER, 1);
    assertFunction("ELEMENT_AT(ARRAY [2, NULL, 3], 2)", INTEGER, null);
    assertFunction("ELEMENT_AT(ARRAY [2, NULL, 3], -2)", INTEGER, null);
    assertFunction("ELEMENT_AT(ARRAY [BIGINT '2', 1, 3], -2)", BIGINT, 1L);
    assertFunction("ELEMENT_AT(ARRAY [2, NULL, BIGINT '3'], -2)", BIGINT, null);
    assertFunction("ELEMENT_AT(ARRAY [1.0E0, 2.5E0, 3.5E0], 3)", DOUBLE, 3.5);
    assertFunction("ELEMENT_AT(ARRAY [1.0E0, 2.5E0, 3.5E0], -1)", DOUBLE, 3.5);
    assertFunction("ELEMENT_AT(ARRAY [ARRAY [1, 2], ARRAY [3]], 2)", new ArrayType(INTEGER), ImmutableList.of(3));
    assertFunction("ELEMENT_AT(ARRAY [ARRAY [1, 2], ARRAY [3]], -1)", new ArrayType(INTEGER), ImmutableList.of(3));
    assertFunction("ELEMENT_AT(ARRAY [ARRAY [1, 2], NULL, ARRAY [3]], 2)", new ArrayType(INTEGER), null);
    assertFunction("ELEMENT_AT(ARRAY [ARRAY [1, 2], NULL, ARRAY [3]], -2)", new ArrayType(INTEGER), null);
    assertFunction("ELEMENT_AT(ELEMENT_AT(ARRAY [ARRAY[1, 2], ARRAY [3]], 2) , 1)", INTEGER, 3);
    assertFunction("ELEMENT_AT(ELEMENT_AT(ARRAY [ARRAY[1, 2], ARRAY [3]], -1) , 1)", INTEGER, 3);
    assertFunction("ELEMENT_AT(ELEMENT_AT(ARRAY [ARRAY[1, 2], ARRAY [3]], 2) , -1)", INTEGER, 3);
    assertFunction("ELEMENT_AT(ELEMENT_AT(ARRAY [ARRAY[1, 2], ARRAY [3]], -1) , -1)", INTEGER, 3);
    assertFunction("ELEMENT_AT(ARRAY ['puppies', 'kittens'], 2)", createVarcharType(7), "kittens");
    assertFunction("ELEMENT_AT(ARRAY ['crocodiles', 'kittens'], 2)", createVarcharType(10), "kittens");
    assertFunction("ELEMENT_AT(ARRAY ['puppies', 'kittens'], -1)", createVarcharType(7), "kittens");
    assertFunction("ELEMENT_AT(ARRAY ['puppies', 'kittens', NULL], 3)", createVarcharType(7), null);
    assertFunction("ELEMENT_AT(ARRAY ['puppies', 'kittens', NULL], -1)", createVarcharType(7), null);
    assertFunction("ELEMENT_AT(ARRAY [TRUE, FALSE], 2)", BOOLEAN, false);
    assertFunction("ELEMENT_AT(ARRAY [TRUE, FALSE], -1)", BOOLEAN, false);
    assertFunction("ELEMENT_AT(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("ELEMENT_AT(ARRAY [TIMESTAMP '1970-01-01 00:00:01', TIMESTAMP '1973-07-08 22:00:01'], -2)", TIMESTAMP, sqlTimestampOf(1970, 1, 1, 0, 0, 1, 0));
    assertFunction("ELEMENT_AT(ARRAY [infinity()], 1)", DOUBLE, POSITIVE_INFINITY);
    assertFunction("ELEMENT_AT(ARRAY [infinity()], -1)", DOUBLE, POSITIVE_INFINITY);
    assertFunction("ELEMENT_AT(ARRAY [-infinity()], 1)", DOUBLE, NEGATIVE_INFINITY);
    assertFunction("ELEMENT_AT(ARRAY [-infinity()], -1)", DOUBLE, NEGATIVE_INFINITY);
    assertFunction("ELEMENT_AT(ARRAY [sqrt(-1)], 1)", DOUBLE, NaN);
    assertFunction("ELEMENT_AT(ARRAY [sqrt(-1)], -1)", DOUBLE, NaN);
    assertDecimalFunction("ELEMENT_AT(ARRAY [2.1, 2.2, 2.3], 3)", decimal("2.3"));
    assertDecimalFunction("ELEMENT_AT(ARRAY [2.111111222111111114111, 2.22222222222222222, 2.222222222222223], 3)", decimal("2.222222222222223000000"));
    assertDecimalFunction("ELEMENT_AT(ARRAY [1.9, 2, 2.3], -1)", decimal("0000000002.3"));
    assertDecimalFunction("ELEMENT_AT(ARRAY [2.22222222222222222, 2.3], -2)", decimal("2.22222222222222222"));
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) Test(org.testng.annotations.Test)

Aggregations

ArrayType (io.prestosql.spi.type.ArrayType)250 Test (org.testng.annotations.Test)182 RowType (io.prestosql.spi.type.RowType)100 Type (io.prestosql.spi.type.Type)99 List (java.util.List)86 ImmutableList (com.google.common.collect.ImmutableList)84 ArrayList (java.util.ArrayList)73 Arrays.asList (java.util.Arrays.asList)64 Collections.singletonList (java.util.Collections.singletonList)64 MessageType (org.apache.parquet.schema.MessageType)58 MessageTypeParser.parseMessageType (org.apache.parquet.schema.MessageTypeParser.parseMessageType)54 VarcharType.createUnboundedVarcharType (io.prestosql.spi.type.VarcharType.createUnboundedVarcharType)53 DecimalType.createDecimalType (io.prestosql.spi.type.DecimalType.createDecimalType)49 StructuralTestUtil.mapType (io.prestosql.tests.StructuralTestUtil.mapType)48 MapType (io.prestosql.spi.type.MapType)31 BigInteger (java.math.BigInteger)24 Map (java.util.Map)23 HashMap (java.util.HashMap)21 Block (io.prestosql.spi.block.Block)20 VarcharType (io.prestosql.spi.type.VarcharType)20