Skip to content

Commit bd44be0

Browse files
committed
Save expression in database
1 parent 405c509 commit bd44be0

12 files changed

+259
-191
lines changed

hash.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
CREATE OR REPLACE FUNCTION @extschema@.create_hash_partitions(
1515
parent_relid REGCLASS,
16-
attribute TEXT,
16+
expression TEXT,
1717
partitions_count INTEGER,
1818
partition_data BOOLEAN DEFAULT TRUE,
1919
partition_names TEXT[] DEFAULT NULL,
@@ -31,15 +31,15 @@ BEGIN
3131
PERFORM @extschema@.lock_partitioned_relation(parent_relid);
3232
END IF;
3333

34-
attribute := lower(attribute);
35-
PERFORM @extschema@.common_relation_checks(parent_relid, attribute);
34+
expression := lower(expression);
35+
PERFORM @extschema@.common_relation_checks(parent_relid, expression);
3636

3737
/* Insert new entry to pathman config */
38-
PERFORM @extschema@.add_to_pathman_config(parent_relid, attribute);
38+
PERFORM @extschema@.add_to_pathman_config(parent_relid, expression, NULL, false);
3939

4040
/* Create partitions */
4141
PERFORM @extschema@.create_hash_partitions_internal(parent_relid,
42-
attribute,
42+
expression,
4343
partitions_count,
4444
partition_names,
4545
tablespaces);

init.sql

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,20 @@ LANGUAGE C;
3535
CREATE TABLE IF NOT EXISTS @extschema@.pathman_config (
3636
partrel REGCLASS NOT NULL PRIMARY KEY,
3737
attname TEXT NOT NULL,
38+
raw_expression TEXT NOT NULL,
3839
atttype OID NOT NULL,
3940
parttype INTEGER NOT NULL,
4041
range_interval TEXT,
4142

4243
/* check for allowed part types */
43-
CHECK (parttype IN (1, 2)),
44+
CHECK (parttype IN (1, 2))
4445

4546
/* check for correct interval */
47+
/*
4648
CHECK (@extschema@.validate_interval_value(partrel,
4749
attname,
4850
parttype,
49-
range_interval))
51+
range_interval)) */
5052
);
5153

5254

@@ -451,10 +453,6 @@ BEGIN
451453
RAISE EXCEPTION 'relation "%" has already been partitioned', relation;
452454
END IF;
453455

454-
IF NOT @extschema@.is_expression_suitable(relation, expression) THEN
455-
RAISE EXCEPTION 'partitioning expression "%" is not suitable', expression;
456-
END IF;
457-
458456
/* Check if there are foreign keys that reference the relation */
459457
FOR v_rec IN (SELECT * FROM pg_catalog.pg_constraint
460458
WHERE confrelid = relation::REGCLASS::OID)
@@ -800,11 +798,13 @@ LANGUAGE C STRICT;
800798
/*
801799
* Checks if expression is suitable
802800
*/
801+
/*
803802
CREATE OR REPLACE FUNCTION @extschema@.is_expression_suitable(
804803
relid REGCLASS,
805804
expr TEXT)
806805
RETURNS BOOLEAN AS 'pg_pathman', 'is_expression_suitable'
807806
LANGUAGE C STRICT;
807+
*/
808808

809809
/*
810810
* Check if regclass is date or timestamp.
@@ -848,9 +848,11 @@ LANGUAGE C STRICT;
848848
* Attach a previously partitioned table.
849849
*/
850850
CREATE OR REPLACE FUNCTION @extschema@.add_to_pathman_config(
851-
parent_relid REGCLASS,
852-
attname TEXT,
853-
range_interval TEXT DEFAULT NULL)
851+
parent_relid REGCLASS,
852+
attname TEXT,
853+
range_interval TEXT DEFAULT NULL,
854+
refresh_part_info BOOL DEFAULT TRUE
855+
)
854856
RETURNS BOOLEAN AS 'pg_pathman', 'add_to_pathman_config'
855857
LANGUAGE C;
856858

src/include/partition_creation.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,25 @@ Oid create_partitions_for_value_internal(Oid relid, Datum value, Oid value_type,
3232
Oid create_single_range_partition_internal(Oid parent_relid,
3333
const Bound *start_value,
3434
const Bound *end_value,
35-
Oid value_type,
3635
RangeVar *partition_rv,
3736
char *tablespace);
3837

3938
/* Create one HASH partition */
4039
Oid create_single_hash_partition_internal(Oid parent_relid,
4140
uint32 part_idx,
4241
uint32 part_count,
43-
Oid value_type,
4442
RangeVar *partition_rv,
4543
char *tablespace);
4644

4745

4846
/* RANGE constraints */
4947
Constraint * build_range_check_constraint(Oid child_relid,
50-
char *attname,
48+
Node *raw_expression,
5149
const Bound *start_value,
5250
const Bound *end_value,
53-
Oid value_type);
51+
Oid expr_type);
5452

55-
Node * build_raw_range_check_tree(char *attname,
53+
Node * build_raw_range_check_tree(Node *raw_expression,
5654
const Bound *start_value,
5755
const Bound *end_value,
5856
Oid value_type);
@@ -66,23 +64,29 @@ bool check_range_available(Oid parent_relid,
6664

6765
/* HASH constraints */
6866
Constraint * build_hash_check_constraint(Oid child_relid,
69-
const char *expr,
67+
Node *raw_expression,
7068
uint32 part_idx,
7169
uint32 part_count,
7270
Oid value_type);
7371

74-
Node * build_raw_hash_check_tree(const char *base_expr,
72+
Node * build_raw_hash_check_tree(Node *raw_expression,
7573
uint32 part_idx,
7674
uint32 part_count,
7775
Oid relid,
7876
Oid value_type);
7977

8078
void drop_check_constraint(Oid relid, AttrNumber attnum);
8179

82-
/* expression parsing functions */
83-
Node *get_expression_node(Oid relid, const char *expr, bool analyze);
84-
Oid get_partition_expr_type(Oid relid, const char *expr);
80+
typedef struct
81+
{
82+
Oid expr_type;
83+
Datum expr_datum;
84+
Node *raw_expr;
85+
} PartExpressionInfo;
8586

87+
/* expression parsing functions */
88+
PartExpressionInfo *get_part_expression_info(Oid relid,
89+
const char *expr_string, bool check_hash_func, bool make_plan);
8690

8791

8892
/* Partitioning callback type */

src/include/pathman.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@
4444
* Definitions for the "pathman_config" table.
4545
*/
4646
#define PATHMAN_CONFIG "pathman_config"
47-
#define Natts_pathman_config 5
47+
#define Natts_pathman_config 6
4848
#define Anum_pathman_config_partrel 1 /* partitioned relation (regclass) */
49-
#define Anum_pathman_config_attname 2 /* partitioned column (text) */
50-
#define Anum_pathman_config_atttype 3 /* partitioned atttype */
51-
#define Anum_pathman_config_parttype 4 /* partitioning type (1|2) */
52-
#define Anum_pathman_config_range_interval 5 /* interval for RANGE pt. (text) */
49+
#define Anum_pathman_config_expression 2 /* partitioned expression (text) */
50+
#define Anum_pathman_config_raw_expression 3 /* partitioned raw expression (text) */
51+
#define Anum_pathman_config_atttype 4 /* partitioned atttype */
52+
#define Anum_pathman_config_parttype 5 /* partitioning type (1|2) */
53+
#define Anum_pathman_config_range_interval 6 /* interval for RANGE pt. (text) */
5354

5455
/* type modifier (typmod) for 'range_interval' */
5556
#define PATHMAN_CONFIG_interval_typmod -1

src/include/relation_info.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ typedef struct
134134
Oid *children; /* Oids of child partitions */
135135
RangeEntry *ranges; /* per-partition range entry or NULL */
136136

137-
Expr *expr; /* planned expression */
137+
Node *expr; /* planned expression */
138138
PartType parttype; /* partitioning type (HASH | RANGE) */
139139
Oid atttype; /* expression type */
140140
int32 atttypmod; /* expression type modifier */

src/init.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,8 @@ pathman_config_contains_relation(Oid relid, Datum *values, bool *isnull,
711711

712712
/* Perform checks for non-NULL columns */
713713
Assert(!isnull[Anum_pathman_config_partrel - 1]);
714-
Assert(!isnull[Anum_pathman_config_attname - 1]);
714+
Assert(!isnull[Anum_pathman_config_expression - 1]);
715+
Assert(!isnull[Anum_pathman_config_raw_expression - 1]);
715716
Assert(!isnull[Anum_pathman_config_parttype - 1]);
716717
}
717718

@@ -825,7 +826,8 @@ read_pathman_config(void)
825826
/* These attributes are marked as NOT NULL, check anyway */
826827
Assert(!isnull[Anum_pathman_config_partrel - 1]);
827828
Assert(!isnull[Anum_pathman_config_parttype - 1]);
828-
Assert(!isnull[Anum_pathman_config_attname - 1]);
829+
Assert(!isnull[Anum_pathman_config_expression - 1]);
830+
Assert(!isnull[Anum_pathman_config_raw_expression - 1]);
829831

830832
/* Extract values from Datums */
831833
relid = DatumGetObjectId(values[Anum_pathman_config_partrel - 1]);
@@ -1112,7 +1114,6 @@ validate_hash_constraint(const Expr *expr,
11121114
const OpExpr *eq_expr;
11131115
const FuncExpr *get_hash_expr,
11141116
*type_hash_proc_expr;
1115-
const Var *var; /* partitioned column */
11161117

11171118
if (!expr)
11181119
return false;

0 commit comments

Comments
 (0)