Search in sources :

Example 66 with ArrayType

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

the class TestArrayOperators method testConstructor.

@Test
public void testConstructor() {
    assertFunction("ARRAY []", new ArrayType(UNKNOWN), ImmutableList.of());
    assertFunction("ARRAY [NULL]", new ArrayType(UNKNOWN), Lists.newArrayList((Object) null));
    assertFunction("ARRAY [1, 2, 3]", new ArrayType(INTEGER), ImmutableList.of(1, 2, 3));
    assertFunction("ARRAY [1, NULL, 3]", new ArrayType(INTEGER), Lists.newArrayList(1, null, 3));
    assertFunction("ARRAY [NULL, 2, 3]", new ArrayType(INTEGER), Lists.newArrayList(null, 2, 3));
    assertFunction("ARRAY [1, 2.0E0, 3]", new ArrayType(DOUBLE), ImmutableList.of(1.0, 2.0, 3.0));
    assertFunction("ARRAY [ARRAY[1, 2], ARRAY[3]]", new ArrayType(new ArrayType(INTEGER)), ImmutableList.of(ImmutableList.of(1, 2), ImmutableList.of(3)));
    assertFunction("ARRAY [ARRAY[1, 2], NULL, ARRAY[3]]", new ArrayType(new ArrayType(INTEGER)), Lists.newArrayList(ImmutableList.of(1, 2), null, ImmutableList.of(3)));
    assertFunction("ARRAY [BIGINT '1', 2, 3]", new ArrayType(BIGINT), ImmutableList.of(1L, 2L, 3L));
    assertFunction("ARRAY [1, CAST (NULL AS BIGINT), 3]", new ArrayType(BIGINT), Lists.newArrayList(1L, null, 3L));
    assertFunction("ARRAY [NULL, 20000000000, 30000000000]", new ArrayType(BIGINT), Lists.newArrayList(null, 20000000000L, 30000000000L));
    assertFunction("ARRAY [1, 2.0E0, 3]", new ArrayType(DOUBLE), ImmutableList.of(1.0, 2.0, 3.0));
    assertFunction("ARRAY [ARRAY[1, 2], ARRAY[3]]", new ArrayType(new ArrayType(INTEGER)), ImmutableList.of(ImmutableList.of(1, 2), ImmutableList.of(3)));
    assertFunction("ARRAY [ARRAY[1, 2], NULL, ARRAY[3]]", new ArrayType(new ArrayType(INTEGER)), Lists.newArrayList(ImmutableList.of(1, 2), null, ImmutableList.of(3)));
    assertFunction("ARRAY [ARRAY[1, 2], NULL, ARRAY[BIGINT '3']]", new ArrayType(new ArrayType(BIGINT)), Lists.newArrayList(ImmutableList.of(1L, 2L), null, ImmutableList.of(3L)));
    assertFunction("ARRAY [1.0E0, 2.5E0, 3.0E0]", new ArrayType(DOUBLE), ImmutableList.of(1.0, 2.5, 3.0));
    assertFunction("ARRAY [1, 2.5E0, 3]", new ArrayType(DOUBLE), ImmutableList.of(1.0, 2.5, 3.0));
    assertFunction("ARRAY ['puppies', 'kittens']", new ArrayType(createVarcharType(7)), ImmutableList.of("puppies", "kittens"));
    assertFunction("ARRAY [TRUE, FALSE]", new ArrayType(BOOLEAN), ImmutableList.of(true, false));
    assertFunction("ARRAY [TIMESTAMP '1970-01-01 00:00:01', TIMESTAMP '1973-07-08 22:00:01']", new ArrayType(TIMESTAMP), ImmutableList.of(sqlTimestampOf(1970, 1, 1, 0, 0, 1, 0, TEST_SESSION), sqlTimestampOf(1973, 7, 8, 22, 0, 1, 0, TEST_SESSION)));
    assertFunction("ARRAY [sqrt(-1)]", new ArrayType(DOUBLE), ImmutableList.of(NaN));
    assertFunction("ARRAY [pow(infinity(), 2)]", new ArrayType(DOUBLE), ImmutableList.of(POSITIVE_INFINITY));
    assertFunction("ARRAY [pow(-infinity(), 1)]", new ArrayType(DOUBLE), ImmutableList.of(NEGATIVE_INFINITY));
    assertFunction("ARRAY [ARRAY [], NULL]", new ArrayType(new ArrayType(UNKNOWN)), asList(ImmutableList.of(), null));
    assertFunction("ARRAY [ARRAY[1.0], ARRAY[2.0, 3.0]]", new ArrayType(new ArrayType(createDecimalType(2, 1))), asList(asList(decimal("1.0")), asList(decimal("2.0"), decimal("3.0"))));
    assertFunction("ARRAY[1.0, 2.0, 3.11]", new ArrayType(createDecimalType(3, 2)), asList(decimal("1.00"), decimal("2.00"), decimal("3.11")));
    assertFunction("ARRAY[1, 2.0, 3.11]", new ArrayType(createDecimalType(12, 2)), asList(decimal("0000000001.00"), decimal("0000000002.00"), decimal("0000000003.11")));
    assertFunction("ARRAY [ARRAY[1.0], ARRAY[2.0, 123456789123456.789]]", new ArrayType(new ArrayType(createDecimalType(18, 3))), asList(asList(decimal("000000000000001.000")), asList(decimal("000000000000002.000"), decimal("123456789123456.789"))));
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) Test(org.testng.annotations.Test)

Example 67 with ArrayType

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

the class TestArrayOperators method testJsonToArray.

@Test
public void testJsonToArray() {
    // special values
    assertFunction("CAST(CAST (null AS JSON) AS ARRAY<BIGINT>)", new ArrayType(BIGINT), null);
    assertFunction("CAST(JSON 'null' AS ARRAY<BIGINT>)", new ArrayType(BIGINT), null);
    assertFunction("CAST(JSON '[]' AS ARRAY<BIGINT>)", new ArrayType(BIGINT), ImmutableList.of());
    assertFunction("CAST(JSON '[null, null]' AS ARRAY<BIGINT>)", new ArrayType(BIGINT), Lists.newArrayList(null, null));
    // boolean
    assertFunction("CAST(JSON '[true, false, 12, 0, 12.3, 0.0, \"true\", \"false\", null]' AS ARRAY<BOOLEAN>)", new ArrayType(BOOLEAN), asList(true, false, true, false, true, false, true, false, null));
    // tinyint, smallint, integer, bigint
    assertFunction("CAST(JSON '[true, false, 12, 12.7, \"12\", null]' AS ARRAY<TINYINT>)", new ArrayType(TINYINT), asList((byte) 1, (byte) 0, (byte) 12, (byte) 13, (byte) 12, null));
    assertFunction("CAST(JSON '[true, false, 12345, 12345.6, \"12345\", null]' AS ARRAY<SMALLINT>)", new ArrayType(SMALLINT), asList((short) 1, (short) 0, (short) 12345, (short) 12346, (short) 12345, null));
    assertFunction("CAST(JSON '[true, false, 12345678, 12345678.9, \"12345678\", null]' AS ARRAY<INTEGER>)", new ArrayType(INTEGER), asList(1, 0, 12345678, 12345679, 12345678, null));
    assertFunction("CAST(JSON '[true, false, 1234567891234567, 1234567891234567.8, \"1234567891234567\", null]' AS ARRAY<BIGINT>)", new ArrayType(BIGINT), asList(1L, 0L, 1234567891234567L, 1234567891234568L, 1234567891234567L, null));
    // real, double, decimal
    assertFunction("CAST(JSON '[true, false, 12345, 12345.67, \"3.14\", \"NaN\", \"Infinity\", \"-Infinity\", null]' AS ARRAY<REAL>)", new ArrayType(REAL), asList(1.0f, 0.0f, 12345.0f, 12345.67f, 3.14f, Float.NaN, Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, null));
    assertFunction("CAST(JSON '[true, false, 1234567890, 1234567890.1, \"3.14\", \"NaN\", \"Infinity\", \"-Infinity\", null]' AS ARRAY<DOUBLE>)", new ArrayType(DOUBLE), asList(1.0, 0.0, 1234567890.0, 1234567890.1, 3.14, Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, null));
    assertFunction("CAST(JSON '[true, false, 128, 123.456, \"3.14\", null]' AS ARRAY<DECIMAL(10, 5)>)", new ArrayType(createDecimalType(10, 5)), asList(decimal("1.00000"), decimal("0.00000"), decimal("128.00000"), decimal("123.45600"), decimal("3.14000"), null));
    assertFunction("CAST(JSON '[true, false, 128, 12345678.12345678, \"3.14\", null]' AS ARRAY<DECIMAL(38, 8)>)", new ArrayType(createDecimalType(38, 8)), asList(decimal("1.00000000"), decimal("0.00000000"), decimal("128.00000000"), decimal("12345678.12345678"), decimal("3.14000000"), null));
    // varchar, json
    assertFunction("CAST(JSON '[true, false, 12, 12.3, \"puppies\", \"kittens\", \"null\", \"\", null]' AS ARRAY<VARCHAR>)", new ArrayType(VARCHAR), asList("true", "false", "12", "12.3", "puppies", "kittens", "null", "", null));
    assertFunction("CAST(JSON '[5, 3.14, [1, 2, 3], \"e\", {\"a\": \"b\"}, null, \"null\", [null]]' AS ARRAY<JSON>)", new ArrayType(JSON), ImmutableList.of("5", "3.14", "[1,2,3]", "\"e\"", "{\"a\":\"b\"}", "null", "\"null\"", "[null]"));
    // nested array/map
    assertFunction("CAST(JSON '[[1, 2], [3, null], [], [null, null], null]' AS ARRAY<ARRAY<BIGINT>>)", new ArrayType(new ArrayType(BIGINT)), asList(asList(1L, 2L), asList(3L, null), emptyList(), asList(null, null), null));
    assertFunction("CAST(JSON '[" + "{\"a\": 1, \"b\": 2}, " + "{\"none\": null, \"three\": 3}, " + "{}, " + "{\"h1\": null,\"h2\": null}, " + "null]' " + "AS ARRAY<MAP<VARCHAR, BIGINT>>)", new ArrayType(mapType(VARCHAR, BIGINT)), asList(ImmutableMap.of("a", 1L, "b", 2L), asMap(ImmutableList.of("none", "three"), asList(null, 3L)), ImmutableMap.of(), asMap(ImmutableList.of("h1", "h2"), asList(null, null)), null));
    assertFunction("CAST(JSON '[" + "[1, \"two\"], " + "[3, null], " + "{\"k1\": 1, \"k2\": \"two\"}, " + "{\"k2\": null, \"k1\": 3}, " + "null]' " + "AS ARRAY<ROW(k1 BIGINT, k2 VARCHAR)>)", new ArrayType(RowType.from(ImmutableList.of(RowType.field("k1", BIGINT), RowType.field("k2", VARCHAR)))), asList(asList(1L, "two"), asList(3L, null), asList(1L, "two"), asList(3L, null), null));
    // invalid cast
    assertInvalidCast("CAST(JSON '{\"a\": 1}' AS ARRAY<BIGINT>)", "Cannot cast to array(bigint). Expected a json array, but got {\n{\"a\":1}");
    assertInvalidCast("CAST(JSON '[1, 2, 3]' AS ARRAY<ARRAY<BIGINT>>)", "Cannot cast to array(array(bigint)). Expected a json array, but got 1\n[1,2,3]");
    assertInvalidCast("CAST(JSON '[1, {}]' AS ARRAY<BIGINT>)", "Cannot cast to array(bigint). Unexpected token when cast to bigint: {\n[1,{}]");
    assertInvalidCast("CAST(JSON '[[1], {}]' AS ARRAY<ARRAY<BIGINT>>)", "Cannot cast to array(array(bigint)). Expected a json array, but got {\n[[1],{}]");
    assertInvalidCast("CAST(unchecked_to_json('1, 2, 3') AS ARRAY<BIGINT>)", "Cannot cast to array(bigint).\n1, 2, 3");
    assertInvalidCast("CAST(unchecked_to_json('[1] 2') AS ARRAY<BIGINT>)", "Cannot cast to array(bigint). Unexpected trailing token: 2\n[1] 2");
    assertInvalidCast("CAST(unchecked_to_json('[1, 2, 3') AS ARRAY<BIGINT>)", "Cannot cast to array(bigint).\n[1, 2, 3");
    assertInvalidCast("CAST(JSON '[\"a\", \"b\"]' AS ARRAY<BIGINT>)", "Cannot cast to array(bigint). Cannot cast 'a' to BIGINT\n[\"a\",\"b\"]");
    assertInvalidCast("CAST(JSON '[1234567890123.456]' AS ARRAY<INTEGER>)", "Cannot cast to array(integer). Out of range for integer: 1.234567890123456E12\n[1.234567890123456E12]");
    assertFunction("CAST(JSON '[1, 2.0, 3]' AS ARRAY(DECIMAL(10,5)))", new ArrayType(createDecimalType(10, 5)), ImmutableList.of(decimal("1.00000"), decimal("2.00000"), decimal("3.00000")));
    assertFunction("CAST(CAST(ARRAY [1, 2.0, 3] as JSON) AS ARRAY(DECIMAL(10,5)))", new ArrayType(createDecimalType(10, 5)), ImmutableList.of(decimal("1.00000"), decimal("2.00000"), decimal("3.00000")));
    assertFunction("CAST(CAST(ARRAY [123456789012345678901234567890.12345678, 1.2] as JSON) AS ARRAY(DECIMAL(38,8)))", new ArrayType(createDecimalType(38, 8)), ImmutableList.of(decimal("123456789012345678901234567890.12345678"), decimal("1.20000000")));
    assertFunction("CAST(CAST(ARRAY [12345.87654] as JSON) AS ARRAY(DECIMAL(7,2)))", new ArrayType(createDecimalType(7, 2)), ImmutableList.of(decimal("12345.88")));
    assertInvalidCast("CAST(CAST(ARRAY [12345.12345] as JSON) AS ARRAY(DECIMAL(6,2)))");
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) Test(org.testng.annotations.Test)

Example 68 with ArrayType

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

the class TestArrayOperators method testSubscript.

@Test
public void testSubscript() {
    String outOfBounds = "Array subscript out of bounds";
    String negativeIndex = "Array subscript is negative";
    String indexIsZero = "SQL array indices start at 1";
    assertInvalidFunction("ARRAY [][1]", outOfBounds);
    assertInvalidFunction("ARRAY [null][-1]", negativeIndex);
    assertInvalidFunction("ARRAY [1, 2, 3][0]", indexIsZero);
    assertInvalidFunction("ARRAY [1, 2, 3][-1]", negativeIndex);
    assertInvalidFunction("ARRAY [1, 2, 3][4]", outOfBounds);
    try {
        assertFunction("ARRAY [1, 2, 3][1.1E0]", BIGINT, null);
        fail("Access to array with double subscript should fail");
    } catch (SemanticException e) {
        assertTrue(e.getCode() == TYPE_MISMATCH);
    }
    assertFunction("ARRAY[NULL][1]", UNKNOWN, null);
    assertFunction("ARRAY[NULL, NULL, NULL][3]", UNKNOWN, null);
    assertFunction("1 + ARRAY [2, 1, 3][2]", INTEGER, 2);
    assertFunction("ARRAY [2, 1, 3][2]", INTEGER, 1);
    assertFunction("ARRAY [2, NULL, 3][2]", INTEGER, null);
    assertFunction("ARRAY [1.0E0, 2.5E0, 3.5E0][3]", DOUBLE, 3.5);
    assertFunction("ARRAY [ARRAY[1, 2], ARRAY[3]][2]", new ArrayType(INTEGER), ImmutableList.of(3));
    assertFunction("ARRAY [ARRAY[1, 2], NULL, ARRAY[3]][2]", new ArrayType(INTEGER), null);
    assertFunction("ARRAY [ARRAY[1, 2], ARRAY[3]][2][1]", INTEGER, 3);
    assertFunction("ARRAY ['puppies', 'kittens'][2]", createVarcharType(7), "kittens");
    assertFunction("ARRAY ['puppies', 'kittens', NULL][3]", createVarcharType(7), null);
    assertFunction("ARRAY [TRUE, FALSE][2]", BOOLEAN, false);
    assertFunction("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("ARRAY [infinity()][1]", DOUBLE, POSITIVE_INFINITY);
    assertFunction("ARRAY [-infinity()][1]", DOUBLE, NEGATIVE_INFINITY);
    assertFunction("ARRAY [sqrt(-1)][1]", DOUBLE, NaN);
    assertDecimalFunction("ARRAY [2.1, 2.2, 2.3][3]", decimal("2.3"));
    assertDecimalFunction("ARRAY [2.111111222111111114111, 2.22222222222222222, 2.222222222222223][3]", decimal("2.222222222222223000000"));
    assertDecimalFunction("ARRAY [1.9, 2, 2.3][3]", decimal("0000000002.3"));
    assertDecimalFunction("ARRAY [2.22222222222222222, 2.3][1]", decimal("2.22222222222222222"));
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) SemanticException(com.facebook.presto.sql.analyzer.SemanticException) Test(org.testng.annotations.Test)

Example 69 with ArrayType

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

the class TestArrayOperators method assertArrayHashOperator.

private void assertArrayHashOperator(String inputArray, Type elementType, List<Object> elements) {
    ArrayType arrayType = new ArrayType(elementType);
    BlockBuilder arrayArrayBuilder = arrayType.createBlockBuilder(null, 1);
    BlockBuilder arrayBuilder = elementType.createBlockBuilder(null, elements.size());
    for (Object element : elements) {
        appendToBlockBuilder(elementType, element, arrayBuilder);
    }
    arrayType.writeObject(arrayArrayBuilder, arrayBuilder.build());
    assertOperator(HASH_CODE, inputArray, BIGINT, arrayType.hash(arrayArrayBuilder.build(), 0));
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) StructuralTestUtil.appendToBlockBuilder(com.facebook.presto.util.StructuralTestUtil.appendToBlockBuilder)

Example 70 with ArrayType

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

the class TestArrayOperators method testElementArrayConcat.

@Test
public void testElementArrayConcat() {
    assertFunction("CAST (ARRAY [DATE '2001-08-22'] || DATE '2001-08-23' AS JSON)", JSON, "[\"2001-08-22\",\"2001-08-23\"]");
    assertFunction("CAST (DATE '2001-08-23' || ARRAY [DATE '2001-08-22'] AS JSON)", JSON, "[\"2001-08-23\",\"2001-08-22\"]");
    assertFunction("1 || ARRAY [2]", new ArrayType(INTEGER), Lists.newArrayList(1, 2));
    assertFunction("ARRAY [2] || 1", new ArrayType(INTEGER), Lists.newArrayList(2, 1));
    assertFunction("ARRAY [2] || BIGINT '1'", new ArrayType(BIGINT), Lists.newArrayList(2L, 1L));
    assertFunction("TRUE || ARRAY [FALSE]", new ArrayType(BOOLEAN), Lists.newArrayList(true, false));
    assertFunction("ARRAY [FALSE] || TRUE", new ArrayType(BOOLEAN), Lists.newArrayList(false, true));
    assertFunction("1.0E0 || ARRAY [2.0E0]", new ArrayType(DOUBLE), Lists.newArrayList(1.0, 2.0));
    assertFunction("ARRAY [2.0E0] || 1.0E0", new ArrayType(DOUBLE), Lists.newArrayList(2.0, 1.0));
    assertFunction("'puppies' || ARRAY ['kittens']", new ArrayType(createVarcharType(7)), Lists.newArrayList("puppies", "kittens"));
    assertFunction("ARRAY ['kittens'] || 'puppies'", new ArrayType(createVarcharType(7)), Lists.newArrayList("kittens", "puppies"));
    assertFunction("ARRAY [TIMESTAMP '1970-01-01 00:00:01'] || TIMESTAMP '1973-07-08 22:00:01'", new ArrayType(TIMESTAMP), ImmutableList.of(sqlTimestampOf(1970, 1, 1, 0, 0, 1, 0, TEST_SESSION), sqlTimestampOf(1973, 7, 8, 22, 0, 1, 0, TEST_SESSION)));
    assertFunction("TIMESTAMP '1973-07-08 22:00:01' || ARRAY [TIMESTAMP '1970-01-01 00:00:01']", new ArrayType(TIMESTAMP), ImmutableList.of(sqlTimestampOf(1973, 7, 8, 22, 0, 1, 0, TEST_SESSION), sqlTimestampOf(1970, 1, 1, 0, 0, 1, 0, TEST_SESSION)));
    assertFunction("ARRAY [2, 8] || ARRAY[ARRAY[3, 6], ARRAY[4]]", new ArrayType(new ArrayType(INTEGER)), ImmutableList.of(ImmutableList.of(2, 8), ImmutableList.of(3, 6), ImmutableList.of(4)));
    assertFunction("ARRAY [ARRAY [1], ARRAY [2, 8]] || ARRAY [3, 6]", new ArrayType(new ArrayType(INTEGER)), ImmutableList.of(ImmutableList.of(1), ImmutableList.of(2, 8), ImmutableList.of(3, 6)));
    assertFunction("ARRAY [2.0, 3.11] || 1.0", new ArrayType(createDecimalType(3, 2)), asList(decimal("2.00"), decimal("3.11"), decimal("1.00")));
    assertFunction("ARRAY[1.0] || 2.0 || 123456789123456.789", new ArrayType(createDecimalType(18, 3)), asList(decimal("000000000000001.000"), decimal("000000000000002.000"), decimal("123456789123456.789")));
    try {
        assertFunction("ARRAY [ARRAY[1]] || ARRAY ['x']", new ArrayType(new ArrayType(INTEGER)), null);
        fail("arrays must be of the same type");
    } catch (RuntimeException e) {
    // Expected
    }
    assertCachedInstanceHasBoundedRetainedSize("ARRAY [1, NULL] || 3");
    assertCachedInstanceHasBoundedRetainedSize("3 || ARRAY [1, NULL]");
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) Test(org.testng.annotations.Test)

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