-
Notifications
You must be signed in to change notification settings - Fork 848
/
compression_with_clause.c
279 lines (237 loc) · 7.08 KB
/
compression_with_clause.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
/*
* This file and its contents are licensed under the Apache License 2.0.
* Please see the included NOTICE for copyright information and
* LICENSE-APACHE for a copy of the license.
*/
#include <postgres.h>
#include <fmgr.h>
#include <access/htup_details.h>
#include <catalog/dependency.h>
#include <catalog/namespace.h>
#include <catalog/pg_type.h>
#include <catalog/pg_trigger.h>
#include <commands/trigger.h>
#include <storage/lmgr.h>
#include <utils/builtins.h>
#include <utils/lsyscache.h>
#include <parser/parser.h>
#include "compat/compat.h"
#include "compression_with_clause.h"
static const WithClauseDefinition compress_hypertable_with_clause_def[] = {
[CompressEnabled] = {
.arg_name = "compress",
.type_id = BOOLOID,
.default_val = BoolGetDatum(false),
},
[CompressSegmentBy] = {
.arg_name = "compress_segmentby",
.type_id = TEXTOID,
},
[CompressOrderBy] = {
.arg_name = "compress_orderby",
.type_id = TEXTOID,
},
};
WithClauseResult *
ts_compress_hypertable_set_clause_parse(const List *defelems)
{
return ts_with_clauses_parse(defelems,
compress_hypertable_with_clause_def,
TS_ARRAY_LEN(compress_hypertable_with_clause_def));
}
static inline void
throw_segment_by_error(char *segment_by)
{
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("unable to parse segmenting option \"%s\"", segment_by),
errhint("The option timescaledb.compress_segmentby must"
" be a set of columns separated by commas.")));
}
static bool
select_stmt_as_expected(SelectStmt *stmt)
{
/* The only parts of the select stmt that are allowed to be set are the order by or group by.
* Check that no other fields are set */
if (stmt->distinctClause != NIL || stmt->intoClause != NULL || stmt->targetList != NIL ||
stmt->whereClause != NULL || stmt->havingClause != NULL || stmt->windowClause != NIL ||
stmt->valuesLists != NULL || stmt->limitOffset != NULL || stmt->limitCount != NULL ||
stmt->lockingClause != NIL || stmt->withClause != NULL || stmt->op != 0 ||
stmt->all != false || stmt->larg != NULL || stmt->rarg != NULL)
return false;
return true;
}
static List *
parse_segment_collist(char *inpstr, Hypertable *hypertable)
{
StringInfoData buf;
List *parsed;
ListCell *lc;
SelectStmt *select;
List *collist = NIL;
RawStmt *raw;
if (strlen(inpstr) == 0)
return NIL;
initStringInfo(&buf);
/* parse the segment by list exactly how you would a group by */
appendStringInfo(&buf,
"SELECT FROM %s.%s GROUP BY %s",
quote_identifier(hypertable->fd.schema_name.data),
quote_identifier(hypertable->fd.table_name.data),
inpstr);
PG_TRY();
{
parsed = raw_parser_compat(buf.data);
}
PG_CATCH();
{
throw_segment_by_error(inpstr);
PG_RE_THROW();
}
PG_END_TRY();
if (list_length(parsed) != 1)
throw_segment_by_error(inpstr);
if (!IsA(linitial(parsed), RawStmt))
throw_segment_by_error(inpstr);
raw = linitial(parsed);
if (!IsA(raw->stmt, SelectStmt))
throw_segment_by_error(inpstr);
select = (SelectStmt *) raw->stmt;
if (!select_stmt_as_expected(select))
throw_segment_by_error(inpstr);
if (select->sortClause != NIL)
throw_segment_by_error(inpstr);
short index = 0;
foreach (lc, select->groupClause)
{
ColumnRef *cf;
CompressedParsedCol *col = (CompressedParsedCol *) palloc(sizeof(*col));
if (!IsA(lfirst(lc), ColumnRef))
throw_segment_by_error(inpstr);
cf = lfirst(lc);
if (list_length(cf->fields) != 1)
throw_segment_by_error(inpstr);
if (!IsA(linitial(cf->fields), String))
throw_segment_by_error(inpstr);
col->index = index;
index++;
namestrcpy(&col->colname, strVal(linitial(cf->fields)));
collist = lappend(collist, (void *) col);
}
return collist;
}
static inline void
throw_order_by_error(char *order_by)
{
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("unable to parse ordering option \"%s\"", order_by),
errhint("The timescaledb.compress_orderby option must be a set of column"
" names with sort options, separated by commas."
" It is the same format as an ORDER BY clause.")));
}
/* compress_orderby is parsed same as order by in select queries */
static List *
parse_order_collist(char *inpstr, Hypertable *hypertable)
{
StringInfoData buf;
List *parsed;
ListCell *lc;
SelectStmt *select;
List *collist = NIL;
RawStmt *raw;
if (strlen(inpstr) == 0)
return NIL;
initStringInfo(&buf);
/* parse the segment by list exactly how you would a order by by */
appendStringInfo(&buf,
"SELECT FROM %s.%s ORDER BY %s",
quote_identifier(hypertable->fd.schema_name.data),
quote_identifier(hypertable->fd.table_name.data),
inpstr);
PG_TRY();
{
parsed = raw_parser_compat(buf.data);
}
PG_CATCH();
{
throw_order_by_error(inpstr);
PG_RE_THROW();
}
PG_END_TRY();
if (list_length(parsed) != 1)
throw_order_by_error(inpstr);
if (!IsA(linitial(parsed), RawStmt))
throw_order_by_error(inpstr);
raw = linitial(parsed);
if (!IsA(raw->stmt, SelectStmt))
throw_order_by_error(inpstr);
select = (SelectStmt *) raw->stmt;
if (!select_stmt_as_expected(select))
throw_order_by_error(inpstr);
if (select->groupClause != NIL)
throw_order_by_error(inpstr);
short index = 0;
foreach (lc, select->sortClause)
{
SortBy *sort_by;
ColumnRef *cf;
CompressedParsedCol *col = (CompressedParsedCol *) palloc(sizeof(*col));
if (!IsA(lfirst(lc), SortBy))
throw_order_by_error(inpstr);
sort_by = lfirst(lc);
if (!IsA(sort_by->node, ColumnRef))
throw_order_by_error(inpstr);
cf = (ColumnRef *) sort_by->node;
if (list_length(cf->fields) != 1)
throw_order_by_error(inpstr);
if (!IsA(linitial(cf->fields), String))
throw_order_by_error(inpstr);
col->index = index;
index++;
namestrcpy(&col->colname, strVal(linitial(cf->fields)));
if (sort_by->sortby_dir != SORTBY_ASC && sort_by->sortby_dir != SORTBY_DESC &&
sort_by->sortby_dir != SORTBY_DEFAULT)
throw_order_by_error(inpstr);
col->asc = sort_by->sortby_dir == SORTBY_ASC || sort_by->sortby_dir == SORTBY_DEFAULT;
if (sort_by->sortby_nulls == SORTBY_NULLS_DEFAULT)
{
/* default null ordering is LAST for ASC, FIRST for DESC */
col->nullsfirst = !col->asc;
}
else
{
col->nullsfirst = sort_by->sortby_nulls == SORTBY_NULLS_FIRST;
}
collist = lappend(collist, (void *) col);
}
return collist;
}
/* returns List of CompressedParsedCol
* compress_segmentby = `col1,col2,col3`
*/
List *
ts_compress_hypertable_parse_segment_by(WithClauseResult *parsed_options, Hypertable *hypertable)
{
if (parsed_options[CompressSegmentBy].is_default == false)
{
Datum textarg = parsed_options[CompressSegmentBy].parsed;
return parse_segment_collist(TextDatumGetCString(textarg), hypertable);
}
else
return NIL;
}
/* returns List of CompressedParsedCol
* E.g. timescaledb.compress_orderby = 'col1 asc nulls first,col2 desc,col3'
*/
List *
ts_compress_hypertable_parse_order_by(WithClauseResult *parsed_options, Hypertable *hypertable)
{
if (parsed_options[CompressOrderBy].is_default == false)
{
Datum textarg = parsed_options[CompressOrderBy].parsed;
return parse_order_collist(TextDatumGetCString(textarg), hypertable);
}
else
return NIL;
}