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

geo: allow arbitrary precision when encoding to json #124312

Merged

Conversation

rharding6373
Copy link
Collaborator

We used to set a default limit of 9 digit precision when converting geo datums to json. However, some users expect higher precision in json. This PR changes the default precision to -1, which allows arbitrary precision with our geo library. Because of truncation when the data enters the database, there is a limit to the precision, but now the json output matches the database data.

Epic: none
Fixes: #124175

Release note: Do not limit precision when encoding geo data types to JSON.

@rharding6373 rharding6373 requested review from a team and mgartner and removed request for a team May 16, 2024 23:52
@rharding6373 rharding6373 requested a review from a team as a code owner May 16, 2024 23:52
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Collaborator Author

@rharding6373 rharding6373 left a comment

Choose a reason for hiding this comment

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

This is going to have some affect on the geo_builtins (at the very least need to clean up the info on the precision there), but wanted to get someone else's opinion on whether we want to treat the builtins with a different precision than other json encoded geo types.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @mgartner)

@rharding6373 rharding6373 added backport-23.2.x Flags PRs that need to be backported to 23.2. backport-24.1.x Flags PRs that need to be backported to 24.1. backport-23.1.x Flags PRs that need to be backported to 23.1 labels May 17, 2024
@rharding6373 rharding6373 force-pushed the 20240516_geo_json_prec_124175 branch from 9347d0f to 84ed6c4 Compare May 17, 2024 20:33
@rharding6373 rharding6373 requested a review from a team as a code owner May 17, 2024 20:33
Copy link
Collaborator Author

@rharding6373 rharding6373 left a comment

Choose a reason for hiding this comment

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

I have for the moment decided to remove the documentation about the default precision from the builtin info. PTAL.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @mgartner)


pkg/sql/logictest/testdata/logic_test/geospatial line 280 at r1 (raw file):

  st_asgeojson(g)::JSONB->'coordinates',
  st_asgeojson(tbl.*, 'g')::JSONB->'geometry'->'coordinates',
  st_asgeojson(tbl.*, 'g', 4)::JSONB->'geometry'->'coordinates'

Note that the output for the last one is wrong. I filed #124368 after verifying that it was present in 24.1.0 and not introduced by this change. It's a regression from 23.2.

Copy link
Collaborator

@mgartner mgartner left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 4 of 4 files at r1, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @rharding6373)


pkg/sql/logictest/testdata/logic_test/geospatial line 280 at r1 (raw file):

Previously, rharding6373 (Rachael Harding) wrote…

Note that the output for the last one is wrong. I filed #124368 after verifying that it was present in 24.1.0 and not introduced by this change. It's a regression from 23.2.

It looks like the second row is inconsistent with PG too, where I get:

            ?column?            |            ?column?            |            ?column?            |       ?column?
--------------------------------+--------------------------------+--------------------------------+----------------------
 [-123.45, 12.3456]             | [-123.45, 12.3456]             | [-123.45, 12.3456]             | [-123.45, 12.3456]
 [-123.456789012, 12.345678901] | [-123.456789012, 12.345678901] | [-123.456789012, 12.345678901] | [-123.4568, 12.3457]
 [-123.456789012, 12.345678901] | [-123.456789012, 12.345678901] | [-123.456789012, 12.345678901] | [-123.4568, 12.3457]

@yuzefovich yuzefovich requested a review from mgartner May 20, 2024 22:36
Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 4 of 4 files at r1, all commit messages.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @mgartner and @rharding6373)


-- commits line 14 at r1:
nit: add category (probably sql change).


pkg/sql/logictest/testdata/logic_test/geospatial line 280 at r1 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

It looks like the second row is inconsistent with PG too, where I get:

            ?column?            |            ?column?            |            ?column?            |       ?column?
--------------------------------+--------------------------------+--------------------------------+----------------------
 [-123.45, 12.3456]             | [-123.45, 12.3456]             | [-123.45, 12.3456]             | [-123.45, 12.3456]
 [-123.456789012, 12.345678901] | [-123.456789012, 12.345678901] | [-123.456789012, 12.345678901] | [-123.4568, 12.3457]
 [-123.456789012, 12.345678901] | [-123.456789012, 12.345678901] | [-123.456789012, 12.345678901] | [-123.4568, 12.3457]

I think this is because stAsGeoJSONFromTuple no longer uses numDecimalDigits argument (as of 6009141).

Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

Hm, upon closer reading of the code, I feel like the right solution might be to only adjust tree.AsJSON codepath. For example, according to https://postgis.net/docs/ST_AsGeoJSON.html st_asgeojson builtin defaults to 9 precision when the argument is not specified. In other words, perhaps we should audit every place where DefaultGeoJSONDecimalDigits is used and decide on a case-by-case basis whether 9 or -1 is used?

Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @mgartner and @rharding6373)

Copy link
Collaborator Author

@rharding6373 rharding6373 left a comment

Choose a reason for hiding this comment

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

Good catch. With your change in place, we could pass 9 for max_decimal_digits instead of the default, or we could keep the dfault as 9 and make a new constant for full precision and use that for non-builtin calls to ToJSON(). WDYT?

Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @mgartner)

Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

I think we should move geo.DefaultGeoJSONDecimalDigits back into builtins package (and unexport it), keep that constant to be 9 for builtins, and then specify -1 for two ToJSON calls in tree.AsJSON. We could define -1 as a separate exported constant in tree package and then also use it for two calls to geo.SpatialObjectToGeoJSON in eval.performCastWithoutPrecisionTruncation.

Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @mgartner and @rharding6373)

@rharding6373 rharding6373 force-pushed the 20240516_geo_json_prec_124175 branch from 84ed6c4 to 69a36cf Compare May 21, 2024 19:25
@rharding6373 rharding6373 requested a review from a team as a code owner May 21, 2024 19:25
Copy link
Collaborator Author

@rharding6373 rharding6373 left a comment

Choose a reason for hiding this comment

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

Done, except I put the full precision constant for -1 in the geo package, which seems like a better home for it than tree since it's geo-specific. Let me know if you have strong feelings and I'll move it. RFAL

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @mgartner and @yuzefovich)


-- commits line 14 at r1:

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: add category (probably sql change).

done


pkg/sql/logictest/testdata/logic_test/geospatial line 280 at r1 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

I think this is because stAsGeoJSONFromTuple no longer uses numDecimalDigits argument (as of 6009141).

I think this looks ok now.

Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

:lgtm: Thanks!

Placement sounds good to me - my only concern was not introducing dependency on geo into util/json (which 6009141 was fixing).

Reviewed 5 of 5 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @mgartner)

We used to set a default limit of 9 digit precision when converting geo
datums to json. However, some users expect higher precision in json.
This PR changes the default precision to -1, which allows arbitrary
precision with our geo library. Because of truncation when the data
enters the database, there is a limit to the precision, but now the
json output matches the database data.

Epic: none
Fixes: cockroachdb#124175

Release note(sql change): Do not limit precision when encoding geo data types to
JSON.
@rharding6373 rharding6373 force-pushed the 20240516_geo_json_prec_124175 branch from 69a36cf to 9a99313 Compare May 21, 2024 20:42
@rharding6373
Copy link
Collaborator Author

Unit test failure is known issue.

TFTRs!

bors r+

@craig craig bot merged commit 3707327 into cockroachdb:master May 21, 2024
20 of 22 checks passed
Copy link

blathers-crl bot commented May 21, 2024

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from 9a99313 to blathers/backport-release-23.1-124312: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 23.1.x failed. See errors above.


error creating merge commit from 9a99313 to blathers/backport-release-23.2-124312: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 23.2.x failed. See errors above.


error creating merge commit from 9a99313 to blathers/backport-release-24.1-124312: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 24.1.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-23.1.x Flags PRs that need to be backported to 23.1 backport-23.2.x Flags PRs that need to be backported to 23.2. backport-24.1.x Flags PRs that need to be backported to 24.1.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

geo: higher precision for DefaultGeoJSONDecimalDigits
4 participants