forked from zhangh43/vectorize_engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
165 lines (135 loc) · 3.88 KB
/
utils.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
/*-------------------------------------------------------------------------
*
* utils.c
*
* Copyright (c) 1996-2019, PostgreSQL Global Development Group
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "catalog/namespace.h"
#include "executor/executor.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/hsearch.h"
#include "utils.h"
typedef struct VecTypeHashEntry
{
Oid src;
Oid dest;
}VecTypeHashEntry;
/* Map between the vectorized types and non-vectorized types */
static HTAB *hashMapN2V = NULL;
static HTAB *hashMapV2N = NULL;
#define BUILTIN_TYPE_NUM 12
#define TYPE_HASH_TABLE_SIZE 64
const char *typenames[] = { "any", "int2", "int4", "int8", "float4", "float8",
"bool", "text", "date", "bpchar", "timestamp", "varchar"};
const char *vtypenames[] = { "vany", "vint2", "vint4", "vint8", "vfloat4",
"vfloat8", "vbool", "vtext", "vdate", "vbpchar",
"vtimestamp","vvarchar"};
/*
* Clear common CustomScanState, since we would
* use custom scan to do agg, hash etc.
*/
void
ClearCustomScanState(CustomScanState *node)
{
/* Free the exprcontext */
ExecFreeExprContext(&node->ss.ps);
/* Clean out the tuple table */
ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
ExecClearTuple(node->ss.ss_ScanTupleSlot);
/* Close the heap relation */
if (node->ss.ss_currentRelation)
ExecCloseScanRelation(node->ss.ss_currentRelation);
}
/*
* map non-vectorized type to vectorized type.
* To scan the PG_TYPE is inefficient, so we create a hashtable to map
* the vectorized type and non-vectorized types.
*/
Oid GetVtype(Oid ntype)
{
VecTypeHashEntry *entry = NULL;
bool found = false;
/* construct the hash table */
if(NULL == hashMapN2V)
{
HASHCTL hash_ctl;
MemSet(&hash_ctl, 0, sizeof(hash_ctl));
hash_ctl.keysize = sizeof(Oid);
hash_ctl.entrysize = sizeof(VecTypeHashEntry);
hash_ctl.hash = oid_hash;
hashMapN2V = hash_create("vectorized_n2v",TYPE_HASH_TABLE_SIZE,
&hash_ctl, HASH_ELEM | HASH_FUNCTION);
}
/* insert supported built-in type and vtypes */
{
int i;
Oid vtypid;
Oid typid;
for (i = 0; i < BUILTIN_TYPE_NUM; i++)
{
vtypid = TypenameGetTypid(vtypenames[i]);
typid = TypenameGetTypid(typenames[i]);
if (vtypid == InvalidOid)
return InvalidOid;
/* insert int4->vint4 mapping manually, may construct from catalog in future */
entry = hash_search(hashMapN2V, &typid, HASH_ENTER, &found);
entry->dest = vtypid;
}
}
/* find the vectorized type in hash table */
entry = hash_search(hashMapN2V, &ntype, HASH_FIND, &found);
if(found)
return entry->dest;
return InvalidOid;
}
/*
* map vectorized type to non-vectorized type.
* To scan the PG_TYPE is inefficient, so we create a hashtable to map
* the vectorized type and non-vectorized types.
*/
Oid GetNtype(Oid vtype)
{
VecTypeHashEntry *entry = NULL;
bool found = false;
/* construct the hash table */
if(NULL == hashMapV2N)
{
HASHCTL hash_ctl;
MemSet(&hash_ctl, 0, sizeof(hash_ctl));
hash_ctl.keysize = sizeof(Oid);
hash_ctl.entrysize = sizeof(VecTypeHashEntry);
hash_ctl.hash = oid_hash;
hashMapV2N = hash_create("vectorized_v2n", TYPE_HASH_TABLE_SIZE,
&hash_ctl, HASH_ELEM | HASH_FUNCTION);
}
/* insert supported built-in type and vtypes */
{
int i;
Oid vtypid;
Oid typid;
for (i = 0; i < BUILTIN_TYPE_NUM; i++)
{
vtypid = TypenameGetTypid(vtypenames[i]);
typid = TypenameGetTypid(typenames[i]);
if (vtypid == InvalidOid)
return InvalidOid;
entry = hash_search(hashMapV2N, &vtypid, HASH_ENTER, &found);
entry->dest = typid;
}
}
/* find the vectorized type in hash table */
entry = hash_search(hashMapV2N, &vtype, HASH_FIND, &found);
if(found)
return entry->dest;
return InvalidOid;
}
Oid
GetTupDescAttVType(TupleDesc tupdesc, int i)
{
Form_pg_attribute att = tupdesc->attrs[i];
return GetVtype(att->atttypid);
}