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: add support for WITH clauses #42

Merged
merged 1 commit into from Jan 28, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -161,7 +161,7 @@ ClientSideStatement getClientSideStatement() {
}

private final Set<String> ddlStatements = ImmutableSet.of("CREATE", "DROP", "ALTER");
private final Set<String> selectStatements = ImmutableSet.of("SELECT");
private final Set<String> selectStatements = ImmutableSet.of("SELECT", "WITH");
private final Set<String> dmlStatements = ImmutableSet.of("INSERT", "UPDATE", "DELETE");
private final Set<ClientSideStatementImpl> statements;

Expand Down
Expand Up @@ -266,6 +266,27 @@ public void testIsQuery() {
assertTrue(parser.isQuery("select * from foo"));
assertFalse(parser.isQuery("INSERT INTO FOO (ID, NAME) SELECT ID+1, NAME FROM FOO"));

assertTrue(
parser.isQuery(
"WITH subQ1 AS (SELECT SchoolID FROM Roster),\n"
+ " subQ2 AS (SELECT OpponentID FROM PlayerStats)\n"
+ "SELECT * FROM subQ1\n"
+ "UNION ALL\n"
+ "SELECT * FROM subQ2"));
assertTrue(
parser.isQuery(
"with subQ1 AS (SELECT SchoolID FROM Roster),\n"
+ " subQ2 AS (SELECT OpponentID FROM PlayerStats)\n"
+ "select * FROM subQ1\n"
+ "UNION ALL\n"
+ "SELECT * FROM subQ2"));
assertTrue(
parser
.parse(
Statement.of(
"-- this is a comment\nwith foo as (select * from bar)\nselect * from foo"))
.isQuery());

assertTrue(parser.parse(Statement.of("-- this is a comment\nselect * from foo")).isQuery());
assertTrue(
parser
Expand Down Expand Up @@ -310,6 +331,13 @@ public void testQueryHints() {
assertTrue(
parser.isQuery(
"@{JOIN_METHOD=HASH_JOIN}\n /* Multi line comment\n with more comments\n */SELECT * FROM PersonsTable"));
assertTrue(
parser.isQuery(
"@{JOIN_METHOD=HASH_JOIN} WITH subQ1 AS (SELECT SchoolID FROM Roster),\n"
+ " subQ2 AS (SELECT OpponentID FROM PlayerStats)\n"
+ "SELECT * FROM subQ1\n"
+ "UNION ALL\n"
+ "SELECT * FROM subQ2"));

// Invalid query hints.
assertFalse(parser.isQuery("@{JOIN_METHOD=HASH_JOIN SELECT * FROM PersonsTable"));
Expand Down
Expand Up @@ -668,3 +668,8 @@ WHERE SingerId=2;
SELECT COUNT(*) AS ACTUAL, 0 AS EXPECTED
FROM Songs
WHERE SingerId=2;

@EXPECT RESULT_SET
WITH Song2 AS (SELECT * FROM Songs WHERE SingerId=2)
SELECT COUNT(*) AS ACTUAL, 0 AS EXPECTED
FROM Song2;