-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathexpression_print.h
More file actions
311 lines (301 loc) · 9.71 KB
/
Copy pathexpression_print.h
File metadata and controls
311 lines (301 loc) · 9.71 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#pragma once
#include "expression.h"
/**
* This module defines a function `print_tree` to recursively print an ExpressionItem.
*/
void print_tree_helper(ExpressionItem ref, int depth);
void print_n_spaces(int n) {
if (n == 0)
return;
printf(" ");
print_n_spaces(n - 1);
}
void print_expression_item_list(ExpressionItemList list, int depth) {
for (size_t i = 0; i < list.len; i++) {
print_tree_helper(list.list[i], depth);
}
}
void print_expression_item_list_field(const char* field_name, ExpressionItemList list, int depth) {
print_n_spaces(depth);
printf("%s\n", field_name);
print_expression_item_list(list, depth + 1);
}
void print_non_empty_expression_item_list_field(
const char* field_name, ExpressionItemList list, int depth) {
if (list.len == 0) {
return;
}
print_expression_item_list_field(field_name, list, depth);
}
void print_bool_field(const char* field_name, bool value, int depth) {
print_n_spaces(depth);
printf("%s: %s\n", field_name, value ? "true" : "false");
}
void print_opaque_op_name(void* op_type, KernelStringSlice name) {
int len = name.len & 0x7fffffff; // truncate to 31 bits to ensure a positive value
printf("%s(%.*s)\n", (char*) op_type, len, name.ptr);
}
void print_tree_helper(ExpressionItem ref, int depth) {
print_n_spaces(depth);
switch (ref.type) {
case BinOp: {
struct BinOp* op = ref.ref;
switch (op->op) {
case Add: {
printf("Add\n");
break;
}
case Minus: {
printf("Minus\n");
break;
};
case Divide: {
printf("Divide\n");
break;
};
case Multiply: {
printf("Multiply\n");
break;
};
case LessThan: {
printf("LessThan\n");
break;
};
case GreaterThan: {
printf("GreaterThan\n");
break;
};
case Equal: {
printf("Equal\n");
break;
};
case In: {
printf("In\n");
break;
};
case Distinct:
printf("Distinct\n");
break;
}
print_expression_item_list(op->exprs, depth + 1);
break;
}
case Variadic: {
struct Variadic* var = ref.ref;
switch (var->op) {
case And:
printf("And\n");
break;
case Or:
printf("Or\n");
break;
case StructExpression:
printf("StructExpression\n");
break;
case Coalesce:
printf("Coalesce\n");
break;
case ArrayConstructor:
printf("ArrayConstructor\n");
break;
}
print_expression_item_list(var->exprs, depth + 1);
break;
}
case StructPatch: {
struct StructPatchExpression* patch = ref.ref;
printf("StructPatch\n");
print_non_empty_expression_item_list_field(
"input_path", patch->input_path, depth + 1);
print_non_empty_expression_item_list_field(
"prepended_fields", patch->prepended_fields, depth + 1);
print_non_empty_expression_item_list_field(
"field_patches", patch->field_patches, depth + 1);
print_non_empty_expression_item_list_field(
"appended_fields", patch->appended_fields, depth + 1);
break;
}
case FieldPatch: {
struct FieldPatch* field_patch = ref.ref;
printf("FieldPatch\n");
print_n_spaces(depth + 1);
printf("field_name: %s\n", field_patch->field_name);
print_non_empty_expression_item_list_field(
"insertions", field_patch->insertions, depth + 1);
print_bool_field("keep_input", field_patch->keep_input, depth + 1);
print_bool_field("optional", field_patch->optional, depth + 1);
break;
}
case OpaqueExpression: {
struct OpaqueExpression* opaque = ref.ref;
visit_kernel_opaque_expression_op_name(opaque->op, "OpaqueExpression", print_opaque_op_name);
print_expression_item_list(opaque->exprs, depth + 1);
break;
}
case OpaquePredicate: {
struct OpaquePredicate* opaque = ref.ref;
visit_kernel_opaque_predicate_op_name(opaque->op, "OpaquePredicate", print_opaque_op_name);
print_expression_item_list(opaque->exprs, depth + 1);
break;
}
case Unknown: {
struct Unknown* unknown = ref.ref;
printf("Unknown(%s)\n", unknown->name);
break;
}
case Literal: {
struct Literal* lit = ref.ref;
switch (lit->type) {
case Integer:
printf("Integer(%d)\n", lit->value.integer_data);
break;
case Long:
printf("Long(%lld)\n", (long long)lit->value.long_data);
break;
case Short:
printf("Short(%hd)\n", lit->value.short_data);
break;
case Byte:
printf("Byte(%hhd)\n", lit->value.byte_data);
break;
case Float:
printf("Float(%f)\n", (float)lit->value.float_data);
break;
case Double:
printf("Double(%f)\n", lit->value.double_data);
break;
case String: {
printf("String(%s)\n", lit->value.string_data);
break;
}
case Boolean:
printf("Boolean(%d)\n", lit->value.boolean_data);
break;
case Timestamp:
printf("Timestamp(%lld)\n", (long long)lit->value.long_data);
break;
case TimestampNtz:
printf("TimestampNtz(%lld)\n", (long long)lit->value.long_data);
break;
case Date:
printf("Date(%d)\n", lit->value.integer_data);
break;
case IntervalYearMonth:
printf("IntervalYearMonth(%d)\n", lit->value.integer_data);
break;
case IntervalDayTime:
printf("IntervalDayTime(%lld)\n", (long long)lit->value.long_data);
break;
case Binary: {
printf("Binary(");
for (size_t i = 0; i < lit->value.binary.len; i++) {
printf("%02x", lit->value.binary.buf[i]);
}
printf(")\n");
break;
}
case Decimal: {
struct Decimal* dec = &lit->value.decimal;
printf("Decimal(%lld,%llu,%d,%d)\n",
(long long)dec->hi,
(unsigned long long)dec->lo,
dec->precision,
dec->scale);
break;
}
case Null: {
static const char* null_type_names[] = {
"Boolean", "Byte", "Short", "Integer", "Long", "Float",
"Double", "String", "Binary", "Date", "Timestamp", "TimestampNtz",
"Decimal", "IntervalYearMonth", "IntervalDayTime",
};
const size_t null_type_count = sizeof(null_type_names) / sizeof(null_type_names[0]);
struct NullTypeInfo* nt = &lit->value.null_type;
if (nt->type_tag == 12) {
printf("Null(Decimal(%d,%d))\n", nt->precision, nt->scale);
} else if (nt->type_tag < null_type_count) {
printf("Null(%s)\n", null_type_names[nt->type_tag]);
} else {
printf("Null(tag=%d)\n", nt->type_tag);
}
break;
}
case Struct:
printf("Struct\n");
struct Struct* struct_data = &lit->value.struct_data;
for (size_t i = 0; i < struct_data->values.len; i++) {
print_n_spaces(depth + 1);
// Extract field name from field
ExpressionItem item = struct_data->fields.list[i];
assert(item.type == Literal);
struct Literal* lit = item.ref;
assert(lit->type == String);
printf("Field: %s\n", lit->value.string_data);
print_tree_helper(struct_data->values.list[i], depth + 2);
}
break;
case Array:
printf("Array\n");
struct ArrayData* array = &lit->value.array_data;
print_expression_item_list(array->exprs, depth + 1);
break;
case Map:
printf("Map\n");
struct MapData* map_data = &lit->value.map_data;
for (size_t i = 0; i < map_data->keys.len; i++) {
print_n_spaces(depth + 1);
// Extract key
ExpressionItem key = map_data->keys.list[i];
assert(key.type == Literal);
struct Literal* key_lit = key.ref;
assert(key_lit->type == String);
// Extract val
ExpressionItem val = map_data->vals.list[i];
assert(val.type == Literal);
struct Literal* val_lit = val.ref;
assert(val_lit->type == String);
// instead of recursing (which forces newlines) we just directly print strings here
printf("String(%s): String(%s)\n", key_lit->value.string_data, val_lit->value.string_data);
}
break;
}
break;
}
case Unary: {
struct Unary* unary = ref.ref;
switch (unary->type) {
case Not:
printf("Not\n");
break;
case IsNull:
printf("IsNull\n");
break;
}
print_expression_item_list(unary->sub_expr, depth + 1);
break;
}
case Column: {
struct Column* column = ref.ref;
printf("Column(");
for (size_t i = 0; i < column->len; i++) {
printf("%s", i > 0 ? ", " : "");
fwrite(column->parts[i].ptr, sizeof(char), column->parts[i].len, stdout);
}
printf(")\n");
break;
}
case MapToStruct: {
struct MapToStructExpr* m2s = ref.ref;
if (m2s->timestamp_timezone == NULL) {
printf("MapToStruct\n");
} else {
printf("MapToStruct(timestamp_timezone=%s)\n", m2s->timestamp_timezone);
}
print_expression_item_list(m2s->child_expr, depth + 1);
break;
}
}
}
void print_expression(ExpressionItemList expression) {
print_expression_item_list(expression, 0);
}