From a9ed79825110df7d619209355d7b2528c6e5e397 Mon Sep 17 00:00:00 2001 From: hezw <562991351@qq.com> Date: Wed, 24 May 2023 11:09:19 +0800 Subject: [PATCH] feat:support clickhouse global keyword in IN Expression Add support for clickhouse global keyword in IN Expression example: SELECT lo_linenumber,lo_orderkey from lo_linenumber where lo_linenumber global in (1,2,3) --- .../operators/relational/InExpression.java | 16 ++++++++++++++++ .../util/deparser/ExpressionDeParser.java | 3 +++ .../net/sf/jsqlparser/parser/JSqlParserCC.jjt | 2 +- .../statement/select/ClickHouseTest.java | 7 +++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/sf/jsqlparser/expression/operators/relational/InExpression.java b/src/main/java/net/sf/jsqlparser/expression/operators/relational/InExpression.java index 89f410870..3c37c19b7 100644 --- a/src/main/java/net/sf/jsqlparser/expression/operators/relational/InExpression.java +++ b/src/main/java/net/sf/jsqlparser/expression/operators/relational/InExpression.java @@ -17,6 +17,7 @@ public class InExpression extends ASTNodeAccessImpl implements Expression, Suppo private Expression leftExpression; private ItemsList rightItemsList; + private boolean global = false; private boolean not = false; private Expression rightExpression; private int oldOracleJoinSyntax = NO_ORACLE_JOIN; @@ -69,6 +70,13 @@ public final void setLeftExpression(Expression expression) { leftExpression = expression; } + public boolean isGlobal() { + return global; + } + + public void setGlobal(boolean b) { + global = b; + } public boolean isNot() { return not; } @@ -100,6 +108,9 @@ public String toString() { statementBuilder.append(getLeftExpressionString()); statementBuilder.append(" "); + if (global) { + statementBuilder.append("GLOBAL "); + } if (not) { statementBuilder.append("NOT "); } @@ -141,6 +152,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; diff --git a/src/main/java/net/sf/jsqlparser/util/deparser/ExpressionDeParser.java b/src/main/java/net/sf/jsqlparser/util/deparser/ExpressionDeParser.java index f1e75ae61..5c2d9771e 100644 --- a/src/main/java/net/sf/jsqlparser/util/deparser/ExpressionDeParser.java +++ b/src/main/java/net/sf/jsqlparser/util/deparser/ExpressionDeParser.java @@ -252,6 +252,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"); } diff --git a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt index 472a0c2aa..30bd223aa 100644 --- a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt +++ b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt @@ -3314,7 +3314,7 @@ Expression InExpression() #InExpression : leftExpression=SimpleExpression() { result.setLeftExpression(leftExpression); } [ "(" "+" ")" { result.setOldOracleJoinSyntax(EqualsTo.ORACLE_JOIN_RIGHT); } ] - [ { result.setNot(true); } ] + [ { result.setGlobal(true); } ][ { result.setNot(true); } ] ( LOOKAHEAD(2) token= { result.setRightExpression(new StringValue(token.image)); } | LOOKAHEAD(3) rightExpression = Function() { result.setRightExpression(rightExpression); } diff --git a/src/test/java/net/sf/jsqlparser/statement/select/ClickHouseTest.java b/src/test/java/net/sf/jsqlparser/statement/select/ClickHouseTest.java index fa52af6a9..26f2ec33c 100644 --- a/src/test/java/net/sf/jsqlparser/statement/select/ClickHouseTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/select/ClickHouseTest.java @@ -34,4 +34,11 @@ public void testFunctionWithAttributesIssue1742() throws JSQLParserException { sql = "SELECT schemaName.f1(arguments).f2(arguments).f3.f4 from dual"; assertSqlCanBeParsedAndDeparsed(sql, true); } + + @Test + 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); + } }