Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parent-child relationship #6126

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ public boolean equals(Object obj) {

public SQLListExpr clone() {
SQLListExpr x = new SQLListExpr();
if (isParenthesized()) {
x.setParenthesized(true);
}
for (SQLExpr item : items) {
SQLExpr item2 = item.clone();
item2.setParent(x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ public SQLTableSource getUsing() {
}

public void setUsing(SQLTableSource using) {
if (using != null) {
using.setParent(this);
}
this.using = using;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public SQLExpr getOn() {
}

public void setOn(SQLExpr on) {
if (on != null) {
on.setParent(this);
}
this.on = on;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ protected void parseCreateTableRest(SQLCreateTableStatement createTable) {
for (; ; ) {
if (lexer.token() == Token.LPAREN) {
SQLListExpr list = new SQLListExpr();
list.setParenthesized(true);
lexer.nextToken();
this.exprParser.exprList(list.getItems(), list);
accept(Token.RPAREN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ protected void parseOptions(MySqlCreateTableStatement stmt) {

accept(Token.LPAREN);
SQLListExpr list = new SQLListExpr();
list.setParenthesized(true);
this.exprParser.exprList(list.getItems(), list);
stmt.addOption("UNION", list);
accept(Token.RPAREN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,9 @@ public SQLExpr primary() {
if (lexer.token() != Token.LPAREN) {
SQLExpr expr = primary();
SQLValuesQuery values = new SQLValuesQuery();
values.addValue(new SQLListExpr(expr));
SQLListExpr sqlListExpr = new SQLListExpr(expr);
sqlListExpr.setParenthesized(true);
values.addValue(sqlListExpr);
return new SQLQueryExpr(new SQLSelect(values));
}
return this.methodRest(new SQLIdentifierExpr("VALUES"), true);
Expand Down Expand Up @@ -2542,6 +2544,7 @@ public boolean parseTableOptions(List<SQLAssignItem> assignItems, SQLDDLStatemen

accept(Token.LPAREN);
SQLListExpr list = new SQLListExpr();
list.setParenthesized(true);
exprList(list.getItems(), list);
accept(Token.RPAREN);
assignItem = new SQLAssignItem(new SQLIdentifierExpr("UNION"), list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ protected void parseAssignItemNcToBeExecuted() {
protected boolean parseAssignItemTblProperties(SQLAssignItem item) {
if (lexer.token() == Token.LPAREN) {
SQLListExpr list = new SQLListExpr();
list.setParenthesized(true);
this.exprList(list.getItems(), list);
item.setTarget(new SQLIdentifierExpr("tblproperties"));
item.setValue(list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public OracleReturningClause getReturning() {
}

public void setReturning(OracleReturningClause returning) {
if (returning != null) {
returning.setParent(this);
}
this.returning = returning;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public OracleReturningClause getReturning() {
}

public void setReturning(OracleReturningClause returning) {
if (returning != null) {
returning.setParent(this);
}
this.returning = returning;
}

Expand All @@ -72,6 +75,9 @@ public SQLErrorLoggingClause getErrorLogging() {
}

public void setErrorLogging(SQLErrorLoggingClause errorLogging) {
if (errorLogging != null) {
errorLogging.setParent(this);
}
this.errorLogging = errorLogging;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ public boolean visit(OracleAnalyticWindowing x) {
public boolean visit(OracleDeleteStatement x) {
print0(ucase ? "DELETE " : "delete ");

SQLTableSource tableSource = x.getTableSource();
if (x.getHints().size() > 0) {
printAndAccept(x.getHints(), ", ");
print(' ');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ public SQLExpr primary() {
for (; ; ) {
accept(Token.LPAREN);
SQLListExpr listExpr = new SQLListExpr();
listExpr.setParenthesized(true);
exprList(listExpr.getItems(), listExpr);
accept(Token.RPAREN);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,10 @@ public boolean visit(SQLSetStatement x) {
@Override
public boolean visit(SQLAssignItem x) {
if (!(x.getParent() instanceof SQLSetStatement)) {
return super.visit(x);
x.getTarget().accept(this);
print0(" := ");
x.getValue().accept(this);
return false;
Copy link
Contributor Author

@ZhengguanLi ZhengguanLi Oct 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super.visit(x); 会使用SQLASTOutputVisitor的方法,用=来赋值,但是PG支持两种,:=更常用(https://www.postgresql.org/docs/current/plpgsql-statements.html)

}

x.getTarget().accept(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public SQLCreateTableStatement parseCreateTable() {
for (; ; ) {
if (lexer.token() == Token.LPAREN) {
SQLListExpr list = new SQLListExpr();
list.setParenthesized(true);
lexer.nextToken();
this.exprParser.exprList(list.getItems(), list);
accept(Token.RPAREN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,7 @@ public SQLExpr primaryRest(SQLExpr expr) {
}

SQLListExpr listExpr = new SQLListExpr();
listExpr.setParenthesized(true);
this.exprList(listExpr.getItems(), listExpr);
item = listExpr;

Expand Down Expand Up @@ -3084,6 +3085,7 @@ public SQLUpdateSetItem parseUpdateSetItem() {
if (lexer.token == (Token.LPAREN)) {
lexer.nextToken();
SQLListExpr list = new SQLListExpr();
list.setParenthesized(true);
this.exprList(list.getItems(), list);
accept(Token.RPAREN);
item.setColumn(list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ protected SQLSelectQuery valuesQuery(boolean acceptUnion) {
if (lexer.token == Token.LPAREN) {
lexer.nextToken();
SQLListExpr listExpr = new SQLListExpr();
listExpr.setParenthesized(true);
this.exprParser.exprList(listExpr.getItems(), listExpr);
accept(Token.RPAREN);
valuesQuery.addValue(listExpr);
Expand Down Expand Up @@ -1042,7 +1043,9 @@ protected SQLExpr parseGroupByItem() {

if (lexer.token == Token.RPAREN) {
lexer.nextToken();
return new SQLListExpr();
SQLListExpr sqlListExpr = new SQLListExpr();
sqlListExpr.setParenthesized(true);
return sqlListExpr;
}

lexer.reset(mark);
Expand All @@ -1062,6 +1065,7 @@ protected SQLExpr parseGroupByItem() {
if (lexer.token == Token.LPAREN) {
accept(Token.LPAREN);
SQLListExpr list = new SQLListExpr();
list.setParenthesized(true);
if (lexer.token == Token.COMMA) {
lexer.nextToken();
}
Expand Down Expand Up @@ -1250,6 +1254,7 @@ public SQLTableSource parseTableSource(boolean forFrom) {
for (; ; ) {
accept(Token.LPAREN);
SQLListExpr listExpr = new SQLListExpr();
listExpr.setParenthesized(true);
this.exprParser.exprList(listExpr.getItems(), listExpr);
accept(Token.RPAREN);

Expand Down Expand Up @@ -2258,6 +2263,7 @@ public SQLValuesTableSource parseValues() {
}

SQLListExpr listExpr = new SQLListExpr();
listExpr.setParenthesized(true);

if (isSingleValue) {
SQLExpr expr = expr();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,13 @@ static void resolve(SchemaResolveVisitor visitor, SQLInsertStatement x) {
visitor.visit(query);
}

if (x instanceof OracleInsertStatement) {
SQLObject returning = ((OracleInsertStatement) x).getReturning();
if (returning != null) {
returning.accept(visitor);
}
}

visitor.popContext();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4806,9 +4806,13 @@ public boolean visit(SQLInSubQueryExpr x) {

@Override
public boolean visit(SQLListExpr x) {
print('(');
if (x.isParenthesized()) {
print('(');
}
printAndAccept(x.getItems(), ", ");
print(')');
if (x.isParenthesized()) {
print(')');
}

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExpr;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleExpr;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleInsertStatement;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitorAdapter;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitorAdapter;
import com.alibaba.druid.sql.repository.SchemaObject;
Expand Down Expand Up @@ -1184,6 +1185,9 @@ public boolean visit(SQLInsertStatement x) {

accept(x.getColumns());
accept(x.getQuery());
if (x instanceof OracleInsertStatement) {
accept(((OracleInsertStatement) x).getReturning());
}

return false;
}
Expand Down Expand Up @@ -2148,6 +2152,7 @@ public boolean visit(SQLMethodInvokeExpr x) {
this.functions.add(x);

accept(x.getArguments());
accept(x.getFrom());
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,66 @@ public void test_0() throws Exception {
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "salary")));
}

public void test_1() throws Exception {
String sql = "BEGIN\n" +
"\tINSERT INTO employees (first_name, last_name, job_title)\n" +
"\tVALUES (?, ?, ?)\n" +
"\tRETURNING employee_id INTO ?;\n" +
"\tCOMMIT;\n" +
"END;";
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
print(statementList);

Assert.assertEquals(1, statementList.size());

OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
statemen.accept(visitor);

System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());

Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("employees")));

Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(4, visitor.getColumns().size());

Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "first_name")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "last_name")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "job_title")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "employee_id")));
}

public void test_2() throws Exception {
String sql = "SELECT employee_id, TO_CHAR(TRIM(LEADING 0 FROM hire_date))\n" +
"FROM employees\n" +
"WHERE department_id = 60\n" +
"ORDER BY employee_id";
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
print(statementList);

Assert.assertEquals(1, statementList.size());

OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
statemen.accept(visitor);

System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());

Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("employees")));

Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(3, visitor.getColumns().size());

Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "employee_id")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "hire_date")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "department_id")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void test_0() throws Exception {
+ "\t, in_ProductNo, in_Order_Payment, in_OrderCnt, in_Commision, in_RebateFee\n"
+ "\t, in_ProdDiscount, in_CreditCardFee, in_ServiceFee, in_ActivityNo, in_ProductShelfNo\n"
+ "\t, IN_PayOrganID, IN_CorrelationID\n"
+ "INTO (:b0, :b1, :b2:b3, :b4:b5, :b6:b7, :b8:b9, :b10:b11, :b12:b13, :b14:b15, :b16:b17, :b18:b19, :b20:b21, :b22:b23, :b24:b25, :b26:b27, :b28:b29, :b30:b31, :b32:b33, :b34:b35, :b36:b37, :b38:b39, :b40:b41, :b42:b43, :b44:b45, :b46:b47, :b48:b49, :b50:b51, :b52:b53, :b54:b55, :b56:b57, :b58:b59, :b60:b61, :b62:b63, :b64:b65, :b66:b67, :b68:b69, :b70:b71, :b72:b73, :b74:b75, :b76:b77, :b78:b79, :b80:b81, :b82:b83, :b84:b85, :b86:b87, :b88:b89, :b90:b91, :b92:b93, :b94:b95, :b96:b97, :b98:b99, :b100:b101, :b102:b103, :b104:b105)\n"
+ "INTO :b0, :b1, :b2:b3, :b4:b5, :b6:b7, :b8:b9, :b10:b11, :b12:b13, :b14:b15, :b16:b17, :b18:b19, :b20:b21, :b22:b23, :b24:b25, :b26:b27, :b28:b29, :b30:b31, :b32:b33, :b34:b35, :b36:b37, :b38:b39, :b40:b41, :b42:b43, :b44:b45, :b46:b47, :b48:b49, :b50:b51, :b52:b53, :b54:b55, :b56:b57, :b58:b59, :b60:b61, :b62:b63, :b64:b65, :b66:b67, :b68:b69, :b70:b71, :b72:b73, :b74:b75, :b76:b77, :b78:b79, :b80:b81, :b82:b83, :b84:b85, :b86:b87, :b88:b89, :b90:b91, :b92:b93, :b94:b95, :b96:b97, :b98:b99, :b100:b101, :b102:b103, :b104:b105\n"
+ "FROM b2b_payment_ReconDetail\n" + "WHERE (Recon_seq_id = :b106\n"
+ "\tAND transaction_id = :b107)", text);
}
Expand Down Expand Up @@ -166,7 +166,7 @@ public void test_2() throws Exception {
+ "\t, ACCOUNT_ID, SUBTOTAL_DETAILBILL_ID, NVL(ACCT_HOME_COUNTY, -1)\n"
+ "\t, TO_CHAR(NVL(BILLCYCLE_INURE_DATE, TO_DATE(TO_CHAR(GEN_TIME, 'yyyymm'), 'yyyymm')), 'yyyymmdd')\n"
+ "\t, NVL(BILLCYCLE_MONTH, -1)\n" + "\t, NVL(Detailbill_Flag, 0)\n"
+ "INTO (:b0, :b1, :b2, :b3, :b4, :b5, :b6, :b7, :b8, :b9, :b10, :b11, :b12, :b13, :b14, :b15, :b16, :b17, :b18, :b19, :b20:b21, :b22:b23, :b24:b25, :b26:b27, :b28:b29, :b30, :b31, :b32, :b33)\n"
+ "INTO :b0, :b1, :b2, :b3, :b4, :b5, :b6, :b7, :b8, :b9, :b10, :b11, :b12, :b13, :b14, :b15, :b16, :b17, :b18, :b19, :b20:b21, :b22:b23, :b24:b25, :b26:b27, :b28:b29, :b30, :b31, :b32, :b33\n"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+ "FROM DebtBill_Item\n" + "WHERE ROWID = :b34", text);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@

public class OracleSelectTest57 extends OracleTest {
public void test_0() throws Exception {
String sql = //
"SELECT TRIM(BOTH FROM EUCD) AS \"value\",NTLANG1 AS \"text\" " //
String sql = "SELECT TRIM(BOTH FROM EUCD) AS \"value\",NTLANG1 AS \"text\" "
+ " FROM T_HT_WREM_ENUMLANG_D"
+ " WHERE TYPE=?"
+ " ORDER BY \"value\" ASC"; //
+ " ORDER BY \"value\" ASC";

OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
Expand All @@ -49,8 +48,7 @@ public void test_0() throws Exception {
System.out.println("orderBy : " + visitor.getOrderByColumns());

Assert.assertEquals(1, visitor.getTables().size());

Assert.assertEquals(2, visitor.getColumns().size());
Assert.assertEquals(3, visitor.getColumns().size());

{
String text = SQLUtils.toOracleString(stmt);
Expand All @@ -73,4 +71,27 @@ public void test_0() throws Exception {

// Assert.assertTrue(visitor.getOrderByColumns().contains(new TableStat.Column("employees", "last_name")));
}

public void test_1() throws Exception {
String sql = "SELECT TRIM(BOTH 'x' FROM 'xJohnxx') FROM dual";

OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);

Assert.assertEquals(1, statementList.size());

OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);

System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
System.out.println("orderBy : " + visitor.getOrderByColumns());

Assert.assertEquals(0, visitor.getTables().size());
Assert.assertEquals(0, visitor.getColumns().size());
}
}
2 changes: 1 addition & 1 deletion core/src/test/resources/bvt/parser/postgresql/17.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ INSERT INTO profile_group_execution
, status, count, error_msg, error_code)
VALUES (now(), now(), ?, ?, ?
, ?, ?, ?, ?)
RETURNING (id, gmt_create, gmt_modified, group_id, mode, sql, status, count, error_msg, error_code)
RETURNING id, gmt_create, gmt_modified, group_id, mode, sql, status, count, error_msg, error_code
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

根据PG文档 returning 不需要括号:https://www.postgresql.org/docs/current/dml-returning.html

------------------------------------------------------------------------------------------------------------------------
insert into test1 as xx (tid,tid1,tvalue)
values (1,1,1),(2,2,2)
Expand Down