Skip to content

Commit dc0a854

Browse files
committed
change signature of function build_hash_condition(), more tests
1 parent 6b88f51 commit dc0a854

File tree

5 files changed

+63
-17
lines changed

5 files changed

+63
-17
lines changed

expected/pathman_calamity.out

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,44 @@ SELECT debug_capture();
1212
set client_min_messages = NOTICE;
1313
/* create table to be partitioned */
1414
CREATE TABLE calamity.part_test(val serial);
15+
/* check function build_hash_condition() */
16+
SELECT build_hash_condition('int4', 'val', 10, 1);
17+
build_hash_condition
18+
-------------------------------------------------
19+
public.get_hash_part_idx(hashint4(val), 10) = 1
20+
(1 row)
21+
22+
SELECT build_hash_condition('text', 'val', 10, 1);
23+
build_hash_condition
24+
-------------------------------------------------
25+
public.get_hash_part_idx(hashtext(val), 10) = 1
26+
(1 row)
27+
28+
SELECT build_hash_condition('int4', 'val', 1, 1);
29+
ERROR: 'partition_index' must be lower than 'partitions_count'
30+
SELECT build_hash_condition('int4', 'val', 10, 20);
31+
ERROR: 'partition_index' must be lower than 'partitions_count'
32+
SELECT build_hash_condition('text', 'val', 10, NULL) IS NULL;
33+
?column?
34+
----------
35+
t
36+
(1 row)
37+
38+
SELECT build_hash_condition('calamity.part_test', 'val', 10, 1);
39+
ERROR: no hash function for type calamity.part_test
40+
/* check function build_range_condition() */
41+
SELECT build_range_condition('val', 10, 20);
42+
build_range_condition
43+
----------------------------
44+
val >= '10' AND val < '20'
45+
(1 row)
46+
47+
SELECT build_range_condition('val', 10, NULL) IS NULL;
48+
?column?
49+
----------
50+
t
51+
(1 row)
52+
1553
/* check function validate_relname() */
1654
SELECT validate_relname('calamity.part_test');
1755
validate_relname

hash.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ LANGUAGE C STRICT;
289289
* Build hash condition for a CHECK CONSTRAINT
290290
*/
291291
CREATE OR REPLACE FUNCTION @extschema@.build_hash_condition(
292-
parent_relid REGCLASS,
292+
attribute_type REGTYPE,
293293
attribute TEXT,
294-
part_count INT4,
295-
part_idx INT4)
294+
partitions_count INT4,
295+
partitions_index INT4)
296296
RETURNS TEXT AS 'pg_pathman', 'build_hash_condition'
297297
LANGUAGE C STRICT;

range.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,11 +1228,11 @@ SET client_min_messages = WARNING;
12281228
* Construct CHECK constraint condition for a range partition.
12291229
*/
12301230
CREATE OR REPLACE FUNCTION @extschema@.build_range_condition(
1231-
p_attname TEXT,
1231+
attribute TEXT,
12321232
start_value ANYELEMENT,
12331233
end_value ANYELEMENT)
12341234
RETURNS TEXT AS 'pg_pathman', 'build_range_condition'
1235-
LANGUAGE C;
1235+
LANGUAGE C STRICT;
12361236

12371237
CREATE OR REPLACE FUNCTION @extschema@.build_sequence_name(
12381238
parent_relid REGCLASS)

sql/pathman_calamity.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ set client_min_messages = NOTICE;
1414
CREATE TABLE calamity.part_test(val serial);
1515

1616

17+
/* check function build_hash_condition() */
18+
SELECT build_hash_condition('int4', 'val', 10, 1);
19+
SELECT build_hash_condition('text', 'val', 10, 1);
20+
SELECT build_hash_condition('int4', 'val', 1, 1);
21+
SELECT build_hash_condition('int4', 'val', 10, 20);
22+
SELECT build_hash_condition('text', 'val', 10, NULL) IS NULL;
23+
SELECT build_hash_condition('calamity.part_test', 'val', 10, 1);
24+
25+
/* check function build_range_condition() */
26+
SELECT build_range_condition('val', 10, 20);
27+
SELECT build_range_condition('val', 10, NULL) IS NULL;
28+
1729
/* check function validate_relname() */
1830
SELECT validate_relname('calamity.part_test');
1931
SELECT validate_relname(1::REGCLASS);

src/pl_hash_funcs.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,28 +91,24 @@ get_hash_part_idx(PG_FUNCTION_ARGS)
9191
Datum
9292
build_hash_condition(PG_FUNCTION_ARGS)
9393
{
94-
Oid parent = PG_GETARG_OID(0);
94+
Oid atttype = PG_GETARG_OID(0);
9595
text *attname = PG_GETARG_TEXT_P(1);
96-
uint32 part_count = PG_GETARG_UINT32(2);
97-
uint32 part_idx = PG_GETARG_UINT32(3);
96+
uint32 part_count = PG_GETARG_UINT32(2),
97+
part_idx = PG_GETARG_UINT32(3);
9898

9999
TypeCacheEntry *tce;
100-
Oid attype;
101100
char *attname_cstring = text_to_cstring(attname);
102101

103102
char *result;
104103

105104
if (part_idx >= part_count)
106-
elog(ERROR, "'part_idx' must be lower than 'part_count'");
105+
elog(ERROR, "'partition_index' must be lower than 'partitions_count'");
107106

108-
/* Get attribute type and its hash function oid */
109-
attype = get_attribute_type(parent, attname_cstring, false);
110-
if (attype == InvalidOid)
111-
elog(ERROR, "relation \"%s\" has no attribute \"%s\"",
112-
get_rel_name(parent),
113-
attname_cstring);
107+
tce = lookup_type_cache(atttype, TYPECACHE_HASH_PROC);
114108

115-
tce = lookup_type_cache(attype, TYPECACHE_HASH_PROC);
109+
/* Check that HASH function exists */
110+
if (!OidIsValid(tce->hash_proc))
111+
elog(ERROR, "no hash function for type %s", format_type_be(atttype));
116112

117113
/* Create hash condition CSTRING */
118114
result = psprintf("%s.get_hash_part_idx(%s(%s), %u) = %u",

0 commit comments

Comments
 (0)