@Override public Type createType(TypeManager typeManager, List<TypeParameter> parameters) { if (parameters.isEmpty()) { return createCharType(1); } if (parameters.size() != 1) { throw new IllegalArgumentException("Expected at most one parameter for CHAR"); } TypeParameter parameter = parameters.get(0); if (!parameter.isLongLiteral()) { throw new IllegalArgumentException("CHAR length must be a number"); } return createCharType(parameter.getLongLiteral()); } }
@Override protected Type visitCharLiteral(CharLiteral node, StackableAstVisitorContext<Context> context) { CharType type = CharType.createCharType(node.getValue().length()); return setExpressionType(node, type); }
private static Type getCommonSuperTypeForChar(CharType firstType, CharType secondType) { return createCharType(Math.max(firstType.getLength(), secondType.getLength())); }
public static DataType<String> charDataType(String insertType, int length) { return dataType(insertType, createCharType(length), DataType::quote, input -> padEnd(input, length, ' ')); }
@Override protected RowExpression visitCharLiteral(CharLiteral node, Void context) { return constant(node.getSlice(), createCharType(node.getValue().length())); }
@Test public void testVarcharToCharCast() { assertFunction("cast('bar ' as char(10))", createCharType(10), "bar "); assertFunction("cast('bar' as char)", createCharType(1), "b"); assertFunction("cast(' ' as char)", createCharType(1), " "); }
@Test public void charIsCreatedWithLength() { assertFunction("CHAR ' abc\n '", createCharType(10), " abc\n "); } }
public static Type getPrestoType(ColumnType tpcdsType) { switch (tpcdsType.getBase()) { case IDENTIFIER: return BigintType.BIGINT; case INTEGER: return IntegerType.INTEGER; case DATE: return DateType.DATE; case DECIMAL: return createDecimalType(tpcdsType.getPrecision().get(), tpcdsType.getScale().get()); case CHAR: return createCharType(tpcdsType.getPrecision().get()); case VARCHAR: return createVarcharType(tpcdsType.getPrecision().get()); case TIME: return TimeType.TIME; } throw new IllegalArgumentException("Unsupported TPC-DS type " + tpcdsType); } }
@Test public void testCharConcat() { assertFunction("concat('ab ', cast(' ' as char(1)))", createCharType(4), "ab "); assertFunction("concat('ab ', cast(' ' as char(1))) = 'ab'", BOOLEAN, true); assertFunction("concat('ab ', cast('a' as char(2)))", createCharType(5), "ab a "); assertFunction("concat('ab ', cast('a' as char(2))) = 'ab a'", BOOLEAN, true); assertFunction("concat('ab ', cast('' as char(0)))", createCharType(3), "ab "); assertFunction("concat('ab ', cast('' as char(0))) = 'ab'", BOOLEAN, true); assertFunction("concat('hello na\u00EFve', cast(' world' as char(6)))", createCharType(17), "hello na\u00EFve world"); assertInvalidFunction("concat(cast('ab ' as char(40000)), cast('' as char(40000)))", "CHAR length scale must be in range [0, 65536]"); assertFunction("concat(cast(null as char(1)), cast(' ' as char(1)))", createCharType(2), null); } }
@Test public void testCharLeftTrim() { assertFunction("LTRIM(CAST('' AS CHAR(20)))", createCharType(20), padRight("", 20)); assertFunction("LTRIM(CAST(' hello ' AS CHAR(9)))", createCharType(9), padRight("hello", 9)); assertFunction("LTRIM(CAST(' hello' AS CHAR(7)))", createCharType(7), padRight("hello", 7)); assertFunction("LTRIM(CAST('hello ' AS CHAR(7)))", createCharType(7), padRight("hello", 7)); assertFunction("LTRIM(CAST(' hello world ' AS CHAR(13)))", createCharType(13), padRight("hello world", 13)); assertFunction("LTRIM(CAST('\u4FE1\u5FF5 \u7231 \u5E0C\u671B ' AS CHAR(9)))", createCharType(9), padRight("\u4FE1\u5FF5 \u7231 \u5E0C\u671B", 9)); assertFunction("LTRIM(CAST(' \u4FE1\u5FF5 \u7231 \u5E0C\u671B ' AS CHAR(9)))", createCharType(9), padRight("\u4FE1\u5FF5 \u7231 \u5E0C\u671B", 9)); assertFunction("LTRIM(CAST(' \u4FE1\u5FF5 \u7231 \u5E0C\u671B' AS CHAR(9)))", createCharType(9), padRight("\u4FE1\u5FF5 \u7231 \u5E0C\u671B", 9)); assertFunction("LTRIM(CAST(' \u2028 \u4FE1\u5FF5 \u7231 \u5E0C\u671B' AS CHAR(10)))", createCharType(10), padRight("\u4FE1\u5FF5 \u7231 \u5E0C\u671B", 10)); }
@Test public void testCharRightTrim() { assertFunction("RTRIM(CAST('' AS CHAR(20)))", createCharType(20), padRight("", 20)); assertFunction("RTRIM(CAST(' hello ' AS CHAR(9)))", createCharType(9), padRight(" hello", 9)); assertFunction("RTRIM(CAST(' hello' AS CHAR(7)))", createCharType(7), padRight(" hello", 7)); assertFunction("RTRIM(CAST('hello ' AS CHAR(7)))", createCharType(7), padRight("hello", 7)); assertFunction("RTRIM(CAST(' hello world ' AS CHAR(13)))", createCharType(13), padRight(" hello world", 13)); assertFunction("RTRIM(CAST('\u4FE1\u5FF5 \u7231 \u5E0C\u671B \u2028 ' AS CHAR(10)))", createCharType(10), padRight("\u4FE1\u5FF5 \u7231 \u5E0C\u671B", 10)); assertFunction("RTRIM(CAST('\u4FE1\u5FF5 \u7231 \u5E0C\u671B ' AS CHAR(9)))", createCharType(9), padRight("\u4FE1\u5FF5 \u7231 \u5E0C\u671B", 9)); assertFunction("RTRIM(CAST(' \u4FE1\u5FF5 \u7231 \u5E0C\u671B ' AS CHAR(9)))", createCharType(9), padRight(" \u4FE1\u5FF5 \u7231 \u5E0C\u671B", 9)); assertFunction("RTRIM(CAST(' \u4FE1\u5FF5 \u7231 \u5E0C\u671B' AS CHAR(9)))", createCharType(9), padRight(" \u4FE1\u5FF5 \u7231 \u5E0C\u671B", 9)); }
private Set<Type> getStandardPrimitiveTypes() { ImmutableSet.Builder<Type> builder = ImmutableSet.builder(); // add unparametrized types builder.addAll(typeRegistry.getTypes()); // add corner cases for parametrized types builder.add(createDecimalType(1, 0)); builder.add(createDecimalType(17, 0)); builder.add(createDecimalType(38, 0)); builder.add(createDecimalType(17, 17)); builder.add(createDecimalType(38, 38)); builder.add(createVarcharType(0)); builder.add(createUnboundedVarcharType()); builder.add(createCharType(0)); builder.add(createCharType(42)); return builder.build(); }
@Test public void testCharTrim() { assertFunction("TRIM(CAST('' AS CHAR(20)))", createCharType(20), padRight("", 20)); assertFunction("TRIM(CAST(' hello ' AS CHAR(9)))", createCharType(9), padRight("hello", 9)); assertFunction("TRIM(CAST(' hello' AS CHAR(7)))", createCharType(7), padRight("hello", 7)); assertFunction("TRIM(CAST('hello ' AS CHAR(7)))", createCharType(7), padRight("hello", 7)); assertFunction("TRIM(CAST(' hello world ' AS CHAR(13)))", createCharType(13), padRight("hello world", 13)); assertFunction("TRIM(CAST('\u4FE1\u5FF5 \u7231 \u5E0C\u671B \u2028 ' AS CHAR(10)))", createCharType(10), padRight("\u4FE1\u5FF5 \u7231 \u5E0C\u671B", 10)); assertFunction("TRIM(CAST('\u4FE1\u5FF5 \u7231 \u5E0C\u671B ' AS CHAR(9)))", createCharType(9), padRight("\u4FE1\u5FF5 \u7231 \u5E0C\u671B", 9)); assertFunction("TRIM(CAST(' \u4FE1\u5FF5 \u7231 \u5E0C\u671B ' AS CHAR(9)))", createCharType(9), padRight("\u4FE1\u5FF5 \u7231 \u5E0C\u671B", 9)); assertFunction("TRIM(CAST(' \u4FE1\u5FF5 \u7231 \u5E0C\u671B' AS CHAR(9)))", createCharType(9), padRight("\u4FE1\u5FF5 \u7231 \u5E0C\u671B", 9)); assertFunction("TRIM(CAST(' \u2028 \u4FE1\u5FF5 \u7231 \u5E0C\u671B' AS CHAR(10)))", createCharType(10), padRight("\u4FE1\u5FF5 \u7231 \u5E0C\u671B", 10)); }
@Test public void testCharUpper() { assertFunction("UPPER(CAST('' AS CHAR(10)))", createCharType(10), padRight("", 10)); assertFunction("UPPER(CAST('Hello World' AS CHAR(11)))", createCharType(11), padRight("HELLO WORLD", 11)); assertFunction("UPPER(CAST('what!!' AS CHAR(6)))", createCharType(6), padRight("WHAT!!", 6)); assertFunction("UPPER(CAST('\u00D6sterreich' AS CHAR(10)))", createCharType(10), padRight(upperByCodePoint("\u00D6") + "STERREICH", 10)); assertFunction("UPPER(CAST('From\uD801\uDC2DTo' AS CHAR(7)))", createCharType(7), padRight("FROM" + upperByCodePoint("\uD801\uDC2D") + "TO", 7)); }
@Test public void testCharLower() { assertFunction("LOWER(CAST('' AS CHAR(10)))", createCharType(10), padRight("", 10)); assertFunction("LOWER(CAST('Hello World' AS CHAR(11)))", createCharType(11), padRight("hello world", 11)); assertFunction("LOWER(CAST('WHAT!!' AS CHAR(6)))", createCharType(6), padRight("what!!", 6)); assertFunction("LOWER(CAST('\u00D6STERREICH' AS CHAR(10)))", createCharType(10), padRight(lowerByCodePoint("\u00D6sterreich"), 10)); assertFunction("LOWER(CAST('From\uD801\uDC2DTo' AS CHAR(7)))", createCharType(7), padRight(lowerByCodePoint("from\uD801\uDC2Dto"), 7)); }
@Test public void testCharRightTrimParametrized() { assertFunction("RTRIM(CAST('' AS CHAR(1)), '')", createCharType(1), padRight("", 1)); assertFunction("RTRIM(CAST(' ' AS CHAR(3)), '')", createCharType(3), padRight("", 3)); assertFunction("RTRIM(CAST(' hello ' AS CHAR(9)), '')", createCharType(9), padRight(" hello", 9)); assertFunction("RTRIM(CAST(' hello ' AS CHAR(9)), ' ')", createCharType(9), padRight(" hello", 9)); assertFunction("RTRIM(CAST(' hello ' AS CHAR(9)), 'he ')", createCharType(9), padRight(" hello", 9)); assertFunction("RTRIM(CAST(' hello' AS CHAR(7)), ' ')", createCharType(7), padRight(" hello", 7)); assertFunction("RTRIM(CAST(' hello' AS CHAR(7)), 'e h')", createCharType(7), padRight(" hello", 7)); assertFunction("RTRIM(CAST('hello ' AS CHAR(7)), 'l')", createCharType(7), padRight("hello", 7)); assertFunction("RTRIM(CAST(' hello world ' AS CHAR(13)), ' ')", createCharType(13), padRight(" hello world", 13)); assertFunction("RTRIM(CAST(' hello world ' AS CHAR(13)), ' eh')", createCharType(13), padRight(" hello world", 13)); assertFunction("RTRIM(CAST(' hello world ' AS CHAR(13)), ' ehlowrd')", createCharType(13), padRight("", 13)); assertFunction("RTRIM(CAST(' hello world ' AS CHAR(13)), ' x')", createCharType(13), padRight(" hello world", 13)); // non latin characters assertFunction("RTRIM(CAST('\u017a\u00f3\u0142\u0107' AS CHAR(4)), '\u0107\u0142')", createCharType(4), padRight("\u017a\u00f3", 4)); }
@Test public void testCharLeftTrimParametrized() { assertFunction("LTRIM(CAST('' AS CHAR(1)), '')", createCharType(1), padRight("", 1)); assertFunction("LTRIM(CAST(' ' AS CHAR(3)), '')", createCharType(3), padRight("", 3)); assertFunction("LTRIM(CAST(' hello ' AS CHAR(9)), '')", createCharType(9), padRight(" hello", 9)); assertFunction("LTRIM(CAST(' hello ' AS CHAR(9)), ' ')", createCharType(9), padRight("hello", 9)); assertFunction("LTRIM(CAST(' hello ' AS CHAR(9)), 'he ')", createCharType(9), padRight("llo", 9)); assertFunction("LTRIM(CAST(' hello' AS CHAR(7)), ' ')", createCharType(7), padRight("hello", 7)); assertFunction("LTRIM(CAST(' hello' AS CHAR(7)), 'e h')", createCharType(7), padRight("llo", 7)); assertFunction("LTRIM(CAST('hello ' AS CHAR(7)), 'l')", createCharType(7), padRight("hello", 7)); assertFunction("LTRIM(CAST(' hello world ' AS CHAR(13)), ' ')", createCharType(13), padRight("hello world", 13)); assertFunction("LTRIM(CAST(' hello world ' AS CHAR(13)), ' eh')", createCharType(13), padRight("llo world", 13)); assertFunction("LTRIM(CAST(' hello world ' AS CHAR(13)), ' ehlowrd')", createCharType(13), padRight("", 13)); assertFunction("LTRIM(CAST(' hello world ' AS CHAR(13)), ' x')", createCharType(13), padRight("hello world", 13)); // non latin characters assertFunction("LTRIM(CAST('\u017a\u00f3\u0142\u0107' AS CHAR(4)), '\u00f3\u017a')", createCharType(4), padRight("\u0142\u0107", 4)); }
@Test public void testCharTrimParametrized() { assertFunction("TRIM(CAST('' AS CHAR(1)), '')", createCharType(1), padRight("", 1)); assertFunction("TRIM(CAST(' ' AS CHAR(3)), '')", createCharType(3), padRight("", 3)); assertFunction("TRIM(CAST(' hello ' AS CHAR(9)), '')", createCharType(9), padRight(" hello", 9)); assertFunction("TRIM(CAST(' hello ' AS CHAR(9)), ' ')", createCharType(9), padRight("hello", 9)); assertFunction("TRIM(CAST(' hello ' AS CHAR(9)), 'he ')", createCharType(9), padRight("llo", 9)); assertFunction("TRIM(CAST(' hello' AS CHAR(7)), ' ')", createCharType(7), padRight("hello", 7)); assertFunction("TRIM(CAST(' hello' AS CHAR(7)), 'e h')", createCharType(7), padRight("llo", 7)); assertFunction("TRIM(CAST('hello ' AS CHAR(7)), 'l')", createCharType(7), padRight("hello", 7)); assertFunction("TRIM(CAST(' hello world ' AS CHAR(13)), ' ')", createCharType(13), padRight("hello world", 13)); assertFunction("TRIM(CAST(' hello world ' AS CHAR(13)), ' eh')", createCharType(13), padRight("llo world", 13)); assertFunction("TRIM(CAST(' hello world ' AS CHAR(13)), ' ehlowrd')", createCharType(13), padRight("", 13)); assertFunction("TRIM(CAST(' hello world ' AS CHAR(13)), ' x')", createCharType(13), padRight("hello world", 13)); assertFunction("TRIM(CAST('abc def' AS CHAR(7)), 'def')", createCharType(7), padRight("abc", 7)); // non latin characters assertFunction("TRIM(CAST('\u017a\u00f3\u0142\u0107' AS CHAR(4)), '\u017a\u0107\u0142')", createCharType(4), padRight("\u00f3", 4)); }
@Test public void testCharComparedToVarcharExpression() { Type charType = createCharType(10); // varchar literal is coerced to column (char) type testSimpleComparison(equal(C_CHAR, cast(stringLiteral("abc"), charType)), C_CHAR, Range.equal(charType, Slices.utf8Slice("abc"))); // both sides got coerced to char(11) charType = createCharType(11); assertUnsupportedPredicate(equal(cast(C_CHAR, charType), cast(stringLiteral("abc12345678"), charType))); }
@Test public void testOrderByChar() { assertUpdate("CREATE TABLE char_order_by (c_char char(2))"); assertUpdate("INSERT INTO char_order_by (c_char) VALUES" + "(CAST('a' as CHAR(2)))," + "(CAST('a\0' as CHAR(2)))," + "(CAST('a ' as CHAR(2)))", 3); MaterializedResult actual = computeActual(getSession(), "SELECT * FROM char_order_by ORDER BY c_char ASC"); assertUpdate("DROP TABLE char_order_by"); MaterializedResult expected = resultBuilder(getSession(), createCharType(2)) .row("a\0") .row("a ") .row("a ") .build(); assertEquals(actual, expected); }