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

refactor(pg): store nil map as empty {} #6929

Merged

Conversation

janisz
Copy link
Contributor

@janisz janisz commented Jul 13, 2023

Description

Currently we store nil map as 'null' (string). In next version of pgx the semantic has changed and it will be stored as null.
This PR seal this behaviour and whenever we would like to store nil map it will be replaced with {} (empty but not nil map).

TestIndex/TestMapHighlights/key_does_not_exist shows the difference between having 'null' and null in JSON field and null always returns false so negative queries will not work correctly.

Checklist

  • Investigated and inspected CI test results
  • Unit test and regression tests added
  • Evaluated and added CHANGELOG entry if required
  • Determined and documented upgrade steps
  • Documented user facing changes (create PR based on openshift/openshift-docs and merge into rhacs-docs)

If any of these don't apply, please comment below.

Testing Performed

CI

@janisz janisz requested a review from a team as a code owner July 13, 2023 17:07
@janisz janisz requested a review from a team July 13, 2023 17:13
@janisz janisz changed the title refactor(pg): store empty map as {} not null refactor(pg): store nil map as empty; not null Jul 13, 2023
@roxbot
Copy link
Contributor

roxbot commented Jul 13, 2023

Images are ready for the commit at 7d1d2f6.

To use with deploy scripts, first export MAIN_IMAGE_TAG=4.1.x-525-g7d1d2f6084.

@janisz janisz requested a review from connorgorman July 13, 2023 17:26
@connorgorman
Copy link
Collaborator

What about all the preexisting entries in the DB using the old format?

@janisz
Copy link
Contributor Author

janisz commented Jul 13, 2023

What about all the pre existing entries in the DB using the old format?

I think they are fine. Queries will be the same a before and will work, when upsert will happen we will replace them with {}. I thought about using v4 approach and store 'null' but this doesn't make much sense.

@connorgorman
Copy link
Collaborator

If we change all the values to {}, will pgx v4 still work the same way? We need to ensure any changes are completely backwards compatible

@janisz
Copy link
Contributor Author

janisz commented Jul 13, 2023

That's tricky. If we want be 100% backward compatible then we should store 'null'.

The only incompatibility I could think of is a query that checks exact value or if value is a string and not map. I think our code does not allow such queries but I'm not an expert in this area. @md2119 do we expose any other queries than tested for maps?

@dashrews78
Copy link
Contributor

That's tricky. If we want be 100% backward compatible then we should store 'null'.

The only incompatibility I could think of is a query that checks exact value or if value is a string and not map. I think our code does not allow such queries but I'm not an expert in this area. @md2119 do we expose any other queries than tested for maps?

We pretty much have to be backwards compatible in the event of a rollback from 4.2 to 4.1. Greatly increases the amount of testing required to verify that we can make this change.

@janisz janisz force-pushed the master-janisz/07-13-refactor_pg_store_empty_map_as_not_null branch from 26a47a2 to bea289d Compare July 14, 2023 11:32
@janisz janisz changed the base branch from master to master-janisz/07-14-test_pg_add_test_for_nil_map_serialisation July 14, 2023 11:32
@janisz janisz changed the title refactor(pg): store nil map as empty; not null refactor(pg): store nil map as 'null' Jul 14, 2023
@janisz
Copy link
Contributor Author

janisz commented Jul 14, 2023

@connorgorman @dashrews78 I added test for this behaviour and changed from {} to 'null'

@janisz janisz force-pushed the master-janisz/07-14-test_pg_add_test_for_nil_map_serialisation branch from 2ad6341 to 8f85554 Compare July 14, 2023 12:18
@janisz janisz force-pushed the master-janisz/07-13-refactor_pg_store_empty_map_as_not_null branch from bea289d to da0fb0b Compare July 14, 2023 12:19
@janisz janisz mentioned this pull request Jul 14, 2023
5 tasks
@janisz
Copy link
Contributor Author

janisz commented Jul 17, 2023

/retest

@janisz janisz requested a review from dashrews78 July 18, 2023 10:50
@janisz janisz force-pushed the master-janisz/07-14-test_pg_add_test_for_nil_map_serialisation branch from 8f85554 to e6bb503 Compare July 20, 2023 11:16
Base automatically changed from master-janisz/07-14-test_pg_add_test_for_nil_map_serialisation to master July 24, 2023 11:05
@janisz janisz changed the title refactor(pg): store nil map as 'null' refactor(pg): store nil map as empty {} Jul 24, 2023
@janisz janisz force-pushed the master-janisz/07-13-refactor_pg_store_empty_map_as_not_null branch from da0fb0b to baad7ef Compare July 24, 2023 16:20
Copy link
Contributor

@rhybrillou rhybrillou left a comment

Choose a reason for hiding this comment

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

please ensure value array construction consistency across insertInto and copyFrom

@@ -205,6 +205,8 @@ func {{ template "insertFunctionName" $schema }}({{ if eq (len $schema.Children)
pgutils.NilOrTime({{$field.Getter "obj"}}),
{{- else if eq $field.SQLType "uuid" }}
pgutils.NilOrUUID({{$field.Getter "obj"}}),
{{- else if eq $field.DataType "map" }}
pgutils.EmptyOrMap({{$field.Getter "obj"}}),
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the copyFrom template needs this change too (at least to be future-proof).
Maybe the {{- range $field := $schema.DBColumnFields -}} ... {{- end }} pattern could be extracted into another template.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, good point. I'll try to add a test for it too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@janisz janisz force-pushed the master-janisz/07-13-refactor_pg_store_empty_map_as_not_null branch from baad7ef to f688304 Compare July 25, 2023 10:45
@janisz janisz requested a review from rhybrillou July 25, 2023 10:45
@janisz
Copy link
Contributor Author

janisz commented Jul 25, 2023

/retest

@janisz janisz force-pushed the master-janisz/07-13-refactor_pg_store_empty_map_as_not_null branch from f688304 to 7ccf79d Compare July 25, 2023 16:19
@janisz janisz force-pushed the master-janisz/07-13-refactor_pg_store_empty_map_as_not_null branch from 7ccf79d to 7d1d2f6 Compare July 25, 2023 18:27
@janisz
Copy link
Contributor Author

janisz commented Jul 26, 2023

/retest

@janisz
Copy link
Contributor Author

janisz commented Jul 26, 2023

@janisz started a stack merge that includes this pull request via Graphite.

@janisz janisz merged commit 42f13d8 into master Jul 26, 2023
67 checks passed
@janisz
Copy link
Contributor Author

janisz commented Jul 26, 2023

@janisz merged this pull request with Graphite.

@janisz janisz deleted the master-janisz/07-13-refactor_pg_store_empty_map_as_not_null branch July 26, 2023 09:15
janisz added a commit that referenced this pull request Jul 26, 2023
## Description

Fixes:

- #6929 (comment)

## Checklist
- [ ] Investigated and inspected CI test results
- [ ] Unit test and regression tests added
- [ ] Evaluated and added CHANGELOG entry if required
- [ ] Determined and documented upgrade steps
- [ ] Documented user facing changes (create PR based on [openshift/openshift-docs](https://github.com/openshift/openshift-docs) and merge into [rhacs-docs](https://github.com/openshift/openshift-docs/tree/rhacs-docs))

If any of these don't apply, please comment below.

## Testing Performed

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

Successfully merging this pull request may close these issues.

None yet

5 participants