-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathBackend.hpp
More file actions
196 lines (153 loc) · 5.49 KB
/
Backend.hpp
File metadata and controls
196 lines (153 loc) · 5.49 KB
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
/* ----------------------------------------------------------------------- *//**
*
* @file Backend.hpp
*
*//* ----------------------------------------------------------------------- */
#ifndef MADLIB_POSTGRES_BACKEND_HPP
#define MADLIB_POSTGRES_BACKEND_HPP
namespace madlib {
namespace dbconnector {
namespace postgres {
namespace {
// No need to make these function accessible outside of the postgres namespace.
MADLIB_WRAP_PG_FUNC(
bool, type_is_array, (Oid typid), (typid))
// PostgreSQL 16+ replaced pg_proc_aclcheck with object_aclcheck
#if PG_VERSION_NUM >= 160000
MADLIB_WRAP_PG_FUNC(
AclResult, object_aclcheck, (Oid classid, Oid objectid, Oid roleid, AclMode mode),
(classid, objectid, roleid, mode))
inline AclResult madlib_pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode) {
return madlib_object_aclcheck(ProcedureRelationId, proc_oid, roleid, mode);
}
#else
MADLIB_WRAP_PG_FUNC(
AclResult, pg_proc_aclcheck, (Oid proc_oid, Oid roleid, AclMode mode),
(proc_oid, roleid, mode))
#endif
MADLIB_WRAP_PG_FUNC(
void*, MemoryContextAlloc, (MemoryContext context, Size size),
(context, size))
MADLIB_WRAP_PG_FUNC(
void*, MemoryContextAllocZero, (MemoryContext context, Size size),
(context, size))
MADLIB_WRAP_PG_FUNC(
char*, format_procedure, (Oid procedure_oid), (procedure_oid))
MADLIB_WRAP_PG_FUNC(
Oid, get_fn_expr_argtype, (FmgrInfo* flinfo, int argnum), (flinfo, argnum))
MADLIB_WRAP_PG_FUNC(
TypeFuncClass, get_call_result_type, (FunctionCallInfo fcinfo,
Oid* resultTypeId, TupleDesc* resultTupleDesc),
(fcinfo, resultTypeId, resultTupleDesc))
MADLIB_WRAP_PG_FUNC(
HeapTupleHeader, DatumGetHeapTupleHeader, (Datum d), (d))
MADLIB_WRAP_PG_FUNC(
bytea*, DatumGetByteaPCopy, (Datum d), (d))
MADLIB_WRAP_PG_FUNC(
ArrayType*, DatumGetArrayTypePCopy, (Datum d), (d))
MADLIB_WRAP_PG_FUNC(
Datum, GetAttributeByNum, (HeapTupleHeader tuple, AttrNumber attrno,
bool* isNull),
(tuple, attrno, isNull))
MADLIB_WRAP_VOID_PG_FUNC(
fmgr_info_cxt, (Oid functionId, FmgrInfo* finfo, MemoryContext mcxt),
(functionId, finfo, mcxt))
MADLIB_WRAP_PG_FUNC(
HeapTuple, heap_form_tuple, (TupleDesc tupleDescriptor, Datum* values,
bool* isnull),
(tupleDescriptor, values, isnull))
MADLIB_WRAP_PG_FUNC(
HTAB*, hash_create, (const char* tabname, long nelem, HASHCTL* info,
int flags),
(tabname, nelem, info, flags))
MADLIB_WRAP_PG_FUNC(
void*, hash_search, (HTAB* hashp, const void* keyPtr, HASHACTION action,
bool* foundPtr),
(hashp, keyPtr, action, foundPtr))
// Calls to SearchSysCache and related functions have been wrapped using macros
// with commit e26c539e by Robert Haas <rhaas@postgresql.org>
// on Sun, 14 Feb 2010 18:42:19 UTC. First release: PG9.0.
MADLIB_WRAP_PG_FUNC(
HeapTuple, SearchSysCache1, (int cacheId, Datum key1), (cacheId, key1))
MADLIB_WRAP_PG_FUNC(
TupleDesc, lookup_rowtype_tupdesc_copy, (Oid type_id, int32 typmod),
(type_id, typmod))
MADLIB_WRAP_VOID_PG_FUNC(
ReleaseSysCache, (HeapTuple tuple), (tuple))
MADLIB_WRAP_PG_FUNC(
Datum, SysCacheGetAttr, (int cacheId, HeapTuple tup,
AttrNumber attributeNumber, bool* isNull),
(cacheId, tup, attributeNumber, isNull))
MADLIB_WRAP_PG_FUNC(
struct varlena*, pg_detoast_datum, (struct varlena* datum), (datum))
MADLIB_WRAP_VOID_PG_FUNC(
get_typlenbyvalalign,
(Oid typid, int16 *typlen, bool *typbyval, char *typalign),
(typid, typlen, typbyval, typalign)
)
inline
void
#if PG_VERSION_NUM >= 120000
madlib_InitFunctionCallInfoData(FunctionCallInfoBaseData& fcinfo,
#else
madlib_InitFunctionCallInfoData(FunctionCallInfoData& fcinfo,
#endif
FmgrInfo* flinfo, short nargs, Oid fncollation, fmNodePtr context,
fmNodePtr resultinfo) {
#if PG_VERSION_NUM >= 90100
// Collation support has been added to PostgreSQL with commit
// d64713df by Tom Lane <tgl@sss.pgh.pa.us>
// on Tue Apr 12 2011 23:19:24 UTC. First release: PG9.1.
InitFunctionCallInfoData(fcinfo, flinfo, nargs, fncollation, context,
resultinfo);
#else
(void) fncollation;
InitFunctionCallInfoData(fcinfo, flinfo, nargs, context, resultinfo);
#endif
}
template <typename T>
inline
T*
madlib_detoast_verlena_datum_if_necessary(Datum inDatum) {
varlena* ptr = reinterpret_cast<varlena*>(DatumGetPointer(inDatum));
if (!VARATT_IS_EXTENDED(ptr))
return reinterpret_cast<T*>(ptr);
else
return reinterpret_cast<T*>(madlib_pg_detoast_datum(ptr));
}
/**
* @brief Convert a Datum into a bytea
*
* For performance reasons, we look into the varlena struct in order to check
* if we can avoid a PG_TRY block.
*/
inline
bytea*
madlib_DatumGetByteaP(Datum inDatum) {
return madlib_detoast_verlena_datum_if_necessary<bytea>(inDatum);
}
/**
* @brief Convert a Datum into an ArrayType
*
* For performance reasons, we look into the varlena struct in order to check
* if we can avoid a PG_TRY block.
*/
inline
ArrayType*
madlib_DatumGetArrayTypeP(Datum inDatum) {
ArrayType* x = madlib_detoast_verlena_datum_if_necessary<ArrayType>(inDatum);
if (ARR_HASNULL(x)) {
// an empty array has dimensionality 0
size_t array_size = ARR_NDIM(x) ? 1 : 0;
for (int i = 0; i < ARR_NDIM(x); i ++) {
array_size *= ARR_DIMS(x)[i];
}
throw ArrayWithNullException(array_size);
}
return x;
}
} // namespace
} // namespace postgres
} // namespace dbconnector
} // namespace madlib
#endif // defined(MADLIB_POSTGRES_BACKEND_ABSTRACTION_HPP)