-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_helper_functions.c
290 lines (235 loc) · 7.74 KB
/
test_helper_functions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*-------------------------------------------------------------------------
*
* test_helper_functions.c
*
* This file contains functions to exercise other functions within pg_shard
* modules for purposes of unit testing.
*
* Copyright (c) 2014, Citus Data, Inc.
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "c.h"
#include "fmgr.h"
#include "libpq-fe.h"
#include "postgres_ext.h"
#include "connection.h"
#include "distribution_metadata.h"
#include "test_helper_functions.h"
#include <stddef.h>
#include <string.h>
#include "catalog/pg_type.h"
#include "lib/stringinfo.h"
#include "nodes/pg_list.h"
#include "nodes/primnodes.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/palloc.h"
/* local function forward declarations */
static Datum ExtractIntegerDatum(char *input);
static ArrayType * DatumArrayToArrayType(Datum *datumArray, int datumCount,
Oid datumTypeId);
/* declarations for dynamic loading */
PG_FUNCTION_INFO_V1(initialize_remote_temp_table);
PG_FUNCTION_INFO_V1(count_remote_temp_table_rows);
PG_FUNCTION_INFO_V1(get_and_purge_connection);
PG_FUNCTION_INFO_V1(load_shard_id_array);
PG_FUNCTION_INFO_V1(load_shard_interval_array);
PG_FUNCTION_INFO_V1(load_shard_placement_array);
PG_FUNCTION_INFO_V1(partition_column_id);
/*
* initialize_remote_temp_table connects to a specified host on a specified
* port and creates a temporary table with 100 rows. Because the table is
* temporary, it will be visible if a connection is reused but not if a new
* connection is opened to the node.
*/
Datum
initialize_remote_temp_table(PG_FUNCTION_ARGS)
{
char *nodeName = PG_GETARG_CSTRING(0);
int32 nodePort = PG_GETARG_INT32(1);
PGresult *result = NULL;
PGconn *connection = GetConnection(nodeName, nodePort);
if (connection == NULL)
{
PG_RETURN_BOOL(false);
}
result = PQexec(connection, POPULATE_TEMP_TABLE);
if (PQresultStatus(result) != PGRES_COMMAND_OK)
{
ReportRemoteError(connection, result);
}
PQclear(result);
PG_RETURN_BOOL(true);
}
/*
* count_remote_temp_table_rows just returns the integer count of rows in the
* table created by initialize_remote_temp_table. If no such table exists, this
* function emits a warning and returns -1.
*/
Datum
count_remote_temp_table_rows(PG_FUNCTION_ARGS)
{
char *nodeName = PG_GETARG_CSTRING(0);
int32 nodePort = PG_GETARG_INT32(1);
Datum count = Int32GetDatum(-1);
PGresult *result = NULL;
PGconn *connection = GetConnection(nodeName, nodePort);
if (connection == NULL)
{
PG_RETURN_DATUM(count);
}
result = PQexec(connection, COUNT_TEMP_TABLE);
if (PQresultStatus(result) != PGRES_TUPLES_OK)
{
ReportRemoteError(connection, result);
}
else
{
char *countText = PQgetvalue(result, 0, 0);
count = ExtractIntegerDatum(countText);
}
PQclear(result);
PG_RETURN_DATUM(count);
}
/*
* get_and_purge_connection first gets a connection using the provided hostname
* and port before immediately passing that connection to PurgeConnection.
* Simply a wrapper around PurgeConnection that uses hostname/port rather than
* PGconn.
*/
Datum
get_and_purge_connection(PG_FUNCTION_ARGS)
{
char *nodeName = PG_GETARG_CSTRING(0);
int32 nodePort = PG_GETARG_INT32(1);
PGconn *connection = GetConnection(nodeName, nodePort);
if (connection == NULL)
{
PG_RETURN_BOOL(false);
}
PurgeConnection(connection);
PG_RETURN_BOOL(true);
}
/*
* ExtractIntegerDatum transforms an integer in textual form into a Datum.
*/
static Datum
ExtractIntegerDatum(char *input)
{
Oid typIoFunc = InvalidOid;
Oid typIoParam = InvalidOid;
Datum intDatum = 0;
FmgrInfo fmgrInfo;
memset(&fmgrInfo, 0, sizeof(fmgrInfo));
getTypeInputInfo(INT4OID, &typIoFunc, &typIoParam);
fmgr_info(typIoFunc, &fmgrInfo);
intDatum = InputFunctionCall(&fmgrInfo, input, typIoFunc, -1);
return intDatum;
}
/*
* load_shard_id_array returns the shard identifiers for a particular
* distributed table as a bigint array.
*/
Datum
load_shard_id_array(PG_FUNCTION_ARGS)
{
Oid distributedTableId = PG_GETARG_OID(0);
ArrayType *shardIdArrayType = NULL;
ListCell *shardCell = NULL;
int shardIdIndex = 0;
Oid shardIdTypeId = INT8OID;
List *shardList = LoadShardIntervalList(distributedTableId);
int shardIdCount = list_length(shardList);
Datum *shardIdDatumArray = palloc0(shardIdCount * sizeof(Datum));
foreach(shardCell, shardList)
{
ShardInterval *shardId = (ShardInterval *) lfirst(shardCell);
Datum shardIdDatum = Int64GetDatum(shardId->id);
shardIdDatumArray[shardIdIndex] = shardIdDatum;
shardIdIndex++;
}
shardIdArrayType = DatumArrayToArrayType(shardIdDatumArray, shardIdCount,
shardIdTypeId);
PG_RETURN_ARRAYTYPE_P(shardIdArrayType);
}
/*
* load_shard_interval_array loads a shard interval using a provided identifier
* and returns a two-element array consisting of min/max values contained in
* that shard interval (currently always integer values). If no such interval
* can be found, this function raises an error instead.
*/
Datum
load_shard_interval_array(PG_FUNCTION_ARGS)
{
int64 shardId = PG_GETARG_INT64(0);
ShardInterval *shardInterval = LoadShardInterval(shardId);
Datum shardIntervalArray[] = { shardInterval->minValue, shardInterval->maxValue };
ArrayType *shardIntervalArrayType = NULL;
/* for now we expect value type to always be integer (hash output) */
Assert(shardInterval->valueTypeId == INT4OID);
shardIntervalArrayType = DatumArrayToArrayType(shardIntervalArray, 2,
shardInterval->valueTypeId);
PG_RETURN_ARRAYTYPE_P(shardIntervalArrayType);
}
/*
* load_shard_placement_array loads a shard interval using the provided ID
* and returns an array of strings containing the node name and port for each
* placement of the specified shard interval. If no such shard interval can be
* found, this function raises an error instead.
*/
Datum
load_shard_placement_array(PG_FUNCTION_ARGS)
{
int64 shardId = PG_GETARG_INT64(0);
ArrayType *placementArrayType = NULL;
List *placementList = LoadShardPlacementList(shardId);
ListCell *placementCell = NULL;
int placementCount = list_length(placementList);
int placementIndex = 0;
Datum *placementDatumArray = palloc0(placementCount * sizeof(Datum));
Oid placementTypeId = TEXTOID;
StringInfo placementInfo = makeStringInfo();
foreach(placementCell, placementList)
{
ShardPlacement *placement = (ShardPlacement *) lfirst(placementCell);
appendStringInfo(placementInfo, "%s:%d", placement->nodeName,
placement->nodePort);
placementDatumArray[placementIndex] = CStringGetTextDatum(placementInfo->data);
placementIndex++;
resetStringInfo(placementInfo);
}
placementArrayType = DatumArrayToArrayType(placementDatumArray, placementCount,
placementTypeId);
PG_RETURN_ARRAYTYPE_P(placementArrayType);
}
/*
* partition_column_id simply finds a distributed table using the provided Oid
* and returns the column_id of its partition column. If the specified table is
* not distributed, this function raises an error instead.
*/
Datum
partition_column_id(PG_FUNCTION_ARGS)
{
Oid distributedTableId = PG_GETARG_OID(0);
Var *partitionColumn = PartitionColumn(distributedTableId);
PG_RETURN_INT16((int16) partitionColumn->varattno);
}
/*
* DatumArrayToArrayType converts the provided Datum array (of the specified
* length and type) into an ArrayType suitable for returning from a UDF.
*/
static ArrayType *
DatumArrayToArrayType(Datum *datumArray, int datumCount, Oid datumTypeId)
{
ArrayType *arrayObject = NULL;
int16 typeLength = 0;
bool typeByValue = false;
char typeAlignment = 0;
get_typlenbyvalalign(datumTypeId, &typeLength, &typeByValue, &typeAlignment);
arrayObject = construct_array(datumArray, datumCount, datumTypeId,
typeLength, typeByValue, typeAlignment);
return arrayObject;
}