-
Notifications
You must be signed in to change notification settings - Fork 9k
Expand file tree
/
Copy pathFunctionSQLJSON.h
More file actions
817 lines (714 loc) · 33.1 KB
/
Copy pathFunctionSQLJSON.h
File metadata and controls
817 lines (714 loc) · 33.1 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
#pragma once
#include <sstream>
#include <type_traits>
#include <Columns/ColumnArray.h>
#include <Columns/ColumnConst.h>
#include <Columns/ColumnNullable.h>
#include <Columns/ColumnString.h>
#include <Columns/ColumnsNumber.h>
#include <Columns/ColumnTuple.h>
#include <Core/Settings.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeLowCardinality.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypeTuple.h>
#include <DataTypes/DataTypesNumber.h>
#include <Common/JSONParsers/DummyJSONParser.h>
#include <Common/VectorWithMemoryTracking.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/IFunction.h>
#include <Functions/JSONPath/Generator/GeneratorJSONPath.h>
#include <Functions/JSONPath/Parsers/ParserJSONPath.h>
#include <Common/JSONParsers/SimdJSONParser.h>
#include <Interpreters/Context.h>
#include <base/range.h>
#include "config.h"
namespace DB
{
namespace Setting
{
extern const SettingsBool allow_simdjson;
extern const SettingsBool function_json_value_return_type_allow_complex;
extern const SettingsBool function_json_value_return_type_allow_nullable;
extern const SettingsUInt64 max_parser_backtracks;
extern const SettingsUInt64 max_parser_depth;
}
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int TOO_FEW_ARGUMENTS_FOR_FUNCTION;
extern const int BAD_ARGUMENTS;
}
/// Have implemented the operator << for json elements. So we could use stringstream to serialize json elements.
/// But stingstream have bad performance, not recommend to use it.
template <typename Element>
class DefaultJSONStringSerializer
{
public:
explicit DefaultJSONStringSerializer(ColumnString & col_str_) : col_str(col_str_) { }
void addRawData(const char * ptr, size_t len)
{
out << std::string_view(ptr, len);
}
void addRawString(std::string_view str)
{
out << str;
}
/// serialize the json element into stringstream
void addElement(const Element & element)
{
out << element.getElement();
}
void commit()
{
auto out_str = out.str();
col_str.insertData(out_str.data(), out_str.size());
}
void rollback() {}
private:
ColumnString & col_str;
std::stringstream out; // STYLE_CHECK_ALLOW_STD_STRING_STREAM
};
/// A more efficient way to serialize json elements into destination column.
/// Formatter takes the chars buffer in the ColumnString and put data into it directly.
template<typename Element, typename Formatter>
class JSONStringSerializer
{
public:
explicit JSONStringSerializer(ColumnString & col_str_)
: col_str(col_str_), chars(col_str_.getChars()), offsets(col_str_.getOffsets()), formatter(col_str_.getChars())
{
prev_offset = offsets.empty() ? 0 : offsets.back();
}
/// Put the data into column's buffer directly.
void addRawData(const char * ptr, size_t len)
{
chars.insert(ptr, ptr + len);
}
void addRawString(std::string_view str)
{
chars.insert(str.data(), str.data() + str.size());
}
/// serialize the json element into column's buffer directly
void addElement(const Element & element)
{
formatter.append(element.getElement());
}
void commit()
{
offsets.push_back(chars.size());
}
void rollback()
{
chars.resize(prev_offset);
}
private:
ColumnString & col_str;
ColumnString::Chars & chars;
IColumn::Offsets & offsets;
Formatter formatter;
size_t prev_offset;
};
class FunctionSQLJSONHelpers
{
public:
/// Single-path executor: parses one JSONPath, applies Impl per row.
template <typename Name, typename Impl, class JSONParser>
class Executor
{
public:
static ColumnPtr run(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count, uint32_t parse_depth, uint32_t parse_backtracks, bool function_json_value_return_type_allow_complex)
{
MutableColumnPtr to{result_type->createColumn()};
to->reserve(input_rows_count);
if (arguments.size() < 2)
{
throw Exception(ErrorCodes::TOO_FEW_ARGUMENTS_FOR_FUNCTION, "JSONPath functions require at least 2 arguments");
}
const auto & json_column = arguments[0];
if (!isString(json_column.type))
{
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"JSONPath functions require first argument to be JSON of string, illegal type: {}",
json_column.type->getName());
}
const auto & json_path_column = arguments[1];
if (!isString(json_path_column.type))
{
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Second argument of JSONPath functions must be a String JSONPath, "
"a Tuple(String) of JSONPaths, or an Array(String) of JSONPaths, illegal type: {}",
json_path_column.type->getName());
}
if (!isColumnConst(*json_path_column.column))
{
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Second argument of JSONPath functions must be a constant String, "
"a Tuple(String) of JSONPaths, or an Array(String) of JSONPaths");
}
/// Prepare to parse 1 argument (JSONPath)
String query = typeid_cast<const ColumnConst &>(*json_path_column.column).getValue<String>();
/// Tokenize the query
Tokens tokens(query.data(), query.data() + query.size());
/// Max depth 0 indicates that depth is not limited
IParser::Pos token_iterator(tokens, parse_depth, parse_backtracks);
/// Parse query and create AST tree
Expected expected;
ASTPtr res;
ParserJSONPath parser;
const bool parse_res = parser.parse(token_iterator, res, expected);
if (!parse_res)
{
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Unable to parse JSONPath");
}
JSONParser json_parser;
using Element = typename JSONParser::Element;
Element document;
bool document_ok = false;
/// Parse JSON for every row
Impl impl;
GeneratorJSONPath<JSONParser> generator_json_path(res);
for (size_t i = 0; i < input_rows_count; ++i)
{
std::string_view json = json_column.column->getDataAt(i);
document_ok = json_parser.parse(json, document);
bool added_to_column = false;
if (document_ok)
{
/// Instead of creating a new generator for each row, we can reuse the same one.
generator_json_path.reinitialize();
added_to_column = impl.insertResultToColumn(*to, document, generator_json_path, function_json_value_return_type_allow_complex);
}
if (!added_to_column)
{
to->insertDefault();
}
}
return to;
}
};
/// Multi-path executor: the second argument is a nested Tuple/Array/String structure.
/// Each string leaf is a JSONPath. The result mirrors the structure shape, with each leaf
/// replaced by the result of applying the Impl to that path.
///
/// The shape of the nested structure is analyzed once before the row loop.
/// A "plan" is built: a tree of nodes, each knowing its kind (Leaf/Tuple/Array),
/// its children, and pointers to the destination columns. The per-row loop
/// follows this plan without any type checks.
template <typename Impl, class JSONParser>
class MultiPathExecutor
{
public:
using Element = typename JSONParser::Element;
static ColumnPtr run(
const ColumnsWithTypeAndName & arguments,
const DataTypePtr & result_type,
size_t input_rows_count,
uint32_t parse_depth,
uint32_t parse_backtracks,
bool function_json_value_return_type_allow_complex)
{
const auto & json_column = arguments[0];
const auto & path_column = arguments[1];
if (!isString(json_column.type))
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"First argument of JSONPath functions must be a String, got: {}", json_column.type->getName());
if (!isColumnConst(*path_column.column))
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Second argument of JSONPath functions must be a constant String, "
"a Tuple(String) of JSONPaths, or an Array(String) of JSONPaths");
const auto & const_col = assert_cast<const ColumnConst &>(*path_column.column);
const auto & const_data = const_col.getDataColumn();
MutableColumnPtr to = result_type->createColumn();
to->reserve(input_rows_count);
/// Build the execution plan: parse the structure once, collect generators and column pointers.
VectorWithMemoryTracking<std::shared_ptr<GeneratorJSONPath<JSONParser>>> generators;
PlanNode plan = buildPlan(*to, const_data, path_column.type, 0, parse_depth, parse_backtracks, generators);
JSONParser json_parser;
Element document;
Impl impl;
for (size_t i = 0; i < input_rows_count; ++i)
{
std::string_view json = json_column.column->getDataAt(i);
bool document_ok = json_parser.parse(json, document);
size_t gen_idx = 0;
executePlan(plan, document, generators, gen_idx, document_ok, impl, function_json_value_return_type_allow_complex);
}
return to;
}
private:
enum class NodeKind : uint8_t { Leaf, Tuple, Array };
struct PlanNode
{
NodeKind kind = NodeKind::Leaf;
IColumn * dest = nullptr; /// destination column for this node
size_t array_size = 0; /// for Array nodes: constant number of elements
IColumn::Offsets * array_offsets = nullptr; /// for Array nodes: offsets column
VectorWithMemoryTracking<PlanNode> children; /// for Tuple: one child per element; for Array: single child (the element plan)
};
static std::shared_ptr<GeneratorJSONPath<JSONParser>> parseJSONPath(
std::string_view query, uint32_t parse_depth, uint32_t parse_backtracks)
{
Tokens tokens(query.data(), query.data() + query.size());
IParser::Pos token_iterator(tokens, parse_depth, parse_backtracks);
Expected expected;
ASTPtr res;
ParserJSONPath parser;
if (!parser.parse(token_iterator, res, expected))
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Unable to parse JSONPath");
return std::make_shared<GeneratorJSONPath<JSONParser>>(res);
}
/// Build the plan tree: analyze the structure once, collecting generators and column pointers.
/// `index` is the row index within `path_data` to read structure from.
static PlanNode buildPlan(
IColumn & dest,
const IColumn & path_data,
const DataTypePtr & path_type,
size_t index,
uint32_t parse_depth,
uint32_t parse_backtracks,
VectorWithMemoryTracking<std::shared_ptr<GeneratorJSONPath<JSONParser>>> & generators)
{
if (isString(path_type))
{
PlanNode node;
node.kind = NodeKind::Leaf;
node.dest = &dest;
generators.push_back(parseJSONPath(path_data.getDataAt(index), parse_depth, parse_backtracks));
return node;
}
if (checkAndGetDataType<DataTypeTuple>(path_type.get()))
{
const auto & tuple_col = assert_cast<const ColumnTuple &>(path_data);
const auto & tuple_type = assert_cast<const DataTypeTuple &>(*path_type);
auto & dest_tuple = assert_cast<ColumnTuple &>(dest);
PlanNode node;
node.kind = NodeKind::Tuple;
node.dest = &dest;
node.children.reserve(tuple_col.tupleSize());
for (size_t i = 0; i < tuple_col.tupleSize(); ++i)
node.children.push_back(buildPlan(
dest_tuple.getColumn(i), tuple_col.getColumn(i),
tuple_type.getElement(i), index, parse_depth, parse_backtracks, generators));
return node;
}
if (const auto * array_type = checkAndGetDataType<DataTypeArray>(path_type.get()))
{
const auto & array_path = assert_cast<const ColumnArray &>(path_data);
auto & dest_array = assert_cast<ColumnArray &>(dest);
size_t offset = index == 0 ? 0 : array_path.getOffsets()[index - 1];
size_t array_size = array_path.getOffsets()[index] - offset;
PlanNode node;
node.kind = NodeKind::Array;
node.dest = &dest;
node.array_size = array_size;
node.array_offsets = &dest_array.getOffsets();
/// Each array element gets its own child plan node (they may reference different generators).
node.children.reserve(array_size);
for (size_t i = 0; i < array_size; ++i)
node.children.push_back(buildPlan(
dest_array.getData(), array_path.getData(),
array_type->getNestedType(), offset + i,
parse_depth, parse_backtracks, generators));
return node;
}
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"JSONPath structure must contain only String, Tuple, or Array elements, got: {}",
path_type->getName());
}
/// Execute the plan for one row. No type checks — just follows the pre-built tree.
static void executePlan(
const PlanNode & node,
const Element & document,
const VectorWithMemoryTracking<std::shared_ptr<GeneratorJSONPath<JSONParser>>> & generators,
size_t & gen_idx,
bool document_ok,
Impl & impl,
bool function_json_value_return_type_allow_complex)
{
switch (node.kind)
{
case NodeKind::Leaf:
{
if (!document_ok)
{
node.dest->insertDefault();
++gen_idx;
return;
}
auto & generator = *generators[gen_idx];
generator.reinitialize();
bool added = impl.insertResultToColumn(*node.dest, document, generator, function_json_value_return_type_allow_complex);
if (!added)
node.dest->insertDefault();
++gen_idx;
return;
}
case NodeKind::Tuple:
{
for (const auto & child : node.children)
executePlan(child, document, generators, gen_idx, document_ok, impl, function_json_value_return_type_allow_complex);
return;
}
case NodeKind::Array:
{
for (const auto & child : node.children)
executePlan(child, document, generators, gen_idx, document_ok, impl, function_json_value_return_type_allow_complex);
size_t base = node.array_offsets->empty() ? 0 : node.array_offsets->back();
node.array_offsets->push_back(base + node.array_size);
return;
}
}
}
};
/// SQL JSON functions rely on the default Nullable/LowCardinality implementation, but for a
/// Dynamic argument that stripping has not happened yet at return-type declaration, so do it here
/// (top-level Nullable only, matching what executeImpl receives).
static DataTypePtr normalizeDynamicPathArgument(const DataTypePtr & type)
{
return removeNullable(recursiveRemoveLowCardinality(type));
}
/// Build a return type that mirrors the path argument structure,
/// replacing each String leaf with `leaf_type`.
static DataTypePtr buildReturnType(const DataTypePtr & path_type, const DataTypePtr & leaf_type)
{
if (isString(path_type))
return leaf_type;
if (const auto * tuple_type = checkAndGetDataType<DataTypeTuple>(path_type.get()))
{
DataTypes element_types;
element_types.reserve(tuple_type->getElements().size());
for (const auto & elem : tuple_type->getElements())
element_types.push_back(buildReturnType(elem, leaf_type));
if (tuple_type->hasExplicitNames())
return std::make_shared<DataTypeTuple>(element_types, tuple_type->getElementNames());
return std::make_shared<DataTypeTuple>(element_types);
}
if (const auto * array_type = checkAndGetDataType<DataTypeArray>(path_type.get()))
return std::make_shared<DataTypeArray>(buildReturnType(array_type->getNestedType(), leaf_type));
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"JSONPath structure must contain only String, Tuple, or Array elements, got {}",
path_type->getName());
}
/// Check whether a type is a nested structure (Tuple/Array) with String leaves,
/// i.e., suitable as a multi-path argument.
static bool isMultiPathType(const DataTypePtr & type)
{
if (isString(type))
return false; /// A plain String is not multi-path, it's the normal single-path case.
if (const auto * tuple_type = checkAndGetDataType<DataTypeTuple>(type.get()))
{
if (tuple_type->getElements().empty())
return false; /// Empty Tuple() is not a valid multi-path structure.
for (const auto & elem : tuple_type->getElements())
if (!isString(elem) && !isMultiPathType(elem))
return false;
return true;
}
if (const auto * array_type = checkAndGetDataType<DataTypeArray>(type.get()))
return isString(array_type->getNestedType()) || isMultiPathType(array_type->getNestedType());
return false;
}
};
template <typename Name, template <typename, typename> typename Impl>
class FunctionSQLJSON final : public IFunction
{
public:
static FunctionPtr create(ContextPtr context_) { return std::make_shared<FunctionSQLJSON>(context_); }
explicit FunctionSQLJSON(ContextPtr context_)
: max_parser_depth(context_->getSettingsRef()[Setting::max_parser_depth]),
max_parser_backtracks(context_->getSettingsRef()[Setting::max_parser_backtracks]),
allow_simdjson(context_->getSettingsRef()[Setting::allow_simdjson]),
function_json_value_return_type_allow_complex(context_->getSettingsRef()[Setting::function_json_value_return_type_allow_complex]),
function_json_value_return_type_allow_nullable(context_->getSettingsRef()[Setting::function_json_value_return_type_allow_nullable])
{
}
static constexpr auto name = Name::name;
String getName() const override { return Name::name; }
bool isVariadic() const override { return true; }
size_t getNumberOfArguments() const override { return 0; }
bool useDefaultImplementationForConstants() const override { return true; }
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
return Impl<DummyJSONParser, DefaultJSONStringSerializer<DummyJSONParser::Element>>::getReturnType(
Name::name, arguments, function_json_value_return_type_allow_nullable);
}
DataTypePtr getReturnTypeForDefaultImplementationForDynamic(const DataTypes & arguments) const override
{
return Impl<DummyJSONParser, DefaultJSONStringSerializer<DummyJSONParser::Element>>::getReturnTypeForDynamic(
arguments, function_json_value_return_type_allow_nullable);
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
{
/// Choose JSONParser.
/// 1. Lexer(path) -> Tokens
/// 2. Create ASTPtr
/// 3. Parser(Tokens, ASTPtr) -> complete AST
/// 4. Execute functions: call getNextItem on generator and handle each item
unsigned parse_depth = static_cast<unsigned>(max_parser_depth);
unsigned parse_backtracks = static_cast<unsigned>(max_parser_backtracks);
#if USE_SIMDJSON
if (allow_simdjson)
return Impl<SimdJSONParser, JSONStringSerializer<SimdJSONParser::Element, SimdJSONElementFormatter>>::execute(
arguments, result_type, input_rows_count, parse_depth, parse_backtracks, function_json_value_return_type_allow_complex);
#endif
return Impl<DummyJSONParser, DefaultJSONStringSerializer<DummyJSONParser::Element>>::execute(
arguments, result_type, input_rows_count, parse_depth, parse_backtracks, function_json_value_return_type_allow_complex);
}
private:
const size_t max_parser_depth;
const size_t max_parser_backtracks;
const bool allow_simdjson;
const bool function_json_value_return_type_allow_complex;
const bool function_json_value_return_type_allow_nullable;
};
struct NameJSONExists
{
static constexpr auto name{"JSON_EXISTS"};
};
struct NameJSONValue
{
static constexpr auto name{"JSON_VALUE"};
};
struct NameJSONQuery
{
static constexpr auto name{"JSON_QUERY"};
};
template <typename JSONParser, typename JSONStringSerializer>
class JSONExistsImpl
{
public:
using Element = typename JSONParser::Element;
static DataTypePtr getReturnType(const char *, const ColumnsWithTypeAndName & arguments, bool)
{
if (arguments.size() < 2)
throw Exception(ErrorCodes::TOO_FEW_ARGUMENTS_FOR_FUNCTION, "Function JSON_EXISTS requires at least 2 arguments");
if (FunctionSQLJSONHelpers::isMultiPathType(arguments[1].type))
return FunctionSQLJSONHelpers::buildReturnType(arguments[1].type, std::make_shared<DataTypeUInt8>());
return std::make_shared<DataTypeUInt8>();
}
static DataTypePtr getReturnTypeForDynamic(const DataTypes & arguments, bool)
{
if (arguments.size() >= 2)
{
auto path = FunctionSQLJSONHelpers::normalizeDynamicPathArgument(arguments[1]);
if (FunctionSQLJSONHelpers::isMultiPathType(path))
return FunctionSQLJSONHelpers::buildReturnType(path, std::make_shared<DataTypeUInt8>());
}
return std::make_shared<DataTypeUInt8>();
}
static size_t getNumberOfIndexArguments(const ColumnsWithTypeAndName & arguments) { return arguments.size() - 1; }
static bool insertResultToColumn(IColumn & dest, const Element & root, GeneratorJSONPath<JSONParser> & generator_json_path, bool)
{
Element current_element = root;
VisitorStatus status = {};
while ((status = generator_json_path.getNextItem(current_element)) != VisitorStatus::Exhausted)
{
if (status == VisitorStatus::Ok)
{
break;
}
current_element = root;
}
/// insert result, status can be either Ok (if we found the item)
/// or Exhausted (if we never found the item)
ColumnUInt8 & col_bool = assert_cast<ColumnUInt8 &>(dest);
if (status == VisitorStatus::Ok)
{
col_bool.insert(1);
}
else
{
col_bool.insert(0);
}
return true;
}
static ColumnPtr execute(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count,
uint32_t parse_depth, uint32_t parse_backtracks, bool function_json_value_return_type_allow_complex)
{
if (FunctionSQLJSONHelpers::isMultiPathType(arguments[1].type))
return FunctionSQLJSONHelpers::MultiPathExecutor<JSONExistsImpl, JSONParser>::run(
arguments, result_type, input_rows_count, parse_depth, parse_backtracks, function_json_value_return_type_allow_complex);
return FunctionSQLJSONHelpers::Executor<NameJSONExists, JSONExistsImpl, JSONParser>::run(
arguments, result_type, input_rows_count, parse_depth, parse_backtracks, function_json_value_return_type_allow_complex);
}
};
template <typename JSONParser, typename JSONStringSerializer>
class JSONValueImpl
{
public:
using Element = typename JSONParser::Element;
static DataTypePtr getReturnType(const char *, const ColumnsWithTypeAndName & arguments, bool function_json_value_return_type_allow_nullable)
{
if (arguments.size() < 2)
throw Exception(ErrorCodes::TOO_FEW_ARGUMENTS_FOR_FUNCTION, "Function JSON_VALUE requires at least 2 arguments");
if (FunctionSQLJSONHelpers::isMultiPathType(arguments[1].type))
{
DataTypePtr leaf_type = std::make_shared<DataTypeString>();
if (function_json_value_return_type_allow_nullable)
leaf_type = makeNullable(leaf_type);
return FunctionSQLJSONHelpers::buildReturnType(arguments[1].type, leaf_type);
}
if (function_json_value_return_type_allow_nullable)
{
DataTypePtr string_type = std::make_shared<DataTypeString>();
return std::make_shared<DataTypeNullable>(string_type);
}
return std::make_shared<DataTypeString>();
}
static DataTypePtr getReturnTypeForDynamic(const DataTypes & arguments, bool function_json_value_return_type_allow_nullable)
{
if (arguments.size() >= 2)
{
auto path = FunctionSQLJSONHelpers::normalizeDynamicPathArgument(arguments[1]);
if (FunctionSQLJSONHelpers::isMultiPathType(path))
{
DataTypePtr leaf_type = std::make_shared<DataTypeString>();
if (function_json_value_return_type_allow_nullable)
leaf_type = makeNullable(leaf_type);
return FunctionSQLJSONHelpers::buildReturnType(path, leaf_type);
}
}
return std::make_shared<DataTypeString>();
}
static size_t getNumberOfIndexArguments(const ColumnsWithTypeAndName & arguments) { return arguments.size() - 1; }
static bool insertResultToColumn(IColumn & dest, const Element & root, GeneratorJSONPath<JSONParser> & generator_json_path, bool function_json_value_return_type_allow_complex)
{
Element current_element = root;
VisitorStatus status = {};
while ((status = generator_json_path.getNextItem(current_element)) != VisitorStatus::Exhausted)
{
if (status == VisitorStatus::Ok)
{
if (function_json_value_return_type_allow_complex)
{
break;
}
if (!(current_element.isArray() || current_element.isObject()))
{
break;
}
}
else if (status == VisitorStatus::Error)
{
/// ON ERROR
/// Here it is possible to handle errors with ON ERROR (as described in ISO/IEC TR 19075-6),
/// however this functionality is not implemented yet
}
current_element = root;
}
if (status == VisitorStatus::Exhausted)
return false;
ColumnString * col_str = nullptr;
if (isColumnNullable(dest))
{
ColumnNullable & col_null = assert_cast<ColumnNullable &>(dest);
col_null.getNullMapData().push_back(false);
col_str = assert_cast<ColumnString *>(&col_null.getNestedColumn());
}
else
{
col_str = assert_cast<ColumnString *>(&dest);
}
JSONStringSerializer json_serializer(*col_str);
if (current_element.isString())
{
auto str = current_element.getString();
json_serializer.addRawString(str);
}
else
json_serializer.addElement(current_element);
json_serializer.commit();
return true;
}
static ColumnPtr execute(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count,
uint32_t parse_depth, uint32_t parse_backtracks, bool function_json_value_return_type_allow_complex)
{
if (FunctionSQLJSONHelpers::isMultiPathType(arguments[1].type))
return FunctionSQLJSONHelpers::MultiPathExecutor<JSONValueImpl, JSONParser>::run(
arguments, result_type, input_rows_count, parse_depth, parse_backtracks, function_json_value_return_type_allow_complex);
return FunctionSQLJSONHelpers::Executor<NameJSONValue, JSONValueImpl, JSONParser>::run(
arguments, result_type, input_rows_count, parse_depth, parse_backtracks, function_json_value_return_type_allow_complex);
}
};
/**
* Function to test jsonpath member access, will be removed in final PR
* @tparam JSONParser parser
*/
template <typename JSONParser, typename JSONStringSerializer>
class JSONQueryImpl
{
public:
using Element = typename JSONParser::Element;
static DataTypePtr getReturnType(const char *, const ColumnsWithTypeAndName & arguments, bool)
{
if (arguments.size() < 2)
throw Exception(ErrorCodes::TOO_FEW_ARGUMENTS_FOR_FUNCTION, "Function JSON_QUERY requires at least 2 arguments");
if (FunctionSQLJSONHelpers::isMultiPathType(arguments[1].type))
return FunctionSQLJSONHelpers::buildReturnType(arguments[1].type, std::make_shared<DataTypeString>());
return std::make_shared<DataTypeString>();
}
static DataTypePtr getReturnTypeForDynamic(const DataTypes & arguments, bool)
{
if (arguments.size() >= 2)
{
auto path = FunctionSQLJSONHelpers::normalizeDynamicPathArgument(arguments[1]);
if (FunctionSQLJSONHelpers::isMultiPathType(path))
return FunctionSQLJSONHelpers::buildReturnType(path, std::make_shared<DataTypeString>());
}
return std::make_shared<DataTypeString>();
}
static size_t getNumberOfIndexArguments(const ColumnsWithTypeAndName & arguments) { return arguments.size() - 1; }
static bool insertResultToColumn(IColumn & dest, const Element & root, GeneratorJSONPath<JSONParser> & generator_json_path, bool)
{
ColumnString & col_str = assert_cast<ColumnString &>(dest);
Element current_element = root;
VisitorStatus status = {};
bool success = false;
const char * array_begin = "[";
const char * array_end = "]";
const char * comma = ", ";
JSONStringSerializer json_serializer(col_str);
json_serializer.addRawData(array_begin, 1);
while ((status = generator_json_path.getNextItem(current_element)) != VisitorStatus::Exhausted)
{
if (status == VisitorStatus::Ok)
{
if (success)
{
json_serializer.addRawData(comma, 2);
}
success = true;
json_serializer.addElement(current_element);
}
else if (status == VisitorStatus::Error)
{
/// ON ERROR
/// Here it is possible to handle errors with ON ERROR (as described in ISO/IEC TR 19075-6),
/// however this functionality is not implemented yet
}
current_element = root;
}
if (!success)
{
json_serializer.rollback();
return false;
}
json_serializer.addRawData(array_end, 1);
json_serializer.commit();
return true;
}
static ColumnPtr execute(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count,
uint32_t parse_depth, uint32_t parse_backtracks, bool function_json_value_return_type_allow_complex)
{
if (FunctionSQLJSONHelpers::isMultiPathType(arguments[1].type))
return FunctionSQLJSONHelpers::MultiPathExecutor<JSONQueryImpl, JSONParser>::run(
arguments, result_type, input_rows_count, parse_depth, parse_backtracks, function_json_value_return_type_allow_complex);
return FunctionSQLJSONHelpers::Executor<NameJSONQuery, JSONQueryImpl, JSONParser>::run(
arguments, result_type, input_rows_count, parse_depth, parse_backtracks, function_json_value_return_type_allow_complex);
}
};
}