use of io.trino.spi.type.ArrayType in project trino by trinodb.
the class TestArrayTransformFunction method testBasic.
@Test
public void testBasic() {
assertFunction("transform(ARRAY [5, 6], x -> 9)", new ArrayType(INTEGER), ImmutableList.of(9, 9));
assertFunction("transform(ARRAY [5, 6], x -> x + 1)", new ArrayType(INTEGER), ImmutableList.of(6, 7));
assertFunction("transform(ARRAY [5 + RANDOM(1), 6], x -> x + 1)", new ArrayType(INTEGER), ImmutableList.of(6, 7));
}
use of io.trino.spi.type.ArrayType in project trino by trinodb.
the class TestArrayTransformFunction method testTypeCombinations.
@Test
public void testTypeCombinations() {
assertFunction("transform(ARRAY [25, 26], x -> x + 1)", new ArrayType(INTEGER), ImmutableList.of(26, 27));
assertFunction("transform(ARRAY [25, 26], x -> x + 1.0E0)", new ArrayType(DOUBLE), ImmutableList.of(26.0, 27.0));
assertFunction("transform(ARRAY [25, 26], x -> x = 25)", new ArrayType(BOOLEAN), ImmutableList.of(true, false));
assertFunction("transform(ARRAY [25, 26], x -> to_base(x, 16))", new ArrayType(createVarcharType(64)), ImmutableList.of("19", "1a"));
assertFunction("transform(ARRAY [25, 26], x -> ARRAY[x + 1])", new ArrayType(new ArrayType(INTEGER)), ImmutableList.of(ImmutableList.of(26), ImmutableList.of(27)));
assertFunction("transform(ARRAY [25.6E0, 27.3E0], x -> CAST(x AS BIGINT))", new ArrayType(BIGINT), ImmutableList.of(26L, 27L));
assertFunction("transform(ARRAY [25.6E0, 27.3E0], x -> x + 1.0E0)", new ArrayType(DOUBLE), ImmutableList.of(26.6, 28.3));
assertFunction("transform(ARRAY [25.6E0, 27.3E0], x -> x = 25.6E0)", new ArrayType(BOOLEAN), ImmutableList.of(true, false));
assertFunction("transform(ARRAY [25.6E0, 27.3E0], x -> CAST(x AS VARCHAR))", new ArrayType(createUnboundedVarcharType()), ImmutableList.of("2.56E1", "2.73E1"));
assertFunction("transform(ARRAY [25.6E0, 27.3E0], x -> MAP(ARRAY[x + 1], ARRAY[true]))", new ArrayType(mapType(DOUBLE, BOOLEAN)), ImmutableList.of(ImmutableMap.of(26.6, true), ImmutableMap.of(28.3, true)));
assertFunction("transform(ARRAY [true, false], x -> if(x, 25, 26))", new ArrayType(INTEGER), ImmutableList.of(25, 26));
assertFunction("transform(ARRAY [false, true], x -> if(x, 25.6E0, 28.9E0))", new ArrayType(DOUBLE), ImmutableList.of(28.9, 25.6));
assertFunction("transform(ARRAY [true, false], x -> not x)", new ArrayType(BOOLEAN), ImmutableList.of(false, true));
assertFunction("transform(ARRAY [false, true], x -> CAST(x AS VARCHAR))", new ArrayType(createUnboundedVarcharType()), ImmutableList.of("false", "true"));
assertFunction("transform(ARRAY [true, false], x -> ARRAY[x])", new ArrayType(new ArrayType(BOOLEAN)), ImmutableList.of(ImmutableList.of(true), ImmutableList.of(false)));
assertFunction("transform(ARRAY ['41', '42'], x -> from_base(x, 16))", new ArrayType(BIGINT), ImmutableList.of(65L, 66L));
assertFunction("transform(ARRAY ['25.6E0', '27.3E0'], x -> CAST(x AS DOUBLE))", new ArrayType(DOUBLE), ImmutableList.of(25.6, 27.3));
assertFunction("transform(ARRAY ['abc', 'def'], x -> 'abc' = x)", new ArrayType(BOOLEAN), ImmutableList.of(true, false));
assertFunction("transform(ARRAY ['abc', 'def'], x -> x || x)", new ArrayType(createUnboundedVarcharType()), ImmutableList.of("abcabc", "defdef"));
assertFunction("transform(ARRAY ['123', '456'], x -> ROW(x, CAST(x AS INTEGER), x > '3'))", new ArrayType(RowType.anonymous(ImmutableList.of(createVarcharType(3), INTEGER, BOOLEAN))), ImmutableList.of(ImmutableList.of("123", 123, false), ImmutableList.of("456", 456, true)));
assertFunction("transform(ARRAY [ARRAY ['abc', null, '123'], ARRAY ['def', 'x', '456']], x -> from_base(x[3], 10))", new ArrayType(BIGINT), ImmutableList.of(123L, 456L));
assertFunction("transform(ARRAY [ARRAY ['abc', null, '123'], ARRAY ['def', 'x', '456']], x -> CAST(x[3] AS DOUBLE))", new ArrayType(DOUBLE), ImmutableList.of(123.0, 456.0));
assertFunction("transform(ARRAY [ARRAY ['abc', null, '123'], ARRAY ['def', 'x', '456']], x -> x[2] IS NULL)", new ArrayType(BOOLEAN), ImmutableList.of(true, false));
assertFunction("transform(ARRAY [ARRAY ['abc', null, '123'], ARRAY ['def', 'x', '456']], x -> x[2])", new ArrayType(createVarcharType(3)), asList(null, "x"));
assertFunction("transform(ARRAY [MAP(ARRAY['abc', 'def'], ARRAY[123, 456]), MAP(ARRAY['ghi', 'jkl'], ARRAY[234, 567])], x -> map_keys(x))", new ArrayType(new ArrayType(createVarcharType(3))), ImmutableList.of(ImmutableList.of("abc", "def"), ImmutableList.of("ghi", "jkl")));
}
use of io.trino.spi.type.ArrayType in project trino by trinodb.
the class TestArrayTrimFunction method testTrimArray.
@Test
public void testTrimArray() {
assertFunction("trim_array(ARRAY[1, 2, 3, 4], 0)", new ArrayType(INTEGER), ImmutableList.of(1, 2, 3, 4));
assertFunction("trim_array(ARRAY[1, 2, 3, 4], 1)", new ArrayType(INTEGER), ImmutableList.of(1, 2, 3));
assertFunction("trim_array(ARRAY[1, 2, 3, 4], 2)", new ArrayType(INTEGER), ImmutableList.of(1, 2));
assertFunction("trim_array(ARRAY[1, 2, 3, 4], 3)", new ArrayType(INTEGER), ImmutableList.of(1));
assertFunction("trim_array(ARRAY[1, 2, 3, 4], 4)", new ArrayType(INTEGER), ImmutableList.of());
assertFunction("trim_array(ARRAY['a', 'b', 'c', 'd'], 1)", new ArrayType(createVarcharType(1)), ImmutableList.of("a", "b", "c"));
assertFunction("trim_array(ARRAY['a', 'b', null, 'd'], 1)", new ArrayType(createVarcharType(1)), asList("a", "b", null));
assertFunction("trim_array(ARRAY[ARRAY[1, 2, 3], ARRAY[4, 5, 6]], 1)", new ArrayType(new ArrayType(INTEGER)), ImmutableList.of(ImmutableList.of(1, 2, 3)));
assertInvalidFunction("trim_array(ARRAY[1, 2, 3, 4], 5)", "size must not exceed array cardinality 4: 5");
assertInvalidFunction("trim_array(ARRAY[1, 2, 3, 4], -1)", "size must not be negative: -1");
}
use of io.trino.spi.type.ArrayType in project trino by trinodb.
the class TestArrayCombinationsFunction method testTypeCombinations.
@Test
public void testTypeCombinations() {
assertFunction("combinations(ARRAY[1, 2, 3], 2)", new ArrayType(new ArrayType(INTEGER)), ImmutableList.of(ImmutableList.of(1, 2), ImmutableList.of(1, 3), ImmutableList.of(2, 3)));
assertFunction("combinations(ARRAY[1.1E0, 2.1E0, 3.1E0], 2)", new ArrayType(new ArrayType(DOUBLE)), ImmutableList.of(ImmutableList.of(1.1, 2.1), ImmutableList.of(1.1, 3.1), ImmutableList.of(2.1, 3.1)));
assertFunction("combinations(ARRAY[true, false, true], 2)", new ArrayType(new ArrayType(BOOLEAN)), ImmutableList.of(ImmutableList.of(true, false), ImmutableList.of(true, true), ImmutableList.of(false, true)));
assertFunction("combinations(ARRAY[ARRAY['A1', 'A2'], ARRAY['B1'], ARRAY['C1', 'C2']], 2)", new ArrayType(new ArrayType(new ArrayType(createVarcharType(2)))), ImmutableList.of(ImmutableList.of(ImmutableList.of("A1", "A2"), ImmutableList.of("B1")), ImmutableList.of(ImmutableList.of("A1", "A2"), ImmutableList.of("C1", "C2")), ImmutableList.of(ImmutableList.of("B1"), ImmutableList.of("C1", "C2"))));
assertFunction("combinations(ARRAY['\u4FE1\u5FF5\u7231', '\u5E0C\u671B', '\u671B'], 2)", new ArrayType(new ArrayType(createVarcharType(3))), ImmutableList.of(ImmutableList.of("\u4FE1\u5FF5\u7231", "\u5E0C\u671B"), ImmutableList.of("\u4FE1\u5FF5\u7231", "\u671B"), ImmutableList.of("\u5E0C\u671B", "\u671B")));
assertFunction("combinations(ARRAY[], 2)", new ArrayType(new ArrayType(UNKNOWN)), ImmutableList.of());
assertFunction("combinations(ARRAY[''], 2)", new ArrayType(new ArrayType(createVarcharType(0))), ImmutableList.of());
assertFunction("combinations(ARRAY['', ''], 2)", new ArrayType(new ArrayType(createVarcharType(0))), ImmutableList.of(ImmutableList.of("", "")));
}
use of io.trino.spi.type.ArrayType in project trino by trinodb.
the class TestArrayExceptFunction method testNull.
@Test
public void testNull() {
assertFunction("array_except(ARRAY[NULL], NULL)", new ArrayType(UNKNOWN), null);
assertFunction("array_except(NULL, NULL)", new ArrayType(UNKNOWN), null);
assertFunction("array_except(NULL, ARRAY[NULL])", new ArrayType(UNKNOWN), null);
assertFunction("array_except(ARRAY[NULL], ARRAY[NULL])", new ArrayType(UNKNOWN), ImmutableList.of());
assertFunction("array_except(ARRAY[], ARRAY[NULL])", new ArrayType(UNKNOWN), ImmutableList.of());
assertFunction("array_except(ARRAY[NULL], ARRAY[])", new ArrayType(UNKNOWN), singletonList(null));
}
Aggregations