@Test public void testReverse() { assertFunction("REVERSE('')", VARCHAR, ""); assertFunction("REVERSE('hello')", VARCHAR, "olleh"); assertFunction("REVERSE('Quadratically')", VARCHAR, "yllacitardauQ"); assertFunction("REVERSE('racecar')", VARCHAR, "racecar"); // Test REVERSE for non-ASCII assertFunction("REVERSE('\u4FE1\u5FF5,\u7231,\u5E0C\u671B')", VARCHAR, "\u671B\u5E0C,\u7231,\u5FF5\u4FE1"); assertFunction("REVERSE('\u00D6sterreich')", VARCHAR, "hcierrets\u00D6"); assertFunction("REVERSE('na\u00EFve')", VARCHAR, "ev\u00EFan"); assertFunction("REVERSE('\uD801\uDC2Dend')", VARCHAR, "dne\uD801\uDC2D"); assertFunction("CAST(REVERSE(utf8(from_hex('CE'))) AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] {(byte) 0xCE})); assertFunction("CAST(REVERSE('hello' || utf8(from_hex('CE'))) AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] {(byte) 0xCE, 'o', 'l', 'l', 'e', 'h'})); }
@Override public Object getObjectValue(ConnectorSession session, Block block, int position) { if (block.isNull(position)) { return null; } return new SqlVarbinary(block.getSlice(position, 0, block.getLength(position)).getBytes()); }
private static SqlVarbinary byteArrayToVarbinary(byte[] input) { if (input == null) { return null; } return new SqlVarbinary(input); }
private static SqlVarbinary expectedChecksum(Type type, Block block) { long result = 0; for (int i = 0; i < block.getPositionCount(); i++) { if (block.isNull(i)) { result += PRIME64; } else { result += type.hash(block, i) * PRIME64; } } return new SqlVarbinary(wrappedLongArray(result).getBytes()); }
@Test public void testReplace() { assertFunction("REPLACE('aaa', 'a', 'aa')", VARCHAR, "aaaaaa"); assertFunction("REPLACE('abcdefabcdef', 'cd', 'XX')", VARCHAR, "abXXefabXXef"); assertFunction("REPLACE('abcdefabcdef', 'cd')", VARCHAR, "abefabef"); assertFunction("REPLACE('123123tech', '123')", VARCHAR, "tech"); assertFunction("REPLACE('123tech123', '123')", VARCHAR, "tech"); assertFunction("REPLACE('222tech', '2', '3')", VARCHAR, "333tech"); assertFunction("REPLACE('0000123', '0')", VARCHAR, "123"); assertFunction("REPLACE('0000123', '0', ' ')", VARCHAR, " 123"); assertFunction("REPLACE('foo', '')", VARCHAR, "foo"); assertFunction("REPLACE('foo', '', '')", VARCHAR, "foo"); assertFunction("REPLACE('foo', 'foo', '')", VARCHAR, ""); assertFunction("REPLACE('abc', '', 'xx')", VARCHAR, "xxaxxbxxcxx"); assertFunction("REPLACE('', '', 'xx')", VARCHAR, "xx"); assertFunction("REPLACE('', '')", VARCHAR, ""); assertFunction("REPLACE('', '', '')", VARCHAR, ""); assertFunction("REPLACE('\u4FE1\u5FF5,\u7231,\u5E0C\u671B', ',', '\u2014')", VARCHAR, "\u4FE1\u5FF5\u2014\u7231\u2014\u5E0C\u671B"); assertFunction("REPLACE('::\uD801\uDC2D::', ':', '')", VARCHAR, "\uD801\uDC2D"); assertFunction("REPLACE('\u00D6sterreich', '\u00D6', 'Oe')", VARCHAR, "Oesterreich"); assertFunction("CAST(REPLACE(utf8(from_hex('CE')), '', 'X') AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] {'X', (byte) 0xCE, 'X'})); assertFunction("CAST(REPLACE('abc' || utf8(from_hex('CE')), '', 'X') AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] {'X', 'a', 'X', 'b', 'X', 'c', 'X', (byte) 0xCE, 'X'})); assertFunction("CAST(REPLACE(utf8(from_hex('CE')) || 'xyz', '', 'X') AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] {'X', (byte) 0xCE, 'X', 'x', 'X', 'y', 'X', 'z', 'X'})); assertFunction("CAST(REPLACE('abc' || utf8(from_hex('CE')) || 'xyz', '', 'X') AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] {'X', 'a', 'X', 'b', 'X', 'c', 'X', (byte) 0xCE, 'X', 'x', 'X', 'y', 'X', 'z', 'X'})); }
@Test public void testLower() { assertFunction("LOWER('')", VARCHAR, ""); assertFunction("LOWER('Hello World')", VARCHAR, "hello world"); assertFunction("LOWER('WHAT!!')", VARCHAR, "what!!"); assertFunction("LOWER('\u00D6STERREICH')", VARCHAR, lowerByCodePoint("\u00D6sterreich")); assertFunction("LOWER('From\uD801\uDC2DTo')", VARCHAR, lowerByCodePoint("from\uD801\uDC2Dto")); assertFunction("CAST(LOWER(utf8(from_hex('CE'))) AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] {(byte) 0xCE})); assertFunction("CAST(LOWER('HELLO' || utf8(from_hex('CE'))) AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] {'h', 'e', 'l', 'l', 'o', (byte) 0xCE})); assertFunction("CAST(LOWER(utf8(from_hex('CE')) || 'HELLO') AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] {(byte) 0xCE, 'h', 'e', 'l', 'l', 'o'})); assertFunction("CAST(LOWER(utf8(from_hex('C8BAFF'))) AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] {(byte) 0xE2, (byte) 0xB1, (byte) 0xA5, (byte) 0xFF})); }
@Test public void testUpper() { assertFunction("UPPER('')", VARCHAR, ""); assertFunction("UPPER('Hello World')", VARCHAR, "HELLO WORLD"); assertFunction("UPPER('what!!')", VARCHAR, "WHAT!!"); assertFunction("UPPER('\u00D6sterreich')", VARCHAR, upperByCodePoint("\u00D6STERREICH")); assertFunction("UPPER('From\uD801\uDC2DTo')", VARCHAR, upperByCodePoint("FROM\uD801\uDC2DTO")); assertFunction("CAST(UPPER(utf8(from_hex('CE'))) AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] {(byte) 0xCE})); assertFunction("CAST(UPPER('hello' || utf8(from_hex('CE'))) AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] {'H', 'E', 'L', 'L', 'O', (byte) 0xCE})); assertFunction("CAST(UPPER(utf8(from_hex('CE')) || 'hello') AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] {(byte) 0xCE, 'H', 'E', 'L', 'L', 'O'})); }
@Test public void smokedTest() throws Exception { assertExecute("cast(true as boolean)", BOOLEAN, true); assertExecute("true", BOOLEAN, true); assertExecute("false", BOOLEAN, false); assertExecute("42", BIGINT, 42L); assertExecute("'foo'", VARCHAR, "foo"); assertExecute("4.2", DOUBLE, 4.2); assertExecute("1 + 1", BIGINT, 2L); assertExecute("X' 1 f'", VARBINARY, new SqlVarbinary(Slices.wrappedBuffer((byte) 0x1f).getBytes())); assertExecute("X' '", VARBINARY, new SqlVarbinary(new byte[0])); assertExecute("bound_long", BIGINT, 1234L); assertExecute("bound_string", VARCHAR, "hello"); assertExecute("bound_double", DOUBLE, 12.34); assertExecute("bound_boolean", BOOLEAN, true); assertExecute("bound_timestamp", BIGINT, new DateTime(2001, 8, 22, 3, 4, 5, 321, UTC).getMillis()); assertExecute("bound_pattern", VARCHAR, "%el%"); assertExecute("bound_null_string", VARCHAR, null); assertExecute("bound_timestamp_with_timezone", TIMESTAMP_WITH_TIME_ZONE, new SqlTimestampWithTimeZone(new DateTime(1970, 1, 1, 0, 1, 0, 999, DateTimeZone.UTC).getMillis(), TimeZoneKey.getTimeZoneKey("Z"))); assertExecute("bound_binary_literal", VARBINARY, new SqlVarbinary(new byte[]{(byte) 0xab})); // todo enable when null output type is supported // assertExecute("null", null); Futures.allAsList(futures).get(); }
@Test public void testMapKeys() throws Exception { assertFunction("MAP_KEYS(MAP(ARRAY['1', '3'], ARRAY['2', '4']))", new ArrayType(VARCHAR), ImmutableList.of("1", "3")); assertFunction("MAP_KEYS(MAP(ARRAY[1.0, 2.0], ARRAY[ARRAY[1, 2], ARRAY[3]]))", new ArrayType(DOUBLE), ImmutableList.of(1.0, 2.0)); assertFunction("MAP_KEYS(MAP(ARRAY['puppies'], ARRAY['kittens']))", new ArrayType(VARCHAR), ImmutableList.of("puppies")); assertFunction("MAP_KEYS(MAP(ARRAY[TRUE], ARRAY[2]))", new ArrayType(BOOLEAN), ImmutableList.of(true)); assertFunction("MAP_KEYS(MAP(ARRAY[from_unixtime(1)], ARRAY[1.0]))", new ArrayType(TIMESTAMP), ImmutableList.of(new SqlTimestamp(1000, TEST_SESSION.getTimeZoneKey()))); assertFunction("MAP_KEYS(MAP(ARRAY[CAST('puppies' as varbinary)], ARRAY['kittens']))", new ArrayType(VARBINARY), ImmutableList.of(new SqlVarbinary("puppies".getBytes(UTF_8)))); assertFunction("MAP_KEYS(MAP(ARRAY[1,2], ARRAY[ARRAY[1, 2], ARRAY[3]]))", new ArrayType(BIGINT), ImmutableList.of(1L, 2L)); assertFunction("MAP_KEYS(MAP(ARRAY[1,4], ARRAY[MAP(ARRAY[2], ARRAY[3]), MAP(ARRAY[5], ARRAY[6])]))", new ArrayType(BIGINT), ImmutableList.of(1L, 4L)); assertFunction("MAP_KEYS(MAP(ARRAY [ARRAY [1], ARRAY [2, 3]], ARRAY [ARRAY [3, 4], ARRAY [5]]))", new ArrayType(new ArrayType(BIGINT)), ImmutableList.of(ImmutableList.of(1L), ImmutableList.of(2L, 3L))); }
private static void assertValueTypes(MaterializedRow row, List<ColumnMetadata> schema) { for (int columnIndex = 0; columnIndex < schema.size(); columnIndex++) { ColumnMetadata column = schema.get(columnIndex); Object value = row.getField(columnIndex); if (value != null) { if (BOOLEAN.equals(column.getType())) { assertInstanceOf(value, Boolean.class); } else if (BIGINT.equals(column.getType())) { assertInstanceOf(value, Long.class); } else if (DOUBLE.equals(column.getType())) { assertInstanceOf(value, Double.class); } else if (VARCHAR.equals(column.getType())) { assertInstanceOf(value, String.class); } else if (VARBINARY.equals(column.getType())) { assertInstanceOf(value, SqlVarbinary.class); } else if (TIMESTAMP.equals(column.getType())) { assertInstanceOf(value, SqlTimestamp.class); } else if (DATE.equals(column.getType())) { assertInstanceOf(value, SqlDate.class); } else if (column.getType() instanceof ArrayType) { assertInstanceOf(value, List.class); } else if (column.getType() instanceof MapType) { assertInstanceOf(value, Map.class); } else { fail("Unknown primitive type " + columnIndex); } } } }
@Override public Object getObjectValue(ConnectorSession session, Block block, int position) { if (block.isNull(position)) { return null; } return new SqlVarbinary(block.getSlice(position, 0, block.getSliceLength(position)).getBytes()); }
private static SqlVarbinary sqlBinary(byte[] bytes) { return new SqlVarbinary(bytes); }
@Test public void testBinaryLiteral() throws Exception { assertFunction("X'58F7'", VARBINARY, new SqlVarbinary(new byte[]{(byte) 0x58, (byte) 0xF7})); }
private static SqlVarbinary sqlVarbinary(String value) { return new SqlVarbinary(value.getBytes(UTF_8)); }
private static SqlVarbinary sqlVarbinaryHex(String value) { return new SqlVarbinary(base16().decode(value)); }
public TestVarbinaryType() { super(VARBINARY, SqlVarbinary.class, createTestBlock()); }
protected void checkPageSource(ConnectorPageSource pageSource, List<TestColumn> testColumns, List<Type> types) throws IOException { try { MaterializedResult result = materializeSourceDataStream(SESSION, pageSource, types); for (MaterializedRow row : result) { for (int i = 0, testColumnsSize = testColumns.size(); i < testColumnsSize; i++) { TestColumn testColumn = testColumns.get(i); Type type = types.get(i); Object actualValue = row.getField(i); Object expectedValue = testColumn.getExpectedValue(); if (actualValue == null) { assertEquals(null, expectedValue, String.format("Expected non-null for column %d", i)); } else if (testColumn.getObjectInspector().getTypeName().equals("float") || testColumn.getObjectInspector().getTypeName().equals("double")) { assertEquals((double) actualValue, (double) expectedValue, EPSILON); } else if (testColumn.getObjectInspector().getTypeName().equals("date")) { SqlDate expectedDate = new SqlDate(((Long) expectedValue).intValue()); assertEquals(actualValue, expectedDate); } else if (testColumn.getObjectInspector().getTypeName().equals("timestamp")) { SqlTimestamp expectedTimestamp = new SqlTimestamp((Long) expectedValue, SESSION.getTimeZoneKey()); assertEquals(actualValue, expectedTimestamp); } else if (testColumn.getObjectInspector().getCategory() == Category.PRIMITIVE) { if (expectedValue instanceof Slice) { expectedValue = ((Slice) expectedValue).toStringUtf8(); } if (actualValue instanceof Slice) { actualValue = ((Slice) actualValue).toStringUtf8(); } if (actualValue instanceof SqlVarbinary) { actualValue = new String(((SqlVarbinary) actualValue).getBytes(), UTF_8); } assertEquals(actualValue, expectedValue, String.format("Wrong value for column %d", i)); } else { BlockBuilder builder = type.createBlockBuilder(new BlockBuilderStatus(), 1); type.writeObject(builder, expectedValue); expectedValue = type.getObjectValue(SESSION, builder.build(), 0); assertEquals(actualValue, expectedValue, String.format("Wrong value for column %s", testColumn.getName())); } } } } finally { pageSource.close(); } }