duckdb 0.7.2-dev16.0 → 0.7.2-dev314.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 (242) hide show
  1. package/binding.gyp +2 -0
  2. package/package.json +1 -1
  3. package/src/duckdb/extension/icu/icu-extension.cpp +2 -0
  4. package/src/duckdb/extension/icu/icu-table-range.cpp +194 -0
  5. package/src/duckdb/extension/icu/include/icu-table-range.hpp +17 -0
  6. package/src/duckdb/extension/parquet/column_reader.cpp +5 -6
  7. package/src/duckdb/extension/parquet/column_writer.cpp +0 -1
  8. package/src/duckdb/extension/parquet/include/column_reader.hpp +1 -2
  9. package/src/duckdb/extension/parquet/include/generated_column_reader.hpp +1 -11
  10. package/src/duckdb/extension/parquet/parquet-extension.cpp +11 -2
  11. package/src/duckdb/extension/parquet/parquet_statistics.cpp +26 -32
  12. package/src/duckdb/src/catalog/catalog_entry/duck_schema_entry.cpp +4 -0
  13. package/src/duckdb/src/catalog/catalog_entry/scalar_function_catalog_entry.cpp +7 -6
  14. package/src/duckdb/src/catalog/catalog_entry/table_function_catalog_entry.cpp +20 -1
  15. package/src/duckdb/src/common/enums/statement_type.cpp +2 -0
  16. package/src/duckdb/src/common/sort/sort_state.cpp +5 -7
  17. package/src/duckdb/src/common/types/bit.cpp +95 -58
  18. package/src/duckdb/src/common/types/value.cpp +149 -53
  19. package/src/duckdb/src/common/types/vector.cpp +13 -10
  20. package/src/duckdb/src/execution/column_binding_resolver.cpp +6 -0
  21. package/src/duckdb/src/execution/operator/aggregate/physical_perfecthash_aggregate.cpp +4 -5
  22. package/src/duckdb/src/execution/operator/aggregate/physical_window.cpp +1 -1
  23. package/src/duckdb/src/execution/operator/helper/physical_vacuum.cpp +2 -3
  24. package/src/duckdb/src/execution/operator/join/physical_blockwise_nl_join.cpp +32 -6
  25. package/src/duckdb/src/execution/operator/persistent/buffered_csv_reader.cpp +1 -1
  26. package/src/duckdb/src/execution/physical_plan/plan_aggregate.cpp +15 -15
  27. package/src/duckdb/src/execution/physical_plan/plan_comparison_join.cpp +18 -12
  28. package/src/duckdb/src/function/aggregate/algebraic/avg.cpp +0 -6
  29. package/src/duckdb/src/function/aggregate/distributive/bitagg.cpp +99 -95
  30. package/src/duckdb/src/function/aggregate/distributive/bitstring_agg.cpp +254 -0
  31. package/src/duckdb/src/function/aggregate/distributive/count.cpp +2 -4
  32. package/src/duckdb/src/function/aggregate/distributive/sum.cpp +11 -16
  33. package/src/duckdb/src/function/aggregate/distributive_functions.cpp +1 -0
  34. package/src/duckdb/src/function/aggregate/sorted_aggregate_function.cpp +16 -5
  35. package/src/duckdb/src/function/cast/bit_cast.cpp +0 -2
  36. package/src/duckdb/src/function/cast/blob_cast.cpp +0 -1
  37. package/src/duckdb/src/function/scalar/bit/bitstring.cpp +99 -0
  38. package/src/duckdb/src/function/scalar/date/date_diff.cpp +0 -1
  39. package/src/duckdb/src/function/scalar/date/date_part.cpp +17 -25
  40. package/src/duckdb/src/function/scalar/date/date_sub.cpp +0 -1
  41. package/src/duckdb/src/function/scalar/date/date_trunc.cpp +10 -14
  42. package/src/duckdb/src/function/scalar/generic/stats.cpp +2 -4
  43. package/src/duckdb/src/function/scalar/list/flatten.cpp +5 -12
  44. package/src/duckdb/src/function/scalar/list/list_concat.cpp +3 -8
  45. package/src/duckdb/src/function/scalar/list/list_extract.cpp +5 -12
  46. package/src/duckdb/src/function/scalar/list/list_value.cpp +5 -9
  47. package/src/duckdb/src/function/scalar/map/map_entries.cpp +61 -0
  48. package/src/duckdb/src/function/scalar/map/map_keys_values.cpp +97 -0
  49. package/src/duckdb/src/function/scalar/math/numeric.cpp +14 -17
  50. package/src/duckdb/src/function/scalar/nested_functions.cpp +3 -0
  51. package/src/duckdb/src/function/scalar/operators/add.cpp +0 -9
  52. package/src/duckdb/src/function/scalar/operators/arithmetic.cpp +29 -48
  53. package/src/duckdb/src/function/scalar/operators/bitwise.cpp +0 -63
  54. package/src/duckdb/src/function/scalar/operators/multiply.cpp +0 -6
  55. package/src/duckdb/src/function/scalar/operators/subtract.cpp +0 -6
  56. package/src/duckdb/src/function/scalar/string/caseconvert.cpp +2 -6
  57. package/src/duckdb/src/function/scalar/string/instr.cpp +2 -6
  58. package/src/duckdb/src/function/scalar/string/length.cpp +2 -6
  59. package/src/duckdb/src/function/scalar/string/like.cpp +2 -6
  60. package/src/duckdb/src/function/scalar/string/substring.cpp +2 -6
  61. package/src/duckdb/src/function/scalar/string_functions.cpp +1 -0
  62. package/src/duckdb/src/function/scalar/struct/struct_extract.cpp +4 -9
  63. package/src/duckdb/src/function/scalar/struct/struct_insert.cpp +10 -13
  64. package/src/duckdb/src/function/scalar/struct/struct_pack.cpp +5 -6
  65. package/src/duckdb/src/function/table/read_csv.cpp +9 -0
  66. package/src/duckdb/src/function/table/version/pragma_version.cpp +2 -2
  67. package/src/duckdb/src/function/table_function.cpp +19 -0
  68. package/src/duckdb/src/include/duckdb/catalog/catalog_entry/table_function_catalog_entry.hpp +6 -8
  69. package/src/duckdb/src/include/duckdb/common/constants.hpp +0 -19
  70. package/src/duckdb/src/include/duckdb/common/enums/statement_type.hpp +2 -1
  71. package/src/duckdb/src/include/duckdb/common/enums/tableref_type.hpp +2 -1
  72. package/src/duckdb/src/include/duckdb/common/types/bit.hpp +5 -1
  73. package/src/duckdb/src/include/duckdb/common/types/value.hpp +2 -8
  74. package/src/duckdb/src/include/duckdb/common/types.hpp +1 -2
  75. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/physical_perfecthash_aggregate.hpp +1 -1
  76. package/src/duckdb/src/include/duckdb/function/aggregate/distributive_functions.hpp +5 -0
  77. package/src/duckdb/src/include/duckdb/function/aggregate_function.hpp +12 -3
  78. package/src/duckdb/src/include/duckdb/function/scalar/bit_functions.hpp +4 -0
  79. package/src/duckdb/src/include/duckdb/function/scalar/nested_functions.hpp +12 -0
  80. package/src/duckdb/src/include/duckdb/function/scalar_function.hpp +2 -2
  81. package/src/duckdb/src/include/duckdb/function/table_function.hpp +2 -0
  82. package/src/duckdb/src/include/duckdb/main/capi/capi_internal.hpp +2 -0
  83. package/src/duckdb/src/include/duckdb/main/config.hpp +3 -0
  84. package/src/duckdb/src/include/duckdb/main/database.hpp +1 -0
  85. package/src/duckdb/src/include/duckdb/optimizer/join_order/cardinality_estimator.hpp +2 -2
  86. package/src/duckdb/src/include/duckdb/parser/common_table_expression_info.hpp +2 -0
  87. package/src/duckdb/src/include/duckdb/parser/parsed_data/alter_info.hpp +2 -1
  88. package/src/duckdb/src/include/duckdb/parser/parsed_data/{alter_function_info.hpp → alter_scalar_function_info.hpp} +13 -13
  89. package/src/duckdb/src/include/duckdb/parser/parsed_data/alter_table_function_info.hpp +47 -0
  90. package/src/duckdb/src/include/duckdb/parser/parsed_data/create_table_function_info.hpp +2 -1
  91. package/src/duckdb/src/include/duckdb/parser/query_node.hpp +2 -1
  92. package/src/duckdb/src/include/duckdb/parser/statement/multi_statement.hpp +28 -0
  93. package/src/duckdb/src/include/duckdb/parser/tableref/list.hpp +1 -0
  94. package/src/duckdb/src/include/duckdb/parser/tableref/pivotref.hpp +76 -0
  95. package/src/duckdb/src/include/duckdb/parser/tokens.hpp +2 -0
  96. package/src/duckdb/src/include/duckdb/parser/transformer.hpp +28 -0
  97. package/src/duckdb/src/include/duckdb/planner/bind_context.hpp +2 -0
  98. package/src/duckdb/src/include/duckdb/planner/binder.hpp +8 -0
  99. package/src/duckdb/src/include/duckdb/storage/buffer/block_handle.hpp +2 -0
  100. package/src/duckdb/src/include/duckdb/storage/buffer_manager.hpp +76 -44
  101. package/src/duckdb/src/include/duckdb/storage/checkpoint/table_data_writer.hpp +3 -2
  102. package/src/duckdb/src/include/duckdb/storage/compression/chimp/chimp_compress.hpp +2 -2
  103. package/src/duckdb/src/include/duckdb/storage/compression/chimp/chimp_fetch.hpp +1 -1
  104. package/src/duckdb/src/include/duckdb/storage/compression/chimp/chimp_scan.hpp +1 -1
  105. package/src/duckdb/src/include/duckdb/storage/compression/patas/patas_compress.hpp +2 -2
  106. package/src/duckdb/src/include/duckdb/storage/compression/patas/patas_fetch.hpp +1 -1
  107. package/src/duckdb/src/include/duckdb/storage/compression/patas/patas_scan.hpp +1 -1
  108. package/src/duckdb/src/include/duckdb/storage/data_pointer.hpp +5 -2
  109. package/src/duckdb/src/include/duckdb/storage/data_table.hpp +1 -1
  110. package/src/duckdb/src/include/duckdb/storage/statistics/base_statistics.hpp +93 -29
  111. package/src/duckdb/src/include/duckdb/storage/statistics/column_statistics.hpp +22 -3
  112. package/src/duckdb/src/include/duckdb/storage/statistics/distinct_statistics.hpp +6 -6
  113. package/src/duckdb/src/include/duckdb/storage/statistics/list_stats.hpp +41 -0
  114. package/src/duckdb/src/include/duckdb/storage/statistics/node_statistics.hpp +26 -0
  115. package/src/duckdb/src/include/duckdb/storage/statistics/numeric_stats.hpp +157 -0
  116. package/src/duckdb/src/include/duckdb/storage/statistics/segment_statistics.hpp +2 -7
  117. package/src/duckdb/src/include/duckdb/storage/statistics/string_stats.hpp +74 -0
  118. package/src/duckdb/src/include/duckdb/storage/statistics/struct_stats.hpp +42 -0
  119. package/src/duckdb/src/include/duckdb/storage/string_uncompressed.hpp +2 -3
  120. package/src/duckdb/src/include/duckdb/storage/table/column_segment.hpp +2 -2
  121. package/src/duckdb/src/include/duckdb/storage/table/list_column_data.hpp +1 -1
  122. package/src/duckdb/src/include/duckdb/storage/table/persistent_table_data.hpp +2 -1
  123. package/src/duckdb/src/include/duckdb/storage/table/row_group.hpp +4 -3
  124. package/src/duckdb/src/include/duckdb/storage/table/row_group_collection.hpp +3 -2
  125. package/src/duckdb/src/include/duckdb/storage/table/scan_state.hpp +2 -0
  126. package/src/duckdb/src/include/duckdb/storage/table/table_statistics.hpp +5 -0
  127. package/src/duckdb/src/include/duckdb.h +49 -1
  128. package/src/duckdb/src/include/duckdb.hpp +0 -1
  129. package/src/duckdb/src/main/capi/pending-c.cpp +16 -3
  130. package/src/duckdb/src/main/capi/result-c.cpp +27 -1
  131. package/src/duckdb/src/main/capi/stream-c.cpp +25 -0
  132. package/src/duckdb/src/main/client_context.cpp +8 -1
  133. package/src/duckdb/src/main/config.cpp +66 -1
  134. package/src/duckdb/src/main/database.cpp +10 -2
  135. package/src/duckdb/src/optimizer/join_order/cardinality_estimator.cpp +98 -67
  136. package/src/duckdb/src/optimizer/join_order/join_order_optimizer.cpp +16 -3
  137. package/src/duckdb/src/optimizer/statistics/expression/propagate_aggregate.cpp +9 -3
  138. package/src/duckdb/src/optimizer/statistics/expression/propagate_and_compress.cpp +6 -7
  139. package/src/duckdb/src/optimizer/statistics/expression/propagate_cast.cpp +14 -11
  140. package/src/duckdb/src/optimizer/statistics/expression/propagate_columnref.cpp +1 -1
  141. package/src/duckdb/src/optimizer/statistics/expression/propagate_comparison.cpp +13 -15
  142. package/src/duckdb/src/optimizer/statistics/expression/propagate_conjunction.cpp +0 -1
  143. package/src/duckdb/src/optimizer/statistics/expression/propagate_constant.cpp +3 -75
  144. package/src/duckdb/src/optimizer/statistics/expression/propagate_function.cpp +7 -2
  145. package/src/duckdb/src/optimizer/statistics/expression/propagate_operator.cpp +10 -0
  146. package/src/duckdb/src/optimizer/statistics/operator/propagate_aggregate.cpp +2 -3
  147. package/src/duckdb/src/optimizer/statistics/operator/propagate_filter.cpp +28 -31
  148. package/src/duckdb/src/optimizer/statistics/operator/propagate_join.cpp +4 -5
  149. package/src/duckdb/src/optimizer/statistics/operator/propagate_set_operation.cpp +3 -3
  150. package/src/duckdb/src/optimizer/statistics_propagator.cpp +1 -1
  151. package/src/duckdb/src/parser/parsed_data/alter_info.cpp +7 -3
  152. package/src/duckdb/src/parser/parsed_data/alter_scalar_function_info.cpp +56 -0
  153. package/src/duckdb/src/parser/parsed_data/alter_table_function_info.cpp +51 -0
  154. package/src/duckdb/src/parser/parsed_data/create_scalar_function_info.cpp +3 -2
  155. package/src/duckdb/src/parser/parsed_data/create_table_function_info.cpp +6 -0
  156. package/src/duckdb/src/parser/parsed_expression_iterator.cpp +8 -0
  157. package/src/duckdb/src/parser/query_node.cpp +1 -1
  158. package/src/duckdb/src/parser/statement/multi_statement.cpp +18 -0
  159. package/src/duckdb/src/parser/tableref/pivotref.cpp +296 -0
  160. package/src/duckdb/src/parser/tableref.cpp +3 -0
  161. package/src/duckdb/src/parser/transform/helpers/transform_alias.cpp +12 -6
  162. package/src/duckdb/src/parser/transform/helpers/transform_cte.cpp +24 -0
  163. package/src/duckdb/src/parser/transform/statement/transform_create_function.cpp +4 -0
  164. package/src/duckdb/src/parser/transform/statement/transform_create_view.cpp +4 -0
  165. package/src/duckdb/src/parser/transform/statement/transform_pivot_stmt.cpp +150 -0
  166. package/src/duckdb/src/parser/transform/statement/transform_select.cpp +8 -0
  167. package/src/duckdb/src/parser/transform/statement/transform_select_node.cpp +1 -1
  168. package/src/duckdb/src/parser/transform/tableref/transform_join.cpp +4 -0
  169. package/src/duckdb/src/parser/transform/tableref/transform_pivot.cpp +105 -0
  170. package/src/duckdb/src/parser/transform/tableref/transform_tableref.cpp +2 -0
  171. package/src/duckdb/src/parser/transformer.cpp +15 -3
  172. package/src/duckdb/src/planner/bind_context.cpp +16 -0
  173. package/src/duckdb/src/planner/binder/query_node/bind_select_node.cpp +11 -3
  174. package/src/duckdb/src/planner/binder/query_node/plan_select_node.cpp +0 -1
  175. package/src/duckdb/src/planner/binder/statement/bind_create.cpp +1 -1
  176. package/src/duckdb/src/planner/binder/statement/bind_logical_plan.cpp +17 -0
  177. package/src/duckdb/src/planner/binder/tableref/bind_joinref.cpp +9 -0
  178. package/src/duckdb/src/planner/binder/tableref/bind_pivot.cpp +365 -0
  179. package/src/duckdb/src/planner/binder.cpp +7 -1
  180. package/src/duckdb/src/planner/bound_result_modifier.cpp +1 -1
  181. package/src/duckdb/src/planner/expression/bound_window_expression.cpp +1 -1
  182. package/src/duckdb/src/planner/filter/constant_filter.cpp +4 -6
  183. package/src/duckdb/src/planner/pragma_handler.cpp +10 -2
  184. package/src/duckdb/src/storage/buffer_manager.cpp +44 -46
  185. package/src/duckdb/src/storage/checkpoint/row_group_writer.cpp +1 -1
  186. package/src/duckdb/src/storage/checkpoint/table_data_reader.cpp +1 -4
  187. package/src/duckdb/src/storage/checkpoint/table_data_writer.cpp +4 -4
  188. package/src/duckdb/src/storage/compression/bitpacking.cpp +28 -24
  189. package/src/duckdb/src/storage/compression/fixed_size_uncompressed.cpp +43 -45
  190. package/src/duckdb/src/storage/compression/numeric_constant.cpp +9 -10
  191. package/src/duckdb/src/storage/compression/patas.cpp +1 -1
  192. package/src/duckdb/src/storage/compression/rle.cpp +19 -15
  193. package/src/duckdb/src/storage/compression/validity_uncompressed.cpp +5 -5
  194. package/src/duckdb/src/storage/data_table.cpp +4 -6
  195. package/src/duckdb/src/storage/statistics/base_statistics.cpp +373 -128
  196. package/src/duckdb/src/storage/statistics/column_statistics.cpp +58 -3
  197. package/src/duckdb/src/storage/statistics/distinct_statistics.cpp +4 -9
  198. package/src/duckdb/src/storage/statistics/list_stats.cpp +117 -0
  199. package/src/duckdb/src/storage/statistics/numeric_stats.cpp +529 -0
  200. package/src/duckdb/src/storage/statistics/segment_statistics.cpp +2 -11
  201. package/src/duckdb/src/storage/statistics/string_stats.cpp +273 -0
  202. package/src/duckdb/src/storage/statistics/struct_stats.cpp +131 -0
  203. package/src/duckdb/src/storage/storage_info.cpp +1 -1
  204. package/src/duckdb/src/storage/table/column_checkpoint_state.cpp +3 -4
  205. package/src/duckdb/src/storage/table/column_data.cpp +16 -11
  206. package/src/duckdb/src/storage/table/column_data_checkpointer.cpp +2 -3
  207. package/src/duckdb/src/storage/table/column_segment.cpp +6 -8
  208. package/src/duckdb/src/storage/table/list_column_data.cpp +39 -58
  209. package/src/duckdb/src/storage/table/row_group.cpp +24 -23
  210. package/src/duckdb/src/storage/table/row_group_collection.cpp +12 -12
  211. package/src/duckdb/src/storage/table/standard_column_data.cpp +6 -6
  212. package/src/duckdb/src/storage/table/struct_column_data.cpp +15 -16
  213. package/src/duckdb/src/storage/table/table_statistics.cpp +27 -7
  214. package/src/duckdb/src/storage/table/update_segment.cpp +10 -12
  215. package/src/duckdb/third_party/libpg_query/include/nodes/nodes.hpp +3 -0
  216. package/src/duckdb/third_party/libpg_query/include/nodes/parsenodes.hpp +34 -1
  217. package/src/duckdb/third_party/libpg_query/include/parser/gram.hpp +1020 -530
  218. package/src/duckdb/third_party/libpg_query/include/parser/kwlist.hpp +7 -0
  219. package/src/duckdb/third_party/libpg_query/src_backend_parser_gram.cpp +23560 -22737
  220. package/src/duckdb/ub_src_function_aggregate_distributive.cpp +2 -0
  221. package/src/duckdb/ub_src_function_scalar_bit.cpp +2 -0
  222. package/src/duckdb/ub_src_function_scalar_map.cpp +4 -0
  223. package/src/duckdb/ub_src_main_capi.cpp +2 -0
  224. package/src/duckdb/ub_src_parser_parsed_data.cpp +4 -2
  225. package/src/duckdb/ub_src_parser_statement.cpp +2 -0
  226. package/src/duckdb/ub_src_parser_tableref.cpp +2 -0
  227. package/src/duckdb/ub_src_parser_transform_statement.cpp +2 -0
  228. package/src/duckdb/ub_src_parser_transform_tableref.cpp +2 -0
  229. package/src/duckdb/ub_src_planner_binder_tableref.cpp +2 -0
  230. package/src/duckdb/ub_src_storage_statistics.cpp +4 -6
  231. package/src/duckdb/src/include/duckdb/main/loadable_extension.hpp +0 -59
  232. package/src/duckdb/src/include/duckdb/storage/statistics/list_statistics.hpp +0 -36
  233. package/src/duckdb/src/include/duckdb/storage/statistics/numeric_statistics.hpp +0 -75
  234. package/src/duckdb/src/include/duckdb/storage/statistics/string_statistics.hpp +0 -49
  235. package/src/duckdb/src/include/duckdb/storage/statistics/struct_statistics.hpp +0 -36
  236. package/src/duckdb/src/include/duckdb/storage/statistics/validity_statistics.hpp +0 -45
  237. package/src/duckdb/src/parser/parsed_data/alter_function_info.cpp +0 -55
  238. package/src/duckdb/src/storage/statistics/list_statistics.cpp +0 -94
  239. package/src/duckdb/src/storage/statistics/numeric_statistics.cpp +0 -307
  240. package/src/duckdb/src/storage/statistics/string_statistics.cpp +0 -220
  241. package/src/duckdb/src/storage/statistics/struct_statistics.cpp +0 -108
  242. package/src/duckdb/src/storage/statistics/validity_statistics.cpp +0 -91
@@ -0,0 +1,254 @@
1
+ #include "duckdb/function/aggregate/distributive_functions.hpp"
2
+ #include "duckdb/common/exception.hpp"
3
+ #include "duckdb/common/types/null_value.hpp"
4
+ #include "duckdb/common/vector_operations/aggregate_executor.hpp"
5
+ #include "duckdb/common/types/bit.hpp"
6
+ #include "duckdb/storage/statistics/base_statistics.hpp"
7
+ #include "duckdb/execution/expression_executor.hpp"
8
+ #include "duckdb/common/types/cast_helpers.hpp"
9
+
10
+ namespace duckdb {
11
+
12
+ template <class INPUT_TYPE>
13
+ struct BitAggState {
14
+ bool is_set;
15
+ string_t value;
16
+ INPUT_TYPE min;
17
+ INPUT_TYPE max;
18
+ };
19
+
20
+ struct BitstringAggBindData : public FunctionData {
21
+ Value min;
22
+ Value max;
23
+
24
+ BitstringAggBindData() {
25
+ }
26
+
27
+ BitstringAggBindData(Value min, Value max) : min(std::move(min)), max(std::move(max)) {
28
+ }
29
+
30
+ unique_ptr<FunctionData> Copy() const override {
31
+ return make_unique<BitstringAggBindData>(*this);
32
+ }
33
+
34
+ bool Equals(const FunctionData &other_p) const override {
35
+ auto &other = (BitstringAggBindData &)other_p;
36
+ if (min.IsNull() && other.min.IsNull() && max.IsNull() && other.max.IsNull()) {
37
+ return true;
38
+ }
39
+ if (Value::NotDistinctFrom(min, other.min) && Value::NotDistinctFrom(max, other.max)) {
40
+ return true;
41
+ }
42
+ return false;
43
+ }
44
+ };
45
+
46
+ struct BitStringAggOperation {
47
+ static constexpr const idx_t MAX_BIT_RANGE = 1000000000; // for now capped at 1 billion bits
48
+
49
+ template <class STATE>
50
+ static void Initialize(STATE *state) {
51
+ state->is_set = false;
52
+ }
53
+
54
+ template <class INPUT_TYPE, class STATE, class OP>
55
+ static void Operation(STATE *state, AggregateInputData &data, INPUT_TYPE *input, ValidityMask &mask, idx_t idx) {
56
+ auto bind_agg_data = (BitstringAggBindData *)data.bind_data;
57
+ if (!state->is_set) {
58
+ if (bind_agg_data->min.IsNull() || bind_agg_data->max.IsNull()) {
59
+ throw BinderException(
60
+ "Could not retrieve required statistics. Alternatively, try by providing the statistics "
61
+ "explicitly: BITSTRING_AGG(col, min, max) ");
62
+ }
63
+ state->min = bind_agg_data->min.GetValue<INPUT_TYPE>();
64
+ state->max = bind_agg_data->max.GetValue<INPUT_TYPE>();
65
+ idx_t bit_range =
66
+ GetRange(bind_agg_data->min.GetValue<INPUT_TYPE>(), bind_agg_data->max.GetValue<INPUT_TYPE>());
67
+ if (bit_range > MAX_BIT_RANGE) {
68
+ throw OutOfRangeException(
69
+ "The range between min and max value (%s <-> %s) is too large for bitstring aggregation",
70
+ NumericHelper::ToString(state->min), NumericHelper::ToString(state->max));
71
+ }
72
+ idx_t len = Bit::ComputeBitstringLen(bit_range);
73
+ auto target = len > string_t::INLINE_LENGTH ? string_t(new char[len], len) : string_t(len);
74
+ Bit::SetEmptyBitString(target, bit_range);
75
+
76
+ state->value = target;
77
+ state->is_set = true;
78
+ }
79
+ if (input[idx] >= state->min && input[idx] <= state->max) {
80
+ Execute(state, input[idx], bind_agg_data->min.GetValue<INPUT_TYPE>());
81
+ } else {
82
+ throw OutOfRangeException("Value %s is outside of provided min and max range (%s <-> %s)",
83
+ NumericHelper::ToString(input[idx]), NumericHelper::ToString(state->min),
84
+ NumericHelper::ToString(state->max));
85
+ }
86
+ }
87
+
88
+ template <class INPUT_TYPE, class STATE, class OP>
89
+ static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *input,
90
+ ValidityMask &mask, idx_t count) {
91
+ OP::template Operation<INPUT_TYPE, STATE, OP>(state, aggr_input_data, input, mask, 0);
92
+ }
93
+
94
+ template <class INPUT_TYPE>
95
+ static idx_t GetRange(INPUT_TYPE min, INPUT_TYPE max) {
96
+ return max - min + 1;
97
+ }
98
+
99
+ template <class INPUT_TYPE, class STATE>
100
+ static void Execute(STATE *state, INPUT_TYPE input, INPUT_TYPE min) {
101
+ Bit::SetBit(state->value, input - min, 1);
102
+ }
103
+
104
+ template <class STATE, class OP>
105
+ static void Combine(const STATE &source, STATE *target, AggregateInputData &) {
106
+ if (!source.is_set) {
107
+ return;
108
+ }
109
+ if (!target->is_set) {
110
+ Assign(target, source.value);
111
+ target->is_set = true;
112
+ target->min = source.min;
113
+ target->max = source.max;
114
+ } else {
115
+ Bit::BitwiseOr(source.value, target->value, target->value);
116
+ }
117
+ }
118
+
119
+ template <class INPUT_TYPE, class STATE>
120
+ static void Assign(STATE *state, INPUT_TYPE input) {
121
+ D_ASSERT(state->is_set == false);
122
+ if (input.IsInlined()) {
123
+ state->value = input;
124
+ } else { // non-inlined string, need to allocate space for it
125
+ auto len = input.GetSize();
126
+ auto ptr = new char[len];
127
+ memcpy(ptr, input.GetDataUnsafe(), len);
128
+ state->value = string_t(ptr, len);
129
+ }
130
+ }
131
+
132
+ template <class T, class STATE>
133
+ static void Finalize(Vector &result, AggregateInputData &, STATE *state, T *target, ValidityMask &mask, idx_t idx) {
134
+ if (!state->is_set) {
135
+ mask.SetInvalid(idx);
136
+ } else {
137
+ target[idx] = StringVector::AddStringOrBlob(result, state->value);
138
+ }
139
+ }
140
+
141
+ template <class STATE>
142
+ static void Destroy(STATE *state) {
143
+ if (state->is_set && !state->value.IsInlined()) {
144
+ delete[] state->value.GetDataUnsafe();
145
+ }
146
+ }
147
+
148
+ static bool IgnoreNull() {
149
+ return true;
150
+ }
151
+ };
152
+
153
+ template <>
154
+ void BitStringAggOperation::Execute(BitAggState<hugeint_t> *state, hugeint_t input, hugeint_t min) {
155
+ idx_t val;
156
+ if (Hugeint::TryCast(input - min, val)) {
157
+ Bit::SetBit(state->value, val, 1);
158
+ } else {
159
+ throw OutOfRangeException("Range too large for bitstring aggregation");
160
+ }
161
+ }
162
+
163
+ template <>
164
+ idx_t BitStringAggOperation::GetRange(hugeint_t min, hugeint_t max) {
165
+ idx_t val;
166
+ if (Hugeint::TryCast(max - min + 1, val)) {
167
+ return val;
168
+ } else {
169
+ throw OutOfRangeException("Range too large for bitstring aggregation");
170
+ }
171
+ }
172
+
173
+ unique_ptr<BaseStatistics> BitstringPropagateStats(ClientContext &context, BoundAggregateExpression &expr,
174
+ AggregateStatisticsInput &input) {
175
+
176
+ if (!NumericStats::HasMinMax(input.child_stats[0])) {
177
+ throw BinderException("Could not retrieve required statistics. Alternatively, try by providing the statistics "
178
+ "explicitly: BITSTRING_AGG(col, min, max) ");
179
+ }
180
+ auto bind_agg_data = (BitstringAggBindData *)input.bind_data;
181
+ bind_agg_data->min = NumericStats::Min(input.child_stats[0]);
182
+ bind_agg_data->max = NumericStats::Max(input.child_stats[0]);
183
+ return nullptr;
184
+ }
185
+
186
+ unique_ptr<FunctionData> BindBitstringAgg(ClientContext &context, AggregateFunction &function,
187
+ vector<unique_ptr<Expression>> &arguments) {
188
+
189
+ if (arguments.size() == 3) {
190
+ auto min = ExpressionExecutor::EvaluateScalar(context, *arguments[1]);
191
+ auto max = ExpressionExecutor::EvaluateScalar(context, *arguments[2]);
192
+ Function::EraseArgument(function, arguments, 2);
193
+ Function::EraseArgument(function, arguments, 1);
194
+ return make_unique<BitstringAggBindData>(min, max);
195
+ }
196
+ return make_unique<BitstringAggBindData>();
197
+ }
198
+
199
+ template <class TYPE>
200
+ static void BindBitString(AggregateFunctionSet &bitstring_agg, const LogicalTypeId &type) {
201
+ auto function =
202
+ AggregateFunction::UnaryAggregateDestructor<BitAggState<TYPE>, TYPE, string_t, BitStringAggOperation>(
203
+ type, LogicalType::BIT);
204
+ function.bind = BindBitstringAgg; // create new a 'BitstringAggBindData'
205
+ function.statistics = BitstringPropagateStats; // stores min and max from column stats in BitstringAggBindData
206
+ bitstring_agg.AddFunction(function); // uses the BitstringAggBindData to access statistics for creating bitstring
207
+ function.arguments = {type, type, type};
208
+ function.statistics = nullptr; // min and max are provided as arguments
209
+ bitstring_agg.AddFunction(function);
210
+ }
211
+
212
+ void BitStringAggFun::GetBitStringAggregate(const LogicalType &type, AggregateFunctionSet &bitstring_agg) {
213
+ switch (type.id()) {
214
+ case LogicalType::TINYINT: {
215
+ return BindBitString<int8_t>(bitstring_agg, type.id());
216
+ }
217
+ case LogicalType::SMALLINT: {
218
+ return BindBitString<int16_t>(bitstring_agg, type.id());
219
+ }
220
+ case LogicalType::INTEGER: {
221
+ return BindBitString<int32_t>(bitstring_agg, type.id());
222
+ }
223
+ case LogicalType::BIGINT: {
224
+ return BindBitString<int64_t>(bitstring_agg, type.id());
225
+ }
226
+ case LogicalType::HUGEINT: {
227
+ return BindBitString<hugeint_t>(bitstring_agg, type.id());
228
+ }
229
+ case LogicalType::UTINYINT: {
230
+ return BindBitString<uint8_t>(bitstring_agg, type.id());
231
+ }
232
+ case LogicalType::USMALLINT: {
233
+ return BindBitString<uint16_t>(bitstring_agg, type.id());
234
+ }
235
+ case LogicalType::UINTEGER: {
236
+ return BindBitString<uint32_t>(bitstring_agg, type.id());
237
+ }
238
+ case LogicalType::UBIGINT: {
239
+ return BindBitString<uint64_t>(bitstring_agg, type.id());
240
+ }
241
+ default:
242
+ throw InternalException("Unimplemented bitstring aggregate");
243
+ }
244
+ }
245
+
246
+ void BitStringAggFun::RegisterFunction(BuiltinFunctions &set) {
247
+ AggregateFunctionSet bitstring_agg("bitstring_agg");
248
+ for (auto &type : LogicalType::Integral()) {
249
+ GetBitStringAggregate(type, bitstring_agg);
250
+ }
251
+ set.AddFunction(bitstring_agg);
252
+ }
253
+
254
+ } // namespace duckdb
@@ -2,7 +2,6 @@
2
2
  #include "duckdb/common/vector_operations/vector_operations.hpp"
3
3
  #include "duckdb/function/aggregate/distributive_functions.hpp"
4
4
  #include "duckdb/planner/expression/bound_aggregate_expression.hpp"
5
- #include "duckdb/storage/statistics/validity_statistics.hpp"
6
5
 
7
6
  namespace duckdb {
8
7
 
@@ -100,9 +99,8 @@ AggregateFunction CountStarFun::GetFunction() {
100
99
  }
101
100
 
102
101
  unique_ptr<BaseStatistics> CountPropagateStats(ClientContext &context, BoundAggregateExpression &expr,
103
- FunctionData *bind_data, vector<unique_ptr<BaseStatistics>> &child_stats,
104
- NodeStatistics *node_stats) {
105
- if (!expr.IsDistinct() && child_stats[0] && !child_stats[0]->CanHaveNull()) {
102
+ AggregateStatisticsInput &input) {
103
+ if (!expr.IsDistinct() && !input.child_stats[0].CanHaveNull()) {
106
104
  // count on a column without null values: use count star
107
105
  expr.function = CountStarFun::GetFunction();
108
106
  expr.function.name = "count_star";
@@ -2,7 +2,6 @@
2
2
  #include "duckdb/function/aggregate/sum_helpers.hpp"
3
3
  #include "duckdb/common/exception.hpp"
4
4
  #include "duckdb/common/types/decimal.hpp"
5
- #include "duckdb/storage/statistics/numeric_statistics.hpp"
6
5
  #include "duckdb/planner/expression/bound_aggregate_expression.hpp"
7
6
  #include "duckdb/function/aggregate/algebraic_functions.hpp"
8
7
 
@@ -52,9 +51,6 @@ struct DoubleSumOperation : public BaseSumOperation<SumSetOperation, ADD_OPERATO
52
51
  if (!state->isset) {
53
52
  mask.SetInvalid(idx);
54
53
  } else {
55
- if (!Value::DoubleIsFinite(state->value)) {
56
- throw OutOfRangeException("SUM is out of range!");
57
- }
58
54
  target[idx] = state->value;
59
55
  }
60
56
  }
@@ -75,30 +71,29 @@ struct HugeintSumOperation : public BaseSumOperation<SumSetOperation, RegularAdd
75
71
  };
76
72
 
77
73
  unique_ptr<BaseStatistics> SumPropagateStats(ClientContext &context, BoundAggregateExpression &expr,
78
- FunctionData *bind_data, vector<unique_ptr<BaseStatistics>> &child_stats,
79
- NodeStatistics *node_stats) {
80
- if (child_stats[0] && node_stats && node_stats->has_max_cardinality) {
81
- auto &numeric_stats = (NumericStatistics &)*child_stats[0];
82
- if (numeric_stats.min.IsNull() || numeric_stats.max.IsNull()) {
74
+ AggregateStatisticsInput &input) {
75
+ if (input.node_stats && input.node_stats->has_max_cardinality) {
76
+ auto &numeric_stats = input.child_stats[0];
77
+ if (!NumericStats::HasMinMax(numeric_stats)) {
83
78
  return nullptr;
84
79
  }
85
- auto internal_type = numeric_stats.min.type().InternalType();
80
+ auto internal_type = numeric_stats.GetType().InternalType();
86
81
  hugeint_t max_negative;
87
82
  hugeint_t max_positive;
88
83
  switch (internal_type) {
89
84
  case PhysicalType::INT32:
90
- max_negative = numeric_stats.min.GetValueUnsafe<int32_t>();
91
- max_positive = numeric_stats.max.GetValueUnsafe<int32_t>();
85
+ max_negative = NumericStats::Min(numeric_stats).GetValueUnsafe<int32_t>();
86
+ max_positive = NumericStats::Max(numeric_stats).GetValueUnsafe<int32_t>();
92
87
  break;
93
88
  case PhysicalType::INT64:
94
- max_negative = numeric_stats.min.GetValueUnsafe<int64_t>();
95
- max_positive = numeric_stats.max.GetValueUnsafe<int64_t>();
89
+ max_negative = NumericStats::Min(numeric_stats).GetValueUnsafe<int64_t>();
90
+ max_positive = NumericStats::Max(numeric_stats).GetValueUnsafe<int64_t>();
96
91
  break;
97
92
  default:
98
93
  throw InternalException("Unsupported type for propagate sum stats");
99
94
  }
100
- auto max_sum_negative = max_negative * hugeint_t(node_stats->max_cardinality);
101
- auto max_sum_positive = max_positive * hugeint_t(node_stats->max_cardinality);
95
+ auto max_sum_negative = max_negative * hugeint_t(input.node_stats->max_cardinality);
96
+ auto max_sum_positive = max_positive * hugeint_t(input.node_stats->max_cardinality);
102
97
  if (max_sum_positive >= NumericLimits<int64_t>::Maximum() ||
103
98
  max_sum_negative <= NumericLimits<int64_t>::Minimum()) {
104
99
  // sum can potentially exceed int64_t bounds: use hugeint sum
@@ -10,6 +10,7 @@ void BuiltinFunctions::RegisterDistributiveAggregates() {
10
10
  Register<BitAndFun>();
11
11
  Register<BitOrFun>();
12
12
  Register<BitXorFun>();
13
+ Register<BitStringAggFun>();
13
14
  Register<CountStarFun>();
14
15
  Register<CountFun>();
15
16
  Register<FirstFun>();
@@ -71,17 +71,25 @@ struct SortedAggregateBindData : public FunctionData {
71
71
  };
72
72
 
73
73
  struct SortedAggregateState {
74
- static const idx_t BUFFER_CAPACITY = STANDARD_VECTOR_SIZE;
74
+ //! Default buffer size, optimised for small group to avoid blowing out memory.
75
+ static const idx_t BUFFER_CAPACITY = 16;
75
76
 
76
77
  SortedAggregateState() : nsel(0) {
77
78
  }
78
79
 
79
80
  static inline void InitializeBuffer(DataChunk &chunk, const vector<LogicalType> &types) {
80
81
  if (!chunk.ColumnCount() && !types.empty()) {
81
- chunk.Initialize(Allocator::DefaultAllocator(), types);
82
+ chunk.Initialize(Allocator::DefaultAllocator(), types, BUFFER_CAPACITY);
82
83
  }
83
84
  }
84
85
 
86
+ //! Make sure the buffer is large enough for slicing
87
+ static inline void ResetBuffer(DataChunk &chunk, const vector<LogicalType> &types) {
88
+ chunk.Reset();
89
+ chunk.Destroy();
90
+ chunk.Initialize(Allocator::DefaultAllocator(), types);
91
+ }
92
+
85
93
  void Flush(SortedAggregateBindData &order_bind) {
86
94
  if (ordering) {
87
95
  return;
@@ -90,10 +98,12 @@ struct SortedAggregateState {
90
98
  ordering = make_unique<ColumnDataCollection>(order_bind.buffer_manager, order_bind.sort_types);
91
99
  InitializeBuffer(sort_buffer, order_bind.sort_types);
92
100
  ordering->Append(sort_buffer);
101
+ ResetBuffer(sort_buffer, order_bind.sort_types);
93
102
 
94
103
  arguments = make_unique<ColumnDataCollection>(order_bind.buffer_manager, order_bind.arg_types);
95
104
  InitializeBuffer(arg_buffer, order_bind.arg_types);
96
105
  arguments->Append(arg_buffer);
106
+ ResetBuffer(arg_buffer, order_bind.arg_types);
97
107
  }
98
108
 
99
109
  void Update(SortedAggregateBindData &order_bind, DataChunk &sort_chunk, DataChunk &arg_chunk) {
@@ -101,7 +111,7 @@ struct SortedAggregateState {
101
111
  InitializeBuffer(sort_buffer, order_bind.sort_types);
102
112
  InitializeBuffer(arg_buffer, order_bind.arg_types);
103
113
 
104
- if (sort_chunk.size() + sort_buffer.size() > BUFFER_CAPACITY) {
114
+ if (sort_chunk.size() + sort_buffer.size() > STANDARD_VECTOR_SIZE) {
105
115
  Flush(order_bind);
106
116
  }
107
117
  if (ordering) {
@@ -118,7 +128,7 @@ struct SortedAggregateState {
118
128
  InitializeBuffer(sort_buffer, order_bind.sort_types);
119
129
  InitializeBuffer(arg_buffer, order_bind.arg_types);
120
130
 
121
- if (nsel + sort_buffer.size() > BUFFER_CAPACITY) {
131
+ if (nsel + sort_buffer.size() > STANDARD_VECTOR_SIZE) {
122
132
  Flush(order_bind);
123
133
  }
124
134
  if (ordering) {
@@ -276,6 +286,8 @@ struct SortedAggregateFunction {
276
286
  auto &orders = order_bind->orders;
277
287
  RowLayout payload_layout;
278
288
  payload_layout.Initialize(order_bind->arg_types);
289
+ DataChunk chunk;
290
+ chunk.Initialize(Allocator::DefaultAllocator(), order_bind->arg_types);
279
291
 
280
292
  // Reusable inner state
281
293
  vector<data_t> agg_state(order_bind->function.state_size());
@@ -314,7 +326,6 @@ struct SortedAggregateFunction {
314
326
  global_sort->CompleteMergeRound(false);
315
327
  }
316
328
 
317
- auto &chunk = state->arg_buffer;
318
329
  PayloadScanner scanner(*global_sort);
319
330
  for (;;) {
320
331
  chunk.Reset();
@@ -9,8 +9,6 @@ BoundCastInfo DefaultCasts::BitCastSwitch(BindCastInput &input, const LogicalTyp
9
9
  case LogicalTypeId::VARCHAR:
10
10
  // bit to varchar
11
11
  return BoundCastInfo(&VectorCastHelpers::StringCast<string_t, duckdb::CastFromBit>);
12
- case LogicalTypeId::BLOB:
13
- return DefaultCasts::ReinterpretCast;
14
12
  default:
15
13
  return DefaultCasts::TryVectorNullCast;
16
14
  }
@@ -10,7 +10,6 @@ BoundCastInfo DefaultCasts::BlobCastSwitch(BindCastInput &input, const LogicalTy
10
10
  // blob to varchar
11
11
  return BoundCastInfo(&VectorCastHelpers::StringCast<string_t, duckdb::CastFromBlob>);
12
12
  case LogicalTypeId::AGGREGATE_STATE:
13
- case LogicalTypeId::BIT:
14
13
  return DefaultCasts::ReinterpretCast;
15
14
  default:
16
15
  return DefaultCasts::TryVectorNullCast;
@@ -0,0 +1,99 @@
1
+ #include "duckdb/common/types/bit.hpp"
2
+ #include "duckdb/function/scalar/bit_functions.hpp"
3
+ #include "duckdb/common/types/cast_helpers.hpp"
4
+
5
+ namespace duckdb {
6
+
7
+ //===--------------------------------------------------------------------===//
8
+ // BitStringFunction
9
+ //===--------------------------------------------------------------------===//
10
+
11
+ static void BitStringFunction(DataChunk &args, ExpressionState &state, Vector &result) {
12
+ BinaryExecutor::Execute<string_t, int32_t, string_t>(
13
+ args.data[0], args.data[1], result, args.size(), [&](string_t input, int32_t n) {
14
+ if (n < 0) {
15
+ throw InvalidInputException("The bitstring length cannot be negative");
16
+ }
17
+ if ((idx_t)n < input.GetSize()) {
18
+ throw InvalidInputException("Length must be equal or larger than input string");
19
+ }
20
+ idx_t len;
21
+ Bit::TryGetBitStringSize(input, len, nullptr); // string verification
22
+
23
+ len = Bit::ComputeBitstringLen(n);
24
+ string_t target = StringVector::EmptyString(result, len);
25
+
26
+ Bit::BitString(input, n, target);
27
+ return target;
28
+ });
29
+ }
30
+
31
+ void BitStringFun::RegisterFunction(BuiltinFunctions &set) {
32
+ // bitstring creates a new bitstring from varchar with a fixed-length
33
+ set.AddFunction(
34
+ ScalarFunction("bitstring", {LogicalType::VARCHAR, LogicalType::INTEGER}, LogicalType::BIT, BitStringFunction));
35
+ }
36
+
37
+ //===--------------------------------------------------------------------===//
38
+ // get_bit
39
+ //===--------------------------------------------------------------------===//
40
+ struct GetBitOperator {
41
+ template <class TA, class TB, class TR>
42
+ static inline TR Operation(TA input, TB n) {
43
+ if (n < 0 || (idx_t)n > Bit::BitLength(input) - 1) {
44
+ throw OutOfRangeException("bit index %s out of valid range (0..%s)", NumericHelper::ToString(n),
45
+ NumericHelper::ToString(Bit::BitLength(input) - 1));
46
+ }
47
+ return Bit::GetBit(input, n);
48
+ }
49
+ };
50
+
51
+ void GetBitFun::RegisterFunction(BuiltinFunctions &set) {
52
+ set.AddFunction(ScalarFunction("get_bit", {LogicalType::BIT, LogicalType::INTEGER}, LogicalType::INTEGER,
53
+ ScalarFunction::BinaryFunction<string_t, int32_t, int32_t, GetBitOperator>));
54
+ }
55
+
56
+ //===--------------------------------------------------------------------===//
57
+ // set_bit
58
+ //===--------------------------------------------------------------------===//
59
+ static void SetBitOperation(DataChunk &args, ExpressionState &state, Vector &result) {
60
+ TernaryExecutor::Execute<string_t, int32_t, int32_t, string_t>(
61
+ args.data[0], args.data[1], args.data[2], result, args.size(),
62
+ [&](string_t input, int32_t n, int32_t new_value) {
63
+ if (new_value != 0 && new_value != 1) {
64
+ throw InvalidInputException("The new bit must be 1 or 0");
65
+ }
66
+ if (n < 0 || (idx_t)n > Bit::BitLength(input) - 1) {
67
+ throw OutOfRangeException("bit index %s out of valid range (0..%s)", NumericHelper::ToString(n),
68
+ NumericHelper::ToString(Bit::BitLength(input) - 1));
69
+ }
70
+ string_t target = StringVector::EmptyString(result, input.GetSize());
71
+ Bit::SetBit(input, n, new_value, target);
72
+ return target;
73
+ });
74
+ }
75
+
76
+ void SetBitFun::RegisterFunction(BuiltinFunctions &set) {
77
+ set.AddFunction(ScalarFunction("set_bit", {LogicalType::BIT, LogicalType::INTEGER, LogicalType::INTEGER},
78
+ LogicalType::BIT, SetBitOperation));
79
+ }
80
+
81
+ //===--------------------------------------------------------------------===//
82
+ // bit_position
83
+ //===--------------------------------------------------------------------===//
84
+ struct BitPositionOperator {
85
+ template <class TA, class TB, class TR>
86
+ static inline TR Operation(TA substring, TB input) {
87
+ if (substring.GetSize() > input.GetSize()) {
88
+ return 0;
89
+ }
90
+ return Bit::BitPosition(substring, input);
91
+ }
92
+ };
93
+
94
+ void BitPositionFun::RegisterFunction(BuiltinFunctions &set) {
95
+ set.AddFunction(ScalarFunction("bit_position", {LogicalType::BIT, LogicalType::BIT}, LogicalType::INTEGER,
96
+ ScalarFunction::BinaryFunction<string_t, string_t, int32_t, BitPositionOperator>));
97
+ }
98
+
99
+ } // namespace duckdb
@@ -9,7 +9,6 @@
9
9
  #include "duckdb/common/vector_operations/ternary_executor.hpp"
10
10
  #include "duckdb/common/vector_operations/vector_operations.hpp"
11
11
  #include "duckdb/common/string_util.hpp"
12
- #include "duckdb/storage/statistics/numeric_statistics.hpp"
13
12
 
14
13
  namespace duckdb {
15
14
 
@@ -9,7 +9,7 @@
9
9
  #include "duckdb/function/scalar/date_functions.hpp"
10
10
  #include "duckdb/function/scalar/nested_functions.hpp"
11
11
  #include "duckdb/planner/expression/bound_function_expression.hpp"
12
- #include "duckdb/storage/statistics/numeric_statistics.hpp"
12
+
13
13
  #include "duckdb/common/field_writer.hpp"
14
14
 
15
15
  namespace duckdb {
@@ -161,34 +161,27 @@ DatePartSpecifier GetDateTypePartSpecifier(const string &specifier, LogicalType
161
161
  }
162
162
 
163
163
  template <int64_t MIN, int64_t MAX>
164
- static unique_ptr<BaseStatistics> PropagateSimpleDatePartStatistics(vector<unique_ptr<BaseStatistics>> &child_stats) {
164
+ static unique_ptr<BaseStatistics> PropagateSimpleDatePartStatistics(vector<BaseStatistics> &child_stats) {
165
165
  // we can always propagate simple date part statistics
166
166
  // since the min and max can never exceed these bounds
167
- auto result = make_unique<NumericStatistics>(LogicalType::BIGINT, Value::BIGINT(MIN), Value::BIGINT(MAX),
168
- StatisticsType::LOCAL_STATS);
169
- if (!child_stats[0]) {
170
- // if there are no child stats, we don't know
171
- result->validity_stats = make_unique<ValidityStatistics>(true);
172
- } else if (child_stats[0]->validity_stats) {
173
- result->validity_stats = child_stats[0]->validity_stats->Copy();
174
- }
175
- return std::move(result);
167
+ auto result = NumericStats::CreateEmpty(LogicalType::BIGINT);
168
+ result.CopyValidity(child_stats[0]);
169
+ NumericStats::SetMin(result, Value::BIGINT(MIN));
170
+ NumericStats::SetMax(result, Value::BIGINT(MAX));
171
+ return result.ToUnique();
176
172
  }
177
173
 
178
174
  struct DatePart {
179
175
  template <class T, class OP>
180
- static unique_ptr<BaseStatistics> PropagateDatePartStatistics(vector<unique_ptr<BaseStatistics>> &child_stats) {
176
+ static unique_ptr<BaseStatistics> PropagateDatePartStatistics(vector<BaseStatistics> &child_stats) {
181
177
  // we can only propagate complex date part stats if the child has stats
182
- if (!child_stats[0]) {
183
- return nullptr;
184
- }
185
- auto &nstats = (NumericStatistics &)*child_stats[0];
186
- if (nstats.min.IsNull() || nstats.max.IsNull()) {
178
+ auto &nstats = child_stats[0];
179
+ if (!NumericStats::HasMinMax(nstats)) {
187
180
  return nullptr;
188
181
  }
189
182
  // run the operator on both the min and the max, this gives us the [min, max] bound
190
- auto min = nstats.min.GetValueUnsafe<T>();
191
- auto max = nstats.max.GetValueUnsafe<T>();
183
+ auto min = NumericStats::GetMinUnsafe<T>(nstats);
184
+ auto max = NumericStats::GetMaxUnsafe<T>(nstats);
192
185
  if (min > max) {
193
186
  return nullptr;
194
187
  }
@@ -198,12 +191,11 @@ struct DatePart {
198
191
  }
199
192
  auto min_part = OP::template Operation<T, int64_t>(min);
200
193
  auto max_part = OP::template Operation<T, int64_t>(max);
201
- auto result = make_unique<NumericStatistics>(LogicalType::BIGINT, Value::BIGINT(min_part),
202
- Value::BIGINT(max_part), StatisticsType::LOCAL_STATS);
203
- if (child_stats[0]->validity_stats) {
204
- result->validity_stats = child_stats[0]->validity_stats->Copy();
205
- }
206
- return std::move(result);
194
+ auto result = NumericStats::CreateEmpty(LogicalType::BIGINT);
195
+ NumericStats::SetMin(result, Value::BIGINT(min_part));
196
+ NumericStats::SetMax(result, Value::BIGINT(max_part));
197
+ result.CopyValidity(child_stats[0]);
198
+ return result.ToUnique();
207
199
  }
208
200
 
209
201
  template <typename OP>
@@ -9,7 +9,6 @@
9
9
  #include "duckdb/common/vector_operations/ternary_executor.hpp"
10
10
  #include "duckdb/common/vector_operations/vector_operations.hpp"
11
11
  #include "duckdb/common/string_util.hpp"
12
- #include "duckdb/storage/statistics/numeric_statistics.hpp"
13
12
 
14
13
  namespace duckdb {
15
14