Skip to content

Commit

Permalink
feat: Clickhouse GLOBAL IN ...
Browse files Browse the repository at this point in the history
  • Loading branch information
manticore-projects committed Sep 12, 2023
2 parents da13d7d + a9ed798 commit ced0d00
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class InExpression extends ASTNodeAccessImpl
implements Expression, SupportsOldOracleJoinSyntax {

private Expression leftExpression;
private boolean global = false;
private boolean not = false;
private Expression rightExpression;
private int oldOracleJoinSyntax = NO_ORACLE_JOIN;
Expand Down Expand Up @@ -56,6 +57,15 @@ public final void setLeftExpression(Expression expression) {
leftExpression = expression;
}

public boolean isGlobal() {
return global;
}

public InExpression setGlobal(boolean b) {
global = b;
return this;
}

public boolean isNot() {
return not;
}
Expand Down Expand Up @@ -87,6 +97,9 @@ public String toString() {
statementBuilder.append(getLeftExpressionString());

statementBuilder.append(" ");
if (global) {
statementBuilder.append("GLOBAL ");
}
if (not) {
statementBuilder.append("NOT ");
}
Expand Down Expand Up @@ -124,6 +137,11 @@ public InExpression withOraclePriorPosition(int priorPosition) {
return this;
}

public InExpression withGlobal(boolean global) {
this.setGlobal(global);
return this;
}

public InExpression withNot(boolean not) {
this.setNot(not);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ public void visit(InExpression inExpression) {
.getOldOracleJoinSyntax() == SupportsOldOracleJoinSyntax.ORACLE_JOIN_RIGHT) {
buffer.append("(+)");
}
if (inExpression.isGlobal()) {
buffer.append(" GLOBAL");
}
if (inExpression.isNot()) {
buffer.append(" NOT");
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -3464,15 +3464,18 @@ Expression InExpression() #InExpression :
{
Token token;
int oldOracleJoin = 0;
boolean usesNot = false;
boolean usingNot = false;
boolean usingGlobal = false;
Expression leftExpression;
Expression rightExpression;
}
{
leftExpression=SimpleExpression()
[ "(" "+" ")" { oldOracleJoin=EqualsTo.ORACLE_JOIN_RIGHT; } ]

[<K_NOT> { usesNot=true; } ] <K_IN>
[<K_GLOBAL> { usingGlobal=true; } ]
[<K_NOT> { usingNot=true; } ]
<K_IN>
(
LOOKAHEAD(2) token=<S_CHAR_LITERAL> { rightExpression = new StringValue(token.image); }
| LOOKAHEAD(3) rightExpression = Function()
Expand All @@ -3483,7 +3486,8 @@ Expression InExpression() #InExpression :
{
InExpression inExpression = new InExpression(leftExpression, rightExpression)
.withOldOracleJoinSyntax(oldOracleJoin)
.withNot(usesNot);
.withNot(usingNot)
.setGlobal(usingGlobal);
linkAST(inExpression,jjtThis);
return inExpression;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
package net.sf.jsqlparser.statement.select;

import net.sf.jsqlparser.JSQLParserException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
Expand All @@ -37,33 +36,9 @@ public void testFunctionWithAttributesIssue1742() throws JSQLParserException {
}

@Test
public void testSelectUsingFinal() throws JSQLParserException {
String sqlStr = "SELECT column FROM table_name AS tn FINAL";
assertSqlCanBeParsedAndDeparsed(sqlStr, true);

// check that FINAL is reserved keyword and won't be read as an Alias
sqlStr = "SELECT column FROM table_name FINAL";
PlainSelect select = (PlainSelect) assertSqlCanBeParsedAndDeparsed(sqlStr, true);

Assertions.assertTrue(select.isUsingFinal());
Assertions.assertFalse(select.withUsingFinal(false).toString().contains("FINAL"));
}

@Test
public void testLimitBy() throws JSQLParserException {
String sqlStr = "SELECT * FROM limit_by ORDER BY id, val LIMIT 1, 2 BY id";
assertSqlCanBeParsedAndDeparsed(sqlStr, true);

sqlStr = "SELECT\n"
+ " domainWithoutWWW(URL) AS domain,\n"
+ " domainWithoutWWW(REFERRER_URL) AS referrer,\n"
+ " device_type,\n"
+ " count() cnt\n"
+ "FROM hits\n"
+ "GROUP BY domain, referrer, device_type\n"
+ "ORDER BY cnt DESC\n"
+ "LIMIT 5 BY domain, device_type\n"
+ "LIMIT 100";
assertSqlCanBeParsedAndDeparsed(sqlStr, true);
public void testGlobalIn() throws JSQLParserException {
String sql =
"SELECT lo_linenumber,lo_orderkey from lo_linenumber where lo_linenumber global in (1,2,3)";
assertSqlCanBeParsedAndDeparsed(sql, true);
}
}

0 comments on commit ced0d00

Please sign in to comment.