@Override public Set<ColumnStatisticType> getSupportedColumnStatistics(Type type) { if (type.equals(createVarcharType(123))) { return ImmutableSet.of(MIN_VALUE, MAX_VALUE); } return ImmutableSet.of(); }
@Override public Set<ColumnStatisticType> getSupportedColumnStatistics(Type type) { if (type.equals(createVarcharType(123))) { return ImmutableSet.of(MIN_VALUE, MAX_VALUE); } return ImmutableSet.of(); }
private static VarcharType varcharType(List<String> values) { if (values.stream().anyMatch(Objects::isNull)) { return VARCHAR; } return createVarcharType(values.stream().mapToInt(String::length).max().getAsInt()); }
private static VarcharType varcharType(List<String> values) { if (values.stream().anyMatch(Objects::isNull)) { return VARCHAR; } return createVarcharType(values.stream().mapToInt(String::length).max().getAsInt()); }
@Override protected Type visitStringLiteral(StringLiteral node, StackableAstVisitorContext<Context> context) { VarcharType type = VarcharType.createVarcharType(SliceUtf8.countCodePoints(node.getSlice())); return setExpressionType(node, type); }
@Test public void testSubstring() { assertFunction("SUBSTRING('Quadratically', 5)", createVarcharType(13), "ratically"); assertFunction("SUBSTRING('Quadratically', 5, 6)", createVarcharType(13), "ratica"); }
@Test public void testVarcharToVarcharCast() { assertFunction("cast('bar' as varchar(20))", createVarcharType(20), "bar"); assertFunction("cast(cast('bar' as varchar(20)) as varchar(30))", createVarcharType(30), "bar"); assertFunction("cast(cast('bar' as varchar(20)) as varchar)", VARCHAR, "bar"); assertFunction("cast('banana' as varchar(3))", createVarcharType(3), "ban"); assertFunction("cast(cast('banana' as varchar(20)) as varchar(3))", createVarcharType(3), "ban"); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "types must be the same") public void testMismatchedTypes() { List<Type> types1 = ImmutableList.of(createVarcharType(3)); List<Type> types2 = ImmutableList.of(createVarcharType(4)); Tuple tuple1 = new Tuple(types1, ImmutableList.of("abc")); Tuple tuple2 = new Tuple(types2, ImmutableList.of("abcd")); tuple1.compareTo(tuple2); } }
private static Type getCommonSuperTypeForVarchar(VarcharType firstType, VarcharType secondType) { if (firstType.isUnbounded() || secondType.isUnbounded()) { return createUnboundedVarcharType(); } return createVarcharType(Math.max(firstType.getBoundedLength(), secondType.getBoundedLength())); }
private static Type getCommonSuperTypeForVarchar(VarcharType firstType, VarcharType secondType) { if (firstType.isUnbounded() || secondType.isUnbounded()) { return createUnboundedVarcharType(); } return createVarcharType(Math.max(firstType.getLength(), secondType.getLength())); }
@Test public void testRegexpExtract() { assertFunction("REGEXP_EXTRACT('Hello world bye', '\\b[a-z]([a-z]*)')", createVarcharType(15), "world"); assertFunction("REGEXP_EXTRACT('Hello world bye', '\\b[a-z]([a-z]*)', 1)", createVarcharType(15), "orld"); assertFunction("REGEXP_EXTRACT('rat cat\nbat dog', 'ra(.)|blah(.)(.)', 2)", createVarcharType(15), null); assertFunction("REGEXP_EXTRACT('12345', 'x')", createVarcharType(5), null); assertInvalidFunction("REGEXP_EXTRACT('Hello world bye', '\\b[a-z]([a-z]*)', -1)", INVALID_FUNCTION_ARGUMENT); assertInvalidFunction("REGEXP_EXTRACT('Hello world bye', '\\b[a-z]([a-z]*)', 2)", INVALID_FUNCTION_ARGUMENT); }
@Test public void testChr() { assertFunction("CHR(65)", createVarcharType(1), "A"); assertFunction("CHR(9731)", createVarcharType(1), "\u2603"); assertFunction("CHR(131210)", createVarcharType(1), new String(Character.toChars(131210))); assertFunction("CHR(0)", createVarcharType(1), "\0"); assertInvalidFunction("CHR(-1)", "Not a valid Unicode code point: -1"); assertInvalidFunction("CHR(1234567)", "Not a valid Unicode code point: 1234567"); assertInvalidFunction("CHR(8589934592)", "Not a valid Unicode code point: 8589934592"); }
@Test public void eliminateTableScanWhenNoLayoutExist() { tester().assertThat(pickTableLayout.pickTableLayoutForPredicate()) .on(p -> p.filter(expression("orderstatus = 'G'"), p.tableScan( ordersTableHandle, ImmutableList.of(p.symbol("orderstatus", createVarcharType(1))), ImmutableMap.of(p.symbol("orderstatus", createVarcharType(1)), new TpchColumnHandle("orderstatus", createVarcharType(1))), Optional.of(ordersTableLayoutHandle)))) .matches(values("A")); }
@BeforeMethod(alwaysRun = true) public void setUp() throws Exception { type = VarcharType.createVarcharType(100); BlockBuilder blockBuilder = type.createBlockBuilder(null, 100, 10); valueStore = new ValueStore(100, blockBuilder); valueStoreSmall = new ValueStore(1, blockBuilder); block = BlockAssertions.createStringsBlock("a", "b", "c", "d"); }
@Test public void testEmpty() { assertFunction("transform_keys(map(ARRAY[], ARRAY[]), (k, v) -> NULL)", mapType(UNKNOWN, UNKNOWN), ImmutableMap.of()); assertFunction("transform_keys(map(ARRAY[], ARRAY[]), (k, v) -> k)", mapType(UNKNOWN, UNKNOWN), ImmutableMap.of()); assertFunction("transform_keys(map(ARRAY[], ARRAY[]), (k, v) -> v)", mapType(UNKNOWN, UNKNOWN), ImmutableMap.of()); assertFunction("transform_keys(map(ARRAY[], ARRAY[]), (k, v) -> 0)", mapType(INTEGER, UNKNOWN), ImmutableMap.of()); assertFunction("transform_keys(map(ARRAY[], ARRAY[]), (k, v) -> true)", mapType(BOOLEAN, UNKNOWN), ImmutableMap.of()); assertFunction("transform_keys(map(ARRAY[], ARRAY[]), (k, v) -> 'key')", mapType(createVarcharType(3), UNKNOWN), ImmutableMap.of()); assertFunction("transform_keys(CAST (map(ARRAY[], ARRAY[]) AS MAP(BIGINT,VARCHAR)), (k, v) -> k + CAST(v as BIGINT))", mapType(BIGINT, VARCHAR), ImmutableMap.of()); assertFunction("transform_keys(CAST (map(ARRAY[], ARRAY[]) AS MAP(BIGINT,VARCHAR)), (k, v) -> v)", mapType(VARCHAR, VARCHAR), ImmutableMap.of()); }
@Test public void testEmpty() { assertFunction("transform_values(map(ARRAY[], ARRAY[]), (k, v) -> NULL)", mapType(UNKNOWN, UNKNOWN), ImmutableMap.of()); assertFunction("transform_values(map(ARRAY[], ARRAY[]), (k, v) -> k)", mapType(UNKNOWN, UNKNOWN), ImmutableMap.of()); assertFunction("transform_values(map(ARRAY[], ARRAY[]), (k, v) -> v)", mapType(UNKNOWN, UNKNOWN), ImmutableMap.of()); assertFunction("transform_values(map(ARRAY[], ARRAY[]), (k, v) -> 0)", mapType(UNKNOWN, INTEGER), ImmutableMap.of()); assertFunction("transform_values(map(ARRAY[], ARRAY[]), (k, v) -> true)", mapType(UNKNOWN, BOOLEAN), ImmutableMap.of()); assertFunction("transform_values(map(ARRAY[], ARRAY[]), (k, v) -> 'value')", mapType(UNKNOWN, createVarcharType(5)), ImmutableMap.of()); assertFunction("transform_values(CAST (map(ARRAY[], ARRAY[]) AS MAP(BIGINT,VARCHAR)), (k, v) -> k + CAST(v as BIGINT))", mapType(BIGINT, BIGINT), ImmutableMap.of()); assertFunction("transform_values(CAST (map(ARRAY[], ARRAY[]) AS MAP(BIGINT,VARCHAR)), (k, v) -> CAST(k AS VARCHAR) || v)", mapType(BIGINT, VARCHAR), ImmutableMap.of()); }
@Test public void testReverse() { assertFunction("REVERSE(ARRAY[1])", new ArrayType(INTEGER), ImmutableList.of(1)); assertFunction("REVERSE(ARRAY[1, 2, 3, 4])", new ArrayType(INTEGER), ImmutableList.of(4, 3, 2, 1)); assertFunction("REVERSE(ARRAY_SORT(ARRAY[2, 3, 4, 1]))", new ArrayType(INTEGER), ImmutableList.of(4, 3, 2, 1)); assertFunction("REVERSE(ARRAY[2, BIGINT '3', 4, 1])", new ArrayType(BIGINT), ImmutableList.of(1L, 4L, 3L, 2L)); assertFunction("REVERSE(ARRAY['a', 'b', 'c', 'd'])", new ArrayType(createVarcharType(1)), ImmutableList.of("d", "c", "b", "a")); assertFunction("REVERSE(ARRAY[TRUE, FALSE])", new ArrayType(BOOLEAN), ImmutableList.of(false, true)); assertFunction("REVERSE(ARRAY[1.1E0, 2.2E0, 3.3E0, 4.4E0])", new ArrayType(DOUBLE), ImmutableList.of(4.4, 3.3, 2.2, 1.1)); assertCachedInstanceHasBoundedRetainedSize("REVERSE(ARRAY[1.1E0, 2.2E0, 3.3E0, 4.4E0])"); }
@Test public void testVarchar() { assertEquals(VARCHAR.getTypeSignature().toString(), "varchar"); assertEquals(createVarcharType(42).getTypeSignature().toString(), "varchar(42)"); assertEquals(parseTypeSignature("varchar"), createUnboundedVarcharType().getTypeSignature()); assertEquals(createUnboundedVarcharType().getTypeSignature(), parseTypeSignature("varchar")); assertEquals(parseTypeSignature("varchar").hashCode(), createUnboundedVarcharType().getTypeSignature().hashCode()); assertNotEquals(createUnboundedVarcharType().getTypeSignature(), parseTypeSignature("varchar(10)")); }
@Test public void testVarchar() { assertEquals(VARCHAR.getTypeSignature().toString(), "varchar"); assertEquals(createVarcharType(42).getTypeSignature().toString(), "varchar(42)"); assertEquals(parseTypeSignature("varchar"), createUnboundedVarcharType().getTypeSignature()); assertEquals(createUnboundedVarcharType().getTypeSignature(), parseTypeSignature("varchar")); assertEquals(parseTypeSignature("varchar").hashCode(), createUnboundedVarcharType().getTypeSignature().hashCode()); assertNotEquals(createUnboundedVarcharType().getTypeSignature(), parseTypeSignature("varchar(10)")); }
@Test public void testBasic() { assertFunction("\"$internal$try\"(() -> 42)", INTEGER, 42); assertFunction("\"$internal$try\"(() -> DOUBLE '4.5')", DOUBLE, 4.5); assertFunction("\"$internal$try\"(() -> DECIMAL '4.5')", createDecimalType(2, 1), SqlDecimal.of("4.5")); assertFunction("\"$internal$try\"(() -> TRUE)", BOOLEAN, true); assertFunction("\"$internal$try\"(() -> 'hello')", createVarcharType(5), "hello"); assertFunction("\"$internal$try\"(() -> JSON '[true, false, 12, 12.7, \"12\", null]')", JSON, "[true,false,12,12.7,\"12\",null]"); assertFunction("\"$internal$try\"(() -> ARRAY [1, 2])", new ArrayType(INTEGER), asList(1, 2)); assertFunction("\"$internal$try\"(() -> NULL)", UNKNOWN, null); }