Skip to content

Commit

Permalink
Infer a table for column if there is only one table mentioned (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
piranha authored May 9, 2024
1 parent 8d587e3 commit 9491bf0
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
(b/javac (merge default-options
opts
{:src-dirs ["java"]
:javac-opts ["--release" "11"]})))
:javac-opts ["--release" "11" "-Xlint:-options" #_"-Xlint:unchecked"]})))

(defn jar [opts]
(println "\nStarting to build a JAR...")
Expand Down
3 changes: 2 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
:aliases
{:dev
{:extra-deps
{io.github.metabase/hawk {:sha "539eefaa31a43d52d7c9b5731f471bb6742e7131"}}
{io.github.metabase/hawk {:sha "539eefaa31a43d52d7c9b5731f471bb6742e7131"}
virgil/virgil {:mvn/version "0.3.0"}}

:extra-paths
["test"]
Expand Down
2 changes: 2 additions & 0 deletions java/com/metabase/macaw/AstWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ public AstWalker(Map<CallbackKey, IFn> rawCallbacks, Acc val) {
/**
* Safely invoke the given callback by name.
*/
@SuppressWarnings("unchecked")
public void invokeCallback(CallbackKey key, Object visitedItem) {
IFn callback = this.callbacks.get(key);
if (callback != null) {
Expand Down Expand Up @@ -882,6 +883,7 @@ public void visit(Delete delete) {
popContext(); // DELETE
}

@SuppressWarnings("deprecation")
@Override
public void visit(Update update) {
pushContext(UPDATE);
Expand Down
7 changes: 5 additions & 2 deletions src/macaw/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@
(defn- make-column [alias-map table-map ^Column c]
(merge
{:column (.getColumnName c)}
(when-let [t (.getTable c)]
(if-let [t (.getTable c)]
(or
(get alias-map (.getName t))
(:component (get table-map (.getName t)))))))
(:component (get table-map (.getName t))))
;; if we see only a single table, we can safely say it's the table of that column
(when (= (count table-map) 1)
(:component (val (first table-map)))))))

(defn- alias-mapping
[^Table table]
Expand Down
18 changes: 9 additions & 9 deletions test/macaw/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
{:column "quux_id" :table "baz"}}
(columns "SELECT foo, bar FROM baz INNER JOIN quux ON quux.id = baz.quux_id"))))
(testing "'group by' columns present"
(is (= #{{:column "id"}
{:column "user_id"}}
(is (= #{{:column "id" :table "orders"}
{:column "user_id" :table "orders"}}
(columns "SELECT id FROM orders GROUP BY user_id"))))
(testing "table alias present"
(is (= #{{:column "id" :table "orders" :schema "public"}}
Expand Down Expand Up @@ -165,26 +165,26 @@
(deftest context-test
(testing "Sub-select with outer wildcard"
(is (= {:columns
#{{:component {:column "total"}, :context ["SELECT" "SUB_SELECT" "FROM" "SELECT"]}
{:component {:column "id"}, :context ["SELECT" "SUB_SELECT" "FROM" "SELECT"]}
{:component {:column "total"}, :context ["WHERE" "JOIN" "FROM" "SELECT"]}},
#{{:component {:column "total" :table "orders"}, :context ["SELECT" "SUB_SELECT" "FROM" "SELECT"]}
{:component {:column "id" :table "orders"}, :context ["SELECT" "SUB_SELECT" "FROM" "SELECT"]}
{:component {:column "total" :table "orders"}, :context ["WHERE" "JOIN" "FROM" "SELECT"]}},
:has-wildcard? #{{:component true, :context ["SELECT"]}},
:mutation-commands #{},
:tables #{{:component {:table "orders"}, :context ["FROM" "SELECT" "SUB_SELECT" "FROM" "SELECT"]}},
:table-wildcards #{}}
(components "SELECT * FROM (SELECT id, total FROM orders) WHERE total > 10"))))
(testing "Sub-select with inner wildcard"
(is (= {:columns
#{{:component {:column "id"}, :context ["SELECT"]}
{:component {:column "total"}, :context ["SELECT"]}
{:component {:column "total"}, :context ["WHERE" "JOIN" "FROM" "SELECT"]}},
#{{:component {:column "id" :table "orders"}, :context ["SELECT"]}
{:component {:column "total" :table "orders"}, :context ["SELECT"]}
{:component {:column "total" :table "orders"}, :context ["WHERE" "JOIN" "FROM" "SELECT"]}},
:has-wildcard? #{{:component true, :context ["SELECT" "SUB_SELECT" "FROM" "SELECT"]}},
:mutation-commands #{},
:tables #{{:component {:table "orders"}, :context ["FROM" "SELECT" "SUB_SELECT" "FROM" "SELECT"]}},
:table-wildcards #{}}
(components "SELECT id, total FROM (SELECT * FROM orders) WHERE total > 10"))))
(testing "Sub-select with dual wildcards"
(is (= {:columns #{{:component {:column "total"}, :context ["WHERE" "JOIN" "FROM" "SELECT"]}},
(is (= {:columns #{{:component {:column "total" :table "orders"}, :context ["WHERE" "JOIN" "FROM" "SELECT"]}},
:has-wildcard?
#{{:component true, :context ["SELECT" "SUB_SELECT" "FROM" "SELECT"]}
{:component true, :context ["SELECT"]}},
Expand Down

0 comments on commit 9491bf0

Please sign in to comment.