Skip to content

Commit

Permalink
fix split part
Browse files Browse the repository at this point in the history
  • Loading branch information
LiBinfeng-01 committed Dec 11, 2024
1 parent 917b8d5 commit 7a9d32b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,14 @@ public static Expression instr(StringLikeLiteral first, StringLikeLiteral second
* Executable arithmetic functions Ascii
*/
@ExecFunction(name = "ascii")
public static Expression ascii(StringLikeLiteral first) {
public static Expression ascii(StringLikeLiteral first) throws UnsupportedEncodingException {
if (first.getValue().length() == 0) {
return new IntegerLiteral(0);
}
char firstChar = first.getValue().charAt(0);
return new IntegerLiteral(firstChar);
String character = first.getValue();
byte[] utf8Bytes = character.getBytes("UTF-8");
int firstByteAscii = utf8Bytes[0] & 0xFF;
return new IntegerLiteral(firstByteAscii);
}

/**
Expand Down Expand Up @@ -675,7 +677,7 @@ public static Expression splitPart(StringLikeLiteral first, StringLikeLiteral ch
}

if (parts.length < Math.abs(number.getValue()) || number.getValue() == 0) {
if (parts.length == Math.abs(number.getValue()) - 1) {
if (parts.length == Math.abs(number.getValue())) {
if (number.getValue() < 0 && first.getValue().startsWith(chr.getValue())
|| number.getValue() > 0 && first.getValue().endsWith(chr.getValue())) {
return castStringLikeLiteral(first, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ suite("fold_constant_string_arithmatic") {
testFoldConst("select ascii('1')")
testFoldConst("select ascii('a')")
testFoldConst("select ascii('A')")
// Unicode code point or UTF-8? pg is 12371 mysql is 227 because it is stored as E3 81 82 so E3 would be showed
// testFoldConst("select ascii('')")
// testFoldConst("select ascii('')")
testFoldConst("select ascii('こ')")
testFoldConst("select ascii('')")
testFoldConst("select ascii('')")
testFoldConst("select ascii(cast('1' as string))")
testFoldConst("select ascii(cast('a' as string))")
testFoldConst("select ascii(cast('A' as string))")
testFoldConst("select ascii(cast('!' as string))")
// testFoldConst("select ascii(cast('こ' as string))")
// testFoldConst("select ascii(cast('안' as string))")
testFoldConst("select ascii(cast('こ' as string))")
testFoldConst("select ascii(cast('안' as string))")
testFoldConst("select ascii(cast('中' as string))")

// bin
testFoldConst("select bin(5)")
Expand Down Expand Up @@ -431,8 +432,7 @@ suite("fold_constant_string_arithmatic") {
testFoldConst("SELECT split_part('哈哈哈AAA','A', 2)")
testFoldConst("SELECT split_part('哈哈哈AAA','A', 3)")
testFoldConst("SELECT split_part('哈哈哈AAA','A', 4)")
// should exceed become null? pg is empty but not null
// testFoldConst("SELECT split_part('哈哈哈AAA','A', 5)")
testFoldConst("SELECT split_part('哈哈哈AAA','A', 5)")
testFoldConst("SELECT split_part('哈哈哈AA+','A', -4)")
testFoldConst("SELECT split_part('哈哈哈AA+','A', -3)")
testFoldConst("SELECT split_part('哈哈哈AA+','A', -2)")
Expand Down

0 comments on commit 7a9d32b

Please sign in to comment.