duckdb 1.0.1-dev22.0 → 1.0.1-dev27.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 (1389) hide show
  1. package/.github/workflows/NodeJS.yml +1 -1
  2. package/binding.gyp +41 -0
  3. package/package.json +1 -1
  4. package/src/duckdb/extension/icu/icu-dateadd.cpp +4 -2
  5. package/src/duckdb/extension/icu/icu-datefunc.cpp +6 -2
  6. package/src/duckdb/extension/icu/icu-datesub.cpp +13 -2
  7. package/src/duckdb/extension/icu/icu-strptime.cpp +6 -6
  8. package/src/duckdb/extension/icu/icu-table-range.cpp +92 -73
  9. package/src/duckdb/extension/icu/icu-timebucket.cpp +12 -2
  10. package/src/duckdb/extension/icu/icu-timezone.cpp +3 -3
  11. package/src/duckdb/extension/icu/icu_extension.cpp +61 -9
  12. package/src/duckdb/extension/json/include/json_executors.hpp +20 -23
  13. package/src/duckdb/extension/json/include/json_functions.hpp +4 -0
  14. package/src/duckdb/extension/json/include/json_scan.hpp +6 -2
  15. package/src/duckdb/extension/json/include/json_structure.hpp +12 -9
  16. package/src/duckdb/extension/json/json_common.cpp +66 -10
  17. package/src/duckdb/extension/json/json_extension.cpp +13 -5
  18. package/src/duckdb/extension/json/json_functions/json_array_length.cpp +1 -1
  19. package/src/duckdb/extension/json/json_functions/json_create.cpp +21 -4
  20. package/src/duckdb/extension/json/json_functions/json_exists.cpp +32 -0
  21. package/src/duckdb/extension/json/json_functions/json_extract.cpp +2 -2
  22. package/src/duckdb/extension/json/json_functions/json_keys.cpp +1 -1
  23. package/src/duckdb/extension/json/json_functions/json_pretty.cpp +32 -0
  24. package/src/duckdb/extension/json/json_functions/json_serialize_sql.cpp +5 -1
  25. package/src/duckdb/extension/json/json_functions/json_structure.cpp +305 -94
  26. package/src/duckdb/extension/json/json_functions/json_transform.cpp +1 -1
  27. package/src/duckdb/extension/json/json_functions/json_type.cpp +3 -3
  28. package/src/duckdb/extension/json/json_functions/json_value.cpp +42 -0
  29. package/src/duckdb/extension/json/json_functions/read_json.cpp +16 -2
  30. package/src/duckdb/extension/json/json_functions/read_json_objects.cpp +3 -2
  31. package/src/duckdb/extension/json/json_functions.cpp +5 -1
  32. package/src/duckdb/extension/json/json_scan.cpp +13 -12
  33. package/src/duckdb/extension/json/serialize_json.cpp +5 -3
  34. package/src/duckdb/extension/parquet/column_reader.cpp +206 -43
  35. package/src/duckdb/extension/parquet/column_writer.cpp +133 -62
  36. package/src/duckdb/extension/parquet/geo_parquet.cpp +391 -0
  37. package/src/duckdb/extension/parquet/include/boolean_column_reader.hpp +16 -5
  38. package/src/duckdb/extension/parquet/include/column_reader.hpp +37 -12
  39. package/src/duckdb/extension/parquet/include/column_writer.hpp +10 -11
  40. package/src/duckdb/extension/parquet/include/expression_column_reader.hpp +52 -0
  41. package/src/duckdb/extension/parquet/include/geo_parquet.hpp +139 -0
  42. package/src/duckdb/extension/parquet/include/parquet_crypto.hpp +13 -8
  43. package/src/duckdb/extension/parquet/include/parquet_decimal_utils.hpp +3 -0
  44. package/src/duckdb/extension/parquet/include/parquet_file_metadata_cache.hpp +7 -3
  45. package/src/duckdb/extension/parquet/include/parquet_reader.hpp +55 -8
  46. package/src/duckdb/extension/parquet/include/parquet_rle_bp_decoder.hpp +3 -3
  47. package/src/duckdb/extension/parquet/include/parquet_rle_bp_encoder.hpp +1 -1
  48. package/src/duckdb/extension/parquet/include/parquet_timestamp.hpp +8 -0
  49. package/src/duckdb/extension/parquet/include/parquet_writer.hpp +21 -7
  50. package/src/duckdb/extension/parquet/include/resizable_buffer.hpp +33 -11
  51. package/src/duckdb/extension/parquet/include/string_column_reader.hpp +5 -2
  52. package/src/duckdb/extension/parquet/include/templated_column_reader.hpp +48 -14
  53. package/src/duckdb/extension/parquet/parquet_crypto.cpp +109 -61
  54. package/src/duckdb/extension/parquet/parquet_extension.cpp +305 -72
  55. package/src/duckdb/extension/parquet/parquet_metadata.cpp +4 -4
  56. package/src/duckdb/extension/parquet/parquet_reader.cpp +151 -40
  57. package/src/duckdb/extension/parquet/parquet_statistics.cpp +50 -16
  58. package/src/duckdb/extension/parquet/parquet_timestamp.cpp +42 -1
  59. package/src/duckdb/extension/parquet/parquet_writer.cpp +67 -75
  60. package/src/duckdb/extension/parquet/serialize_parquet.cpp +3 -1
  61. package/src/duckdb/extension/parquet/zstd_file_system.cpp +5 -1
  62. package/src/duckdb/src/catalog/catalog.cpp +14 -16
  63. package/src/duckdb/src/catalog/catalog_entry/duck_index_entry.cpp +14 -11
  64. package/src/duckdb/src/catalog/catalog_entry/duck_schema_entry.cpp +39 -19
  65. package/src/duckdb/src/catalog/catalog_entry/duck_table_entry.cpp +92 -78
  66. package/src/duckdb/src/catalog/catalog_entry/index_catalog_entry.cpp +10 -2
  67. package/src/duckdb/src/catalog/catalog_entry/macro_catalog_entry.cpp +10 -3
  68. package/src/duckdb/src/catalog/catalog_entry/schema_catalog_entry.cpp +3 -3
  69. package/src/duckdb/src/catalog/catalog_entry/table_catalog_entry.cpp +7 -7
  70. package/src/duckdb/src/catalog/catalog_entry.cpp +6 -3
  71. package/src/duckdb/src/catalog/catalog_set.cpp +14 -19
  72. package/src/duckdb/src/catalog/default/default_functions.cpp +179 -166
  73. package/src/duckdb/src/catalog/default/default_generator.cpp +24 -0
  74. package/src/duckdb/src/catalog/default/default_schemas.cpp +4 -3
  75. package/src/duckdb/src/catalog/default/default_table_functions.cpp +148 -0
  76. package/src/duckdb/src/catalog/default/default_views.cpp +7 -3
  77. package/src/duckdb/src/catalog/duck_catalog.cpp +7 -1
  78. package/src/duckdb/src/common/adbc/adbc.cpp +120 -58
  79. package/src/duckdb/src/common/allocator.cpp +71 -6
  80. package/src/duckdb/src/common/arrow/appender/bool_data.cpp +8 -7
  81. package/src/duckdb/src/common/arrow/appender/fixed_size_list_data.cpp +1 -1
  82. package/src/duckdb/src/common/arrow/appender/union_data.cpp +4 -5
  83. package/src/duckdb/src/common/arrow/arrow_appender.cpp +55 -21
  84. package/src/duckdb/src/common/arrow/arrow_converter.cpp +85 -10
  85. package/src/duckdb/src/common/arrow/arrow_merge_event.cpp +142 -0
  86. package/src/duckdb/src/common/arrow/arrow_query_result.cpp +56 -0
  87. package/src/duckdb/src/common/arrow/physical_arrow_batch_collector.cpp +37 -0
  88. package/src/duckdb/src/common/arrow/physical_arrow_collector.cpp +128 -0
  89. package/src/duckdb/src/common/arrow/schema_metadata.cpp +101 -0
  90. package/src/duckdb/src/common/cgroups.cpp +189 -0
  91. package/src/duckdb/src/common/compressed_file_system.cpp +6 -3
  92. package/src/duckdb/src/common/encryption_state.cpp +38 -0
  93. package/src/duckdb/src/common/enum_util.cpp +682 -14
  94. package/src/duckdb/src/common/enums/file_compression_type.cpp +24 -0
  95. package/src/duckdb/src/common/enums/metric_type.cpp +208 -0
  96. package/src/duckdb/src/common/enums/optimizer_type.cpp +8 -2
  97. package/src/duckdb/src/common/enums/physical_operator_type.cpp +2 -0
  98. package/src/duckdb/src/common/enums/relation_type.cpp +4 -0
  99. package/src/duckdb/src/common/enums/statement_type.cpp +15 -0
  100. package/src/duckdb/src/common/error_data.cpp +22 -20
  101. package/src/duckdb/src/common/exception/binder_exception.cpp +5 -0
  102. package/src/duckdb/src/common/exception.cpp +11 -1
  103. package/src/duckdb/src/common/extra_type_info.cpp +3 -0
  104. package/src/duckdb/src/common/file_buffer.cpp +1 -1
  105. package/src/duckdb/src/common/file_system.cpp +25 -3
  106. package/src/duckdb/src/common/filename_pattern.cpp +1 -0
  107. package/src/duckdb/src/common/fsst.cpp +15 -14
  108. package/src/duckdb/src/common/gzip_file_system.cpp +3 -1
  109. package/src/duckdb/src/common/hive_partitioning.cpp +103 -43
  110. package/src/duckdb/src/common/http_util.cpp +25 -0
  111. package/src/duckdb/src/common/local_file_system.cpp +48 -27
  112. package/src/duckdb/src/common/multi_file_list.cpp +113 -22
  113. package/src/duckdb/src/common/multi_file_reader.cpp +59 -58
  114. package/src/duckdb/src/common/operator/cast_operators.cpp +133 -34
  115. package/src/duckdb/src/common/operator/string_cast.cpp +42 -11
  116. package/src/duckdb/src/common/progress_bar/progress_bar.cpp +2 -2
  117. package/src/duckdb/src/common/progress_bar/terminal_progress_bar_display.cpp +1 -1
  118. package/src/duckdb/src/common/radix_partitioning.cpp +31 -21
  119. package/src/duckdb/src/common/random_engine.cpp +4 -0
  120. package/src/duckdb/src/common/re2_regex.cpp +47 -12
  121. package/src/duckdb/src/common/render_tree.cpp +243 -0
  122. package/src/duckdb/src/common/row_operations/row_aggregate.cpp +1 -1
  123. package/src/duckdb/src/common/row_operations/row_gather.cpp +2 -2
  124. package/src/duckdb/src/common/row_operations/row_matcher.cpp +58 -5
  125. package/src/duckdb/src/common/row_operations/row_radix_scatter.cpp +79 -43
  126. package/src/duckdb/src/common/serializer/binary_deserializer.cpp +1 -1
  127. package/src/duckdb/src/common/serializer/buffered_file_reader.cpp +6 -4
  128. package/src/duckdb/src/common/serializer/buffered_file_writer.cpp +18 -9
  129. package/src/duckdb/src/common/serializer/memory_stream.cpp +1 -0
  130. package/src/duckdb/src/common/sort/partition_state.cpp +33 -18
  131. package/src/duckdb/src/common/sort/radix_sort.cpp +22 -15
  132. package/src/duckdb/src/common/sort/sort_state.cpp +19 -16
  133. package/src/duckdb/src/common/sort/sorted_block.cpp +11 -10
  134. package/src/duckdb/src/common/string_util.cpp +167 -10
  135. package/src/duckdb/src/common/tree_renderer/graphviz_tree_renderer.cpp +108 -0
  136. package/src/duckdb/src/common/tree_renderer/html_tree_renderer.cpp +267 -0
  137. package/src/duckdb/src/common/tree_renderer/json_tree_renderer.cpp +116 -0
  138. package/src/duckdb/src/common/tree_renderer/text_tree_renderer.cpp +482 -0
  139. package/src/duckdb/src/common/tree_renderer/tree_renderer.cpp +12 -0
  140. package/src/duckdb/src/common/tree_renderer.cpp +16 -508
  141. package/src/duckdb/src/common/types/batched_data_collection.cpp +78 -9
  142. package/src/duckdb/src/common/types/bit.cpp +24 -22
  143. package/src/duckdb/src/common/types/blob.cpp +15 -11
  144. package/src/duckdb/src/common/types/column/column_data_allocator.cpp +18 -9
  145. package/src/duckdb/src/common/types/column/column_data_collection.cpp +4 -4
  146. package/src/duckdb/src/common/types/column/column_data_collection_segment.cpp +3 -4
  147. package/src/duckdb/src/common/types/column/column_data_consumer.cpp +2 -2
  148. package/src/duckdb/src/common/types/column/partitioned_column_data.cpp +70 -21
  149. package/src/duckdb/src/common/types/data_chunk.cpp +10 -1
  150. package/src/duckdb/src/common/types/date.cpp +8 -19
  151. package/src/duckdb/src/common/types/decimal.cpp +3 -2
  152. package/src/duckdb/src/common/types/hugeint.cpp +11 -3
  153. package/src/duckdb/src/common/types/hyperloglog.cpp +212 -227
  154. package/src/duckdb/src/common/types/interval.cpp +1 -1
  155. package/src/duckdb/src/common/types/list_segment.cpp +83 -49
  156. package/src/duckdb/src/common/types/row/partitioned_tuple_data.cpp +22 -83
  157. package/src/duckdb/src/common/types/row/row_data_collection.cpp +2 -2
  158. package/src/duckdb/src/common/types/row/row_data_collection_scanner.cpp +20 -4
  159. package/src/duckdb/src/common/types/row/tuple_data_allocator.cpp +28 -7
  160. package/src/duckdb/src/common/types/row/tuple_data_collection.cpp +29 -14
  161. package/src/duckdb/src/common/types/row/tuple_data_scatter_gather.cpp +152 -102
  162. package/src/duckdb/src/common/types/row/tuple_data_segment.cpp +4 -1
  163. package/src/duckdb/src/common/types/selection_vector.cpp +17 -1
  164. package/src/duckdb/src/common/types/time.cpp +62 -31
  165. package/src/duckdb/src/common/types/timestamp.cpp +70 -12
  166. package/src/duckdb/src/common/types/uuid.cpp +1 -1
  167. package/src/duckdb/src/common/types/validity_mask.cpp +40 -5
  168. package/src/duckdb/src/common/types/value.cpp +50 -8
  169. package/src/duckdb/src/common/types/varint.cpp +295 -0
  170. package/src/duckdb/src/common/types/vector.cpp +165 -54
  171. package/src/duckdb/src/common/types/vector_buffer.cpp +5 -4
  172. package/src/duckdb/src/common/types.cpp +106 -26
  173. package/src/duckdb/src/common/vector_operations/vector_copy.cpp +13 -25
  174. package/src/duckdb/src/common/vector_operations/vector_hash.cpp +6 -0
  175. package/src/duckdb/src/common/virtual_file_system.cpp +3 -3
  176. package/src/duckdb/src/core_functions/aggregate/distributive/approx_count.cpp +35 -82
  177. package/src/duckdb/src/core_functions/aggregate/distributive/arg_min_max.cpp +283 -46
  178. package/src/duckdb/src/core_functions/aggregate/distributive/bitagg.cpp +4 -4
  179. package/src/duckdb/src/core_functions/aggregate/distributive/entropy.cpp +3 -2
  180. package/src/duckdb/src/core_functions/aggregate/distributive/minmax.cpp +226 -338
  181. package/src/duckdb/src/core_functions/aggregate/distributive/sum.cpp +2 -0
  182. package/src/duckdb/src/core_functions/aggregate/holistic/approx_top_k.cpp +388 -0
  183. package/src/duckdb/src/core_functions/aggregate/holistic/approximate_quantile.cpp +63 -21
  184. package/src/duckdb/src/core_functions/aggregate/holistic/mad.cpp +330 -0
  185. package/src/duckdb/src/core_functions/aggregate/holistic/mode.cpp +136 -97
  186. package/src/duckdb/src/core_functions/aggregate/holistic/quantile.cpp +601 -1485
  187. package/src/duckdb/src/core_functions/aggregate/nested/binned_histogram.cpp +405 -0
  188. package/src/duckdb/src/core_functions/aggregate/nested/histogram.cpp +136 -165
  189. package/src/duckdb/src/core_functions/function_list.cpp +35 -8
  190. package/src/duckdb/src/core_functions/lambda_functions.cpp +5 -7
  191. package/src/duckdb/src/core_functions/scalar/array/array_functions.cpp +172 -198
  192. package/src/duckdb/src/core_functions/scalar/blob/create_sort_key.cpp +341 -54
  193. package/src/duckdb/src/core_functions/scalar/date/date_diff.cpp +2 -2
  194. package/src/duckdb/src/core_functions/scalar/date/date_part.cpp +89 -29
  195. package/src/duckdb/src/core_functions/scalar/date/date_trunc.cpp +1 -1
  196. package/src/duckdb/src/core_functions/scalar/date/make_date.cpp +2 -2
  197. package/src/duckdb/src/core_functions/scalar/date/strftime.cpp +133 -71
  198. package/src/duckdb/src/core_functions/scalar/date/to_interval.cpp +1 -1
  199. package/src/duckdb/src/core_functions/scalar/enum/enum_functions.cpp +1 -1
  200. package/src/duckdb/src/core_functions/scalar/generic/can_implicitly_cast.cpp +40 -0
  201. package/src/duckdb/src/core_functions/scalar/generic/error.cpp +1 -1
  202. package/src/duckdb/src/core_functions/scalar/generic/least.cpp +161 -58
  203. package/src/duckdb/src/core_functions/scalar/generic/typeof.cpp +13 -0
  204. package/src/duckdb/src/core_functions/scalar/list/array_slice.cpp +1 -1
  205. package/src/duckdb/src/core_functions/scalar/list/list_aggregates.cpp +59 -75
  206. package/src/duckdb/src/core_functions/scalar/list/list_distance.cpp +93 -40
  207. package/src/duckdb/src/core_functions/scalar/list/list_has_any_or_all.cpp +227 -0
  208. package/src/duckdb/src/core_functions/scalar/list/list_reduce.cpp +20 -19
  209. package/src/duckdb/src/core_functions/scalar/list/list_sort.cpp +0 -2
  210. package/src/duckdb/src/core_functions/scalar/list/list_value.cpp +106 -8
  211. package/src/duckdb/src/core_functions/scalar/map/map_contains.cpp +56 -0
  212. package/src/duckdb/src/core_functions/scalar/map/map_extract.cpp +73 -118
  213. package/src/duckdb/src/core_functions/scalar/math/numeric.cpp +98 -2
  214. package/src/duckdb/src/core_functions/scalar/operators/bitwise.cpp +1 -2
  215. package/src/duckdb/src/core_functions/scalar/random/setseed.cpp +1 -1
  216. package/src/duckdb/src/core_functions/scalar/string/bar.cpp +1 -1
  217. package/src/duckdb/src/core_functions/scalar/string/hex.cpp +5 -1
  218. package/src/duckdb/src/core_functions/scalar/string/md5.cpp +10 -37
  219. package/src/duckdb/src/core_functions/scalar/string/printf.cpp +18 -2
  220. package/src/duckdb/src/core_functions/scalar/string/repeat.cpp +45 -0
  221. package/src/duckdb/src/core_functions/scalar/string/reverse.cpp +4 -5
  222. package/src/duckdb/src/core_functions/scalar/string/sha1.cpp +35 -0
  223. package/src/duckdb/src/core_functions/scalar/string/sha256.cpp +5 -2
  224. package/src/duckdb/src/core_functions/scalar/string/url_encode.cpp +49 -0
  225. package/src/duckdb/src/core_functions/scalar/struct/struct_pack.cpp +1 -2
  226. package/src/duckdb/src/core_functions/scalar/union/union_extract.cpp +4 -2
  227. package/src/duckdb/src/execution/adaptive_filter.cpp +30 -11
  228. package/src/duckdb/src/execution/aggregate_hashtable.cpp +13 -18
  229. package/src/duckdb/src/execution/expression_executor/execute_conjunction.cpp +4 -9
  230. package/src/duckdb/src/execution/expression_executor.cpp +1 -1
  231. package/src/duckdb/src/execution/index/art/art.cpp +683 -670
  232. package/src/duckdb/src/execution/index/art/art_key.cpp +121 -38
  233. package/src/duckdb/src/execution/index/art/base_leaf.cpp +168 -0
  234. package/src/duckdb/src/execution/index/art/base_node.cpp +163 -0
  235. package/src/duckdb/src/execution/index/art/iterator.cpp +148 -77
  236. package/src/duckdb/src/execution/index/art/leaf.cpp +159 -263
  237. package/src/duckdb/src/execution/index/art/node.cpp +493 -247
  238. package/src/duckdb/src/execution/index/art/node256.cpp +31 -91
  239. package/src/duckdb/src/execution/index/art/node256_leaf.cpp +71 -0
  240. package/src/duckdb/src/execution/index/art/node48.cpp +75 -143
  241. package/src/duckdb/src/execution/index/art/prefix.cpp +424 -244
  242. package/src/duckdb/src/execution/index/bound_index.cpp +7 -1
  243. package/src/duckdb/src/execution/index/fixed_size_allocator.cpp +22 -18
  244. package/src/duckdb/src/execution/index/fixed_size_buffer.cpp +22 -73
  245. package/src/duckdb/src/execution/join_hashtable.cpp +637 -179
  246. package/src/duckdb/src/execution/operator/aggregate/aggregate_object.cpp +4 -4
  247. package/src/duckdb/src/execution/operator/aggregate/physical_hash_aggregate.cpp +15 -10
  248. package/src/duckdb/src/execution/operator/aggregate/physical_perfecthash_aggregate.cpp +13 -8
  249. package/src/duckdb/src/execution/operator/aggregate/physical_streaming_window.cpp +525 -132
  250. package/src/duckdb/src/execution/operator/aggregate/physical_ungrouped_aggregate.cpp +147 -138
  251. package/src/duckdb/src/execution/operator/aggregate/physical_window.cpp +531 -312
  252. package/src/duckdb/src/execution/operator/csv_scanner/buffer_manager/csv_buffer.cpp +1 -1
  253. package/src/duckdb/src/execution/operator/csv_scanner/buffer_manager/csv_buffer_manager.cpp +4 -3
  254. package/src/duckdb/src/execution/operator/csv_scanner/buffer_manager/csv_file_handle.cpp +9 -2
  255. package/src/duckdb/src/execution/operator/csv_scanner/scanner/base_scanner.cpp +13 -17
  256. package/src/duckdb/src/execution/operator/csv_scanner/scanner/column_count_scanner.cpp +60 -16
  257. package/src/duckdb/src/execution/operator/csv_scanner/scanner/csv_schema.cpp +105 -0
  258. package/src/duckdb/src/execution/operator/csv_scanner/scanner/scanner_boundary.cpp +24 -24
  259. package/src/duckdb/src/execution/operator/csv_scanner/scanner/skip_scanner.cpp +25 -2
  260. package/src/duckdb/src/execution/operator/csv_scanner/scanner/string_value_scanner.cpp +275 -112
  261. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/csv_sniffer.cpp +106 -11
  262. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/dialect_detection.cpp +253 -115
  263. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/header_detection.cpp +93 -52
  264. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/type_detection.cpp +116 -76
  265. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/type_refinement.cpp +29 -14
  266. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/type_replacement.cpp +1 -1
  267. package/src/duckdb/src/execution/operator/csv_scanner/state_machine/csv_state_machine_cache.cpp +70 -26
  268. package/src/duckdb/src/execution/operator/csv_scanner/table_function/csv_file_scanner.cpp +81 -60
  269. package/src/duckdb/src/execution/operator/csv_scanner/table_function/global_csv_state.cpp +88 -50
  270. package/src/duckdb/src/execution/operator/csv_scanner/util/csv_error.cpp +161 -51
  271. package/src/duckdb/src/execution/operator/csv_scanner/util/csv_reader_options.cpp +59 -17
  272. package/src/duckdb/src/execution/operator/filter/physical_filter.cpp +5 -5
  273. package/src/duckdb/src/execution/operator/helper/physical_batch_collector.cpp +0 -21
  274. package/src/duckdb/src/execution/operator/helper/physical_buffered_batch_collector.cpp +109 -0
  275. package/src/duckdb/src/execution/operator/helper/physical_buffered_collector.cpp +5 -13
  276. package/src/duckdb/src/execution/operator/helper/physical_explain_analyze.cpp +1 -1
  277. package/src/duckdb/src/execution/operator/helper/physical_load.cpp +12 -4
  278. package/src/duckdb/src/execution/operator/helper/physical_materialized_collector.cpp +0 -16
  279. package/src/duckdb/src/execution/operator/helper/physical_reservoir_sample.cpp +4 -2
  280. package/src/duckdb/src/execution/operator/helper/physical_reset.cpp +5 -0
  281. package/src/duckdb/src/execution/operator/helper/physical_result_collector.cpp +3 -1
  282. package/src/duckdb/src/execution/operator/helper/physical_set_variable.cpp +39 -0
  283. package/src/duckdb/src/execution/operator/helper/physical_streaming_sample.cpp +4 -2
  284. package/src/duckdb/src/execution/operator/helper/physical_transaction.cpp +16 -5
  285. package/src/duckdb/src/execution/operator/join/outer_join_marker.cpp +1 -1
  286. package/src/duckdb/src/execution/operator/join/perfect_hash_join_executor.cpp +1 -1
  287. package/src/duckdb/src/execution/operator/join/physical_asof_join.cpp +1 -1
  288. package/src/duckdb/src/execution/operator/join/physical_blockwise_nl_join.cpp +5 -4
  289. package/src/duckdb/src/execution/operator/join/physical_comparison_join.cpp +59 -21
  290. package/src/duckdb/src/execution/operator/join/physical_delim_join.cpp +7 -4
  291. package/src/duckdb/src/execution/operator/join/physical_hash_join.cpp +333 -176
  292. package/src/duckdb/src/execution/operator/join/physical_iejoin.cpp +57 -34
  293. package/src/duckdb/src/execution/operator/join/physical_join.cpp +16 -8
  294. package/src/duckdb/src/execution/operator/join/physical_left_delim_join.cpp +10 -4
  295. package/src/duckdb/src/execution/operator/join/physical_nested_loop_join.cpp +2 -5
  296. package/src/duckdb/src/execution/operator/join/physical_piecewise_merge_join.cpp +3 -3
  297. package/src/duckdb/src/execution/operator/join/physical_range_join.cpp +5 -5
  298. package/src/duckdb/src/execution/operator/join/physical_right_delim_join.cpp +7 -2
  299. package/src/duckdb/src/execution/operator/order/physical_order.cpp +17 -12
  300. package/src/duckdb/src/execution/operator/order/physical_top_n.cpp +12 -9
  301. package/src/duckdb/src/execution/operator/persistent/physical_batch_copy_to_file.cpp +35 -17
  302. package/src/duckdb/src/execution/operator/persistent/physical_batch_insert.cpp +17 -11
  303. package/src/duckdb/src/execution/operator/persistent/physical_copy_database.cpp +5 -1
  304. package/src/duckdb/src/execution/operator/persistent/physical_copy_to_file.cpp +156 -47
  305. package/src/duckdb/src/execution/operator/persistent/physical_insert.cpp +10 -2
  306. package/src/duckdb/src/execution/operator/persistent/physical_update.cpp +1 -3
  307. package/src/duckdb/src/execution/operator/projection/physical_pivot.cpp +2 -2
  308. package/src/duckdb/src/execution/operator/projection/physical_projection.cpp +13 -6
  309. package/src/duckdb/src/execution/operator/projection/physical_tableinout_function.cpp +22 -3
  310. package/src/duckdb/src/execution/operator/projection/physical_unnest.cpp +19 -3
  311. package/src/duckdb/src/execution/operator/scan/physical_column_data_scan.cpp +37 -22
  312. package/src/duckdb/src/execution/operator/scan/physical_table_scan.cpp +77 -21
  313. package/src/duckdb/src/execution/operator/schema/physical_attach.cpp +27 -55
  314. package/src/duckdb/src/execution/operator/schema/physical_create_art_index.cpp +41 -44
  315. package/src/duckdb/src/execution/operator/set/physical_cte.cpp +4 -6
  316. package/src/duckdb/src/execution/operator/set/physical_recursive_cte.cpp +4 -6
  317. package/src/duckdb/src/execution/operator/set/physical_union.cpp +18 -4
  318. package/src/duckdb/src/execution/perfect_aggregate_hashtable.cpp +3 -2
  319. package/src/duckdb/src/execution/physical_operator.cpp +45 -4
  320. package/src/duckdb/src/execution/physical_plan/plan_comparison_join.cpp +18 -7
  321. package/src/duckdb/src/execution/physical_plan/plan_copy_to_file.cpp +8 -3
  322. package/src/duckdb/src/execution/physical_plan/plan_delim_join.cpp +13 -6
  323. package/src/duckdb/src/execution/physical_plan/plan_explain.cpp +3 -3
  324. package/src/duckdb/src/execution/physical_plan/plan_get.cpp +111 -19
  325. package/src/duckdb/src/execution/physical_plan/plan_limit.cpp +19 -2
  326. package/src/duckdb/src/execution/physical_plan/plan_set.cpp +9 -0
  327. package/src/duckdb/src/execution/physical_plan/plan_window.cpp +3 -1
  328. package/src/duckdb/src/execution/physical_plan_generator.cpp +3 -3
  329. package/src/duckdb/src/execution/radix_partitioned_hashtable.cpp +49 -49
  330. package/src/duckdb/src/execution/reservoir_sample.cpp +2 -2
  331. package/src/duckdb/src/execution/window_executor.cpp +556 -318
  332. package/src/duckdb/src/execution/window_segment_tree.cpp +1058 -485
  333. package/src/duckdb/src/function/aggregate/distributive/count.cpp +5 -5
  334. package/src/duckdb/src/function/aggregate/distributive/first.cpp +92 -95
  335. package/src/duckdb/src/function/aggregate/sorted_aggregate_function.cpp +10 -9
  336. package/src/duckdb/src/function/aggregate_function.cpp +8 -0
  337. package/src/duckdb/src/function/cast/cast_function_set.cpp +10 -1
  338. package/src/duckdb/src/function/cast/decimal_cast.cpp +10 -1
  339. package/src/duckdb/src/function/cast/default_casts.cpp +2 -0
  340. package/src/duckdb/src/function/cast/numeric_casts.cpp +3 -0
  341. package/src/duckdb/src/function/cast/string_cast.cpp +8 -5
  342. package/src/duckdb/src/function/cast/time_casts.cpp +2 -2
  343. package/src/duckdb/src/function/cast/union_casts.cpp +1 -1
  344. package/src/duckdb/src/function/cast/varint_casts.cpp +283 -0
  345. package/src/duckdb/src/function/cast/vector_cast_helpers.cpp +3 -1
  346. package/src/duckdb/src/function/cast_rules.cpp +104 -15
  347. package/src/duckdb/src/function/compression_config.cpp +35 -33
  348. package/src/duckdb/src/function/copy_function.cpp +27 -0
  349. package/src/duckdb/src/function/function_binder.cpp +39 -11
  350. package/src/duckdb/src/function/macro_function.cpp +75 -32
  351. package/src/duckdb/src/function/pragma/pragma_queries.cpp +10 -0
  352. package/src/duckdb/src/function/scalar/compressed_materialization/compress_string.cpp +1 -0
  353. package/src/duckdb/src/function/scalar/generic/binning.cpp +507 -0
  354. package/src/duckdb/src/function/scalar/generic/getvariable.cpp +58 -0
  355. package/src/duckdb/src/function/scalar/generic_functions.cpp +1 -0
  356. package/src/duckdb/src/function/scalar/list/contains_or_position.cpp +33 -47
  357. package/src/duckdb/src/function/scalar/list/list_extract.cpp +70 -143
  358. package/src/duckdb/src/function/scalar/list/list_resize.cpp +93 -84
  359. package/src/duckdb/src/function/scalar/list/list_zip.cpp +3 -0
  360. package/src/duckdb/src/function/scalar/operators/arithmetic.cpp +24 -11
  361. package/src/duckdb/src/function/scalar/sequence/nextval.cpp +4 -4
  362. package/src/duckdb/src/function/scalar/strftime_format.cpp +196 -57
  363. package/src/duckdb/src/function/scalar/string/caseconvert.cpp +9 -7
  364. package/src/duckdb/src/function/scalar/string/concat.cpp +239 -123
  365. package/src/duckdb/src/function/scalar/string/concat_ws.cpp +149 -0
  366. package/src/duckdb/src/function/scalar/string/contains.cpp +18 -7
  367. package/src/duckdb/src/function/scalar/string/like.cpp +2 -2
  368. package/src/duckdb/src/function/scalar/string/substring.cpp +6 -11
  369. package/src/duckdb/src/function/scalar/string_functions.cpp +1 -0
  370. package/src/duckdb/src/function/scalar/struct/struct_extract.cpp +7 -3
  371. package/src/duckdb/src/function/scalar/system/aggregate_export.cpp +5 -5
  372. package/src/duckdb/src/function/scalar_function.cpp +5 -2
  373. package/src/duckdb/src/function/scalar_macro_function.cpp +2 -2
  374. package/src/duckdb/src/function/table/arrow/arrow_duck_schema.cpp +20 -39
  375. package/src/duckdb/src/function/table/arrow/arrow_type_info.cpp +135 -0
  376. package/src/duckdb/src/function/table/arrow.cpp +194 -52
  377. package/src/duckdb/src/function/table/arrow_conversion.cpp +212 -69
  378. package/src/duckdb/src/function/table/copy_csv.cpp +43 -14
  379. package/src/duckdb/src/function/table/query_function.cpp +80 -0
  380. package/src/duckdb/src/function/table/range.cpp +222 -142
  381. package/src/duckdb/src/function/table/read_csv.cpp +25 -13
  382. package/src/duckdb/src/function/table/sniff_csv.cpp +55 -35
  383. package/src/duckdb/src/function/table/system/duckdb_constraints.cpp +141 -129
  384. package/src/duckdb/src/function/table/system/duckdb_extensions.cpp +25 -14
  385. package/src/duckdb/src/function/table/system/duckdb_functions.cpp +20 -14
  386. package/src/duckdb/src/function/table/system/duckdb_indexes.cpp +15 -1
  387. package/src/duckdb/src/function/table/system/duckdb_variables.cpp +84 -0
  388. package/src/duckdb/src/function/table/system/test_all_types.cpp +1 -0
  389. package/src/duckdb/src/function/table/system/test_vector_types.cpp +33 -3
  390. package/src/duckdb/src/function/table/system_functions.cpp +1 -0
  391. package/src/duckdb/src/function/table/table_scan.cpp +45 -22
  392. package/src/duckdb/src/function/table/unnest.cpp +2 -2
  393. package/src/duckdb/src/function/table/version/pragma_version.cpp +4 -4
  394. package/src/duckdb/src/function/table_function.cpp +5 -4
  395. package/src/duckdb/src/function/table_macro_function.cpp +2 -2
  396. package/src/duckdb/src/include/duckdb/catalog/catalog.hpp +8 -4
  397. package/src/duckdb/src/include/duckdb/catalog/catalog_entry/duck_index_entry.hpp +5 -2
  398. package/src/duckdb/src/include/duckdb/catalog/catalog_entry/duck_schema_entry.hpp +3 -0
  399. package/src/duckdb/src/include/duckdb/catalog/catalog_entry/index_catalog_entry.hpp +2 -2
  400. package/src/duckdb/src/include/duckdb/catalog/catalog_entry/macro_catalog_entry.hpp +3 -4
  401. package/src/duckdb/src/include/duckdb/catalog/catalog_entry/table_catalog_entry.hpp +5 -5
  402. package/src/duckdb/src/include/duckdb/catalog/default/builtin_types/types.hpp +2 -1
  403. package/src/duckdb/src/include/duckdb/catalog/default/default_functions.hpp +4 -5
  404. package/src/duckdb/src/include/duckdb/catalog/default/default_generator.hpp +4 -5
  405. package/src/duckdb/src/include/duckdb/catalog/default/default_schemas.hpp +2 -1
  406. package/src/duckdb/src/include/duckdb/catalog/default/default_table_functions.hpp +47 -0
  407. package/src/duckdb/src/include/duckdb/catalog/duck_catalog.hpp +2 -0
  408. package/src/duckdb/src/include/duckdb/catalog/similar_catalog_entry.hpp +2 -2
  409. package/src/duckdb/src/include/duckdb/common/allocator.hpp +9 -1
  410. package/src/duckdb/src/include/duckdb/common/array_ptr.hpp +120 -0
  411. package/src/duckdb/src/include/duckdb/common/arrow/appender/append_data.hpp +37 -11
  412. package/src/duckdb/src/include/duckdb/common/arrow/appender/enum_data.hpp +9 -8
  413. package/src/duckdb/src/include/duckdb/common/arrow/appender/list.hpp +1 -0
  414. package/src/duckdb/src/include/duckdb/common/arrow/appender/list_data.hpp +6 -4
  415. package/src/duckdb/src/include/duckdb/common/arrow/appender/list_view_data.hpp +92 -0
  416. package/src/duckdb/src/include/duckdb/common/arrow/appender/map_data.hpp +2 -2
  417. package/src/duckdb/src/include/duckdb/common/arrow/appender/scalar_data.hpp +26 -4
  418. package/src/duckdb/src/include/duckdb/common/arrow/appender/varchar_data.hpp +90 -11
  419. package/src/duckdb/src/include/duckdb/common/arrow/arrow_appender.hpp +6 -6
  420. package/src/duckdb/src/include/duckdb/common/arrow/arrow_buffer.hpp +8 -1
  421. package/src/duckdb/src/include/duckdb/common/arrow/arrow_merge_event.hpp +62 -0
  422. package/src/duckdb/src/include/duckdb/common/arrow/arrow_query_result.hpp +52 -0
  423. package/src/duckdb/src/include/duckdb/common/arrow/arrow_types_extension.hpp +42 -0
  424. package/src/duckdb/src/include/duckdb/common/arrow/physical_arrow_batch_collector.hpp +30 -0
  425. package/src/duckdb/src/include/duckdb/common/arrow/physical_arrow_collector.hpp +65 -0
  426. package/src/duckdb/src/include/duckdb/common/arrow/schema_metadata.hpp +43 -0
  427. package/src/duckdb/src/include/duckdb/common/bswap.hpp +18 -16
  428. package/src/duckdb/src/include/duckdb/common/cgroups.hpp +30 -0
  429. package/src/duckdb/src/include/duckdb/common/compressed_file_system.hpp +3 -0
  430. package/src/duckdb/src/include/duckdb/common/dl.hpp +8 -1
  431. package/src/duckdb/src/include/duckdb/common/encryption_state.hpp +48 -0
  432. package/src/duckdb/src/include/duckdb/common/enum_util.hpp +88 -0
  433. package/src/duckdb/src/include/duckdb/common/enums/checkpoint_type.hpp +2 -2
  434. package/src/duckdb/src/include/duckdb/common/enums/copy_overwrite_mode.hpp +6 -1
  435. package/src/duckdb/src/include/duckdb/common/enums/destroy_buffer_upon.hpp +21 -0
  436. package/src/duckdb/src/include/duckdb/common/enums/explain_format.hpp +17 -0
  437. package/src/duckdb/src/include/duckdb/common/enums/file_compression_type.hpp +4 -0
  438. package/src/duckdb/src/include/duckdb/common/enums/join_type.hpp +2 -2
  439. package/src/duckdb/src/include/duckdb/common/enums/metric_type.hpp +88 -0
  440. package/src/duckdb/src/include/duckdb/common/enums/optimizer_type.hpp +6 -1
  441. package/src/duckdb/src/include/duckdb/common/enums/pending_execution_result.hpp +2 -1
  442. package/src/duckdb/src/include/duckdb/common/enums/physical_operator_type.hpp +1 -0
  443. package/src/duckdb/src/include/duckdb/common/enums/profiler_format.hpp +1 -1
  444. package/src/duckdb/src/include/duckdb/common/enums/relation_type.hpp +3 -1
  445. package/src/duckdb/src/include/duckdb/common/enums/set_scope.hpp +2 -1
  446. package/src/duckdb/src/include/duckdb/common/enums/statement_type.hpp +23 -2
  447. package/src/duckdb/src/include/duckdb/common/enums/stream_execution_result.hpp +25 -0
  448. package/src/duckdb/src/include/duckdb/common/enums/tableref_type.hpp +2 -1
  449. package/src/duckdb/src/include/duckdb/common/enums/wal_type.hpp +1 -0
  450. package/src/duckdb/src/include/duckdb/common/error_data.hpp +5 -2
  451. package/src/duckdb/src/include/duckdb/common/exception/binder_exception.hpp +1 -0
  452. package/src/duckdb/src/include/duckdb/common/exception.hpp +20 -2
  453. package/src/duckdb/src/include/duckdb/common/extra_operator_info.hpp +12 -0
  454. package/src/duckdb/src/include/duckdb/common/file_buffer.hpp +2 -0
  455. package/src/duckdb/src/include/duckdb/common/file_open_flags.hpp +16 -0
  456. package/src/duckdb/src/include/duckdb/common/file_opener.hpp +18 -0
  457. package/src/duckdb/src/include/duckdb/common/file_system.hpp +3 -0
  458. package/src/duckdb/src/include/duckdb/common/filename_pattern.hpp +4 -0
  459. package/src/duckdb/src/include/duckdb/common/fixed_size_map.hpp +160 -96
  460. package/src/duckdb/src/include/duckdb/common/fsst.hpp +9 -2
  461. package/src/duckdb/src/include/duckdb/common/helper.hpp +22 -8
  462. package/src/duckdb/src/include/duckdb/common/hive_partitioning.hpp +16 -7
  463. package/src/duckdb/src/include/duckdb/common/http_util.hpp +19 -0
  464. package/src/duckdb/src/include/duckdb/common/insertion_order_preserving_map.hpp +19 -6
  465. package/src/duckdb/src/include/duckdb/common/limits.hpp +9 -2
  466. package/src/duckdb/src/include/duckdb/common/multi_file_list.hpp +38 -6
  467. package/src/duckdb/src/include/duckdb/common/multi_file_reader.hpp +9 -2
  468. package/src/duckdb/src/include/duckdb/common/multi_file_reader_options.hpp +5 -1
  469. package/src/duckdb/src/include/duckdb/common/numeric_utils.hpp +82 -50
  470. package/src/duckdb/src/include/duckdb/common/operator/abs.hpp +11 -0
  471. package/src/duckdb/src/include/duckdb/common/operator/cast_operators.hpp +7 -3
  472. package/src/duckdb/src/include/duckdb/common/operator/decimal_cast_operators.hpp +23 -1
  473. package/src/duckdb/src/include/duckdb/common/operator/double_cast_operator.hpp +2 -1
  474. package/src/duckdb/src/include/duckdb/common/operator/integer_cast_operator.hpp +1 -1
  475. package/src/duckdb/src/include/duckdb/common/operator/numeric_cast.hpp +4 -0
  476. package/src/duckdb/src/include/duckdb/common/operator/string_cast.hpp +2 -0
  477. package/src/duckdb/src/include/duckdb/common/optional_ptr.hpp +10 -5
  478. package/src/duckdb/src/include/duckdb/common/optionally_owned_ptr.hpp +1 -0
  479. package/src/duckdb/src/include/duckdb/common/owning_string_map.hpp +155 -0
  480. package/src/duckdb/src/include/duckdb/common/perfect_map_set.hpp +2 -3
  481. package/src/duckdb/src/include/duckdb/common/platform.hpp +58 -0
  482. package/src/duckdb/src/include/duckdb/common/radix.hpp +172 -27
  483. package/src/duckdb/src/include/duckdb/common/radix_partitioning.hpp +5 -1
  484. package/src/duckdb/src/include/duckdb/common/random_engine.hpp +1 -0
  485. package/src/duckdb/src/include/duckdb/common/re2_regex.hpp +1 -1
  486. package/src/duckdb/src/include/duckdb/common/render_tree.hpp +77 -0
  487. package/src/duckdb/src/include/duckdb/common/row_operations/row_matcher.hpp +12 -0
  488. package/src/duckdb/src/include/duckdb/common/serializer/binary_serializer.hpp +6 -2
  489. package/src/duckdb/src/include/duckdb/common/serializer/buffered_file_writer.hpp +5 -3
  490. package/src/duckdb/src/include/duckdb/common/serializer/deserializer.hpp +15 -7
  491. package/src/duckdb/src/include/duckdb/common/serializer/memory_stream.hpp +3 -1
  492. package/src/duckdb/src/include/duckdb/common/serializer/serialization_data.hpp +245 -0
  493. package/src/duckdb/src/include/duckdb/common/serializer/serializer.hpp +10 -0
  494. package/src/duckdb/src/include/duckdb/common/sort/duckdb_pdqsort.hpp +10 -11
  495. package/src/duckdb/src/include/duckdb/common/sort/partition_state.hpp +12 -6
  496. package/src/duckdb/src/include/duckdb/common/string_util.hpp +37 -7
  497. package/src/duckdb/src/include/duckdb/common/tree_renderer/graphviz_tree_renderer.hpp +44 -0
  498. package/src/duckdb/src/include/duckdb/common/tree_renderer/html_tree_renderer.hpp +44 -0
  499. package/src/duckdb/src/include/duckdb/common/tree_renderer/json_tree_renderer.hpp +44 -0
  500. package/src/duckdb/src/include/duckdb/common/tree_renderer/text_tree_renderer.hpp +119 -0
  501. package/src/duckdb/src/include/duckdb/common/tree_renderer.hpp +9 -123
  502. package/src/duckdb/src/include/duckdb/common/type_visitor.hpp +96 -0
  503. package/src/duckdb/src/include/duckdb/common/typedefs.hpp +11 -1
  504. package/src/duckdb/src/include/duckdb/common/types/arrow_string_view_type.hpp +84 -0
  505. package/src/duckdb/src/include/duckdb/common/types/batched_data_collection.hpp +36 -1
  506. package/src/duckdb/src/include/duckdb/common/types/bit.hpp +1 -1
  507. package/src/duckdb/src/include/duckdb/common/types/cast_helpers.hpp +2 -2
  508. package/src/duckdb/src/include/duckdb/common/types/column/column_data_allocator.hpp +4 -2
  509. package/src/duckdb/src/include/duckdb/common/types/column/partitioned_column_data.hpp +52 -0
  510. package/src/duckdb/src/include/duckdb/common/types/data_chunk.hpp +2 -0
  511. package/src/duckdb/src/include/duckdb/common/types/date.hpp +0 -3
  512. package/src/duckdb/src/include/duckdb/common/types/date_lookup_cache.hpp +65 -0
  513. package/src/duckdb/src/include/duckdb/common/types/datetime.hpp +5 -2
  514. package/src/duckdb/src/include/duckdb/common/types/hyperloglog.hpp +49 -40
  515. package/src/duckdb/src/include/duckdb/common/types/interval.hpp +5 -1
  516. package/src/duckdb/src/include/duckdb/common/types/list_segment.hpp +2 -1
  517. package/src/duckdb/src/include/duckdb/common/types/row/partitioned_tuple_data.hpp +41 -9
  518. package/src/duckdb/src/include/duckdb/common/types/row/row_data_collection.hpp +4 -3
  519. package/src/duckdb/src/include/duckdb/common/types/row/row_data_collection_scanner.hpp +3 -1
  520. package/src/duckdb/src/include/duckdb/common/types/row/tuple_data_allocator.hpp +4 -0
  521. package/src/duckdb/src/include/duckdb/common/types/row/tuple_data_collection.hpp +4 -0
  522. package/src/duckdb/src/include/duckdb/common/types/row/tuple_data_states.hpp +1 -1
  523. package/src/duckdb/src/include/duckdb/common/types/selection_vector.hpp +4 -0
  524. package/src/duckdb/src/include/duckdb/common/types/string_type.hpp +4 -1
  525. package/src/duckdb/src/include/duckdb/common/types/time.hpp +11 -6
  526. package/src/duckdb/src/include/duckdb/common/types/timestamp.hpp +13 -3
  527. package/src/duckdb/src/include/duckdb/common/types/validity_mask.hpp +103 -12
  528. package/src/duckdb/src/include/duckdb/common/types/value.hpp +12 -3
  529. package/src/duckdb/src/include/duckdb/common/types/varint.hpp +107 -0
  530. package/src/duckdb/src/include/duckdb/common/types/vector.hpp +5 -1
  531. package/src/duckdb/src/include/duckdb/common/types/vector_buffer.hpp +7 -2
  532. package/src/duckdb/src/include/duckdb/common/types.hpp +6 -39
  533. package/src/duckdb/src/include/duckdb/common/union_by_name.hpp +42 -10
  534. package/src/duckdb/src/include/duckdb/common/vector_operations/generic_executor.hpp +29 -0
  535. package/src/duckdb/src/include/duckdb/common/vector_operations/unary_executor.hpp +0 -7
  536. package/src/duckdb/src/include/duckdb/common/vector_operations/vector_operations.hpp +2 -0
  537. package/src/duckdb/src/include/duckdb/common/winapi.hpp +8 -0
  538. package/src/duckdb/src/include/duckdb/core_functions/aggregate/algebraic/covar.hpp +8 -4
  539. package/src/duckdb/src/include/duckdb/core_functions/aggregate/algebraic/stddev.hpp +8 -4
  540. package/src/duckdb/src/include/duckdb/core_functions/aggregate/distributive_functions.hpp +4 -2
  541. package/src/duckdb/src/include/duckdb/core_functions/aggregate/histogram_helpers.hpp +99 -0
  542. package/src/duckdb/src/include/duckdb/core_functions/aggregate/holistic_functions.hpp +16 -7
  543. package/src/duckdb/src/include/duckdb/core_functions/aggregate/minmax_n_helpers.hpp +396 -0
  544. package/src/duckdb/src/include/duckdb/core_functions/aggregate/nested_functions.hpp +10 -0
  545. package/src/duckdb/src/include/duckdb/core_functions/aggregate/quantile_helpers.hpp +65 -0
  546. package/src/duckdb/src/include/duckdb/core_functions/aggregate/quantile_sort_tree.hpp +349 -0
  547. package/src/duckdb/src/include/duckdb/core_functions/aggregate/quantile_state.hpp +300 -0
  548. package/src/duckdb/src/include/duckdb/core_functions/aggregate/regression/regr_slope.hpp +1 -1
  549. package/src/duckdb/src/include/duckdb/core_functions/aggregate/sort_key_helpers.hpp +55 -0
  550. package/src/duckdb/src/include/duckdb/core_functions/array_kernels.hpp +107 -0
  551. package/src/duckdb/src/include/duckdb/core_functions/create_sort_key.hpp +55 -0
  552. package/src/duckdb/src/include/duckdb/core_functions/lambda_functions.hpp +1 -2
  553. package/src/duckdb/src/include/duckdb/core_functions/scalar/array_functions.hpp +24 -0
  554. package/src/duckdb/src/include/duckdb/core_functions/scalar/date_functions.hpp +9 -0
  555. package/src/duckdb/src/include/duckdb/core_functions/scalar/generic_functions.hpp +27 -0
  556. package/src/duckdb/src/include/duckdb/core_functions/scalar/list_functions.hpp +80 -8
  557. package/src/duckdb/src/include/duckdb/core_functions/scalar/map_functions.hpp +9 -0
  558. package/src/duckdb/src/include/duckdb/core_functions/scalar/math_functions.hpp +54 -0
  559. package/src/duckdb/src/include/duckdb/core_functions/scalar/string_functions.hpp +30 -21
  560. package/src/duckdb/src/include/duckdb/execution/adaptive_filter.hpp +25 -14
  561. package/src/duckdb/src/include/duckdb/execution/aggregate_hashtable.hpp +2 -48
  562. package/src/duckdb/src/include/duckdb/execution/executor.hpp +25 -2
  563. package/src/duckdb/src/include/duckdb/execution/ht_entry.hpp +102 -0
  564. package/src/duckdb/src/include/duckdb/execution/index/art/art.hpp +94 -101
  565. package/src/duckdb/src/include/duckdb/execution/index/art/art_key.hpp +43 -25
  566. package/src/duckdb/src/include/duckdb/execution/index/art/base_leaf.hpp +109 -0
  567. package/src/duckdb/src/include/duckdb/execution/index/art/base_node.hpp +140 -0
  568. package/src/duckdb/src/include/duckdb/execution/index/art/iterator.hpp +43 -24
  569. package/src/duckdb/src/include/duckdb/execution/index/art/leaf.hpp +41 -52
  570. package/src/duckdb/src/include/duckdb/execution/index/art/node.hpp +133 -74
  571. package/src/duckdb/src/include/duckdb/execution/index/art/node256.hpp +46 -29
  572. package/src/duckdb/src/include/duckdb/execution/index/art/node256_leaf.hpp +53 -0
  573. package/src/duckdb/src/include/duckdb/execution/index/art/node48.hpp +52 -35
  574. package/src/duckdb/src/include/duckdb/execution/index/art/prefix.hpp +96 -57
  575. package/src/duckdb/src/include/duckdb/execution/index/bound_index.hpp +9 -4
  576. package/src/duckdb/src/include/duckdb/execution/index/fixed_size_allocator.hpp +48 -10
  577. package/src/duckdb/src/include/duckdb/execution/index/fixed_size_buffer.hpp +0 -2
  578. package/src/duckdb/src/include/duckdb/execution/index/index_pointer.hpp +4 -2
  579. package/src/duckdb/src/include/duckdb/execution/join_hashtable.hpp +114 -36
  580. package/src/duckdb/src/include/duckdb/execution/merge_sort_tree.hpp +158 -67
  581. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/aggregate_object.hpp +1 -1
  582. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/physical_hash_aggregate.hpp +1 -1
  583. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/physical_perfecthash_aggregate.hpp +1 -1
  584. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/physical_streaming_window.hpp +19 -2
  585. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/physical_ungrouped_aggregate.hpp +1 -1
  586. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/physical_window.hpp +1 -1
  587. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/ungrouped_aggregate_state.hpp +75 -0
  588. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/base_scanner.hpp +81 -23
  589. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/column_count_scanner.hpp +27 -8
  590. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_buffer_manager.hpp +2 -1
  591. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_error.hpp +31 -22
  592. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_file_handle.hpp +4 -2
  593. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_file_scanner.hpp +48 -5
  594. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_option.hpp +7 -3
  595. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_reader_options.hpp +22 -12
  596. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_schema.hpp +35 -0
  597. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_sniffer.hpp +81 -39
  598. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_state.hpp +2 -1
  599. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_state_machine.hpp +18 -1
  600. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_state_machine_cache.hpp +9 -7
  601. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/global_csv_state.hpp +5 -4
  602. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/header_value.hpp +26 -0
  603. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/scanner_boundary.hpp +6 -9
  604. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/skip_scanner.hpp +3 -0
  605. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/state_machine_options.hpp +5 -3
  606. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/string_value_scanner.hpp +36 -19
  607. package/src/duckdb/src/include/duckdb/execution/operator/filter/physical_filter.hpp +1 -1
  608. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_batch_collector.hpp +21 -0
  609. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_buffered_batch_collector.hpp +53 -0
  610. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_buffered_collector.hpp +3 -0
  611. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_explain_analyze.hpp +6 -2
  612. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_materialized_collector.hpp +18 -0
  613. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_reservoir_sample.hpp +1 -1
  614. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_result_collector.hpp +6 -0
  615. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_set.hpp +2 -2
  616. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_set_variable.hpp +43 -0
  617. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_streaming_sample.hpp +1 -1
  618. package/src/duckdb/src/include/duckdb/execution/operator/join/join_filter_pushdown.hpp +59 -0
  619. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_blockwise_nl_join.hpp +1 -1
  620. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_comparison_join.hpp +8 -1
  621. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_delim_join.hpp +5 -2
  622. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_hash_join.hpp +4 -2
  623. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_iejoin.hpp +2 -0
  624. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_join.hpp +1 -1
  625. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_left_delim_join.hpp +3 -1
  626. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_range_join.hpp +4 -1
  627. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_right_delim_join.hpp +3 -1
  628. package/src/duckdb/src/include/duckdb/execution/operator/order/physical_order.hpp +1 -1
  629. package/src/duckdb/src/include/duckdb/execution/operator/order/physical_top_n.hpp +1 -1
  630. package/src/duckdb/src/include/duckdb/execution/operator/persistent/batch_memory_manager.hpp +5 -37
  631. package/src/duckdb/src/include/duckdb/execution/operator/persistent/physical_batch_copy_to_file.hpp +5 -4
  632. package/src/duckdb/src/include/duckdb/execution/operator/persistent/physical_copy_to_file.hpp +8 -2
  633. package/src/duckdb/src/include/duckdb/execution/operator/projection/physical_projection.hpp +1 -1
  634. package/src/duckdb/src/include/duckdb/execution/operator/projection/physical_tableinout_function.hpp +2 -0
  635. package/src/duckdb/src/include/duckdb/execution/operator/scan/physical_column_data_scan.hpp +9 -3
  636. package/src/duckdb/src/include/duckdb/execution/operator/scan/physical_table_scan.hpp +8 -6
  637. package/src/duckdb/src/include/duckdb/execution/operator/schema/physical_create_art_index.hpp +2 -2
  638. package/src/duckdb/src/include/duckdb/execution/operator/set/physical_cte.hpp +1 -1
  639. package/src/duckdb/src/include/duckdb/execution/operator/set/physical_recursive_cte.hpp +1 -1
  640. package/src/duckdb/src/include/duckdb/execution/physical_operator.hpp +21 -6
  641. package/src/duckdb/src/include/duckdb/execution/physical_operator_states.hpp +3 -2
  642. package/src/duckdb/src/include/duckdb/execution/physical_plan_generator.hpp +3 -0
  643. package/src/duckdb/src/include/duckdb/execution/window_executor.hpp +137 -110
  644. package/src/duckdb/src/include/duckdb/execution/window_segment_tree.hpp +57 -126
  645. package/src/duckdb/src/include/duckdb/function/aggregate_function.hpp +21 -4
  646. package/src/duckdb/src/include/duckdb/function/cast/default_casts.hpp +1 -1
  647. package/src/duckdb/src/include/duckdb/function/compression/compression.hpp +10 -10
  648. package/src/duckdb/src/include/duckdb/function/compression_function.hpp +37 -7
  649. package/src/duckdb/src/include/duckdb/function/copy_function.hpp +24 -11
  650. package/src/duckdb/src/include/duckdb/function/function_binder.hpp +4 -4
  651. package/src/duckdb/src/include/duckdb/function/function_serialization.hpp +41 -1
  652. package/src/duckdb/src/include/duckdb/function/macro_function.hpp +15 -5
  653. package/src/duckdb/src/include/duckdb/function/pragma/pragma_functions.hpp +1 -0
  654. package/src/duckdb/src/include/duckdb/function/replacement_scan.hpp +20 -4
  655. package/src/duckdb/src/include/duckdb/function/scalar/generic_functions.hpp +6 -0
  656. package/src/duckdb/src/include/duckdb/function/scalar/list/contains_or_position.hpp +77 -109
  657. package/src/duckdb/src/include/duckdb/function/scalar/nested_functions.hpp +1 -1
  658. package/src/duckdb/src/include/duckdb/function/scalar/regexp.hpp +6 -3
  659. package/src/duckdb/src/include/duckdb/function/scalar/strftime_format.hpp +25 -12
  660. package/src/duckdb/src/include/duckdb/function/scalar/string_functions.hpp +9 -8
  661. package/src/duckdb/src/include/duckdb/function/scalar_function.hpp +38 -4
  662. package/src/duckdb/src/include/duckdb/function/scalar_macro_function.hpp +1 -1
  663. package/src/duckdb/src/include/duckdb/function/table/arrow/arrow_duck_schema.hpp +11 -57
  664. package/src/duckdb/src/include/duckdb/function/table/arrow/arrow_type_info.hpp +142 -0
  665. package/src/duckdb/src/include/duckdb/function/table/arrow/enum/arrow_datetime_type.hpp +18 -0
  666. package/src/duckdb/src/include/duckdb/function/table/arrow/enum/arrow_type_info_type.hpp +7 -0
  667. package/src/duckdb/src/include/duckdb/function/table/arrow/enum/arrow_variable_size_type.hpp +10 -0
  668. package/src/duckdb/src/include/duckdb/function/table/arrow.hpp +2 -0
  669. package/src/duckdb/src/include/duckdb/function/table/range.hpp +4 -0
  670. package/src/duckdb/src/include/duckdb/function/table/read_csv.hpp +4 -1
  671. package/src/duckdb/src/include/duckdb/function/table/system_functions.hpp +4 -0
  672. package/src/duckdb/src/include/duckdb/function/table/table_scan.hpp +5 -5
  673. package/src/duckdb/src/include/duckdb/function/table_function.hpp +14 -2
  674. package/src/duckdb/src/include/duckdb/function/table_macro_function.hpp +1 -1
  675. package/src/duckdb/src/include/duckdb/main/appender.hpp +14 -4
  676. package/src/duckdb/src/include/duckdb/main/attached_database.hpp +25 -7
  677. package/src/duckdb/src/include/duckdb/main/buffered_data/batched_buffered_data.hpp +79 -0
  678. package/src/duckdb/src/include/duckdb/main/buffered_data/buffered_data.hpp +10 -20
  679. package/src/duckdb/src/include/duckdb/main/buffered_data/simple_buffered_data.hpp +11 -12
  680. package/src/duckdb/src/include/duckdb/main/capi/capi_internal.hpp +7 -2
  681. package/src/duckdb/src/include/duckdb/main/capi/cast/generic.hpp +1 -1
  682. package/src/duckdb/src/include/duckdb/main/capi/cast/utils.hpp +2 -2
  683. package/src/duckdb/src/include/duckdb/main/capi/extension_api.hpp +809 -0
  684. package/src/duckdb/src/include/duckdb/main/chunk_scan_state/batched_data_collection.hpp +35 -0
  685. package/src/duckdb/src/include/duckdb/main/client_config.hpp +68 -2
  686. package/src/duckdb/src/include/duckdb/main/client_context.hpp +30 -22
  687. package/src/duckdb/src/include/duckdb/main/client_context_state.hpp +79 -1
  688. package/src/duckdb/src/include/duckdb/main/client_properties.hpp +9 -3
  689. package/src/duckdb/src/include/duckdb/main/config.hpp +55 -7
  690. package/src/duckdb/src/include/duckdb/main/connection.hpp +5 -1
  691. package/src/duckdb/src/include/duckdb/main/database.hpp +16 -5
  692. package/src/duckdb/src/include/duckdb/main/database_manager.hpp +9 -8
  693. package/src/duckdb/src/include/duckdb/main/db_instance_cache.hpp +21 -6
  694. package/src/duckdb/src/include/duckdb/main/extension.hpp +20 -0
  695. package/src/duckdb/src/include/duckdb/main/extension_entries.hpp +25 -0
  696. package/src/duckdb/src/include/duckdb/main/extension_helper.hpp +29 -23
  697. package/src/duckdb/src/include/duckdb/main/extension_install_info.hpp +6 -0
  698. package/src/duckdb/src/include/duckdb/main/extension_util.hpp +3 -0
  699. package/src/duckdb/src/include/duckdb/main/pending_query_result.hpp +4 -2
  700. package/src/duckdb/src/include/duckdb/main/prepared_statement.hpp +5 -6
  701. package/src/duckdb/src/include/duckdb/main/prepared_statement_data.hpp +2 -5
  702. package/src/duckdb/src/include/duckdb/main/profiling_info.hpp +87 -0
  703. package/src/duckdb/src/include/duckdb/main/profiling_node.hpp +60 -0
  704. package/src/duckdb/src/include/duckdb/main/query_profiler.hpp +72 -34
  705. package/src/duckdb/src/include/duckdb/main/query_result.hpp +1 -1
  706. package/src/duckdb/src/include/duckdb/main/relation/create_table_relation.hpp +2 -1
  707. package/src/duckdb/src/include/duckdb/main/relation/delim_get_relation.hpp +30 -0
  708. package/src/duckdb/src/include/duckdb/main/relation/explain_relation.hpp +3 -1
  709. package/src/duckdb/src/include/duckdb/main/relation/join_relation.hpp +3 -0
  710. package/src/duckdb/src/include/duckdb/main/relation/materialized_relation.hpp +1 -4
  711. package/src/duckdb/src/include/duckdb/main/relation/query_relation.hpp +4 -1
  712. package/src/duckdb/src/include/duckdb/main/relation/read_json_relation.hpp +6 -0
  713. package/src/duckdb/src/include/duckdb/main/relation/table_function_relation.hpp +1 -0
  714. package/src/duckdb/src/include/duckdb/main/relation/view_relation.hpp +2 -0
  715. package/src/duckdb/src/include/duckdb/main/relation.hpp +7 -4
  716. package/src/duckdb/src/include/duckdb/main/secret/default_secrets.hpp +36 -0
  717. package/src/duckdb/src/include/duckdb/main/secret/secret.hpp +108 -0
  718. package/src/duckdb/src/include/duckdb/main/secret/secret_manager.hpp +14 -4
  719. package/src/duckdb/src/include/duckdb/main/settings.hpp +227 -3
  720. package/src/duckdb/src/include/duckdb/main/stream_query_result.hpp +8 -0
  721. package/src/duckdb/src/include/duckdb/optimizer/build_probe_side_optimizer.hpp +51 -0
  722. package/src/duckdb/src/include/duckdb/optimizer/compressed_materialization.hpp +7 -0
  723. package/src/duckdb/src/include/duckdb/optimizer/cte_filter_pusher.hpp +46 -0
  724. package/src/duckdb/src/include/duckdb/optimizer/filter_combiner.hpp +1 -1
  725. package/src/duckdb/src/include/duckdb/optimizer/filter_pushdown.hpp +7 -0
  726. package/src/duckdb/src/include/duckdb/optimizer/join_filter_pushdown_optimizer.hpp +31 -0
  727. package/src/duckdb/src/include/duckdb/optimizer/join_order/cardinality_estimator.hpp +51 -10
  728. package/src/duckdb/src/include/duckdb/optimizer/join_order/cost_model.hpp +1 -0
  729. package/src/duckdb/src/include/duckdb/optimizer/join_order/join_order_optimizer.hpp +17 -5
  730. package/src/duckdb/src/include/duckdb/optimizer/join_order/query_graph.hpp +1 -1
  731. package/src/duckdb/src/include/duckdb/optimizer/join_order/query_graph_manager.hpp +15 -13
  732. package/src/duckdb/src/include/duckdb/optimizer/join_order/relation_manager.hpp +9 -4
  733. package/src/duckdb/src/include/duckdb/optimizer/limit_pushdown.hpp +25 -0
  734. package/src/duckdb/src/include/duckdb/optimizer/optimizer.hpp +1 -0
  735. package/src/duckdb/src/include/duckdb/optimizer/rule/join_dependent_filter.hpp +37 -0
  736. package/src/duckdb/src/include/duckdb/parallel/executor_task.hpp +6 -1
  737. package/src/duckdb/src/include/duckdb/parallel/interrupt.hpp +54 -2
  738. package/src/duckdb/src/include/duckdb/parallel/meta_pipeline.hpp +27 -8
  739. package/src/duckdb/src/include/duckdb/parallel/pipeline.hpp +1 -0
  740. package/src/duckdb/src/include/duckdb/parallel/pipeline_prepare_finish_event.hpp +25 -0
  741. package/src/duckdb/src/include/duckdb/parallel/task_executor.hpp +63 -0
  742. package/src/duckdb/src/include/duckdb/parallel/task_scheduler.hpp +10 -1
  743. package/src/duckdb/src/include/duckdb/parser/expression/function_expression.hpp +4 -1
  744. package/src/duckdb/src/include/duckdb/parser/expression/star_expression.hpp +5 -0
  745. package/src/duckdb/src/include/duckdb/parser/parsed_data/alter_table_info.hpp +5 -0
  746. package/src/duckdb/src/include/duckdb/parser/parsed_data/attach_info.hpp +5 -0
  747. package/src/duckdb/src/include/duckdb/parser/parsed_data/create_index_info.hpp +2 -0
  748. package/src/duckdb/src/include/duckdb/parser/parsed_data/create_macro_info.hpp +11 -1
  749. package/src/duckdb/src/include/duckdb/parser/parsed_data/transaction_info.hpp +9 -0
  750. package/src/duckdb/src/include/duckdb/parser/parsed_expression_iterator.hpp +13 -6
  751. package/src/duckdb/src/include/duckdb/parser/parser_extension.hpp +1 -1
  752. package/src/duckdb/src/include/duckdb/parser/sql_statement.hpp +1 -3
  753. package/src/duckdb/src/include/duckdb/parser/statement/copy_statement.hpp +2 -0
  754. package/src/duckdb/src/include/duckdb/parser/statement/explain_statement.hpp +5 -1
  755. package/src/duckdb/src/include/duckdb/parser/statement/set_statement.hpp +2 -2
  756. package/src/duckdb/src/include/duckdb/parser/statement/transaction_statement.hpp +1 -1
  757. package/src/duckdb/src/include/duckdb/parser/tableref/basetableref.hpp +0 -2
  758. package/src/duckdb/src/include/duckdb/parser/tableref/column_data_ref.hpp +9 -7
  759. package/src/duckdb/src/include/duckdb/parser/tableref/delimgetref.hpp +37 -0
  760. package/src/duckdb/src/include/duckdb/parser/tableref/joinref.hpp +4 -0
  761. package/src/duckdb/src/include/duckdb/parser/tableref/pivotref.hpp +0 -2
  762. package/src/duckdb/src/include/duckdb/parser/tableref/subqueryref.hpp +0 -2
  763. package/src/duckdb/src/include/duckdb/parser/tableref/table_function_ref.hpp +0 -1
  764. package/src/duckdb/src/include/duckdb/parser/tableref.hpp +3 -1
  765. package/src/duckdb/src/include/duckdb/parser/transformer.hpp +17 -9
  766. package/src/duckdb/src/include/duckdb/planner/binder.hpp +24 -14
  767. package/src/duckdb/src/include/duckdb/planner/collation_binding.hpp +44 -0
  768. package/src/duckdb/src/include/duckdb/planner/expression/bound_aggregate_expression.hpp +1 -1
  769. package/src/duckdb/src/include/duckdb/planner/expression/bound_between_expression.hpp +1 -1
  770. package/src/duckdb/src/include/duckdb/planner/expression/bound_case_expression.hpp +1 -1
  771. package/src/duckdb/src/include/duckdb/planner/expression/bound_cast_expression.hpp +1 -1
  772. package/src/duckdb/src/include/duckdb/planner/expression/bound_columnref_expression.hpp +1 -1
  773. package/src/duckdb/src/include/duckdb/planner/expression/bound_comparison_expression.hpp +1 -1
  774. package/src/duckdb/src/include/duckdb/planner/expression/bound_conjunction_expression.hpp +1 -1
  775. package/src/duckdb/src/include/duckdb/planner/expression/bound_constant_expression.hpp +1 -1
  776. package/src/duckdb/src/include/duckdb/planner/expression/bound_default_expression.hpp +1 -1
  777. package/src/duckdb/src/include/duckdb/planner/expression/bound_expanded_expression.hpp +1 -1
  778. package/src/duckdb/src/include/duckdb/planner/expression/bound_function_expression.hpp +1 -1
  779. package/src/duckdb/src/include/duckdb/planner/expression/bound_lambda_expression.hpp +1 -1
  780. package/src/duckdb/src/include/duckdb/planner/expression/bound_lambdaref_expression.hpp +1 -1
  781. package/src/duckdb/src/include/duckdb/planner/expression/bound_operator_expression.hpp +1 -1
  782. package/src/duckdb/src/include/duckdb/planner/expression/bound_parameter_data.hpp +2 -0
  783. package/src/duckdb/src/include/duckdb/planner/expression/bound_parameter_expression.hpp +1 -1
  784. package/src/duckdb/src/include/duckdb/planner/expression/bound_reference_expression.hpp +1 -1
  785. package/src/duckdb/src/include/duckdb/planner/expression/bound_subquery_expression.hpp +2 -2
  786. package/src/duckdb/src/include/duckdb/planner/expression/bound_unnest_expression.hpp +1 -1
  787. package/src/duckdb/src/include/duckdb/planner/expression/bound_window_expression.hpp +1 -1
  788. package/src/duckdb/src/include/duckdb/planner/expression.hpp +2 -2
  789. package/src/duckdb/src/include/duckdb/planner/expression_binder/column_alias_binder.hpp +2 -0
  790. package/src/duckdb/src/include/duckdb/planner/expression_binder/group_binder.hpp +1 -0
  791. package/src/duckdb/src/include/duckdb/planner/expression_binder/order_binder.hpp +6 -5
  792. package/src/duckdb/src/include/duckdb/planner/expression_binder/where_binder.hpp +1 -0
  793. package/src/duckdb/src/include/duckdb/planner/expression_binder.hpp +19 -11
  794. package/src/duckdb/src/include/duckdb/planner/filter/conjunction_filter.hpp +4 -0
  795. package/src/duckdb/src/include/duckdb/planner/filter/constant_filter.hpp +2 -0
  796. package/src/duckdb/src/include/duckdb/planner/filter/null_filter.hpp +4 -0
  797. package/src/duckdb/src/include/duckdb/planner/filter/struct_filter.hpp +2 -0
  798. package/src/duckdb/src/include/duckdb/planner/logical_operator.hpp +7 -2
  799. package/src/duckdb/src/include/duckdb/planner/logical_operator_visitor.hpp +2 -1
  800. package/src/duckdb/src/include/duckdb/planner/operator/logical_aggregate.hpp +1 -1
  801. package/src/duckdb/src/include/duckdb/planner/operator/logical_any_join.hpp +1 -1
  802. package/src/duckdb/src/include/duckdb/planner/operator/logical_comparison_join.hpp +6 -1
  803. package/src/duckdb/src/include/duckdb/planner/operator/logical_copy_to_file.hpp +10 -2
  804. package/src/duckdb/src/include/duckdb/planner/operator/logical_cteref.hpp +1 -0
  805. package/src/duckdb/src/include/duckdb/planner/operator/logical_delim_get.hpp +1 -1
  806. package/src/duckdb/src/include/duckdb/planner/operator/logical_distinct.hpp +1 -1
  807. package/src/duckdb/src/include/duckdb/planner/operator/logical_execute.hpp +1 -1
  808. package/src/duckdb/src/include/duckdb/planner/operator/logical_explain.hpp +4 -2
  809. package/src/duckdb/src/include/duckdb/planner/operator/logical_get.hpp +15 -5
  810. package/src/duckdb/src/include/duckdb/planner/operator/logical_materialized_cte.hpp +1 -0
  811. package/src/duckdb/src/include/duckdb/planner/operator/logical_order.hpp +1 -1
  812. package/src/duckdb/src/include/duckdb/planner/subquery/flatten_dependent_join.hpp +2 -1
  813. package/src/duckdb/src/include/duckdb/planner/table_filter.hpp +24 -2
  814. package/src/duckdb/src/include/duckdb/planner/tableref/bound_delimgetref.hpp +26 -0
  815. package/src/duckdb/src/include/duckdb/planner/tableref/bound_joinref.hpp +6 -0
  816. package/src/duckdb/src/include/duckdb/planner/tableref/bound_subqueryref.hpp +1 -1
  817. package/src/duckdb/src/include/duckdb/planner/tableref/bound_table_function.hpp +2 -0
  818. package/src/duckdb/src/include/duckdb/planner/tableref/list.hpp +2 -0
  819. package/src/duckdb/src/include/duckdb/storage/arena_allocator.hpp +2 -1
  820. package/src/duckdb/src/include/duckdb/storage/block.hpp +4 -2
  821. package/src/duckdb/src/include/duckdb/storage/block_manager.hpp +48 -3
  822. package/src/duckdb/src/include/duckdb/storage/buffer/block_handle.hpp +21 -7
  823. package/src/duckdb/src/include/duckdb/storage/buffer/buffer_pool.hpp +65 -51
  824. package/src/duckdb/src/include/duckdb/storage/buffer_manager.hpp +14 -5
  825. package/src/duckdb/src/include/duckdb/storage/checkpoint/row_group_writer.hpp +0 -4
  826. package/src/duckdb/src/include/duckdb/storage/checkpoint/string_checkpoint_state.hpp +3 -2
  827. package/src/duckdb/src/include/duckdb/storage/checkpoint/table_data_writer.hpp +1 -0
  828. package/src/duckdb/src/include/duckdb/storage/checkpoint/write_overflow_strings_to_disk.hpp +3 -4
  829. package/src/duckdb/src/include/duckdb/storage/checkpoint_manager.hpp +2 -0
  830. package/src/duckdb/src/include/duckdb/storage/compression/alp/algorithm/alp.hpp +4 -4
  831. package/src/duckdb/src/include/duckdb/storage/compression/alp/alp_analyze.hpp +6 -4
  832. package/src/duckdb/src/include/duckdb/storage/compression/alp/alp_compress.hpp +19 -17
  833. package/src/duckdb/src/include/duckdb/storage/compression/alp/alp_constants.hpp +2 -2
  834. package/src/duckdb/src/include/duckdb/storage/compression/alp/alp_scan.hpp +3 -4
  835. package/src/duckdb/src/include/duckdb/storage/compression/alp/alp_utils.hpp +3 -2
  836. package/src/duckdb/src/include/duckdb/storage/compression/alprd/algorithm/alprd.hpp +3 -2
  837. package/src/duckdb/src/include/duckdb/storage/compression/alprd/alprd_analyze.hpp +13 -11
  838. package/src/duckdb/src/include/duckdb/storage/compression/alprd/alprd_compress.hpp +19 -19
  839. package/src/duckdb/src/include/duckdb/storage/compression/alprd/alprd_scan.hpp +3 -4
  840. package/src/duckdb/src/include/duckdb/storage/compression/chimp/chimp_scan.hpp +1 -1
  841. package/src/duckdb/src/include/duckdb/storage/compression/patas/patas_scan.hpp +1 -1
  842. package/src/duckdb/src/include/duckdb/storage/data_pointer.hpp +10 -2
  843. package/src/duckdb/src/include/duckdb/storage/data_table.hpp +3 -2
  844. package/src/duckdb/src/include/duckdb/storage/in_memory_block_manager.hpp +15 -0
  845. package/src/duckdb/src/include/duckdb/storage/index_storage_info.hpp +14 -10
  846. package/src/duckdb/src/include/duckdb/storage/metadata/metadata_manager.hpp +6 -8
  847. package/src/duckdb/src/include/duckdb/storage/partial_block_manager.hpp +7 -4
  848. package/src/duckdb/src/include/duckdb/storage/segment/uncompressed.hpp +4 -7
  849. package/src/duckdb/src/include/duckdb/storage/single_file_block_manager.hpp +29 -4
  850. package/src/duckdb/src/include/duckdb/storage/standard_buffer_manager.hpp +22 -7
  851. package/src/duckdb/src/include/duckdb/storage/statistics/base_statistics.hpp +15 -2
  852. package/src/duckdb/src/include/duckdb/storage/statistics/distinct_statistics.hpp +8 -2
  853. package/src/duckdb/src/include/duckdb/storage/statistics/numeric_stats.hpp +5 -16
  854. package/src/duckdb/src/include/duckdb/storage/statistics/numeric_stats_union.hpp +51 -13
  855. package/src/duckdb/src/include/duckdb/storage/statistics/string_stats.hpp +6 -3
  856. package/src/duckdb/src/include/duckdb/storage/storage_info.hpp +29 -19
  857. package/src/duckdb/src/include/duckdb/storage/storage_manager.hpp +23 -7
  858. package/src/duckdb/src/include/duckdb/storage/string_uncompressed.hpp +27 -18
  859. package/src/duckdb/src/include/duckdb/storage/table/append_state.hpp +6 -3
  860. package/src/duckdb/src/include/duckdb/storage/table/array_column_data.hpp +5 -2
  861. package/src/duckdb/src/include/duckdb/storage/table/chunk_info.hpp +3 -0
  862. package/src/duckdb/src/include/duckdb/storage/table/column_checkpoint_state.hpp +5 -1
  863. package/src/duckdb/src/include/duckdb/storage/table/column_data.hpp +77 -6
  864. package/src/duckdb/src/include/duckdb/storage/table/column_segment.hpp +23 -11
  865. package/src/duckdb/src/include/duckdb/storage/table/data_table_info.hpp +3 -0
  866. package/src/duckdb/src/include/duckdb/storage/table/list_column_data.hpp +5 -2
  867. package/src/duckdb/src/include/duckdb/storage/table/row_group.hpp +18 -4
  868. package/src/duckdb/src/include/duckdb/storage/table/row_group_collection.hpp +7 -1
  869. package/src/duckdb/src/include/duckdb/storage/table/row_version_manager.hpp +2 -1
  870. package/src/duckdb/src/include/duckdb/storage/table/scan_state.hpp +89 -14
  871. package/src/duckdb/src/include/duckdb/storage/table/standard_column_data.hpp +4 -2
  872. package/src/duckdb/src/include/duckdb/storage/table/struct_column_data.hpp +4 -2
  873. package/src/duckdb/src/include/duckdb/storage/table/table_index_list.hpp +2 -2
  874. package/src/duckdb/src/include/duckdb/storage/table/validity_column_data.hpp +1 -1
  875. package/src/duckdb/src/include/duckdb/storage/temporary_memory_manager.hpp +33 -15
  876. package/src/duckdb/src/include/duckdb/storage/write_ahead_log.hpp +9 -9
  877. package/src/duckdb/src/include/duckdb/transaction/cleanup_state.hpp +3 -1
  878. package/src/duckdb/src/include/duckdb/transaction/commit_state.hpp +4 -16
  879. package/src/duckdb/src/include/duckdb/transaction/duck_transaction.hpp +27 -4
  880. package/src/duckdb/src/include/duckdb/transaction/duck_transaction_manager.hpp +11 -0
  881. package/src/duckdb/src/include/duckdb/transaction/local_storage.hpp +6 -2
  882. package/src/duckdb/src/include/duckdb/transaction/meta_transaction.hpp +5 -5
  883. package/src/duckdb/src/include/duckdb/transaction/transaction_context.hpp +6 -2
  884. package/src/duckdb/src/include/duckdb/transaction/undo_buffer.hpp +5 -3
  885. package/src/duckdb/src/include/duckdb/transaction/wal_write_state.hpp +48 -0
  886. package/src/duckdb/src/include/duckdb.h +1779 -739
  887. package/src/duckdb/src/include/duckdb_extension.h +921 -0
  888. package/src/duckdb/src/main/appender.cpp +53 -7
  889. package/src/duckdb/src/main/attached_database.cpp +87 -17
  890. package/src/duckdb/src/main/buffered_data/batched_buffered_data.cpp +226 -0
  891. package/src/duckdb/src/main/buffered_data/buffered_data.cpp +35 -0
  892. package/src/duckdb/src/main/buffered_data/simple_buffered_data.cpp +48 -23
  893. package/src/duckdb/src/main/capi/aggregate_function-c.cpp +327 -0
  894. package/src/duckdb/src/main/capi/appender-c.cpp +18 -0
  895. package/src/duckdb/src/main/capi/cast/utils-c.cpp +2 -2
  896. package/src/duckdb/src/main/capi/cast_function-c.cpp +210 -0
  897. package/src/duckdb/src/main/capi/config-c.cpp +3 -3
  898. package/src/duckdb/src/main/capi/data_chunk-c.cpp +18 -7
  899. package/src/duckdb/src/main/capi/duckdb_value-c.cpp +223 -24
  900. package/src/duckdb/src/main/capi/helper-c.cpp +51 -11
  901. package/src/duckdb/src/main/capi/logical_types-c.cpp +105 -46
  902. package/src/duckdb/src/main/capi/pending-c.cpp +7 -6
  903. package/src/duckdb/src/main/capi/prepared-c.cpp +18 -7
  904. package/src/duckdb/src/main/capi/profiling_info-c.cpp +84 -0
  905. package/src/duckdb/src/main/capi/result-c.cpp +139 -37
  906. package/src/duckdb/src/main/capi/scalar_function-c.cpp +269 -0
  907. package/src/duckdb/src/main/capi/table_description-c.cpp +82 -0
  908. package/src/duckdb/src/main/capi/table_function-c.cpp +161 -95
  909. package/src/duckdb/src/main/capi/value-c.cpp +2 -2
  910. package/src/duckdb/src/main/chunk_scan_state/batched_data_collection.cpp +57 -0
  911. package/src/duckdb/src/main/client_config.cpp +17 -0
  912. package/src/duckdb/src/main/client_context.cpp +67 -52
  913. package/src/duckdb/src/main/client_data.cpp +3 -3
  914. package/src/duckdb/src/main/config.cpp +120 -62
  915. package/src/duckdb/src/main/connection.cpp +14 -2
  916. package/src/duckdb/src/main/database.cpp +96 -35
  917. package/src/duckdb/src/main/database_manager.cpp +25 -23
  918. package/src/duckdb/src/main/database_path_and_type.cpp +2 -2
  919. package/src/duckdb/src/main/db_instance_cache.cpp +54 -19
  920. package/src/duckdb/src/main/extension/extension_helper.cpp +47 -42
  921. package/src/duckdb/src/main/extension/extension_install.cpp +155 -87
  922. package/src/duckdb/src/main/extension/extension_load.cpp +180 -26
  923. package/src/duckdb/src/main/extension/extension_util.cpp +8 -0
  924. package/src/duckdb/src/main/extension.cpp +72 -5
  925. package/src/duckdb/src/main/pending_query_result.cpp +20 -12
  926. package/src/duckdb/src/main/prepared_statement.cpp +6 -6
  927. package/src/duckdb/src/main/prepared_statement_data.cpp +28 -17
  928. package/src/duckdb/src/main/profiling_info.cpp +196 -0
  929. package/src/duckdb/src/main/query_profiler.cpp +413 -224
  930. package/src/duckdb/src/main/query_result.cpp +1 -1
  931. package/src/duckdb/src/main/relation/create_table_relation.cpp +4 -2
  932. package/src/duckdb/src/main/relation/create_view_relation.cpp +0 -6
  933. package/src/duckdb/src/main/relation/delim_get_relation.cpp +44 -0
  934. package/src/duckdb/src/main/relation/explain_relation.cpp +4 -3
  935. package/src/duckdb/src/main/relation/join_relation.cpp +5 -0
  936. package/src/duckdb/src/main/relation/limit_relation.cpp +1 -1
  937. package/src/duckdb/src/main/relation/materialized_relation.cpp +3 -3
  938. package/src/duckdb/src/main/relation/query_relation.cpp +42 -15
  939. package/src/duckdb/src/main/relation/read_csv_relation.cpp +7 -14
  940. package/src/duckdb/src/main/relation/read_json_relation.cpp +20 -0
  941. package/src/duckdb/src/main/relation/setop_relation.cpp +1 -1
  942. package/src/duckdb/src/main/relation/table_function_relation.cpp +6 -0
  943. package/src/duckdb/src/main/relation/view_relation.cpp +10 -0
  944. package/src/duckdb/src/main/relation.cpp +12 -8
  945. package/src/duckdb/src/main/secret/default_secrets.cpp +108 -0
  946. package/src/duckdb/src/main/secret/secret.cpp +145 -2
  947. package/src/duckdb/src/main/secret/secret_manager.cpp +85 -35
  948. package/src/duckdb/src/main/secret/secret_storage.cpp +29 -17
  949. package/src/duckdb/src/main/settings/settings.cpp +503 -11
  950. package/src/duckdb/src/main/stream_query_result.cpp +75 -2
  951. package/src/duckdb/src/optimizer/build_probe_side_optimizer.cpp +248 -0
  952. package/src/duckdb/src/optimizer/column_lifetime_analyzer.cpp +28 -6
  953. package/src/duckdb/src/optimizer/compressed_materialization/compress_comparison_join.cpp +152 -0
  954. package/src/duckdb/src/optimizer/compressed_materialization.cpp +11 -1
  955. package/src/duckdb/src/optimizer/cse_optimizer.cpp +3 -0
  956. package/src/duckdb/src/optimizer/cte_filter_pusher.cpp +117 -0
  957. package/src/duckdb/src/optimizer/filter_combiner.cpp +30 -9
  958. package/src/duckdb/src/optimizer/filter_pullup.cpp +54 -2
  959. package/src/duckdb/src/optimizer/filter_pushdown.cpp +71 -3
  960. package/src/duckdb/src/optimizer/join_filter_pushdown_optimizer.cpp +154 -0
  961. package/src/duckdb/src/optimizer/join_order/cardinality_estimator.cpp +245 -114
  962. package/src/duckdb/src/optimizer/join_order/join_order_optimizer.cpp +42 -20
  963. package/src/duckdb/src/optimizer/join_order/join_relation_set.cpp +6 -2
  964. package/src/duckdb/src/optimizer/join_order/plan_enumerator.cpp +32 -10
  965. package/src/duckdb/src/optimizer/join_order/query_graph_manager.cpp +97 -131
  966. package/src/duckdb/src/optimizer/join_order/relation_manager.cpp +265 -51
  967. package/src/duckdb/src/optimizer/join_order/relation_statistics_helper.cpp +21 -17
  968. package/src/duckdb/src/optimizer/limit_pushdown.cpp +42 -0
  969. package/src/duckdb/src/optimizer/optimizer.cpp +51 -8
  970. package/src/duckdb/src/optimizer/pushdown/pushdown_aggregate.cpp +17 -17
  971. package/src/duckdb/src/optimizer/pushdown/pushdown_cross_product.cpp +22 -4
  972. package/src/duckdb/src/optimizer/pushdown/pushdown_get.cpp +1 -18
  973. package/src/duckdb/src/optimizer/pushdown/pushdown_inner_join.cpp +6 -0
  974. package/src/duckdb/src/optimizer/pushdown/pushdown_mark_join.cpp +4 -2
  975. package/src/duckdb/src/optimizer/pushdown/pushdown_window.cpp +91 -0
  976. package/src/duckdb/src/optimizer/remove_unused_columns.cpp +21 -25
  977. package/src/duckdb/src/optimizer/rule/comparison_simplification.cpp +1 -0
  978. package/src/duckdb/src/optimizer/rule/empty_needle_removal.cpp +3 -0
  979. package/src/duckdb/src/optimizer/rule/equal_or_null_simplification.cpp +2 -2
  980. package/src/duckdb/src/optimizer/rule/in_clause_simplification_rule.cpp +8 -2
  981. package/src/duckdb/src/optimizer/rule/join_dependent_filter.cpp +135 -0
  982. package/src/duckdb/src/optimizer/rule/like_optimizations.cpp +1 -1
  983. package/src/duckdb/src/optimizer/rule/regex_optimizations.cpp +1 -1
  984. package/src/duckdb/src/optimizer/statistics/operator/propagate_filter.cpp +6 -1
  985. package/src/duckdb/src/optimizer/statistics/operator/propagate_get.cpp +7 -6
  986. package/src/duckdb/src/optimizer/statistics/operator/propagate_join.cpp +1 -1
  987. package/src/duckdb/src/optimizer/topn_optimizer.cpp +46 -7
  988. package/src/duckdb/src/parallel/executor.cpp +129 -51
  989. package/src/duckdb/src/parallel/executor_task.cpp +16 -3
  990. package/src/duckdb/src/parallel/meta_pipeline.cpp +98 -29
  991. package/src/duckdb/src/parallel/pipeline.cpp +17 -3
  992. package/src/duckdb/src/parallel/pipeline_executor.cpp +14 -2
  993. package/src/duckdb/src/parallel/pipeline_prepare_finish_event.cpp +34 -0
  994. package/src/duckdb/src/parallel/task_executor.cpp +84 -0
  995. package/src/duckdb/src/parallel/task_scheduler.cpp +94 -16
  996. package/src/duckdb/src/parallel/thread_context.cpp +1 -1
  997. package/src/duckdb/src/parser/expression/function_expression.cpp +14 -0
  998. package/src/duckdb/src/parser/expression/star_expression.cpp +35 -2
  999. package/src/duckdb/src/parser/parsed_data/alter_table_info.cpp +5 -1
  1000. package/src/duckdb/src/parser/parsed_data/attach_info.cpp +17 -0
  1001. package/src/duckdb/src/parser/parsed_data/create_index_info.cpp +37 -28
  1002. package/src/duckdb/src/parser/parsed_data/create_macro_info.cpp +44 -2
  1003. package/src/duckdb/src/parser/parsed_data/transaction_info.cpp +21 -1
  1004. package/src/duckdb/src/parser/parsed_expression_iterator.cpp +29 -25
  1005. package/src/duckdb/src/parser/parser.cpp +41 -1
  1006. package/src/duckdb/src/parser/query_node/recursive_cte_node.cpp +1 -0
  1007. package/src/duckdb/src/parser/statement/explain_statement.cpp +28 -13
  1008. package/src/duckdb/src/parser/statement/relation_statement.cpp +5 -0
  1009. package/src/duckdb/src/parser/statement/set_statement.cpp +4 -2
  1010. package/src/duckdb/src/parser/statement/transaction_statement.cpp +3 -3
  1011. package/src/duckdb/src/parser/tableref/column_data_ref.cpp +1 -27
  1012. package/src/duckdb/src/parser/tableref/delimgetref.cpp +30 -0
  1013. package/src/duckdb/src/parser/tableref/joinref.cpp +4 -0
  1014. package/src/duckdb/src/parser/transform/constraint/transform_constraint.cpp +35 -29
  1015. package/src/duckdb/src/parser/transform/expression/transform_array_access.cpp +32 -32
  1016. package/src/duckdb/src/parser/transform/expression/transform_columnref.cpp +2 -1
  1017. package/src/duckdb/src/parser/transform/expression/transform_constant.cpp +17 -0
  1018. package/src/duckdb/src/parser/transform/expression/transform_function.cpp +5 -0
  1019. package/src/duckdb/src/parser/transform/expression/transform_multi_assign_reference.cpp +36 -34
  1020. package/src/duckdb/src/parser/transform/expression/transform_operator.cpp +30 -14
  1021. package/src/duckdb/src/parser/transform/expression/transform_subquery.cpp +1 -1
  1022. package/src/duckdb/src/parser/transform/helpers/transform_alias.cpp +2 -1
  1023. package/src/duckdb/src/parser/transform/helpers/transform_cte.cpp +27 -19
  1024. package/src/duckdb/src/parser/transform/helpers/transform_orderby.cpp +31 -28
  1025. package/src/duckdb/src/parser/transform/statement/transform_alter_table.cpp +25 -27
  1026. package/src/duckdb/src/parser/transform/statement/transform_copy.cpp +1 -1
  1027. package/src/duckdb/src/parser/transform/statement/transform_create_function.cpp +53 -42
  1028. package/src/duckdb/src/parser/transform/statement/transform_create_table.cpp +6 -6
  1029. package/src/duckdb/src/parser/transform/statement/transform_create_table_as.cpp +1 -1
  1030. package/src/duckdb/src/parser/transform/statement/transform_create_type.cpp +1 -1
  1031. package/src/duckdb/src/parser/transform/statement/transform_create_view.cpp +1 -1
  1032. package/src/duckdb/src/parser/transform/statement/transform_explain.cpp +38 -3
  1033. package/src/duckdb/src/parser/transform/statement/transform_insert.cpp +1 -2
  1034. package/src/duckdb/src/parser/transform/statement/transform_pivot_stmt.cpp +1 -1
  1035. package/src/duckdb/src/parser/transform/statement/transform_prepare.cpp +1 -1
  1036. package/src/duckdb/src/parser/transform/statement/transform_select.cpp +26 -21
  1037. package/src/duckdb/src/parser/transform/statement/transform_set.cpp +8 -8
  1038. package/src/duckdb/src/parser/transform/statement/transform_show.cpp +5 -2
  1039. package/src/duckdb/src/parser/transform/statement/transform_show_select.cpp +6 -4
  1040. package/src/duckdb/src/parser/transform/statement/transform_transaction.cpp +27 -6
  1041. package/src/duckdb/src/parser/transform/statement/transform_update.cpp +8 -9
  1042. package/src/duckdb/src/parser/transform/statement/transform_upsert.cpp +11 -12
  1043. package/src/duckdb/src/parser/transform/statement/transform_vacuum.cpp +3 -3
  1044. package/src/duckdb/src/parser/transform/tableref/transform_join.cpp +16 -10
  1045. package/src/duckdb/src/parser/transform/tableref/transform_pivot.cpp +1 -1
  1046. package/src/duckdb/src/parser/transform/tableref/transform_subquery.cpp +1 -1
  1047. package/src/duckdb/src/parser/transformer.cpp +11 -7
  1048. package/src/duckdb/src/planner/bind_context.cpp +3 -3
  1049. package/src/duckdb/src/planner/binder/expression/bind_aggregate_expression.cpp +22 -7
  1050. package/src/duckdb/src/planner/binder/expression/bind_between_expression.cpp +3 -3
  1051. package/src/duckdb/src/planner/binder/expression/bind_collate_expression.cpp +3 -2
  1052. package/src/duckdb/src/planner/binder/expression/bind_columnref_expression.cpp +11 -4
  1053. package/src/duckdb/src/planner/binder/expression/bind_comparison_expression.cpp +9 -54
  1054. package/src/duckdb/src/planner/binder/expression/bind_function_expression.cpp +3 -5
  1055. package/src/duckdb/src/planner/binder/expression/bind_macro_expression.cpp +24 -27
  1056. package/src/duckdb/src/planner/binder/expression/bind_operator_expression.cpp +7 -7
  1057. package/src/duckdb/src/planner/binder/expression/bind_parameter_expression.cpp +9 -2
  1058. package/src/duckdb/src/planner/binder/expression/bind_star_expression.cpp +26 -7
  1059. package/src/duckdb/src/planner/binder/expression/bind_unnest_expression.cpp +5 -0
  1060. package/src/duckdb/src/planner/binder/expression/bind_unpacked_star_expression.cpp +91 -0
  1061. package/src/duckdb/src/planner/binder/expression/bind_window_expression.cpp +2 -2
  1062. package/src/duckdb/src/planner/binder/query_node/bind_select_node.cpp +11 -8
  1063. package/src/duckdb/src/planner/binder/query_node/bind_setop_node.cpp +1 -1
  1064. package/src/duckdb/src/planner/binder/query_node/bind_table_macro_node.cpp +6 -10
  1065. package/src/duckdb/src/planner/binder/query_node/plan_cte_node.cpp +14 -10
  1066. package/src/duckdb/src/planner/binder/query_node/plan_setop.cpp +3 -3
  1067. package/src/duckdb/src/planner/binder/query_node/plan_subquery.cpp +46 -7
  1068. package/src/duckdb/src/planner/binder/statement/bind_call.cpp +13 -20
  1069. package/src/duckdb/src/planner/binder/statement/bind_copy.cpp +105 -13
  1070. package/src/duckdb/src/planner/binder/statement/bind_copy_database.cpp +7 -3
  1071. package/src/duckdb/src/planner/binder/statement/bind_create.cpp +75 -55
  1072. package/src/duckdb/src/planner/binder/statement/bind_create_table.cpp +1 -1
  1073. package/src/duckdb/src/planner/binder/statement/bind_delete.cpp +5 -4
  1074. package/src/duckdb/src/planner/binder/statement/bind_drop.cpp +2 -2
  1075. package/src/duckdb/src/planner/binder/statement/bind_execute.cpp +24 -8
  1076. package/src/duckdb/src/planner/binder/statement/bind_explain.cpp +2 -2
  1077. package/src/duckdb/src/planner/binder/statement/bind_export.cpp +5 -105
  1078. package/src/duckdb/src/planner/binder/statement/bind_extension.cpp +2 -2
  1079. package/src/duckdb/src/planner/binder/statement/bind_insert.cpp +109 -41
  1080. package/src/duckdb/src/planner/binder/statement/bind_set.cpp +23 -7
  1081. package/src/duckdb/src/planner/binder/statement/bind_simple.cpp +4 -1
  1082. package/src/duckdb/src/planner/binder/statement/bind_summarize.cpp +17 -3
  1083. package/src/duckdb/src/planner/binder/statement/bind_update.cpp +5 -4
  1084. package/src/duckdb/src/planner/binder/statement/bind_vacuum.cpp +8 -6
  1085. package/src/duckdb/src/planner/binder/tableref/bind_basetableref.cpp +55 -42
  1086. package/src/duckdb/src/planner/binder/tableref/bind_column_data_ref.cpp +3 -2
  1087. package/src/duckdb/src/planner/binder/tableref/bind_delimgetref.cpp +16 -0
  1088. package/src/duckdb/src/planner/binder/tableref/bind_joinref.cpp +31 -1
  1089. package/src/duckdb/src/planner/binder/tableref/bind_pivot.cpp +6 -0
  1090. package/src/duckdb/src/planner/binder/tableref/bind_showref.cpp +2 -0
  1091. package/src/duckdb/src/planner/binder/tableref/bind_table_function.cpp +106 -46
  1092. package/src/duckdb/src/planner/binder/tableref/plan_delimgetref.cpp +11 -0
  1093. package/src/duckdb/src/planner/binder/tableref/plan_joinref.cpp +15 -2
  1094. package/src/duckdb/src/planner/binder/tableref/plan_table_function.cpp +4 -0
  1095. package/src/duckdb/src/planner/binder.cpp +172 -15
  1096. package/src/duckdb/src/planner/collation_binding.cpp +99 -0
  1097. package/src/duckdb/src/planner/expression/bound_aggregate_expression.cpp +10 -4
  1098. package/src/duckdb/src/planner/expression/bound_between_expression.cpp +1 -1
  1099. package/src/duckdb/src/planner/expression/bound_case_expression.cpp +1 -1
  1100. package/src/duckdb/src/planner/expression/bound_cast_expression.cpp +14 -12
  1101. package/src/duckdb/src/planner/expression/bound_columnref_expression.cpp +1 -1
  1102. package/src/duckdb/src/planner/expression/bound_comparison_expression.cpp +1 -1
  1103. package/src/duckdb/src/planner/expression/bound_conjunction_expression.cpp +1 -1
  1104. package/src/duckdb/src/planner/expression/bound_constant_expression.cpp +1 -1
  1105. package/src/duckdb/src/planner/expression/bound_expanded_expression.cpp +1 -1
  1106. package/src/duckdb/src/planner/expression/bound_function_expression.cpp +8 -2
  1107. package/src/duckdb/src/planner/expression/bound_lambda_expression.cpp +1 -1
  1108. package/src/duckdb/src/planner/expression/bound_lambdaref_expression.cpp +1 -1
  1109. package/src/duckdb/src/planner/expression/bound_operator_expression.cpp +1 -1
  1110. package/src/duckdb/src/planner/expression/bound_parameter_expression.cpp +1 -1
  1111. package/src/duckdb/src/planner/expression/bound_reference_expression.cpp +1 -1
  1112. package/src/duckdb/src/planner/expression/bound_subquery_expression.cpp +1 -1
  1113. package/src/duckdb/src/planner/expression/bound_unnest_expression.cpp +1 -1
  1114. package/src/duckdb/src/planner/expression/bound_window_expression.cpp +6 -6
  1115. package/src/duckdb/src/planner/expression_binder/aggregate_binder.cpp +1 -1
  1116. package/src/duckdb/src/planner/expression_binder/alter_binder.cpp +2 -2
  1117. package/src/duckdb/src/planner/expression_binder/base_select_binder.cpp +1 -1
  1118. package/src/duckdb/src/planner/expression_binder/column_alias_binder.cpp +7 -0
  1119. package/src/duckdb/src/planner/expression_binder/constant_binder.cpp +3 -3
  1120. package/src/duckdb/src/planner/expression_binder/group_binder.cpp +26 -22
  1121. package/src/duckdb/src/planner/expression_binder/having_binder.cpp +7 -1
  1122. package/src/duckdb/src/planner/expression_binder/index_binder.cpp +2 -2
  1123. package/src/duckdb/src/planner/expression_binder/insert_binder.cpp +2 -2
  1124. package/src/duckdb/src/planner/expression_binder/lateral_binder.cpp +2 -2
  1125. package/src/duckdb/src/planner/expression_binder/order_binder.cpp +61 -43
  1126. package/src/duckdb/src/planner/expression_binder/qualify_binder.cpp +2 -2
  1127. package/src/duckdb/src/planner/expression_binder/relation_binder.cpp +4 -4
  1128. package/src/duckdb/src/planner/expression_binder/returning_binder.cpp +3 -2
  1129. package/src/duckdb/src/planner/expression_binder/table_function_binder.cpp +10 -3
  1130. package/src/duckdb/src/planner/expression_binder/update_binder.cpp +1 -1
  1131. package/src/duckdb/src/planner/expression_binder/where_binder.cpp +9 -2
  1132. package/src/duckdb/src/planner/expression_binder.cpp +121 -21
  1133. package/src/duckdb/src/planner/expression_iterator.cpp +26 -1
  1134. package/src/duckdb/src/planner/filter/conjunction_filter.cpp +33 -0
  1135. package/src/duckdb/src/planner/filter/constant_filter.cpp +15 -0
  1136. package/src/duckdb/src/planner/filter/null_filter.cpp +22 -0
  1137. package/src/duckdb/src/planner/filter/struct_filter.cpp +16 -0
  1138. package/src/duckdb/src/planner/logical_operator.cpp +24 -7
  1139. package/src/duckdb/src/planner/operator/logical_aggregate.cpp +13 -7
  1140. package/src/duckdb/src/planner/operator/logical_any_join.cpp +5 -2
  1141. package/src/duckdb/src/planner/operator/logical_comparison_join.cpp +13 -5
  1142. package/src/duckdb/src/planner/operator/logical_copy_to_file.cpp +64 -8
  1143. package/src/duckdb/src/planner/operator/logical_cteref.cpp +7 -0
  1144. package/src/duckdb/src/planner/operator/logical_distinct.cpp +6 -5
  1145. package/src/duckdb/src/planner/operator/logical_get.cpp +60 -18
  1146. package/src/duckdb/src/planner/operator/logical_materialized_cte.cpp +7 -0
  1147. package/src/duckdb/src/planner/operator/logical_order.cpp +7 -4
  1148. package/src/duckdb/src/planner/operator/logical_top_n.cpp +2 -2
  1149. package/src/duckdb/src/planner/operator/logical_vacuum.cpp +1 -1
  1150. package/src/duckdb/src/planner/planner.cpp +2 -3
  1151. package/src/duckdb/src/planner/subquery/flatten_dependent_join.cpp +27 -10
  1152. package/src/duckdb/src/planner/table_filter.cpp +51 -0
  1153. package/src/duckdb/src/storage/arena_allocator.cpp +28 -10
  1154. package/src/duckdb/src/storage/block.cpp +3 -2
  1155. package/src/duckdb/src/storage/buffer/block_handle.cpp +29 -14
  1156. package/src/duckdb/src/storage/buffer/block_manager.cpp +6 -5
  1157. package/src/duckdb/src/storage/buffer/buffer_handle.cpp +1 -1
  1158. package/src/duckdb/src/storage/buffer/buffer_pool.cpp +264 -125
  1159. package/src/duckdb/src/storage/buffer_manager.cpp +5 -1
  1160. package/src/duckdb/src/storage/checkpoint/row_group_writer.cpp +0 -6
  1161. package/src/duckdb/src/storage/checkpoint/table_data_writer.cpp +26 -3
  1162. package/src/duckdb/src/storage/checkpoint/write_overflow_strings_to_disk.cpp +21 -9
  1163. package/src/duckdb/src/storage/checkpoint_manager.cpp +49 -24
  1164. package/src/duckdb/src/storage/compression/alp/alp.cpp +6 -11
  1165. package/src/duckdb/src/storage/compression/alprd.cpp +5 -9
  1166. package/src/duckdb/src/storage/compression/bitpacking.cpp +35 -31
  1167. package/src/duckdb/src/storage/compression/chimp/chimp.cpp +6 -8
  1168. package/src/duckdb/src/storage/compression/dictionary_compression.cpp +71 -58
  1169. package/src/duckdb/src/storage/compression/fixed_size_uncompressed.cpp +15 -13
  1170. package/src/duckdb/src/storage/compression/fsst.cpp +66 -53
  1171. package/src/duckdb/src/storage/compression/numeric_constant.cpp +4 -5
  1172. package/src/duckdb/src/storage/compression/patas.cpp +6 -17
  1173. package/src/duckdb/src/storage/compression/rle.cpp +20 -18
  1174. package/src/duckdb/src/storage/compression/string_uncompressed.cpp +71 -52
  1175. package/src/duckdb/src/storage/compression/uncompressed.cpp +2 -2
  1176. package/src/duckdb/src/storage/compression/validity_uncompressed.cpp +8 -7
  1177. package/src/duckdb/src/storage/data_pointer.cpp +22 -0
  1178. package/src/duckdb/src/storage/data_table.cpp +41 -12
  1179. package/src/duckdb/src/storage/local_storage.cpp +22 -8
  1180. package/src/duckdb/src/storage/metadata/metadata_manager.cpp +33 -17
  1181. package/src/duckdb/src/storage/metadata/metadata_reader.cpp +4 -4
  1182. package/src/duckdb/src/storage/metadata/metadata_writer.cpp +3 -3
  1183. package/src/duckdb/src/storage/partial_block_manager.cpp +19 -8
  1184. package/src/duckdb/src/storage/serialization/serialize_create_info.cpp +11 -8
  1185. package/src/duckdb/src/storage/serialization/serialize_expression.cpp +1 -1
  1186. package/src/duckdb/src/storage/serialization/serialize_extension_install_info.cpp +2 -0
  1187. package/src/duckdb/src/storage/serialization/serialize_logical_operator.cpp +3 -3
  1188. package/src/duckdb/src/storage/serialization/serialize_nodes.cpp +19 -5
  1189. package/src/duckdb/src/storage/serialization/serialize_parse_info.cpp +21 -1
  1190. package/src/duckdb/src/storage/serialization/serialize_parsed_expression.cpp +4 -2
  1191. package/src/duckdb/src/storage/serialization/serialize_query_node.cpp +2 -2
  1192. package/src/duckdb/src/storage/serialization/serialize_storage.cpp +2 -0
  1193. package/src/duckdb/src/storage/serialization/serialize_tableref.cpp +8 -4
  1194. package/src/duckdb/src/storage/serialization/serialize_types.cpp +4 -4
  1195. package/src/duckdb/src/storage/single_file_block_manager.cpp +170 -34
  1196. package/src/duckdb/src/storage/standard_buffer_manager.cpp +221 -64
  1197. package/src/duckdb/src/storage/statistics/column_statistics.cpp +4 -3
  1198. package/src/duckdb/src/storage/statistics/distinct_statistics.cpp +36 -26
  1199. package/src/duckdb/src/storage/statistics/numeric_stats.cpp +4 -15
  1200. package/src/duckdb/src/storage/statistics/string_stats.cpp +14 -8
  1201. package/src/duckdb/src/storage/statistics/struct_stats.cpp +2 -1
  1202. package/src/duckdb/src/storage/storage_info.cpp +34 -9
  1203. package/src/duckdb/src/storage/storage_manager.cpp +147 -74
  1204. package/src/duckdb/src/storage/table/array_column_data.cpp +37 -17
  1205. package/src/duckdb/src/storage/table/chunk_info.cpp +38 -0
  1206. package/src/duckdb/src/storage/table/column_checkpoint_state.cpp +10 -6
  1207. package/src/duckdb/src/storage/table/column_data.cpp +252 -31
  1208. package/src/duckdb/src/storage/table/column_data_checkpointer.cpp +2 -12
  1209. package/src/duckdb/src/storage/table/column_segment.cpp +63 -34
  1210. package/src/duckdb/src/storage/table/list_column_data.cpp +34 -15
  1211. package/src/duckdb/src/storage/table/row_group.cpp +228 -120
  1212. package/src/duckdb/src/storage/table/row_group_collection.cpp +122 -120
  1213. package/src/duckdb/src/storage/table/row_version_manager.cpp +27 -1
  1214. package/src/duckdb/src/storage/table/scan_state.cpp +101 -18
  1215. package/src/duckdb/src/storage/table/standard_column_data.cpp +20 -34
  1216. package/src/duckdb/src/storage/table/struct_column_data.cpp +39 -42
  1217. package/src/duckdb/src/storage/table/table_statistics.cpp +2 -1
  1218. package/src/duckdb/src/storage/table/update_segment.cpp +9 -8
  1219. package/src/duckdb/src/storage/table/validity_column_data.cpp +2 -2
  1220. package/src/duckdb/src/storage/table_index_list.cpp +8 -7
  1221. package/src/duckdb/src/storage/temporary_file_manager.cpp +11 -9
  1222. package/src/duckdb/src/storage/temporary_memory_manager.cpp +227 -39
  1223. package/src/duckdb/src/storage/wal_replay.cpp +68 -28
  1224. package/src/duckdb/src/storage/write_ahead_log.cpp +56 -47
  1225. package/src/duckdb/src/transaction/cleanup_state.cpp +9 -1
  1226. package/src/duckdb/src/transaction/commit_state.cpp +7 -170
  1227. package/src/duckdb/src/transaction/duck_transaction.cpp +87 -19
  1228. package/src/duckdb/src/transaction/duck_transaction_manager.cpp +65 -10
  1229. package/src/duckdb/src/transaction/meta_transaction.cpp +18 -3
  1230. package/src/duckdb/src/transaction/transaction_context.cpp +21 -17
  1231. package/src/duckdb/src/transaction/undo_buffer.cpp +20 -14
  1232. package/src/duckdb/src/transaction/wal_write_state.cpp +292 -0
  1233. package/src/duckdb/src/verification/prepared_statement_verifier.cpp +0 -1
  1234. package/src/duckdb/third_party/brotli/common/brotli_constants.h +204 -0
  1235. package/src/duckdb/third_party/brotli/common/brotli_platform.h +543 -0
  1236. package/src/duckdb/third_party/brotli/common/constants.cpp +17 -0
  1237. package/src/duckdb/third_party/brotli/common/context.cpp +156 -0
  1238. package/src/duckdb/third_party/brotli/common/context.h +110 -0
  1239. package/src/duckdb/third_party/brotli/common/dictionary.cpp +5912 -0
  1240. package/src/duckdb/third_party/brotli/common/dictionary.h +60 -0
  1241. package/src/duckdb/third_party/brotli/common/platform.cpp +24 -0
  1242. package/src/duckdb/third_party/brotli/common/shared_dictionary.cpp +517 -0
  1243. package/src/duckdb/third_party/brotli/common/shared_dictionary_internal.h +71 -0
  1244. package/src/duckdb/third_party/brotli/common/transform.cpp +287 -0
  1245. package/src/duckdb/third_party/brotli/common/transform.h +77 -0
  1246. package/src/duckdb/third_party/brotli/common/version.h +51 -0
  1247. package/src/duckdb/third_party/brotli/dec/bit_reader.cpp +74 -0
  1248. package/src/duckdb/third_party/brotli/dec/bit_reader.h +419 -0
  1249. package/src/duckdb/third_party/brotli/dec/decode.cpp +2758 -0
  1250. package/src/duckdb/third_party/brotli/dec/huffman.cpp +338 -0
  1251. package/src/duckdb/third_party/brotli/dec/huffman.h +118 -0
  1252. package/src/duckdb/third_party/brotli/dec/prefix.h +733 -0
  1253. package/src/duckdb/third_party/brotli/dec/state.cpp +178 -0
  1254. package/src/duckdb/third_party/brotli/dec/state.h +386 -0
  1255. package/src/duckdb/third_party/brotli/enc/backward_references.cpp +3775 -0
  1256. package/src/duckdb/third_party/brotli/enc/backward_references.h +36 -0
  1257. package/src/duckdb/third_party/brotli/enc/backward_references_hq.cpp +935 -0
  1258. package/src/duckdb/third_party/brotli/enc/backward_references_hq.h +92 -0
  1259. package/src/duckdb/third_party/brotli/enc/bit_cost.cpp +410 -0
  1260. package/src/duckdb/third_party/brotli/enc/bit_cost.h +60 -0
  1261. package/src/duckdb/third_party/brotli/enc/block_splitter.cpp +1653 -0
  1262. package/src/duckdb/third_party/brotli/enc/block_splitter.h +48 -0
  1263. package/src/duckdb/third_party/brotli/enc/brotli_bit_stream.cpp +1431 -0
  1264. package/src/duckdb/third_party/brotli/enc/brotli_bit_stream.h +85 -0
  1265. package/src/duckdb/third_party/brotli/enc/brotli_hash.h +4352 -0
  1266. package/src/duckdb/third_party/brotli/enc/brotli_params.h +47 -0
  1267. package/src/duckdb/third_party/brotli/enc/cluster.cpp +1025 -0
  1268. package/src/duckdb/third_party/brotli/enc/cluster.h +1017 -0
  1269. package/src/duckdb/third_party/brotli/enc/command.cpp +24 -0
  1270. package/src/duckdb/third_party/brotli/enc/command.h +187 -0
  1271. package/src/duckdb/third_party/brotli/enc/compound_dictionary.cpp +209 -0
  1272. package/src/duckdb/third_party/brotli/enc/compound_dictionary.h +75 -0
  1273. package/src/duckdb/third_party/brotli/enc/compress_fragment.cpp +796 -0
  1274. package/src/duckdb/third_party/brotli/enc/compress_fragment.h +82 -0
  1275. package/src/duckdb/third_party/brotli/enc/compress_fragment_two_pass.cpp +653 -0
  1276. package/src/duckdb/third_party/brotli/enc/compress_fragment_two_pass.h +68 -0
  1277. package/src/duckdb/third_party/brotli/enc/dictionary_hash.cpp +1844 -0
  1278. package/src/duckdb/third_party/brotli/enc/dictionary_hash.h +21 -0
  1279. package/src/duckdb/third_party/brotli/enc/encode.cpp +1990 -0
  1280. package/src/duckdb/third_party/brotli/enc/encoder_dict.cpp +636 -0
  1281. package/src/duckdb/third_party/brotli/enc/encoder_dict.h +153 -0
  1282. package/src/duckdb/third_party/brotli/enc/entropy_encode.cpp +500 -0
  1283. package/src/duckdb/third_party/brotli/enc/entropy_encode.h +119 -0
  1284. package/src/duckdb/third_party/brotli/enc/entropy_encode_static.h +538 -0
  1285. package/src/duckdb/third_party/brotli/enc/fast_log.cpp +101 -0
  1286. package/src/duckdb/third_party/brotli/enc/fast_log.h +63 -0
  1287. package/src/duckdb/third_party/brotli/enc/find_match_length.h +68 -0
  1288. package/src/duckdb/third_party/brotli/enc/histogram.cpp +96 -0
  1289. package/src/duckdb/third_party/brotli/enc/histogram.h +210 -0
  1290. package/src/duckdb/third_party/brotli/enc/literal_cost.cpp +176 -0
  1291. package/src/duckdb/third_party/brotli/enc/literal_cost.h +28 -0
  1292. package/src/duckdb/third_party/brotli/enc/memory.cpp +190 -0
  1293. package/src/duckdb/third_party/brotli/enc/memory.h +127 -0
  1294. package/src/duckdb/third_party/brotli/enc/metablock.cpp +1225 -0
  1295. package/src/duckdb/third_party/brotli/enc/metablock.h +102 -0
  1296. package/src/duckdb/third_party/brotli/enc/prefix.h +50 -0
  1297. package/src/duckdb/third_party/brotli/enc/quality.h +202 -0
  1298. package/src/duckdb/third_party/brotli/enc/ringbuffer.h +164 -0
  1299. package/src/duckdb/third_party/brotli/enc/state.h +106 -0
  1300. package/src/duckdb/third_party/brotli/enc/static_dict.cpp +538 -0
  1301. package/src/duckdb/third_party/brotli/enc/static_dict.h +37 -0
  1302. package/src/duckdb/third_party/brotli/enc/static_dict_lut.h +5862 -0
  1303. package/src/duckdb/third_party/brotli/enc/utf8_util.cpp +81 -0
  1304. package/src/duckdb/third_party/brotli/enc/utf8_util.h +29 -0
  1305. package/src/duckdb/third_party/brotli/enc/write_bits.h +84 -0
  1306. package/src/duckdb/third_party/brotli/include/brotli/decode.h +405 -0
  1307. package/src/duckdb/third_party/brotli/include/brotli/encode.h +489 -0
  1308. package/src/duckdb/third_party/brotli/include/brotli/port.h +238 -0
  1309. package/src/duckdb/third_party/brotli/include/brotli/shared_dictionary.h +96 -0
  1310. package/src/duckdb/third_party/brotli/include/brotli/types.h +83 -0
  1311. package/src/duckdb/third_party/fast_float/fast_float/fast_float.h +20 -4
  1312. package/src/duckdb/third_party/fmt/include/fmt/format.h +54 -10
  1313. package/src/duckdb/third_party/fsst/fsst.h +2 -2
  1314. package/src/duckdb/third_party/fsst/libfsst.hpp +2 -2
  1315. package/src/duckdb/third_party/httplib/httplib.hpp +6763 -5580
  1316. package/src/duckdb/third_party/hyperloglog/hyperloglog.cpp +13 -30
  1317. package/src/duckdb/third_party/hyperloglog/hyperloglog.hpp +8 -2
  1318. package/src/duckdb/third_party/libpg_query/include/nodes/nodes.hpp +1 -0
  1319. package/src/duckdb/third_party/libpg_query/include/nodes/parsenodes.hpp +22 -9
  1320. package/src/duckdb/third_party/libpg_query/include/parser/gram.hpp +1041 -554
  1321. package/src/duckdb/third_party/libpg_query/include/parser/kwlist.hpp +1 -0
  1322. package/src/duckdb/third_party/libpg_query/postgres_parser.cpp +2 -1
  1323. package/src/duckdb/third_party/libpg_query/src_backend_parser_gram.cpp +21605 -21752
  1324. package/src/duckdb/third_party/libpg_query/src_backend_parser_scan.cpp +538 -299
  1325. package/src/duckdb/third_party/mbedtls/include/mbedtls/mbedtls_config.h +1 -0
  1326. package/src/duckdb/third_party/mbedtls/include/mbedtls_wrapper.hpp +36 -12
  1327. package/src/duckdb/third_party/mbedtls/library/md.cpp +6 -6
  1328. package/src/duckdb/third_party/mbedtls/library/sha1.cpp +2 -0
  1329. package/src/duckdb/third_party/mbedtls/library/sha256.cpp +3 -0
  1330. package/src/duckdb/third_party/mbedtls/mbedtls_wrapper.cpp +99 -47
  1331. package/src/duckdb/third_party/pcg/pcg_extras.hpp +1 -1
  1332. package/src/duckdb/third_party/re2/re2/prog.cc +2 -2
  1333. package/src/duckdb/third_party/snappy/snappy-internal.h +398 -0
  1334. package/src/duckdb/third_party/snappy/snappy-sinksource.cc +111 -9
  1335. package/src/duckdb/third_party/snappy/snappy-sinksource.h +158 -0
  1336. package/src/duckdb/third_party/snappy/snappy-stubs-internal.h +523 -3
  1337. package/src/duckdb/third_party/snappy/snappy-stubs-public.h +34 -1
  1338. package/src/duckdb/third_party/snappy/snappy.cc +2626 -0
  1339. package/src/duckdb/third_party/snappy/snappy.h +223 -0
  1340. package/src/duckdb/third_party/snappy/snappy_version.hpp +11 -0
  1341. package/src/duckdb/third_party/utf8proc/include/utf8proc.hpp +69 -101
  1342. package/src/duckdb/third_party/utf8proc/include/utf8proc_wrapper.hpp +53 -0
  1343. package/src/duckdb/third_party/utf8proc/utf8proc.cpp +627 -678
  1344. package/src/duckdb/third_party/utf8proc/utf8proc_data.cpp +15008 -12868
  1345. package/src/duckdb/third_party/utf8proc/utf8proc_wrapper.cpp +185 -29
  1346. package/src/duckdb/ub_extension_json_json_functions.cpp +6 -0
  1347. package/src/duckdb/ub_src_catalog_default.cpp +4 -0
  1348. package/src/duckdb/ub_src_common.cpp +7 -1
  1349. package/src/duckdb/ub_src_common_arrow.cpp +10 -0
  1350. package/src/duckdb/ub_src_common_enums.cpp +2 -0
  1351. package/src/duckdb/ub_src_common_tree_renderer.cpp +10 -0
  1352. package/src/duckdb/ub_src_common_types.cpp +2 -0
  1353. package/src/duckdb/ub_src_core_functions_aggregate_holistic.cpp +4 -0
  1354. package/src/duckdb/ub_src_core_functions_aggregate_nested.cpp +2 -0
  1355. package/src/duckdb/ub_src_core_functions_scalar_generic.cpp +2 -0
  1356. package/src/duckdb/ub_src_core_functions_scalar_list.cpp +2 -4
  1357. package/src/duckdb/ub_src_core_functions_scalar_map.cpp +2 -0
  1358. package/src/duckdb/ub_src_core_functions_scalar_string.cpp +4 -0
  1359. package/src/duckdb/ub_src_execution_index_art.cpp +5 -3
  1360. package/src/duckdb/ub_src_execution_operator_csv_scanner_scanner.cpp +2 -0
  1361. package/src/duckdb/ub_src_execution_operator_helper.cpp +4 -0
  1362. package/src/duckdb/ub_src_function.cpp +4 -0
  1363. package/src/duckdb/ub_src_function_cast.cpp +2 -0
  1364. package/src/duckdb/ub_src_function_scalar_generic.cpp +4 -0
  1365. package/src/duckdb/ub_src_function_scalar_list.cpp +0 -2
  1366. package/src/duckdb/ub_src_function_scalar_string.cpp +2 -0
  1367. package/src/duckdb/ub_src_function_table.cpp +2 -0
  1368. package/src/duckdb/ub_src_function_table_arrow.cpp +2 -0
  1369. package/src/duckdb/ub_src_function_table_system.cpp +2 -0
  1370. package/src/duckdb/ub_src_main.cpp +4 -0
  1371. package/src/duckdb/ub_src_main_buffered_data.cpp +4 -0
  1372. package/src/duckdb/ub_src_main_capi.cpp +10 -0
  1373. package/src/duckdb/ub_src_main_chunk_scan_state.cpp +2 -0
  1374. package/src/duckdb/ub_src_main_relation.cpp +2 -0
  1375. package/src/duckdb/ub_src_main_secret.cpp +2 -0
  1376. package/src/duckdb/ub_src_optimizer.cpp +8 -0
  1377. package/src/duckdb/ub_src_optimizer_compressed_materialization.cpp +2 -0
  1378. package/src/duckdb/ub_src_optimizer_pushdown.cpp +2 -0
  1379. package/src/duckdb/ub_src_optimizer_rule.cpp +2 -0
  1380. package/src/duckdb/ub_src_parallel.cpp +4 -0
  1381. package/src/duckdb/ub_src_parser_tableref.cpp +2 -0
  1382. package/src/duckdb/ub_src_planner.cpp +2 -0
  1383. package/src/duckdb/ub_src_planner_binder_expression.cpp +2 -0
  1384. package/src/duckdb/ub_src_planner_binder_tableref.cpp +4 -0
  1385. package/src/duckdb/ub_src_storage_statistics.cpp +0 -2
  1386. package/src/duckdb/ub_src_transaction.cpp +2 -0
  1387. package/test/columns.test.ts +1 -1
  1388. package/test/prepare.test.ts +1 -1
  1389. package/test/test_all_types.test.ts +1 -1
@@ -0,0 +1,1990 @@
1
+ /* Copyright 2013 Google Inc. All Rights Reserved.
2
+
3
+ Distributed under MIT license.
4
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
+ */
6
+
7
+ /* Implementation of Brotli compressor. */
8
+
9
+ #include <brotli/encode.h>
10
+ #include <brotli/shared_dictionary.h>
11
+ #include <brotli/types.h>
12
+
13
+ #include <stdlib.h> /* free, malloc */
14
+ #include <string.h> /* memcpy, memset */
15
+
16
+ #include "../common/brotli_constants.h"
17
+ #include "../common/context.h"
18
+ #include "../common/brotli_platform.h"
19
+ #include "../common/version.h"
20
+ #include "backward_references.h"
21
+ #include "backward_references_hq.h"
22
+ #include "bit_cost.h"
23
+ #include "brotli_bit_stream.h"
24
+ #include "compress_fragment.h"
25
+ #include "compress_fragment_two_pass.h"
26
+ #include "dictionary_hash.h"
27
+ #include "encoder_dict.h"
28
+ #include "entropy_encode.h"
29
+ #include "fast_log.h"
30
+ #include "brotli_hash.h"
31
+ #include "histogram.h"
32
+ #include "memory.h"
33
+ #include "metablock.h"
34
+ #include "prefix.h"
35
+ #include "state.h"
36
+ #include "quality.h"
37
+ #include "ringbuffer.h"
38
+ #include "utf8_util.h"
39
+ #include "write_bits.h"
40
+
41
+ using namespace duckdb_brotli;
42
+
43
+ #define COPY_ARRAY(dst, src) memcpy(dst, src, sizeof(src));
44
+
45
+ static size_t InputBlockSize(BrotliEncoderState* s) {
46
+ return (size_t)1 << s->params.lgblock;
47
+ }
48
+
49
+ static uint64_t UnprocessedInputSize(BrotliEncoderState* s) {
50
+ return s->input_pos_ - s->last_processed_pos_;
51
+ }
52
+
53
+ static size_t RemainingInputBlockSize(BrotliEncoderState* s) {
54
+ const uint64_t delta = UnprocessedInputSize(s);
55
+ size_t block_size = InputBlockSize(s);
56
+ if (delta >= block_size) return 0;
57
+ return block_size - (size_t)delta;
58
+ }
59
+
60
+ BROTLI_BOOL duckdb_brotli::BrotliEncoderSetParameter(
61
+ BrotliEncoderState* state, BrotliEncoderParameter p, uint32_t value) {
62
+ /* Changing parameters on the fly is not implemented yet. */
63
+ if (state->is_initialized_) return BROTLI_FALSE;
64
+ /* TODO(eustas): Validate/clamp parameters here. */
65
+ switch (p) {
66
+ case BROTLI_PARAM_MODE:
67
+ state->params.mode = (BrotliEncoderMode)value;
68
+ return BROTLI_TRUE;
69
+
70
+ case BROTLI_PARAM_QUALITY:
71
+ state->params.quality = (int)value;
72
+ return BROTLI_TRUE;
73
+
74
+ case BROTLI_PARAM_LGWIN:
75
+ state->params.lgwin = (int)value;
76
+ return BROTLI_TRUE;
77
+
78
+ case BROTLI_PARAM_LGBLOCK:
79
+ state->params.lgblock = (int)value;
80
+ return BROTLI_TRUE;
81
+
82
+ case BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING:
83
+ if ((value != 0) && (value != 1)) return BROTLI_FALSE;
84
+ state->params.disable_literal_context_modeling = TO_BROTLI_BOOL(!!value);
85
+ return BROTLI_TRUE;
86
+
87
+ case BROTLI_PARAM_SIZE_HINT:
88
+ state->params.size_hint = value;
89
+ return BROTLI_TRUE;
90
+
91
+ case BROTLI_PARAM_LARGE_WINDOW:
92
+ state->params.large_window = TO_BROTLI_BOOL(!!value);
93
+ return BROTLI_TRUE;
94
+
95
+ case BROTLI_PARAM_NPOSTFIX:
96
+ state->params.dist.distance_postfix_bits = value;
97
+ return BROTLI_TRUE;
98
+
99
+ case BROTLI_PARAM_NDIRECT:
100
+ state->params.dist.num_direct_distance_codes = value;
101
+ return BROTLI_TRUE;
102
+
103
+ case BROTLI_PARAM_STREAM_OFFSET:
104
+ if (value > (1u << 30)) return BROTLI_FALSE;
105
+ state->params.stream_offset = value;
106
+ return BROTLI_TRUE;
107
+
108
+ default: return BROTLI_FALSE;
109
+ }
110
+ }
111
+
112
+ /* Wraps 64-bit input position to 32-bit ring-buffer position preserving
113
+ "not-a-first-lap" feature. */
114
+ static uint32_t WrapPosition(uint64_t position) {
115
+ uint32_t result = (uint32_t)position;
116
+ uint64_t gb = position >> 30;
117
+ if (gb > 2) {
118
+ /* Wrap every 2GiB; The first 3GB are continuous. */
119
+ result = (result & ((1u << 30) - 1)) | ((uint32_t)((gb - 1) & 1) + 1) << 30;
120
+ }
121
+ return result;
122
+ }
123
+
124
+ static uint8_t* GetBrotliStorage(BrotliEncoderState* s, size_t size) {
125
+ MemoryManager* m = &s->memory_manager_;
126
+ if (s->storage_size_ < size) {
127
+ BROTLI_FREE(m, s->storage_);
128
+ s->storage_ = BROTLI_ALLOC(m, uint8_t, size);
129
+ if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(s->storage_)) return NULL;
130
+ s->storage_size_ = size;
131
+ }
132
+ return s->storage_;
133
+ }
134
+
135
+ static size_t HashTableSize(size_t max_table_size, size_t input_size) {
136
+ size_t htsize = 256;
137
+ while (htsize < max_table_size && htsize < input_size) {
138
+ htsize <<= 1;
139
+ }
140
+ return htsize;
141
+ }
142
+
143
+ static int* GetHashTable(BrotliEncoderState* s, int quality,
144
+ size_t input_size, size_t* table_size) {
145
+ /* Use smaller hash table when input.size() is smaller, since we
146
+ fill the table, incurring O(hash table size) overhead for
147
+ compression, and if the input is short, we won't need that
148
+ many hash table entries anyway. */
149
+ MemoryManager* m = &s->memory_manager_;
150
+ const size_t max_table_size = MaxHashTableSize(quality);
151
+ size_t htsize = HashTableSize(max_table_size, input_size);
152
+ int* table;
153
+ BROTLI_DCHECK(max_table_size >= 256);
154
+ if (quality == FAST_ONE_PASS_COMPRESSION_QUALITY) {
155
+ /* Only odd shifts are supported by fast-one-pass. */
156
+ if ((htsize & 0xAAAAA) == 0) {
157
+ htsize <<= 1;
158
+ }
159
+ }
160
+
161
+ if (htsize <= sizeof(s->small_table_) / sizeof(s->small_table_[0])) {
162
+ table = s->small_table_;
163
+ } else {
164
+ if (htsize > s->large_table_size_) {
165
+ s->large_table_size_ = htsize;
166
+ BROTLI_FREE(m, s->large_table_);
167
+ s->large_table_ = BROTLI_ALLOC(m, int, htsize);
168
+ if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(s->large_table_)) return 0;
169
+ }
170
+ table = s->large_table_;
171
+ }
172
+
173
+ *table_size = htsize;
174
+ memset(table, 0, htsize * sizeof(*table));
175
+ return table;
176
+ }
177
+
178
+ static void EncodeWindowBits(int lgwin, BROTLI_BOOL large_window,
179
+ uint16_t* last_bytes, uint8_t* last_bytes_bits) {
180
+ if (large_window) {
181
+ *last_bytes = (uint16_t)(((lgwin & 0x3F) << 8) | 0x11);
182
+ *last_bytes_bits = 14;
183
+ } else {
184
+ if (lgwin == 16) {
185
+ *last_bytes = 0;
186
+ *last_bytes_bits = 1;
187
+ } else if (lgwin == 17) {
188
+ *last_bytes = 1;
189
+ *last_bytes_bits = 7;
190
+ } else if (lgwin > 17) {
191
+ *last_bytes = (uint16_t)(((lgwin - 17) << 1) | 0x01);
192
+ *last_bytes_bits = 4;
193
+ } else {
194
+ *last_bytes = (uint16_t)(((lgwin - 8) << 4) | 0x01);
195
+ *last_bytes_bits = 7;
196
+ }
197
+ }
198
+ }
199
+
200
+ /* TODO(eustas): move to compress_fragment.c? */
201
+ /* Initializes the command and distance prefix codes for the first block. */
202
+ static void InitCommandPrefixCodes(BrotliOnePassArena* s) {
203
+ static const uint8_t kDefaultCommandDepths[128] = {
204
+ 0, 4, 4, 5, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8,
205
+ 0, 0, 0, 4, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7,
206
+ 7, 7, 10, 10, 10, 10, 10, 10, 0, 4, 4, 5, 5, 5, 6, 6,
207
+ 7, 8, 8, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
208
+ 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209
+ 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4,
210
+ 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 8, 10,
211
+ 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
212
+ };
213
+ static const uint16_t kDefaultCommandBits[128] = {
214
+ 0, 0, 8, 9, 3, 35, 7, 71,
215
+ 39, 103, 23, 47, 175, 111, 239, 31,
216
+ 0, 0, 0, 4, 12, 2, 10, 6,
217
+ 13, 29, 11, 43, 27, 59, 87, 55,
218
+ 15, 79, 319, 831, 191, 703, 447, 959,
219
+ 0, 14, 1, 25, 5, 21, 19, 51,
220
+ 119, 159, 95, 223, 479, 991, 63, 575,
221
+ 127, 639, 383, 895, 255, 767, 511, 1023,
222
+ 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
223
+ 27, 59, 7, 39, 23, 55, 30, 1, 17, 9, 25, 5, 0, 8, 4, 12,
224
+ 2, 10, 6, 21, 13, 29, 3, 19, 11, 15, 47, 31, 95, 63, 127, 255,
225
+ 767, 2815, 1791, 3839, 511, 2559, 1535, 3583, 1023, 3071, 2047, 4095,
226
+ };
227
+ static const uint8_t kDefaultCommandCode[] = {
228
+ 0xff, 0x77, 0xd5, 0xbf, 0xe7, 0xde, 0xea, 0x9e, 0x51, 0x5d, 0xde, 0xc6,
229
+ 0x70, 0x57, 0xbc, 0x58, 0x58, 0x58, 0xd8, 0xd8, 0x58, 0xd5, 0xcb, 0x8c,
230
+ 0xea, 0xe0, 0xc3, 0x87, 0x1f, 0x83, 0xc1, 0x60, 0x1c, 0x67, 0xb2, 0xaa,
231
+ 0x06, 0x83, 0xc1, 0x60, 0x30, 0x18, 0xcc, 0xa1, 0xce, 0x88, 0x54, 0x94,
232
+ 0x46, 0xe1, 0xb0, 0xd0, 0x4e, 0xb2, 0xf7, 0x04, 0x00,
233
+ };
234
+ static const size_t kDefaultCommandCodeNumBits = 448;
235
+ COPY_ARRAY(s->cmd_depth, kDefaultCommandDepths);
236
+ COPY_ARRAY(s->cmd_bits, kDefaultCommandBits);
237
+
238
+ /* Initialize the pre-compressed form of the command and distance prefix
239
+ codes. */
240
+ COPY_ARRAY(s->cmd_code, kDefaultCommandCode);
241
+ s->cmd_code_numbits = kDefaultCommandCodeNumBits;
242
+ }
243
+
244
+ /* Decide about the context map based on the ability of the prediction
245
+ ability of the previous byte UTF8-prefix on the next byte. The
246
+ prediction ability is calculated as Shannon entropy. Here we need
247
+ Shannon entropy instead of 'BitsEntropy' since the prefix will be
248
+ encoded with the remaining 6 bits of the following byte, and
249
+ BitsEntropy will assume that symbol to be stored alone using Huffman
250
+ coding. */
251
+ static void ChooseContextMap(int quality,
252
+ uint32_t* bigram_histo,
253
+ size_t* num_literal_contexts,
254
+ const uint32_t** literal_context_map) {
255
+ static const uint32_t kStaticContextMapContinuation[64] = {
256
+ 1, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
257
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
258
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
259
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
260
+ };
261
+ static const uint32_t kStaticContextMapSimpleUTF8[64] = {
262
+ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
263
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
264
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
265
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
266
+ };
267
+
268
+ uint32_t monogram_histo[3] = { 0 };
269
+ uint32_t two_prefix_histo[6] = { 0 };
270
+ size_t total;
271
+ size_t i;
272
+ size_t dummy;
273
+ double entropy[4];
274
+ for (i = 0; i < 9; ++i) {
275
+ monogram_histo[i % 3] += bigram_histo[i];
276
+ two_prefix_histo[i % 6] += bigram_histo[i];
277
+ }
278
+ entropy[1] = ShannonEntropy(monogram_histo, 3, &dummy);
279
+ entropy[2] = (ShannonEntropy(two_prefix_histo, 3, &dummy) +
280
+ ShannonEntropy(two_prefix_histo + 3, 3, &dummy));
281
+ entropy[3] = 0;
282
+ for (i = 0; i < 3; ++i) {
283
+ entropy[3] += ShannonEntropy(bigram_histo + 3 * i, 3, &dummy);
284
+ }
285
+
286
+ total = monogram_histo[0] + monogram_histo[1] + monogram_histo[2];
287
+ BROTLI_DCHECK(total != 0);
288
+ entropy[0] = 1.0 / (double)total;
289
+ entropy[1] *= entropy[0];
290
+ entropy[2] *= entropy[0];
291
+ entropy[3] *= entropy[0];
292
+
293
+ if (quality < MIN_QUALITY_FOR_HQ_CONTEXT_MODELING) {
294
+ /* 3 context models is a bit slower, don't use it at lower qualities. */
295
+ entropy[3] = entropy[1] * 10;
296
+ }
297
+ /* If expected savings by symbol are less than 0.2 bits, skip the
298
+ context modeling -- in exchange for faster decoding speed. */
299
+ if (entropy[1] - entropy[2] < 0.2 &&
300
+ entropy[1] - entropy[3] < 0.2) {
301
+ *num_literal_contexts = 1;
302
+ } else if (entropy[2] - entropy[3] < 0.02) {
303
+ *num_literal_contexts = 2;
304
+ *literal_context_map = kStaticContextMapSimpleUTF8;
305
+ } else {
306
+ *num_literal_contexts = 3;
307
+ *literal_context_map = kStaticContextMapContinuation;
308
+ }
309
+ }
310
+
311
+ /* Decide if we want to use a more complex static context map containing 13
312
+ context values, based on the entropy reduction of histograms over the
313
+ first 5 bits of literals. */
314
+ static BROTLI_BOOL ShouldUseComplexStaticContextMap(const uint8_t* input,
315
+ size_t start_pos, size_t length, size_t mask, int quality, size_t size_hint,
316
+ size_t* num_literal_contexts, const uint32_t** literal_context_map,
317
+ uint32_t* arena) {
318
+ static const uint32_t kStaticContextMapComplexUTF8[64] = {
319
+ 11, 11, 12, 12, /* 0 special */
320
+ 0, 0, 0, 0, /* 4 lf */
321
+ 1, 1, 9, 9, /* 8 space */
322
+ 2, 2, 2, 2, /* !, first after space/lf and after something else. */
323
+ 1, 1, 1, 1, /* " */
324
+ 8, 3, 3, 3, /* % */
325
+ 1, 1, 1, 1, /* ({[ */
326
+ 2, 2, 2, 2, /* }]) */
327
+ 8, 4, 4, 4, /* :; */
328
+ 8, 7, 4, 4, /* . */
329
+ 8, 0, 0, 0, /* > */
330
+ 3, 3, 3, 3, /* [0..9] */
331
+ 5, 5, 10, 5, /* [A-Z] */
332
+ 5, 5, 10, 5,
333
+ 6, 6, 6, 6, /* [a-z] */
334
+ 6, 6, 6, 6,
335
+ };
336
+ BROTLI_UNUSED(quality);
337
+ /* Try the more complex static context map only for long data. */
338
+ if (size_hint < (1 << 20)) {
339
+ return BROTLI_FALSE;
340
+ } else {
341
+ const size_t end_pos = start_pos + length;
342
+ /* To make entropy calculations faster, we collect histograms
343
+ over the 5 most significant bits of literals. One histogram
344
+ without context and 13 additional histograms for each context value. */
345
+ uint32_t* BROTLI_RESTRICT const combined_histo = arena;
346
+ uint32_t* BROTLI_RESTRICT const context_histo = arena + 32;
347
+ uint32_t total = 0;
348
+ double entropy[3];
349
+ size_t dummy;
350
+ size_t i;
351
+ ContextLut utf8_lut = BROTLI_CONTEXT_LUT(CONTEXT_UTF8);
352
+ memset(arena, 0, sizeof(arena[0]) * 32 * 14);
353
+ for (; start_pos + 64 <= end_pos; start_pos += 4096) {
354
+ const size_t stride_end_pos = start_pos + 64;
355
+ uint8_t prev2 = input[start_pos & mask];
356
+ uint8_t prev1 = input[(start_pos + 1) & mask];
357
+ size_t pos;
358
+ /* To make the analysis of the data faster we only examine 64 byte long
359
+ strides at every 4kB intervals. */
360
+ for (pos = start_pos + 2; pos < stride_end_pos; ++pos) {
361
+ const uint8_t literal = input[pos & mask];
362
+ const uint8_t context = (uint8_t)kStaticContextMapComplexUTF8[
363
+ BROTLI_CONTEXT(prev1, prev2, utf8_lut)];
364
+ ++total;
365
+ ++combined_histo[literal >> 3];
366
+ ++context_histo[(context << 5) + (literal >> 3)];
367
+ prev2 = prev1;
368
+ prev1 = literal;
369
+ }
370
+ }
371
+ entropy[1] = ShannonEntropy(combined_histo, 32, &dummy);
372
+ entropy[2] = 0;
373
+ for (i = 0; i < 13; ++i) {
374
+ entropy[2] += ShannonEntropy(context_histo + (i << 5), 32, &dummy);
375
+ }
376
+ entropy[0] = 1.0 / (double)total;
377
+ entropy[1] *= entropy[0];
378
+ entropy[2] *= entropy[0];
379
+ /* The triggering heuristics below were tuned by compressing the individual
380
+ files of the silesia corpus. If we skip this kind of context modeling
381
+ for not very well compressible input (i.e. entropy using context modeling
382
+ is 60% of maximal entropy) or if expected savings by symbol are less
383
+ than 0.2 bits, then in every case when it triggers, the final compression
384
+ ratio is improved. Note however that this heuristics might be too strict
385
+ for some cases and could be tuned further. */
386
+ if (entropy[2] > 3.0 || entropy[1] - entropy[2] < 0.2) {
387
+ return BROTLI_FALSE;
388
+ } else {
389
+ *num_literal_contexts = 13;
390
+ *literal_context_map = kStaticContextMapComplexUTF8;
391
+ return BROTLI_TRUE;
392
+ }
393
+ }
394
+ }
395
+
396
+ static void DecideOverLiteralContextModeling(const uint8_t* input,
397
+ size_t start_pos, size_t length, size_t mask, int quality, size_t size_hint,
398
+ size_t* num_literal_contexts, const uint32_t** literal_context_map,
399
+ uint32_t* arena) {
400
+ if (quality < MIN_QUALITY_FOR_CONTEXT_MODELING || length < 64) {
401
+ return;
402
+ } else if (ShouldUseComplexStaticContextMap(
403
+ input, start_pos, length, mask, quality, size_hint,
404
+ num_literal_contexts, literal_context_map, arena)) {
405
+ /* Context map was already set, nothing else to do. */
406
+ } else {
407
+ /* Gather bi-gram data of the UTF8 byte prefixes. To make the analysis of
408
+ UTF8 data faster we only examine 64 byte long strides at every 4kB
409
+ intervals. */
410
+ const size_t end_pos = start_pos + length;
411
+ uint32_t* BROTLI_RESTRICT const bigram_prefix_histo = arena;
412
+ memset(bigram_prefix_histo, 0, sizeof(arena[0]) * 9);
413
+ for (; start_pos + 64 <= end_pos; start_pos += 4096) {
414
+ static const int lut[4] = { 0, 0, 1, 2 };
415
+ const size_t stride_end_pos = start_pos + 64;
416
+ int prev = lut[input[start_pos & mask] >> 6] * 3;
417
+ size_t pos;
418
+ for (pos = start_pos + 1; pos < stride_end_pos; ++pos) {
419
+ const uint8_t literal = input[pos & mask];
420
+ ++bigram_prefix_histo[prev + lut[literal >> 6]];
421
+ prev = lut[literal >> 6] * 3;
422
+ }
423
+ }
424
+ ChooseContextMap(quality, &bigram_prefix_histo[0], num_literal_contexts,
425
+ literal_context_map);
426
+ }
427
+ }
428
+
429
+ static BROTLI_BOOL ShouldCompress(
430
+ const uint8_t* data, const size_t mask, const uint64_t last_flush_pos,
431
+ const size_t bytes, const size_t num_literals, const size_t num_commands) {
432
+ /* TODO(eustas): find more precise minimal block overhead. */
433
+ if (bytes <= 2) return BROTLI_FALSE;
434
+ if (num_commands < (bytes >> 8) + 2) {
435
+ if ((double)num_literals > 0.99 * (double)bytes) {
436
+ uint32_t literal_histo[256] = { 0 };
437
+ static const uint32_t kSampleRate = 13;
438
+ static const double kMinEntropy = 7.92;
439
+ const double bit_cost_threshold =
440
+ (double)bytes * kMinEntropy / kSampleRate;
441
+ size_t t = (bytes + kSampleRate - 1) / kSampleRate;
442
+ uint32_t pos = (uint32_t)last_flush_pos;
443
+ size_t i;
444
+ for (i = 0; i < t; i++) {
445
+ ++literal_histo[data[pos & mask]];
446
+ pos += kSampleRate;
447
+ }
448
+ if (BitsEntropy(literal_histo, 256) > bit_cost_threshold) {
449
+ return BROTLI_FALSE;
450
+ }
451
+ }
452
+ }
453
+ return BROTLI_TRUE;
454
+ }
455
+
456
+ /* Chooses the literal context mode for a metablock */
457
+ static ContextType ChooseContextMode(const BrotliEncoderParams* params,
458
+ const uint8_t* data, const size_t pos, const size_t mask,
459
+ const size_t length) {
460
+ /* We only do the computation for the option of something else than
461
+ CONTEXT_UTF8 for the highest qualities */
462
+ if (params->quality >= MIN_QUALITY_FOR_HQ_BLOCK_SPLITTING &&
463
+ !BrotliIsMostlyUTF8(data, pos, mask, length, kMinUTF8Ratio)) {
464
+ return CONTEXT_SIGNED;
465
+ }
466
+ return CONTEXT_UTF8;
467
+ }
468
+
469
+ static void WriteMetaBlockInternal(MemoryManager* m,
470
+ const uint8_t* data,
471
+ const size_t mask,
472
+ const uint64_t last_flush_pos,
473
+ const size_t bytes,
474
+ const BROTLI_BOOL is_last,
475
+ ContextType literal_context_mode,
476
+ const BrotliEncoderParams* params,
477
+ const uint8_t prev_byte,
478
+ const uint8_t prev_byte2,
479
+ const size_t num_literals,
480
+ const size_t num_commands,
481
+ Command* commands,
482
+ const int* saved_dist_cache,
483
+ int* dist_cache,
484
+ size_t* storage_ix,
485
+ uint8_t* storage) {
486
+ const uint32_t wrapped_last_flush_pos = WrapPosition(last_flush_pos);
487
+ uint16_t last_bytes;
488
+ uint8_t last_bytes_bits;
489
+ ContextLut literal_context_lut = BROTLI_CONTEXT_LUT(literal_context_mode);
490
+ BrotliEncoderParams block_params = *params;
491
+
492
+ if (bytes == 0) {
493
+ /* Write the ISLAST and ISEMPTY bits. */
494
+ BrotliWriteBits(2, 3, storage_ix, storage);
495
+ *storage_ix = (*storage_ix + 7u) & ~7u;
496
+ return;
497
+ }
498
+
499
+ if (!ShouldCompress(data, mask, last_flush_pos, bytes,
500
+ num_literals, num_commands)) {
501
+ /* Restore the distance cache, as its last update by
502
+ CreateBackwardReferences is now unused. */
503
+ memcpy(dist_cache, saved_dist_cache, 4 * sizeof(dist_cache[0]));
504
+ BrotliStoreUncompressedMetaBlock(is_last, data,
505
+ wrapped_last_flush_pos, mask, bytes,
506
+ storage_ix, storage);
507
+ return;
508
+ }
509
+
510
+ BROTLI_DCHECK(*storage_ix <= 14);
511
+ last_bytes = (uint16_t)((storage[1] << 8) | storage[0]);
512
+ last_bytes_bits = (uint8_t)(*storage_ix);
513
+ if (params->quality <= MAX_QUALITY_FOR_STATIC_ENTROPY_CODES) {
514
+ BrotliStoreMetaBlockFast(m, data, wrapped_last_flush_pos,
515
+ bytes, mask, is_last, params,
516
+ commands, num_commands,
517
+ storage_ix, storage);
518
+ if (BROTLI_IS_OOM(m)) return;
519
+ } else if (params->quality < MIN_QUALITY_FOR_BLOCK_SPLIT) {
520
+ BrotliStoreMetaBlockTrivial(m, data, wrapped_last_flush_pos,
521
+ bytes, mask, is_last, params,
522
+ commands, num_commands,
523
+ storage_ix, storage);
524
+ if (BROTLI_IS_OOM(m)) return;
525
+ } else {
526
+ MetaBlockSplit mb;
527
+ InitMetaBlockSplit(&mb);
528
+ if (params->quality < MIN_QUALITY_FOR_HQ_BLOCK_SPLITTING) {
529
+ size_t num_literal_contexts = 1;
530
+ const uint32_t* literal_context_map = NULL;
531
+ if (!params->disable_literal_context_modeling) {
532
+ /* TODO(eustas): pull to higher level and reuse. */
533
+ uint32_t* arena = BROTLI_ALLOC(m, uint32_t, 14 * 32);
534
+ if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(arena)) return;
535
+ DecideOverLiteralContextModeling(
536
+ data, wrapped_last_flush_pos, bytes, mask, params->quality,
537
+ params->size_hint, &num_literal_contexts,
538
+ &literal_context_map, arena);
539
+ BROTLI_FREE(m, arena);
540
+ }
541
+ BrotliBuildMetaBlockGreedy(m, data, wrapped_last_flush_pos, mask,
542
+ prev_byte, prev_byte2, literal_context_lut, num_literal_contexts,
543
+ literal_context_map, commands, num_commands, &mb);
544
+ if (BROTLI_IS_OOM(m)) return;
545
+ } else {
546
+ BrotliBuildMetaBlock(m, data, wrapped_last_flush_pos, mask, &block_params,
547
+ prev_byte, prev_byte2,
548
+ commands, num_commands,
549
+ literal_context_mode,
550
+ &mb);
551
+ if (BROTLI_IS_OOM(m)) return;
552
+ }
553
+ if (params->quality >= MIN_QUALITY_FOR_OPTIMIZE_HISTOGRAMS) {
554
+ /* The number of distance symbols effectively used for distance
555
+ histograms. It might be less than distance alphabet size
556
+ for "Large Window Brotli" (32-bit). */
557
+ BrotliOptimizeHistograms(block_params.dist.alphabet_size_limit, &mb);
558
+ }
559
+ BrotliStoreMetaBlock(m, data, wrapped_last_flush_pos, bytes, mask,
560
+ prev_byte, prev_byte2,
561
+ is_last,
562
+ &block_params,
563
+ literal_context_mode,
564
+ commands, num_commands,
565
+ &mb,
566
+ storage_ix, storage);
567
+ if (BROTLI_IS_OOM(m)) return;
568
+ DestroyMetaBlockSplit(m, &mb);
569
+ }
570
+ if (bytes + 4 < (*storage_ix >> 3)) {
571
+ /* Restore the distance cache and last byte. */
572
+ memcpy(dist_cache, saved_dist_cache, 4 * sizeof(dist_cache[0]));
573
+ storage[0] = (uint8_t)last_bytes;
574
+ storage[1] = (uint8_t)(last_bytes >> 8);
575
+ *storage_ix = last_bytes_bits;
576
+ BrotliStoreUncompressedMetaBlock(is_last, data,
577
+ wrapped_last_flush_pos, mask,
578
+ bytes, storage_ix, storage);
579
+ }
580
+ }
581
+
582
+ static void ChooseDistanceParams(BrotliEncoderParams* params) {
583
+ uint32_t distance_postfix_bits = 0;
584
+ uint32_t num_direct_distance_codes = 0;
585
+
586
+ if (params->quality >= MIN_QUALITY_FOR_NONZERO_DISTANCE_PARAMS) {
587
+ uint32_t ndirect_msb;
588
+ if (params->mode == BROTLI_MODE_FONT) {
589
+ distance_postfix_bits = 1;
590
+ num_direct_distance_codes = 12;
591
+ } else {
592
+ distance_postfix_bits = params->dist.distance_postfix_bits;
593
+ num_direct_distance_codes = params->dist.num_direct_distance_codes;
594
+ }
595
+ ndirect_msb = (num_direct_distance_codes >> distance_postfix_bits) & 0x0F;
596
+ if (distance_postfix_bits > BROTLI_MAX_NPOSTFIX ||
597
+ num_direct_distance_codes > BROTLI_MAX_NDIRECT ||
598
+ (ndirect_msb << distance_postfix_bits) != num_direct_distance_codes) {
599
+ distance_postfix_bits = 0;
600
+ num_direct_distance_codes = 0;
601
+ }
602
+ }
603
+
604
+ BrotliInitDistanceParams(&params->dist, distance_postfix_bits,
605
+ num_direct_distance_codes, params->large_window);
606
+ }
607
+
608
+ static BROTLI_BOOL EnsureInitialized(BrotliEncoderState* s) {
609
+ MemoryManager* m = &s->memory_manager_;
610
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
611
+ if (s->is_initialized_) return BROTLI_TRUE;
612
+
613
+ s->last_bytes_bits_ = 0;
614
+ s->last_bytes_ = 0;
615
+ s->flint_ = BROTLI_FLINT_DONE;
616
+ s->remaining_metadata_bytes_ = BROTLI_UINT32_MAX;
617
+
618
+ SanitizeParams(&s->params);
619
+ s->params.lgblock = ComputeLgBlock(&s->params);
620
+ ChooseDistanceParams(&s->params);
621
+
622
+ if (s->params.stream_offset != 0) {
623
+ s->flint_ = BROTLI_FLINT_NEEDS_2_BYTES;
624
+ /* Poison the distance cache. -16 +- 3 is still less than zero (invalid). */
625
+ s->dist_cache_[0] = -16;
626
+ s->dist_cache_[1] = -16;
627
+ s->dist_cache_[2] = -16;
628
+ s->dist_cache_[3] = -16;
629
+ memcpy(s->saved_dist_cache_, s->dist_cache_, sizeof(s->saved_dist_cache_));
630
+ }
631
+
632
+ RingBufferSetup(&s->params, &s->ringbuffer_);
633
+
634
+ /* Initialize last byte with stream header. */
635
+ {
636
+ int lgwin = s->params.lgwin;
637
+ if (s->params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY ||
638
+ s->params.quality == FAST_TWO_PASS_COMPRESSION_QUALITY) {
639
+ lgwin = BROTLI_MAX(int, lgwin, 18);
640
+ }
641
+ if (s->params.stream_offset == 0) {
642
+ EncodeWindowBits(lgwin, s->params.large_window,
643
+ &s->last_bytes_, &s->last_bytes_bits_);
644
+ } else {
645
+ /* Bigger values have the same effect, but could cause overflows. */
646
+ s->params.stream_offset = BROTLI_MIN(size_t,
647
+ s->params.stream_offset, BROTLI_MAX_BACKWARD_LIMIT(lgwin));
648
+ }
649
+ }
650
+
651
+ if (s->params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY) {
652
+ s->one_pass_arena_ = BROTLI_ALLOC(m, BrotliOnePassArena, 1);
653
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
654
+ InitCommandPrefixCodes(s->one_pass_arena_);
655
+ } else if (s->params.quality == FAST_TWO_PASS_COMPRESSION_QUALITY) {
656
+ s->two_pass_arena_ = BROTLI_ALLOC(m, BrotliTwoPassArena, 1);
657
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
658
+ }
659
+
660
+ s->is_initialized_ = BROTLI_TRUE;
661
+ return BROTLI_TRUE;
662
+ }
663
+
664
+ static void BrotliEncoderInitParams(BrotliEncoderParams* params) {
665
+ params->mode = BROTLI_DEFAULT_MODE;
666
+ params->large_window = BROTLI_FALSE;
667
+ params->quality = BROTLI_DEFAULT_QUALITY;
668
+ params->lgwin = BROTLI_DEFAULT_WINDOW;
669
+ params->lgblock = 0;
670
+ params->stream_offset = 0;
671
+ params->size_hint = 0;
672
+ params->disable_literal_context_modeling = BROTLI_FALSE;
673
+ BrotliInitSharedEncoderDictionary(&params->dictionary);
674
+ params->dist.distance_postfix_bits = 0;
675
+ params->dist.num_direct_distance_codes = 0;
676
+ params->dist.alphabet_size_max =
677
+ BROTLI_DISTANCE_ALPHABET_SIZE(0, 0, BROTLI_MAX_DISTANCE_BITS);
678
+ params->dist.alphabet_size_limit = params->dist.alphabet_size_max;
679
+ params->dist.max_distance = BROTLI_MAX_DISTANCE;
680
+ }
681
+
682
+ static void BrotliEncoderCleanupParams(MemoryManager* m,
683
+ BrotliEncoderParams* params) {
684
+ BrotliCleanupSharedEncoderDictionary(m, &params->dictionary);
685
+ }
686
+
687
+ static void BrotliEncoderInitState(BrotliEncoderState* s) {
688
+ BrotliEncoderInitParams(&s->params);
689
+ s->input_pos_ = 0;
690
+ s->num_commands_ = 0;
691
+ s->num_literals_ = 0;
692
+ s->last_insert_len_ = 0;
693
+ s->last_flush_pos_ = 0;
694
+ s->last_processed_pos_ = 0;
695
+ s->prev_byte_ = 0;
696
+ s->prev_byte2_ = 0;
697
+ s->storage_size_ = 0;
698
+ s->storage_ = 0;
699
+ HasherInit(&s->hasher_);
700
+ s->large_table_ = NULL;
701
+ s->large_table_size_ = 0;
702
+ s->one_pass_arena_ = NULL;
703
+ s->two_pass_arena_ = NULL;
704
+ s->command_buf_ = NULL;
705
+ s->literal_buf_ = NULL;
706
+ s->total_in_ = 0;
707
+ s->next_out_ = NULL;
708
+ s->available_out_ = 0;
709
+ s->total_out_ = 0;
710
+ s->stream_state_ = BROTLI_STREAM_PROCESSING;
711
+ s->is_last_block_emitted_ = BROTLI_FALSE;
712
+ s->is_initialized_ = BROTLI_FALSE;
713
+
714
+ RingBufferInit(&s->ringbuffer_);
715
+
716
+ s->commands_ = 0;
717
+ s->cmd_alloc_size_ = 0;
718
+
719
+ /* Initialize distance cache. */
720
+ s->dist_cache_[0] = 4;
721
+ s->dist_cache_[1] = 11;
722
+ s->dist_cache_[2] = 15;
723
+ s->dist_cache_[3] = 16;
724
+ /* Save the state of the distance cache in case we need to restore it for
725
+ emitting an uncompressed block. */
726
+ memcpy(s->saved_dist_cache_, s->dist_cache_, sizeof(s->saved_dist_cache_));
727
+ }
728
+
729
+ BrotliEncoderState* duckdb_brotli::BrotliEncoderCreateInstance(
730
+ brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
731
+ BrotliEncoderState* state = (BrotliEncoderState*)BrotliBootstrapAlloc(
732
+ sizeof(BrotliEncoderState), alloc_func, free_func, opaque);
733
+ if (state == NULL) {
734
+ /* BROTLI_DUMP(); */
735
+ return 0;
736
+ }
737
+ BrotliInitMemoryManager(
738
+ &state->memory_manager_, alloc_func, free_func, opaque);
739
+ BrotliEncoderInitState(state);
740
+ return state;
741
+ }
742
+
743
+ #ifdef BROTLI_REPORTING
744
+ /* When BROTLI_REPORTING is defined extra reporting module have to be linked. */
745
+ void BrotliEncoderOnFinish(const BrotliEncoderState* s);
746
+ #define BROTLI_ENCODER_ON_FINISH(s) BrotliEncoderOnFinish(s);
747
+ #else
748
+ #if !defined(BROTLI_ENCODER_ON_FINISH)
749
+ #define BROTLI_ENCODER_ON_FINISH(s) (void)(s);
750
+ #endif
751
+ #endif
752
+
753
+ static void BrotliEncoderCleanupState(BrotliEncoderState* s) {
754
+ MemoryManager* m = &s->memory_manager_;
755
+
756
+ BROTLI_ENCODER_ON_FINISH(s);
757
+
758
+ if (BROTLI_IS_OOM(m)) {
759
+ BrotliWipeOutMemoryManager(m);
760
+ return;
761
+ }
762
+
763
+ BROTLI_FREE(m, s->storage_);
764
+ BROTLI_FREE(m, s->commands_);
765
+ RingBufferFree(m, &s->ringbuffer_);
766
+ DestroyHasher(m, &s->hasher_);
767
+ BROTLI_FREE(m, s->large_table_);
768
+ BROTLI_FREE(m, s->one_pass_arena_);
769
+ BROTLI_FREE(m, s->two_pass_arena_);
770
+ BROTLI_FREE(m, s->command_buf_);
771
+ BROTLI_FREE(m, s->literal_buf_);
772
+ BrotliEncoderCleanupParams(m, &s->params);
773
+ }
774
+
775
+ /* Deinitializes and frees BrotliEncoderState instance. */
776
+ void duckdb_brotli::BrotliEncoderDestroyInstance(BrotliEncoderState* state) {
777
+ if (!state) {
778
+ return;
779
+ } else {
780
+ BrotliEncoderCleanupState(state);
781
+ BrotliBootstrapFree(state, &state->memory_manager_);
782
+ }
783
+ }
784
+
785
+ /*
786
+ Copies the given input data to the internal ring buffer of the compressor.
787
+ No processing of the data occurs at this time and this function can be
788
+ called multiple times before calling WriteBrotliData() to process the
789
+ accumulated input. At most input_block_size() bytes of input data can be
790
+ copied to the ring buffer, otherwise the next WriteBrotliData() will fail.
791
+ */
792
+ static void CopyInputToRingBuffer(BrotliEncoderState* s,
793
+ const size_t input_size,
794
+ const uint8_t* input_buffer) {
795
+ RingBuffer* ringbuffer_ = &s->ringbuffer_;
796
+ MemoryManager* m = &s->memory_manager_;
797
+ RingBufferWrite(m, input_buffer, input_size, ringbuffer_);
798
+ if (BROTLI_IS_OOM(m)) return;
799
+ s->input_pos_ += input_size;
800
+
801
+ /* TL;DR: If needed, initialize 7 more bytes in the ring buffer to make the
802
+ hashing not depend on uninitialized data. This makes compression
803
+ deterministic and it prevents uninitialized memory warnings in Valgrind.
804
+ Even without erasing, the output would be valid (but nondeterministic).
805
+
806
+ Background information: The compressor stores short (at most 8 bytes)
807
+ substrings of the input already read in a hash table, and detects
808
+ repetitions by looking up such substrings in the hash table. If it
809
+ can find a substring, it checks whether the substring is really there
810
+ in the ring buffer (or it's just a hash collision). Should the hash
811
+ table become corrupt, this check makes sure that the output is
812
+ still valid, albeit the compression ratio would be bad.
813
+
814
+ The compressor populates the hash table from the ring buffer as it's
815
+ reading new bytes from the input. However, at the last few indexes of
816
+ the ring buffer, there are not enough bytes to build full-length
817
+ substrings from. Since the hash table always contains full-length
818
+ substrings, we erase with dummy zeros here to make sure that those
819
+ substrings will contain zeros at the end instead of uninitialized
820
+ data.
821
+
822
+ Please note that erasing is not necessary (because the
823
+ memory region is already initialized since he ring buffer
824
+ has a `tail' that holds a copy of the beginning,) so we
825
+ skip erasing if we have already gone around at least once in
826
+ the ring buffer.
827
+
828
+ Only clear during the first round of ring-buffer writes. On
829
+ subsequent rounds data in the ring-buffer would be affected. */
830
+ if (ringbuffer_->pos_ <= ringbuffer_->mask_) {
831
+ /* This is the first time when the ring buffer is being written.
832
+ We clear 7 bytes just after the bytes that have been copied from
833
+ the input buffer.
834
+
835
+ The ring-buffer has a "tail" that holds a copy of the beginning,
836
+ but only once the ring buffer has been fully written once, i.e.,
837
+ pos <= mask. For the first time, we need to write values
838
+ in this tail (where index may be larger than mask), so that
839
+ we have exactly defined behavior and don't read uninitialized
840
+ memory. Due to performance reasons, hashing reads data using a
841
+ LOAD64, which can go 7 bytes beyond the bytes written in the
842
+ ring-buffer. */
843
+ memset(ringbuffer_->buffer_ + ringbuffer_->pos_, 0, 7);
844
+ }
845
+ }
846
+
847
+ /* Marks all input as processed.
848
+ Returns true if position wrapping occurs. */
849
+ static BROTLI_BOOL UpdateLastProcessedPos(BrotliEncoderState* s) {
850
+ uint32_t wrapped_last_processed_pos = WrapPosition(s->last_processed_pos_);
851
+ uint32_t wrapped_input_pos = WrapPosition(s->input_pos_);
852
+ s->last_processed_pos_ = s->input_pos_;
853
+ return TO_BROTLI_BOOL(wrapped_input_pos < wrapped_last_processed_pos);
854
+ }
855
+
856
+ static void ExtendLastCommand(BrotliEncoderState* s, uint32_t* bytes,
857
+ uint32_t* wrapped_last_processed_pos) {
858
+ Command* last_command = &s->commands_[s->num_commands_ - 1];
859
+ const uint8_t* data = s->ringbuffer_.buffer_;
860
+ const uint32_t mask = s->ringbuffer_.mask_;
861
+ uint64_t max_backward_distance =
862
+ (((uint64_t)1) << s->params.lgwin) - BROTLI_WINDOW_GAP;
863
+ uint64_t last_copy_len = last_command->copy_len_ & 0x1FFFFFF;
864
+ uint64_t last_processed_pos = s->last_processed_pos_ - last_copy_len;
865
+ uint64_t max_distance = last_processed_pos < max_backward_distance ?
866
+ last_processed_pos : max_backward_distance;
867
+ uint64_t cmd_dist = (uint64_t)s->dist_cache_[0];
868
+ uint32_t distance_code = CommandRestoreDistanceCode(last_command,
869
+ &s->params.dist);
870
+ const CompoundDictionary* dict = &s->params.dictionary.compound;
871
+ size_t compound_dictionary_size = dict->total_size;
872
+ if (distance_code < BROTLI_NUM_DISTANCE_SHORT_CODES ||
873
+ distance_code - (BROTLI_NUM_DISTANCE_SHORT_CODES - 1) == cmd_dist) {
874
+ if (cmd_dist <= max_distance) {
875
+ while (*bytes != 0 && data[*wrapped_last_processed_pos & mask] ==
876
+ data[(*wrapped_last_processed_pos - cmd_dist) & mask]) {
877
+ last_command->copy_len_++;
878
+ (*bytes)--;
879
+ (*wrapped_last_processed_pos)++;
880
+ }
881
+ } else {
882
+ if ((cmd_dist - max_distance - 1) < compound_dictionary_size &&
883
+ last_copy_len < cmd_dist - max_distance) {
884
+ size_t address =
885
+ compound_dictionary_size - (size_t)(cmd_dist - max_distance) +
886
+ (size_t)last_copy_len;
887
+ size_t br_index = 0;
888
+ size_t br_offset;
889
+ const uint8_t* chunk;
890
+ size_t chunk_length;
891
+ while (address >= dict->chunk_offsets[br_index + 1]) br_index++;
892
+ br_offset = address - dict->chunk_offsets[br_index];
893
+ chunk = dict->chunk_source[br_index];
894
+ chunk_length =
895
+ dict->chunk_offsets[br_index + 1] - dict->chunk_offsets[br_index];
896
+ while (*bytes != 0 && data[*wrapped_last_processed_pos & mask] ==
897
+ chunk[br_offset]) {
898
+ last_command->copy_len_++;
899
+ (*bytes)--;
900
+ (*wrapped_last_processed_pos)++;
901
+ if (++br_offset == chunk_length) {
902
+ br_index++;
903
+ br_offset = 0;
904
+ if (br_index != dict->num_chunks) {
905
+ chunk = dict->chunk_source[br_index];
906
+ chunk_length = dict->chunk_offsets[br_index + 1] -
907
+ dict->chunk_offsets[br_index];
908
+ } else {
909
+ break;
910
+ }
911
+ }
912
+ }
913
+ }
914
+ }
915
+ /* The copy length is at most the metablock size, and thus expressible. */
916
+ GetLengthCode(last_command->insert_len_,
917
+ (size_t)((int)(last_command->copy_len_ & 0x1FFFFFF) +
918
+ (int)(last_command->copy_len_ >> 25)),
919
+ TO_BROTLI_BOOL((last_command->dist_prefix_ & 0x3FF) == 0),
920
+ &last_command->cmd_prefix_);
921
+ }
922
+ }
923
+
924
+ /*
925
+ Processes the accumulated input data and sets |*out_size| to the length of
926
+ the new output meta-block, or to zero if no new output meta-block has been
927
+ created (in this case the processed input data is buffered internally).
928
+ If |*out_size| is positive, |*output| points to the start of the output
929
+ data. If |is_last| or |force_flush| is BROTLI_TRUE, an output meta-block is
930
+ always created. However, until |is_last| is BROTLI_TRUE encoder may retain up
931
+ to 7 bits of the last byte of output. Byte-alignment could be enforced by
932
+ emitting an empty meta-data block.
933
+ Returns BROTLI_FALSE if the size of the input data is larger than
934
+ input_block_size().
935
+ */
936
+ static BROTLI_BOOL EncodeData(
937
+ BrotliEncoderState* s, const BROTLI_BOOL is_last,
938
+ const BROTLI_BOOL force_flush, size_t* out_size, uint8_t** output) {
939
+ const uint64_t delta = UnprocessedInputSize(s);
940
+ uint32_t bytes = (uint32_t)delta;
941
+ uint32_t wrapped_last_processed_pos = WrapPosition(s->last_processed_pos_);
942
+ uint8_t* data;
943
+ uint32_t mask;
944
+ MemoryManager* m = &s->memory_manager_;
945
+ ContextType literal_context_mode;
946
+ ContextLut literal_context_lut;
947
+ BROTLI_BOOL fast_compress =
948
+ s->params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY ||
949
+ s->params.quality == FAST_TWO_PASS_COMPRESSION_QUALITY;
950
+
951
+ data = s->ringbuffer_.buffer_;
952
+ mask = s->ringbuffer_.mask_;
953
+
954
+ if (delta == 0) { /* No new input; still might want to flush or finish. */
955
+ if (!data) { /* No input has been processed so far. */
956
+ if (is_last) { /* Emit complete finalized stream. */
957
+ BROTLI_DCHECK(s->last_bytes_bits_ <= 14);
958
+ s->last_bytes_ |= (uint16_t)(3u << s->last_bytes_bits_);
959
+ s->last_bytes_bits_ = (uint8_t)(s->last_bytes_bits_ + 2u);
960
+ s->tiny_buf_.u8[0] = (uint8_t)s->last_bytes_;
961
+ s->tiny_buf_.u8[1] = (uint8_t)(s->last_bytes_ >> 8);
962
+ *output = s->tiny_buf_.u8;
963
+ *out_size = (s->last_bytes_bits_ + 7u) >> 3u;
964
+ return BROTLI_TRUE;
965
+ } else { /* No data, not last -> no-op. */
966
+ *out_size = 0;
967
+ return BROTLI_TRUE;
968
+ }
969
+ } else {
970
+ /* Fast compress performs flush every block -> flush is no-op. */
971
+ if (!is_last && (!force_flush || fast_compress)) { /* Another no-op. */
972
+ *out_size = 0;
973
+ return BROTLI_TRUE;
974
+ }
975
+ }
976
+ }
977
+ BROTLI_DCHECK(data);
978
+
979
+ if (s->params.quality > s->params.dictionary.max_quality) return BROTLI_FALSE;
980
+ /* Adding more blocks after "last" block is forbidden. */
981
+ if (s->is_last_block_emitted_) return BROTLI_FALSE;
982
+ if (is_last) s->is_last_block_emitted_ = BROTLI_TRUE;
983
+
984
+ if (delta > InputBlockSize(s)) {
985
+ return BROTLI_FALSE;
986
+ }
987
+ if (s->params.quality == FAST_TWO_PASS_COMPRESSION_QUALITY &&
988
+ !s->command_buf_) {
989
+ s->command_buf_ =
990
+ BROTLI_ALLOC(m, uint32_t, kCompressFragmentTwoPassBlockSize);
991
+ s->literal_buf_ =
992
+ BROTLI_ALLOC(m, uint8_t, kCompressFragmentTwoPassBlockSize);
993
+ if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(s->command_buf_) ||
994
+ BROTLI_IS_NULL(s->literal_buf_)) {
995
+ return BROTLI_FALSE;
996
+ }
997
+ }
998
+
999
+ if (fast_compress) {
1000
+ uint8_t* storage;
1001
+ size_t storage_ix = s->last_bytes_bits_;
1002
+ size_t table_size;
1003
+ int* table;
1004
+
1005
+ storage = GetBrotliStorage(s, 2 * bytes + 503);
1006
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1007
+ storage[0] = (uint8_t)s->last_bytes_;
1008
+ storage[1] = (uint8_t)(s->last_bytes_ >> 8);
1009
+ table = GetHashTable(s, s->params.quality, bytes, &table_size);
1010
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1011
+ if (s->params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY) {
1012
+ BrotliCompressFragmentFast(
1013
+ s->one_pass_arena_, &data[wrapped_last_processed_pos & mask],
1014
+ bytes, is_last,
1015
+ table, table_size,
1016
+ &storage_ix, storage);
1017
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1018
+ } else {
1019
+ BrotliCompressFragmentTwoPass(
1020
+ s->two_pass_arena_, &data[wrapped_last_processed_pos & mask],
1021
+ bytes, is_last,
1022
+ s->command_buf_, s->literal_buf_,
1023
+ table, table_size,
1024
+ &storage_ix, storage);
1025
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1026
+ }
1027
+ s->last_bytes_ = (uint16_t)(storage[storage_ix >> 3]);
1028
+ s->last_bytes_bits_ = storage_ix & 7u;
1029
+ UpdateLastProcessedPos(s);
1030
+ *output = &storage[0];
1031
+ *out_size = storage_ix >> 3;
1032
+ return BROTLI_TRUE;
1033
+ }
1034
+
1035
+ {
1036
+ /* Theoretical max number of commands is 1 per 2 bytes. */
1037
+ size_t newsize = s->num_commands_ + bytes / 2 + 1;
1038
+ if (newsize > s->cmd_alloc_size_) {
1039
+ Command* new_commands;
1040
+ /* Reserve a bit more memory to allow merging with a next block
1041
+ without reallocation: that would impact speed. */
1042
+ newsize += (bytes / 4) + 16;
1043
+ s->cmd_alloc_size_ = newsize;
1044
+ new_commands = BROTLI_ALLOC(m, Command, newsize);
1045
+ if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(new_commands)) return BROTLI_FALSE;
1046
+ if (s->commands_) {
1047
+ memcpy(new_commands, s->commands_, sizeof(Command) * s->num_commands_);
1048
+ BROTLI_FREE(m, s->commands_);
1049
+ }
1050
+ s->commands_ = new_commands;
1051
+ }
1052
+ }
1053
+
1054
+ InitOrStitchToPreviousBlock(m, &s->hasher_, data, mask, &s->params,
1055
+ wrapped_last_processed_pos, bytes, is_last);
1056
+
1057
+ literal_context_mode = ChooseContextMode(
1058
+ &s->params, data, WrapPosition(s->last_flush_pos_),
1059
+ mask, (size_t)(s->input_pos_ - s->last_flush_pos_));
1060
+ literal_context_lut = BROTLI_CONTEXT_LUT(literal_context_mode);
1061
+
1062
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1063
+
1064
+ if (s->num_commands_ && s->last_insert_len_ == 0) {
1065
+ ExtendLastCommand(s, &bytes, &wrapped_last_processed_pos);
1066
+ }
1067
+
1068
+ if (s->params.quality == ZOPFLIFICATION_QUALITY) {
1069
+ BROTLI_DCHECK(s->params.hasher.type == 10);
1070
+ BrotliCreateZopfliBackwardReferences(m, bytes, wrapped_last_processed_pos,
1071
+ data, mask, literal_context_lut, &s->params,
1072
+ &s->hasher_, s->dist_cache_,
1073
+ &s->last_insert_len_, &s->commands_[s->num_commands_],
1074
+ &s->num_commands_, &s->num_literals_);
1075
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1076
+ } else if (s->params.quality == HQ_ZOPFLIFICATION_QUALITY) {
1077
+ BROTLI_DCHECK(s->params.hasher.type == 10);
1078
+ BrotliCreateHqZopfliBackwardReferences(m, bytes, wrapped_last_processed_pos,
1079
+ data, mask, literal_context_lut, &s->params,
1080
+ &s->hasher_, s->dist_cache_,
1081
+ &s->last_insert_len_, &s->commands_[s->num_commands_],
1082
+ &s->num_commands_, &s->num_literals_);
1083
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1084
+ } else {
1085
+ BrotliCreateBackwardReferences(bytes, wrapped_last_processed_pos,
1086
+ data, mask, literal_context_lut, &s->params,
1087
+ &s->hasher_, s->dist_cache_,
1088
+ &s->last_insert_len_, &s->commands_[s->num_commands_],
1089
+ &s->num_commands_, &s->num_literals_);
1090
+ }
1091
+
1092
+ {
1093
+ const size_t max_length = MaxMetablockSize(&s->params);
1094
+ const size_t max_literals = max_length / 8;
1095
+ const size_t max_commands = max_length / 8;
1096
+ const size_t processed_bytes = (size_t)(s->input_pos_ - s->last_flush_pos_);
1097
+ /* If maximal possible additional block doesn't fit metablock, flush now. */
1098
+ /* TODO(eustas): Postpone decision until next block arrives? */
1099
+ const BROTLI_BOOL next_input_fits_metablock = TO_BROTLI_BOOL(
1100
+ processed_bytes + InputBlockSize(s) <= max_length);
1101
+ /* If block splitting is not used, then flush as soon as there is some
1102
+ amount of commands / literals produced. */
1103
+ const BROTLI_BOOL should_flush = TO_BROTLI_BOOL(
1104
+ s->params.quality < MIN_QUALITY_FOR_BLOCK_SPLIT &&
1105
+ s->num_literals_ + s->num_commands_ >= MAX_NUM_DELAYED_SYMBOLS);
1106
+ if (!is_last && !force_flush && !should_flush &&
1107
+ next_input_fits_metablock &&
1108
+ s->num_literals_ < max_literals &&
1109
+ s->num_commands_ < max_commands) {
1110
+ /* Merge with next input block. Everything will happen later. */
1111
+ if (UpdateLastProcessedPos(s)) {
1112
+ HasherReset(&s->hasher_);
1113
+ }
1114
+ *out_size = 0;
1115
+ return BROTLI_TRUE;
1116
+ }
1117
+ }
1118
+
1119
+ /* Create the last insert-only command. */
1120
+ if (s->last_insert_len_ > 0) {
1121
+ InitInsertCommand(&s->commands_[s->num_commands_++], s->last_insert_len_);
1122
+ s->num_literals_ += s->last_insert_len_;
1123
+ s->last_insert_len_ = 0;
1124
+ }
1125
+
1126
+ if (!is_last && s->input_pos_ == s->last_flush_pos_) {
1127
+ /* We have no new input data and we don't have to finish the stream, so
1128
+ nothing to do. */
1129
+ *out_size = 0;
1130
+ return BROTLI_TRUE;
1131
+ }
1132
+ BROTLI_DCHECK(s->input_pos_ >= s->last_flush_pos_);
1133
+ BROTLI_DCHECK(s->input_pos_ > s->last_flush_pos_ || is_last);
1134
+ BROTLI_DCHECK(s->input_pos_ - s->last_flush_pos_ <= 1u << 24);
1135
+ {
1136
+ const uint32_t metablock_size =
1137
+ (uint32_t)(s->input_pos_ - s->last_flush_pos_);
1138
+ uint8_t* storage = GetBrotliStorage(s, 2 * metablock_size + 503);
1139
+ size_t storage_ix = s->last_bytes_bits_;
1140
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1141
+ storage[0] = (uint8_t)s->last_bytes_;
1142
+ storage[1] = (uint8_t)(s->last_bytes_ >> 8);
1143
+ WriteMetaBlockInternal(
1144
+ m, data, mask, s->last_flush_pos_, metablock_size, is_last,
1145
+ literal_context_mode, &s->params, s->prev_byte_, s->prev_byte2_,
1146
+ s->num_literals_, s->num_commands_, s->commands_, s->saved_dist_cache_,
1147
+ s->dist_cache_, &storage_ix, storage);
1148
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1149
+ s->last_bytes_ = (uint16_t)(storage[storage_ix >> 3]);
1150
+ s->last_bytes_bits_ = storage_ix & 7u;
1151
+ s->last_flush_pos_ = s->input_pos_;
1152
+ if (UpdateLastProcessedPos(s)) {
1153
+ HasherReset(&s->hasher_);
1154
+ }
1155
+ if (s->last_flush_pos_ > 0) {
1156
+ s->prev_byte_ = data[((uint32_t)s->last_flush_pos_ - 1) & mask];
1157
+ }
1158
+ if (s->last_flush_pos_ > 1) {
1159
+ s->prev_byte2_ = data[(uint32_t)(s->last_flush_pos_ - 2) & mask];
1160
+ }
1161
+ s->num_commands_ = 0;
1162
+ s->num_literals_ = 0;
1163
+ /* Save the state of the distance cache in case we need to restore it for
1164
+ emitting an uncompressed block. */
1165
+ memcpy(s->saved_dist_cache_, s->dist_cache_, sizeof(s->saved_dist_cache_));
1166
+ *output = &storage[0];
1167
+ *out_size = storage_ix >> 3;
1168
+ return BROTLI_TRUE;
1169
+ }
1170
+ }
1171
+
1172
+ /* Dumps remaining output bits and metadata header to |header|.
1173
+ Returns number of produced bytes.
1174
+ REQUIRED: |header| should be 8-byte aligned and at least 16 bytes long.
1175
+ REQUIRED: |block_size| <= (1 << 24). */
1176
+ static size_t WriteMetadataHeader(
1177
+ BrotliEncoderState* s, const size_t block_size, uint8_t* header) {
1178
+ size_t storage_ix;
1179
+ storage_ix = s->last_bytes_bits_;
1180
+ header[0] = (uint8_t)s->last_bytes_;
1181
+ header[1] = (uint8_t)(s->last_bytes_ >> 8);
1182
+ s->last_bytes_ = 0;
1183
+ s->last_bytes_bits_ = 0;
1184
+
1185
+ BrotliWriteBits(1, 0, &storage_ix, header);
1186
+ BrotliWriteBits(2, 3, &storage_ix, header);
1187
+ BrotliWriteBits(1, 0, &storage_ix, header);
1188
+ if (block_size == 0) {
1189
+ BrotliWriteBits(2, 0, &storage_ix, header);
1190
+ } else {
1191
+ uint32_t nbits = (block_size == 1) ? 1 :
1192
+ (Log2FloorNonZero((uint32_t)block_size - 1) + 1);
1193
+ uint32_t nbytes = (nbits + 7) / 8;
1194
+ BrotliWriteBits(2, nbytes, &storage_ix, header);
1195
+ BrotliWriteBits(8 * nbytes, block_size - 1, &storage_ix, header);
1196
+ }
1197
+ return (storage_ix + 7u) >> 3;
1198
+ }
1199
+
1200
+ size_t duckdb_brotli::BrotliEncoderMaxCompressedSize(size_t input_size) {
1201
+ /* [window bits / empty metadata] + N * [uncompressed] + [last empty] */
1202
+ size_t num_large_blocks = input_size >> 14;
1203
+ size_t overhead = 2 + (4 * num_large_blocks) + 3 + 1;
1204
+ size_t result = input_size + overhead;
1205
+ if (input_size == 0) return 2;
1206
+ return (result < input_size) ? 0 : result;
1207
+ }
1208
+
1209
+ /* Wraps data to uncompressed brotli stream with minimal window size.
1210
+ |output| should point at region with at least BrotliEncoderMaxCompressedSize
1211
+ addressable bytes.
1212
+ Returns the length of stream. */
1213
+ static size_t MakeUncompressedStream(
1214
+ const uint8_t* input, size_t input_size, uint8_t* output) {
1215
+ size_t size = input_size;
1216
+ size_t result = 0;
1217
+ size_t offset = 0;
1218
+ if (input_size == 0) {
1219
+ output[0] = 6;
1220
+ return 1;
1221
+ }
1222
+ output[result++] = 0x21; /* window bits = 10, is_last = false */
1223
+ output[result++] = 0x03; /* empty metadata, padding */
1224
+ while (size > 0) {
1225
+ uint32_t nibbles = 0;
1226
+ uint32_t chunk_size;
1227
+ uint32_t bits;
1228
+ chunk_size = (size > (1u << 24)) ? (1u << 24) : (uint32_t)size;
1229
+ if (chunk_size > (1u << 16)) nibbles = (chunk_size > (1u << 20)) ? 2 : 1;
1230
+ bits =
1231
+ (nibbles << 1) | ((chunk_size - 1) << 3) | (1u << (19 + 4 * nibbles));
1232
+ output[result++] = (uint8_t)bits;
1233
+ output[result++] = (uint8_t)(bits >> 8);
1234
+ output[result++] = (uint8_t)(bits >> 16);
1235
+ if (nibbles == 2) output[result++] = (uint8_t)(bits >> 24);
1236
+ memcpy(&output[result], &input[offset], chunk_size);
1237
+ result += chunk_size;
1238
+ offset += chunk_size;
1239
+ size -= chunk_size;
1240
+ }
1241
+ output[result++] = 3;
1242
+ return result;
1243
+ }
1244
+
1245
+ BROTLI_BOOL duckdb_brotli::BrotliEncoderCompress(
1246
+ int quality, int lgwin, BrotliEncoderMode mode, size_t input_size,
1247
+ const uint8_t input_buffer[BROTLI_ARRAY_PARAM(input_size)],
1248
+ size_t* encoded_size,
1249
+ uint8_t encoded_buffer[BROTLI_ARRAY_PARAM(*encoded_size)]) {
1250
+ BrotliEncoderState* s;
1251
+ size_t out_size = *encoded_size;
1252
+ const uint8_t* input_start = input_buffer;
1253
+ uint8_t* output_start = encoded_buffer;
1254
+ size_t max_out_size = BrotliEncoderMaxCompressedSize(input_size);
1255
+ if (out_size == 0) {
1256
+ /* Output buffer needs at least one byte. */
1257
+ return BROTLI_FALSE;
1258
+ }
1259
+ if (input_size == 0) {
1260
+ /* Handle the special case of empty input. */
1261
+ *encoded_size = 1;
1262
+ *encoded_buffer = 6;
1263
+ return BROTLI_TRUE;
1264
+ }
1265
+
1266
+ s = BrotliEncoderCreateInstance(0, 0, 0);
1267
+ if (!s) {
1268
+ return BROTLI_FALSE;
1269
+ } else {
1270
+ size_t available_in = input_size;
1271
+ const uint8_t* next_in = input_buffer;
1272
+ size_t available_out = *encoded_size;
1273
+ uint8_t* next_out = encoded_buffer;
1274
+ size_t total_out = 0;
1275
+ BROTLI_BOOL result = BROTLI_FALSE;
1276
+ /* TODO(eustas): check that parameters are sane. */
1277
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_QUALITY, (uint32_t)quality);
1278
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_LGWIN, (uint32_t)lgwin);
1279
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_MODE, (uint32_t)mode);
1280
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_SIZE_HINT, (uint32_t)input_size);
1281
+ if (lgwin > BROTLI_MAX_WINDOW_BITS) {
1282
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_LARGE_WINDOW, BROTLI_TRUE);
1283
+ }
1284
+ result = BrotliEncoderCompressStream(s, BROTLI_OPERATION_FINISH,
1285
+ &available_in, &next_in, &available_out, &next_out, &total_out);
1286
+ if (!BrotliEncoderIsFinished(s)) result = 0;
1287
+ *encoded_size = total_out;
1288
+ BrotliEncoderDestroyInstance(s);
1289
+ if (!result || (max_out_size && *encoded_size > max_out_size)) {
1290
+ goto fallback;
1291
+ }
1292
+ return BROTLI_TRUE;
1293
+ }
1294
+ fallback:
1295
+ *encoded_size = 0;
1296
+ if (!max_out_size) return BROTLI_FALSE;
1297
+ if (out_size >= max_out_size) {
1298
+ *encoded_size =
1299
+ MakeUncompressedStream(input_start, input_size, output_start);
1300
+ return BROTLI_TRUE;
1301
+ }
1302
+ return BROTLI_FALSE;
1303
+ }
1304
+
1305
+ static void InjectBytePaddingBlock(BrotliEncoderState* s) {
1306
+ uint32_t seal = s->last_bytes_;
1307
+ size_t seal_bits = s->last_bytes_bits_;
1308
+ uint8_t* destination;
1309
+ s->last_bytes_ = 0;
1310
+ s->last_bytes_bits_ = 0;
1311
+ /* is_last = 0, data_nibbles = 11, reserved = 0, meta_nibbles = 00 */
1312
+ seal |= 0x6u << seal_bits;
1313
+ seal_bits += 6;
1314
+ /* If we have already created storage, then append to it.
1315
+ Storage is valid until next block is being compressed. */
1316
+ if (s->next_out_) {
1317
+ destination = s->next_out_ + s->available_out_;
1318
+ } else {
1319
+ destination = s->tiny_buf_.u8;
1320
+ s->next_out_ = destination;
1321
+ }
1322
+ destination[0] = (uint8_t)seal;
1323
+ if (seal_bits > 8) destination[1] = (uint8_t)(seal >> 8);
1324
+ if (seal_bits > 16) destination[2] = (uint8_t)(seal >> 16);
1325
+ s->available_out_ += (seal_bits + 7) >> 3;
1326
+ }
1327
+
1328
+ /* Fills the |total_out|, if it is not NULL. */
1329
+ static void SetTotalOut(BrotliEncoderState* s, size_t* total_out) {
1330
+ if (total_out) {
1331
+ /* Saturating conversion uint64_t -> size_t */
1332
+ size_t result = (size_t)-1;
1333
+ if (s->total_out_ < result) {
1334
+ result = (size_t)s->total_out_;
1335
+ }
1336
+ *total_out = result;
1337
+ }
1338
+ }
1339
+
1340
+ /* Injects padding bits or pushes compressed data to output.
1341
+ Returns false if nothing is done. */
1342
+ static BROTLI_BOOL InjectFlushOrPushOutput(BrotliEncoderState* s,
1343
+ size_t* available_out, uint8_t** next_out, size_t* total_out) {
1344
+ if (s->stream_state_ == BROTLI_STREAM_FLUSH_REQUESTED &&
1345
+ s->last_bytes_bits_ != 0) {
1346
+ InjectBytePaddingBlock(s);
1347
+ return BROTLI_TRUE;
1348
+ }
1349
+
1350
+ if (s->available_out_ != 0 && *available_out != 0) {
1351
+ size_t copy_output_size =
1352
+ BROTLI_MIN(size_t, s->available_out_, *available_out);
1353
+ memcpy(*next_out, s->next_out_, copy_output_size);
1354
+ *next_out += copy_output_size;
1355
+ *available_out -= copy_output_size;
1356
+ s->next_out_ += copy_output_size;
1357
+ s->available_out_ -= copy_output_size;
1358
+ s->total_out_ += copy_output_size;
1359
+ SetTotalOut(s, total_out);
1360
+ return BROTLI_TRUE;
1361
+ }
1362
+
1363
+ return BROTLI_FALSE;
1364
+ }
1365
+
1366
+ static void CheckFlushComplete(BrotliEncoderState* s) {
1367
+ if (s->stream_state_ == BROTLI_STREAM_FLUSH_REQUESTED &&
1368
+ s->available_out_ == 0) {
1369
+ s->stream_state_ = BROTLI_STREAM_PROCESSING;
1370
+ s->next_out_ = 0;
1371
+ }
1372
+ }
1373
+
1374
+ static BROTLI_BOOL BrotliEncoderCompressStreamFast(
1375
+ BrotliEncoderState* s, BrotliEncoderOperation op, size_t* available_in,
1376
+ const uint8_t** next_in, size_t* available_out, uint8_t** next_out,
1377
+ size_t* total_out) {
1378
+ const size_t block_size_limit = (size_t)1 << s->params.lgwin;
1379
+ const size_t buf_size = BROTLI_MIN(size_t, kCompressFragmentTwoPassBlockSize,
1380
+ BROTLI_MIN(size_t, *available_in, block_size_limit));
1381
+ uint32_t* tmp_command_buf = NULL;
1382
+ uint32_t* command_buf = NULL;
1383
+ uint8_t* tmp_literal_buf = NULL;
1384
+ uint8_t* literal_buf = NULL;
1385
+ MemoryManager* m = &s->memory_manager_;
1386
+ if (s->params.quality != FAST_ONE_PASS_COMPRESSION_QUALITY &&
1387
+ s->params.quality != FAST_TWO_PASS_COMPRESSION_QUALITY) {
1388
+ return BROTLI_FALSE;
1389
+ }
1390
+ if (s->params.quality == FAST_TWO_PASS_COMPRESSION_QUALITY) {
1391
+ if (!s->command_buf_ && buf_size == kCompressFragmentTwoPassBlockSize) {
1392
+ s->command_buf_ =
1393
+ BROTLI_ALLOC(m, uint32_t, kCompressFragmentTwoPassBlockSize);
1394
+ s->literal_buf_ =
1395
+ BROTLI_ALLOC(m, uint8_t, kCompressFragmentTwoPassBlockSize);
1396
+ if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(s->command_buf_) ||
1397
+ BROTLI_IS_NULL(s->literal_buf_)) {
1398
+ return BROTLI_FALSE;
1399
+ }
1400
+ }
1401
+ if (s->command_buf_) {
1402
+ command_buf = s->command_buf_;
1403
+ literal_buf = s->literal_buf_;
1404
+ } else {
1405
+ tmp_command_buf = BROTLI_ALLOC(m, uint32_t, buf_size);
1406
+ tmp_literal_buf = BROTLI_ALLOC(m, uint8_t, buf_size);
1407
+ if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(tmp_command_buf) ||
1408
+ BROTLI_IS_NULL(tmp_literal_buf)) {
1409
+ return BROTLI_FALSE;
1410
+ }
1411
+ command_buf = tmp_command_buf;
1412
+ literal_buf = tmp_literal_buf;
1413
+ }
1414
+ }
1415
+
1416
+ while (BROTLI_TRUE) {
1417
+ if (InjectFlushOrPushOutput(s, available_out, next_out, total_out)) {
1418
+ continue;
1419
+ }
1420
+
1421
+ /* Compress block only when internal output buffer is empty, stream is not
1422
+ finished, there is no pending flush request, and there is either
1423
+ additional input or pending operation. */
1424
+ if (s->available_out_ == 0 &&
1425
+ s->stream_state_ == BROTLI_STREAM_PROCESSING &&
1426
+ (*available_in != 0 || op != BROTLI_OPERATION_PROCESS)) {
1427
+ size_t block_size = BROTLI_MIN(size_t, block_size_limit, *available_in);
1428
+ BROTLI_BOOL is_last =
1429
+ (*available_in == block_size) && (op == BROTLI_OPERATION_FINISH);
1430
+ BROTLI_BOOL force_flush =
1431
+ (*available_in == block_size) && (op == BROTLI_OPERATION_FLUSH);
1432
+ size_t max_out_size = 2 * block_size + 503;
1433
+ BROTLI_BOOL inplace = BROTLI_TRUE;
1434
+ uint8_t* storage = NULL;
1435
+ size_t storage_ix = s->last_bytes_bits_;
1436
+ size_t table_size;
1437
+ int* table;
1438
+
1439
+ if (force_flush && block_size == 0) {
1440
+ s->stream_state_ = BROTLI_STREAM_FLUSH_REQUESTED;
1441
+ continue;
1442
+ }
1443
+ if (max_out_size <= *available_out) {
1444
+ storage = *next_out;
1445
+ } else {
1446
+ inplace = BROTLI_FALSE;
1447
+ storage = GetBrotliStorage(s, max_out_size);
1448
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1449
+ }
1450
+ storage[0] = (uint8_t)s->last_bytes_;
1451
+ storage[1] = (uint8_t)(s->last_bytes_ >> 8);
1452
+ table = GetHashTable(s, s->params.quality, block_size, &table_size);
1453
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1454
+
1455
+ if (s->params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY) {
1456
+ BrotliCompressFragmentFast(s->one_pass_arena_, *next_in, block_size,
1457
+ is_last, table, table_size, &storage_ix, storage);
1458
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1459
+ } else {
1460
+ BrotliCompressFragmentTwoPass(s->two_pass_arena_, *next_in, block_size,
1461
+ is_last, command_buf, literal_buf, table, table_size,
1462
+ &storage_ix, storage);
1463
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1464
+ }
1465
+ if (block_size != 0) {
1466
+ *next_in += block_size;
1467
+ *available_in -= block_size;
1468
+ s->total_in_ += block_size;
1469
+ }
1470
+ if (inplace) {
1471
+ size_t out_bytes = storage_ix >> 3;
1472
+ BROTLI_DCHECK(out_bytes <= *available_out);
1473
+ BROTLI_DCHECK((storage_ix & 7) == 0 || out_bytes < *available_out);
1474
+ *next_out += out_bytes;
1475
+ *available_out -= out_bytes;
1476
+ s->total_out_ += out_bytes;
1477
+ SetTotalOut(s, total_out);
1478
+ } else {
1479
+ size_t out_bytes = storage_ix >> 3;
1480
+ s->next_out_ = storage;
1481
+ s->available_out_ = out_bytes;
1482
+ }
1483
+ s->last_bytes_ = (uint16_t)(storage[storage_ix >> 3]);
1484
+ s->last_bytes_bits_ = storage_ix & 7u;
1485
+
1486
+ if (force_flush) s->stream_state_ = BROTLI_STREAM_FLUSH_REQUESTED;
1487
+ if (is_last) s->stream_state_ = BROTLI_STREAM_FINISHED;
1488
+ continue;
1489
+ }
1490
+ break;
1491
+ }
1492
+ BROTLI_FREE(m, tmp_command_buf);
1493
+ BROTLI_FREE(m, tmp_literal_buf);
1494
+ CheckFlushComplete(s);
1495
+ return BROTLI_TRUE;
1496
+ }
1497
+
1498
+ static BROTLI_BOOL ProcessMetadata(
1499
+ BrotliEncoderState* s, size_t* available_in, const uint8_t** next_in,
1500
+ size_t* available_out, uint8_t** next_out, size_t* total_out) {
1501
+ if (*available_in > (1u << 24)) return BROTLI_FALSE;
1502
+ /* Switch to metadata block workflow, if required. */
1503
+ if (s->stream_state_ == BROTLI_STREAM_PROCESSING) {
1504
+ s->remaining_metadata_bytes_ = (uint32_t)*available_in;
1505
+ s->stream_state_ = BROTLI_STREAM_METADATA_HEAD;
1506
+ }
1507
+ if (s->stream_state_ != BROTLI_STREAM_METADATA_HEAD &&
1508
+ s->stream_state_ != BROTLI_STREAM_METADATA_BODY) {
1509
+ return BROTLI_FALSE;
1510
+ }
1511
+
1512
+ while (BROTLI_TRUE) {
1513
+ if (InjectFlushOrPushOutput(s, available_out, next_out, total_out)) {
1514
+ continue;
1515
+ }
1516
+ if (s->available_out_ != 0) break;
1517
+
1518
+ if (s->input_pos_ != s->last_flush_pos_) {
1519
+ BROTLI_BOOL result = EncodeData(s, BROTLI_FALSE, BROTLI_TRUE,
1520
+ &s->available_out_, &s->next_out_);
1521
+ if (!result) return BROTLI_FALSE;
1522
+ continue;
1523
+ }
1524
+
1525
+ if (s->stream_state_ == BROTLI_STREAM_METADATA_HEAD) {
1526
+ s->next_out_ = s->tiny_buf_.u8;
1527
+ s->available_out_ =
1528
+ WriteMetadataHeader(s, s->remaining_metadata_bytes_, s->next_out_);
1529
+ s->stream_state_ = BROTLI_STREAM_METADATA_BODY;
1530
+ continue;
1531
+ } else {
1532
+ /* Exit workflow only when there is no more input and no more output.
1533
+ Otherwise client may continue producing empty metadata blocks. */
1534
+ if (s->remaining_metadata_bytes_ == 0) {
1535
+ s->remaining_metadata_bytes_ = BROTLI_UINT32_MAX;
1536
+ s->stream_state_ = BROTLI_STREAM_PROCESSING;
1537
+ break;
1538
+ }
1539
+ if (*available_out) {
1540
+ /* Directly copy input to output. */
1541
+ uint32_t copy = (uint32_t)BROTLI_MIN(
1542
+ size_t, s->remaining_metadata_bytes_, *available_out);
1543
+ memcpy(*next_out, *next_in, copy);
1544
+ *next_in += copy;
1545
+ *available_in -= copy;
1546
+ s->total_in_ += copy; /* not actually data input, though */
1547
+ s->remaining_metadata_bytes_ -= copy;
1548
+ *next_out += copy;
1549
+ *available_out -= copy;
1550
+ } else {
1551
+ /* This guarantees progress in "TakeOutput" workflow. */
1552
+ uint32_t copy = BROTLI_MIN(uint32_t, s->remaining_metadata_bytes_, 16);
1553
+ s->next_out_ = s->tiny_buf_.u8;
1554
+ memcpy(s->next_out_, *next_in, copy);
1555
+ *next_in += copy;
1556
+ *available_in -= copy;
1557
+ s->total_in_ += copy; /* not actually data input, though */
1558
+ s->remaining_metadata_bytes_ -= copy;
1559
+ s->available_out_ = copy;
1560
+ }
1561
+ continue;
1562
+ }
1563
+ }
1564
+
1565
+ return BROTLI_TRUE;
1566
+ }
1567
+
1568
+ static void UpdateSizeHint(BrotliEncoderState* s, size_t available_in) {
1569
+ if (s->params.size_hint == 0) {
1570
+ uint64_t delta = UnprocessedInputSize(s);
1571
+ uint64_t tail = available_in;
1572
+ uint32_t limit = 1u << 30;
1573
+ uint32_t total;
1574
+ if ((delta >= limit) || (tail >= limit) || ((delta + tail) >= limit)) {
1575
+ total = limit;
1576
+ } else {
1577
+ total = (uint32_t)(delta + tail);
1578
+ }
1579
+ s->params.size_hint = total;
1580
+ }
1581
+ }
1582
+
1583
+ BROTLI_BOOL duckdb_brotli::BrotliEncoderCompressStream(
1584
+ BrotliEncoderState* s, BrotliEncoderOperation op, size_t* available_in,
1585
+ const uint8_t** next_in, size_t* available_out, uint8_t** next_out,
1586
+ size_t* total_out) {
1587
+ if (!EnsureInitialized(s)) return BROTLI_FALSE;
1588
+
1589
+ /* Unfinished metadata block; check requirements. */
1590
+ if (s->remaining_metadata_bytes_ != BROTLI_UINT32_MAX) {
1591
+ if (*available_in != s->remaining_metadata_bytes_) return BROTLI_FALSE;
1592
+ if (op != BROTLI_OPERATION_EMIT_METADATA) return BROTLI_FALSE;
1593
+ }
1594
+
1595
+ if (op == BROTLI_OPERATION_EMIT_METADATA) {
1596
+ UpdateSizeHint(s, 0); /* First data metablock might be emitted here. */
1597
+ return ProcessMetadata(
1598
+ s, available_in, next_in, available_out, next_out, total_out);
1599
+ }
1600
+
1601
+ if (s->stream_state_ == BROTLI_STREAM_METADATA_HEAD ||
1602
+ s->stream_state_ == BROTLI_STREAM_METADATA_BODY) {
1603
+ return BROTLI_FALSE;
1604
+ }
1605
+
1606
+ if (s->stream_state_ != BROTLI_STREAM_PROCESSING && *available_in != 0) {
1607
+ return BROTLI_FALSE;
1608
+ }
1609
+ if (s->params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY ||
1610
+ s->params.quality == FAST_TWO_PASS_COMPRESSION_QUALITY) {
1611
+ return BrotliEncoderCompressStreamFast(s, op, available_in, next_in,
1612
+ available_out, next_out, total_out);
1613
+ }
1614
+ while (BROTLI_TRUE) {
1615
+ size_t remaining_block_size = RemainingInputBlockSize(s);
1616
+ /* Shorten input to flint size. */
1617
+ if (s->flint_ >= 0 && remaining_block_size > (size_t)s->flint_) {
1618
+ remaining_block_size = (size_t)s->flint_;
1619
+ }
1620
+
1621
+ if (remaining_block_size != 0 && *available_in != 0) {
1622
+ size_t copy_input_size =
1623
+ BROTLI_MIN(size_t, remaining_block_size, *available_in);
1624
+ CopyInputToRingBuffer(s, copy_input_size, *next_in);
1625
+ *next_in += copy_input_size;
1626
+ *available_in -= copy_input_size;
1627
+ s->total_in_ += copy_input_size;
1628
+ if (s->flint_ > 0) s->flint_ = (int8_t)(s->flint_ - (int)copy_input_size);
1629
+ continue;
1630
+ }
1631
+
1632
+ if (InjectFlushOrPushOutput(s, available_out, next_out, total_out)) {
1633
+ /* Exit the "emit flint" workflow. */
1634
+ if (s->flint_ == BROTLI_FLINT_WAITING_FOR_FLUSHING) {
1635
+ CheckFlushComplete(s);
1636
+ if (s->stream_state_ == BROTLI_STREAM_PROCESSING) {
1637
+ s->flint_ = BROTLI_FLINT_DONE;
1638
+ }
1639
+ }
1640
+ continue;
1641
+ }
1642
+
1643
+ /* Compress data only when internal output buffer is empty, stream is not
1644
+ finished and there is no pending flush request. */
1645
+ if (s->available_out_ == 0 &&
1646
+ s->stream_state_ == BROTLI_STREAM_PROCESSING) {
1647
+ if (remaining_block_size == 0 || op != BROTLI_OPERATION_PROCESS) {
1648
+ BROTLI_BOOL is_last = TO_BROTLI_BOOL(
1649
+ (*available_in == 0) && op == BROTLI_OPERATION_FINISH);
1650
+ BROTLI_BOOL force_flush = TO_BROTLI_BOOL(
1651
+ (*available_in == 0) && op == BROTLI_OPERATION_FLUSH);
1652
+ BROTLI_BOOL result;
1653
+ /* Force emitting (uncompressed) piece containing flint. */
1654
+ if (!is_last && s->flint_ == 0) {
1655
+ s->flint_ = BROTLI_FLINT_WAITING_FOR_FLUSHING;
1656
+ force_flush = BROTLI_TRUE;
1657
+ }
1658
+ UpdateSizeHint(s, *available_in);
1659
+ result = EncodeData(s, is_last, force_flush,
1660
+ &s->available_out_, &s->next_out_);
1661
+ if (!result) return BROTLI_FALSE;
1662
+ if (force_flush) s->stream_state_ = BROTLI_STREAM_FLUSH_REQUESTED;
1663
+ if (is_last) s->stream_state_ = BROTLI_STREAM_FINISHED;
1664
+ continue;
1665
+ }
1666
+ }
1667
+ break;
1668
+ }
1669
+ CheckFlushComplete(s);
1670
+ return BROTLI_TRUE;
1671
+ }
1672
+
1673
+ BROTLI_BOOL duckdb_brotli::BrotliEncoderIsFinished(BrotliEncoderState* s) {
1674
+ return TO_BROTLI_BOOL(s->stream_state_ == BROTLI_STREAM_FINISHED &&
1675
+ !BrotliEncoderHasMoreOutput(s));
1676
+ }
1677
+
1678
+ BROTLI_BOOL duckdb_brotli::BrotliEncoderHasMoreOutput(BrotliEncoderState* s) {
1679
+ return TO_BROTLI_BOOL(s->available_out_ != 0);
1680
+ }
1681
+
1682
+ const uint8_t* BrotliEncoderTakeOutput(BrotliEncoderState* s, size_t* size) {
1683
+ size_t consumed_size = s->available_out_;
1684
+ uint8_t* result = s->next_out_;
1685
+ if (*size) {
1686
+ consumed_size = BROTLI_MIN(size_t, *size, s->available_out_);
1687
+ }
1688
+ if (consumed_size) {
1689
+ s->next_out_ += consumed_size;
1690
+ s->available_out_ -= consumed_size;
1691
+ s->total_out_ += consumed_size;
1692
+ CheckFlushComplete(s);
1693
+ *size = consumed_size;
1694
+ } else {
1695
+ *size = 0;
1696
+ result = 0;
1697
+ }
1698
+ return result;
1699
+ }
1700
+
1701
+ uint32_t duckdb_brotli::BrotliEncoderVersion(void) {
1702
+ return BROTLI_VERSION;
1703
+ }
1704
+
1705
+ BrotliEncoderPreparedDictionary* duckdb_brotli::BrotliEncoderPrepareDictionary(
1706
+ BrotliSharedDictionaryType type, size_t size,
1707
+ const uint8_t data[BROTLI_ARRAY_PARAM(size)], int quality,
1708
+ brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
1709
+ ManagedDictionary* managed_dictionary = NULL;
1710
+ BROTLI_BOOL type_is_known = BROTLI_FALSE;
1711
+ type_is_known |= (type == BROTLI_SHARED_DICTIONARY_RAW);
1712
+ #if defined(BROTLI_EXPERIMENTAL)
1713
+ type_is_known |= (type == BROTLI_SHARED_DICTIONARY_SERIALIZED);
1714
+ #endif /* BROTLI_EXPERIMENTAL */
1715
+ if (!type_is_known) {
1716
+ return NULL;
1717
+ }
1718
+ managed_dictionary =
1719
+ BrotliCreateManagedDictionary(alloc_func, free_func, opaque);
1720
+ if (managed_dictionary == NULL) {
1721
+ return NULL;
1722
+ }
1723
+ if (type == BROTLI_SHARED_DICTIONARY_RAW) {
1724
+ managed_dictionary->dictionary = (uint32_t*)CreatePreparedDictionary(
1725
+ &managed_dictionary->memory_manager_, data, size);
1726
+ }
1727
+ #if defined(BROTLI_EXPERIMENTAL)
1728
+ if (type == BROTLI_SHARED_DICTIONARY_SERIALIZED) {
1729
+ SharedEncoderDictionary* dict = (SharedEncoderDictionary*)BrotliAllocate(
1730
+ &managed_dictionary->memory_manager_, sizeof(SharedEncoderDictionary));
1731
+ managed_dictionary->dictionary = (uint32_t*)dict;
1732
+ if (dict != NULL) {
1733
+ BROTLI_BOOL ok = BrotliInitCustomSharedEncoderDictionary(
1734
+ &managed_dictionary->memory_manager_, data, size, quality, dict);
1735
+ if (!ok) {
1736
+ BrotliFree(&managed_dictionary->memory_manager_, dict);
1737
+ managed_dictionary->dictionary = NULL;
1738
+ }
1739
+ }
1740
+ }
1741
+ #else /* BROTLI_EXPERIMENTAL */
1742
+ (void)quality;
1743
+ #endif /* BROTLI_EXPERIMENTAL */
1744
+ if (managed_dictionary->dictionary == NULL) {
1745
+ BrotliDestroyManagedDictionary(managed_dictionary);
1746
+ return NULL;
1747
+ }
1748
+ return (BrotliEncoderPreparedDictionary*)managed_dictionary;
1749
+ }
1750
+
1751
+ void BrotliEncoderDestroyPreparedDictionary(
1752
+ BrotliEncoderPreparedDictionary* dictionary) {
1753
+ ManagedDictionary* dict = (ManagedDictionary*)dictionary;
1754
+ if (!dictionary) return;
1755
+ /* First field of dictionary structs. */
1756
+ /* Only managed dictionaries are eligible for destruction by this method. */
1757
+ if (dict->magic != kManagedDictionaryMagic) {
1758
+ return;
1759
+ }
1760
+ if (dict->dictionary == NULL) {
1761
+ /* This should never ever happen. */
1762
+ } else if (*dict->dictionary == kLeanPreparedDictionaryMagic) {
1763
+ DestroyPreparedDictionary(
1764
+ &dict->memory_manager_, (PreparedDictionary*)dict->dictionary);
1765
+ } else if (*dict->dictionary == kSharedDictionaryMagic) {
1766
+ BrotliCleanupSharedEncoderDictionary(&dict->memory_manager_,
1767
+ (SharedEncoderDictionary*)dict->dictionary);
1768
+ BrotliFree(&dict->memory_manager_, dict->dictionary);
1769
+ } else {
1770
+ /* There is also kPreparedDictionaryMagic, but such instances should be
1771
+ * constructed and destroyed by different means. */
1772
+ }
1773
+ dict->dictionary = NULL;
1774
+ BrotliDestroyManagedDictionary(dict);
1775
+ }
1776
+
1777
+ BROTLI_BOOL BrotliEncoderAttachPreparedDictionary(BrotliEncoderState* state,
1778
+ const BrotliEncoderPreparedDictionary* dictionary) {
1779
+ /* First field of dictionary structs */
1780
+ const BrotliEncoderPreparedDictionary* dict = dictionary;
1781
+ uint32_t magic = *((const uint32_t*)dict);
1782
+ SharedEncoderDictionary* current = NULL;
1783
+ if (magic == kManagedDictionaryMagic) {
1784
+ /* Unwrap managed dictionary. */
1785
+ ManagedDictionary* managed_dictionary = (ManagedDictionary*)dict;
1786
+ magic = *managed_dictionary->dictionary;
1787
+ dict = (BrotliEncoderPreparedDictionary*)managed_dictionary->dictionary;
1788
+ }
1789
+ current = &state->params.dictionary;
1790
+ if (magic == kPreparedDictionaryMagic ||
1791
+ magic == kLeanPreparedDictionaryMagic) {
1792
+ const PreparedDictionary* prepared = (const PreparedDictionary*)dict;
1793
+ if (!AttachPreparedDictionary(&current->compound, prepared)) {
1794
+ return BROTLI_FALSE;
1795
+ }
1796
+ } else if (magic == kSharedDictionaryMagic) {
1797
+ const SharedEncoderDictionary* attached =
1798
+ (const SharedEncoderDictionary*)dict;
1799
+ BROTLI_BOOL was_default = !current->contextual.context_based &&
1800
+ current->contextual.num_dictionaries == 1 &&
1801
+ current->contextual.dict[0]->hash_table_words ==
1802
+ kStaticDictionaryHashWords &&
1803
+ current->contextual.dict[0]->hash_table_lengths ==
1804
+ kStaticDictionaryHashLengths;
1805
+ BROTLI_BOOL new_default = !attached->contextual.context_based &&
1806
+ attached->contextual.num_dictionaries == 1 &&
1807
+ attached->contextual.dict[0]->hash_table_words ==
1808
+ kStaticDictionaryHashWords &&
1809
+ attached->contextual.dict[0]->hash_table_lengths ==
1810
+ kStaticDictionaryHashLengths;
1811
+ size_t i;
1812
+ if (state->is_initialized_) return BROTLI_FALSE;
1813
+ current->max_quality =
1814
+ BROTLI_MIN(int, current->max_quality, attached->max_quality);
1815
+ for (i = 0; i < attached->compound.num_chunks; i++) {
1816
+ if (!AttachPreparedDictionary(&current->compound,
1817
+ attached->compound.chunks[i])) {
1818
+ return BROTLI_FALSE;
1819
+ }
1820
+ }
1821
+ if (!new_default) {
1822
+ if (!was_default) return BROTLI_FALSE;
1823
+ /* Copy by value, but then set num_instances_ to 0 because their memory
1824
+ is managed by attached, not by current */
1825
+ current->contextual = attached->contextual;
1826
+ current->contextual.num_instances_ = 0;
1827
+ }
1828
+ } else {
1829
+ return BROTLI_FALSE;
1830
+ }
1831
+ return BROTLI_TRUE;
1832
+ }
1833
+
1834
+ size_t duckdb_brotli::BrotliEncoderEstimatePeakMemoryUsage(int quality, int lgwin,
1835
+ size_t input_size) {
1836
+ BrotliEncoderParams params;
1837
+ size_t memory_manager_slots = BROTLI_ENCODER_MEMORY_MANAGER_SLOTS;
1838
+ size_t memory_manager_size = memory_manager_slots * sizeof(void*);
1839
+ BrotliEncoderInitParams(&params);
1840
+ params.quality = quality;
1841
+ params.lgwin = lgwin;
1842
+ params.size_hint = input_size;
1843
+ params.large_window = lgwin > BROTLI_MAX_WINDOW_BITS;
1844
+ SanitizeParams(&params);
1845
+ params.lgblock = ComputeLgBlock(&params);
1846
+ ChooseHasher(&params, &params.hasher);
1847
+ if (params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY ||
1848
+ params.quality == FAST_TWO_PASS_COMPRESSION_QUALITY) {
1849
+ size_t state_size = sizeof(BrotliEncoderState);
1850
+ size_t block_size = BROTLI_MIN(size_t, input_size, (1ul << params.lgwin));
1851
+ size_t hash_table_size =
1852
+ HashTableSize(MaxHashTableSize(params.quality), block_size);
1853
+ size_t hash_size =
1854
+ (hash_table_size < (1u << 10)) ? 0 : sizeof(int) * hash_table_size;
1855
+ size_t cmdbuf_size = params.quality == FAST_TWO_PASS_COMPRESSION_QUALITY ?
1856
+ 5 * BROTLI_MIN(size_t, block_size, 1ul << 17) : 0;
1857
+ if (params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY) {
1858
+ state_size += sizeof(BrotliOnePassArena);
1859
+ } else {
1860
+ state_size += sizeof(BrotliTwoPassArena);
1861
+ }
1862
+ return hash_size + cmdbuf_size + state_size;
1863
+ } else {
1864
+ size_t short_ringbuffer_size = (size_t)1 << params.lgblock;
1865
+ int ringbuffer_bits = ComputeRbBits(&params);
1866
+ size_t ringbuffer_size = input_size < short_ringbuffer_size ?
1867
+ input_size : (1u << ringbuffer_bits) + short_ringbuffer_size;
1868
+ size_t hash_size[4] = {0};
1869
+ size_t metablock_size =
1870
+ BROTLI_MIN(size_t, input_size, MaxMetablockSize(&params));
1871
+ size_t inputblock_size =
1872
+ BROTLI_MIN(size_t, input_size, (size_t)1 << params.lgblock);
1873
+ size_t cmdbuf_size = metablock_size * 2 + inputblock_size * 6;
1874
+ size_t outbuf_size = metablock_size * 2 + 503;
1875
+ size_t histogram_size = 0;
1876
+ HasherSize(&params, BROTLI_TRUE, input_size, hash_size);
1877
+ if (params.quality < MIN_QUALITY_FOR_BLOCK_SPLIT) {
1878
+ cmdbuf_size = BROTLI_MIN(size_t, cmdbuf_size,
1879
+ MAX_NUM_DELAYED_SYMBOLS * sizeof(Command) + inputblock_size * 12);
1880
+ }
1881
+ if (params.quality >= MIN_QUALITY_FOR_HQ_BLOCK_SPLITTING) {
1882
+ /* Only a very rough estimation, based on enwik8. */
1883
+ histogram_size = 200 << 20;
1884
+ } else if (params.quality >= MIN_QUALITY_FOR_BLOCK_SPLIT) {
1885
+ size_t literal_histograms =
1886
+ BROTLI_MIN(size_t, metablock_size / 6144, 256);
1887
+ size_t command_histograms =
1888
+ BROTLI_MIN(size_t, metablock_size / 6144, 256);
1889
+ size_t distance_histograms =
1890
+ BROTLI_MIN(size_t, metablock_size / 6144, 256);
1891
+ histogram_size = literal_histograms * sizeof(HistogramLiteral) +
1892
+ command_histograms * sizeof(HistogramCommand) +
1893
+ distance_histograms * sizeof(HistogramDistance);
1894
+ }
1895
+ return (memory_manager_size + ringbuffer_size +
1896
+ hash_size[0] + hash_size[1] + hash_size[2] + hash_size[3] +
1897
+ cmdbuf_size +
1898
+ outbuf_size +
1899
+ histogram_size);
1900
+ }
1901
+ }
1902
+ size_t duckdb_brotli::BrotliEncoderGetPreparedDictionarySize(
1903
+ const BrotliEncoderPreparedDictionary* prepared_dictionary) {
1904
+ /* First field of dictionary structs */
1905
+ const BrotliEncoderPreparedDictionary* prepared = prepared_dictionary;
1906
+ uint32_t magic = *((const uint32_t*)prepared);
1907
+ size_t overhead = 0;
1908
+ if (magic == kManagedDictionaryMagic) {
1909
+ const ManagedDictionary* managed = (const ManagedDictionary*)prepared;
1910
+ overhead = sizeof(ManagedDictionary);
1911
+ magic = *managed->dictionary;
1912
+ prepared = (const BrotliEncoderPreparedDictionary*)managed->dictionary;
1913
+ }
1914
+
1915
+ if (magic == kPreparedDictionaryMagic) {
1916
+ const PreparedDictionary* dictionary =
1917
+ (const PreparedDictionary*)prepared;
1918
+ /* Keep in sync with step 3 of CreatePreparedDictionary */
1919
+ return sizeof(PreparedDictionary) + dictionary->source_size +
1920
+ (sizeof(uint32_t) << dictionary->slot_bits) +
1921
+ (sizeof(uint16_t) << dictionary->bucket_bits) +
1922
+ (sizeof(uint32_t) * dictionary->num_items) + overhead;
1923
+ } else if (magic == kLeanPreparedDictionaryMagic) {
1924
+ const PreparedDictionary* dictionary =
1925
+ (const PreparedDictionary*)prepared;
1926
+ /* Keep in sync with step 3 of CreatePreparedDictionary */
1927
+ return sizeof(PreparedDictionary) + sizeof(uint8_t*) +
1928
+ (sizeof(uint32_t) << dictionary->slot_bits) +
1929
+ (sizeof(uint16_t) << dictionary->bucket_bits) +
1930
+ (sizeof(uint32_t) * dictionary->num_items) + overhead;
1931
+ } else if (magic == kSharedDictionaryMagic) {
1932
+ const SharedEncoderDictionary* dictionary =
1933
+ (const SharedEncoderDictionary*)prepared;
1934
+ const CompoundDictionary* compound = &dictionary->compound;
1935
+ const ContextualEncoderDictionary* contextual = &dictionary->contextual;
1936
+ size_t result = sizeof(*dictionary);
1937
+ size_t i;
1938
+ size_t num_instances;
1939
+ const BrotliEncoderDictionary* instances;
1940
+ for (i = 0; i < compound->num_prepared_instances_; i++) {
1941
+ size_t size = BrotliEncoderGetPreparedDictionarySize(
1942
+ (const BrotliEncoderPreparedDictionary*)
1943
+ compound->prepared_instances_[i]);
1944
+ if (!size) return 0; /* error */
1945
+ result += size;
1946
+ }
1947
+ if (contextual->context_based) {
1948
+ num_instances = contextual->num_instances_;
1949
+ instances = contextual->instances_;
1950
+ result += sizeof(*instances) * num_instances;
1951
+ } else {
1952
+ num_instances = 1;
1953
+ instances = &contextual->instance_;
1954
+ }
1955
+ for (i = 0; i < num_instances; i++) {
1956
+ const BrotliEncoderDictionary* dict = &instances[i];
1957
+ result += dict->trie.pool_capacity * sizeof(BrotliTrieNode);
1958
+ if (dict->hash_table_data_words_) {
1959
+ result += sizeof(kStaticDictionaryHashWords);
1960
+ }
1961
+ if (dict->hash_table_data_lengths_) {
1962
+ result += sizeof(kStaticDictionaryHashLengths);
1963
+ }
1964
+ if (dict->buckets_data_) {
1965
+ result += sizeof(*dict->buckets_data_) * dict->buckets_alloc_size_;
1966
+ }
1967
+ if (dict->dict_words_data_) {
1968
+ result += sizeof(*dict->dict_words) * dict->dict_words_alloc_size_;
1969
+ }
1970
+ if (dict->words_instance_) {
1971
+ result += sizeof(*dict->words_instance_);
1972
+ /* data_size not added here: it is never allocated by the
1973
+ SharedEncoderDictionary, instead it always points to the file
1974
+ already loaded in memory. So if the caller wants to include
1975
+ this memory as well, add the size of the loaded dictionary
1976
+ file to this. */
1977
+ }
1978
+ }
1979
+ return result + overhead;
1980
+ }
1981
+ return 0; /* error */
1982
+ }
1983
+
1984
+ #if defined(BROTLI_TEST)
1985
+ size_t MakeUncompressedStreamForTest(const uint8_t*, size_t, uint8_t*);
1986
+ size_t MakeUncompressedStreamForTest(
1987
+ const uint8_t* input, size_t input_size, uint8_t* output) {
1988
+ return MakeUncompressedStream(input, input_size, output);
1989
+ }
1990
+ #endif