-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Feature request: column to column comparison #1105
Comments
Why not define that in the view you're exposing?, say like: create view api.persons as (
select id, first_name, last_name, first_name = last_name as is_tautonym
from private.persons
); A computed column will also work: create or replace function is_tautonym(api.persons) returns boolean as $$
select $1.first_name = $1.last_name;
$$ language SQL stable; Then you could do: /persons?is_tautonym=is.true For now I'd be against implementing this, as it's more clean to represent that kind of comparisons in the db. |
@steve-chavez That works for that one particular query, but not for any other queries (you can create any column-to-value comparison query otherwise). Is there any other reason besides the cleanliness of the PostgREST code? Alternatively, I could implement something like this in a custom version of PostgREST; but I'm wondering if would this be a difficult undertaking for someone with 0 Haskell knowledge? |
Considering the difficulty required towards implementing this and defining views/computed columns in a few lines of SQL, I'd say the latter is a much more easy option. I think this kind of comparisons are core business logic, and as so, they should be defined at the db level with an appropriate name/comment on the column so the REST API can have proper documentation, as your project matures you're gonna need this. Also there needs to be a limit in what kind of operations PostgREST exposes, having this somehow feels like a step closer to exposing custom functions in filters values. |
I would say that something like 'regular expression comparison operator' is a use-case specific feature, as in not everyone will want/need that (and it increases risk of ReDoS). Hence, that is something you can expose via custom functions if you need. I think of PostgREST as a way to create API for any PostgreSQL db, and a way to execute some common queries. I would say that I'll try to figure out how to implement this, but if someone experienced on contributing to this project can point me in the right direction (i.e. which file should I start at, which files would need to be modified to handle URL parsing and executing queries), I'd appreciate it! |
Better to drop the |
Giving it more thought, the only operators that make sense on column to column comparison are: I think we can have another HashMap of operators here, we'd need another constructor for them, those We would also need another set of tests for this, making sure that computed columns comparisons work as well. |
the need to strictly compare column values is very rare. It's even less likely that those two columns are in the same table (and this is what is being suggested/requested). As a test for my theory, let's see here examples of real tables with two columns where people would need to compare them. |
I currently use PostgREST to work with a massive biological database. I thought there was some problem with the way it was created so I needed to do some validation. There is a I definitely agree that being able to do complex manipulation of column values for comparison is great and needed pretty frequently. However, there are also other Postgres functions that are commonly used, yet not supported by PostgREST (GROUP BY for example). That been said, I personally can't work on this at the moment, busy with wrapping up my degree; plus I have 0 knowledge of Haskell (tried to follow Steve's dev guide, but can't make heads or tails of the language atm). :( But if this was implemented, I would definitely implement it in my application! |
@priyank-purohit For validating data, seems more suitable to query your PostgreSQL db directly(with I hesitate on including the column to column comparison feature on master for now, would like to see more compelling use cases first. |
Probably not so relevant - we are currently trying to do:
If we could introduce literals into the
|
The conclusion seems to be that this will not be implemented right now, therefore I'm closing this. For more complex scenarios then what the query string syntax has to offer, we can already use some very powerful tools: Views, Stored Procedures and Computed Columns. Taking the example mentioned above:
This can be solved with a computed column function that just returns -1, 0, or 1 for the comparison of |
Just noted that @sinogermany use case above can be solved with JSON Patch copy operation. So relating to #465. |
Description of issue
Currently PostgREST supports only column-to-value(s) comparison, however column-to-column comparisons are extremely common and too important to not support.
Implementation
This is a somewhat tricky feature to implement because you need to differentiate values from column names. The best solution I came up with is to add a
.c.
prefix between the operator and the column.For example,
?and=(first_name.c.eq.last_name)
vs?and=(first_name.eq.last_name)
to search for people with same first and last names vs. people whose first name is in the db as literal string 'last_name'.The text was updated successfully, but these errors were encountered: