Skip to content

Commit

Permalink
Fix output precision limit for double values (issue #118) (#119)
Browse files Browse the repository at this point in the history
Fix output precision limit for double values (issue #118)

pgSphere used its own setting (set_sphere_output_precision function)
to limit the output precision of double values, that could not be
greater than 15 significant digits (DBL_DIG). It introduced some
problems with dump/restore. PostgreSQL uses its own precision setting:
extra_float_digits. The PostgreSQL setting allows to use more significant
digits.

This patch changes the default pgSphere output behaviour to utilize
PostgreSQL extra_float_digits. Now, extra_float_digits is used by default,
until set_sphere_output_precision is called.

The old behaviour is kept for compatibility purposes. Once,
set_sphere_output_precision is called, pgSphere starts to use the old
behaviour (read, please, issue #118 discussion).

The patch introduces a new function - reset_sphere_output_precision.
It is used to reset to the PostgreSQL behaviour after using
set_sphere_output_precision.

* Update upgrade script (reset_sphere_output_precision function)

* Add test for pgSphere output precision with different settings

expected/output_precision.out   - PG 10-11
expected/output_precision_1.out - PG 12+

* Add extra_float_digits = 2 for epochprop and bounding_box_gist tests
  • Loading branch information
vitcpp authored Mar 26, 2024
1 parent 462fa03 commit 8394ae7
Show file tree
Hide file tree
Showing 13 changed files with 1,283 additions and 55 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ DATA_built = $(RELEASE_SQL) \
DOCS = README.pg_sphere COPYRIGHT.pg_sphere
TESTS = version tables points euler circle line ellipse poly path box \
index contains_ops contains_ops_compat bounding_box_gist gnomo \
epochprop contains overlaps spoint_brin sbox_brin selectivity knn
epochprop contains overlaps spoint_brin sbox_brin selectivity \
knn output_precision
REGRESS = init $(TESTS)

PG_CFLAGS += -DPGSPHERE_VERSION=$(PGSPHERE_VERSION)
Expand Down
37 changes: 19 additions & 18 deletions expected/bounding_box_gist.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
SET extra_float_digits = 2;
SET enable_seqscan=true;
CREATE TABLE bbox_ellipse (e sellipse not null);
INSERT INTO bbox_ellipse VALUES ('<{10d, 0.1d}, (0d,0d), 0d>');
Expand All @@ -20,19 +21,19 @@ SELECT COUNT(*) FROM bbox_ellipse WHERE spoint '(5d, 0d)' <@ e;
(1 row)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_ellipse WHERE spoint '(5d, 0d)' @ e;
QUERY PLAN
----------------------------------------------------------
QUERY PLAN
------------------------------------------------------------
Aggregate
-> Seq Scan on bbox_ellipse
Filter: ('(0.0872664625997165 , 0)'::spoint @ e)
Filter: ('(0.087266462599716474 , 0)'::spoint @ e)
(3 rows)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_ellipse WHERE spoint '(5d, 0d)' <@ e;
QUERY PLAN
-----------------------------------------------------------
QUERY PLAN
-------------------------------------------------------------
Aggregate
-> Seq Scan on bbox_ellipse
Filter: ('(0.0872664625997165 , 0)'::spoint <@ e)
Filter: ('(0.087266462599716474 , 0)'::spoint <@ e)
(3 rows)

-- The ellipse has semi-major axis length of 10 degrees along the equator,
Expand All @@ -53,19 +54,19 @@ SELECT COUNT(*) FROM bbox_ellipse WHERE spoint '(5d, 0d)' <@ e;
(1 row)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_ellipse WHERE spoint '(5d, 0d)' @ e;
QUERY PLAN
--------------------------------------------------------------
QUERY PLAN
----------------------------------------------------------------
Aggregate
-> Index Scan using idx_bbox_ellipse on bbox_ellipse
Index Cond: ('(0.0872664625997165 , 0)'::spoint @ e)
Index Cond: ('(0.087266462599716474 , 0)'::spoint @ e)
(3 rows)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_ellipse WHERE spoint '(5d, 0d)' <@ e;
QUERY PLAN
---------------------------------------------------------------
QUERY PLAN
-----------------------------------------------------------------
Aggregate
-> Index Scan using idx_bbox_ellipse on bbox_ellipse
Index Cond: ('(0.0872664625997165 , 0)'::spoint <@ e)
Index Cond: ('(0.087266462599716474 , 0)'::spoint <@ e)
(3 rows)

SET enable_seqscan=true;
Expand Down Expand Up @@ -170,11 +171,11 @@ SELECT COUNT(*) FROM bbox_path WHERE spoint '(0d, 0d)' <@ p;
(1 row)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_path WHERE sline(spoint '(0d, -10d)', spoint '(0d, 10d)') && p;
QUERY PLAN
--------------------------------------------------------------------------------------------------
QUERY PLAN
---------------------------------------------------------------------------------------------------------
Aggregate
-> Seq Scan on bbox_path
Filter: ('( 6.10865238198015, 1.5707963267949, 0, ZXZ ), 0.349065850398866'::sline && p)
Filter: ('( 6.1086523819801535, 1.5707963267948966, 0, ZXZ ), 0.34906585039886584'::sline && p)
(3 rows)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_path WHERE spoint '(0d, 0d)' @ p;
Expand Down Expand Up @@ -215,11 +216,11 @@ SELECT COUNT(*) FROM bbox_path WHERE spoint '(0d, 0d)' <@ p;
(1 row)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_path WHERE sline(spoint '(0d, -10d)', spoint '(0d, 10d)') && p;
QUERY PLAN
------------------------------------------------------------------------------------------------------
QUERY PLAN
-------------------------------------------------------------------------------------------------------------
Aggregate
-> Index Scan using idx_bbox_path on bbox_path
Index Cond: ('( 6.10865238198015, 1.5707963267949, 0, ZXZ ), 0.349065850398866'::sline && p)
Index Cond: ('( 6.1086523819801535, 1.5707963267948966, 0, ZXZ ), 0.34906585039886584'::sline && p)
(3 rows)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_path WHERE spoint '(0d, 0d)' @ p;
Expand Down
37 changes: 19 additions & 18 deletions expected/bounding_box_gist_1.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
SET extra_float_digits = 2;
SET enable_seqscan=true;
CREATE TABLE bbox_ellipse (e sellipse not null);
INSERT INTO bbox_ellipse VALUES ('<{10d, 0.1d}, (0d,0d), 0d>');
Expand All @@ -20,19 +21,19 @@ SELECT COUNT(*) FROM bbox_ellipse WHERE spoint '(5d, 0d)' <@ e;
(1 row)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_ellipse WHERE spoint '(5d, 0d)' @ e;
QUERY PLAN
----------------------------------------------------------
QUERY PLAN
-----------------------------------------------------------
Aggregate
-> Seq Scan on bbox_ellipse
Filter: ('(0.0872664625997165 , 0)'::spoint @ e)
Filter: ('(0.08726646259971647 , 0)'::spoint @ e)
(3 rows)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_ellipse WHERE spoint '(5d, 0d)' <@ e;
QUERY PLAN
-----------------------------------------------------------
QUERY PLAN
------------------------------------------------------------
Aggregate
-> Seq Scan on bbox_ellipse
Filter: ('(0.0872664625997165 , 0)'::spoint <@ e)
Filter: ('(0.08726646259971647 , 0)'::spoint <@ e)
(3 rows)

-- The ellipse has semi-major axis length of 10 degrees along the equator,
Expand All @@ -53,19 +54,19 @@ SELECT COUNT(*) FROM bbox_ellipse WHERE spoint '(5d, 0d)' <@ e;
(1 row)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_ellipse WHERE spoint '(5d, 0d)' @ e;
QUERY PLAN
--------------------------------------------------------------
QUERY PLAN
---------------------------------------------------------------
Aggregate
-> Index Scan using idx_bbox_ellipse on bbox_ellipse
Index Cond: (e ~ '(0.0872664625997165 , 0)'::spoint)
Index Cond: (e ~ '(0.08726646259971647 , 0)'::spoint)
(3 rows)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_ellipse WHERE spoint '(5d, 0d)' <@ e;
QUERY PLAN
---------------------------------------------------------------
QUERY PLAN
----------------------------------------------------------------
Aggregate
-> Index Scan using idx_bbox_ellipse on bbox_ellipse
Index Cond: (e @> '(0.0872664625997165 , 0)'::spoint)
Index Cond: (e @> '(0.08726646259971647 , 0)'::spoint)
(3 rows)

SET enable_seqscan=true;
Expand Down Expand Up @@ -170,11 +171,11 @@ SELECT COUNT(*) FROM bbox_path WHERE spoint '(0d, 0d)' <@ p;
(1 row)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_path WHERE sline(spoint '(0d, -10d)', spoint '(0d, 10d)') && p;
QUERY PLAN
--------------------------------------------------------------------------------------------------
QUERY PLAN
---------------------------------------------------------------------------------------------------------
Aggregate
-> Seq Scan on bbox_path
Filter: ('( 6.10865238198015, 1.5707963267949, 0, ZXZ ), 0.349065850398866'::sline && p)
Filter: ('( 6.1086523819801535, 1.5707963267948966, 0, ZXZ ), 0.34906585039886584'::sline && p)
(3 rows)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_path WHERE spoint '(0d, 0d)' @ p;
Expand Down Expand Up @@ -215,11 +216,11 @@ SELECT COUNT(*) FROM bbox_path WHERE spoint '(0d, 0d)' <@ p;
(1 row)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_path WHERE sline(spoint '(0d, -10d)', spoint '(0d, 10d)') && p;
QUERY PLAN
------------------------------------------------------------------------------------------------------
QUERY PLAN
-------------------------------------------------------------------------------------------------------------
Aggregate
-> Index Scan using idx_bbox_path on bbox_path
Index Cond: (p && '( 6.10865238198015, 1.5707963267949, 0, ZXZ ), 0.349065850398866'::sline)
Index Cond: (p && '( 6.1086523819801535, 1.5707963267948966, 0, ZXZ ), 0.34906585039886584'::sline)
(3 rows)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_path WHERE spoint '(0d, 0d)' @ p;
Expand Down
13 changes: 7 additions & 6 deletions expected/epochprop.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
SET extra_float_digits = 2;
SELECT
to_char(DEGREES(tp[1]), '999D9999999999'),
to_char(DEGREES(tp[2]), '999D9999999999'),
Expand Down Expand Up @@ -92,16 +93,16 @@ SELECT epoch_prop_pos(spoint(radians(269.45207695), radians(4.693364966)),
23,
RADIANS(-801.551/3.6e6), RADIANS(10362/3.6e6), -110,
20) AS tp;
tp
-----------------------------------------
(4.70274792658313 , 0.0829194509345993)
tp
---------------------------------------------
(4.7027479265831289 , 0.082919450934599334)
(1 row)

SELECT epoch_prop_pos(spoint(radians(269.45207695), radians(4.693364966)),
RADIANS(-801.551/3.6e6), RADIANS(10362/3.6e6),
20) AS tp;
tp
-----------------------------------------
(4.70274793061952 , 0.0829193989380876)
tp
---------------------------------------------
(4.7027479306195161 , 0.082919398938087627)
(1 row)

108 changes: 108 additions & 0 deletions expected/epochprop_1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
SET extra_float_digits = 2;
SELECT
to_char(DEGREES(tp[1]), '999D9999999999'),
to_char(DEGREES(tp[2]), '999D9999999999'),
to_char(tp[3], '999D999'),
to_char(DEGREES(tp[4])*3.6e6, '999D999'),
to_char(DEGREES(tp[5])*3.6e6, '99999D999'),
to_char(tp[6], '999D999')
FROM (
SELECT epoch_prop(spoint(radians(269.45207695), radians(4.693364966)),
546.9759,
RADIANS(-801.551/3.6e6), RADIANS(10362/3.6e6), -110,
-100) AS tp) AS q;
to_char | to_char | to_char | to_char | to_char | to_char
-----------------+-----------------+----------+----------+------------+----------
269.4742714391 | 4.4072939987 | 543.624 | -791.442 | 10235.412 | -110.450
(1 row)

SELECT
to_char(DEGREES(tp[1]), '999D9999999999'),
to_char(DEGREES(tp[2]), '999D9999999999'),
to_char(tp[3], '999D999'),
to_char(DEGREES(tp[4])*3.6e6, '999D999'),
to_char(DEGREES(tp[5])*3.6e6, '99999D999'),
to_char(tp[6], '999D999')
FROM (
SELECT epoch_prop(spoint(radians(269.45207695), radians(4.693364966)),
0,
RADIANS(-801.551/3.6e6), RADIANS(10362/3.6e6), -110,
-100) AS tp) AS q;
to_char | to_char | to_char | to_char | to_char | to_char
-----------------+-----------------+---------+----------+------------+---------
269.4744079540 | 4.4055337210 | | -801.210 | 10361.762 |
(1 row)

SELECT
to_char(DEGREES(tp[1]), '999D9999999999'),
to_char(DEGREES(tp[2]), '999D9999999999'),
to_char(tp[3], '999D999'),
to_char(DEGREES(tp[4])*3.6e6, '999D999'),
to_char(DEGREES(tp[5])*3.6e6, '99999D999'),
to_char(tp[6], '999D999')
FROM (
SELECT epoch_prop(spoint(radians(269.45207695), radians(4.693364966)),
NULL,
RADIANS(-801.551/3.6e6), RADIANS(10362/3.6e6), -110,
-100) AS tp) AS q;
to_char | to_char | to_char | to_char | to_char | to_char
-----------------+-----------------+---------+----------+------------+---------
269.4744079540 | 4.4055337210 | | -801.210 | 10361.762 |
(1 row)

SELECT
to_char(DEGREES(tp[1]), '999D9999999999'),
to_char(DEGREES(tp[2]), '999D9999999999'),
to_char(tp[3], '999D999'),
to_char(DEGREES(tp[4])*3.6e6, '999D999'),
to_char(DEGREES(tp[5])*3.6e6, '99999D999'),
to_char(tp[6], '999D999')
FROM (
SELECT epoch_prop(spoint(radians(269.45207695), radians(4.693364966)),
23,
RADIANS(-801.551/3.6e6), RADIANS(10362/3.6e6), NULL,
20) AS tp) AS q;
to_char | to_char | to_char | to_char | to_char | to_char
-----------------+-----------------+----------+----------+------------+----------
269.4476085384 | 4.7509315989 | 23.000 | -801.617 | 10361.984 | 2.159
(1 row)

SELECT
to_char(DEGREES(tp[1]), '999D9999999999'),
to_char(DEGREES(tp[2]), '999D9999999999'),
to_char(tp[3], '999D999'),
to_char(DEGREES(tp[4])*3.6e6, '999D999'),
to_char(DEGREES(tp[5])*3.6e6, '99999D999'),
to_char(tp[6], '999D999')
FROM (
SELECT epoch_prop(spoint(radians(269.45207695), radians(4.693364966)),
23,
NULL, RADIANS(10362/3.6e6), -110,
120) AS tp) AS q;
to_char | to_char | to_char | to_char | to_char | to_char
-----------------+-----------------+----------+----------+------------+----------
269.4520769500 | 5.0388680565 | 23.007 | -.000 | 10368.061 | -97.120
(1 row)

SELECT epoch_prop(NULL,
23,
0.01 , RADIANS(10362/3.6e6), -110,
120);
ERROR: NULL position not supported in epoch propagation
SELECT epoch_prop_pos(spoint(radians(269.45207695), radians(4.693364966)),
23,
RADIANS(-801.551/3.6e6), RADIANS(10362/3.6e6), -110,
20) AS tp;
tp
-------------------------------------------
(4.702747926583129 , 0.08291945093459933)
(1 row)

SELECT epoch_prop_pos(spoint(radians(269.45207695), radians(4.693364966)),
RADIANS(-801.551/3.6e6), RADIANS(10362/3.6e6),
20) AS tp;
tp
-------------------------------------------
(4.702747930619516 , 0.08291939893808763)
(1 row)

Loading

0 comments on commit 8394ae7

Please sign in to comment.