Skip to content

Postgres adapter: Allow fields in json_extract_path #660

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

Merged

Conversation

greg-rychlewski
Copy link
Member

@greg-rychlewski greg-rychlewski commented Apr 12, 2025

Closes #646
Companion Ecto PR: elixir-ecto/ecto#4604

I'm pretty sure we can do this. I've shown how it can be accomplished with Postgres in this PR. I left out MySQL for now for a few reasons..mainly because I am tired, but also because if I'm making a horrible mistake then I want to save some wasted effort.

If no one can see a reason why this is bad, I can add the MySQL stuff!

@greg-rychlewski
Copy link
Member Author

greg-rychlewski commented Apr 12, 2025

Actually I did miss something. I've only tested with with :map type. For embeds we validate the path is valid based on the fields in the schema. Which you can't do with field names. So the planner has to be modified to skip those instead of raising.

edit: added to the Ecto PR

@greg-rychlewski
Copy link
Member Author

@josevalim After looking into it closer, MySQL is a bit more awkward than postgres. It doesn't have a syntax for just plopping in the field reference. You have to use their concat function to build up the string properly.

It seems like a waste to use concat if there are no fields, so I was thinking we might be able to put in the metadata of json_extract_path whether there is a field or not and use that to determine which way to build up the query.

If that sounds like an ok direction to explore, would it be alright if I send a follow up PR shortly and merge this one as is?

@greg-rychlewski
Copy link
Member Author

Maybe we don't have to do it in Ecto actually. We can probably figure it out in the adapter.

@josevalim
Copy link
Member

Maybe we don't have to do it in Ecto actually. We can probably figure it out in the adapter.

that's what I was thinking. those are static paths anyway, they are not meant to be very long.

@greg-rychlewski
Copy link
Member Author

@josevalim Sorry for all the back and forth. So there is another complication. In MySQL you need to know whether you are taking about an integer or string because the syntax is different between them: [int] vs .string. So when we have source fields that comes with some complications.

I think it will need some careful thinking whether we can find a sane way to do it for MySQL. Do you mind if I just merge the postgres stuff?

@josevalim
Copy link
Member

We are completely fine with adding database specific functionality, as long as we raise for the other adapters that don't actually support it. :) Go for it!

@greg-rychlewski greg-rychlewski merged commit 486a3ad into elixir-ecto:master Apr 13, 2025
20 of 22 checks passed
@greg-rychlewski greg-rychlewski deleted the json_path_with_fields branch April 13, 2025 13:11
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.

Using column references in json_extract_path doesn't work
2 participants