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

修复mysql语句中partition和别名的顺序问题 #5351 #5352

Merged
merged 1 commit into from
Jul 9, 2023
Merged
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 @@ -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());
}
}
Loading