@Override protected Type visitCharLiteral(CharLiteral node, StackableAstVisitorContext<Context> context) { CharType type = CharType.createCharType(node.getValue().length()); return setExpressionType(node, type); }
@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())); }
private static Type getCommonSuperTypeForChar(CharType firstType, CharType secondType) { return createCharType(Math.max(firstType.getLength(), secondType.getLength())); }
@Override protected RowExpression visitCharLiteral(CharLiteral node, Void context) { return constant(node.getSlice(), createCharType(node.getValue().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 "); } }
@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 "); } }
@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)); }
@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)); }
@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 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 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 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))); }