Search in sources :

Example 36 with ArrayType

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

the class TestPushDownDereferencesRules method testPushdownDereferencesThroughUnnest.

@Test
public void testPushdownDereferencesThroughUnnest() {
    ArrayType arrayType = new ArrayType(BIGINT);
    tester().assertThat(new PushDownDereferenceThroughUnnest(tester().getTypeAnalyzer())).on(p -> p.project(Assignments.of(p.symbol("x"), expression("msg[1]")), p.unnest(ImmutableList.of(p.symbol("msg", ROW_TYPE)), ImmutableList.of(new UnnestNode.Mapping(p.symbol("arr", arrayType), ImmutableList.of(p.symbol("field")))), Optional.empty(), INNER, Optional.empty(), p.values(p.symbol("msg", ROW_TYPE), p.symbol("arr", arrayType))))).matches(strictProject(ImmutableMap.of("x", PlanMatchPattern.expression("msg_x")), unnest(strictProject(ImmutableMap.of("msg_x", PlanMatchPattern.expression("msg[1]"), "msg", PlanMatchPattern.expression("msg"), "arr", PlanMatchPattern.expression("arr")), values("msg", "arr")))));
    // Test with dereferences on unnested column
    RowType rowType = rowType(field("f1", BIGINT), field("f2", BIGINT));
    ArrayType nestedColumnType = new ArrayType(rowType(field("f1", BIGINT), field("f2", rowType)));
    tester().assertThat(new PushDownDereferenceThroughUnnest(tester().getTypeAnalyzer())).on(p -> p.project(Assignments.of(p.symbol("deref_replicate", BIGINT), expression("replicate[2]"), p.symbol("deref_unnest", BIGINT), expression("unnested_row[2]")), p.unnest(ImmutableList.of(p.symbol("replicate", rowType)), ImmutableList.of(new UnnestNode.Mapping(p.symbol("nested", nestedColumnType), ImmutableList.of(p.symbol("unnested_bigint", BIGINT), p.symbol("unnested_row", rowType)))), p.values(p.symbol("replicate", rowType), p.symbol("nested", nestedColumnType))))).matches(strictProject(ImmutableMap.of("deref_replicate", PlanMatchPattern.expression("symbol"), "deref_unnest", // not pushed down
    PlanMatchPattern.expression("unnested_row[2]")), unnest(ImmutableList.of("replicate", "symbol"), ImmutableList.of(unnestMapping("nested", ImmutableList.of("unnested_bigint", "unnested_row"))), strictProject(ImmutableMap.of("symbol", PlanMatchPattern.expression("replicate[2]"), "replicate", PlanMatchPattern.expression("replicate"), "nested", PlanMatchPattern.expression("nested")), values("replicate", "nested")))));
}
Also used : ArrayType(io.trino.spi.type.ArrayType) ROW_NUMBER(io.trino.sql.planner.plan.TopNRankingNode.RankingType.ROW_NUMBER) CATALOG_ID(io.trino.sql.planner.iterative.rule.test.RuleTester.CATALOG_ID) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) PlanMatchPattern(io.trino.sql.planner.assertions.PlanMatchPattern) Test(org.testng.annotations.Test) Collections.singletonList(java.util.Collections.singletonList) PlanMatchPattern.assignUniqueId(io.trino.sql.planner.assertions.PlanMatchPattern.assignUniqueId) PlanMatchPattern.limit(io.trino.sql.planner.assertions.PlanMatchPattern.limit) CatalogName(io.trino.connector.CatalogName) PlanMatchPattern.markDistinct(io.trino.sql.planner.assertions.PlanMatchPattern.markDistinct) TpchTableHandle(io.trino.plugin.tpch.TpchTableHandle) TEST_SESSION(io.trino.SessionTestUtils.TEST_SESSION) ASCENDING(io.trino.sql.tree.SortItem.Ordering.ASCENDING) ExpressionMatcher(io.trino.sql.planner.assertions.ExpressionMatcher) TpchColumnHandle(io.trino.plugin.tpch.TpchColumnHandle) RowType(io.trino.spi.type.RowType) ImmutableMap(com.google.common.collect.ImmutableMap) PlanMatchPattern.topNRanking(io.trino.sql.planner.assertions.PlanMatchPattern.topNRanking) Assignments(io.trino.sql.planner.plan.Assignments) ArrayType(io.trino.spi.type.ArrayType) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) SortItem(io.trino.sql.tree.SortItem) ASC_NULLS_FIRST(io.trino.spi.connector.SortOrder.ASC_NULLS_FIRST) FIRST(io.trino.sql.tree.SortItem.NullOrdering.FIRST) PlanMatchPattern.strictProject(io.trino.sql.planner.assertions.PlanMatchPattern.strictProject) BIGINT(io.trino.spi.type.BigintType.BIGINT) MetadataManager.createTestMetadataManager(io.trino.metadata.MetadataManager.createTestMetadataManager) WindowFrame(io.trino.sql.tree.WindowFrame) Optional(java.util.Optional) RowType.field(io.trino.spi.type.RowType.field) WindowNode(io.trino.sql.planner.plan.WindowNode) PlanMatchPattern.rowNumber(io.trino.sql.planner.assertions.PlanMatchPattern.rowNumber) PlanBuilder.expression(io.trino.sql.planner.iterative.rule.test.PlanBuilder.expression) PlanMatchPattern.semiJoin(io.trino.sql.planner.assertions.PlanMatchPattern.semiJoin) INNER(io.trino.sql.planner.plan.JoinNode.Type.INNER) PlanMatchPattern.window(io.trino.sql.planner.assertions.PlanMatchPattern.window) Type(io.trino.spi.type.Type) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) PlanMatchPattern.filter(io.trino.sql.planner.assertions.PlanMatchPattern.filter) ImmutableList(com.google.common.collect.ImmutableList) RowType.rowType(io.trino.spi.type.RowType.rowType) PlanMatchPattern.sort(io.trino.sql.planner.assertions.PlanMatchPattern.sort) PlanMatchPattern.join(io.trino.sql.planner.assertions.PlanMatchPattern.join) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) PlanMatchPattern.unnest(io.trino.sql.planner.assertions.PlanMatchPattern.unnest) TestingTransactionHandle(io.trino.testing.TestingTransactionHandle) PlanMatchPattern.topN(io.trino.sql.planner.assertions.PlanMatchPattern.topN) TupleDomain(io.trino.spi.predicate.TupleDomain) OrderingScheme(io.trino.sql.planner.OrderingScheme) UnnestNode(io.trino.sql.planner.plan.UnnestNode) SortOrder(io.trino.spi.connector.SortOrder) PlanMatchPattern.functionCall(io.trino.sql.planner.assertions.PlanMatchPattern.functionCall) QualifiedName(io.trino.sql.tree.QualifiedName) TableHandle(io.trino.metadata.TableHandle) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) FrameBound(io.trino.sql.tree.FrameBound) UnnestMapping.unnestMapping(io.trino.sql.planner.assertions.PlanMatchPattern.UnnestMapping.unnestMapping) PlanMatchPattern.tableScan(io.trino.sql.planner.assertions.PlanMatchPattern.tableScan) UnnestNode(io.trino.sql.planner.plan.UnnestNode) RowType(io.trino.spi.type.RowType) Test(org.testng.annotations.Test) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest)

Example 37 with ArrayType

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

the class TestRowOperators method testJsonToRow.

@Test
public void testJsonToRow() {
    // special values
    assertFunction("CAST(CAST (null AS JSON) AS ROW(BIGINT))", RowType.anonymous(ImmutableList.of(BIGINT)), null);
    assertFunction("CAST(JSON 'null' AS ROW(BIGINT))", RowType.anonymous(ImmutableList.of(BIGINT)), null);
    assertFunction("CAST(JSON '[null, null]' AS ROW(VARCHAR, BIGINT))", RowType.anonymous(ImmutableList.of(VARCHAR, BIGINT)), Lists.newArrayList(null, null));
    assertFunction("CAST(JSON '{\"k2\": null, \"k1\": null}' AS ROW(k1 VARCHAR, k2 BIGINT))", RowType.from(ImmutableList.of(RowType.field("k1", VARCHAR), RowType.field("k2", BIGINT))), Lists.newArrayList(null, null));
    // allow json object contains non-exist field names
    assertFunction("CAST(JSON '{\"k1\": [1, 2], \"used\": 3, \"k2\": [4, 5]}' AS ROW(used BIGINT))", RowType.from(ImmutableList.of(RowType.field("used", BIGINT))), ImmutableList.of(3L));
    assertFunction("CAST(JSON '[{\"k1\": [1, 2], \"used\": 3, \"k2\": [4, 5]}]' AS ARRAY(ROW(used BIGINT)))", new ArrayType(RowType.from(ImmutableList.of(RowType.field("used", BIGINT)))), ImmutableList.of(ImmutableList.of(3L)));
    // allow non-exist fields in json object
    assertFunction("CAST(JSON '{\"a\":1,\"c\":3}' AS ROW(a BIGINT, b BIGINT, c BIGINT, d BIGINT))", RowType.from(ImmutableList.of(RowType.field("a", BIGINT), RowType.field("b", BIGINT), RowType.field("c", BIGINT), RowType.field("d", BIGINT))), asList(1L, null, 3L, null));
    assertFunction("CAST(JSON '[{\"a\":1,\"c\":3}]' AS ARRAY<ROW(a BIGINT, b BIGINT, c BIGINT, d BIGINT)>)", new ArrayType(RowType.from(ImmutableList.of(RowType.field("a", BIGINT), RowType.field("b", BIGINT), RowType.field("c", BIGINT), RowType.field("d", BIGINT)))), ImmutableList.of(asList(1L, null, 3L, null)));
    // fields out of order
    assertFunction("CAST(unchecked_to_json('{\"k4\": 4, \"k2\": 2, \"k3\": 3, \"k1\": 1}') AS ROW(k1 BIGINT, k2 BIGINT, k3 BIGINT, k4 BIGINT))", RowType.from(ImmutableList.of(RowType.field("k1", BIGINT), RowType.field("k2", BIGINT), RowType.field("k3", BIGINT), RowType.field("k4", BIGINT))), ImmutableList.of(1L, 2L, 3L, 4L));
    assertFunction("CAST(unchecked_to_json('[{\"k4\": 4, \"k2\": 2, \"k3\": 3, \"k1\": 1}]') AS ARRAY(ROW(k1 BIGINT, k2 BIGINT, k3 BIGINT, k4 BIGINT)))", new ArrayType(RowType.from(ImmutableList.of(RowType.field("k1", BIGINT), RowType.field("k2", BIGINT), RowType.field("k3", BIGINT), RowType.field("k4", BIGINT)))), ImmutableList.of(ImmutableList.of(1L, 2L, 3L, 4L)));
    // boolean
    assertFunction("CAST(JSON '[true, false, 12, 0, 12.3, 0.0, \"true\", \"false\", null]' AS ROW(BOOLEAN, BOOLEAN, BOOLEAN, BOOLEAN, BOOLEAN, BOOLEAN, BOOLEAN, BOOLEAN, BOOLEAN))", RowType.anonymous(ImmutableList.of(BOOLEAN, BOOLEAN, BOOLEAN, BOOLEAN, BOOLEAN, BOOLEAN, BOOLEAN, BOOLEAN, BOOLEAN)), asList(true, false, true, false, true, false, true, false, null));
    assertFunction("CAST(JSON '{\"k1\": true, \"k2\": false, \"k3\": 12, \"k4\": 0, \"k5\": 12.3, \"k6\": 0.0, \"k7\": \"true\", \"k8\": \"false\", \"k9\": null}' AS ROW(k1 BOOLEAN, k2 BOOLEAN, k3 BOOLEAN, k4 BOOLEAN, k5 BOOLEAN, k6 BOOLEAN, k7 BOOLEAN, k8 BOOLEAN, k9 BOOLEAN))", RowType.from(ImmutableList.of(RowType.field("k1", BOOLEAN), RowType.field("k2", BOOLEAN), RowType.field("k3", BOOLEAN), RowType.field("k4", BOOLEAN), RowType.field("k5", BOOLEAN), RowType.field("k6", BOOLEAN), RowType.field("k7", BOOLEAN), RowType.field("k8", BOOLEAN), RowType.field("k9", BOOLEAN))), asList(true, false, true, false, true, false, true, false, null));
    // tinyint, smallint, integer, bigint
    assertFunction("CAST(JSON '[12,12345,123456789,1234567890123456789,null,null,null,null]' AS ROW(TINYINT, SMALLINT, INTEGER, BIGINT, TINYINT, SMALLINT, INTEGER, BIGINT))", RowType.anonymous(ImmutableList.of(TINYINT, SMALLINT, INTEGER, BIGINT, TINYINT, SMALLINT, INTEGER, BIGINT)), asList((byte) 12, (short) 12345, 123456789, 1234567890123456789L, null, null, null, null));
    assertFunction("CAST(JSON '{\"tinyint_value\": 12, \"tinyint_null\":null, " + "\"smallint_value\":12345, \"smallint_null\":null, " + " \"integer_value\":123456789, \"integer_null\": null, " + "\"bigint_value\":1234567890123456789, \"bigint_null\": null}' " + "AS ROW(tinyint_value TINYINT, smallint_value SMALLINT, integer_value INTEGER, bigint_value BIGINT, " + "tinyint_null TINYINT, smallint_null SMALLINT, integer_null INTEGER, bigint_null BIGINT))", RowType.from(ImmutableList.of(RowType.field("tinyint_value", TINYINT), RowType.field("smallint_value", SMALLINT), RowType.field("integer_value", INTEGER), RowType.field("bigint_value", BIGINT), RowType.field("tinyint_null", TINYINT), RowType.field("smallint_null", SMALLINT), RowType.field("integer_null", INTEGER), RowType.field("bigint_null", BIGINT))), asList((byte) 12, (short) 12345, 123456789, 1234567890123456789L, null, null, null, null));
    // real, double, decimal
    assertFunction("CAST(JSON '[12345.67,1234567890.1,123.456,12345678.12345678,null,null,null]' AS ROW(REAL, DOUBLE, DECIMAL(10, 5), DECIMAL(38, 8), REAL, DOUBLE, DECIMAL(7, 7)))", RowType.anonymous(ImmutableList.of(REAL, DOUBLE, createDecimalType(10, 5), createDecimalType(38, 8), REAL, DOUBLE, createDecimalType(7, 7))), asList(12345.67f, 1234567890.1, decimal("123.45600", createDecimalType(10, 5)), decimal("12345678.12345678", createDecimalType(38, 8)), null, null, null));
    assertFunction("CAST(JSON '{" + "\"real_value\": 12345.67, \"real_null\": null, " + "\"double_value\": 1234567890.1, \"double_null\": null, " + "\"decimal_value1\": 123.456, \"decimal_value2\": 12345678.12345678, \"decimal_null\": null}' " + "AS ROW(real_value REAL, double_value DOUBLE, decimal_value1 DECIMAL(10, 5), decimal_value2 DECIMAL(38, 8), " + "real_null REAL, double_null DOUBLE, decimal_null DECIMAL(7, 7)))", RowType.from(ImmutableList.of(RowType.field("real_value", REAL), RowType.field("double_value", DOUBLE), RowType.field("decimal_value1", createDecimalType(10, 5)), RowType.field("decimal_value2", createDecimalType(38, 8)), RowType.field("real_null", REAL), RowType.field("double_null", DOUBLE), RowType.field("decimal_null", createDecimalType(7, 7)))), asList(12345.67f, 1234567890.1, decimal("123.45600", createDecimalType(10, 5)), decimal("12345678.12345678", createDecimalType(38, 8)), null, null, null));
    // varchar, json
    assertFunction("CAST(JSON '[\"puppies\", [1, 2, 3], null, null]' AS ROW(VARCHAR, JSON, VARCHAR, JSON))", RowType.anonymous(ImmutableList.of(VARCHAR, JSON, VARCHAR, JSON)), asList("puppies", "[1,2,3]", null, "null"));
    assertFunction("CAST(JSON '{\"varchar_value\": \"puppies\", \"json_value\": [1, 2, 3], \"varchar_null\": null, \"json_null\": null}' " + "AS ROW(varchar_value VARCHAR, json_value JSON, varchar_null VARCHAR, json_null JSON))", RowType.from(ImmutableList.of(RowType.field("varchar_value", VARCHAR), RowType.field("json_value", JSON), RowType.field("varchar_null", VARCHAR), RowType.field("json_null", JSON))), asList("puppies", "[1,2,3]", null, "null"));
    // nested array/map/row
    assertFunction("CAST(JSON '[" + "[1, 2, null, 3], [], null, " + "{\"a\": 1, \"b\": 2, \"none\": null, \"three\": 3}, {}, null, " + "[1, 2, null, 3], null, " + "{\"a\": 1, \"b\": 2, \"none\": null, \"three\": 3}, null]' " + "AS ROW(ARRAY(BIGINT), ARRAY(BIGINT), ARRAY(BIGINT), " + "MAP(VARCHAR, BIGINT), MAP(VARCHAR, BIGINT), MAP(VARCHAR, BIGINT), " + "ROW(BIGINT, BIGINT, BIGINT, BIGINT), ROW(BIGINT)," + "ROW(a BIGINT, b BIGINT, three BIGINT, none BIGINT), ROW(nothing BIGINT)))", RowType.anonymous(ImmutableList.of(new ArrayType(BIGINT), new ArrayType(BIGINT), new ArrayType(BIGINT), mapType(VARCHAR, BIGINT), mapType(VARCHAR, BIGINT), mapType(VARCHAR, BIGINT), RowType.anonymous(ImmutableList.of(BIGINT, BIGINT, BIGINT, BIGINT)), RowType.anonymous(ImmutableList.of(BIGINT)), RowType.from(ImmutableList.of(RowType.field("a", BIGINT), RowType.field("b", BIGINT), RowType.field("three", BIGINT), RowType.field("none", BIGINT))), RowType.from(ImmutableList.of(RowType.field("nothing", BIGINT))))), asList(asList(1L, 2L, null, 3L), emptyList(), null, asMap(ImmutableList.of("a", "b", "none", "three"), asList(1L, 2L, null, 3L)), ImmutableMap.of(), null, asList(1L, 2L, null, 3L), null, asList(1L, 2L, 3L, null), null));
    assertFunction("CAST(JSON '{" + "\"array2\": [1, 2, null, 3], " + "\"array1\": [], " + "\"array3\": null, " + "\"map3\": {\"a\": 1, \"b\": 2, \"none\": null, \"three\": 3}, " + "\"map1\": {}, " + "\"map2\": null, " + "\"rowAsJsonArray1\": [1, 2, null, 3], " + "\"rowAsJsonArray2\": null, " + "\"rowAsJsonObject2\": {\"a\": 1, \"b\": 2, \"none\": null, \"three\": 3}, " + "\"rowAsJsonObject1\": null}' " + "AS ROW(array1 ARRAY(BIGINT), array2 ARRAY(BIGINT), array3 ARRAY(BIGINT), " + "map1 MAP(VARCHAR, BIGINT), map2 MAP(VARCHAR, BIGINT), map3 MAP(VARCHAR, BIGINT), " + "rowAsJsonArray1 ROW(BIGINT, BIGINT, BIGINT, BIGINT), rowAsJsonArray2 ROW(BIGINT)," + "rowAsJsonObject1 ROW(nothing BIGINT), rowAsJsonObject2 ROW(a BIGINT, b BIGINT, three BIGINT, none BIGINT)))", RowType.from(ImmutableList.of(RowType.field("array1", new ArrayType(BIGINT)), RowType.field("array2", new ArrayType(BIGINT)), RowType.field("array3", new ArrayType(BIGINT)), RowType.field("map1", mapType(VARCHAR, BIGINT)), RowType.field("map2", mapType(VARCHAR, BIGINT)), RowType.field("map3", mapType(VARCHAR, BIGINT)), RowType.field("rowasjsonarray1", RowType.anonymous(ImmutableList.of(BIGINT, BIGINT, BIGINT, BIGINT))), RowType.field("rowasjsonarray2", RowType.anonymous(ImmutableList.of(BIGINT))), RowType.field("rowasjsonobject1", RowType.from(ImmutableList.of(RowType.field("nothing", BIGINT)))), RowType.field("rowasjsonobject2", RowType.from(ImmutableList.of(RowType.field("a", BIGINT), RowType.field("b", BIGINT), RowType.field("three", BIGINT), RowType.field("none", BIGINT)))))), asList(emptyList(), asList(1L, 2L, null, 3L), null, ImmutableMap.of(), null, asMap(ImmutableList.of("a", "b", "none", "three"), asList(1L, 2L, null, 3L)), asList(1L, 2L, null, 3L), null, null, asList(1L, 2L, 3L, null)));
    // invalid cast
    assertInvalidCast("CAST(unchecked_to_json('{\"a\":1,\"b\":2,\"a\":3}') AS ROW(a BIGINT, b BIGINT))", "Cannot cast to row(a bigint, b bigint). Duplicate field: a\n{\"a\":1,\"b\":2,\"a\":3}");
    assertInvalidCast("CAST(unchecked_to_json('[{\"a\":1,\"b\":2,\"a\":3}]') AS ARRAY(ROW(a BIGINT, b BIGINT)))", "Cannot cast to array(row(a bigint, b bigint)). Duplicate field: a\n[{\"a\":1,\"b\":2,\"a\":3}]");
}
Also used : ArrayType(io.trino.spi.type.ArrayType) Test(org.testng.annotations.Test)

Example 38 with ArrayType

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

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));
    assertInvalidTypeWithJsonParse("{\"[1, 1]\":[2, 2]}", "MAP(ARRAY(INTEGER),ARRAY(INTEGER))", "line 1:8: Cannot cast json to map(array(integer), array(integer))");
    assertInvalidCastWithJsonParse("{true: false, false:false}", "MAP(BOOLEAN,BOOLEAN)", "Cannot convert value to JSON: '{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.trino.spi.type.ArrayType) Test(org.testng.annotations.Test)

Example 39 with ArrayType

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

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')", createTimestampType(0), sqlTimestampOf(0, 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);
    assertFunction("element_at(MAP(ARRAY [TIMESTAMP '2020-05-10 12:34:56.123456789', TIMESTAMP '2222-05-10 12:34:56.123456789'], ARRAY [1, 2]), TIMESTAMP '2222-05-10 12:34:56.123456789')", INTEGER, 2);
}
Also used : ArrayType(io.trino.spi.type.ArrayType) Test(org.testng.annotations.Test)

Example 40 with ArrayType

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

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']", createTimestampType(0), sqlTimestampOf(0, 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", createDecimalType(2, 1)));
    assertFunction("MAP(ARRAY[000000000000001.00000000000000], ARRAY [2.2])[000000000000001.00000000000000]", createDecimalType(2, 1), decimal("2.2", createDecimalType(2, 1)));
    assertFunction("MAP(ARRAY [TIMESTAMP '2020-05-10 12:34:56.123456789', TIMESTAMP '2222-05-10 12:34:56.123456789'], ARRAY [1, 2])[TIMESTAMP '2222-05-10 12:34:56.123456789']", INTEGER, 2);
}
Also used : ArrayType(io.trino.spi.type.ArrayType) Test(org.testng.annotations.Test)

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