Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7c70a12

Browse files
committed
Fix typcache's failure to treat ranges as container types.
Like the similar logic for arrays and records, it's necessary to examinethe range's subtype to decide whether the range type can support hashing.We can omit checking the subtype for btree-defined operations, though,since range subtypes are required to have those operations. (Possiblythat simplification for btree cases led us to overlook that it doesnot apply for hash cases.)This is only an issue if the subtype lacks hash support, which is nottrue of any built-in range type, but it's easy to demonstrate a problemwith a range type over, eg, money: you can get a "could not identifya hash function" failure when the planner is misled into thinking thathash join or aggregation would work.This was born broken, so back-patch to all supported branches.
1 parent06b2a73 commit7c70a12

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

‎src/backend/utils/cache/typcache.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ static void cache_array_element_properties(TypeCacheEntry *typentry);
131131
staticboolrecord_fields_have_equality(TypeCacheEntry*typentry);
132132
staticboolrecord_fields_have_compare(TypeCacheEntry*typentry);
133133
staticvoidcache_record_field_properties(TypeCacheEntry*typentry);
134+
staticboolrange_element_has_hashing(TypeCacheEntry*typentry);
135+
staticvoidcache_range_element_properties(TypeCacheEntry*typentry);
134136
staticvoidTypeCacheRelCallback(Datumarg,Oidrelid);
135137
staticvoidload_enum_cache_data(TypeCacheEntry*tcache);
136138
staticEnumItem*find_enumitem(TypeCacheEnumData*enumdata,Oidarg);
@@ -407,6 +409,13 @@ lookup_type_cache(Oid type_id, int flags)
407409
!array_element_has_hashing(typentry))
408410
hash_proc=InvalidOid;
409411

412+
/*
413+
* Likewise for hash_range.
414+
*/
415+
if (hash_proc==F_HASH_RANGE&&
416+
!range_element_has_hashing(typentry))
417+
hash_proc=InvalidOid;
418+
410419
typentry->hash_proc=hash_proc;
411420
}
412421

@@ -610,6 +619,10 @@ cache_array_element_properties(TypeCacheEntry *typentry)
610619
typentry->flags |=TCFLAGS_CHECKED_ELEM_PROPERTIES;
611620
}
612621

622+
/*
623+
* Likewise, some helper functions for composite types.
624+
*/
625+
613626
staticbool
614627
record_fields_have_equality(TypeCacheEntry*typentry)
615628
{
@@ -680,6 +693,43 @@ cache_record_field_properties(TypeCacheEntry *typentry)
680693
typentry->flags |=TCFLAGS_CHECKED_FIELD_PROPERTIES;
681694
}
682695

696+
/*
697+
* Likewise, some helper functions for range types.
698+
*
699+
* We can borrow the flag bits for array element properties to use for range
700+
* element properties, since those flag bits otherwise have no use in a
701+
* range type's typcache entry.
702+
*/
703+
704+
staticbool
705+
range_element_has_hashing(TypeCacheEntry*typentry)
706+
{
707+
if (!(typentry->flags&TCFLAGS_CHECKED_ELEM_PROPERTIES))
708+
cache_range_element_properties(typentry);
709+
return (typentry->flags&TCFLAGS_HAVE_ELEM_HASHING)!=0;
710+
}
711+
712+
staticvoid
713+
cache_range_element_properties(TypeCacheEntry*typentry)
714+
{
715+
/* load up subtype link if we didn't already */
716+
if (typentry->rngelemtype==NULL&&
717+
typentry->typtype==TYPTYPE_RANGE)
718+
load_rangetype_info(typentry);
719+
720+
if (typentry->rngelemtype!=NULL)
721+
{
722+
TypeCacheEntry*elementry;
723+
724+
/* might need to calculate subtype's hash function properties */
725+
elementry=lookup_type_cache(typentry->rngelemtype->type_id,
726+
TYPECACHE_HASH_PROC);
727+
if (OidIsValid(elementry->hash_proc))
728+
typentry->flags |=TCFLAGS_HAVE_ELEM_HASHING;
729+
}
730+
typentry->flags |=TCFLAGS_CHECKED_ELEM_PROPERTIES;
731+
}
732+
683733

684734
/*
685735
* lookup_rowtype_tupdesc_internal --- internal routine to lookup a rowtype

‎src/test/regress/expected/rangetypes.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,18 @@ select array[1,3] <@ arrayrange(array[1,2], array[2,1]);
12991299
t
13001300
(1 row)
13011301

1302+
--
1303+
-- Check behavior when subtype lacks a hash function
1304+
--
1305+
create type cashrange as range (subtype = money);
1306+
set enable_sort = off; -- try to make it pick a hash setop implementation
1307+
select '(2,5)'::cashrange except select '(5,6)'::cashrange;
1308+
cashrange
1309+
---------------
1310+
($2.00,$5.00)
1311+
(1 row)
1312+
1313+
reset enable_sort;
13021314
--
13031315
-- OUT/INOUT/TABLE functions
13041316
--

‎src/test/regress/sql/rangetypes.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,18 @@ select arrayrange(ARRAY[2,1], ARRAY[1,2]); -- fail
438438
select array[1,1]<@ arrayrange(array[1,2], array[2,1]);
439439
select array[1,3]<@ arrayrange(array[1,2], array[2,1]);
440440

441+
--
442+
-- Check behavior when subtype lacks a hash function
443+
--
444+
445+
createtypecashrangeas range (subtype=money);
446+
447+
set enable_sort= off;-- try to make it pick a hash setop implementation
448+
449+
select'(2,5)'::cashrange exceptselect'(5,6)'::cashrange;
450+
451+
reset enable_sort;
452+
441453
--
442454
-- OUT/INOUT/TABLE functions
443455
--

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp