Skip to content

Commit

Permalink
修复mysql语句中partition和别名的顺序问题 #5351
Browse files Browse the repository at this point in the history
修复mysql语句中partition和别名的顺序问题 #5351
  • Loading branch information
lizongbo authored and wenshao committed Jul 9, 2023
1 parent 16d9677 commit e766f53
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3599,6 +3599,12 @@ public boolean visit(SQLExprTableSource x) {
sampling.accept(this);
}

if (x.getPartitionSize() > 0) {
print0(ucase ? " PARTITION (" : " partition (");
printlnAndAccept(x.getPartitions(), ", ");
print(')');
}

String alias = x.getAlias();
List<SQLName> columns = x.getColumnsDirect();
if (alias != null) {
Expand All @@ -3620,12 +3626,6 @@ public boolean visit(SQLExprTableSource x) {
x.getHints().get(i).accept(this);
}

if (x.getPartitionSize() > 0) {
print0(ucase ? " PARTITION (" : " partition (");
printlnAndAccept(x.getPartitions(), ", ");
print(')');
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.alibaba.druid.bvt.sql.mysql.select;

import java.util.List;

import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;

public class MySqlSelectTest_partition extends MysqlTest {
public void test_0() throws Exception {
String sql = "select\n"
+ " p.id as id,\n"
+ " c.template_no as templateNo\n"
+ " from\n"
+ " contract_relation_party partition(p0) p\n"
+ " left join\n"
+ " contract partition(p0) c\n"
+ " on\n"
+ " p.contract_sn = c.contract_sn\n"
+ " and p.contract_id_no = c.id_no\n"
+ " where p.seal_type is null\n"
+ " order by p.update_time asc\n"
+ " limit 5";

//System.out.println(sql);

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

assertEquals(1, statementList.size());
//System.out.println(stmt.toString());
assertEquals("SELECT p.id AS id, c.template_no AS templateNo\n"
+ "FROM contract_relation_party PARTITION (p0) p\n"
+ "\tLEFT JOIN contract PARTITION (p0) c\n"
+ "\tON p.contract_sn = c.contract_sn\n"
+ "\t\tAND p.contract_id_no = c.id_no\n"
+ "WHERE p.seal_type IS NULL\n"
+ "ORDER BY p.update_time ASC\n"
+ "LIMIT 5", stmt.toString());
}
}

0 comments on commit e766f53

Please sign in to comment.