Search in sources :

Example 76 with ArrayType

use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.

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(com.facebook.presto.common.type.ArrayType) Test(org.testng.annotations.Test)

Example 77 with ArrayType

use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.

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"));
    // Legacy anonymous row field ordinal access
    legacyRowFieldOrdinalAccess.assertFunction("row(1, CAST(NULL AS DOUBLE)).field1", DOUBLE, null);
    legacyRowFieldOrdinalAccess.assertFunction("row(TRUE, CAST(NULL AS BOOLEAN)).field1", BOOLEAN, null);
    legacyRowFieldOrdinalAccess.assertFunction("row(TRUE, CAST(NULL AS ARRAY<INTEGER>)).field1", new ArrayType(INTEGER), null);
    legacyRowFieldOrdinalAccess.assertFunction("row(1.0E0, CAST(NULL AS VARCHAR)).field1", createUnboundedVarcharType(), null);
    legacyRowFieldOrdinalAccess.assertFunction("row(1, 2).field0", INTEGER, 1);
    legacyRowFieldOrdinalAccess.assertFunction("row(1, 'kittens').field1", createVarcharType(7), "kittens");
    legacyRowFieldOrdinalAccess.assertFunction("row(1, 2).\"field1\"", INTEGER, 2);
    legacyRowFieldOrdinalAccess.assertFunction("array[row(1, 2)][1].field1", INTEGER, 2);
    legacyRowFieldOrdinalAccess.assertFunction("row(FALSE, ARRAY [1, 2], MAP(ARRAY[1, 3], ARRAY[2.0E0, 4.0E0])).field1", new ArrayType(INTEGER), ImmutableList.of(1, 2));
    legacyRowFieldOrdinalAccess.assertFunction("row(FALSE, ARRAY [1, 2], MAP(ARRAY[1, 3], ARRAY[2.0E0, 4.0E0])).field2", mapType(INTEGER, DOUBLE), ImmutableMap.of(1, 2.0, 3, 4.0));
    legacyRowFieldOrdinalAccess.assertFunction("row(1.0E0, ARRAY[row(31, 4.1E0), row(32, 4.2E0)], row(3, 4.0E0)).field1[2].field0", INTEGER, 32);
    legacyRowFieldOrdinalAccess.assertFunction("row(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11).field10", INTEGER, 11);
    legacyRowFieldOrdinalAccess.assertInvalidFunction("CAST(row(1, 2) as ROW(col0 integer, col1 integer)).field1", MISSING_ATTRIBUTE);
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) Test(org.testng.annotations.Test)

Example 78 with ArrayType

use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.

the class TestMapOperators method testMapValues.

@Test
public void testMapValues() {
    assertFunction("MAP_VALUES(MAP(ARRAY['1'], ARRAY[ARRAY[TRUE, FALSE, NULL]]))", new ArrayType(new ArrayType(BOOLEAN)), ImmutableList.of(Lists.newArrayList(true, false, null)));
    assertFunction("MAP_VALUES(MAP(ARRAY['1'], ARRAY[ARRAY[ARRAY[1, 2]]]))", new ArrayType(new ArrayType(new ArrayType(INTEGER))), ImmutableList.of(ImmutableList.of(ImmutableList.of(1, 2))));
    assertFunction("MAP_VALUES(MAP(ARRAY [1, 3], ARRAY ['2', '4']))", new ArrayType(createVarcharType(1)), ImmutableList.of("2", "4"));
    assertFunction("MAP_VALUES(MAP(ARRAY[1.0E0,2.0E0], ARRAY[ARRAY[1, 2], ARRAY[3]]))", new ArrayType(new ArrayType(INTEGER)), ImmutableList.of(ImmutableList.of(1, 2), ImmutableList.of(3)));
    assertFunction("MAP_VALUES(MAP(ARRAY['puppies'], ARRAY['kittens']))", new ArrayType(createVarcharType(7)), ImmutableList.of("kittens"));
    assertFunction("MAP_VALUES(MAP(ARRAY[TRUE], ARRAY[2]))", new ArrayType(INTEGER), ImmutableList.of(2));
    assertFunction("MAP_VALUES(MAP(ARRAY['1'], ARRAY[NULL]))", new ArrayType(UNKNOWN), Lists.newArrayList((Object) null));
    assertFunction("MAP_VALUES(MAP(ARRAY['1'], ARRAY[TRUE]))", new ArrayType(BOOLEAN), ImmutableList.of(true));
    assertFunction("MAP_VALUES(MAP(ARRAY['1'], ARRAY[1.0E0]))", new ArrayType(DOUBLE), ImmutableList.of(1.0));
    assertFunction("MAP_VALUES(MAP(ARRAY['1', '2'], ARRAY[ARRAY[1.0E0, 2.0E0], ARRAY[3.0E0, 4.0E0]]))", new ArrayType(new ArrayType(DOUBLE)), ImmutableList.of(ImmutableList.of(1.0, 2.0), ImmutableList.of(3.0, 4.0)));
    assertFunction("MAP_VALUES(MAP(ARRAY [1.0, 383838383838383.12324234234234], ARRAY [2.2, 3.3]))", new ArrayType(createDecimalType(2, 1)), ImmutableList.of(decimal("2.2"), decimal("3.3")));
    assertFunction("MAP_VALUES(MAP(ARRAY [1.0, 2.01], ARRAY [383838383838383.12324234234234, 3.3]))", new ArrayType(createDecimalType(29, 14)), ImmutableList.of(decimal("383838383838383.12324234234234"), decimal("000000000000003.30000000000000")));
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) Test(org.testng.annotations.Test)

Example 79 with ArrayType

use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.

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, TEST_SESSION));
    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"));
    assertInvalidFunction("MAP(ARRAY[cast('1' as varbinary)], ARRAY[null])[cast('2' as varbinary)]", "Key not present in map");
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) Test(org.testng.annotations.Test)

Example 80 with ArrayType

use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.

the class AbstractMinMaxByNAggregationFunction method output.

public static void output(ArrayType outputType, MinMaxByNState state, BlockBuilder out) {
    TypedKeyValueHeap heap = state.getTypedKeyValueHeap();
    if (heap == null || heap.isEmpty()) {
        out.appendNull();
        return;
    }
    Type elementType = outputType.getElementType();
    BlockBuilder arrayBlockBuilder = out.beginBlockEntry();
    BlockBuilder reversedBlockBuilder = elementType.createBlockBuilder(null, heap.getCapacity());
    long startSize = heap.getEstimatedSize();
    heap.popAll(reversedBlockBuilder);
    state.addMemoryUsage(heap.getEstimatedSize() - startSize);
    for (int i = reversedBlockBuilder.getPositionCount() - 1; i >= 0; i--) {
        elementType.appendTo(reversedBlockBuilder, i, arrayBlockBuilder);
    }
    out.closeEntry();
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) TypedKeyValueHeap(com.facebook.presto.operator.aggregation.TypedKeyValueHeap) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Aggregations

ArrayType (com.facebook.presto.common.type.ArrayType)287 Test (org.testng.annotations.Test)219 Type (com.facebook.presto.common.type.Type)99 RowType (com.facebook.presto.common.type.RowType)79 ArrayList (java.util.ArrayList)58 ImmutableList (com.google.common.collect.ImmutableList)54 List (java.util.List)51 MapType (com.facebook.presto.common.type.MapType)39 DecimalType.createDecimalType (com.facebook.presto.common.type.DecimalType.createDecimalType)36 Arrays.asList (java.util.Arrays.asList)34 Collections.singletonList (java.util.Collections.singletonList)33 MessageType (org.apache.parquet.schema.MessageType)30 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)28 VarcharType.createUnboundedVarcharType (com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType)27 MessageTypeParser.parseMessageType (org.apache.parquet.schema.MessageTypeParser.parseMessageType)27 InternalAggregationFunction (com.facebook.presto.operator.aggregation.InternalAggregationFunction)26 PrimitiveType (org.apache.parquet.schema.PrimitiveType)26 StructuralTestUtil.mapType (com.facebook.presto.tests.StructuralTestUtil.mapType)24 Block (com.facebook.presto.common.block.Block)23 DecimalType (com.facebook.presto.common.type.DecimalType)17