Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c6cf6d3

Browse files
committed
Fix minmax-multi distance for extreme interval values
When calculating distance for interval values, the code mostly mimicked interval_mi, i.e. it built a new interval value for the difference. That however does not work for sufficiently distant interval values, when the difference overflows the interval range. Instead, we can calculate the distance directly, without constructing the intermediate (and unnecessary) interval value. Backpatch to 14, where minmax-multi indexes were introduced. Reported-by: Dean Rasheed Reviewed-by: Ashutosh Bapat, Dean Rasheed Backpatch-through: 14 Discussion: https://postgr.es/m/eef0ea8c-4aaa-8d0d-027f-58b1f35dd170@enterprisedb.com
1 parent 8da86d6 commit c6cf6d3

File tree

3 files changed

+54
-29
lines changed

3 files changed

+54
-29
lines changed

src/backend/access/brin/brin_minmax_multi.c

+4-29
Original file line numberDiff line numberDiff line change
@@ -2160,45 +2160,20 @@ brin_minmax_multi_distance_interval(PG_FUNCTION_ARGS)
21602160

21612161
Interval *ia = PG_GETARG_INTERVAL_P(0);
21622162
Interval *ib = PG_GETARG_INTERVAL_P(1);
2163-
Interval *result;
21642163

21652164
int64 dayfraction;
21662165
int64 days;
21672166

2168-
result = (Interval *) palloc(sizeof(Interval));
2169-
2170-
result->month = ib->month - ia->month;
2171-
/* overflow check copied from int4mi */
2172-
if (!SAMESIGN(ib->month, ia->month) &&
2173-
!SAMESIGN(result->month, ib->month))
2174-
ereport(ERROR,
2175-
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2176-
errmsg("interval out of range")));
2177-
2178-
result->day = ib->day - ia->day;
2179-
if (!SAMESIGN(ib->day, ia->day) &&
2180-
!SAMESIGN(result->day, ib->day))
2181-
ereport(ERROR,
2182-
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2183-
errmsg("interval out of range")));
2184-
2185-
result->time = ib->time - ia->time;
2186-
if (!SAMESIGN(ib->time, ia->time) &&
2187-
!SAMESIGN(result->time, ib->time))
2188-
ereport(ERROR,
2189-
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2190-
errmsg("interval out of range")));
2191-
21922167
/*
21932168
* Delta is (fractional) number of days between the intervals. Assume
21942169
* months have 30 days for consistency with interval_cmp_internal. We
21952170
* don't need to be exact, in the worst case we'll build a bit less
21962171
* efficient ranges. But we should not contradict interval_cmp.
21972172
*/
2198-
dayfraction = result->time % USECS_PER_DAY;
2199-
days = result->time / USECS_PER_DAY;
2200-
days += result->month * INT64CONST(30);
2201-
days += result->day;
2173+
dayfraction = (ib->time % USECS_PER_DAY) - (ia->time % USECS_PER_DAY);
2174+
days = (ib->time / USECS_PER_DAY) - (ia->time / USECS_PER_DAY);
2175+
days += (int64) ib->day - (int64) ia->day;
2176+
days += ((int64) ib->month - (int64) ia->month) * INT64CONST(30);
22022177

22032178
/* convert to double precision */
22042179
delta = (double) days + dayfraction / (double) USECS_PER_DAY;

src/test/regress/expected/brin_multi.out

+29
Original file line numberDiff line numberDiff line change
@@ -915,3 +915,32 @@ SELECT * FROM brin_date_test WHERE a = '1900-01-01'::date;
915915
DROP TABLE brin_date_test;
916916
RESET enable_seqscan;
917917
RESET datestyle;
918+
-- test handling of overflow for interval values
919+
CREATE TABLE brin_interval_test(a INTERVAL);
920+
INSERT INTO brin_interval_test SELECT (i || ' years')::interval FROM generate_series(-178000000, -177999980) s(i);
921+
INSERT INTO brin_interval_test SELECT (i || ' years')::interval FROM generate_series( 177999980, 178000000) s(i);
922+
CREATE INDEX ON brin_interval_test USING brin (a interval_minmax_multi_ops) WITH (pages_per_range=1);
923+
SET enable_seqscan = off;
924+
EXPLAIN (ANALYZE, TIMING OFF, COSTS OFF, SUMMARY OFF)
925+
SELECT * FROM brin_interval_test WHERE a = '-30 years'::interval;
926+
QUERY PLAN
927+
-----------------------------------------------------------------------------
928+
Bitmap Heap Scan on brin_interval_test (actual rows=0 loops=1)
929+
Recheck Cond: (a = '@ 30 years ago'::interval)
930+
-> Bitmap Index Scan on brin_interval_test_a_idx (actual rows=0 loops=1)
931+
Index Cond: (a = '@ 30 years ago'::interval)
932+
(4 rows)
933+
934+
EXPLAIN (ANALYZE, TIMING OFF, COSTS OFF, SUMMARY OFF)
935+
SELECT * FROM brin_interval_test WHERE a = '30 years'::interval;
936+
QUERY PLAN
937+
-----------------------------------------------------------------------------
938+
Bitmap Heap Scan on brin_interval_test (actual rows=0 loops=1)
939+
Recheck Cond: (a = '@ 30 years'::interval)
940+
-> Bitmap Index Scan on brin_interval_test_a_idx (actual rows=0 loops=1)
941+
Index Cond: (a = '@ 30 years'::interval)
942+
(4 rows)
943+
944+
DROP TABLE brin_interval_test;
945+
RESET enable_seqscan;
946+
RESET datestyle;

src/test/regress/sql/brin_multi.sql

+21
Original file line numberDiff line numberDiff line change
@@ -664,3 +664,24 @@ SELECT * FROM brin_date_test WHERE a = '1900-01-01'::date;
664664
DROP TABLE brin_date_test;
665665
RESET enable_seqscan;
666666
RESET datestyle;
667+
668+
-- test handling of overflow for interval values
669+
CREATE TABLE brin_interval_test(a INTERVAL);
670+
671+
INSERT INTO brin_interval_test SELECT (i || ' years')::interval FROM generate_series(-178000000, -177999980) s(i);
672+
673+
INSERT INTO brin_interval_test SELECT (i || ' years')::interval FROM generate_series( 177999980, 178000000) s(i);
674+
675+
CREATE INDEX ON brin_interval_test USING brin (a interval_minmax_multi_ops) WITH (pages_per_range=1);
676+
677+
SET enable_seqscan = off;
678+
679+
EXPLAIN (ANALYZE, TIMING OFF, COSTS OFF, SUMMARY OFF)
680+
SELECT * FROM brin_interval_test WHERE a = '-30 years'::interval;
681+
682+
EXPLAIN (ANALYZE, TIMING OFF, COSTS OFF, SUMMARY OFF)
683+
SELECT * FROM brin_interval_test WHERE a = '30 years'::interval;
684+
685+
DROP TABLE brin_interval_test;
686+
RESET enable_seqscan;
687+
RESET datestyle;

0 commit comments

Comments
 (0)