Skip to content

Commit

Permalink
Fix output precision limit for double values (issue #118)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vitcpp committed Mar 14, 2024
1 parent 462fa03 commit b7e12f3
Show file tree
Hide file tree
Showing 3 changed files with 956 additions and 11 deletions.
240 changes: 240 additions & 0 deletions expected/bounding_box_gist_2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
SET enable_seqscan=true;
CREATE TABLE bbox_ellipse (e sellipse not null);
INSERT INTO bbox_ellipse VALUES ('<{10d, 0.1d}, (0d,0d), 0d>');
SELECT spoint '(5d, 0d)' @ sellipse '<{10d, 0.1d}, (0d,0d), 0d>' AS inside;
inside
--------
t
(1 row)

SELECT COUNT(*) FROM bbox_ellipse WHERE spoint '(5d, 0d)' @ e;
count
-------
1
(1 row)

SELECT COUNT(*) FROM bbox_ellipse WHERE spoint '(5d, 0d)' <@ e;
count
-------
1
(1 row)

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

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

-- The ellipse has semi-major axis length of 10 degrees along the equator,
-- so (lon,lat) = (5,0) should be inside.
CREATE INDEX idx_bbox_ellipse ON bbox_ellipse USING gist (e);
ANALYZE bbox_ellipse;
SET enable_seqscan=false;
SELECT COUNT(*) FROM bbox_ellipse WHERE spoint '(5d, 0d)' @ e;
count
-------
1
(1 row)

SELECT COUNT(*) FROM bbox_ellipse WHERE spoint '(5d, 0d)' <@ e;
count
-------
1
(1 row)

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

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

SET enable_seqscan=true;
CREATE TABLE bbox_poly (p spoly not null);
INSERT INTO bbox_poly VALUES ('{(40d,-40d), (0d,80d), (-40d,-40d)}');
SELECT spoint '(0d, 0d)' @ spoly '{(40d,-40d), (0d,80d), (-40d,-40d)}' AS inside;
inside
--------
t
(1 row)

SELECT COUNT(*) FROM bbox_poly WHERE spoint '(0d, 0d)' @ p;
count
-------
1
(1 row)

SELECT COUNT(*) FROM bbox_poly WHERE spoint '(0d, 0d)' <@ p;
count
-------
1
(1 row)

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

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

CREATE INDEX idx_bbox_poly ON bbox_poly USING gist (p);
ANALYZE bbox_poly;
SET enable_seqscan=false;
SELECT COUNT(*) FROM bbox_poly WHERE spoint '(0d, 0d)' @ p;
count
-------
1
(1 row)

SELECT COUNT(*) FROM bbox_poly WHERE spoint '(0d, 0d)' <@ p;
count
-------
1
(1 row)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_poly WHERE spoint '(0d, 0d)' @ p;
QUERY PLAN
---------------------------------------------------
Aggregate
-> Index Scan using idx_bbox_poly on bbox_poly
Index Cond: (p ~ '(0 , 0)'::spoint)
(3 rows)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_poly WHERE spoint '(0d, 0d)' <@ p;
QUERY PLAN
---------------------------------------------------
Aggregate
-> Index Scan using idx_bbox_poly on bbox_poly
Index Cond: (p @> '(0 , 0)'::spoint)
(3 rows)

SET enable_seqscan=true;
CREATE TABLE bbox_path (p spath not null);
INSERT INTO bbox_path VALUES ('{(-46d,0d), (-45d,80d), (-45d,0d), (80d,0d)}');
SELECT sline(spoint '(0d, -10d)', spoint '(0d, 10d)') && spath '{(-46d,0d), (-45d,80d), (-45d,0d), (80d,0d)}' AS crossing;
crossing
----------
t
(1 row)

SELECT spoint '(0d, 0d)' @ spath '{(-46d,0d), (-45d,80d), (-45d,0d), (80d,0d)}' AS inside;
inside
--------
t
(1 row)

SELECT COUNT(*) FROM bbox_path WHERE sline(spoint '(0d, -10d)', spoint '(0d, 10d)') && p;
count
-------
1
(1 row)

SELECT COUNT(*) FROM bbox_path WHERE spoint '(0d, 0d)' @ p;
count
-------
1
(1 row)

SELECT COUNT(*) FROM bbox_path WHERE spoint '(0d, 0d)' <@ p;
count
-------
1
(1 row)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_path WHERE sline(spoint '(0d, -10d)', spoint '(0d, 10d)') && p;
QUERY PLAN
---------------------------------------------------------------------------------------------------------
Aggregate
-> Seq Scan on bbox_path
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;
QUERY PLAN
-----------------------------------------
Aggregate
-> Seq Scan on bbox_path
Filter: ('(0 , 0)'::spoint @ p)
(3 rows)

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

CREATE INDEX idx_bbox_path ON bbox_path USING gist (p);
ANALYZE bbox_path;
SET enable_seqscan=false;
SELECT COUNT(*) FROM bbox_path WHERE sline(spoint '(0d, -10d)', spoint '(0d, 10d)') && p;
count
-------
1
(1 row)

SELECT COUNT(*) FROM bbox_path WHERE spoint '(0d, 0d)' @ p;
count
-------
1
(1 row)

SELECT COUNT(*) FROM bbox_path WHERE spoint '(0d, 0d)' <@ p;
count
-------
1
(1 row)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_path WHERE sline(spoint '(0d, -10d)', spoint '(0d, 10d)') && p;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------
Aggregate
-> Index Scan using idx_bbox_path on bbox_path
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;
QUERY PLAN
---------------------------------------------------
Aggregate
-> Index Scan using idx_bbox_path on bbox_path
Index Cond: (p ~ '(0 , 0)'::spoint)
(3 rows)

EXPLAIN (COSTS OFF) SELECT COUNT(*) FROM bbox_path WHERE spoint '(0d, 0d)' <@ p;
QUERY PLAN
---------------------------------------------------
Aggregate
-> Index Scan using idx_bbox_path on bbox_path
Index Cond: (p @> '(0 , 0)'::spoint)
(3 rows)

107 changes: 107 additions & 0 deletions expected/epochprop_1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
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 b7e12f3

Please sign in to comment.