-
Notifications
You must be signed in to change notification settings - Fork 1
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
Table extraction for basic queries #105
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package com.metabase.macaw; | ||
|
||
import net.sf.jsqlparser.schema.Table; | ||
import net.sf.jsqlparser.statement.Statement; | ||
import net.sf.jsqlparser.statement.select.*; | ||
|
||
import java.util.*; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Return a simplified query representation we can work with further, if possible. | ||
*/ | ||
@SuppressWarnings({ | ||
"PatternVariableCanBeUsed", "IfCanBeSwitch"} // don't force a newer JVM version | ||
) | ||
public final class BasicTableExtractor { | ||
|
||
public enum ErrorType { | ||
UNSUPPORTED_EXPRESSION, | ||
INVALID_QUERY, | ||
UNABLE_TO_PARSE | ||
} | ||
|
||
public static class AnalysisError extends RuntimeException { | ||
public final ErrorType errorType; | ||
public final Throwable cause; | ||
|
||
public AnalysisError(ErrorType errorType) { | ||
this.errorType = errorType; | ||
this.cause = null; | ||
} | ||
|
||
public AnalysisError(ErrorType errorType, Throwable cause) { | ||
this.errorType = errorType; | ||
this.cause = cause; | ||
} | ||
} | ||
|
||
public static Set<Table> getTables(Statement statement) { | ||
try { | ||
if (statement instanceof Select) { | ||
return getTables((Select) statement); | ||
} | ||
// This is not a query, it's probably a statement. | ||
throw new AnalysisError(ErrorType.INVALID_QUERY); | ||
} catch (IllegalArgumentException e) { | ||
// This query uses features that we do not yet support translating. | ||
throw new AnalysisError(ErrorType.UNABLE_TO_PARSE, e); | ||
} | ||
} | ||
|
||
private static Set<Table> getTables(Select select) { | ||
if (select instanceof PlainSelect) { | ||
return getTables(select.getPlainSelect()); | ||
} else { | ||
// We don't support more complex kinds of select statements yet. | ||
throw new AnalysisError(ErrorType.UNSUPPORTED_EXPRESSION); | ||
} | ||
} | ||
|
||
private static Set<Table> getTables(PlainSelect select) { | ||
// these are fine, but irrelevant | ||
// | ||
// - select.getDistinct() | ||
// - select.getHaving() | ||
// - select.getKsqlWindow() | ||
// - select.getMySqlHintStraightJoin() | ||
// - select.getMySqlSqlCacheFlag() | ||
// - select.getLimitBy() | ||
// - select.getOffset() | ||
// - select.getOptimizeFor() | ||
// - select.getOracleHint() | ||
// - select.getTop() | ||
// - select.getWait() | ||
|
||
// Not currently parseable | ||
if (select.getLateralViews() != null || | ||
select.getOracleHierarchical() != null || | ||
select.getWindowDefinitions() != null) { | ||
throw new AnalysisError(ErrorType.UNABLE_TO_PARSE); | ||
} | ||
|
||
// Not currently supported | ||
if (select.getWithItemsList() != null) { | ||
throw new AnalysisError(ErrorType.UNSUPPORTED_EXPRESSION); | ||
} | ||
|
||
// any of these - nope out | ||
if (select.getFetch() != null || | ||
select.getFirst() != null || | ||
select.getForClause() != null || | ||
select.getForMode() != null || | ||
select.getForUpdateTable() != null || | ||
select.getForXmlPath() != null || | ||
select.getIntoTables() != null || | ||
select.getIsolation() != null || | ||
select.getSkip() != null) { | ||
throw new AnalysisError(ErrorType.INVALID_QUERY); | ||
} | ||
|
||
Set<Table> tables = new HashSet<>(); | ||
|
||
for (SelectItem item : select.getSelectItems()) { | ||
if (item.getExpression() instanceof ParenthesedSelect) { | ||
// Do not allow sub-selects. | ||
throw new AnalysisError(ErrorType.UNSUPPORTED_EXPRESSION); | ||
} | ||
} | ||
|
||
Consumer<FromItem> pushOrThrow = (FromItem item) -> { | ||
if (item instanceof Table) { | ||
Table table = (Table) item; | ||
if (table.getName().contains("*")) { | ||
// Do not allow table wildcards. | ||
throw new AnalysisError(ErrorType.INVALID_QUERY); | ||
} | ||
tables.add(table); | ||
} else if (item instanceof TableFunction) { | ||
// Do not allow dynamic tables | ||
throw new AnalysisError(ErrorType.INVALID_QUERY); | ||
} else { | ||
// Only allow simple table references. | ||
throw new AnalysisError(ErrorType.UNSUPPORTED_EXPRESSION); | ||
} | ||
}; | ||
|
||
if (select.getFromItem() != null) { | ||
pushOrThrow.accept(select.getFromItem()); | ||
List<Join> joins = select.getJoins(); | ||
if (joins != null) { | ||
joins.stream().map(Join::getFromItem).forEach(pushOrThrow); | ||
} | ||
} | ||
return tables; | ||
} | ||
|
||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
{:tables #{{:table "foo"} | ||
{:table "bar"}} | ||
:source-columns #{}} | ||
:source-columns #{} | ||
:overrides | ||
{:basic-select {:error :macaw.error/unsupported-expression}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,7 +160,7 @@ FROM employees | |
WHERE salary > 60000 | ||
GROUP BY department; | ||
|
||
-- FIXTURE: cycle/cte | ||
-- FIXTURE: compound/cycle-cte | ||
-- we eventually want to check that the fields are cycled twice in the attribution of the final outputs. | ||
WITH b AS ( | ||
SELECT | ||
|
@@ -182,7 +182,7 @@ SELECT | |
z | ||
FROM c; | ||
|
||
-- FIXTURE: duplicate-scopes | ||
-- FIXTURE: compound/duplicate-scopes | ||
-- This is a minimal example to illustrate why we need to put an id on each scope. | ||
-- As we add more complex compound query tests this will become redundant, and we can then delete it | ||
-- we eventually want to check that the fields are cycled twice in the attribution of the final outputs. | ||
|
@@ -193,7 +193,7 @@ SELECT | |
c.x | ||
FROM b, c; | ||
|
||
-- FIXTURE: generate-series | ||
-- FIXTURE: dynamic/generate-series | ||
SELECT t.day::date AS date | ||
FROM generate_series(timestamp '2021-01-01', now(), interval '1 day') AS t(day) | ||
|
||
|
@@ -225,7 +225,7 @@ with final as ( | |
|
||
select * from final | ||
|
||
-- FIXTURE: shadow/subselect | ||
-- FIXTURE: compound/shadow-subselect | ||
SELECT | ||
e.id, | ||
e.name, | ||
|
@@ -240,7 +240,7 @@ FROM ( | |
GROUP BY first_name, last_name, department_id | ||
) e JOIN departments d ON d.id = e.department_id; | ||
|
||
-- FIXTURE: simple/select-into | ||
-- FIXTURE: mutation/select-into | ||
SELECT id, name | ||
INTO new_user_summary | ||
FROM user; | ||
|
@@ -294,3 +294,26 @@ EXEC sp_executesql @SQL | |
|
||
-- FIXTURE: string-concat | ||
SELECT x || y AS z FROM t | ||
|
||
-- FIXTURE: mutation/alter-table | ||
ALTER TABLE users | ||
ADD COLUMN email VARCHAR(255); | ||
|
||
-- FIXTURE: mutation/drop-table | ||
DROP TABLE IF EXISTS users; | ||
|
||
-- FIXTURE: mutation/truncate-table | ||
TRUNCATE TABLE users; | ||
|
||
-- FIXTURE: mutation/insert | ||
INSERT INTO users (name, email) | ||
VALUES ('Alice', '[email protected]'); | ||
|
||
-- FIXTURE: mutation/update | ||
UPDATE users | ||
SET email = '[email protected]' | ||
WHERE name = 'Alice'; | ||
|
||
-- FIXTURE: mutation/delete | ||
DELETE FROM users | ||
WHERE name = 'Alice'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
{:source-columns [{:table "invoice" :column "amount_paid_cents"} | ||
{:table "invoice" :column "id"} | ||
{:table "invoice" :column "is_deleted"}] | ||
:tables [{:table "invoice"}]} | ||
:tables [{:table "invoice"}] | ||
:overrides | ||
{:basic-select :macaw.error/unsupported-expression}} |
2 changes: 1 addition & 1 deletion
2
...ources/acceptance/simple__select_star.edn → ...ceptance/simple__select_star.analysis.edn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{:has-wildcard? true | ||
{:has-wildcard? #{true} | ||
:source-columns [] | ||
:tables [{:table "t"}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
{:error :macaw.error/illegal-expression | ||
:overrides :macaw.error/unable-to-parse} | ||
{:error :macaw.error/invalid-query | ||
:overrides {:ast-walker-1 :macaw.error/unable-to-parse}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
{:error :macaw.error/illegal-expression | ||
:overrides :macaw.error/unable-to-parse} | ||
{:error :macaw.error/invalid-query | ||
:overrides {:ast-walker-1 :macaw.error/unable-to-parse}} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very clean