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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

accept table-qualified columns for renaming #33

Merged
merged 4 commits into from
May 23, 2024
Merged

Conversation

piranha
Copy link
Contributor

@piranha piranha commented May 15, 2024

Sooo after some pondering and experiments I extracted the whole data gathering facility because how am I going to get a table name in other way? 馃榿

Not to say this is not a best PR ever - I'm absolutely not sure what to do with schemas? Do we want specified schemas for tables too? I struggle to answer those questions, do you have any thoughts on what would be a more usable API?

Resolves #32

@piranha piranha self-assigned this May 15, 2024
@piranha piranha requested a review from tsmacdonald as a code owner May 15, 2024 15:04
{:tables {"a" "aa"}
:columns {{:table "a" :column "x"} "xx"
"y" "yy"}}
"SELECT aa.xx, b.x, b.yy FROM aa, b;")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have a test that flexed the table-and-schema-are-optional behavior. I guess that'd be a query like

select SUM(public.orders.amount) AS s, MAX(orders.amount) AS max, MIN(amount) AS min FROM public.orders

Copy link
Member

@tsmacdonald tsmacdonald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely getting hairier, but I don't have any good ideas to simplify it. Moving the core stuff into its own ns was definitely the right move.

@calherries
Copy link
Collaborator

calherries commented May 15, 2024

How about limiting the API to only work with :table and :column pairs, rather than also allow a :column without a :table? I imagine when we actually implement the renaming feature in Metabase we'll always know the table name for the column we'll be renaming.

Maybe it doesn't help simplify the most complex parts, but it's one less thing to test.

e.g. we wouldn't need this
https://github.com/metabase/macaw/pull/33/files#diff-dad1d50a3fac52a991059d8acf76e98b7e226ab906f1f4d9766edb658bbcf9c2R221

@piranha
Copy link
Contributor Author

piranha commented May 15, 2024

I'm also still on the fence about schemas. You mostly don't have schemas when working with sql queries, but sometimes you do... So I'm on the verge between "whatever" and "try hard to match rename without schema to column with schemas".

@piranha piranha requested a review from tsmacdonald May 22, 2024 09:39
Co-Authored-By: Chris Truter <chris@metabase.com>
Copy link
Contributor

@crisptrutski crisptrutski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handling so many new cases! 馃コ 馃コ 馃コ

The code can be tightened up a fair bit, and some more docstrings would be nice, but I think we can cycle back to that when we work on getting scopes for nested context working more robustly.

[macaw.core :as m]
[macaw.walk :as mw])
(:import
(net.sf.jsqlparser.schema Table)))

(set! *warn-on-reflection* true)

(defn- normalize-ws [s]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we want to just go back to the test helper now that the tests are passing?

I prefer having something like this anyway to toggle debugging:

(defn- debug-test-replacement [before replacements _]
  (m/replace-names before replacements))

Or maybe a bit fancier:

(defmacro test-replacement [before replacement after]
  `(let [result# (m/replace-names ~before ~replacements)]
     (when *debug-replace* #p result#)
     (is (= ~after result#)))

(binding [*debug-replace* true]
  (test-replacement X Y Z))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly prefer having calls to a tested function in place, since then you can execute it directly and see the result for yourself. So it's kind of unergonomic to have any wrappers...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so should we just totally scrap test-replacement?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove it honestly, it's less intrusive then. But that's up to you, I've merged the pr already. 馃榿

src/macaw/util.clj Show resolved Hide resolved
src/macaw/rewrite.clj Show resolved Hide resolved
src/macaw/rewrite.clj Show resolved Hide resolved
Copy link
Member

@tsmacdonald tsmacdonald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!

@piranha piranha merged commit 4904cb4 into master May 23, 2024
4 checks passed
@piranha piranha deleted the fully-qualified-renames branch May 23, 2024 12:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Accept fully-qualified columns and tables for renames
4 participants