Skip to content

Commit

Permalink
feature: DuckDB SIMILAR TO expression
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
  • Loading branch information
manticore-projects committed Apr 25, 2024
1 parent 6e9bf42 commit 302b4fb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

public class LikeExpression extends BinaryExpression {
public enum KeyWord {
LIKE, ILIKE, RLIKE, REGEXP;
LIKE, ILIKE, RLIKE, REGEXP, SIMILAR_TO;

public static KeyWord from(String keyword) {
return Enum.valueOf(KeyWord.class, keyword.toUpperCase());
return Enum.valueOf(KeyWord.class, keyword.toUpperCase().replaceAll("\\s+", "_"));
}
}

Expand Down Expand Up @@ -58,7 +58,8 @@ public String getStringExpression() {
@Override
public String toString() {
String retval = getLeftExpression() + " " + (not ? "NOT " : "")
+ likeKeyWord + " " + (useBinary ? "BINARY " : "") + getRightExpression();
+ (likeKeyWord == KeyWord.SIMILAR_TO ? "SIMILAR TO" : likeKeyWord) + " "
+ (useBinary ? "BINARY " : "") + getRightExpression();
if (escapeExpression != null) {
retval += " ESCAPE " + escapeExpression;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,16 @@ public void visit(JdbcParameter jdbcParameter) {

@Override
public void visit(LikeExpression likeExpression) {
String keywordStr = likeExpression.getLikeKeyWord() == LikeExpression.KeyWord.SIMILAR_TO
? " SIMILAR TO"
: likeExpression.getLikeKeyWord().toString();

likeExpression.getLeftExpression().accept(this);
buffer.append(" ");
if (likeExpression.isNot()) {
buffer.append("NOT ");
}
buffer.append(likeExpression.getLikeKeyWord()).append(" ");
buffer.append(keywordStr).append(" ");
if (likeExpression.isUseBinary()) {
buffer.append("BINARY ");
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <OPENING_CURLY_BRACKET: "{">
| <CLOSING_CURLY_BRACKET: "}">
| <DOUBLE_COLON: ":">
| <K_SIMILAR_TO: ( <K_SIMILAR> (" ")+ <K_TO> ) >
}

TOKEN : /* Statement Separators */
Expand Down Expand Up @@ -3866,6 +3867,7 @@ Expression LikeExpression(Expression leftExpression) #LikeExpression:
| token = <K_ILIKE>
| token = <K_RLIKE>
| token = <K_REGEXP>
| token = <K_SIMILAR_TO>
) { result.setLikeKeyWord( LikeExpression.KeyWord.from(token.image)); }
[ LOOKAHEAD(2) <K_BINARY> {result.setUseBinary(true); } ]
rightExpression=SimpleExpression()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,13 @@ void testNotRLikeIssue1553() throws JSQLParserException {
String sqlStr = "select * from test where id not rlike '111'";
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}

@Test
void testDuckDBSimuilarTo() throws JSQLParserException {
String sqlStr = "SELECT v\n"
+ " FROM strings\n"
+ " WHERE v SIMILAR TO 'San* [fF].*'\n"
+ " ORDER BY v;";
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}
}

0 comments on commit 302b4fb

Please sign in to comment.