mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
postgresql parser support distinct on, fix #4063
This commit is contained in:
@@ -1125,6 +1125,16 @@ public class SQLExprParser extends SQLParser {
|
||||
break;
|
||||
}
|
||||
throw new ParserException("ERROR. " + lexer.info());
|
||||
case ON:
|
||||
if (dbType == DbType.postgresql) {
|
||||
String methodName = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
if (lexer.token == Token.LPAREN) {
|
||||
sqlExpr = this.methodRest(new SQLIdentifierExpr(methodName), true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
throw new ParserException("ERROR. " + lexer.info());
|
||||
default:
|
||||
throw new ParserException("ERROR. " + lexer.info());
|
||||
}
|
||||
|
||||
@@ -48,6 +48,10 @@ public class PostgresqlResourceTest extends PGTest {
|
||||
exec_test("bvt/parser/postgresql-2.txt");
|
||||
}
|
||||
|
||||
public void test_3() throws Exception {
|
||||
exec_test("bvt/parser/postgresql-3.txt");
|
||||
}
|
||||
|
||||
public void exec_test(String resource) throws Exception {
|
||||
System.out.println(resource);
|
||||
InputStream is = null;
|
||||
@@ -59,16 +63,19 @@ public class PostgresqlResourceTest extends PGTest {
|
||||
String[] items = input.split("---------------------------");
|
||||
String sql = items[0].trim();
|
||||
String expect = items[1].trim();
|
||||
if (expect != null) {
|
||||
expect = expect.replaceAll("\\r\\n", "\n");
|
||||
}
|
||||
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println(sql);
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
@@ -77,6 +84,10 @@ public class PostgresqlResourceTest extends PGTest {
|
||||
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
|
||||
if (expect != null && !expect.isEmpty()) {
|
||||
assertEquals(expect, stmt.toString());
|
||||
}
|
||||
}
|
||||
|
||||
void mergValidate(String sql, String expect) {
|
||||
|
||||
@@ -9,11 +9,14 @@ WITH RECURSIVE employee_recursive(distance, employee_name, manager_name) AS (
|
||||
)
|
||||
SELECT distance, employee_name FROM employee_recursive;
|
||||
---------------------------
|
||||
SELECT e1.last_name
|
||||
FROM employees e1
|
||||
WHERE f(CURSOR((
|
||||
SELECT e2.hire_date
|
||||
FROM employees e2
|
||||
WHERE e1.employee_id = e2.manager_id
|
||||
)), e1.hire_date) = 1
|
||||
ORDER BY last_name;
|
||||
WITH RECURSIVE employee_recursive (distance, employee_name, manager_name) AS (
|
||||
SELECT 1, employee_name, manager_name
|
||||
FROM employee
|
||||
WHERE manager_name = 'Mary'
|
||||
UNION ALL
|
||||
SELECT er.distance + 1, e.employee_name, e.manager_name
|
||||
FROM employee_recursive er, employee e
|
||||
WHERE er.employee_name = e.manager_name
|
||||
)
|
||||
SELECT distance, employee_name
|
||||
FROM employee_recursive;
|
||||
@@ -8,11 +8,9 @@ aid bigint
|
||||
PARTITION BY LIST(ds);
|
||||
|
||||
---------------------------
|
||||
SELECT e1.last_name
|
||||
FROM employees e1
|
||||
WHERE f(CURSOR((
|
||||
SELECT e2.hire_date
|
||||
FROM employees e2
|
||||
WHERE e1.employee_id = e2.manager_id
|
||||
)), e1.hire_date) = 1
|
||||
ORDER BY last_name;
|
||||
CREATE TABLE IF NOT EXISTS test_dim_segment (
|
||||
aid bigint,
|
||||
segment_id bigint,
|
||||
segment_name text,
|
||||
ds text NOT NULL
|
||||
);
|
||||
|
||||
@@ -4,11 +4,7 @@ alter table ACT_RU_INTEGRATION
|
||||
references ACT_RU_EXECUTION (ID_)
|
||||
on delete cascade
|
||||
---------------------------
|
||||
SELECT e1.last_name
|
||||
FROM employees e1
|
||||
WHERE f(CURSOR((
|
||||
SELECT e2.hire_date
|
||||
FROM employees e2
|
||||
WHERE e1.employee_id = e2.manager_id
|
||||
)), e1.hire_date) = 1
|
||||
ORDER BY last_name;
|
||||
ALTER TABLE ACT_RU_INTEGRATION
|
||||
ADD CONSTRAINT ACT_FK_INT_EXECUTION FOREIGN KEY (EXECUTION_ID_)
|
||||
REFERENCES ACT_RU_EXECUTION (ID_)
|
||||
ON DELETE CASCADE
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
create table s_2c3e24b36890b0d31aeca286e84c6471 as (
|
||||
SELECT DISTINCT ON (sn) sn, num01 FROM "s_f73e29e1c9830041f1bdfa8_2c3e24b36890b0d31aeca286e84c6471"
|
||||
);
|
||||
---------------------------
|
||||
CREATE TABLE s_2c3e24b36890b0d31aeca286e84c6471
|
||||
AS
|
||||
SELECT DISTINCT ON(sn) AS sn, num01
|
||||
FROM "s_f73e29e1c9830041f1bdfa8_2c3e24b36890b0d31aeca286e84c6471";
|
||||
Reference in New Issue
Block a user