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

account for group by columns #16

Merged
merged 1 commit into from
Apr 8, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions .dir-locals.el
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
((nil . ((indent-tabs-mode . nil) ; always use spaces for tabs
(require-final-newline . t))) ; add final newline on save
(clojure-mode . ((cider-preferred-build-tool . clojure-cli)
(cider-clojure-cli-aliases . "dev")
(cider-clojure-cli-aliases . "dev:user")
(cljr-favor-prefix-notation . nil)
(fill-column . 120)
(column-enforce-column . 120)
(clojure-docstring-fill-column . 120)
;; prefer keeping source width about ~118, GitHub seems to cut
;; off stuff at either 119 or 120 and it's nicer to look at
;; code in GH when you don't have to scroll back and forth
(fill-column . 118)
(whitespace-line-column . 118)
(column-enforce-column . 118)
(clojure-docstring-fill-column . 118)
(eval . (put-clojure-indent 'with-meta '(:form)))
(eval . (put-clojure-indent 'with-bindings* '(:form)))))
(markdown-mode . ((fill-column . 80)
Expand Down
20 changes: 15 additions & 5 deletions java/com/metabase/macaw/AstWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@
import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;
import net.sf.jsqlparser.statement.select.AllColumns;
import net.sf.jsqlparser.statement.select.AllTableColumns;
import net.sf.jsqlparser.statement.select.GroupByElement;
import net.sf.jsqlparser.statement.select.GroupByVisitor;
import net.sf.jsqlparser.statement.select.FromItemVisitor;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.LateralSubSelect;
Expand Down Expand Up @@ -209,7 +211,7 @@
* map and functions to implement the necessary behavior; no `reify` necesary.
*/
public class AstWalker<Acc> implements SelectVisitor, FromItemVisitor, ExpressionVisitor,
SelectItemVisitor, StatementVisitor {
SelectItemVisitor, StatementVisitor, GroupByVisitor {

public enum CallbackKey {
ALL_COLUMNS,
Expand Down Expand Up @@ -343,6 +345,10 @@ public void visit(PlainSelect plainSelect) {
if (plainSelect.getOracleHierarchical() != null) {
plainSelect.getOracleHierarchical().accept(this);
}

if (plainSelect.getGroupBy() != null) {
plainSelect.getGroupBy().accept(this);
}
}

@Override
Expand Down Expand Up @@ -799,7 +805,6 @@ public void visit(UserVariable var) {
@Override
public void visit(NumericBind bind) {


}

@Override
Expand Down Expand Up @@ -967,7 +972,6 @@ public void visit(RowGetExpression rowGetExpression) {
@Override
public void visit(HexValue hexValue) {


}

@Override
Expand Down Expand Up @@ -1012,13 +1016,11 @@ public void visit(TimeKeyExpression timeKeyExpression) {
@Override
public void visit(DateTimeLiteralExpression literal) {


}

@Override
public void visit(Commit commit) {


}

@Override
Expand All @@ -1044,6 +1046,14 @@ public void visit(ParenthesedFromItem parenthesis) {
visitJoins(parenthesis.getJoins());
}

@Override
public void visit(GroupByElement element) {
element.getGroupByExpressionList().accept(this);
for (ExpressionList exprList : element.getGroupingSets()) {
exprList.accept(this);
}
}

/**
* visit join block
*
Expand Down
5 changes: 4 additions & 1 deletion test/macaw/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
(deftest query->columns-test
(testing "Simple queries"
(is (= #{"foo" "bar" "id" "quux_id"}
(columns "select foo, bar from baz inner join quux on quux.id = baz.quux_id")))))
(columns "select foo, bar from baz inner join quux on quux.id = baz.quux_id"))))
(testing "'group by' columns present"
(is (= #{"id" "user_id"}
(columns "select id from orders group by user_id")))))

(deftest alias-inclusion-test
(testing "Aliases are not included"
Expand Down
Loading