duckdb 0.7.2-dev654.0 → 0.7.2-dev832.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 (161) 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 +2 -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 +12 -4
  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/common/enums/expression_type.cpp +8 -222
  13. package/src/duckdb/src/common/enums/join_type.cpp +3 -22
  14. package/src/duckdb/src/common/exception.cpp +2 -2
  15. package/src/duckdb/src/common/serializer/enum_serializer.cpp +1172 -0
  16. package/src/duckdb/src/common/types/column_data_collection_segment.cpp +11 -6
  17. package/src/duckdb/src/common/types/value.cpp +117 -0
  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/common/vector_operations/vector_cast.cpp +2 -1
  21. package/src/duckdb/src/execution/aggregate_hashtable.cpp +10 -5
  22. package/src/duckdb/src/execution/expression_executor/execute_cast.cpp +2 -1
  23. package/src/duckdb/src/execution/index/art/art.cpp +5 -5
  24. package/src/duckdb/src/execution/operator/persistent/base_csv_reader.cpp +3 -0
  25. package/src/duckdb/src/execution/partitionable_hashtable.cpp +14 -2
  26. package/src/duckdb/src/function/cast/cast_function_set.cpp +1 -1
  27. package/src/duckdb/src/function/cast/enum_casts.cpp +25 -3
  28. package/src/duckdb/src/function/cast/list_casts.cpp +17 -4
  29. package/src/duckdb/src/function/cast/map_cast.cpp +5 -2
  30. package/src/duckdb/src/function/cast/string_cast.cpp +36 -10
  31. package/src/duckdb/src/function/cast/struct_cast.cpp +23 -3
  32. package/src/duckdb/src/function/cast/union_casts.cpp +33 -7
  33. package/src/duckdb/src/function/scalar/string/regexp/regexp_extract_all.cpp +243 -0
  34. package/src/duckdb/src/function/scalar/string/regexp/regexp_util.cpp +79 -0
  35. package/src/duckdb/src/function/scalar/string/regexp.cpp +21 -80
  36. package/src/duckdb/src/function/table/arrow_conversion.cpp +7 -1
  37. package/src/duckdb/src/function/table/table_scan.cpp +1 -1
  38. package/src/duckdb/src/function/table/version/pragma_version.cpp +2 -2
  39. package/src/duckdb/src/include/duckdb/common/enums/aggregate_handling.hpp +2 -0
  40. package/src/duckdb/src/include/duckdb/common/enums/expression_type.hpp +2 -3
  41. package/src/duckdb/src/include/duckdb/common/enums/joinref_type.hpp +2 -0
  42. package/src/duckdb/src/include/duckdb/common/enums/order_type.hpp +2 -0
  43. package/src/duckdb/src/include/duckdb/common/enums/set_operation_type.hpp +2 -1
  44. package/src/duckdb/src/include/duckdb/common/exception.hpp +40 -9
  45. package/src/duckdb/src/include/duckdb/common/optional_ptr.hpp +45 -0
  46. package/src/duckdb/src/include/duckdb/common/preserved_error.hpp +3 -0
  47. package/src/duckdb/src/include/duckdb/common/serializer/enum_serializer.hpp +113 -0
  48. package/src/duckdb/src/include/duckdb/common/serializer/format_deserializer.hpp +336 -0
  49. package/src/duckdb/src/include/duckdb/common/serializer/format_serializer.hpp +268 -0
  50. package/src/duckdb/src/include/duckdb/common/serializer/serialization_traits.hpp +126 -0
  51. package/src/duckdb/src/include/duckdb/common/string_util.hpp +12 -0
  52. package/src/duckdb/src/include/duckdb/common/types/value.hpp +2 -0
  53. package/src/duckdb/src/include/duckdb/common/types/vector.hpp +3 -0
  54. package/src/duckdb/src/include/duckdb/common/types.hpp +8 -2
  55. package/src/duckdb/src/include/duckdb/execution/aggregate_hashtable.hpp +1 -0
  56. package/src/duckdb/src/include/duckdb/execution/index/art/art.hpp +2 -2
  57. package/src/duckdb/src/include/duckdb/execution/partitionable_hashtable.hpp +3 -0
  58. package/src/duckdb/src/include/duckdb/function/cast/bound_cast_data.hpp +84 -0
  59. package/src/duckdb/src/include/duckdb/function/cast/cast_function_set.hpp +2 -2
  60. package/src/duckdb/src/include/duckdb/function/cast/default_casts.hpp +28 -64
  61. package/src/duckdb/src/include/duckdb/function/scalar/regexp.hpp +81 -1
  62. package/src/duckdb/src/include/duckdb/main/extension_entries.hpp +1 -0
  63. package/src/duckdb/src/include/duckdb/parser/common_table_expression_info.hpp +2 -0
  64. package/src/duckdb/src/include/duckdb/parser/expression/between_expression.hpp +3 -0
  65. package/src/duckdb/src/include/duckdb/parser/expression/bound_expression.hpp +2 -0
  66. package/src/duckdb/src/include/duckdb/parser/expression/case_expression.hpp +5 -0
  67. package/src/duckdb/src/include/duckdb/parser/expression/cast_expression.hpp +2 -0
  68. package/src/duckdb/src/include/duckdb/parser/expression/collate_expression.hpp +2 -0
  69. package/src/duckdb/src/include/duckdb/parser/expression/columnref_expression.hpp +2 -0
  70. package/src/duckdb/src/include/duckdb/parser/expression/comparison_expression.hpp +2 -0
  71. package/src/duckdb/src/include/duckdb/parser/expression/conjunction_expression.hpp +2 -0
  72. package/src/duckdb/src/include/duckdb/parser/expression/constant_expression.hpp +3 -0
  73. package/src/duckdb/src/include/duckdb/parser/expression/default_expression.hpp +1 -0
  74. package/src/duckdb/src/include/duckdb/parser/expression/function_expression.hpp +2 -0
  75. package/src/duckdb/src/include/duckdb/parser/expression/lambda_expression.hpp +2 -0
  76. package/src/duckdb/src/include/duckdb/parser/expression/operator_expression.hpp +2 -0
  77. package/src/duckdb/src/include/duckdb/parser/expression/parameter_expression.hpp +2 -0
  78. package/src/duckdb/src/include/duckdb/parser/expression/positional_reference_expression.hpp +2 -0
  79. package/src/duckdb/src/include/duckdb/parser/expression/star_expression.hpp +2 -0
  80. package/src/duckdb/src/include/duckdb/parser/expression/subquery_expression.hpp +2 -0
  81. package/src/duckdb/src/include/duckdb/parser/expression/window_expression.hpp +5 -0
  82. package/src/duckdb/src/include/duckdb/parser/parsed_data/sample_options.hpp +2 -0
  83. package/src/duckdb/src/include/duckdb/parser/parsed_expression.hpp +5 -0
  84. package/src/duckdb/src/include/duckdb/parser/query_node/recursive_cte_node.hpp +3 -0
  85. package/src/duckdb/src/include/duckdb/parser/query_node/select_node.hpp +5 -0
  86. package/src/duckdb/src/include/duckdb/parser/query_node/set_operation_node.hpp +3 -0
  87. package/src/duckdb/src/include/duckdb/parser/query_node.hpp +11 -1
  88. package/src/duckdb/src/include/duckdb/parser/result_modifier.hpp +24 -1
  89. package/src/duckdb/src/include/duckdb/parser/sql_statement.hpp +2 -1
  90. package/src/duckdb/src/include/duckdb/parser/statement/select_statement.hpp +6 -1
  91. package/src/duckdb/src/include/duckdb/parser/tableref/basetableref.hpp +4 -0
  92. package/src/duckdb/src/include/duckdb/parser/tableref/emptytableref.hpp +2 -0
  93. package/src/duckdb/src/include/duckdb/parser/tableref/expressionlistref.hpp +3 -0
  94. package/src/duckdb/src/include/duckdb/parser/tableref/joinref.hpp +3 -0
  95. package/src/duckdb/src/include/duckdb/parser/tableref/pivotref.hpp +9 -0
  96. package/src/duckdb/src/include/duckdb/parser/tableref/subqueryref.hpp +3 -0
  97. package/src/duckdb/src/include/duckdb/parser/tableref/table_function_ref.hpp +3 -0
  98. package/src/duckdb/src/include/duckdb/parser/tableref.hpp +3 -1
  99. package/src/duckdb/src/include/duckdb/storage/data_table.hpp +2 -2
  100. package/src/duckdb/src/include/duckdb/storage/index.hpp +4 -3
  101. package/src/duckdb/src/include/duckdb/transaction/local_storage.hpp +2 -2
  102. package/src/duckdb/src/main/extension/extension_install.cpp +7 -2
  103. package/src/duckdb/src/optimizer/deliminator.cpp +1 -1
  104. package/src/duckdb/src/optimizer/filter_combiner.cpp +1 -1
  105. package/src/duckdb/src/optimizer/join_order/join_order_optimizer.cpp +3 -3
  106. package/src/duckdb/src/optimizer/rule/move_constants.cpp +2 -2
  107. package/src/duckdb/src/optimizer/statistics/operator/propagate_filter.cpp +1 -1
  108. package/src/duckdb/src/parser/common_table_expression_info.cpp +19 -0
  109. package/src/duckdb/src/parser/expression/between_expression.cpp +17 -0
  110. package/src/duckdb/src/parser/expression/case_expression.cpp +28 -0
  111. package/src/duckdb/src/parser/expression/cast_expression.cpp +17 -0
  112. package/src/duckdb/src/parser/expression/collate_expression.cpp +16 -0
  113. package/src/duckdb/src/parser/expression/columnref_expression.cpp +15 -0
  114. package/src/duckdb/src/parser/expression/comparison_expression.cpp +16 -0
  115. package/src/duckdb/src/parser/expression/conjunction_expression.cpp +15 -0
  116. package/src/duckdb/src/parser/expression/constant_expression.cpp +14 -0
  117. package/src/duckdb/src/parser/expression/default_expression.cpp +7 -0
  118. package/src/duckdb/src/parser/expression/function_expression.cpp +35 -0
  119. package/src/duckdb/src/parser/expression/lambda_expression.cpp +16 -0
  120. package/src/duckdb/src/parser/expression/operator_expression.cpp +15 -0
  121. package/src/duckdb/src/parser/expression/parameter_expression.cpp +15 -0
  122. package/src/duckdb/src/parser/expression/positional_reference_expression.cpp +14 -0
  123. package/src/duckdb/src/parser/expression/star_expression.cpp +20 -0
  124. package/src/duckdb/src/parser/expression/subquery_expression.cpp +20 -0
  125. package/src/duckdb/src/parser/expression/window_expression.cpp +43 -0
  126. package/src/duckdb/src/parser/parsed_data/sample_options.cpp +22 -10
  127. package/src/duckdb/src/parser/parsed_expression.cpp +72 -0
  128. package/src/duckdb/src/parser/query_node/recursive_cte_node.cpp +21 -0
  129. package/src/duckdb/src/parser/query_node/select_node.cpp +31 -0
  130. package/src/duckdb/src/parser/query_node/set_operation_node.cpp +17 -0
  131. package/src/duckdb/src/parser/query_node.cpp +50 -0
  132. package/src/duckdb/src/parser/result_modifier.cpp +78 -0
  133. package/src/duckdb/src/parser/statement/select_statement.cpp +12 -0
  134. package/src/duckdb/src/parser/tableref/basetableref.cpp +21 -0
  135. package/src/duckdb/src/parser/tableref/emptytableref.cpp +4 -0
  136. package/src/duckdb/src/parser/tableref/expressionlistref.cpp +17 -0
  137. package/src/duckdb/src/parser/tableref/joinref.cpp +25 -0
  138. package/src/duckdb/src/parser/tableref/pivotref.cpp +53 -0
  139. package/src/duckdb/src/parser/tableref/subqueryref.cpp +15 -0
  140. package/src/duckdb/src/parser/tableref/table_function.cpp +17 -0
  141. package/src/duckdb/src/parser/tableref.cpp +46 -0
  142. package/src/duckdb/src/parser/transform/expression/transform_bool_expr.cpp +1 -1
  143. package/src/duckdb/src/parser/transform/expression/transform_function.cpp +1 -1
  144. package/src/duckdb/src/parser/transform/expression/transform_operator.cpp +1 -1
  145. package/src/duckdb/src/parser/transform/expression/transform_subquery.cpp +1 -1
  146. package/src/duckdb/src/planner/binder/expression/bind_subquery_expression.cpp +4 -0
  147. package/src/duckdb/src/planner/binder/statement/bind_copy.cpp +3 -1
  148. package/src/duckdb/src/planner/binder/tableref/plan_joinref.cpp +1 -1
  149. package/src/duckdb/src/planner/expression/bound_expression.cpp +4 -0
  150. package/src/duckdb/src/storage/data_table.cpp +15 -13
  151. package/src/duckdb/src/storage/index.cpp +12 -1
  152. package/src/duckdb/src/storage/local_storage.cpp +20 -23
  153. package/src/duckdb/src/verification/deserialized_statement_verifier.cpp +0 -1
  154. package/src/duckdb/third_party/re2/re2/re2.cc +9 -0
  155. package/src/duckdb/third_party/re2/re2/re2.h +2 -0
  156. package/src/duckdb/ub_extension_json_json_functions.cpp +2 -0
  157. package/src/duckdb/ub_src_common_serializer.cpp +2 -0
  158. package/src/duckdb/ub_src_function_scalar_string_regexp.cpp +4 -0
  159. package/src/duckdb/ub_src_parser.cpp +2 -0
  160. package/src/utils.cpp +12 -0
  161. package/test/extension.test.ts +44 -26
@@ -155,25 +155,30 @@ void GroupedAggregateHashTable::VerifyInternal() {
155
155
  D_ASSERT(count == entries);
156
156
  }
157
157
 
158
- idx_t GroupedAggregateHashTable::MaxCapacity() {
159
- idx_t max_pages = 0;
160
- idx_t max_tuples = 0;
158
+ idx_t GroupedAggregateHashTable::GetMaxCapacity(HtEntryType entry_type, idx_t tuple_size) {
159
+ idx_t max_pages;
160
+ idx_t max_tuples;
161
161
 
162
162
  switch (entry_type) {
163
163
  case HtEntryType::HT_WIDTH_32:
164
164
  max_pages = NumericLimits<uint8_t>::Maximum();
165
165
  max_tuples = NumericLimits<uint16_t>::Maximum();
166
166
  break;
167
- default:
168
- D_ASSERT(entry_type == HtEntryType::HT_WIDTH_64);
167
+ case HtEntryType::HT_WIDTH_64:
169
168
  max_pages = NumericLimits<uint32_t>::Maximum();
170
169
  max_tuples = NumericLimits<uint16_t>::Maximum();
171
170
  break;
171
+ default:
172
+ throw InternalException("Unsupported hash table width");
172
173
  }
173
174
 
174
175
  return max_pages * MinValue(max_tuples, (idx_t)Storage::BLOCK_SIZE / tuple_size);
175
176
  }
176
177
 
178
+ idx_t GroupedAggregateHashTable::MaxCapacity() {
179
+ return GetMaxCapacity(entry_type, tuple_size);
180
+ }
181
+
177
182
  void GroupedAggregateHashTable::Verify() {
178
183
  #ifdef DEBUG
179
184
  switch (entry_type) {
@@ -11,7 +11,8 @@ unique_ptr<ExpressionState> ExpressionExecutor::InitializeState(const BoundCastE
11
11
  result->AddChild(expr.child.get());
12
12
  result->Finalize();
13
13
  if (expr.bound_cast.init_local_state) {
14
- result->local_state = expr.bound_cast.init_local_state(root.executor->GetContext());
14
+ CastLocalStateParameters parameters(root.executor->GetContext(), expr.bound_cast.cast_data);
15
+ result->local_state = expr.bound_cast.init_local_state(parameters);
15
16
  }
16
17
  return std::move(result);
17
18
  }
@@ -330,8 +330,7 @@ bool ART::ConstructFromSorted(idx_t count, vector<Key> &keys, Vector &row_identi
330
330
  //===--------------------------------------------------------------------===//
331
331
  // Insert / Verification / Constraint Checking
332
332
  //===--------------------------------------------------------------------===//
333
-
334
- bool ART::Insert(IndexLock &lock, DataChunk &input, Vector &row_ids) {
333
+ PreservedError ART::Insert(IndexLock &lock, DataChunk &input, Vector &row_ids) {
335
334
 
336
335
  D_ASSERT(row_ids.GetType().InternalType() == ROW_TYPE);
337
336
  D_ASSERT(logical_types[0] == input.data[0].GetType());
@@ -375,12 +374,13 @@ bool ART::Insert(IndexLock &lock, DataChunk &input, Vector &row_ids) {
375
374
 
376
375
  IncreaseAndVerifyMemorySize(old_memory_size);
377
376
  if (failed_index != DConstants::INVALID_INDEX) {
378
- return false;
377
+ return PreservedError(ConstraintException("PRIMARY KEY or UNIQUE constraint violated: duplicate key \"%s\"",
378
+ AppendRowError(input, failed_index)));
379
379
  }
380
- return true;
380
+ return PreservedError();
381
381
  }
382
382
 
383
- bool ART::Append(IndexLock &lock, DataChunk &appended_data, Vector &row_identifiers) {
383
+ PreservedError ART::Append(IndexLock &lock, DataChunk &appended_data, Vector &row_identifiers) {
384
384
  DataChunk expression_result;
385
385
  expression_result.Initialize(Allocator::DefaultAllocator(), logical_types);
386
386
 
@@ -121,6 +121,9 @@ bool TryCastFloatingValueCommaSeparated(const string_t &value_str, const Logical
121
121
  }
122
122
 
123
123
  bool BaseCSVReader::TryCastValue(const Value &value, const LogicalType &sql_type) {
124
+ if (value.IsNull()) {
125
+ return true;
126
+ }
124
127
  if (options.has_format[LogicalTypeId::DATE] && sql_type.id() == LogicalTypeId::DATE) {
125
128
  date_t result;
126
129
  string error_message;
@@ -62,6 +62,18 @@ PartitionableHashTable::PartitionableHashTable(ClientContext &context, Allocator
62
62
  for (hash_t r = 0; r < partition_info.n_partitions; r++) {
63
63
  sel_vectors[r].Initialize();
64
64
  }
65
+
66
+ RowLayout layout;
67
+ layout.Initialize(group_types, AggregateObject::CreateAggregateObjects(bindings));
68
+ tuple_size = layout.GetRowWidth();
69
+ }
70
+
71
+ HtEntryType PartitionableHashTable::GetHTEntrySize() {
72
+ // we need at least STANDARD_VECTOR_SIZE entries to fit in the hash table
73
+ if (GroupedAggregateHashTable::GetMaxCapacity(HtEntryType::HT_WIDTH_32, tuple_size) < STANDARD_VECTOR_SIZE) {
74
+ return HtEntryType::HT_WIDTH_64;
75
+ }
76
+ return HtEntryType::HT_WIDTH_32;
65
77
  }
66
78
 
67
79
  idx_t PartitionableHashTable::ListAddChunk(HashTableList &list, DataChunk &groups, Vector &group_hashes,
@@ -74,7 +86,7 @@ idx_t PartitionableHashTable::ListAddChunk(HashTableList &list, DataChunk &group
74
86
  list.back()->Finalize();
75
87
  }
76
88
  list.push_back(make_unique<GroupedAggregateHashTable>(context, allocator, group_types, payload_types, bindings,
77
- HtEntryType::HT_WIDTH_32));
89
+ GetHTEntrySize()));
78
90
  }
79
91
  return list.back()->AddChunk(groups, group_hashes, payload, filter);
80
92
  }
@@ -141,7 +153,7 @@ void PartitionableHashTable::Partition() {
141
153
  for (auto &unpartitioned_ht : unpartitioned_hts) {
142
154
  for (idx_t r = 0; r < partition_info.n_partitions; r++) {
143
155
  radix_partitioned_hts[r].push_back(make_unique<GroupedAggregateHashTable>(
144
- context, allocator, group_types, payload_types, bindings, HtEntryType::HT_WIDTH_32));
156
+ context, allocator, group_types, payload_types, bindings, GetHTEntrySize()));
145
157
  partition_hts[r] = radix_partitioned_hts[r].back().get();
146
158
  }
147
159
  unpartitioned_ht->Partition(partition_hts, partition_info.radix_mask, partition_info.RADIX_SHIFT);
@@ -6,7 +6,7 @@
6
6
 
7
7
  namespace duckdb {
8
8
 
9
- BindCastInput::BindCastInput(CastFunctionSet &function_set, BindCastInfo *info, ClientContext *context)
9
+ BindCastInput::BindCastInput(CastFunctionSet &function_set, BindCastInfo *info, optional_ptr<ClientContext> context)
10
10
  : function_set(function_set), info(info), context(context) {
11
11
  }
12
12
 
@@ -109,17 +109,39 @@ unique_ptr<BoundCastData> BindEnumCast(BindCastInput &input, const LogicalType &
109
109
  return make_unique<EnumBoundCastData>(std::move(to_varchar_cast), std::move(from_varchar_cast));
110
110
  }
111
111
 
112
+ struct EnumCastLocalState : public FunctionLocalState {
113
+ public:
114
+ unique_ptr<FunctionLocalState> to_varchar_local;
115
+ unique_ptr<FunctionLocalState> from_varchar_local;
116
+ };
117
+
118
+ static unique_ptr<FunctionLocalState> InitEnumCastLocalState(CastLocalStateParameters &parameters) {
119
+ auto &cast_data = (EnumBoundCastData &)*parameters.cast_data;
120
+ auto result = make_unique<EnumCastLocalState>();
121
+
122
+ if (cast_data.from_varchar_cast.init_local_state) {
123
+ CastLocalStateParameters from_varchar_params(parameters, cast_data.from_varchar_cast.cast_data);
124
+ result->from_varchar_local = cast_data.from_varchar_cast.init_local_state(from_varchar_params);
125
+ }
126
+ if (cast_data.to_varchar_cast.init_local_state) {
127
+ CastLocalStateParameters from_varchar_params(parameters, cast_data.to_varchar_cast.cast_data);
128
+ result->from_varchar_local = cast_data.to_varchar_cast.init_local_state(from_varchar_params);
129
+ }
130
+ return std::move(result);
131
+ }
132
+
112
133
  static bool EnumToAnyCast(Vector &source, Vector &result, idx_t count, CastParameters &parameters) {
113
134
  auto &cast_data = (EnumBoundCastData &)*parameters.cast_data;
135
+ auto &lstate = (EnumCastLocalState &)*parameters.local_state;
114
136
 
115
137
  Vector varchar_cast(LogicalType::VARCHAR, count);
116
138
 
117
139
  // cast to varchar
118
- CastParameters to_varchar_params(parameters, cast_data.to_varchar_cast.cast_data.get());
140
+ CastParameters to_varchar_params(parameters, cast_data.to_varchar_cast.cast_data, lstate.to_varchar_local);
119
141
  cast_data.to_varchar_cast.function(source, varchar_cast, count, to_varchar_params);
120
142
 
121
143
  // cast from varchar to the target
122
- CastParameters from_varchar_params(parameters, cast_data.from_varchar_cast.cast_data.get());
144
+ CastParameters from_varchar_params(parameters, cast_data.from_varchar_cast.cast_data, lstate.from_varchar_local);
123
145
  cast_data.from_varchar_cast.function(varchar_cast, result, count, from_varchar_params);
124
146
  return true;
125
147
  }
@@ -152,7 +174,7 @@ BoundCastInfo DefaultCasts::EnumCastSwitch(BindCastInput &input, const LogicalTy
152
174
  throw InternalException("ENUM can only have unsigned integers (except UINT64) as physical types");
153
175
  }
154
176
  default: {
155
- return BoundCastInfo(EnumToAnyCast, BindEnumCast(input, source, target));
177
+ return BoundCastInfo(EnumToAnyCast, BindEnumCast(input, source, target), InitEnumCastLocalState);
156
178
  }
157
179
  }
158
180
  }
@@ -1,5 +1,6 @@
1
1
  #include "duckdb/function/cast/default_casts.hpp"
2
2
  #include "duckdb/function/cast/cast_function_set.hpp"
3
+ #include "duckdb/function/cast/bound_cast_data.hpp"
3
4
 
4
5
  namespace duckdb {
5
6
 
@@ -12,6 +13,15 @@ unique_ptr<BoundCastData> ListBoundCastData::BindListToListCast(BindCastInput &i
12
13
  return make_unique<ListBoundCastData>(std::move(child_cast));
13
14
  }
14
15
 
16
+ unique_ptr<FunctionLocalState> ListBoundCastData::InitListLocalState(CastLocalStateParameters &parameters) {
17
+ auto &cast_data = (ListBoundCastData &)*parameters.cast_data;
18
+ if (!cast_data.child_cast_info.init_local_state) {
19
+ return nullptr;
20
+ }
21
+ CastLocalStateParameters child_parameters(parameters, cast_data.child_cast_info.cast_data);
22
+ return cast_data.child_cast_info.init_local_state(child_parameters);
23
+ }
24
+
15
25
  bool ListCast::ListToListCast(Vector &source, Vector &result, idx_t count, CastParameters &parameters) {
16
26
  auto &cast_data = (ListBoundCastData &)*parameters.cast_data;
17
27
 
@@ -40,7 +50,7 @@ bool ListCast::ListToListCast(Vector &source, Vector &result, idx_t count, CastP
40
50
  ListVector::Reserve(result, source_size);
41
51
  auto &append_vector = ListVector::GetEntry(result);
42
52
 
43
- CastParameters child_parameters(parameters, cast_data.child_cast_info.cast_data.get());
53
+ CastParameters child_parameters(parameters, cast_data.child_cast_info.cast_data, parameters.local_state);
44
54
  if (!cast_data.child_cast_info.function(source_cc, append_vector, source_size, child_parameters)) {
45
55
  return false;
46
56
  }
@@ -116,10 +126,13 @@ static bool ListToVarcharCast(Vector &source, Vector &result, idx_t count, CastP
116
126
  BoundCastInfo DefaultCasts::ListCastSwitch(BindCastInput &input, const LogicalType &source, const LogicalType &target) {
117
127
  switch (target.id()) {
118
128
  case LogicalTypeId::LIST:
119
- return BoundCastInfo(ListCast::ListToListCast, ListBoundCastData::BindListToListCast(input, source, target));
129
+ return BoundCastInfo(ListCast::ListToListCast, ListBoundCastData::BindListToListCast(input, source, target),
130
+ ListBoundCastData::InitListLocalState);
120
131
  case LogicalTypeId::VARCHAR:
121
- return BoundCastInfo(ListToVarcharCast, ListBoundCastData::BindListToListCast(
122
- input, source, LogicalType::LIST(LogicalType::VARCHAR)));
132
+ return BoundCastInfo(
133
+ ListToVarcharCast,
134
+ ListBoundCastData::BindListToListCast(input, source, LogicalType::LIST(LogicalType::VARCHAR)),
135
+ ListBoundCastData::InitListLocalState);
123
136
  default:
124
137
  return DefaultCasts::TryVectorNullCast;
125
138
  }
@@ -1,5 +1,6 @@
1
1
  #include "duckdb/function/cast/default_casts.hpp"
2
2
  #include "duckdb/function/cast/cast_function_set.hpp"
3
+ #include "duckdb/function/cast/bound_cast_data.hpp"
3
4
 
4
5
  namespace duckdb {
5
6
 
@@ -78,10 +79,12 @@ static bool MapToVarcharCast(Vector &source, Vector &result, idx_t count, CastPa
78
79
  BoundCastInfo DefaultCasts::MapCastSwitch(BindCastInput &input, const LogicalType &source, const LogicalType &target) {
79
80
  switch (target.id()) {
80
81
  case LogicalTypeId::MAP:
81
- return BoundCastInfo(ListCast::ListToListCast, ListBoundCastData::BindListToListCast(input, source, target));
82
+ return BoundCastInfo(ListCast::ListToListCast, ListBoundCastData::BindListToListCast(input, source, target),
83
+ ListBoundCastData::InitListLocalState);
82
84
  case LogicalTypeId::VARCHAR: {
83
85
  auto varchar_type = LogicalType::MAP(LogicalType::VARCHAR, LogicalType::VARCHAR);
84
- return BoundCastInfo(MapToVarcharCast, ListBoundCastData::BindListToListCast(input, source, varchar_type));
86
+ return BoundCastInfo(MapToVarcharCast, ListBoundCastData::BindListToListCast(input, source, varchar_type),
87
+ ListBoundCastData::InitListLocalState);
85
88
  }
86
89
  default:
87
90
  return TryVectorNullCast;
@@ -3,6 +3,7 @@
3
3
  #include "duckdb/common/pair.hpp"
4
4
  #include "duckdb/common/vector.hpp"
5
5
  #include "duckdb/function/scalar/nested_functions.hpp"
6
+ #include "duckdb/function/cast/bound_cast_data.hpp"
6
7
 
7
8
  namespace duckdb {
8
9
 
@@ -115,7 +116,9 @@ static BoundCastInfo VectorStringCastNumericSwitch(BindCastInput &input, const L
115
116
  }
116
117
  }
117
118
 
119
+ //===--------------------------------------------------------------------===//
118
120
  // string -> list casting
121
+ //===--------------------------------------------------------------------===//
119
122
  bool VectorStringToList::StringToNestedTypeCastLoop(string_t *source_data, ValidityMask &source_mask, Vector &result,
120
123
  ValidityMask &result_mask, idx_t count, CastParameters &parameters,
121
124
  const SelectionVector *sel) {
@@ -163,7 +166,7 @@ bool VectorStringToList::StringToNestedTypeCastLoop(string_t *source_data, Valid
163
166
 
164
167
  auto &result_child = ListVector::GetEntry(result);
165
168
  auto &cast_data = (ListBoundCastData &)*parameters.cast_data;
166
- CastParameters child_parameters(parameters, cast_data.child_cast_info.cast_data.get());
169
+ CastParameters child_parameters(parameters, cast_data.child_cast_info.cast_data, parameters.local_state);
167
170
  return cast_data.child_cast_info.function(varchar_vector, result_child, total_list_size, child_parameters) &&
168
171
  all_converted;
169
172
  }
@@ -177,11 +180,12 @@ static LogicalType InitVarcharStructType(const LogicalType &target) {
177
180
  return LogicalType::STRUCT(child_types);
178
181
  }
179
182
 
183
+ //===--------------------------------------------------------------------===//
180
184
  // string -> struct casting
185
+ //===--------------------------------------------------------------------===//
181
186
  bool VectorStringToStruct::StringToNestedTypeCastLoop(string_t *source_data, ValidityMask &source_mask, Vector &result,
182
187
  ValidityMask &result_mask, idx_t count,
183
188
  CastParameters &parameters, const SelectionVector *sel) {
184
-
185
189
  auto varchar_struct_type = InitVarcharStructType(result.GetType());
186
190
  Vector varchar_vector(varchar_struct_type, count);
187
191
  auto &child_vectors = StructVector::GetEntries(varchar_vector);
@@ -216,21 +220,39 @@ bool VectorStringToStruct::StringToNestedTypeCastLoop(string_t *source_data, Val
216
220
  }
217
221
 
218
222
  auto &cast_data = (StructBoundCastData &)*parameters.cast_data;
223
+ auto &lstate = (StructCastLocalState &)*parameters.local_state;
219
224
  D_ASSERT(cast_data.child_cast_info.size() == result_children.size());
220
225
 
221
226
  for (idx_t child_idx = 0; child_idx < result_children.size(); child_idx++) {
222
- auto &varchar_vector = *child_vectors[child_idx];
227
+ auto &child_varchar_vector = *child_vectors[child_idx];
223
228
  auto &result_child_vector = *result_children[child_idx];
224
229
  auto &child_cast_info = cast_data.child_cast_info[child_idx];
225
- CastParameters child_parameters(parameters, child_cast_info.cast_data.get());
226
- if (!child_cast_info.function(varchar_vector, result_child_vector, count, child_parameters)) {
230
+ CastParameters child_parameters(parameters, child_cast_info.cast_data, lstate.local_states[child_idx]);
231
+ if (!child_cast_info.function(child_varchar_vector, result_child_vector, count, child_parameters)) {
227
232
  all_converted = false;
228
233
  }
229
234
  }
230
235
  return all_converted;
231
236
  }
232
237
 
238
+ //===--------------------------------------------------------------------===//
233
239
  // string -> map casting
240
+ //===--------------------------------------------------------------------===//
241
+ unique_ptr<FunctionLocalState> InitMapCastLocalState(CastLocalStateParameters &parameters) {
242
+ auto &cast_data = (MapBoundCastData &)*parameters.cast_data;
243
+ auto result = make_unique<MapCastLocalState>();
244
+
245
+ if (cast_data.key_cast.init_local_state) {
246
+ CastLocalStateParameters child_params(parameters, cast_data.key_cast.cast_data);
247
+ result->key_state = cast_data.key_cast.init_local_state(child_params);
248
+ }
249
+ if (cast_data.value_cast.init_local_state) {
250
+ CastLocalStateParameters child_params(parameters, cast_data.value_cast.cast_data);
251
+ result->value_state = cast_data.value_cast.init_local_state(child_params);
252
+ }
253
+ return std::move(result);
254
+ }
255
+
234
256
  bool VectorStringToMap::StringToNestedTypeCastLoop(string_t *source_data, ValidityMask &source_mask, Vector &result,
235
257
  ValidityMask &result_mask, idx_t count, CastParameters &parameters,
236
258
  const SelectionVector *sel) {
@@ -282,12 +304,13 @@ bool VectorStringToMap::StringToNestedTypeCastLoop(string_t *source_data, Validi
282
304
  auto &result_key_child = MapVector::GetKeys(result);
283
305
  auto &result_val_child = MapVector::GetValues(result);
284
306
  auto &cast_data = (MapBoundCastData &)*parameters.cast_data;
307
+ auto &lstate = (MapCastLocalState &)*parameters.local_state;
285
308
 
286
- CastParameters key_params(parameters, cast_data.key_cast.cast_data.get());
309
+ CastParameters key_params(parameters, cast_data.key_cast.cast_data, lstate.key_state);
287
310
  if (!cast_data.key_cast.function(varchar_key_vector, result_key_child, total_elements, key_params)) {
288
311
  all_converted = false;
289
312
  }
290
- CastParameters val_params(parameters, cast_data.value_cast.cast_data.get());
313
+ CastParameters val_params(parameters, cast_data.value_cast.cast_data, lstate.value_state);
291
314
  if (!cast_data.value_cast.function(varchar_val_vector, result_val_child, total_elements, val_params)) {
292
315
  all_converted = false;
293
316
  }
@@ -373,14 +396,17 @@ BoundCastInfo DefaultCasts::StringCastSwitch(BindCastInput &input, const Logical
373
396
  // the second argument allows for a secondary casting function to be passed in the CastParameters
374
397
  return BoundCastInfo(
375
398
  &StringToNestedTypeCast<VectorStringToList>,
376
- ListBoundCastData::BindListToListCast(input, LogicalType::LIST(LogicalType::VARCHAR), target));
399
+ ListBoundCastData::BindListToListCast(input, LogicalType::LIST(LogicalType::VARCHAR), target),
400
+ ListBoundCastData::InitListLocalState);
377
401
  case LogicalTypeId::STRUCT:
378
402
  return BoundCastInfo(&StringToNestedTypeCast<VectorStringToStruct>,
379
- StructBoundCastData::BindStructToStructCast(input, InitVarcharStructType(target), target));
403
+ StructBoundCastData::BindStructToStructCast(input, InitVarcharStructType(target), target),
404
+ StructBoundCastData::InitStructCastLocalState);
380
405
  case LogicalTypeId::MAP:
381
406
  return BoundCastInfo(&StringToNestedTypeCast<VectorStringToMap>,
382
407
  MapBoundCastData::BindMapToMapCast(
383
- input, LogicalType::MAP(LogicalType::VARCHAR, LogicalType::VARCHAR), target));
408
+ input, LogicalType::MAP(LogicalType::VARCHAR, LogicalType::VARCHAR), target),
409
+ InitMapCastLocalState);
384
410
  default:
385
411
  return VectorStringCastNumericSwitch(input, source, target);
386
412
  }
@@ -1,5 +1,6 @@
1
1
  #include "duckdb/function/cast/default_casts.hpp"
2
2
  #include "duckdb/function/cast/cast_function_set.hpp"
3
+ #include "duckdb/function/cast/bound_cast_data.hpp"
3
4
 
4
5
  namespace duckdb {
5
6
 
@@ -18,8 +19,24 @@ unique_ptr<BoundCastData> StructBoundCastData::BindStructToStructCast(BindCastIn
18
19
  return make_unique<StructBoundCastData>(std::move(child_cast_info), target);
19
20
  }
20
21
 
22
+ unique_ptr<FunctionLocalState> StructBoundCastData::InitStructCastLocalState(CastLocalStateParameters &parameters) {
23
+ auto &cast_data = (StructBoundCastData &)*parameters.cast_data;
24
+ auto result = make_unique<StructCastLocalState>();
25
+
26
+ for (auto &entry : cast_data.child_cast_info) {
27
+ unique_ptr<FunctionLocalState> child_state;
28
+ if (entry.init_local_state) {
29
+ CastLocalStateParameters child_params(parameters, entry.cast_data);
30
+ child_state = entry.init_local_state(child_params);
31
+ }
32
+ result->local_states.push_back(std::move(child_state));
33
+ }
34
+ return std::move(result);
35
+ }
36
+
21
37
  static bool StructToStructCast(Vector &source, Vector &result, idx_t count, CastParameters &parameters) {
22
38
  auto &cast_data = (StructBoundCastData &)*parameters.cast_data;
39
+ auto &lstate = (StructCastLocalState &)*parameters.local_state;
23
40
  auto &source_child_types = StructType::GetChildTypes(source.GetType());
24
41
  auto &source_children = StructVector::GetEntries(source);
25
42
  D_ASSERT(source_children.size() == StructType::GetChildTypes(result.GetType()).size());
@@ -29,7 +46,8 @@ static bool StructToStructCast(Vector &source, Vector &result, idx_t count, Cast
29
46
  for (idx_t c_idx = 0; c_idx < source_child_types.size(); c_idx++) {
30
47
  auto &result_child_vector = *result_children[c_idx];
31
48
  auto &source_child_vector = *source_children[c_idx];
32
- CastParameters child_parameters(parameters, cast_data.child_cast_info[c_idx].cast_data.get());
49
+ CastParameters child_parameters(parameters, cast_data.child_cast_info[c_idx].cast_data,
50
+ lstate.local_states[c_idx]);
33
51
  if (!cast_data.child_cast_info[c_idx].function(source_child_vector, result_child_vector, count,
34
52
  child_parameters)) {
35
53
  all_converted = false;
@@ -121,7 +139,8 @@ BoundCastInfo DefaultCasts::StructCastSwitch(BindCastInput &input, const Logical
121
139
  const LogicalType &target) {
122
140
  switch (target.id()) {
123
141
  case LogicalTypeId::STRUCT:
124
- return BoundCastInfo(StructToStructCast, StructBoundCastData::BindStructToStructCast(input, source, target));
142
+ return BoundCastInfo(StructToStructCast, StructBoundCastData::BindStructToStructCast(input, source, target),
143
+ StructBoundCastData::InitStructCastLocalState);
125
144
  case LogicalTypeId::VARCHAR: {
126
145
  // bind a cast in which we convert all child entries to VARCHAR entries
127
146
  auto &struct_children = StructType::GetChildTypes(source);
@@ -131,7 +150,8 @@ BoundCastInfo DefaultCasts::StructCastSwitch(BindCastInput &input, const Logical
131
150
  }
132
151
  auto varchar_type = LogicalType::STRUCT(varchar_children);
133
152
  return BoundCastInfo(StructToVarcharCast,
134
- StructBoundCastData::BindStructToStructCast(input, source, varchar_type));
153
+ StructBoundCastData::BindStructToStructCast(input, source, varchar_type),
154
+ StructBoundCastData::InitStructCastLocalState);
135
155
  }
136
156
  default:
137
157
  return TryVectorNullCast;
@@ -1,6 +1,6 @@
1
1
  #include "duckdb/function/cast/cast_function_set.hpp"
2
2
  #include "duckdb/function/cast/default_casts.hpp"
3
- #include "duckdb/function/cast/vector_cast_helpers.hpp"
3
+ #include "duckdb/function/cast/bound_cast_data.hpp"
4
4
 
5
5
  #include <algorithm> // for std::sort
6
6
 
@@ -98,12 +98,21 @@ unique_ptr<BoundCastData> BindToUnionCast(BindCastInput &input, const LogicalTyp
98
98
  return make_unique<ToUnionBoundCastData>(std::move(selected_cast));
99
99
  }
100
100
 
101
+ unique_ptr<FunctionLocalState> InitToUnionLocalState(CastLocalStateParameters &parameters) {
102
+ auto &cast_data = (ToUnionBoundCastData &)*parameters.cast_data;
103
+ if (!cast_data.member_cast_info.init_local_state) {
104
+ return nullptr;
105
+ }
106
+ CastLocalStateParameters child_parameters(parameters, cast_data.member_cast_info.cast_data);
107
+ return cast_data.member_cast_info.init_local_state(child_parameters);
108
+ }
109
+
101
110
  static bool ToUnionCast(Vector &source, Vector &result, idx_t count, CastParameters &parameters) {
102
111
  D_ASSERT(result.GetType().id() == LogicalTypeId::UNION);
103
112
  auto &cast_data = (ToUnionBoundCastData &)*parameters.cast_data;
104
113
  auto &selected_member_vector = UnionVector::GetMember(result, cast_data.tag);
105
114
 
106
- CastParameters child_parameters(parameters, cast_data.member_cast_info.cast_data.get());
115
+ CastParameters child_parameters(parameters, cast_data.member_cast_info.cast_data, parameters.local_state);
107
116
  if (!cast_data.member_cast_info.function(source, selected_member_vector, count, child_parameters)) {
108
117
  return false;
109
118
  }
@@ -118,7 +127,7 @@ static bool ToUnionCast(Vector &source, Vector &result, idx_t count, CastParamet
118
127
 
119
128
  BoundCastInfo DefaultCasts::ImplicitToUnionCast(BindCastInput &input, const LogicalType &source,
120
129
  const LogicalType &target) {
121
- return BoundCastInfo(&ToUnionCast, BindToUnionCast(input, source, target));
130
+ return BoundCastInfo(&ToUnionCast, BindToUnionCast(input, source, target), InitToUnionLocalState);
122
131
  }
123
132
 
124
133
  //--------------------------------------------------------------------------------------------------
@@ -197,8 +206,24 @@ unique_ptr<BoundCastData> BindUnionToUnionCast(BindCastInput &input, const Logic
197
206
  return make_unique<UnionToUnionBoundCastData>(tag_map, std::move(member_casts), target);
198
207
  }
199
208
 
209
+ unique_ptr<FunctionLocalState> InitUnionToUnionLocalState(CastLocalStateParameters &parameters) {
210
+ auto &cast_data = (UnionToUnionBoundCastData &)*parameters.cast_data;
211
+ auto result = make_unique<StructCastLocalState>();
212
+
213
+ for (auto &entry : cast_data.member_casts) {
214
+ unique_ptr<FunctionLocalState> child_state;
215
+ if (entry.init_local_state) {
216
+ CastLocalStateParameters child_params(parameters, entry.cast_data);
217
+ child_state = entry.init_local_state(child_params);
218
+ }
219
+ result->local_states.push_back(std::move(child_state));
220
+ }
221
+ return std::move(result);
222
+ }
223
+
200
224
  static bool UnionToUnionCast(Vector &source, Vector &result, idx_t count, CastParameters &parameters) {
201
225
  auto &cast_data = (UnionToUnionBoundCastData &)*parameters.cast_data;
226
+ auto &lstate = (StructCastLocalState &)*parameters.local_state;
202
227
 
203
228
  auto source_member_count = UnionType::GetMemberCount(source.GetType());
204
229
  auto target_member_count = UnionType::GetMemberCount(result.GetType());
@@ -213,7 +238,7 @@ static bool UnionToUnionCast(Vector &source, Vector &result, idx_t count, CastPa
213
238
  auto &target_member_vector = UnionVector::GetMember(result, target_member_idx);
214
239
  auto &member_cast = cast_data.member_casts[member_idx];
215
240
 
216
- CastParameters child_parameters(parameters, member_cast.cast_data.get());
241
+ CastParameters child_parameters(parameters, member_cast.cast_data, lstate.local_states[member_idx]);
217
242
  if (!member_cast.function(source_member_vector, target_member_vector, count, child_parameters)) {
218
243
  return false;
219
244
  }
@@ -339,10 +364,11 @@ BoundCastInfo DefaultCasts::UnionCastSwitch(BindCastInput &input, const LogicalT
339
364
  varchar_members.push_back(make_pair(UnionType::GetMemberName(source, member_idx), LogicalType::VARCHAR));
340
365
  }
341
366
  auto varchar_type = LogicalType::UNION(std::move(varchar_members));
342
- return BoundCastInfo(UnionToVarcharCast, BindUnionToUnionCast(input, source, varchar_type));
343
- } break;
367
+ return BoundCastInfo(UnionToVarcharCast, BindUnionToUnionCast(input, source, varchar_type),
368
+ InitUnionToUnionLocalState);
369
+ }
344
370
  case LogicalTypeId::UNION:
345
- return BoundCastInfo(UnionToUnionCast, BindUnionToUnionCast(input, source, target));
371
+ return BoundCastInfo(UnionToUnionCast, BindUnionToUnionCast(input, source, target), InitUnionToUnionLocalState);
346
372
  default:
347
373
  return TryVectorNullCast;
348
374
  }