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

Can't index Noda Time types #2415

Open
Leh2 opened this issue Dec 5, 2022 · 2 comments
Open

Can't index Noda Time types #2415

Leh2 opened this issue Dec 5, 2022 · 2 comments
Milestone

Comments

@Leh2
Copy link
Contributor

Leh2 commented Dec 5, 2022

Indexing Node Time types fails with

functions in index expression must be marked IMMUTABLE

Index example: CREATE INDEX mt_doc_targetwithdates_idx_local_date ON noda_time_acceptance.mt_doc_targetwithdates USING btree ((CAST(data ->> 'LocalDate' as date)));

Added failing test here #2413

@mysticmind
Copy link
Member

mysticmind commented Dec 22, 2022

I think it is going to be a bit tricky to deal with. This will require a wrapper function for each of the mapped NodaTime Postgres types which does the cast and is marked immutable to circumvent functions in index expression must be marked IMMUTABLE issue:

CREATE
OR REPLACE FUNCTION mt_to_date_immutable(data varchar) RETURNS date AS
$$
BEGIN
return CAST(data as date);
END;
$$
LANGUAGE plpgsql immutable;

and then while creating index, you will use the above function instead of the cast as below:

CREATE INDEX mt_doc_targetwithdates_idx_local_date ON noda_time_acceptance.mt_doc_targetwithdates 
USING btree (mt_to_date_immutable(data ->> 'LocalDate'));

Possibly an alternative and easier approach is to create a duplicate field with relevant type and create an index on it. This won't require any changes on Marten as such. You could try this and confirm. If this works well then we will add docs for the same.

@Leh2
Copy link
Contributor Author

Leh2 commented Jan 2, 2023

Creating a duplicated field works, but if NodaTime is the preferred way of working with date and time (according to Npgsql) I think this should be supported.

With duplicated field you also get a potential unused index if you need a multicolumn index.

@jeremydmiller jeremydmiller added this to the 7.1.0 milestone Feb 28, 2024
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

3 participants