Skip to content

Commit

Permalink
Merge pull request #5795 from lizongbo/master
Browse files Browse the repository at this point in the history
增强postgresql create index 的解析能力 #5780
  • Loading branch information
lizongbo authored Mar 22, 2024
2 parents 4a48c1a + c2203d4 commit fa195c0
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 8 deletions.
27 changes: 27 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!--
please follow the issue template below for bug reports and feature requests
for example, if you are reporting a bug, please provide the following information:
dbtype: mysql
dbversion: 5.7
duird version: 1.2.22
error sql: select * from test
error info: error message
-->
**dbtype:**
**dbversion:**
**druid verion:**
**error sql:**
**testcase code:**
**stacktrace info:**
**error info:**
---
<!--
Thanks for taking the time to create an issue. Please read the following:
- For bugs, specify affected versions and explain what you are trying to do.
- For enhancements, provide context and describe the problem.
Issue or Pull Request? Create only one, not both. GitHub treats them as the same.
If unsure, start with an issue, and if you submit a pull request later, the
issue will be closed as superseded.
-->
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
public final class SQLSelectOrderByItem extends SQLObjectImpl implements SQLReplaceable {
protected SQLExpr expr;
protected String collate;
protected SQLExpr opclass;

protected SQLOrderingSpecification type;
protected NullsOrderType nullsOrderType;

Expand Down Expand Up @@ -60,6 +62,14 @@ public void setCollate(String collate) {
this.collate = collate;
}

public SQLExpr getOpclass() {
return opclass;
}

public void setOpclass(SQLExpr opclass) {
this.opclass = opclass;
}

public SQLOrderingSpecification getType() {
return this.type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2987,8 +2987,17 @@ public SQLSelectOrderByItem parseSelectOrderByItem() {
String collate = lexer.stringVal();
item.setCollate(collate);
lexer.nextToken();
if (lexer.token == Token.DOT) {
lexer.nextToken();
String collateOther = lexer.stringVal();
item.setCollate(collate + "." + collateOther);
lexer.nextToken();
}
}
if (lexer.token == Token.LITERAL_ALIAS) {
SQLExpr name = this.expr();
item.setOpclass(name);
}

if (lexer.token == Token.ASC) {
lexer.nextToken();
item.setType(SQLOrderingSpecification.ASC);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2804,18 +2804,20 @@ public boolean visit(SQLSelectOrderByItem x) {
printExpr(expr, parameterized);
}

SQLOrderingSpecification type = x.getType();
if (type != null) {
print(' ');
print0(ucase ? type.name : type.nameLCase);
}

String collate = x.getCollate();
if (collate != null) {
print0(ucase ? " COLLATE " : " collate ");
print0(collate);
}

if (x.getOpclass() != null) {
print(' ');
x.getOpclass().accept(this);
}
SQLOrderingSpecification type = x.getType();
if (type != null) {
print(' ');
print0(ucase ? type.name : type.nameLCase);
}
SQLSelectOrderByItem.NullsOrderType nullsOrderType = x.getNullsOrderType();
if (nullsOrderType != null) {
print(' ');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.alibaba.druid.bvt.sql.postgresql.issues;

import java.util.List;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.parser.SQLParserUtils;
import com.alibaba.druid.sql.parser.SQLStatementParser;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* @author lizongbo
* @see <a href="https://github.com/alibaba/druid/issues/5780">Issue来源</a>
* @see <a href="https://www.postgresql.org/docs/current/sql-altertable.html">ALTER TABLE</a>
* @see <a href="https://www.postgresql.org/docs/current/sql-createindex.html">CREATE INDEX</a>
*/
public class Issue5780 {

@Test
public void test_parse_alter_table_sql() {
for (DbType dbType : new DbType[]{DbType.postgresql, DbType.greenplum, DbType.edb}) {

for (String sql : new String[]{
"CREATE INDEX \"index_log\" ON \"public\".\"check_log\" USING btree (\n" +
" \"t_no\" COLLATE \"pg_catalog\".\"default\" \"pg_catalog\".\"text_ops\" ASC NULLS LAST\n" +
");",
//"CREATE UNIQUE INDEX title_idx ON films (title);",
//"CREATE UNIQUE INDEX title_idx ON films (title) INCLUDE (director, rating);",
// "CREATE INDEX title_idx ON films (title) WITH (deduplicate_items = off);",
// "CREATE INDEX ON films ((lower(title)));",
// "CREATE INDEX title_idx_german ON films (title COLLATE \"de_DE\");",
// "CREATE INDEX title_idx_nulls_low ON films (title NULLS FIRST);",
// "CREATE UNIQUE INDEX title_idx ON films (title) WITH (fillfactor = 70);",
// "CREATE INDEX gin_idx ON documents_table USING GIN (locations) WITH (fastupdate = off);",
// "CREATE INDEX code_idx ON films (code) TABLESPACE indexspace;",
// "CREATE INDEX pointloc\n"
// + " ON points USING gist (box(location,location));",
// "CREATE INDEX CONCURRENTLY sales_quantity_index ON sales_table (quantity);",
}) {
System.out.println("原始的sql===" + sql);
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> statementList = parser.parseStatementList();
System.out.println("生成的sql===" + statementList);
StringBuilder sb = new StringBuilder();
for (SQLStatement statement : statementList) {
sb.append(statement.toString()).append(";");
}
sb.deleteCharAt(sb.length() - 1);
parser = SQLParserUtils.createSQLStatementParser(sb.toString(), dbType);
List<SQLStatement> statementListNew = parser.parseStatementList();
System.out.println("重新解析再生成的sql===" + statementListNew);
assertEquals(statementList.toString(), statementListNew.toString());
}
}
}
}

0 comments on commit fa195c0

Please sign in to comment.