duckdb 0.7.2-dev717.0 → 0.7.2-dev865.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (179) hide show
  1. package/binding.gyp +2 -0
  2. package/lib/duckdb.d.ts +12 -1
  3. package/lib/duckdb.js +19 -0
  4. package/package.json +1 -1
  5. package/src/duckdb/extension/json/include/json_common.hpp +1 -0
  6. package/src/duckdb/extension/json/include/json_functions.hpp +1 -0
  7. package/src/duckdb/extension/json/include/json_serializer.hpp +77 -0
  8. package/src/duckdb/extension/json/json_functions/json_serialize_sql.cpp +147 -0
  9. package/src/duckdb/extension/json/json_functions.cpp +1 -0
  10. package/src/duckdb/extension/json/json_scan.cpp +2 -2
  11. package/src/duckdb/extension/json/json_serializer.cpp +217 -0
  12. package/src/duckdb/src/catalog/catalog.cpp +21 -5
  13. package/src/duckdb/src/common/enums/expression_type.cpp +8 -222
  14. package/src/duckdb/src/common/enums/join_type.cpp +3 -22
  15. package/src/duckdb/src/common/exception.cpp +2 -2
  16. package/src/duckdb/src/common/serializer/enum_serializer.cpp +1172 -0
  17. package/src/duckdb/src/common/types/value.cpp +117 -93
  18. package/src/duckdb/src/common/types/vector.cpp +140 -1
  19. package/src/duckdb/src/common/types.cpp +166 -89
  20. package/src/duckdb/src/execution/operator/helper/physical_limit.cpp +3 -0
  21. package/src/duckdb/src/execution/physical_plan/plan_aggregate.cpp +5 -8
  22. package/src/duckdb/src/function/scalar/date/date_part.cpp +2 -2
  23. package/src/duckdb/src/function/scalar/date/date_trunc.cpp +2 -2
  24. package/src/duckdb/src/function/scalar/list/list_aggregates.cpp +1 -1
  25. package/src/duckdb/src/function/scalar/list/list_lambdas.cpp +4 -0
  26. package/src/duckdb/src/function/scalar/operators/arithmetic.cpp +8 -8
  27. package/src/duckdb/src/function/scalar/string/regexp/regexp_extract_all.cpp +243 -0
  28. package/src/duckdb/src/function/scalar/string/regexp/regexp_util.cpp +79 -0
  29. package/src/duckdb/src/function/scalar/string/regexp.cpp +21 -80
  30. package/src/duckdb/src/function/table/arrow_conversion.cpp +7 -1
  31. package/src/duckdb/src/function/table/table_scan.cpp +1 -1
  32. package/src/duckdb/src/function/table/version/pragma_version.cpp +2 -2
  33. package/src/duckdb/src/include/duckdb/catalog/catalog.hpp +3 -0
  34. package/src/duckdb/src/include/duckdb/common/enums/aggregate_handling.hpp +2 -0
  35. package/src/duckdb/src/include/duckdb/common/enums/expression_type.hpp +2 -3
  36. package/src/duckdb/src/include/duckdb/common/enums/joinref_type.hpp +2 -0
  37. package/src/duckdb/src/include/duckdb/common/enums/order_type.hpp +2 -0
  38. package/src/duckdb/src/include/duckdb/common/enums/set_operation_type.hpp +2 -1
  39. package/src/duckdb/src/include/duckdb/common/exception.hpp +40 -9
  40. package/src/duckdb/src/include/duckdb/common/preserved_error.hpp +3 -0
  41. package/src/duckdb/src/include/duckdb/common/serializer/enum_serializer.hpp +113 -0
  42. package/src/duckdb/src/include/duckdb/common/serializer/format_deserializer.hpp +336 -0
  43. package/src/duckdb/src/include/duckdb/common/serializer/format_serializer.hpp +268 -0
  44. package/src/duckdb/src/include/duckdb/common/serializer/serialization_traits.hpp +126 -0
  45. package/src/duckdb/src/include/duckdb/common/string_util.hpp +12 -0
  46. package/src/duckdb/src/include/duckdb/common/types/value.hpp +2 -31
  47. package/src/duckdb/src/include/duckdb/common/types/vector.hpp +3 -0
  48. package/src/duckdb/src/include/duckdb/common/types.hpp +8 -2
  49. package/src/duckdb/src/include/duckdb/function/scalar/regexp.hpp +81 -1
  50. package/src/duckdb/src/include/duckdb/main/extension_entries.hpp +1 -0
  51. package/src/duckdb/src/include/duckdb/parser/common_table_expression_info.hpp +2 -0
  52. package/src/duckdb/src/include/duckdb/parser/expression/between_expression.hpp +3 -0
  53. package/src/duckdb/src/include/duckdb/parser/expression/bound_expression.hpp +2 -0
  54. package/src/duckdb/src/include/duckdb/parser/expression/case_expression.hpp +5 -0
  55. package/src/duckdb/src/include/duckdb/parser/expression/cast_expression.hpp +2 -0
  56. package/src/duckdb/src/include/duckdb/parser/expression/collate_expression.hpp +2 -0
  57. package/src/duckdb/src/include/duckdb/parser/expression/columnref_expression.hpp +2 -0
  58. package/src/duckdb/src/include/duckdb/parser/expression/comparison_expression.hpp +2 -0
  59. package/src/duckdb/src/include/duckdb/parser/expression/conjunction_expression.hpp +2 -0
  60. package/src/duckdb/src/include/duckdb/parser/expression/constant_expression.hpp +3 -0
  61. package/src/duckdb/src/include/duckdb/parser/expression/default_expression.hpp +1 -0
  62. package/src/duckdb/src/include/duckdb/parser/expression/function_expression.hpp +2 -0
  63. package/src/duckdb/src/include/duckdb/parser/expression/lambda_expression.hpp +2 -0
  64. package/src/duckdb/src/include/duckdb/parser/expression/operator_expression.hpp +2 -0
  65. package/src/duckdb/src/include/duckdb/parser/expression/parameter_expression.hpp +2 -0
  66. package/src/duckdb/src/include/duckdb/parser/expression/positional_reference_expression.hpp +2 -0
  67. package/src/duckdb/src/include/duckdb/parser/expression/star_expression.hpp +2 -0
  68. package/src/duckdb/src/include/duckdb/parser/expression/subquery_expression.hpp +2 -0
  69. package/src/duckdb/src/include/duckdb/parser/expression/window_expression.hpp +5 -0
  70. package/src/duckdb/src/include/duckdb/parser/parsed_data/sample_options.hpp +2 -0
  71. package/src/duckdb/src/include/duckdb/parser/parsed_expression.hpp +5 -0
  72. package/src/duckdb/src/include/duckdb/parser/query_node/recursive_cte_node.hpp +3 -0
  73. package/src/duckdb/src/include/duckdb/parser/query_node/select_node.hpp +5 -0
  74. package/src/duckdb/src/include/duckdb/parser/query_node/set_operation_node.hpp +3 -0
  75. package/src/duckdb/src/include/duckdb/parser/query_node.hpp +11 -1
  76. package/src/duckdb/src/include/duckdb/parser/result_modifier.hpp +24 -1
  77. package/src/duckdb/src/include/duckdb/parser/sql_statement.hpp +2 -1
  78. package/src/duckdb/src/include/duckdb/parser/statement/select_statement.hpp +6 -1
  79. package/src/duckdb/src/include/duckdb/parser/tableref/basetableref.hpp +4 -0
  80. package/src/duckdb/src/include/duckdb/parser/tableref/emptytableref.hpp +2 -0
  81. package/src/duckdb/src/include/duckdb/parser/tableref/expressionlistref.hpp +3 -0
  82. package/src/duckdb/src/include/duckdb/parser/tableref/joinref.hpp +3 -0
  83. package/src/duckdb/src/include/duckdb/parser/tableref/pivotref.hpp +9 -0
  84. package/src/duckdb/src/include/duckdb/parser/tableref/subqueryref.hpp +3 -0
  85. package/src/duckdb/src/include/duckdb/parser/tableref/table_function_ref.hpp +3 -0
  86. package/src/duckdb/src/include/duckdb/parser/tableref.hpp +3 -1
  87. package/src/duckdb/src/include/duckdb/storage/statistics/numeric_stats.hpp +9 -52
  88. package/src/duckdb/src/include/duckdb/storage/statistics/numeric_stats_union.hpp +62 -0
  89. package/src/duckdb/src/include/duckdb/storage/table/column_checkpoint_state.hpp +2 -1
  90. package/src/duckdb/src/include/duckdb/storage/table/column_data.hpp +6 -3
  91. package/src/duckdb/src/include/duckdb/storage/table/column_data_checkpointer.hpp +3 -2
  92. package/src/duckdb/src/include/duckdb/storage/table/column_segment.hpp +5 -3
  93. package/src/duckdb/src/include/duckdb/storage/table/persistent_table_data.hpp +4 -1
  94. package/src/duckdb/src/include/duckdb/storage/table/row_group.hpp +6 -3
  95. package/src/duckdb/src/include/duckdb/storage/table/row_group_collection.hpp +5 -3
  96. package/src/duckdb/src/include/duckdb/storage/table/row_group_segment_tree.hpp +37 -0
  97. package/src/duckdb/src/include/duckdb/storage/table/scan_state.hpp +8 -1
  98. package/src/duckdb/src/include/duckdb/storage/table/segment_base.hpp +4 -3
  99. package/src/duckdb/src/include/duckdb/storage/table/segment_tree.hpp +271 -26
  100. package/src/duckdb/src/main/extension/extension_install.cpp +7 -2
  101. package/src/duckdb/src/optimizer/deliminator.cpp +1 -1
  102. package/src/duckdb/src/optimizer/filter_combiner.cpp +1 -1
  103. package/src/duckdb/src/optimizer/join_order/join_order_optimizer.cpp +3 -3
  104. package/src/duckdb/src/optimizer/rule/move_constants.cpp +2 -2
  105. package/src/duckdb/src/optimizer/statistics/operator/propagate_filter.cpp +1 -1
  106. package/src/duckdb/src/parser/common_table_expression_info.cpp +19 -0
  107. package/src/duckdb/src/parser/expression/between_expression.cpp +17 -0
  108. package/src/duckdb/src/parser/expression/case_expression.cpp +28 -0
  109. package/src/duckdb/src/parser/expression/cast_expression.cpp +17 -0
  110. package/src/duckdb/src/parser/expression/collate_expression.cpp +16 -0
  111. package/src/duckdb/src/parser/expression/columnref_expression.cpp +15 -0
  112. package/src/duckdb/src/parser/expression/comparison_expression.cpp +16 -0
  113. package/src/duckdb/src/parser/expression/conjunction_expression.cpp +15 -0
  114. package/src/duckdb/src/parser/expression/constant_expression.cpp +14 -0
  115. package/src/duckdb/src/parser/expression/default_expression.cpp +7 -0
  116. package/src/duckdb/src/parser/expression/function_expression.cpp +35 -0
  117. package/src/duckdb/src/parser/expression/lambda_expression.cpp +16 -0
  118. package/src/duckdb/src/parser/expression/operator_expression.cpp +15 -0
  119. package/src/duckdb/src/parser/expression/parameter_expression.cpp +15 -0
  120. package/src/duckdb/src/parser/expression/positional_reference_expression.cpp +14 -0
  121. package/src/duckdb/src/parser/expression/star_expression.cpp +20 -0
  122. package/src/duckdb/src/parser/expression/subquery_expression.cpp +20 -0
  123. package/src/duckdb/src/parser/expression/window_expression.cpp +43 -0
  124. package/src/duckdb/src/parser/parsed_data/sample_options.cpp +22 -10
  125. package/src/duckdb/src/parser/parsed_expression.cpp +72 -0
  126. package/src/duckdb/src/parser/query_node/recursive_cte_node.cpp +21 -0
  127. package/src/duckdb/src/parser/query_node/select_node.cpp +31 -0
  128. package/src/duckdb/src/parser/query_node/set_operation_node.cpp +17 -0
  129. package/src/duckdb/src/parser/query_node.cpp +50 -0
  130. package/src/duckdb/src/parser/result_modifier.cpp +78 -0
  131. package/src/duckdb/src/parser/statement/select_statement.cpp +12 -0
  132. package/src/duckdb/src/parser/tableref/basetableref.cpp +21 -0
  133. package/src/duckdb/src/parser/tableref/emptytableref.cpp +4 -0
  134. package/src/duckdb/src/parser/tableref/expressionlistref.cpp +17 -0
  135. package/src/duckdb/src/parser/tableref/joinref.cpp +25 -0
  136. package/src/duckdb/src/parser/tableref/pivotref.cpp +53 -0
  137. package/src/duckdb/src/parser/tableref/subqueryref.cpp +15 -0
  138. package/src/duckdb/src/parser/tableref/table_function.cpp +17 -0
  139. package/src/duckdb/src/parser/tableref.cpp +46 -0
  140. package/src/duckdb/src/parser/transform/expression/transform_array_access.cpp +11 -0
  141. package/src/duckdb/src/parser/transform/expression/transform_bool_expr.cpp +1 -1
  142. package/src/duckdb/src/parser/transform/expression/transform_operator.cpp +1 -1
  143. package/src/duckdb/src/parser/transform/expression/transform_subquery.cpp +1 -1
  144. package/src/duckdb/src/planner/binder/expression/bind_function_expression.cpp +22 -4
  145. package/src/duckdb/src/planner/binder/expression/bind_subquery_expression.cpp +4 -0
  146. package/src/duckdb/src/planner/binder/tableref/plan_joinref.cpp +1 -1
  147. package/src/duckdb/src/planner/expression/bound_expression.cpp +4 -0
  148. package/src/duckdb/src/storage/checkpoint/table_data_reader.cpp +3 -11
  149. package/src/duckdb/src/storage/checkpoint/table_data_writer.cpp +6 -0
  150. package/src/duckdb/src/storage/checkpoint_manager.cpp +1 -0
  151. package/src/duckdb/src/storage/compression/numeric_constant.cpp +2 -2
  152. package/src/duckdb/src/storage/data_table.cpp +1 -1
  153. package/src/duckdb/src/storage/statistics/numeric_stats.cpp +145 -83
  154. package/src/duckdb/src/storage/statistics/numeric_stats_union.cpp +65 -0
  155. package/src/duckdb/src/storage/storage_info.cpp +1 -1
  156. package/src/duckdb/src/storage/table/column_checkpoint_state.cpp +1 -6
  157. package/src/duckdb/src/storage/table/column_data.cpp +29 -35
  158. package/src/duckdb/src/storage/table/column_data_checkpointer.cpp +5 -5
  159. package/src/duckdb/src/storage/table/column_segment.cpp +8 -7
  160. package/src/duckdb/src/storage/table/list_column_data.cpp +2 -1
  161. package/src/duckdb/src/storage/table/persistent_table_data.cpp +2 -1
  162. package/src/duckdb/src/storage/table/row_group.cpp +9 -9
  163. package/src/duckdb/src/storage/table/row_group_collection.cpp +82 -66
  164. package/src/duckdb/src/storage/table/scan_state.cpp +22 -3
  165. package/src/duckdb/src/storage/table/standard_column_data.cpp +1 -0
  166. package/src/duckdb/src/storage/table/struct_column_data.cpp +1 -0
  167. package/src/duckdb/src/verification/deserialized_statement_verifier.cpp +0 -1
  168. package/src/duckdb/third_party/libpg_query/src_backend_parser_gram.cpp +11780 -11512
  169. package/src/duckdb/third_party/re2/re2/re2.cc +9 -0
  170. package/src/duckdb/third_party/re2/re2/re2.h +2 -0
  171. package/src/duckdb/ub_extension_json_json_functions.cpp +2 -0
  172. package/src/duckdb/ub_src_common_serializer.cpp +2 -0
  173. package/src/duckdb/ub_src_function_scalar_string_regexp.cpp +4 -0
  174. package/src/duckdb/ub_src_parser.cpp +2 -0
  175. package/src/duckdb/ub_src_storage_statistics.cpp +2 -0
  176. package/src/duckdb/ub_src_storage_table.cpp +0 -2
  177. package/src/utils.cpp +12 -0
  178. package/test/extension.test.ts +44 -26
  179. package/src/duckdb/src/storage/table/segment_tree.cpp +0 -179
@@ -0,0 +1,113 @@
1
+ #pragma once
2
+ #include <stdint.h>
3
+
4
+ namespace duckdb {
5
+
6
+ struct EnumSerializer {
7
+ // String -> Enum
8
+ template <class T>
9
+ static T StringToEnum(const char *value) = delete;
10
+
11
+ // Enum -> String
12
+ template <class T>
13
+ static const char *EnumToString(T value) = delete;
14
+ };
15
+
16
+ enum class OrderType : uint8_t;
17
+ enum class OrderByNullType : uint8_t;
18
+ enum class ResultModifierType : uint8_t;
19
+ enum class ExtraTypeInfoType : uint8_t;
20
+ enum class TableReferenceType : uint8_t;
21
+ enum class JoinRefType : uint8_t;
22
+ enum class JoinType : uint8_t;
23
+ enum class AggregateHandling : uint8_t;
24
+ enum class QueryNodeType : uint8_t;
25
+ enum class SetOperationType : uint8_t;
26
+ enum class WindowBoundary : uint8_t;
27
+ enum class SubqueryType : uint8_t;
28
+ enum class ExpressionType : uint8_t;
29
+ enum class ExpressionClass : uint8_t;
30
+ enum class SampleMethod : uint8_t;
31
+ enum class LogicalTypeId : uint8_t;
32
+
33
+ template <>
34
+ OrderType EnumSerializer::StringToEnum<OrderType>(const char *value);
35
+ template <>
36
+ const char *EnumSerializer::EnumToString<OrderType>(OrderType value);
37
+
38
+ template <>
39
+ OrderByNullType EnumSerializer::StringToEnum<OrderByNullType>(const char *value);
40
+ template <>
41
+ const char *EnumSerializer::EnumToString<OrderByNullType>(OrderByNullType value);
42
+
43
+ template <>
44
+ ResultModifierType EnumSerializer::StringToEnum<ResultModifierType>(const char *value);
45
+ template <>
46
+ const char *EnumSerializer::EnumToString<ResultModifierType>(ResultModifierType value);
47
+
48
+ template <>
49
+ ExtraTypeInfoType EnumSerializer::StringToEnum<ExtraTypeInfoType>(const char *value);
50
+ template <>
51
+ const char *EnumSerializer::EnumToString<ExtraTypeInfoType>(ExtraTypeInfoType value);
52
+
53
+ template <>
54
+ TableReferenceType EnumSerializer::StringToEnum<TableReferenceType>(const char *value);
55
+ template <>
56
+ const char *EnumSerializer::EnumToString<TableReferenceType>(TableReferenceType value);
57
+
58
+ template <>
59
+ JoinRefType EnumSerializer::StringToEnum<JoinRefType>(const char *value);
60
+ template <>
61
+ const char *EnumSerializer::EnumToString<JoinRefType>(JoinRefType value);
62
+
63
+ template <>
64
+ JoinType EnumSerializer::StringToEnum<JoinType>(const char *value);
65
+ template <>
66
+ const char *EnumSerializer::EnumToString<JoinType>(JoinType value);
67
+
68
+ template <>
69
+ AggregateHandling EnumSerializer::StringToEnum<AggregateHandling>(const char *value);
70
+ template <>
71
+ const char *EnumSerializer::EnumToString<AggregateHandling>(AggregateHandling value);
72
+
73
+ template <>
74
+ QueryNodeType EnumSerializer::StringToEnum<QueryNodeType>(const char *value);
75
+ template <>
76
+ const char *EnumSerializer::EnumToString<QueryNodeType>(QueryNodeType value);
77
+
78
+ template <>
79
+ SetOperationType EnumSerializer::StringToEnum<SetOperationType>(const char *value);
80
+ template <>
81
+ const char *EnumSerializer::EnumToString<SetOperationType>(SetOperationType value);
82
+
83
+ template <>
84
+ WindowBoundary EnumSerializer::StringToEnum<WindowBoundary>(const char *value);
85
+ template <>
86
+ const char *EnumSerializer::EnumToString<WindowBoundary>(WindowBoundary value);
87
+
88
+ template <>
89
+ SubqueryType EnumSerializer::StringToEnum<SubqueryType>(const char *value);
90
+ template <>
91
+ const char *EnumSerializer::EnumToString<SubqueryType>(SubqueryType value);
92
+
93
+ template <>
94
+ ExpressionType EnumSerializer::StringToEnum<ExpressionType>(const char *value);
95
+ template <>
96
+ const char *EnumSerializer::EnumToString<ExpressionType>(ExpressionType value);
97
+
98
+ template <>
99
+ ExpressionClass EnumSerializer::StringToEnum<ExpressionClass>(const char *value);
100
+ template <>
101
+ const char *EnumSerializer::EnumToString<ExpressionClass>(ExpressionClass value);
102
+
103
+ template <>
104
+ SampleMethod EnumSerializer::StringToEnum<SampleMethod>(const char *value);
105
+ template <>
106
+ const char *EnumSerializer::EnumToString<SampleMethod>(SampleMethod value);
107
+
108
+ template <>
109
+ LogicalTypeId EnumSerializer::StringToEnum<LogicalTypeId>(const char *value);
110
+ template <>
111
+ const char *EnumSerializer::EnumToString<LogicalTypeId>(LogicalTypeId value);
112
+
113
+ } // namespace duckdb
@@ -0,0 +1,336 @@
1
+ //===----------------------------------------------------------------------===//
2
+ // DuckDB
3
+ //
4
+ // duckdb/common/serializer/format_serializer.hpp
5
+ //
6
+ //
7
+ //===----------------------------------------------------------------------===//
8
+
9
+ #pragma once
10
+
11
+ #include "duckdb/common/field_writer.hpp"
12
+ #include "duckdb/common/serializer.hpp"
13
+ #include "duckdb/common/serializer/enum_serializer.hpp"
14
+ #include "duckdb/common/serializer/serialization_traits.hpp"
15
+ #include "duckdb/common/types/interval.hpp"
16
+ #include "duckdb/common/types/string_type.hpp"
17
+ #include "duckdb/common/unordered_map.hpp"
18
+ #include "duckdb/common/unordered_set.hpp"
19
+
20
+ namespace duckdb {
21
+
22
+ class FormatDeserializer {
23
+ protected:
24
+ bool deserialize_enum_from_string = false;
25
+
26
+ public:
27
+ // We fake return-type overloading using templates and enable_if
28
+ /*
29
+ template<typename T>
30
+ T ReadProperty(const char* tag) {
31
+ SetTag(tag);
32
+ return std::move<T>(Read<T>());
33
+ }
34
+ */
35
+
36
+ // Read into an existing value
37
+ template <typename T>
38
+ inline void ReadProperty(const char *tag, T &ret) {
39
+ SetTag(tag);
40
+ ret = Read<T>();
41
+ }
42
+
43
+ // Read and return a value
44
+ template <typename T>
45
+ inline T ReadProperty(const char *tag) {
46
+ SetTag(tag);
47
+ return Read<T>();
48
+ }
49
+
50
+ // Read optional property and return a value, or forward a default value
51
+ template <typename T>
52
+ inline T ReadOptionalPropertyOrDefault(const char *tag, T &&default_value) {
53
+ SetTag(tag);
54
+ auto present = OnOptionalBegin();
55
+ if (present) {
56
+ return Read<T>();
57
+ } else {
58
+ return std::forward<T>(default_value);
59
+ }
60
+ }
61
+
62
+ // Read optional property into an existing value, or use a default value
63
+ template <typename T>
64
+ inline void ReadOptionalPropertyOrDefault(const char *tag, T &ret, T &&default_value) {
65
+ SetTag(tag);
66
+ auto present = OnOptionalBegin();
67
+ if (present) {
68
+ ret = Read<T>();
69
+ } else {
70
+ ret = std::forward<T>(default_value);
71
+ }
72
+ }
73
+
74
+ // Read optional property and return a value, or default construct it
75
+ template <typename T>
76
+ inline typename std::enable_if<std::is_default_constructible<T>::value, T>::type
77
+ ReadOptionalProperty(const char *tag) {
78
+ SetTag(tag);
79
+ auto present = OnOptionalBegin();
80
+ if (present) {
81
+ return Read<T>();
82
+ } else {
83
+ return T();
84
+ }
85
+ }
86
+
87
+ // Read optional property into an existing value, or default construct it
88
+ template <typename T>
89
+ inline typename std::enable_if<std::is_default_constructible<T>::value, void>::type
90
+ ReadOptionalProperty(const char *tag, T &ret) {
91
+ SetTag(tag);
92
+ auto present = OnOptionalBegin();
93
+ if (present) {
94
+ ret = Read<T>();
95
+ } else {
96
+ ret = T();
97
+ }
98
+ }
99
+
100
+ private:
101
+ // Deserialize anything implementing a FormatDeserialize method
102
+ template <typename T = void>
103
+ inline typename std::enable_if<has_deserialize<T>::value, T>::type Read() {
104
+ return T::FormatDeserialize(*this);
105
+ }
106
+
107
+ // Structural Types
108
+ // Deserialize a unique_ptr
109
+ template <class T = void>
110
+ inline typename std::enable_if<is_unique_ptr<T>::value, T>::type Read() {
111
+ using ELEMENT_TYPE = typename is_unique_ptr<T>::ELEMENT_TYPE;
112
+ return std::move(ELEMENT_TYPE::FormatDeserialize(*this));
113
+ }
114
+
115
+ // Deserialize shared_ptr
116
+ template <typename T = void>
117
+ inline typename std::enable_if<is_shared_ptr<T>::value, T>::type Read() {
118
+ using ELEMENT_TYPE = typename is_shared_ptr<T>::ELEMENT_TYPE;
119
+ return std::move(ELEMENT_TYPE::FormatDeserialize(*this));
120
+ }
121
+
122
+ // Deserialize a vector
123
+ template <typename T = void>
124
+ inline typename std::enable_if<is_vector<T>::value, T>::type Read() {
125
+ using ELEMENT_TYPE = typename is_vector<T>::ELEMENT_TYPE;
126
+ T vec;
127
+ auto size = ReadUnsignedInt32();
128
+ for (idx_t i = 0; i < size; i++) {
129
+ vec.push_back(Read<ELEMENT_TYPE>());
130
+ }
131
+
132
+ return vec;
133
+ }
134
+
135
+ // Deserialize a map
136
+ template <typename T = void>
137
+ inline typename std::enable_if<is_unordered_map<T>::value, T>::type Read() {
138
+ using KEY_TYPE = typename is_unordered_map<T>::KEY_TYPE;
139
+ using VALUE_TYPE = typename is_unordered_map<T>::VALUE_TYPE;
140
+ auto size = ReadUnsignedInt32();
141
+ T map;
142
+ for (idx_t i = 0; i < size; i++) {
143
+ map[Read<KEY_TYPE>()] = Read<VALUE_TYPE>();
144
+ }
145
+
146
+ return map;
147
+ }
148
+
149
+ // Deserialize an unordered set
150
+ template <typename T = void>
151
+ inline typename std::enable_if<is_unordered_set<T>::value, T>::type Read() {
152
+ using ELEMENT_TYPE = typename is_unordered_set<T>::ELEMENT_TYPE;
153
+ auto size = ReadUnsignedInt32();
154
+ T set;
155
+ for (idx_t i = 0; i < size; i++) {
156
+ set.insert(Read<ELEMENT_TYPE>());
157
+ }
158
+
159
+ return set;
160
+ }
161
+
162
+ // Deserialize a set
163
+ template <typename T = void>
164
+ inline typename std::enable_if<is_set<T>::value, T>::type Read() {
165
+ using ELEMENT_TYPE = typename is_set<T>::ELEMENT_TYPE;
166
+ auto size = ReadUnsignedInt32();
167
+ T set;
168
+ for (idx_t i = 0; i < size; i++) {
169
+ set.insert(Read<ELEMENT_TYPE>());
170
+ }
171
+
172
+ return set;
173
+ }
174
+
175
+ // Deserialize a pair
176
+ template <typename T = void>
177
+ inline typename std::enable_if<is_pair<T>::value, T>::type Read() {
178
+ using FIRST_TYPE = typename is_pair<T>::FIRST_TYPE;
179
+ using SECOND_TYPE = typename is_pair<T>::SECOND_TYPE;
180
+
181
+ OnPairBegin();
182
+ OnPairKeyBegin();
183
+ FIRST_TYPE first = Read<FIRST_TYPE>();
184
+ OnPairKeyEnd();
185
+ OnPairValueBegin();
186
+ SECOND_TYPE second = Read<SECOND_TYPE>();
187
+ OnPairValueEnd();
188
+ OnPairEnd();
189
+ return std::make_pair(first, second);
190
+ }
191
+
192
+ // Primitive types
193
+ // Deserialize a bool
194
+ template <typename T = void>
195
+ inline typename std::enable_if<std::is_same<T, bool>::value, T>::type Read() {
196
+ return ReadBool();
197
+ }
198
+
199
+ // Deserialize a int8_t
200
+ template <typename T = void>
201
+ inline typename std::enable_if<std::is_same<T, int8_t>::value, T>::type Read() {
202
+ return ReadSignedInt8();
203
+ }
204
+
205
+ // Deserialize a uint8_t
206
+ template <typename T = void>
207
+ inline typename std::enable_if<std::is_same<T, uint8_t>::value, T>::type Read() {
208
+ return ReadUnsignedInt8();
209
+ }
210
+
211
+ // Deserialize a int16_t
212
+ template <typename T = void>
213
+ inline typename std::enable_if<std::is_same<T, int16_t>::value, T>::type Read() {
214
+ return ReadSignedInt16();
215
+ }
216
+
217
+ // Deserialize a uint16_t
218
+ template <typename T = void>
219
+ inline typename std::enable_if<std::is_same<T, uint16_t>::value, T>::type Read() {
220
+ return ReadUnsignedInt16();
221
+ }
222
+
223
+ // Deserialize a int32_t
224
+ template <typename T = void>
225
+ inline typename std::enable_if<std::is_same<T, int32_t>::value, T>::type Read() {
226
+ return ReadSignedInt32();
227
+ }
228
+
229
+ // Deserialize a uint32_t
230
+ template <typename T = void>
231
+ inline typename std::enable_if<std::is_same<T, uint32_t>::value, T>::type Read() {
232
+ return ReadUnsignedInt32();
233
+ }
234
+
235
+ // Deserialize a int64_t
236
+ template <typename T = void>
237
+ inline typename std::enable_if<std::is_same<T, int64_t>::value, T>::type Read() {
238
+ return ReadSignedInt64();
239
+ }
240
+
241
+ // Deserialize a uint64_t
242
+ template <typename T = void>
243
+ inline typename std::enable_if<std::is_same<T, uint64_t>::value, T>::type Read() {
244
+ return ReadUnsignedInt64();
245
+ }
246
+
247
+ // Deserialize a float
248
+ template <typename T = void>
249
+ inline typename std::enable_if<std::is_same<T, float>::value, T>::type Read() {
250
+ return ReadFloat();
251
+ }
252
+
253
+ // Deserialize a double
254
+ template <typename T = void>
255
+ inline typename std::enable_if<std::is_same<T, double>::value, T>::type Read() {
256
+ return ReadDouble();
257
+ }
258
+
259
+ // Deserialize a string
260
+ template <typename T = void>
261
+ inline typename std::enable_if<std::is_same<T, string>::value, T>::type Read() {
262
+ return ReadString();
263
+ }
264
+
265
+ // Deserialize a Enum
266
+ template <typename T = void>
267
+ inline typename std::enable_if<std::is_enum<T>::value, T>::type Read() {
268
+ auto str = ReadString();
269
+ return EnumSerializer::StringToEnum<T>(str.c_str());
270
+ }
271
+
272
+ // Deserialize a interval_t
273
+ template <typename T = void>
274
+ inline typename std::enable_if<std::is_same<T, interval_t>::value, T>::type Read() {
275
+ return ReadInterval();
276
+ }
277
+
278
+ protected:
279
+ virtual void SetTag(const char *tag) {
280
+ (void)tag;
281
+ }
282
+
283
+ virtual idx_t OnListBegin() = 0;
284
+ virtual void OnListEnd() {
285
+ }
286
+ virtual idx_t OnMapBegin() = 0;
287
+ virtual void OnMapEnd() {
288
+ }
289
+ virtual void OnMapEntryBegin() {
290
+ }
291
+ virtual void OnMapEntryEnd() {
292
+ }
293
+ virtual void OnMapKeyBegin() {
294
+ }
295
+ virtual void OnMapKeyEnd() {
296
+ }
297
+ virtual void OnMapValueBegin() {
298
+ }
299
+ virtual void OnMapValueEnd() {
300
+ }
301
+ virtual bool OnOptionalBegin() = 0;
302
+ virtual void OnOptionalEnd() {
303
+ }
304
+ virtual void OnObjectBegin() {
305
+ }
306
+ virtual void OnObjectEnd() {
307
+ }
308
+ virtual void OnPairBegin() {
309
+ }
310
+ virtual void OnPairKeyBegin() {
311
+ }
312
+ virtual void OnPairKeyEnd() {
313
+ }
314
+ virtual void OnPairValueBegin() {
315
+ }
316
+ virtual void OnPairValueEnd() {
317
+ }
318
+ virtual void OnPairEnd() {
319
+ }
320
+
321
+ virtual bool ReadBool() = 0;
322
+ virtual int8_t ReadSignedInt8() = 0;
323
+ virtual uint8_t ReadUnsignedInt8() = 0;
324
+ virtual int16_t ReadSignedInt16() = 0;
325
+ virtual uint16_t ReadUnsignedInt16() = 0;
326
+ virtual int32_t ReadSignedInt32() = 0;
327
+ virtual uint32_t ReadUnsignedInt32() = 0;
328
+ virtual int64_t ReadSignedInt64() = 0;
329
+ virtual uint64_t ReadUnsignedInt64() = 0;
330
+ virtual float ReadFloat() = 0;
331
+ virtual double ReadDouble() = 0;
332
+ virtual string ReadString() = 0;
333
+ virtual interval_t ReadInterval() = 0;
334
+ };
335
+
336
+ } // namespace duckdb
@@ -0,0 +1,268 @@
1
+ //===----------------------------------------------------------------------===//
2
+ // DuckDB
3
+ //
4
+ // duckdb/common/serializer/format_serializer.hpp
5
+ //
6
+ //
7
+ //===----------------------------------------------------------------------===//
8
+
9
+ #pragma once
10
+
11
+ #include "duckdb/common/field_writer.hpp"
12
+ #include "duckdb/common/serializer.hpp"
13
+ #include "duckdb/common/serializer/enum_serializer.hpp"
14
+ #include "duckdb/common/serializer/serialization_traits.hpp"
15
+ #include "duckdb/common/types/interval.hpp"
16
+ #include "duckdb/common/types/string_type.hpp"
17
+ #include "duckdb/common/unordered_map.hpp"
18
+ #include "duckdb/common/unordered_set.hpp"
19
+
20
+ namespace duckdb {
21
+
22
+ class FormatSerializer {
23
+ protected:
24
+ bool serialize_enum_as_string = false;
25
+
26
+ public:
27
+ // Serialize a value
28
+ template <class T>
29
+ typename std::enable_if<!std::is_enum<T>::value, void>::type WriteProperty(const char *tag, T &value) {
30
+ SetTag(tag);
31
+ WriteValue(value);
32
+ }
33
+
34
+ // Serialize a range
35
+ template <class T>
36
+ typename std::enable_if<std::is_pointer<T>::value, void>::type WriteProperty(const char *tag, const T start,
37
+ idx_t count) {
38
+ SetTag(tag);
39
+ WriteValue(start, count);
40
+ }
41
+
42
+ // Serialize an enum
43
+ template <class T>
44
+ typename std::enable_if<std::is_enum<T>::value, void>::type WriteProperty(const char *tag, T value) {
45
+ SetTag(tag);
46
+ if (serialize_enum_as_string) {
47
+ // Use the enum serializer to lookup tostring function
48
+ auto str = EnumSerializer::EnumToString(value);
49
+ WriteValue(str);
50
+ } else {
51
+ // Use the underlying type
52
+ WriteValue(static_cast<typename std::underlying_type<T>::type>(value));
53
+ }
54
+ }
55
+
56
+ // Optional pointer
57
+ template <class T>
58
+ void WriteOptionalProperty(const char *tag, T *ptr) {
59
+ SetTag(tag);
60
+ if (ptr == nullptr) {
61
+ OnOptionalBegin(false);
62
+ OnOptionalEnd(false);
63
+ } else {
64
+ OnOptionalBegin(true);
65
+ WriteValue(*ptr);
66
+ OnOptionalEnd(true);
67
+ }
68
+ }
69
+
70
+ // Optional unique_ptr
71
+ template <class T>
72
+ void WriteOptionalProperty(const char *tag, const unique_ptr<T> &ptr) {
73
+ SetTag(tag);
74
+ if (ptr == nullptr) {
75
+ OnOptionalBegin(false);
76
+ OnOptionalEnd(false);
77
+ } else {
78
+ OnOptionalBegin(true);
79
+ WriteValue(*ptr);
80
+ OnOptionalEnd(true);
81
+ }
82
+ }
83
+
84
+ protected:
85
+ // Unique Pointer Ref
86
+ template <typename T>
87
+ void WriteValue(const unique_ptr<T> &ptr) {
88
+ WriteValue(ptr.get());
89
+ }
90
+
91
+ // Pointer
92
+ template <typename T>
93
+ typename std::enable_if<std::is_pointer<T>::value, void>::type WriteValue(const T ptr) {
94
+ if (ptr == nullptr) {
95
+ WriteNull();
96
+ } else {
97
+ WriteValue(*ptr);
98
+ }
99
+ }
100
+
101
+ // data_ptr_t
102
+ void WriteValue(data_ptr_t ptr, idx_t count) {
103
+ OnListBegin(count);
104
+ auto end = ptr + count;
105
+ while (ptr != end) {
106
+ WriteValue(*ptr);
107
+ ptr++;
108
+ }
109
+ OnListEnd(count);
110
+ }
111
+
112
+ void WriteValue(const_data_ptr_t ptr, idx_t count) {
113
+ OnListBegin(count);
114
+ auto end = ptr + count;
115
+ while (ptr != end) {
116
+ WriteValue(*ptr);
117
+ ptr++;
118
+ }
119
+ OnListEnd(count);
120
+ }
121
+
122
+ // Pair
123
+ template <class K, class V>
124
+ void WriteValue(const std::pair<K, V> &pair) {
125
+ OnPairBegin();
126
+ OnPairKeyBegin();
127
+ WriteValue(pair.first);
128
+ OnPairKeyEnd();
129
+ OnPairValueBegin();
130
+ WriteValue(pair.second);
131
+ OnPairValueEnd();
132
+ OnPairEnd();
133
+ }
134
+
135
+ // Vector
136
+ template <class T>
137
+ void WriteValue(const vector<T> &vec) {
138
+ auto count = vec.size();
139
+ OnListBegin(count);
140
+ for (auto &item : vec) {
141
+ WriteValue(item);
142
+ }
143
+ OnListEnd(count);
144
+ }
145
+
146
+ // UnorderedSet
147
+ // Serialized the same way as a list/vector
148
+ template <class T, class HASH, class CMP>
149
+ void WriteValue(const unordered_set<T, HASH, CMP> &set) {
150
+ auto count = set.size();
151
+ OnListBegin(count);
152
+ for (auto &item : set) {
153
+ WriteValue(item);
154
+ }
155
+ OnListEnd(count);
156
+ }
157
+
158
+ // Set
159
+ // Serialized the same way as a list/vector
160
+ template <class T, class HASH, class CMP>
161
+ void WriteValue(const set<T, HASH, CMP> &set) {
162
+ auto count = set.size();
163
+ OnListBegin(count);
164
+ for (auto &item : set) {
165
+ WriteValue(item);
166
+ }
167
+ OnListEnd(count);
168
+ }
169
+
170
+ // Map
171
+ template <class K, class V, class HASH, class CMP>
172
+ void WriteValue(const std::unordered_map<K, V, HASH, CMP> &map) {
173
+ auto count = map.size();
174
+ OnMapBegin(count);
175
+ for (auto &item : map) {
176
+ OnMapEntryBegin();
177
+ OnMapKeyBegin();
178
+ WriteValue(item.first);
179
+ OnMapKeyEnd();
180
+ OnMapValueBegin();
181
+ WriteValue(item.second);
182
+ OnMapValueEnd();
183
+ OnMapEntryEnd();
184
+ }
185
+ OnMapEnd(count);
186
+ }
187
+
188
+ // class or struct implementing `FormatSerialize(FormatSerializer& FormatSerializer)`;
189
+ template <typename T>
190
+ typename std::enable_if<has_serialize_v<T>(), void>::type WriteValue(T &value) {
191
+ // Else, we defer to the .FormatSerialize method
192
+ OnObjectBegin();
193
+ value.FormatSerialize(*this);
194
+ OnObjectEnd();
195
+ }
196
+
197
+ // Handle setting a "tag" (optional)
198
+ virtual void SetTag(const char *tag) {
199
+ (void)tag;
200
+ }
201
+
202
+ // Hooks for subclasses to override to implement custom behavior
203
+ virtual void OnListBegin(idx_t count) {
204
+ (void)count;
205
+ }
206
+ virtual void OnListEnd(idx_t count) {
207
+ (void)count;
208
+ }
209
+ virtual void OnMapBegin(idx_t count) {
210
+ (void)count;
211
+ }
212
+ virtual void OnMapEnd(idx_t count) {
213
+ (void)count;
214
+ }
215
+ virtual void OnMapEntryBegin() {
216
+ }
217
+ virtual void OnMapEntryEnd() {
218
+ }
219
+ virtual void OnMapKeyBegin() {
220
+ }
221
+ virtual void OnMapKeyEnd() {
222
+ }
223
+ virtual void OnMapValueBegin() {
224
+ }
225
+ virtual void OnMapValueEnd() {
226
+ }
227
+ virtual void OnOptionalBegin(bool present) {
228
+ }
229
+ virtual void OnOptionalEnd(bool present) {
230
+ }
231
+ virtual void OnObjectBegin() {
232
+ }
233
+ virtual void OnObjectEnd() {
234
+ }
235
+ virtual void OnPairBegin() {
236
+ }
237
+ virtual void OnPairKeyBegin() {
238
+ }
239
+ virtual void OnPairKeyEnd() {
240
+ }
241
+ virtual void OnPairValueBegin() {
242
+ }
243
+ virtual void OnPairValueEnd() {
244
+ }
245
+ virtual void OnPairEnd() {
246
+ }
247
+
248
+ // Handle primitive types, a serializer needs to implement these.
249
+ virtual void WriteNull() = 0;
250
+ virtual void WriteValue(uint8_t value) = 0;
251
+ virtual void WriteValue(int8_t value) = 0;
252
+ virtual void WriteValue(uint16_t value) = 0;
253
+ virtual void WriteValue(int16_t value) = 0;
254
+ virtual void WriteValue(uint32_t value) = 0;
255
+ virtual void WriteValue(int32_t value) = 0;
256
+ virtual void WriteValue(uint64_t value) = 0;
257
+ virtual void WriteValue(int64_t value) = 0;
258
+ virtual void WriteValue(hugeint_t value) = 0;
259
+ virtual void WriteValue(float value) = 0;
260
+ virtual void WriteValue(double value) = 0;
261
+ virtual void WriteValue(interval_t value) = 0;
262
+ virtual void WriteValue(const string &value) = 0;
263
+ virtual void WriteValue(const string_t value) = 0;
264
+ virtual void WriteValue(const char *value) = 0;
265
+ virtual void WriteValue(bool value) = 0;
266
+ };
267
+
268
+ } // namespace duckdb