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

Syntax for expanding records in PostgreSQL #474

Closed
p-himik opened this issue Mar 1, 2023 · 6 comments
Closed

Syntax for expanding records in PostgreSQL #474

p-himik opened this issue Mar 1, 2023 · 6 comments
Assignees

Comments

@p-himik
Copy link
Contributor

p-himik commented Mar 1, 2023

In a query like

SELECT (v).*
FROM (SELECT v, row_number() OVER () rn FROM (VALUES (1, 2), (2, 3)) v(a, b)) v2
WHERE rn = 2;

you have to use (v).* because just v would return a record and v.* would lead to an error.

Currently, the way to do it is to use [:raw ["(" (sql/format-entity :v) ").*"]].

@seancorfield seancorfield self-assigned this Mar 1, 2023
@seancorfield seancorfield added the needs analysis I need to think about this! label Mar 1, 2023
@seancorfield
Copy link
Owner

Something like this (name TBD):

user=> (sql/register-fn! :project (fn [_ [expr col]] (let [[sql & params] (sql/format-expr expr)] (into [(str "(" sql ")." (sql/format-entity col))] params))))
...
user=> (sql/format {:select [[[:project :v :*]]]})
["SELECT (v).*"]
user=> (sql/format {:select [[[:project :v :*]]]} {:quoted true})
["SELECT (\"v\").*"]
user=> (sql/format {:select [[[:project :v :a]]]} {:quoted true})
["SELECT (\"v\").\"a\""]
user=>

@seancorfield
Copy link
Owner

It turns out being able to dot-select from dynamic table or column names is useful so it makes more sense to split this in two:

  • we already have :nest to wrap in (..)
  • we could use :. (or '.) for the field selection since it sort of mirrors Clojure/Script

@seancorfield seancorfield added enhancement and removed needs analysis I need to think about this! labels Mar 1, 2023
seancorfield added a commit that referenced this issue Mar 2, 2023
@seancorfield
Copy link
Owner

This has been pushed to develop for testing. Documentation still to do, including a migration note about qualify.

@seancorfield
Copy link
Owner

Currently, dot takes precisely two arguments: an expression and a column. qualify used to take an arbitrary number of table/column names (not an expression). Should dot be expanded to support multiple parts (i.e., expr followed by multiple subparts)?

@p-himik
Copy link
Contributor Author

p-himik commented Mar 2, 2023

Personally, I have never seen any need to support an arbitrary amount of arguments. My own version of qualify supports just 2.

But it might be useful for people that often use schema.table.column.
Still can't imagine any need for 4-arity, but maybe some DBs do have that.

@seancorfield
Copy link
Owner

OK, I'll expand this to accept up to three arguments. And then document it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants