Search in sources :

Example 21 with SqlVarbinary

use of io.prestosql.spi.type.SqlVarbinary in project hetu-core by openlookeng.

the class TestStringFunctions method testLower.

@Test
public void testLower() {
    assertFunction("LOWER('')", createVarcharType(0), "");
    assertFunction("LOWER('Hello World')", createVarcharType(11), "hello world");
    assertFunction("LOWER('WHAT!!')", createVarcharType(6), "what!!");
    assertFunction("LOWER('\u00D6STERREICH')", createVarcharType(10), lowerByCodePoint("\u00D6sterreich"));
    assertFunction("LOWER('From\uD801\uDC2DTo')", createVarcharType(7), 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 }));
}
Also used : SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) Test(org.testng.annotations.Test)

Example 22 with SqlVarbinary

use of io.prestosql.spi.type.SqlVarbinary in project hetu-core by openlookeng.

the class TestStringFunctions method testReplace.

@Test
public void testReplace() {
    assertFunction("REPLACE('aaa', 'a', 'aa')", createVarcharType(11), "aaaaaa");
    assertFunction("REPLACE('abcdefabcdef', 'cd', 'XX')", createVarcharType(38), "abXXefabXXef");
    assertFunction("REPLACE('abcdefabcdef', 'cd')", createVarcharType(12), "abefabef");
    assertFunction("REPLACE('123123tech', '123')", createVarcharType(10), "tech");
    assertFunction("REPLACE('123tech123', '123')", createVarcharType(10), "tech");
    assertFunction("REPLACE('222tech', '2', '3')", createVarcharType(15), "333tech");
    assertFunction("REPLACE('0000123', '0')", createVarcharType(7), "123");
    assertFunction("REPLACE('0000123', '0', ' ')", createVarcharType(15), "    123");
    assertFunction("REPLACE('foo', '')", createVarcharType(3), "foo");
    assertFunction("REPLACE('foo', '', '')", createVarcharType(3), "foo");
    assertFunction("REPLACE('foo', 'foo', '')", createVarcharType(3), "");
    assertFunction("REPLACE('abc', '', 'xx')", createVarcharType(11), "xxaxxbxxcxx");
    assertFunction("REPLACE('', '', 'xx')", createVarcharType(2), "xx");
    assertFunction("REPLACE('', '')", createVarcharType(0), "");
    assertFunction("REPLACE('', '', '')", createVarcharType(0), "");
    assertFunction("REPLACE('\u4FE1\u5FF5,\u7231,\u5E0C\u671B', ',', '\u2014')", createVarcharType(15), "\u4FE1\u5FF5\u2014\u7231\u2014\u5E0C\u671B");
    // \uD801\uDC2D is one character
    assertFunction("REPLACE('::\uD801\uDC2D::', ':', '')", createVarcharType(5), "\uD801\uDC2D");
    assertFunction("REPLACE('\u00D6sterreich', '\u00D6', 'Oe')", createVarcharType(32), "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' }));
}
Also used : SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) Test(org.testng.annotations.Test)

Example 23 with SqlVarbinary

use of io.prestosql.spi.type.SqlVarbinary in project hetu-core by openlookeng.

the class TestMapOperators method testMapKeys.

@Test
public void testMapKeys() {
    assertFunction("MAP_KEYS(MAP(ARRAY['1', '3'], ARRAY['2', '4']))", new ArrayType(createVarcharType(1)), ImmutableList.of("1", "3"));
    assertFunction("MAP_KEYS(MAP(ARRAY[1.0E0, 2.0E0], 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(createVarcharType(7)), ImmutableList.of("puppies"));
    assertFunction("MAP_KEYS(MAP(ARRAY[TRUE], ARRAY[2]))", new ArrayType(BOOLEAN), ImmutableList.of(true));
    assertFunction("MAP_KEYS(MAP(ARRAY[TIMESTAMP '1970-01-01 00:00:01'], ARRAY[1.0E0]))", new ArrayType(TIMESTAMP), ImmutableList.of(sqlTimestampOf(1970, 1, 1, 0, 0, 1, 0)));
    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(INTEGER), ImmutableList.of(1, 2));
    assertFunction("MAP_KEYS(MAP(ARRAY[1,4], ARRAY[MAP(ARRAY[2], ARRAY[3]), MAP(ARRAY[5], ARRAY[6])]))", new ArrayType(INTEGER), ImmutableList.of(1, 4));
    assertFunction("MAP_KEYS(MAP(ARRAY [ARRAY [1], ARRAY [2, 3]],  ARRAY [ARRAY [3, 4], ARRAY [5]]))", new ArrayType(new ArrayType(INTEGER)), ImmutableList.of(ImmutableList.of(1), ImmutableList.of(2, 3)));
    assertFunction("MAP_KEYS(MAP(ARRAY [1.0, 383838383838383.12324234234234], ARRAY [2.2, 3.3]))", new ArrayType(createDecimalType(29, 14)), ImmutableList.of(decimal("000000000000001.00000000000000"), decimal("383838383838383.12324234234234")));
    assertFunction("MAP_KEYS(MAP(ARRAY [1.0, 2.01], ARRAY [2.2, 3.3]))", new ArrayType(createDecimalType(3, 2)), ImmutableList.of(decimal("1.00"), decimal("2.01")));
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) Test(org.testng.annotations.Test)

Example 24 with SqlVarbinary

use of io.prestosql.spi.type.SqlVarbinary in project hetu-core by openlookeng.

the class TestIpAddressOperators method testIpAddressToVarbinaryCast.

@Test
public void testIpAddressToVarbinaryCast() {
    assertFunction("CAST(IPADDRESS '::ffff:1.2.3.4' AS VARBINARY)", VARBINARY, new SqlVarbinary(base16().decode("00000000000000000000FFFF01020304")));
    assertFunction("CAST(IPADDRESS '2001:0db8:0000:0000:0000:ff00:0042:8329' AS VARBINARY)", VARBINARY, new SqlVarbinary(base16().decode("20010DB8000000000000FF0000428329")));
    assertFunction("CAST(IPADDRESS '2001:db8::ff00:42:8329' AS VARBINARY)", VARBINARY, new SqlVarbinary(base16().decode("20010DB8000000000000FF0000428329")));
}
Also used : SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) Test(org.testng.annotations.Test)

Example 25 with SqlVarbinary

use of io.prestosql.spi.type.SqlVarbinary in project hetu-core by openlookeng.

the class AbstractTestHiveFileFormats method checkPageSource.

protected void checkPageSource(ConnectorPageSource pageSource, List<TestColumn> testColumns, List<Type> types, int rowCount) throws IOException {
    try {
        MaterializedResult result = materializeSourceDataStream(SESSION, pageSource, types);
        assertEquals(result.getMaterializedRows().size(), rowCount);
        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 (expectedValue instanceof Slice) {
                    expectedValue = ((Slice) expectedValue).toStringUtf8();
                }
                if (actualValue == null || expectedValue == null) {
                    assertEquals(actualValue, expectedValue, "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getTypeName().equals("float")) {
                    assertEquals((float) actualValue, (float) expectedValue, EPSILON, "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getTypeName().equals("double")) {
                    assertEquals((double) actualValue, (double) expectedValue, EPSILON, "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getTypeName().equals("date")) {
                    SqlDate expectedDate = new SqlDate(((Long) expectedValue).intValue());
                    assertEquals(actualValue, expectedDate, "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getTypeName().equals("int") || testColumn.getObjectInspector().getTypeName().equals("smallint") || testColumn.getObjectInspector().getTypeName().equals("tinyint")) {
                    assertEquals(actualValue, expectedValue);
                } else if (testColumn.getObjectInspector().getTypeName().equals("timestamp")) {
                    SqlTimestamp expectedTimestamp = sqlTimestampOf((Long) expectedValue);
                    assertEquals(actualValue, expectedTimestamp, "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getTypeName().startsWith("char")) {
                    assertEquals(actualValue, padEnd((String) expectedValue, ((CharType) type).getLength(), ' '), "Wrong value for column " + testColumn.getName());
                } 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);
                    }
                    if (actualValue instanceof SqlDecimal) {
                        actualValue = new BigDecimal(actualValue.toString());
                    }
                    assertEquals(actualValue, expectedValue, "Wrong value for column " + testColumn.getName());
                } else {
                    BlockBuilder builder = type.createBlockBuilder(null, 1);
                    type.writeObject(builder, expectedValue);
                    expectedValue = type.getObjectValue(SESSION, builder.build(), 0);
                    assertEquals(actualValue, expectedValue, "Wrong value for column " + testColumn.getName());
                }
            }
        }
    } finally {
        pageSource.close();
    }
}
Also used : SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) SqlTimestamp(io.prestosql.spi.type.SqlTimestamp) SqlDecimal(io.prestosql.spi.type.SqlDecimal) BigDecimal(java.math.BigDecimal) RowType(io.prestosql.spi.type.RowType) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) VarcharType.createVarcharType(io.prestosql.spi.type.VarcharType.createVarcharType) TimestampType(io.prestosql.spi.type.TimestampType) CharType(io.prestosql.spi.type.CharType) HiveTestUtils.mapType(io.prestosql.plugin.hive.HiveTestUtils.mapType) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) CharType.createCharType(io.prestosql.spi.type.CharType.createCharType) VarcharType.createUnboundedVarcharType(io.prestosql.spi.type.VarcharType.createUnboundedVarcharType) HiveUtil.isStructuralType(io.prestosql.plugin.hive.HiveUtil.isStructuralType) DateType(io.prestosql.spi.type.DateType) Slice(io.airlift.slice.Slice) SqlDate(io.prestosql.spi.type.SqlDate) SerDeUtils.serializeObject(io.prestosql.plugin.hive.util.SerDeUtils.serializeObject) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) CharType(io.prestosql.spi.type.CharType) CharType.createCharType(io.prestosql.spi.type.CharType.createCharType) MaterializedResult(io.prestosql.testing.MaterializedResult) MaterializedRow(io.prestosql.testing.MaterializedRow) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Aggregations

SqlVarbinary (io.prestosql.spi.type.SqlVarbinary)28 DecimalType (io.prestosql.spi.type.DecimalType)10 SqlDate (io.prestosql.spi.type.SqlDate)10 SqlTimestamp (io.prestosql.spi.type.SqlTimestamp)10 ImmutableList (com.google.common.collect.ImmutableList)9 SqlDecimal (io.prestosql.spi.type.SqlDecimal)9 List (java.util.List)9 Type (io.prestosql.spi.type.Type)8 Collectors.toList (java.util.stream.Collectors.toList)8 Test (org.testng.annotations.Test)8 Slice (io.airlift.slice.Slice)6 BlockBuilder (io.prestosql.spi.block.BlockBuilder)6 CharType (io.prestosql.spi.type.CharType)6 ArrayList (java.util.ArrayList)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)5 VarcharType (io.prestosql.spi.type.VarcharType)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)3