duckdb 1.0.1-dev21.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 (1390) hide show
  1. package/.github/workflows/HighPriorityIssues.yml +2 -2
  2. package/.github/workflows/NodeJS.yml +1 -1
  3. package/binding.gyp +41 -0
  4. package/package.json +1 -1
  5. package/src/duckdb/extension/icu/icu-dateadd.cpp +4 -2
  6. package/src/duckdb/extension/icu/icu-datefunc.cpp +6 -2
  7. package/src/duckdb/extension/icu/icu-datesub.cpp +13 -2
  8. package/src/duckdb/extension/icu/icu-strptime.cpp +6 -6
  9. package/src/duckdb/extension/icu/icu-table-range.cpp +92 -73
  10. package/src/duckdb/extension/icu/icu-timebucket.cpp +12 -2
  11. package/src/duckdb/extension/icu/icu-timezone.cpp +3 -3
  12. package/src/duckdb/extension/icu/icu_extension.cpp +61 -9
  13. package/src/duckdb/extension/json/include/json_executors.hpp +20 -23
  14. package/src/duckdb/extension/json/include/json_functions.hpp +4 -0
  15. package/src/duckdb/extension/json/include/json_scan.hpp +6 -2
  16. package/src/duckdb/extension/json/include/json_structure.hpp +12 -9
  17. package/src/duckdb/extension/json/json_common.cpp +66 -10
  18. package/src/duckdb/extension/json/json_extension.cpp +13 -5
  19. package/src/duckdb/extension/json/json_functions/json_array_length.cpp +1 -1
  20. package/src/duckdb/extension/json/json_functions/json_create.cpp +21 -4
  21. package/src/duckdb/extension/json/json_functions/json_exists.cpp +32 -0
  22. package/src/duckdb/extension/json/json_functions/json_extract.cpp +2 -2
  23. package/src/duckdb/extension/json/json_functions/json_keys.cpp +1 -1
  24. package/src/duckdb/extension/json/json_functions/json_pretty.cpp +32 -0
  25. package/src/duckdb/extension/json/json_functions/json_serialize_sql.cpp +5 -1
  26. package/src/duckdb/extension/json/json_functions/json_structure.cpp +305 -94
  27. package/src/duckdb/extension/json/json_functions/json_transform.cpp +1 -1
  28. package/src/duckdb/extension/json/json_functions/json_type.cpp +3 -3
  29. package/src/duckdb/extension/json/json_functions/json_value.cpp +42 -0
  30. package/src/duckdb/extension/json/json_functions/read_json.cpp +16 -2
  31. package/src/duckdb/extension/json/json_functions/read_json_objects.cpp +3 -2
  32. package/src/duckdb/extension/json/json_functions.cpp +5 -1
  33. package/src/duckdb/extension/json/json_scan.cpp +13 -12
  34. package/src/duckdb/extension/json/serialize_json.cpp +5 -3
  35. package/src/duckdb/extension/parquet/column_reader.cpp +206 -43
  36. package/src/duckdb/extension/parquet/column_writer.cpp +133 -62
  37. package/src/duckdb/extension/parquet/geo_parquet.cpp +391 -0
  38. package/src/duckdb/extension/parquet/include/boolean_column_reader.hpp +16 -5
  39. package/src/duckdb/extension/parquet/include/column_reader.hpp +37 -12
  40. package/src/duckdb/extension/parquet/include/column_writer.hpp +10 -11
  41. package/src/duckdb/extension/parquet/include/expression_column_reader.hpp +52 -0
  42. package/src/duckdb/extension/parquet/include/geo_parquet.hpp +139 -0
  43. package/src/duckdb/extension/parquet/include/parquet_crypto.hpp +13 -8
  44. package/src/duckdb/extension/parquet/include/parquet_decimal_utils.hpp +3 -0
  45. package/src/duckdb/extension/parquet/include/parquet_file_metadata_cache.hpp +7 -3
  46. package/src/duckdb/extension/parquet/include/parquet_reader.hpp +55 -8
  47. package/src/duckdb/extension/parquet/include/parquet_rle_bp_decoder.hpp +3 -3
  48. package/src/duckdb/extension/parquet/include/parquet_rle_bp_encoder.hpp +1 -1
  49. package/src/duckdb/extension/parquet/include/parquet_timestamp.hpp +8 -0
  50. package/src/duckdb/extension/parquet/include/parquet_writer.hpp +21 -7
  51. package/src/duckdb/extension/parquet/include/resizable_buffer.hpp +33 -11
  52. package/src/duckdb/extension/parquet/include/string_column_reader.hpp +5 -2
  53. package/src/duckdb/extension/parquet/include/templated_column_reader.hpp +48 -14
  54. package/src/duckdb/extension/parquet/parquet_crypto.cpp +109 -61
  55. package/src/duckdb/extension/parquet/parquet_extension.cpp +305 -72
  56. package/src/duckdb/extension/parquet/parquet_metadata.cpp +4 -4
  57. package/src/duckdb/extension/parquet/parquet_reader.cpp +151 -40
  58. package/src/duckdb/extension/parquet/parquet_statistics.cpp +50 -16
  59. package/src/duckdb/extension/parquet/parquet_timestamp.cpp +42 -1
  60. package/src/duckdb/extension/parquet/parquet_writer.cpp +67 -75
  61. package/src/duckdb/extension/parquet/serialize_parquet.cpp +3 -1
  62. package/src/duckdb/extension/parquet/zstd_file_system.cpp +5 -1
  63. package/src/duckdb/src/catalog/catalog.cpp +14 -16
  64. package/src/duckdb/src/catalog/catalog_entry/duck_index_entry.cpp +14 -11
  65. package/src/duckdb/src/catalog/catalog_entry/duck_schema_entry.cpp +39 -19
  66. package/src/duckdb/src/catalog/catalog_entry/duck_table_entry.cpp +92 -78
  67. package/src/duckdb/src/catalog/catalog_entry/index_catalog_entry.cpp +10 -2
  68. package/src/duckdb/src/catalog/catalog_entry/macro_catalog_entry.cpp +10 -3
  69. package/src/duckdb/src/catalog/catalog_entry/schema_catalog_entry.cpp +3 -3
  70. package/src/duckdb/src/catalog/catalog_entry/table_catalog_entry.cpp +7 -7
  71. package/src/duckdb/src/catalog/catalog_entry.cpp +6 -3
  72. package/src/duckdb/src/catalog/catalog_set.cpp +14 -19
  73. package/src/duckdb/src/catalog/default/default_functions.cpp +179 -166
  74. package/src/duckdb/src/catalog/default/default_generator.cpp +24 -0
  75. package/src/duckdb/src/catalog/default/default_schemas.cpp +4 -3
  76. package/src/duckdb/src/catalog/default/default_table_functions.cpp +148 -0
  77. package/src/duckdb/src/catalog/default/default_views.cpp +7 -3
  78. package/src/duckdb/src/catalog/duck_catalog.cpp +7 -1
  79. package/src/duckdb/src/common/adbc/adbc.cpp +120 -58
  80. package/src/duckdb/src/common/allocator.cpp +71 -6
  81. package/src/duckdb/src/common/arrow/appender/bool_data.cpp +8 -7
  82. package/src/duckdb/src/common/arrow/appender/fixed_size_list_data.cpp +1 -1
  83. package/src/duckdb/src/common/arrow/appender/union_data.cpp +4 -5
  84. package/src/duckdb/src/common/arrow/arrow_appender.cpp +55 -21
  85. package/src/duckdb/src/common/arrow/arrow_converter.cpp +85 -10
  86. package/src/duckdb/src/common/arrow/arrow_merge_event.cpp +142 -0
  87. package/src/duckdb/src/common/arrow/arrow_query_result.cpp +56 -0
  88. package/src/duckdb/src/common/arrow/physical_arrow_batch_collector.cpp +37 -0
  89. package/src/duckdb/src/common/arrow/physical_arrow_collector.cpp +128 -0
  90. package/src/duckdb/src/common/arrow/schema_metadata.cpp +101 -0
  91. package/src/duckdb/src/common/cgroups.cpp +189 -0
  92. package/src/duckdb/src/common/compressed_file_system.cpp +6 -3
  93. package/src/duckdb/src/common/encryption_state.cpp +38 -0
  94. package/src/duckdb/src/common/enum_util.cpp +682 -14
  95. package/src/duckdb/src/common/enums/file_compression_type.cpp +24 -0
  96. package/src/duckdb/src/common/enums/metric_type.cpp +208 -0
  97. package/src/duckdb/src/common/enums/optimizer_type.cpp +8 -2
  98. package/src/duckdb/src/common/enums/physical_operator_type.cpp +2 -0
  99. package/src/duckdb/src/common/enums/relation_type.cpp +4 -0
  100. package/src/duckdb/src/common/enums/statement_type.cpp +15 -0
  101. package/src/duckdb/src/common/error_data.cpp +22 -20
  102. package/src/duckdb/src/common/exception/binder_exception.cpp +5 -0
  103. package/src/duckdb/src/common/exception.cpp +11 -1
  104. package/src/duckdb/src/common/extra_type_info.cpp +3 -0
  105. package/src/duckdb/src/common/file_buffer.cpp +1 -1
  106. package/src/duckdb/src/common/file_system.cpp +25 -3
  107. package/src/duckdb/src/common/filename_pattern.cpp +1 -0
  108. package/src/duckdb/src/common/fsst.cpp +15 -14
  109. package/src/duckdb/src/common/gzip_file_system.cpp +3 -1
  110. package/src/duckdb/src/common/hive_partitioning.cpp +103 -43
  111. package/src/duckdb/src/common/http_util.cpp +25 -0
  112. package/src/duckdb/src/common/local_file_system.cpp +48 -27
  113. package/src/duckdb/src/common/multi_file_list.cpp +113 -22
  114. package/src/duckdb/src/common/multi_file_reader.cpp +59 -58
  115. package/src/duckdb/src/common/operator/cast_operators.cpp +133 -34
  116. package/src/duckdb/src/common/operator/string_cast.cpp +42 -11
  117. package/src/duckdb/src/common/progress_bar/progress_bar.cpp +2 -2
  118. package/src/duckdb/src/common/progress_bar/terminal_progress_bar_display.cpp +1 -1
  119. package/src/duckdb/src/common/radix_partitioning.cpp +31 -21
  120. package/src/duckdb/src/common/random_engine.cpp +4 -0
  121. package/src/duckdb/src/common/re2_regex.cpp +47 -12
  122. package/src/duckdb/src/common/render_tree.cpp +243 -0
  123. package/src/duckdb/src/common/row_operations/row_aggregate.cpp +1 -1
  124. package/src/duckdb/src/common/row_operations/row_gather.cpp +2 -2
  125. package/src/duckdb/src/common/row_operations/row_matcher.cpp +58 -5
  126. package/src/duckdb/src/common/row_operations/row_radix_scatter.cpp +79 -43
  127. package/src/duckdb/src/common/serializer/binary_deserializer.cpp +1 -1
  128. package/src/duckdb/src/common/serializer/buffered_file_reader.cpp +6 -4
  129. package/src/duckdb/src/common/serializer/buffered_file_writer.cpp +18 -9
  130. package/src/duckdb/src/common/serializer/memory_stream.cpp +1 -0
  131. package/src/duckdb/src/common/sort/partition_state.cpp +33 -18
  132. package/src/duckdb/src/common/sort/radix_sort.cpp +22 -15
  133. package/src/duckdb/src/common/sort/sort_state.cpp +19 -16
  134. package/src/duckdb/src/common/sort/sorted_block.cpp +11 -10
  135. package/src/duckdb/src/common/string_util.cpp +167 -10
  136. package/src/duckdb/src/common/tree_renderer/graphviz_tree_renderer.cpp +108 -0
  137. package/src/duckdb/src/common/tree_renderer/html_tree_renderer.cpp +267 -0
  138. package/src/duckdb/src/common/tree_renderer/json_tree_renderer.cpp +116 -0
  139. package/src/duckdb/src/common/tree_renderer/text_tree_renderer.cpp +482 -0
  140. package/src/duckdb/src/common/tree_renderer/tree_renderer.cpp +12 -0
  141. package/src/duckdb/src/common/tree_renderer.cpp +16 -508
  142. package/src/duckdb/src/common/types/batched_data_collection.cpp +78 -9
  143. package/src/duckdb/src/common/types/bit.cpp +24 -22
  144. package/src/duckdb/src/common/types/blob.cpp +15 -11
  145. package/src/duckdb/src/common/types/column/column_data_allocator.cpp +18 -9
  146. package/src/duckdb/src/common/types/column/column_data_collection.cpp +4 -4
  147. package/src/duckdb/src/common/types/column/column_data_collection_segment.cpp +3 -4
  148. package/src/duckdb/src/common/types/column/column_data_consumer.cpp +2 -2
  149. package/src/duckdb/src/common/types/column/partitioned_column_data.cpp +70 -21
  150. package/src/duckdb/src/common/types/data_chunk.cpp +10 -1
  151. package/src/duckdb/src/common/types/date.cpp +8 -19
  152. package/src/duckdb/src/common/types/decimal.cpp +3 -2
  153. package/src/duckdb/src/common/types/hugeint.cpp +11 -3
  154. package/src/duckdb/src/common/types/hyperloglog.cpp +212 -227
  155. package/src/duckdb/src/common/types/interval.cpp +1 -1
  156. package/src/duckdb/src/common/types/list_segment.cpp +83 -49
  157. package/src/duckdb/src/common/types/row/partitioned_tuple_data.cpp +22 -83
  158. package/src/duckdb/src/common/types/row/row_data_collection.cpp +2 -2
  159. package/src/duckdb/src/common/types/row/row_data_collection_scanner.cpp +20 -4
  160. package/src/duckdb/src/common/types/row/tuple_data_allocator.cpp +28 -7
  161. package/src/duckdb/src/common/types/row/tuple_data_collection.cpp +29 -14
  162. package/src/duckdb/src/common/types/row/tuple_data_scatter_gather.cpp +152 -102
  163. package/src/duckdb/src/common/types/row/tuple_data_segment.cpp +4 -1
  164. package/src/duckdb/src/common/types/selection_vector.cpp +17 -1
  165. package/src/duckdb/src/common/types/time.cpp +62 -31
  166. package/src/duckdb/src/common/types/timestamp.cpp +70 -12
  167. package/src/duckdb/src/common/types/uuid.cpp +1 -1
  168. package/src/duckdb/src/common/types/validity_mask.cpp +40 -5
  169. package/src/duckdb/src/common/types/value.cpp +50 -8
  170. package/src/duckdb/src/common/types/varint.cpp +295 -0
  171. package/src/duckdb/src/common/types/vector.cpp +165 -54
  172. package/src/duckdb/src/common/types/vector_buffer.cpp +5 -4
  173. package/src/duckdb/src/common/types.cpp +106 -26
  174. package/src/duckdb/src/common/vector_operations/vector_copy.cpp +13 -25
  175. package/src/duckdb/src/common/vector_operations/vector_hash.cpp +6 -0
  176. package/src/duckdb/src/common/virtual_file_system.cpp +3 -3
  177. package/src/duckdb/src/core_functions/aggregate/distributive/approx_count.cpp +35 -82
  178. package/src/duckdb/src/core_functions/aggregate/distributive/arg_min_max.cpp +283 -46
  179. package/src/duckdb/src/core_functions/aggregate/distributive/bitagg.cpp +4 -4
  180. package/src/duckdb/src/core_functions/aggregate/distributive/entropy.cpp +3 -2
  181. package/src/duckdb/src/core_functions/aggregate/distributive/minmax.cpp +226 -338
  182. package/src/duckdb/src/core_functions/aggregate/distributive/sum.cpp +2 -0
  183. package/src/duckdb/src/core_functions/aggregate/holistic/approx_top_k.cpp +388 -0
  184. package/src/duckdb/src/core_functions/aggregate/holistic/approximate_quantile.cpp +63 -21
  185. package/src/duckdb/src/core_functions/aggregate/holistic/mad.cpp +330 -0
  186. package/src/duckdb/src/core_functions/aggregate/holistic/mode.cpp +136 -97
  187. package/src/duckdb/src/core_functions/aggregate/holistic/quantile.cpp +601 -1485
  188. package/src/duckdb/src/core_functions/aggregate/nested/binned_histogram.cpp +405 -0
  189. package/src/duckdb/src/core_functions/aggregate/nested/histogram.cpp +136 -165
  190. package/src/duckdb/src/core_functions/function_list.cpp +35 -8
  191. package/src/duckdb/src/core_functions/lambda_functions.cpp +5 -7
  192. package/src/duckdb/src/core_functions/scalar/array/array_functions.cpp +172 -198
  193. package/src/duckdb/src/core_functions/scalar/blob/create_sort_key.cpp +341 -54
  194. package/src/duckdb/src/core_functions/scalar/date/date_diff.cpp +2 -2
  195. package/src/duckdb/src/core_functions/scalar/date/date_part.cpp +89 -29
  196. package/src/duckdb/src/core_functions/scalar/date/date_trunc.cpp +1 -1
  197. package/src/duckdb/src/core_functions/scalar/date/make_date.cpp +2 -2
  198. package/src/duckdb/src/core_functions/scalar/date/strftime.cpp +133 -71
  199. package/src/duckdb/src/core_functions/scalar/date/to_interval.cpp +1 -1
  200. package/src/duckdb/src/core_functions/scalar/enum/enum_functions.cpp +1 -1
  201. package/src/duckdb/src/core_functions/scalar/generic/can_implicitly_cast.cpp +40 -0
  202. package/src/duckdb/src/core_functions/scalar/generic/error.cpp +1 -1
  203. package/src/duckdb/src/core_functions/scalar/generic/least.cpp +161 -58
  204. package/src/duckdb/src/core_functions/scalar/generic/typeof.cpp +13 -0
  205. package/src/duckdb/src/core_functions/scalar/list/array_slice.cpp +1 -1
  206. package/src/duckdb/src/core_functions/scalar/list/list_aggregates.cpp +59 -75
  207. package/src/duckdb/src/core_functions/scalar/list/list_distance.cpp +93 -40
  208. package/src/duckdb/src/core_functions/scalar/list/list_has_any_or_all.cpp +227 -0
  209. package/src/duckdb/src/core_functions/scalar/list/list_reduce.cpp +20 -19
  210. package/src/duckdb/src/core_functions/scalar/list/list_sort.cpp +0 -2
  211. package/src/duckdb/src/core_functions/scalar/list/list_value.cpp +106 -8
  212. package/src/duckdb/src/core_functions/scalar/map/map_contains.cpp +56 -0
  213. package/src/duckdb/src/core_functions/scalar/map/map_extract.cpp +73 -118
  214. package/src/duckdb/src/core_functions/scalar/math/numeric.cpp +98 -2
  215. package/src/duckdb/src/core_functions/scalar/operators/bitwise.cpp +1 -2
  216. package/src/duckdb/src/core_functions/scalar/random/setseed.cpp +1 -1
  217. package/src/duckdb/src/core_functions/scalar/string/bar.cpp +1 -1
  218. package/src/duckdb/src/core_functions/scalar/string/hex.cpp +5 -1
  219. package/src/duckdb/src/core_functions/scalar/string/md5.cpp +10 -37
  220. package/src/duckdb/src/core_functions/scalar/string/printf.cpp +18 -2
  221. package/src/duckdb/src/core_functions/scalar/string/repeat.cpp +45 -0
  222. package/src/duckdb/src/core_functions/scalar/string/reverse.cpp +4 -5
  223. package/src/duckdb/src/core_functions/scalar/string/sha1.cpp +35 -0
  224. package/src/duckdb/src/core_functions/scalar/string/sha256.cpp +5 -2
  225. package/src/duckdb/src/core_functions/scalar/string/url_encode.cpp +49 -0
  226. package/src/duckdb/src/core_functions/scalar/struct/struct_pack.cpp +1 -2
  227. package/src/duckdb/src/core_functions/scalar/union/union_extract.cpp +4 -2
  228. package/src/duckdb/src/execution/adaptive_filter.cpp +30 -11
  229. package/src/duckdb/src/execution/aggregate_hashtable.cpp +13 -18
  230. package/src/duckdb/src/execution/expression_executor/execute_conjunction.cpp +4 -9
  231. package/src/duckdb/src/execution/expression_executor.cpp +1 -1
  232. package/src/duckdb/src/execution/index/art/art.cpp +683 -670
  233. package/src/duckdb/src/execution/index/art/art_key.cpp +121 -38
  234. package/src/duckdb/src/execution/index/art/base_leaf.cpp +168 -0
  235. package/src/duckdb/src/execution/index/art/base_node.cpp +163 -0
  236. package/src/duckdb/src/execution/index/art/iterator.cpp +148 -77
  237. package/src/duckdb/src/execution/index/art/leaf.cpp +159 -263
  238. package/src/duckdb/src/execution/index/art/node.cpp +493 -247
  239. package/src/duckdb/src/execution/index/art/node256.cpp +31 -91
  240. package/src/duckdb/src/execution/index/art/node256_leaf.cpp +71 -0
  241. package/src/duckdb/src/execution/index/art/node48.cpp +75 -143
  242. package/src/duckdb/src/execution/index/art/prefix.cpp +424 -244
  243. package/src/duckdb/src/execution/index/bound_index.cpp +7 -1
  244. package/src/duckdb/src/execution/index/fixed_size_allocator.cpp +22 -18
  245. package/src/duckdb/src/execution/index/fixed_size_buffer.cpp +22 -73
  246. package/src/duckdb/src/execution/join_hashtable.cpp +637 -179
  247. package/src/duckdb/src/execution/operator/aggregate/aggregate_object.cpp +4 -4
  248. package/src/duckdb/src/execution/operator/aggregate/physical_hash_aggregate.cpp +15 -10
  249. package/src/duckdb/src/execution/operator/aggregate/physical_perfecthash_aggregate.cpp +13 -8
  250. package/src/duckdb/src/execution/operator/aggregate/physical_streaming_window.cpp +525 -132
  251. package/src/duckdb/src/execution/operator/aggregate/physical_ungrouped_aggregate.cpp +147 -138
  252. package/src/duckdb/src/execution/operator/aggregate/physical_window.cpp +531 -312
  253. package/src/duckdb/src/execution/operator/csv_scanner/buffer_manager/csv_buffer.cpp +1 -1
  254. package/src/duckdb/src/execution/operator/csv_scanner/buffer_manager/csv_buffer_manager.cpp +4 -3
  255. package/src/duckdb/src/execution/operator/csv_scanner/buffer_manager/csv_file_handle.cpp +9 -2
  256. package/src/duckdb/src/execution/operator/csv_scanner/scanner/base_scanner.cpp +13 -17
  257. package/src/duckdb/src/execution/operator/csv_scanner/scanner/column_count_scanner.cpp +60 -16
  258. package/src/duckdb/src/execution/operator/csv_scanner/scanner/csv_schema.cpp +105 -0
  259. package/src/duckdb/src/execution/operator/csv_scanner/scanner/scanner_boundary.cpp +24 -24
  260. package/src/duckdb/src/execution/operator/csv_scanner/scanner/skip_scanner.cpp +25 -2
  261. package/src/duckdb/src/execution/operator/csv_scanner/scanner/string_value_scanner.cpp +275 -112
  262. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/csv_sniffer.cpp +106 -11
  263. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/dialect_detection.cpp +253 -115
  264. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/header_detection.cpp +93 -52
  265. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/type_detection.cpp +116 -76
  266. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/type_refinement.cpp +29 -14
  267. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/type_replacement.cpp +1 -1
  268. package/src/duckdb/src/execution/operator/csv_scanner/state_machine/csv_state_machine_cache.cpp +70 -26
  269. package/src/duckdb/src/execution/operator/csv_scanner/table_function/csv_file_scanner.cpp +81 -60
  270. package/src/duckdb/src/execution/operator/csv_scanner/table_function/global_csv_state.cpp +88 -50
  271. package/src/duckdb/src/execution/operator/csv_scanner/util/csv_error.cpp +161 -51
  272. package/src/duckdb/src/execution/operator/csv_scanner/util/csv_reader_options.cpp +59 -17
  273. package/src/duckdb/src/execution/operator/filter/physical_filter.cpp +5 -5
  274. package/src/duckdb/src/execution/operator/helper/physical_batch_collector.cpp +0 -21
  275. package/src/duckdb/src/execution/operator/helper/physical_buffered_batch_collector.cpp +109 -0
  276. package/src/duckdb/src/execution/operator/helper/physical_buffered_collector.cpp +5 -13
  277. package/src/duckdb/src/execution/operator/helper/physical_explain_analyze.cpp +1 -1
  278. package/src/duckdb/src/execution/operator/helper/physical_load.cpp +12 -4
  279. package/src/duckdb/src/execution/operator/helper/physical_materialized_collector.cpp +0 -16
  280. package/src/duckdb/src/execution/operator/helper/physical_reservoir_sample.cpp +4 -2
  281. package/src/duckdb/src/execution/operator/helper/physical_reset.cpp +5 -0
  282. package/src/duckdb/src/execution/operator/helper/physical_result_collector.cpp +3 -1
  283. package/src/duckdb/src/execution/operator/helper/physical_set_variable.cpp +39 -0
  284. package/src/duckdb/src/execution/operator/helper/physical_streaming_sample.cpp +4 -2
  285. package/src/duckdb/src/execution/operator/helper/physical_transaction.cpp +16 -5
  286. package/src/duckdb/src/execution/operator/join/outer_join_marker.cpp +1 -1
  287. package/src/duckdb/src/execution/operator/join/perfect_hash_join_executor.cpp +1 -1
  288. package/src/duckdb/src/execution/operator/join/physical_asof_join.cpp +1 -1
  289. package/src/duckdb/src/execution/operator/join/physical_blockwise_nl_join.cpp +5 -4
  290. package/src/duckdb/src/execution/operator/join/physical_comparison_join.cpp +59 -21
  291. package/src/duckdb/src/execution/operator/join/physical_delim_join.cpp +7 -4
  292. package/src/duckdb/src/execution/operator/join/physical_hash_join.cpp +333 -176
  293. package/src/duckdb/src/execution/operator/join/physical_iejoin.cpp +57 -34
  294. package/src/duckdb/src/execution/operator/join/physical_join.cpp +16 -8
  295. package/src/duckdb/src/execution/operator/join/physical_left_delim_join.cpp +10 -4
  296. package/src/duckdb/src/execution/operator/join/physical_nested_loop_join.cpp +2 -5
  297. package/src/duckdb/src/execution/operator/join/physical_piecewise_merge_join.cpp +3 -3
  298. package/src/duckdb/src/execution/operator/join/physical_range_join.cpp +5 -5
  299. package/src/duckdb/src/execution/operator/join/physical_right_delim_join.cpp +7 -2
  300. package/src/duckdb/src/execution/operator/order/physical_order.cpp +17 -12
  301. package/src/duckdb/src/execution/operator/order/physical_top_n.cpp +12 -9
  302. package/src/duckdb/src/execution/operator/persistent/physical_batch_copy_to_file.cpp +35 -17
  303. package/src/duckdb/src/execution/operator/persistent/physical_batch_insert.cpp +17 -11
  304. package/src/duckdb/src/execution/operator/persistent/physical_copy_database.cpp +5 -1
  305. package/src/duckdb/src/execution/operator/persistent/physical_copy_to_file.cpp +156 -47
  306. package/src/duckdb/src/execution/operator/persistent/physical_insert.cpp +10 -2
  307. package/src/duckdb/src/execution/operator/persistent/physical_update.cpp +1 -3
  308. package/src/duckdb/src/execution/operator/projection/physical_pivot.cpp +2 -2
  309. package/src/duckdb/src/execution/operator/projection/physical_projection.cpp +13 -6
  310. package/src/duckdb/src/execution/operator/projection/physical_tableinout_function.cpp +22 -3
  311. package/src/duckdb/src/execution/operator/projection/physical_unnest.cpp +19 -3
  312. package/src/duckdb/src/execution/operator/scan/physical_column_data_scan.cpp +37 -22
  313. package/src/duckdb/src/execution/operator/scan/physical_table_scan.cpp +77 -21
  314. package/src/duckdb/src/execution/operator/schema/physical_attach.cpp +27 -55
  315. package/src/duckdb/src/execution/operator/schema/physical_create_art_index.cpp +41 -44
  316. package/src/duckdb/src/execution/operator/set/physical_cte.cpp +4 -6
  317. package/src/duckdb/src/execution/operator/set/physical_recursive_cte.cpp +4 -6
  318. package/src/duckdb/src/execution/operator/set/physical_union.cpp +18 -4
  319. package/src/duckdb/src/execution/perfect_aggregate_hashtable.cpp +3 -2
  320. package/src/duckdb/src/execution/physical_operator.cpp +45 -4
  321. package/src/duckdb/src/execution/physical_plan/plan_comparison_join.cpp +18 -7
  322. package/src/duckdb/src/execution/physical_plan/plan_copy_to_file.cpp +8 -3
  323. package/src/duckdb/src/execution/physical_plan/plan_delim_join.cpp +13 -6
  324. package/src/duckdb/src/execution/physical_plan/plan_explain.cpp +3 -3
  325. package/src/duckdb/src/execution/physical_plan/plan_get.cpp +111 -19
  326. package/src/duckdb/src/execution/physical_plan/plan_limit.cpp +19 -2
  327. package/src/duckdb/src/execution/physical_plan/plan_set.cpp +9 -0
  328. package/src/duckdb/src/execution/physical_plan/plan_window.cpp +3 -1
  329. package/src/duckdb/src/execution/physical_plan_generator.cpp +3 -3
  330. package/src/duckdb/src/execution/radix_partitioned_hashtable.cpp +49 -49
  331. package/src/duckdb/src/execution/reservoir_sample.cpp +2 -2
  332. package/src/duckdb/src/execution/window_executor.cpp +556 -318
  333. package/src/duckdb/src/execution/window_segment_tree.cpp +1058 -485
  334. package/src/duckdb/src/function/aggregate/distributive/count.cpp +5 -5
  335. package/src/duckdb/src/function/aggregate/distributive/first.cpp +92 -95
  336. package/src/duckdb/src/function/aggregate/sorted_aggregate_function.cpp +10 -9
  337. package/src/duckdb/src/function/aggregate_function.cpp +8 -0
  338. package/src/duckdb/src/function/cast/cast_function_set.cpp +10 -1
  339. package/src/duckdb/src/function/cast/decimal_cast.cpp +10 -1
  340. package/src/duckdb/src/function/cast/default_casts.cpp +2 -0
  341. package/src/duckdb/src/function/cast/numeric_casts.cpp +3 -0
  342. package/src/duckdb/src/function/cast/string_cast.cpp +8 -5
  343. package/src/duckdb/src/function/cast/time_casts.cpp +2 -2
  344. package/src/duckdb/src/function/cast/union_casts.cpp +1 -1
  345. package/src/duckdb/src/function/cast/varint_casts.cpp +283 -0
  346. package/src/duckdb/src/function/cast/vector_cast_helpers.cpp +3 -1
  347. package/src/duckdb/src/function/cast_rules.cpp +104 -15
  348. package/src/duckdb/src/function/compression_config.cpp +35 -33
  349. package/src/duckdb/src/function/copy_function.cpp +27 -0
  350. package/src/duckdb/src/function/function_binder.cpp +39 -11
  351. package/src/duckdb/src/function/macro_function.cpp +75 -32
  352. package/src/duckdb/src/function/pragma/pragma_queries.cpp +10 -0
  353. package/src/duckdb/src/function/scalar/compressed_materialization/compress_string.cpp +1 -0
  354. package/src/duckdb/src/function/scalar/generic/binning.cpp +507 -0
  355. package/src/duckdb/src/function/scalar/generic/getvariable.cpp +58 -0
  356. package/src/duckdb/src/function/scalar/generic_functions.cpp +1 -0
  357. package/src/duckdb/src/function/scalar/list/contains_or_position.cpp +33 -47
  358. package/src/duckdb/src/function/scalar/list/list_extract.cpp +70 -143
  359. package/src/duckdb/src/function/scalar/list/list_resize.cpp +93 -84
  360. package/src/duckdb/src/function/scalar/list/list_zip.cpp +3 -0
  361. package/src/duckdb/src/function/scalar/operators/arithmetic.cpp +24 -11
  362. package/src/duckdb/src/function/scalar/sequence/nextval.cpp +4 -4
  363. package/src/duckdb/src/function/scalar/strftime_format.cpp +196 -57
  364. package/src/duckdb/src/function/scalar/string/caseconvert.cpp +9 -7
  365. package/src/duckdb/src/function/scalar/string/concat.cpp +239 -123
  366. package/src/duckdb/src/function/scalar/string/concat_ws.cpp +149 -0
  367. package/src/duckdb/src/function/scalar/string/contains.cpp +18 -7
  368. package/src/duckdb/src/function/scalar/string/like.cpp +2 -2
  369. package/src/duckdb/src/function/scalar/string/substring.cpp +6 -11
  370. package/src/duckdb/src/function/scalar/string_functions.cpp +1 -0
  371. package/src/duckdb/src/function/scalar/struct/struct_extract.cpp +7 -3
  372. package/src/duckdb/src/function/scalar/system/aggregate_export.cpp +5 -5
  373. package/src/duckdb/src/function/scalar_function.cpp +5 -2
  374. package/src/duckdb/src/function/scalar_macro_function.cpp +2 -2
  375. package/src/duckdb/src/function/table/arrow/arrow_duck_schema.cpp +20 -39
  376. package/src/duckdb/src/function/table/arrow/arrow_type_info.cpp +135 -0
  377. package/src/duckdb/src/function/table/arrow.cpp +194 -52
  378. package/src/duckdb/src/function/table/arrow_conversion.cpp +212 -69
  379. package/src/duckdb/src/function/table/copy_csv.cpp +43 -14
  380. package/src/duckdb/src/function/table/query_function.cpp +80 -0
  381. package/src/duckdb/src/function/table/range.cpp +222 -142
  382. package/src/duckdb/src/function/table/read_csv.cpp +25 -13
  383. package/src/duckdb/src/function/table/sniff_csv.cpp +55 -35
  384. package/src/duckdb/src/function/table/system/duckdb_constraints.cpp +141 -129
  385. package/src/duckdb/src/function/table/system/duckdb_extensions.cpp +25 -14
  386. package/src/duckdb/src/function/table/system/duckdb_functions.cpp +20 -14
  387. package/src/duckdb/src/function/table/system/duckdb_indexes.cpp +15 -1
  388. package/src/duckdb/src/function/table/system/duckdb_variables.cpp +84 -0
  389. package/src/duckdb/src/function/table/system/test_all_types.cpp +1 -0
  390. package/src/duckdb/src/function/table/system/test_vector_types.cpp +33 -3
  391. package/src/duckdb/src/function/table/system_functions.cpp +1 -0
  392. package/src/duckdb/src/function/table/table_scan.cpp +45 -22
  393. package/src/duckdb/src/function/table/unnest.cpp +2 -2
  394. package/src/duckdb/src/function/table/version/pragma_version.cpp +4 -4
  395. package/src/duckdb/src/function/table_function.cpp +5 -4
  396. package/src/duckdb/src/function/table_macro_function.cpp +2 -2
  397. package/src/duckdb/src/include/duckdb/catalog/catalog.hpp +8 -4
  398. package/src/duckdb/src/include/duckdb/catalog/catalog_entry/duck_index_entry.hpp +5 -2
  399. package/src/duckdb/src/include/duckdb/catalog/catalog_entry/duck_schema_entry.hpp +3 -0
  400. package/src/duckdb/src/include/duckdb/catalog/catalog_entry/index_catalog_entry.hpp +2 -2
  401. package/src/duckdb/src/include/duckdb/catalog/catalog_entry/macro_catalog_entry.hpp +3 -4
  402. package/src/duckdb/src/include/duckdb/catalog/catalog_entry/table_catalog_entry.hpp +5 -5
  403. package/src/duckdb/src/include/duckdb/catalog/default/builtin_types/types.hpp +2 -1
  404. package/src/duckdb/src/include/duckdb/catalog/default/default_functions.hpp +4 -5
  405. package/src/duckdb/src/include/duckdb/catalog/default/default_generator.hpp +4 -5
  406. package/src/duckdb/src/include/duckdb/catalog/default/default_schemas.hpp +2 -1
  407. package/src/duckdb/src/include/duckdb/catalog/default/default_table_functions.hpp +47 -0
  408. package/src/duckdb/src/include/duckdb/catalog/duck_catalog.hpp +2 -0
  409. package/src/duckdb/src/include/duckdb/catalog/similar_catalog_entry.hpp +2 -2
  410. package/src/duckdb/src/include/duckdb/common/allocator.hpp +9 -1
  411. package/src/duckdb/src/include/duckdb/common/array_ptr.hpp +120 -0
  412. package/src/duckdb/src/include/duckdb/common/arrow/appender/append_data.hpp +37 -11
  413. package/src/duckdb/src/include/duckdb/common/arrow/appender/enum_data.hpp +9 -8
  414. package/src/duckdb/src/include/duckdb/common/arrow/appender/list.hpp +1 -0
  415. package/src/duckdb/src/include/duckdb/common/arrow/appender/list_data.hpp +6 -4
  416. package/src/duckdb/src/include/duckdb/common/arrow/appender/list_view_data.hpp +92 -0
  417. package/src/duckdb/src/include/duckdb/common/arrow/appender/map_data.hpp +2 -2
  418. package/src/duckdb/src/include/duckdb/common/arrow/appender/scalar_data.hpp +26 -4
  419. package/src/duckdb/src/include/duckdb/common/arrow/appender/varchar_data.hpp +90 -11
  420. package/src/duckdb/src/include/duckdb/common/arrow/arrow_appender.hpp +6 -6
  421. package/src/duckdb/src/include/duckdb/common/arrow/arrow_buffer.hpp +8 -1
  422. package/src/duckdb/src/include/duckdb/common/arrow/arrow_merge_event.hpp +62 -0
  423. package/src/duckdb/src/include/duckdb/common/arrow/arrow_query_result.hpp +52 -0
  424. package/src/duckdb/src/include/duckdb/common/arrow/arrow_types_extension.hpp +42 -0
  425. package/src/duckdb/src/include/duckdb/common/arrow/physical_arrow_batch_collector.hpp +30 -0
  426. package/src/duckdb/src/include/duckdb/common/arrow/physical_arrow_collector.hpp +65 -0
  427. package/src/duckdb/src/include/duckdb/common/arrow/schema_metadata.hpp +43 -0
  428. package/src/duckdb/src/include/duckdb/common/bswap.hpp +18 -16
  429. package/src/duckdb/src/include/duckdb/common/cgroups.hpp +30 -0
  430. package/src/duckdb/src/include/duckdb/common/compressed_file_system.hpp +3 -0
  431. package/src/duckdb/src/include/duckdb/common/dl.hpp +8 -1
  432. package/src/duckdb/src/include/duckdb/common/encryption_state.hpp +48 -0
  433. package/src/duckdb/src/include/duckdb/common/enum_util.hpp +88 -0
  434. package/src/duckdb/src/include/duckdb/common/enums/checkpoint_type.hpp +2 -2
  435. package/src/duckdb/src/include/duckdb/common/enums/copy_overwrite_mode.hpp +6 -1
  436. package/src/duckdb/src/include/duckdb/common/enums/destroy_buffer_upon.hpp +21 -0
  437. package/src/duckdb/src/include/duckdb/common/enums/explain_format.hpp +17 -0
  438. package/src/duckdb/src/include/duckdb/common/enums/file_compression_type.hpp +4 -0
  439. package/src/duckdb/src/include/duckdb/common/enums/join_type.hpp +2 -2
  440. package/src/duckdb/src/include/duckdb/common/enums/metric_type.hpp +88 -0
  441. package/src/duckdb/src/include/duckdb/common/enums/optimizer_type.hpp +6 -1
  442. package/src/duckdb/src/include/duckdb/common/enums/pending_execution_result.hpp +2 -1
  443. package/src/duckdb/src/include/duckdb/common/enums/physical_operator_type.hpp +1 -0
  444. package/src/duckdb/src/include/duckdb/common/enums/profiler_format.hpp +1 -1
  445. package/src/duckdb/src/include/duckdb/common/enums/relation_type.hpp +3 -1
  446. package/src/duckdb/src/include/duckdb/common/enums/set_scope.hpp +2 -1
  447. package/src/duckdb/src/include/duckdb/common/enums/statement_type.hpp +23 -2
  448. package/src/duckdb/src/include/duckdb/common/enums/stream_execution_result.hpp +25 -0
  449. package/src/duckdb/src/include/duckdb/common/enums/tableref_type.hpp +2 -1
  450. package/src/duckdb/src/include/duckdb/common/enums/wal_type.hpp +1 -0
  451. package/src/duckdb/src/include/duckdb/common/error_data.hpp +5 -2
  452. package/src/duckdb/src/include/duckdb/common/exception/binder_exception.hpp +1 -0
  453. package/src/duckdb/src/include/duckdb/common/exception.hpp +20 -2
  454. package/src/duckdb/src/include/duckdb/common/extra_operator_info.hpp +12 -0
  455. package/src/duckdb/src/include/duckdb/common/file_buffer.hpp +2 -0
  456. package/src/duckdb/src/include/duckdb/common/file_open_flags.hpp +16 -0
  457. package/src/duckdb/src/include/duckdb/common/file_opener.hpp +18 -0
  458. package/src/duckdb/src/include/duckdb/common/file_system.hpp +3 -0
  459. package/src/duckdb/src/include/duckdb/common/filename_pattern.hpp +4 -0
  460. package/src/duckdb/src/include/duckdb/common/fixed_size_map.hpp +160 -96
  461. package/src/duckdb/src/include/duckdb/common/fsst.hpp +9 -2
  462. package/src/duckdb/src/include/duckdb/common/helper.hpp +22 -8
  463. package/src/duckdb/src/include/duckdb/common/hive_partitioning.hpp +16 -7
  464. package/src/duckdb/src/include/duckdb/common/http_util.hpp +19 -0
  465. package/src/duckdb/src/include/duckdb/common/insertion_order_preserving_map.hpp +19 -6
  466. package/src/duckdb/src/include/duckdb/common/limits.hpp +9 -2
  467. package/src/duckdb/src/include/duckdb/common/multi_file_list.hpp +38 -6
  468. package/src/duckdb/src/include/duckdb/common/multi_file_reader.hpp +9 -2
  469. package/src/duckdb/src/include/duckdb/common/multi_file_reader_options.hpp +5 -1
  470. package/src/duckdb/src/include/duckdb/common/numeric_utils.hpp +82 -50
  471. package/src/duckdb/src/include/duckdb/common/operator/abs.hpp +11 -0
  472. package/src/duckdb/src/include/duckdb/common/operator/cast_operators.hpp +7 -3
  473. package/src/duckdb/src/include/duckdb/common/operator/decimal_cast_operators.hpp +23 -1
  474. package/src/duckdb/src/include/duckdb/common/operator/double_cast_operator.hpp +2 -1
  475. package/src/duckdb/src/include/duckdb/common/operator/integer_cast_operator.hpp +1 -1
  476. package/src/duckdb/src/include/duckdb/common/operator/numeric_cast.hpp +4 -0
  477. package/src/duckdb/src/include/duckdb/common/operator/string_cast.hpp +2 -0
  478. package/src/duckdb/src/include/duckdb/common/optional_ptr.hpp +10 -5
  479. package/src/duckdb/src/include/duckdb/common/optionally_owned_ptr.hpp +1 -0
  480. package/src/duckdb/src/include/duckdb/common/owning_string_map.hpp +155 -0
  481. package/src/duckdb/src/include/duckdb/common/perfect_map_set.hpp +2 -3
  482. package/src/duckdb/src/include/duckdb/common/platform.hpp +58 -0
  483. package/src/duckdb/src/include/duckdb/common/radix.hpp +172 -27
  484. package/src/duckdb/src/include/duckdb/common/radix_partitioning.hpp +5 -1
  485. package/src/duckdb/src/include/duckdb/common/random_engine.hpp +1 -0
  486. package/src/duckdb/src/include/duckdb/common/re2_regex.hpp +1 -1
  487. package/src/duckdb/src/include/duckdb/common/render_tree.hpp +77 -0
  488. package/src/duckdb/src/include/duckdb/common/row_operations/row_matcher.hpp +12 -0
  489. package/src/duckdb/src/include/duckdb/common/serializer/binary_serializer.hpp +6 -2
  490. package/src/duckdb/src/include/duckdb/common/serializer/buffered_file_writer.hpp +5 -3
  491. package/src/duckdb/src/include/duckdb/common/serializer/deserializer.hpp +15 -7
  492. package/src/duckdb/src/include/duckdb/common/serializer/memory_stream.hpp +3 -1
  493. package/src/duckdb/src/include/duckdb/common/serializer/serialization_data.hpp +245 -0
  494. package/src/duckdb/src/include/duckdb/common/serializer/serializer.hpp +10 -0
  495. package/src/duckdb/src/include/duckdb/common/sort/duckdb_pdqsort.hpp +10 -11
  496. package/src/duckdb/src/include/duckdb/common/sort/partition_state.hpp +12 -6
  497. package/src/duckdb/src/include/duckdb/common/string_util.hpp +37 -7
  498. package/src/duckdb/src/include/duckdb/common/tree_renderer/graphviz_tree_renderer.hpp +44 -0
  499. package/src/duckdb/src/include/duckdb/common/tree_renderer/html_tree_renderer.hpp +44 -0
  500. package/src/duckdb/src/include/duckdb/common/tree_renderer/json_tree_renderer.hpp +44 -0
  501. package/src/duckdb/src/include/duckdb/common/tree_renderer/text_tree_renderer.hpp +119 -0
  502. package/src/duckdb/src/include/duckdb/common/tree_renderer.hpp +9 -123
  503. package/src/duckdb/src/include/duckdb/common/type_visitor.hpp +96 -0
  504. package/src/duckdb/src/include/duckdb/common/typedefs.hpp +11 -1
  505. package/src/duckdb/src/include/duckdb/common/types/arrow_string_view_type.hpp +84 -0
  506. package/src/duckdb/src/include/duckdb/common/types/batched_data_collection.hpp +36 -1
  507. package/src/duckdb/src/include/duckdb/common/types/bit.hpp +1 -1
  508. package/src/duckdb/src/include/duckdb/common/types/cast_helpers.hpp +2 -2
  509. package/src/duckdb/src/include/duckdb/common/types/column/column_data_allocator.hpp +4 -2
  510. package/src/duckdb/src/include/duckdb/common/types/column/partitioned_column_data.hpp +52 -0
  511. package/src/duckdb/src/include/duckdb/common/types/data_chunk.hpp +2 -0
  512. package/src/duckdb/src/include/duckdb/common/types/date.hpp +0 -3
  513. package/src/duckdb/src/include/duckdb/common/types/date_lookup_cache.hpp +65 -0
  514. package/src/duckdb/src/include/duckdb/common/types/datetime.hpp +5 -2
  515. package/src/duckdb/src/include/duckdb/common/types/hyperloglog.hpp +49 -40
  516. package/src/duckdb/src/include/duckdb/common/types/interval.hpp +5 -1
  517. package/src/duckdb/src/include/duckdb/common/types/list_segment.hpp +2 -1
  518. package/src/duckdb/src/include/duckdb/common/types/row/partitioned_tuple_data.hpp +41 -9
  519. package/src/duckdb/src/include/duckdb/common/types/row/row_data_collection.hpp +4 -3
  520. package/src/duckdb/src/include/duckdb/common/types/row/row_data_collection_scanner.hpp +3 -1
  521. package/src/duckdb/src/include/duckdb/common/types/row/tuple_data_allocator.hpp +4 -0
  522. package/src/duckdb/src/include/duckdb/common/types/row/tuple_data_collection.hpp +4 -0
  523. package/src/duckdb/src/include/duckdb/common/types/row/tuple_data_states.hpp +1 -1
  524. package/src/duckdb/src/include/duckdb/common/types/selection_vector.hpp +4 -0
  525. package/src/duckdb/src/include/duckdb/common/types/string_type.hpp +4 -1
  526. package/src/duckdb/src/include/duckdb/common/types/time.hpp +11 -6
  527. package/src/duckdb/src/include/duckdb/common/types/timestamp.hpp +13 -3
  528. package/src/duckdb/src/include/duckdb/common/types/validity_mask.hpp +103 -12
  529. package/src/duckdb/src/include/duckdb/common/types/value.hpp +12 -3
  530. package/src/duckdb/src/include/duckdb/common/types/varint.hpp +107 -0
  531. package/src/duckdb/src/include/duckdb/common/types/vector.hpp +5 -1
  532. package/src/duckdb/src/include/duckdb/common/types/vector_buffer.hpp +7 -2
  533. package/src/duckdb/src/include/duckdb/common/types.hpp +6 -39
  534. package/src/duckdb/src/include/duckdb/common/union_by_name.hpp +42 -10
  535. package/src/duckdb/src/include/duckdb/common/vector_operations/generic_executor.hpp +29 -0
  536. package/src/duckdb/src/include/duckdb/common/vector_operations/unary_executor.hpp +0 -7
  537. package/src/duckdb/src/include/duckdb/common/vector_operations/vector_operations.hpp +2 -0
  538. package/src/duckdb/src/include/duckdb/common/winapi.hpp +8 -0
  539. package/src/duckdb/src/include/duckdb/core_functions/aggregate/algebraic/covar.hpp +8 -4
  540. package/src/duckdb/src/include/duckdb/core_functions/aggregate/algebraic/stddev.hpp +8 -4
  541. package/src/duckdb/src/include/duckdb/core_functions/aggregate/distributive_functions.hpp +4 -2
  542. package/src/duckdb/src/include/duckdb/core_functions/aggregate/histogram_helpers.hpp +99 -0
  543. package/src/duckdb/src/include/duckdb/core_functions/aggregate/holistic_functions.hpp +16 -7
  544. package/src/duckdb/src/include/duckdb/core_functions/aggregate/minmax_n_helpers.hpp +396 -0
  545. package/src/duckdb/src/include/duckdb/core_functions/aggregate/nested_functions.hpp +10 -0
  546. package/src/duckdb/src/include/duckdb/core_functions/aggregate/quantile_helpers.hpp +65 -0
  547. package/src/duckdb/src/include/duckdb/core_functions/aggregate/quantile_sort_tree.hpp +349 -0
  548. package/src/duckdb/src/include/duckdb/core_functions/aggregate/quantile_state.hpp +300 -0
  549. package/src/duckdb/src/include/duckdb/core_functions/aggregate/regression/regr_slope.hpp +1 -1
  550. package/src/duckdb/src/include/duckdb/core_functions/aggregate/sort_key_helpers.hpp +55 -0
  551. package/src/duckdb/src/include/duckdb/core_functions/array_kernels.hpp +107 -0
  552. package/src/duckdb/src/include/duckdb/core_functions/create_sort_key.hpp +55 -0
  553. package/src/duckdb/src/include/duckdb/core_functions/lambda_functions.hpp +1 -2
  554. package/src/duckdb/src/include/duckdb/core_functions/scalar/array_functions.hpp +24 -0
  555. package/src/duckdb/src/include/duckdb/core_functions/scalar/date_functions.hpp +9 -0
  556. package/src/duckdb/src/include/duckdb/core_functions/scalar/generic_functions.hpp +27 -0
  557. package/src/duckdb/src/include/duckdb/core_functions/scalar/list_functions.hpp +80 -8
  558. package/src/duckdb/src/include/duckdb/core_functions/scalar/map_functions.hpp +9 -0
  559. package/src/duckdb/src/include/duckdb/core_functions/scalar/math_functions.hpp +54 -0
  560. package/src/duckdb/src/include/duckdb/core_functions/scalar/string_functions.hpp +30 -21
  561. package/src/duckdb/src/include/duckdb/execution/adaptive_filter.hpp +25 -14
  562. package/src/duckdb/src/include/duckdb/execution/aggregate_hashtable.hpp +2 -48
  563. package/src/duckdb/src/include/duckdb/execution/executor.hpp +25 -2
  564. package/src/duckdb/src/include/duckdb/execution/ht_entry.hpp +102 -0
  565. package/src/duckdb/src/include/duckdb/execution/index/art/art.hpp +94 -101
  566. package/src/duckdb/src/include/duckdb/execution/index/art/art_key.hpp +43 -25
  567. package/src/duckdb/src/include/duckdb/execution/index/art/base_leaf.hpp +109 -0
  568. package/src/duckdb/src/include/duckdb/execution/index/art/base_node.hpp +140 -0
  569. package/src/duckdb/src/include/duckdb/execution/index/art/iterator.hpp +43 -24
  570. package/src/duckdb/src/include/duckdb/execution/index/art/leaf.hpp +41 -52
  571. package/src/duckdb/src/include/duckdb/execution/index/art/node.hpp +133 -74
  572. package/src/duckdb/src/include/duckdb/execution/index/art/node256.hpp +46 -29
  573. package/src/duckdb/src/include/duckdb/execution/index/art/node256_leaf.hpp +53 -0
  574. package/src/duckdb/src/include/duckdb/execution/index/art/node48.hpp +52 -35
  575. package/src/duckdb/src/include/duckdb/execution/index/art/prefix.hpp +96 -57
  576. package/src/duckdb/src/include/duckdb/execution/index/bound_index.hpp +9 -4
  577. package/src/duckdb/src/include/duckdb/execution/index/fixed_size_allocator.hpp +48 -10
  578. package/src/duckdb/src/include/duckdb/execution/index/fixed_size_buffer.hpp +0 -2
  579. package/src/duckdb/src/include/duckdb/execution/index/index_pointer.hpp +4 -2
  580. package/src/duckdb/src/include/duckdb/execution/join_hashtable.hpp +114 -36
  581. package/src/duckdb/src/include/duckdb/execution/merge_sort_tree.hpp +158 -67
  582. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/aggregate_object.hpp +1 -1
  583. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/physical_hash_aggregate.hpp +1 -1
  584. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/physical_perfecthash_aggregate.hpp +1 -1
  585. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/physical_streaming_window.hpp +19 -2
  586. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/physical_ungrouped_aggregate.hpp +1 -1
  587. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/physical_window.hpp +1 -1
  588. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/ungrouped_aggregate_state.hpp +75 -0
  589. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/base_scanner.hpp +81 -23
  590. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/column_count_scanner.hpp +27 -8
  591. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_buffer_manager.hpp +2 -1
  592. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_error.hpp +31 -22
  593. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_file_handle.hpp +4 -2
  594. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_file_scanner.hpp +48 -5
  595. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_option.hpp +7 -3
  596. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_reader_options.hpp +22 -12
  597. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_schema.hpp +35 -0
  598. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_sniffer.hpp +81 -39
  599. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_state.hpp +2 -1
  600. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_state_machine.hpp +18 -1
  601. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_state_machine_cache.hpp +9 -7
  602. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/global_csv_state.hpp +5 -4
  603. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/header_value.hpp +26 -0
  604. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/scanner_boundary.hpp +6 -9
  605. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/skip_scanner.hpp +3 -0
  606. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/state_machine_options.hpp +5 -3
  607. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/string_value_scanner.hpp +36 -19
  608. package/src/duckdb/src/include/duckdb/execution/operator/filter/physical_filter.hpp +1 -1
  609. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_batch_collector.hpp +21 -0
  610. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_buffered_batch_collector.hpp +53 -0
  611. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_buffered_collector.hpp +3 -0
  612. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_explain_analyze.hpp +6 -2
  613. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_materialized_collector.hpp +18 -0
  614. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_reservoir_sample.hpp +1 -1
  615. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_result_collector.hpp +6 -0
  616. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_set.hpp +2 -2
  617. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_set_variable.hpp +43 -0
  618. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_streaming_sample.hpp +1 -1
  619. package/src/duckdb/src/include/duckdb/execution/operator/join/join_filter_pushdown.hpp +59 -0
  620. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_blockwise_nl_join.hpp +1 -1
  621. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_comparison_join.hpp +8 -1
  622. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_delim_join.hpp +5 -2
  623. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_hash_join.hpp +4 -2
  624. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_iejoin.hpp +2 -0
  625. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_join.hpp +1 -1
  626. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_left_delim_join.hpp +3 -1
  627. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_range_join.hpp +4 -1
  628. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_right_delim_join.hpp +3 -1
  629. package/src/duckdb/src/include/duckdb/execution/operator/order/physical_order.hpp +1 -1
  630. package/src/duckdb/src/include/duckdb/execution/operator/order/physical_top_n.hpp +1 -1
  631. package/src/duckdb/src/include/duckdb/execution/operator/persistent/batch_memory_manager.hpp +5 -37
  632. package/src/duckdb/src/include/duckdb/execution/operator/persistent/physical_batch_copy_to_file.hpp +5 -4
  633. package/src/duckdb/src/include/duckdb/execution/operator/persistent/physical_copy_to_file.hpp +8 -2
  634. package/src/duckdb/src/include/duckdb/execution/operator/projection/physical_projection.hpp +1 -1
  635. package/src/duckdb/src/include/duckdb/execution/operator/projection/physical_tableinout_function.hpp +2 -0
  636. package/src/duckdb/src/include/duckdb/execution/operator/scan/physical_column_data_scan.hpp +9 -3
  637. package/src/duckdb/src/include/duckdb/execution/operator/scan/physical_table_scan.hpp +8 -6
  638. package/src/duckdb/src/include/duckdb/execution/operator/schema/physical_create_art_index.hpp +2 -2
  639. package/src/duckdb/src/include/duckdb/execution/operator/set/physical_cte.hpp +1 -1
  640. package/src/duckdb/src/include/duckdb/execution/operator/set/physical_recursive_cte.hpp +1 -1
  641. package/src/duckdb/src/include/duckdb/execution/physical_operator.hpp +21 -6
  642. package/src/duckdb/src/include/duckdb/execution/physical_operator_states.hpp +3 -2
  643. package/src/duckdb/src/include/duckdb/execution/physical_plan_generator.hpp +3 -0
  644. package/src/duckdb/src/include/duckdb/execution/window_executor.hpp +137 -110
  645. package/src/duckdb/src/include/duckdb/execution/window_segment_tree.hpp +57 -126
  646. package/src/duckdb/src/include/duckdb/function/aggregate_function.hpp +21 -4
  647. package/src/duckdb/src/include/duckdb/function/cast/default_casts.hpp +1 -1
  648. package/src/duckdb/src/include/duckdb/function/compression/compression.hpp +10 -10
  649. package/src/duckdb/src/include/duckdb/function/compression_function.hpp +37 -7
  650. package/src/duckdb/src/include/duckdb/function/copy_function.hpp +24 -11
  651. package/src/duckdb/src/include/duckdb/function/function_binder.hpp +4 -4
  652. package/src/duckdb/src/include/duckdb/function/function_serialization.hpp +41 -1
  653. package/src/duckdb/src/include/duckdb/function/macro_function.hpp +15 -5
  654. package/src/duckdb/src/include/duckdb/function/pragma/pragma_functions.hpp +1 -0
  655. package/src/duckdb/src/include/duckdb/function/replacement_scan.hpp +20 -4
  656. package/src/duckdb/src/include/duckdb/function/scalar/generic_functions.hpp +6 -0
  657. package/src/duckdb/src/include/duckdb/function/scalar/list/contains_or_position.hpp +77 -109
  658. package/src/duckdb/src/include/duckdb/function/scalar/nested_functions.hpp +1 -1
  659. package/src/duckdb/src/include/duckdb/function/scalar/regexp.hpp +6 -3
  660. package/src/duckdb/src/include/duckdb/function/scalar/strftime_format.hpp +25 -12
  661. package/src/duckdb/src/include/duckdb/function/scalar/string_functions.hpp +9 -8
  662. package/src/duckdb/src/include/duckdb/function/scalar_function.hpp +38 -4
  663. package/src/duckdb/src/include/duckdb/function/scalar_macro_function.hpp +1 -1
  664. package/src/duckdb/src/include/duckdb/function/table/arrow/arrow_duck_schema.hpp +11 -57
  665. package/src/duckdb/src/include/duckdb/function/table/arrow/arrow_type_info.hpp +142 -0
  666. package/src/duckdb/src/include/duckdb/function/table/arrow/enum/arrow_datetime_type.hpp +18 -0
  667. package/src/duckdb/src/include/duckdb/function/table/arrow/enum/arrow_type_info_type.hpp +7 -0
  668. package/src/duckdb/src/include/duckdb/function/table/arrow/enum/arrow_variable_size_type.hpp +10 -0
  669. package/src/duckdb/src/include/duckdb/function/table/arrow.hpp +2 -0
  670. package/src/duckdb/src/include/duckdb/function/table/range.hpp +4 -0
  671. package/src/duckdb/src/include/duckdb/function/table/read_csv.hpp +4 -1
  672. package/src/duckdb/src/include/duckdb/function/table/system_functions.hpp +4 -0
  673. package/src/duckdb/src/include/duckdb/function/table/table_scan.hpp +5 -5
  674. package/src/duckdb/src/include/duckdb/function/table_function.hpp +14 -2
  675. package/src/duckdb/src/include/duckdb/function/table_macro_function.hpp +1 -1
  676. package/src/duckdb/src/include/duckdb/main/appender.hpp +14 -4
  677. package/src/duckdb/src/include/duckdb/main/attached_database.hpp +25 -7
  678. package/src/duckdb/src/include/duckdb/main/buffered_data/batched_buffered_data.hpp +79 -0
  679. package/src/duckdb/src/include/duckdb/main/buffered_data/buffered_data.hpp +10 -20
  680. package/src/duckdb/src/include/duckdb/main/buffered_data/simple_buffered_data.hpp +11 -12
  681. package/src/duckdb/src/include/duckdb/main/capi/capi_internal.hpp +7 -2
  682. package/src/duckdb/src/include/duckdb/main/capi/cast/generic.hpp +1 -1
  683. package/src/duckdb/src/include/duckdb/main/capi/cast/utils.hpp +2 -2
  684. package/src/duckdb/src/include/duckdb/main/capi/extension_api.hpp +809 -0
  685. package/src/duckdb/src/include/duckdb/main/chunk_scan_state/batched_data_collection.hpp +35 -0
  686. package/src/duckdb/src/include/duckdb/main/client_config.hpp +68 -2
  687. package/src/duckdb/src/include/duckdb/main/client_context.hpp +30 -22
  688. package/src/duckdb/src/include/duckdb/main/client_context_state.hpp +79 -1
  689. package/src/duckdb/src/include/duckdb/main/client_properties.hpp +9 -3
  690. package/src/duckdb/src/include/duckdb/main/config.hpp +55 -7
  691. package/src/duckdb/src/include/duckdb/main/connection.hpp +5 -1
  692. package/src/duckdb/src/include/duckdb/main/database.hpp +16 -5
  693. package/src/duckdb/src/include/duckdb/main/database_manager.hpp +9 -8
  694. package/src/duckdb/src/include/duckdb/main/db_instance_cache.hpp +21 -6
  695. package/src/duckdb/src/include/duckdb/main/extension.hpp +20 -0
  696. package/src/duckdb/src/include/duckdb/main/extension_entries.hpp +25 -0
  697. package/src/duckdb/src/include/duckdb/main/extension_helper.hpp +29 -23
  698. package/src/duckdb/src/include/duckdb/main/extension_install_info.hpp +6 -0
  699. package/src/duckdb/src/include/duckdb/main/extension_util.hpp +3 -0
  700. package/src/duckdb/src/include/duckdb/main/pending_query_result.hpp +4 -2
  701. package/src/duckdb/src/include/duckdb/main/prepared_statement.hpp +5 -6
  702. package/src/duckdb/src/include/duckdb/main/prepared_statement_data.hpp +2 -5
  703. package/src/duckdb/src/include/duckdb/main/profiling_info.hpp +87 -0
  704. package/src/duckdb/src/include/duckdb/main/profiling_node.hpp +60 -0
  705. package/src/duckdb/src/include/duckdb/main/query_profiler.hpp +72 -34
  706. package/src/duckdb/src/include/duckdb/main/query_result.hpp +1 -1
  707. package/src/duckdb/src/include/duckdb/main/relation/create_table_relation.hpp +2 -1
  708. package/src/duckdb/src/include/duckdb/main/relation/delim_get_relation.hpp +30 -0
  709. package/src/duckdb/src/include/duckdb/main/relation/explain_relation.hpp +3 -1
  710. package/src/duckdb/src/include/duckdb/main/relation/join_relation.hpp +3 -0
  711. package/src/duckdb/src/include/duckdb/main/relation/materialized_relation.hpp +1 -4
  712. package/src/duckdb/src/include/duckdb/main/relation/query_relation.hpp +4 -1
  713. package/src/duckdb/src/include/duckdb/main/relation/read_json_relation.hpp +6 -0
  714. package/src/duckdb/src/include/duckdb/main/relation/table_function_relation.hpp +1 -0
  715. package/src/duckdb/src/include/duckdb/main/relation/view_relation.hpp +2 -0
  716. package/src/duckdb/src/include/duckdb/main/relation.hpp +7 -4
  717. package/src/duckdb/src/include/duckdb/main/secret/default_secrets.hpp +36 -0
  718. package/src/duckdb/src/include/duckdb/main/secret/secret.hpp +108 -0
  719. package/src/duckdb/src/include/duckdb/main/secret/secret_manager.hpp +14 -4
  720. package/src/duckdb/src/include/duckdb/main/settings.hpp +227 -3
  721. package/src/duckdb/src/include/duckdb/main/stream_query_result.hpp +8 -0
  722. package/src/duckdb/src/include/duckdb/optimizer/build_probe_side_optimizer.hpp +51 -0
  723. package/src/duckdb/src/include/duckdb/optimizer/compressed_materialization.hpp +7 -0
  724. package/src/duckdb/src/include/duckdb/optimizer/cte_filter_pusher.hpp +46 -0
  725. package/src/duckdb/src/include/duckdb/optimizer/filter_combiner.hpp +1 -1
  726. package/src/duckdb/src/include/duckdb/optimizer/filter_pushdown.hpp +7 -0
  727. package/src/duckdb/src/include/duckdb/optimizer/join_filter_pushdown_optimizer.hpp +31 -0
  728. package/src/duckdb/src/include/duckdb/optimizer/join_order/cardinality_estimator.hpp +51 -10
  729. package/src/duckdb/src/include/duckdb/optimizer/join_order/cost_model.hpp +1 -0
  730. package/src/duckdb/src/include/duckdb/optimizer/join_order/join_order_optimizer.hpp +17 -5
  731. package/src/duckdb/src/include/duckdb/optimizer/join_order/query_graph.hpp +1 -1
  732. package/src/duckdb/src/include/duckdb/optimizer/join_order/query_graph_manager.hpp +15 -13
  733. package/src/duckdb/src/include/duckdb/optimizer/join_order/relation_manager.hpp +9 -4
  734. package/src/duckdb/src/include/duckdb/optimizer/limit_pushdown.hpp +25 -0
  735. package/src/duckdb/src/include/duckdb/optimizer/optimizer.hpp +1 -0
  736. package/src/duckdb/src/include/duckdb/optimizer/rule/join_dependent_filter.hpp +37 -0
  737. package/src/duckdb/src/include/duckdb/parallel/executor_task.hpp +6 -1
  738. package/src/duckdb/src/include/duckdb/parallel/interrupt.hpp +54 -2
  739. package/src/duckdb/src/include/duckdb/parallel/meta_pipeline.hpp +27 -8
  740. package/src/duckdb/src/include/duckdb/parallel/pipeline.hpp +1 -0
  741. package/src/duckdb/src/include/duckdb/parallel/pipeline_prepare_finish_event.hpp +25 -0
  742. package/src/duckdb/src/include/duckdb/parallel/task_executor.hpp +63 -0
  743. package/src/duckdb/src/include/duckdb/parallel/task_scheduler.hpp +10 -1
  744. package/src/duckdb/src/include/duckdb/parser/expression/function_expression.hpp +4 -1
  745. package/src/duckdb/src/include/duckdb/parser/expression/star_expression.hpp +5 -0
  746. package/src/duckdb/src/include/duckdb/parser/parsed_data/alter_table_info.hpp +5 -0
  747. package/src/duckdb/src/include/duckdb/parser/parsed_data/attach_info.hpp +5 -0
  748. package/src/duckdb/src/include/duckdb/parser/parsed_data/create_index_info.hpp +2 -0
  749. package/src/duckdb/src/include/duckdb/parser/parsed_data/create_macro_info.hpp +11 -1
  750. package/src/duckdb/src/include/duckdb/parser/parsed_data/transaction_info.hpp +9 -0
  751. package/src/duckdb/src/include/duckdb/parser/parsed_expression_iterator.hpp +13 -6
  752. package/src/duckdb/src/include/duckdb/parser/parser_extension.hpp +1 -1
  753. package/src/duckdb/src/include/duckdb/parser/sql_statement.hpp +1 -3
  754. package/src/duckdb/src/include/duckdb/parser/statement/copy_statement.hpp +2 -0
  755. package/src/duckdb/src/include/duckdb/parser/statement/explain_statement.hpp +5 -1
  756. package/src/duckdb/src/include/duckdb/parser/statement/set_statement.hpp +2 -2
  757. package/src/duckdb/src/include/duckdb/parser/statement/transaction_statement.hpp +1 -1
  758. package/src/duckdb/src/include/duckdb/parser/tableref/basetableref.hpp +0 -2
  759. package/src/duckdb/src/include/duckdb/parser/tableref/column_data_ref.hpp +9 -7
  760. package/src/duckdb/src/include/duckdb/parser/tableref/delimgetref.hpp +37 -0
  761. package/src/duckdb/src/include/duckdb/parser/tableref/joinref.hpp +4 -0
  762. package/src/duckdb/src/include/duckdb/parser/tableref/pivotref.hpp +0 -2
  763. package/src/duckdb/src/include/duckdb/parser/tableref/subqueryref.hpp +0 -2
  764. package/src/duckdb/src/include/duckdb/parser/tableref/table_function_ref.hpp +0 -1
  765. package/src/duckdb/src/include/duckdb/parser/tableref.hpp +3 -1
  766. package/src/duckdb/src/include/duckdb/parser/transformer.hpp +17 -9
  767. package/src/duckdb/src/include/duckdb/planner/binder.hpp +24 -14
  768. package/src/duckdb/src/include/duckdb/planner/collation_binding.hpp +44 -0
  769. package/src/duckdb/src/include/duckdb/planner/expression/bound_aggregate_expression.hpp +1 -1
  770. package/src/duckdb/src/include/duckdb/planner/expression/bound_between_expression.hpp +1 -1
  771. package/src/duckdb/src/include/duckdb/planner/expression/bound_case_expression.hpp +1 -1
  772. package/src/duckdb/src/include/duckdb/planner/expression/bound_cast_expression.hpp +1 -1
  773. package/src/duckdb/src/include/duckdb/planner/expression/bound_columnref_expression.hpp +1 -1
  774. package/src/duckdb/src/include/duckdb/planner/expression/bound_comparison_expression.hpp +1 -1
  775. package/src/duckdb/src/include/duckdb/planner/expression/bound_conjunction_expression.hpp +1 -1
  776. package/src/duckdb/src/include/duckdb/planner/expression/bound_constant_expression.hpp +1 -1
  777. package/src/duckdb/src/include/duckdb/planner/expression/bound_default_expression.hpp +1 -1
  778. package/src/duckdb/src/include/duckdb/planner/expression/bound_expanded_expression.hpp +1 -1
  779. package/src/duckdb/src/include/duckdb/planner/expression/bound_function_expression.hpp +1 -1
  780. package/src/duckdb/src/include/duckdb/planner/expression/bound_lambda_expression.hpp +1 -1
  781. package/src/duckdb/src/include/duckdb/planner/expression/bound_lambdaref_expression.hpp +1 -1
  782. package/src/duckdb/src/include/duckdb/planner/expression/bound_operator_expression.hpp +1 -1
  783. package/src/duckdb/src/include/duckdb/planner/expression/bound_parameter_data.hpp +2 -0
  784. package/src/duckdb/src/include/duckdb/planner/expression/bound_parameter_expression.hpp +1 -1
  785. package/src/duckdb/src/include/duckdb/planner/expression/bound_reference_expression.hpp +1 -1
  786. package/src/duckdb/src/include/duckdb/planner/expression/bound_subquery_expression.hpp +2 -2
  787. package/src/duckdb/src/include/duckdb/planner/expression/bound_unnest_expression.hpp +1 -1
  788. package/src/duckdb/src/include/duckdb/planner/expression/bound_window_expression.hpp +1 -1
  789. package/src/duckdb/src/include/duckdb/planner/expression.hpp +2 -2
  790. package/src/duckdb/src/include/duckdb/planner/expression_binder/column_alias_binder.hpp +2 -0
  791. package/src/duckdb/src/include/duckdb/planner/expression_binder/group_binder.hpp +1 -0
  792. package/src/duckdb/src/include/duckdb/planner/expression_binder/order_binder.hpp +6 -5
  793. package/src/duckdb/src/include/duckdb/planner/expression_binder/where_binder.hpp +1 -0
  794. package/src/duckdb/src/include/duckdb/planner/expression_binder.hpp +19 -11
  795. package/src/duckdb/src/include/duckdb/planner/filter/conjunction_filter.hpp +4 -0
  796. package/src/duckdb/src/include/duckdb/planner/filter/constant_filter.hpp +2 -0
  797. package/src/duckdb/src/include/duckdb/planner/filter/null_filter.hpp +4 -0
  798. package/src/duckdb/src/include/duckdb/planner/filter/struct_filter.hpp +2 -0
  799. package/src/duckdb/src/include/duckdb/planner/logical_operator.hpp +7 -2
  800. package/src/duckdb/src/include/duckdb/planner/logical_operator_visitor.hpp +2 -1
  801. package/src/duckdb/src/include/duckdb/planner/operator/logical_aggregate.hpp +1 -1
  802. package/src/duckdb/src/include/duckdb/planner/operator/logical_any_join.hpp +1 -1
  803. package/src/duckdb/src/include/duckdb/planner/operator/logical_comparison_join.hpp +6 -1
  804. package/src/duckdb/src/include/duckdb/planner/operator/logical_copy_to_file.hpp +10 -2
  805. package/src/duckdb/src/include/duckdb/planner/operator/logical_cteref.hpp +1 -0
  806. package/src/duckdb/src/include/duckdb/planner/operator/logical_delim_get.hpp +1 -1
  807. package/src/duckdb/src/include/duckdb/planner/operator/logical_distinct.hpp +1 -1
  808. package/src/duckdb/src/include/duckdb/planner/operator/logical_execute.hpp +1 -1
  809. package/src/duckdb/src/include/duckdb/planner/operator/logical_explain.hpp +4 -2
  810. package/src/duckdb/src/include/duckdb/planner/operator/logical_get.hpp +15 -5
  811. package/src/duckdb/src/include/duckdb/planner/operator/logical_materialized_cte.hpp +1 -0
  812. package/src/duckdb/src/include/duckdb/planner/operator/logical_order.hpp +1 -1
  813. package/src/duckdb/src/include/duckdb/planner/subquery/flatten_dependent_join.hpp +2 -1
  814. package/src/duckdb/src/include/duckdb/planner/table_filter.hpp +24 -2
  815. package/src/duckdb/src/include/duckdb/planner/tableref/bound_delimgetref.hpp +26 -0
  816. package/src/duckdb/src/include/duckdb/planner/tableref/bound_joinref.hpp +6 -0
  817. package/src/duckdb/src/include/duckdb/planner/tableref/bound_subqueryref.hpp +1 -1
  818. package/src/duckdb/src/include/duckdb/planner/tableref/bound_table_function.hpp +2 -0
  819. package/src/duckdb/src/include/duckdb/planner/tableref/list.hpp +2 -0
  820. package/src/duckdb/src/include/duckdb/storage/arena_allocator.hpp +2 -1
  821. package/src/duckdb/src/include/duckdb/storage/block.hpp +4 -2
  822. package/src/duckdb/src/include/duckdb/storage/block_manager.hpp +48 -3
  823. package/src/duckdb/src/include/duckdb/storage/buffer/block_handle.hpp +21 -7
  824. package/src/duckdb/src/include/duckdb/storage/buffer/buffer_pool.hpp +65 -51
  825. package/src/duckdb/src/include/duckdb/storage/buffer_manager.hpp +14 -5
  826. package/src/duckdb/src/include/duckdb/storage/checkpoint/row_group_writer.hpp +0 -4
  827. package/src/duckdb/src/include/duckdb/storage/checkpoint/string_checkpoint_state.hpp +3 -2
  828. package/src/duckdb/src/include/duckdb/storage/checkpoint/table_data_writer.hpp +1 -0
  829. package/src/duckdb/src/include/duckdb/storage/checkpoint/write_overflow_strings_to_disk.hpp +3 -4
  830. package/src/duckdb/src/include/duckdb/storage/checkpoint_manager.hpp +2 -0
  831. package/src/duckdb/src/include/duckdb/storage/compression/alp/algorithm/alp.hpp +4 -4
  832. package/src/duckdb/src/include/duckdb/storage/compression/alp/alp_analyze.hpp +6 -4
  833. package/src/duckdb/src/include/duckdb/storage/compression/alp/alp_compress.hpp +19 -17
  834. package/src/duckdb/src/include/duckdb/storage/compression/alp/alp_constants.hpp +2 -2
  835. package/src/duckdb/src/include/duckdb/storage/compression/alp/alp_scan.hpp +3 -4
  836. package/src/duckdb/src/include/duckdb/storage/compression/alp/alp_utils.hpp +3 -2
  837. package/src/duckdb/src/include/duckdb/storage/compression/alprd/algorithm/alprd.hpp +3 -2
  838. package/src/duckdb/src/include/duckdb/storage/compression/alprd/alprd_analyze.hpp +13 -11
  839. package/src/duckdb/src/include/duckdb/storage/compression/alprd/alprd_compress.hpp +19 -19
  840. package/src/duckdb/src/include/duckdb/storage/compression/alprd/alprd_scan.hpp +3 -4
  841. package/src/duckdb/src/include/duckdb/storage/compression/chimp/chimp_scan.hpp +1 -1
  842. package/src/duckdb/src/include/duckdb/storage/compression/patas/patas_scan.hpp +1 -1
  843. package/src/duckdb/src/include/duckdb/storage/data_pointer.hpp +10 -2
  844. package/src/duckdb/src/include/duckdb/storage/data_table.hpp +3 -2
  845. package/src/duckdb/src/include/duckdb/storage/in_memory_block_manager.hpp +15 -0
  846. package/src/duckdb/src/include/duckdb/storage/index_storage_info.hpp +14 -10
  847. package/src/duckdb/src/include/duckdb/storage/metadata/metadata_manager.hpp +6 -8
  848. package/src/duckdb/src/include/duckdb/storage/partial_block_manager.hpp +7 -4
  849. package/src/duckdb/src/include/duckdb/storage/segment/uncompressed.hpp +4 -7
  850. package/src/duckdb/src/include/duckdb/storage/single_file_block_manager.hpp +29 -4
  851. package/src/duckdb/src/include/duckdb/storage/standard_buffer_manager.hpp +22 -7
  852. package/src/duckdb/src/include/duckdb/storage/statistics/base_statistics.hpp +15 -2
  853. package/src/duckdb/src/include/duckdb/storage/statistics/distinct_statistics.hpp +8 -2
  854. package/src/duckdb/src/include/duckdb/storage/statistics/numeric_stats.hpp +5 -16
  855. package/src/duckdb/src/include/duckdb/storage/statistics/numeric_stats_union.hpp +51 -13
  856. package/src/duckdb/src/include/duckdb/storage/statistics/string_stats.hpp +6 -3
  857. package/src/duckdb/src/include/duckdb/storage/storage_info.hpp +29 -19
  858. package/src/duckdb/src/include/duckdb/storage/storage_manager.hpp +23 -7
  859. package/src/duckdb/src/include/duckdb/storage/string_uncompressed.hpp +27 -18
  860. package/src/duckdb/src/include/duckdb/storage/table/append_state.hpp +6 -3
  861. package/src/duckdb/src/include/duckdb/storage/table/array_column_data.hpp +5 -2
  862. package/src/duckdb/src/include/duckdb/storage/table/chunk_info.hpp +3 -0
  863. package/src/duckdb/src/include/duckdb/storage/table/column_checkpoint_state.hpp +5 -1
  864. package/src/duckdb/src/include/duckdb/storage/table/column_data.hpp +77 -6
  865. package/src/duckdb/src/include/duckdb/storage/table/column_segment.hpp +23 -11
  866. package/src/duckdb/src/include/duckdb/storage/table/data_table_info.hpp +3 -0
  867. package/src/duckdb/src/include/duckdb/storage/table/list_column_data.hpp +5 -2
  868. package/src/duckdb/src/include/duckdb/storage/table/row_group.hpp +18 -4
  869. package/src/duckdb/src/include/duckdb/storage/table/row_group_collection.hpp +7 -1
  870. package/src/duckdb/src/include/duckdb/storage/table/row_version_manager.hpp +2 -1
  871. package/src/duckdb/src/include/duckdb/storage/table/scan_state.hpp +89 -14
  872. package/src/duckdb/src/include/duckdb/storage/table/standard_column_data.hpp +4 -2
  873. package/src/duckdb/src/include/duckdb/storage/table/struct_column_data.hpp +4 -2
  874. package/src/duckdb/src/include/duckdb/storage/table/table_index_list.hpp +2 -2
  875. package/src/duckdb/src/include/duckdb/storage/table/validity_column_data.hpp +1 -1
  876. package/src/duckdb/src/include/duckdb/storage/temporary_memory_manager.hpp +33 -15
  877. package/src/duckdb/src/include/duckdb/storage/write_ahead_log.hpp +9 -9
  878. package/src/duckdb/src/include/duckdb/transaction/cleanup_state.hpp +3 -1
  879. package/src/duckdb/src/include/duckdb/transaction/commit_state.hpp +4 -16
  880. package/src/duckdb/src/include/duckdb/transaction/duck_transaction.hpp +27 -4
  881. package/src/duckdb/src/include/duckdb/transaction/duck_transaction_manager.hpp +11 -0
  882. package/src/duckdb/src/include/duckdb/transaction/local_storage.hpp +6 -2
  883. package/src/duckdb/src/include/duckdb/transaction/meta_transaction.hpp +5 -5
  884. package/src/duckdb/src/include/duckdb/transaction/transaction_context.hpp +6 -2
  885. package/src/duckdb/src/include/duckdb/transaction/undo_buffer.hpp +5 -3
  886. package/src/duckdb/src/include/duckdb/transaction/wal_write_state.hpp +48 -0
  887. package/src/duckdb/src/include/duckdb.h +1779 -739
  888. package/src/duckdb/src/include/duckdb_extension.h +921 -0
  889. package/src/duckdb/src/main/appender.cpp +53 -7
  890. package/src/duckdb/src/main/attached_database.cpp +87 -17
  891. package/src/duckdb/src/main/buffered_data/batched_buffered_data.cpp +226 -0
  892. package/src/duckdb/src/main/buffered_data/buffered_data.cpp +35 -0
  893. package/src/duckdb/src/main/buffered_data/simple_buffered_data.cpp +48 -23
  894. package/src/duckdb/src/main/capi/aggregate_function-c.cpp +327 -0
  895. package/src/duckdb/src/main/capi/appender-c.cpp +18 -0
  896. package/src/duckdb/src/main/capi/cast/utils-c.cpp +2 -2
  897. package/src/duckdb/src/main/capi/cast_function-c.cpp +210 -0
  898. package/src/duckdb/src/main/capi/config-c.cpp +3 -3
  899. package/src/duckdb/src/main/capi/data_chunk-c.cpp +18 -7
  900. package/src/duckdb/src/main/capi/duckdb_value-c.cpp +223 -24
  901. package/src/duckdb/src/main/capi/helper-c.cpp +51 -11
  902. package/src/duckdb/src/main/capi/logical_types-c.cpp +105 -46
  903. package/src/duckdb/src/main/capi/pending-c.cpp +7 -6
  904. package/src/duckdb/src/main/capi/prepared-c.cpp +18 -7
  905. package/src/duckdb/src/main/capi/profiling_info-c.cpp +84 -0
  906. package/src/duckdb/src/main/capi/result-c.cpp +139 -37
  907. package/src/duckdb/src/main/capi/scalar_function-c.cpp +269 -0
  908. package/src/duckdb/src/main/capi/table_description-c.cpp +82 -0
  909. package/src/duckdb/src/main/capi/table_function-c.cpp +161 -95
  910. package/src/duckdb/src/main/capi/value-c.cpp +2 -2
  911. package/src/duckdb/src/main/chunk_scan_state/batched_data_collection.cpp +57 -0
  912. package/src/duckdb/src/main/client_config.cpp +17 -0
  913. package/src/duckdb/src/main/client_context.cpp +67 -52
  914. package/src/duckdb/src/main/client_data.cpp +3 -3
  915. package/src/duckdb/src/main/config.cpp +120 -62
  916. package/src/duckdb/src/main/connection.cpp +14 -2
  917. package/src/duckdb/src/main/database.cpp +96 -35
  918. package/src/duckdb/src/main/database_manager.cpp +25 -23
  919. package/src/duckdb/src/main/database_path_and_type.cpp +2 -2
  920. package/src/duckdb/src/main/db_instance_cache.cpp +54 -19
  921. package/src/duckdb/src/main/extension/extension_helper.cpp +47 -42
  922. package/src/duckdb/src/main/extension/extension_install.cpp +155 -87
  923. package/src/duckdb/src/main/extension/extension_load.cpp +180 -26
  924. package/src/duckdb/src/main/extension/extension_util.cpp +8 -0
  925. package/src/duckdb/src/main/extension.cpp +72 -5
  926. package/src/duckdb/src/main/pending_query_result.cpp +20 -12
  927. package/src/duckdb/src/main/prepared_statement.cpp +6 -6
  928. package/src/duckdb/src/main/prepared_statement_data.cpp +28 -17
  929. package/src/duckdb/src/main/profiling_info.cpp +196 -0
  930. package/src/duckdb/src/main/query_profiler.cpp +413 -224
  931. package/src/duckdb/src/main/query_result.cpp +1 -1
  932. package/src/duckdb/src/main/relation/create_table_relation.cpp +4 -2
  933. package/src/duckdb/src/main/relation/create_view_relation.cpp +0 -6
  934. package/src/duckdb/src/main/relation/delim_get_relation.cpp +44 -0
  935. package/src/duckdb/src/main/relation/explain_relation.cpp +4 -3
  936. package/src/duckdb/src/main/relation/join_relation.cpp +5 -0
  937. package/src/duckdb/src/main/relation/limit_relation.cpp +1 -1
  938. package/src/duckdb/src/main/relation/materialized_relation.cpp +3 -3
  939. package/src/duckdb/src/main/relation/query_relation.cpp +42 -15
  940. package/src/duckdb/src/main/relation/read_csv_relation.cpp +7 -14
  941. package/src/duckdb/src/main/relation/read_json_relation.cpp +20 -0
  942. package/src/duckdb/src/main/relation/setop_relation.cpp +1 -1
  943. package/src/duckdb/src/main/relation/table_function_relation.cpp +6 -0
  944. package/src/duckdb/src/main/relation/view_relation.cpp +10 -0
  945. package/src/duckdb/src/main/relation.cpp +12 -8
  946. package/src/duckdb/src/main/secret/default_secrets.cpp +108 -0
  947. package/src/duckdb/src/main/secret/secret.cpp +145 -2
  948. package/src/duckdb/src/main/secret/secret_manager.cpp +85 -35
  949. package/src/duckdb/src/main/secret/secret_storage.cpp +29 -17
  950. package/src/duckdb/src/main/settings/settings.cpp +503 -11
  951. package/src/duckdb/src/main/stream_query_result.cpp +75 -2
  952. package/src/duckdb/src/optimizer/build_probe_side_optimizer.cpp +248 -0
  953. package/src/duckdb/src/optimizer/column_lifetime_analyzer.cpp +28 -6
  954. package/src/duckdb/src/optimizer/compressed_materialization/compress_comparison_join.cpp +152 -0
  955. package/src/duckdb/src/optimizer/compressed_materialization.cpp +11 -1
  956. package/src/duckdb/src/optimizer/cse_optimizer.cpp +3 -0
  957. package/src/duckdb/src/optimizer/cte_filter_pusher.cpp +117 -0
  958. package/src/duckdb/src/optimizer/filter_combiner.cpp +30 -9
  959. package/src/duckdb/src/optimizer/filter_pullup.cpp +54 -2
  960. package/src/duckdb/src/optimizer/filter_pushdown.cpp +71 -3
  961. package/src/duckdb/src/optimizer/join_filter_pushdown_optimizer.cpp +154 -0
  962. package/src/duckdb/src/optimizer/join_order/cardinality_estimator.cpp +245 -114
  963. package/src/duckdb/src/optimizer/join_order/join_order_optimizer.cpp +42 -20
  964. package/src/duckdb/src/optimizer/join_order/join_relation_set.cpp +6 -2
  965. package/src/duckdb/src/optimizer/join_order/plan_enumerator.cpp +32 -10
  966. package/src/duckdb/src/optimizer/join_order/query_graph_manager.cpp +97 -131
  967. package/src/duckdb/src/optimizer/join_order/relation_manager.cpp +265 -51
  968. package/src/duckdb/src/optimizer/join_order/relation_statistics_helper.cpp +21 -17
  969. package/src/duckdb/src/optimizer/limit_pushdown.cpp +42 -0
  970. package/src/duckdb/src/optimizer/optimizer.cpp +51 -8
  971. package/src/duckdb/src/optimizer/pushdown/pushdown_aggregate.cpp +17 -17
  972. package/src/duckdb/src/optimizer/pushdown/pushdown_cross_product.cpp +22 -4
  973. package/src/duckdb/src/optimizer/pushdown/pushdown_get.cpp +1 -18
  974. package/src/duckdb/src/optimizer/pushdown/pushdown_inner_join.cpp +6 -0
  975. package/src/duckdb/src/optimizer/pushdown/pushdown_mark_join.cpp +4 -2
  976. package/src/duckdb/src/optimizer/pushdown/pushdown_window.cpp +91 -0
  977. package/src/duckdb/src/optimizer/remove_unused_columns.cpp +21 -25
  978. package/src/duckdb/src/optimizer/rule/comparison_simplification.cpp +1 -0
  979. package/src/duckdb/src/optimizer/rule/empty_needle_removal.cpp +3 -0
  980. package/src/duckdb/src/optimizer/rule/equal_or_null_simplification.cpp +2 -2
  981. package/src/duckdb/src/optimizer/rule/in_clause_simplification_rule.cpp +8 -2
  982. package/src/duckdb/src/optimizer/rule/join_dependent_filter.cpp +135 -0
  983. package/src/duckdb/src/optimizer/rule/like_optimizations.cpp +1 -1
  984. package/src/duckdb/src/optimizer/rule/regex_optimizations.cpp +1 -1
  985. package/src/duckdb/src/optimizer/statistics/operator/propagate_filter.cpp +6 -1
  986. package/src/duckdb/src/optimizer/statistics/operator/propagate_get.cpp +7 -6
  987. package/src/duckdb/src/optimizer/statistics/operator/propagate_join.cpp +1 -1
  988. package/src/duckdb/src/optimizer/topn_optimizer.cpp +46 -7
  989. package/src/duckdb/src/parallel/executor.cpp +129 -51
  990. package/src/duckdb/src/parallel/executor_task.cpp +16 -3
  991. package/src/duckdb/src/parallel/meta_pipeline.cpp +98 -29
  992. package/src/duckdb/src/parallel/pipeline.cpp +17 -3
  993. package/src/duckdb/src/parallel/pipeline_executor.cpp +14 -2
  994. package/src/duckdb/src/parallel/pipeline_prepare_finish_event.cpp +34 -0
  995. package/src/duckdb/src/parallel/task_executor.cpp +84 -0
  996. package/src/duckdb/src/parallel/task_scheduler.cpp +94 -16
  997. package/src/duckdb/src/parallel/thread_context.cpp +1 -1
  998. package/src/duckdb/src/parser/expression/function_expression.cpp +14 -0
  999. package/src/duckdb/src/parser/expression/star_expression.cpp +35 -2
  1000. package/src/duckdb/src/parser/parsed_data/alter_table_info.cpp +5 -1
  1001. package/src/duckdb/src/parser/parsed_data/attach_info.cpp +17 -0
  1002. package/src/duckdb/src/parser/parsed_data/create_index_info.cpp +37 -28
  1003. package/src/duckdb/src/parser/parsed_data/create_macro_info.cpp +44 -2
  1004. package/src/duckdb/src/parser/parsed_data/transaction_info.cpp +21 -1
  1005. package/src/duckdb/src/parser/parsed_expression_iterator.cpp +29 -25
  1006. package/src/duckdb/src/parser/parser.cpp +41 -1
  1007. package/src/duckdb/src/parser/query_node/recursive_cte_node.cpp +1 -0
  1008. package/src/duckdb/src/parser/statement/explain_statement.cpp +28 -13
  1009. package/src/duckdb/src/parser/statement/relation_statement.cpp +5 -0
  1010. package/src/duckdb/src/parser/statement/set_statement.cpp +4 -2
  1011. package/src/duckdb/src/parser/statement/transaction_statement.cpp +3 -3
  1012. package/src/duckdb/src/parser/tableref/column_data_ref.cpp +1 -27
  1013. package/src/duckdb/src/parser/tableref/delimgetref.cpp +30 -0
  1014. package/src/duckdb/src/parser/tableref/joinref.cpp +4 -0
  1015. package/src/duckdb/src/parser/transform/constraint/transform_constraint.cpp +35 -29
  1016. package/src/duckdb/src/parser/transform/expression/transform_array_access.cpp +32 -32
  1017. package/src/duckdb/src/parser/transform/expression/transform_columnref.cpp +2 -1
  1018. package/src/duckdb/src/parser/transform/expression/transform_constant.cpp +17 -0
  1019. package/src/duckdb/src/parser/transform/expression/transform_function.cpp +5 -0
  1020. package/src/duckdb/src/parser/transform/expression/transform_multi_assign_reference.cpp +36 -34
  1021. package/src/duckdb/src/parser/transform/expression/transform_operator.cpp +30 -14
  1022. package/src/duckdb/src/parser/transform/expression/transform_subquery.cpp +1 -1
  1023. package/src/duckdb/src/parser/transform/helpers/transform_alias.cpp +2 -1
  1024. package/src/duckdb/src/parser/transform/helpers/transform_cte.cpp +27 -19
  1025. package/src/duckdb/src/parser/transform/helpers/transform_orderby.cpp +31 -28
  1026. package/src/duckdb/src/parser/transform/statement/transform_alter_table.cpp +25 -27
  1027. package/src/duckdb/src/parser/transform/statement/transform_copy.cpp +1 -1
  1028. package/src/duckdb/src/parser/transform/statement/transform_create_function.cpp +53 -42
  1029. package/src/duckdb/src/parser/transform/statement/transform_create_table.cpp +6 -6
  1030. package/src/duckdb/src/parser/transform/statement/transform_create_table_as.cpp +1 -1
  1031. package/src/duckdb/src/parser/transform/statement/transform_create_type.cpp +1 -1
  1032. package/src/duckdb/src/parser/transform/statement/transform_create_view.cpp +1 -1
  1033. package/src/duckdb/src/parser/transform/statement/transform_explain.cpp +38 -3
  1034. package/src/duckdb/src/parser/transform/statement/transform_insert.cpp +1 -2
  1035. package/src/duckdb/src/parser/transform/statement/transform_pivot_stmt.cpp +1 -1
  1036. package/src/duckdb/src/parser/transform/statement/transform_prepare.cpp +1 -1
  1037. package/src/duckdb/src/parser/transform/statement/transform_select.cpp +26 -21
  1038. package/src/duckdb/src/parser/transform/statement/transform_set.cpp +8 -8
  1039. package/src/duckdb/src/parser/transform/statement/transform_show.cpp +5 -2
  1040. package/src/duckdb/src/parser/transform/statement/transform_show_select.cpp +6 -4
  1041. package/src/duckdb/src/parser/transform/statement/transform_transaction.cpp +27 -6
  1042. package/src/duckdb/src/parser/transform/statement/transform_update.cpp +8 -9
  1043. package/src/duckdb/src/parser/transform/statement/transform_upsert.cpp +11 -12
  1044. package/src/duckdb/src/parser/transform/statement/transform_vacuum.cpp +3 -3
  1045. package/src/duckdb/src/parser/transform/tableref/transform_join.cpp +16 -10
  1046. package/src/duckdb/src/parser/transform/tableref/transform_pivot.cpp +1 -1
  1047. package/src/duckdb/src/parser/transform/tableref/transform_subquery.cpp +1 -1
  1048. package/src/duckdb/src/parser/transformer.cpp +11 -7
  1049. package/src/duckdb/src/planner/bind_context.cpp +3 -3
  1050. package/src/duckdb/src/planner/binder/expression/bind_aggregate_expression.cpp +22 -7
  1051. package/src/duckdb/src/planner/binder/expression/bind_between_expression.cpp +3 -3
  1052. package/src/duckdb/src/planner/binder/expression/bind_collate_expression.cpp +3 -2
  1053. package/src/duckdb/src/planner/binder/expression/bind_columnref_expression.cpp +11 -4
  1054. package/src/duckdb/src/planner/binder/expression/bind_comparison_expression.cpp +9 -54
  1055. package/src/duckdb/src/planner/binder/expression/bind_function_expression.cpp +3 -5
  1056. package/src/duckdb/src/planner/binder/expression/bind_macro_expression.cpp +24 -27
  1057. package/src/duckdb/src/planner/binder/expression/bind_operator_expression.cpp +7 -7
  1058. package/src/duckdb/src/planner/binder/expression/bind_parameter_expression.cpp +9 -2
  1059. package/src/duckdb/src/planner/binder/expression/bind_star_expression.cpp +26 -7
  1060. package/src/duckdb/src/planner/binder/expression/bind_unnest_expression.cpp +5 -0
  1061. package/src/duckdb/src/planner/binder/expression/bind_unpacked_star_expression.cpp +91 -0
  1062. package/src/duckdb/src/planner/binder/expression/bind_window_expression.cpp +2 -2
  1063. package/src/duckdb/src/planner/binder/query_node/bind_select_node.cpp +11 -8
  1064. package/src/duckdb/src/planner/binder/query_node/bind_setop_node.cpp +1 -1
  1065. package/src/duckdb/src/planner/binder/query_node/bind_table_macro_node.cpp +6 -10
  1066. package/src/duckdb/src/planner/binder/query_node/plan_cte_node.cpp +14 -10
  1067. package/src/duckdb/src/planner/binder/query_node/plan_setop.cpp +3 -3
  1068. package/src/duckdb/src/planner/binder/query_node/plan_subquery.cpp +46 -7
  1069. package/src/duckdb/src/planner/binder/statement/bind_call.cpp +13 -20
  1070. package/src/duckdb/src/planner/binder/statement/bind_copy.cpp +105 -13
  1071. package/src/duckdb/src/planner/binder/statement/bind_copy_database.cpp +7 -3
  1072. package/src/duckdb/src/planner/binder/statement/bind_create.cpp +75 -55
  1073. package/src/duckdb/src/planner/binder/statement/bind_create_table.cpp +1 -1
  1074. package/src/duckdb/src/planner/binder/statement/bind_delete.cpp +5 -4
  1075. package/src/duckdb/src/planner/binder/statement/bind_drop.cpp +2 -2
  1076. package/src/duckdb/src/planner/binder/statement/bind_execute.cpp +24 -8
  1077. package/src/duckdb/src/planner/binder/statement/bind_explain.cpp +2 -2
  1078. package/src/duckdb/src/planner/binder/statement/bind_export.cpp +5 -105
  1079. package/src/duckdb/src/planner/binder/statement/bind_extension.cpp +2 -2
  1080. package/src/duckdb/src/planner/binder/statement/bind_insert.cpp +109 -41
  1081. package/src/duckdb/src/planner/binder/statement/bind_set.cpp +23 -7
  1082. package/src/duckdb/src/planner/binder/statement/bind_simple.cpp +4 -1
  1083. package/src/duckdb/src/planner/binder/statement/bind_summarize.cpp +17 -3
  1084. package/src/duckdb/src/planner/binder/statement/bind_update.cpp +5 -4
  1085. package/src/duckdb/src/planner/binder/statement/bind_vacuum.cpp +8 -6
  1086. package/src/duckdb/src/planner/binder/tableref/bind_basetableref.cpp +55 -42
  1087. package/src/duckdb/src/planner/binder/tableref/bind_column_data_ref.cpp +3 -2
  1088. package/src/duckdb/src/planner/binder/tableref/bind_delimgetref.cpp +16 -0
  1089. package/src/duckdb/src/planner/binder/tableref/bind_joinref.cpp +31 -1
  1090. package/src/duckdb/src/planner/binder/tableref/bind_pivot.cpp +6 -0
  1091. package/src/duckdb/src/planner/binder/tableref/bind_showref.cpp +2 -0
  1092. package/src/duckdb/src/planner/binder/tableref/bind_table_function.cpp +106 -46
  1093. package/src/duckdb/src/planner/binder/tableref/plan_delimgetref.cpp +11 -0
  1094. package/src/duckdb/src/planner/binder/tableref/plan_joinref.cpp +15 -2
  1095. package/src/duckdb/src/planner/binder/tableref/plan_table_function.cpp +4 -0
  1096. package/src/duckdb/src/planner/binder.cpp +172 -15
  1097. package/src/duckdb/src/planner/collation_binding.cpp +99 -0
  1098. package/src/duckdb/src/planner/expression/bound_aggregate_expression.cpp +10 -4
  1099. package/src/duckdb/src/planner/expression/bound_between_expression.cpp +1 -1
  1100. package/src/duckdb/src/planner/expression/bound_case_expression.cpp +1 -1
  1101. package/src/duckdb/src/planner/expression/bound_cast_expression.cpp +14 -12
  1102. package/src/duckdb/src/planner/expression/bound_columnref_expression.cpp +1 -1
  1103. package/src/duckdb/src/planner/expression/bound_comparison_expression.cpp +1 -1
  1104. package/src/duckdb/src/planner/expression/bound_conjunction_expression.cpp +1 -1
  1105. package/src/duckdb/src/planner/expression/bound_constant_expression.cpp +1 -1
  1106. package/src/duckdb/src/planner/expression/bound_expanded_expression.cpp +1 -1
  1107. package/src/duckdb/src/planner/expression/bound_function_expression.cpp +8 -2
  1108. package/src/duckdb/src/planner/expression/bound_lambda_expression.cpp +1 -1
  1109. package/src/duckdb/src/planner/expression/bound_lambdaref_expression.cpp +1 -1
  1110. package/src/duckdb/src/planner/expression/bound_operator_expression.cpp +1 -1
  1111. package/src/duckdb/src/planner/expression/bound_parameter_expression.cpp +1 -1
  1112. package/src/duckdb/src/planner/expression/bound_reference_expression.cpp +1 -1
  1113. package/src/duckdb/src/planner/expression/bound_subquery_expression.cpp +1 -1
  1114. package/src/duckdb/src/planner/expression/bound_unnest_expression.cpp +1 -1
  1115. package/src/duckdb/src/planner/expression/bound_window_expression.cpp +6 -6
  1116. package/src/duckdb/src/planner/expression_binder/aggregate_binder.cpp +1 -1
  1117. package/src/duckdb/src/planner/expression_binder/alter_binder.cpp +2 -2
  1118. package/src/duckdb/src/planner/expression_binder/base_select_binder.cpp +1 -1
  1119. package/src/duckdb/src/planner/expression_binder/column_alias_binder.cpp +7 -0
  1120. package/src/duckdb/src/planner/expression_binder/constant_binder.cpp +3 -3
  1121. package/src/duckdb/src/planner/expression_binder/group_binder.cpp +26 -22
  1122. package/src/duckdb/src/planner/expression_binder/having_binder.cpp +7 -1
  1123. package/src/duckdb/src/planner/expression_binder/index_binder.cpp +2 -2
  1124. package/src/duckdb/src/planner/expression_binder/insert_binder.cpp +2 -2
  1125. package/src/duckdb/src/planner/expression_binder/lateral_binder.cpp +2 -2
  1126. package/src/duckdb/src/planner/expression_binder/order_binder.cpp +61 -43
  1127. package/src/duckdb/src/planner/expression_binder/qualify_binder.cpp +2 -2
  1128. package/src/duckdb/src/planner/expression_binder/relation_binder.cpp +4 -4
  1129. package/src/duckdb/src/planner/expression_binder/returning_binder.cpp +3 -2
  1130. package/src/duckdb/src/planner/expression_binder/table_function_binder.cpp +10 -3
  1131. package/src/duckdb/src/planner/expression_binder/update_binder.cpp +1 -1
  1132. package/src/duckdb/src/planner/expression_binder/where_binder.cpp +9 -2
  1133. package/src/duckdb/src/planner/expression_binder.cpp +121 -21
  1134. package/src/duckdb/src/planner/expression_iterator.cpp +26 -1
  1135. package/src/duckdb/src/planner/filter/conjunction_filter.cpp +33 -0
  1136. package/src/duckdb/src/planner/filter/constant_filter.cpp +15 -0
  1137. package/src/duckdb/src/planner/filter/null_filter.cpp +22 -0
  1138. package/src/duckdb/src/planner/filter/struct_filter.cpp +16 -0
  1139. package/src/duckdb/src/planner/logical_operator.cpp +24 -7
  1140. package/src/duckdb/src/planner/operator/logical_aggregate.cpp +13 -7
  1141. package/src/duckdb/src/planner/operator/logical_any_join.cpp +5 -2
  1142. package/src/duckdb/src/planner/operator/logical_comparison_join.cpp +13 -5
  1143. package/src/duckdb/src/planner/operator/logical_copy_to_file.cpp +64 -8
  1144. package/src/duckdb/src/planner/operator/logical_cteref.cpp +7 -0
  1145. package/src/duckdb/src/planner/operator/logical_distinct.cpp +6 -5
  1146. package/src/duckdb/src/planner/operator/logical_get.cpp +60 -18
  1147. package/src/duckdb/src/planner/operator/logical_materialized_cte.cpp +7 -0
  1148. package/src/duckdb/src/planner/operator/logical_order.cpp +7 -4
  1149. package/src/duckdb/src/planner/operator/logical_top_n.cpp +2 -2
  1150. package/src/duckdb/src/planner/operator/logical_vacuum.cpp +1 -1
  1151. package/src/duckdb/src/planner/planner.cpp +2 -3
  1152. package/src/duckdb/src/planner/subquery/flatten_dependent_join.cpp +27 -10
  1153. package/src/duckdb/src/planner/table_filter.cpp +51 -0
  1154. package/src/duckdb/src/storage/arena_allocator.cpp +28 -10
  1155. package/src/duckdb/src/storage/block.cpp +3 -2
  1156. package/src/duckdb/src/storage/buffer/block_handle.cpp +29 -14
  1157. package/src/duckdb/src/storage/buffer/block_manager.cpp +6 -5
  1158. package/src/duckdb/src/storage/buffer/buffer_handle.cpp +1 -1
  1159. package/src/duckdb/src/storage/buffer/buffer_pool.cpp +264 -125
  1160. package/src/duckdb/src/storage/buffer_manager.cpp +5 -1
  1161. package/src/duckdb/src/storage/checkpoint/row_group_writer.cpp +0 -6
  1162. package/src/duckdb/src/storage/checkpoint/table_data_writer.cpp +26 -3
  1163. package/src/duckdb/src/storage/checkpoint/write_overflow_strings_to_disk.cpp +21 -9
  1164. package/src/duckdb/src/storage/checkpoint_manager.cpp +49 -24
  1165. package/src/duckdb/src/storage/compression/alp/alp.cpp +6 -11
  1166. package/src/duckdb/src/storage/compression/alprd.cpp +5 -9
  1167. package/src/duckdb/src/storage/compression/bitpacking.cpp +35 -31
  1168. package/src/duckdb/src/storage/compression/chimp/chimp.cpp +6 -8
  1169. package/src/duckdb/src/storage/compression/dictionary_compression.cpp +71 -58
  1170. package/src/duckdb/src/storage/compression/fixed_size_uncompressed.cpp +15 -13
  1171. package/src/duckdb/src/storage/compression/fsst.cpp +66 -53
  1172. package/src/duckdb/src/storage/compression/numeric_constant.cpp +4 -5
  1173. package/src/duckdb/src/storage/compression/patas.cpp +6 -17
  1174. package/src/duckdb/src/storage/compression/rle.cpp +20 -18
  1175. package/src/duckdb/src/storage/compression/string_uncompressed.cpp +71 -52
  1176. package/src/duckdb/src/storage/compression/uncompressed.cpp +2 -2
  1177. package/src/duckdb/src/storage/compression/validity_uncompressed.cpp +8 -7
  1178. package/src/duckdb/src/storage/data_pointer.cpp +22 -0
  1179. package/src/duckdb/src/storage/data_table.cpp +41 -12
  1180. package/src/duckdb/src/storage/local_storage.cpp +22 -8
  1181. package/src/duckdb/src/storage/metadata/metadata_manager.cpp +33 -17
  1182. package/src/duckdb/src/storage/metadata/metadata_reader.cpp +4 -4
  1183. package/src/duckdb/src/storage/metadata/metadata_writer.cpp +3 -3
  1184. package/src/duckdb/src/storage/partial_block_manager.cpp +19 -8
  1185. package/src/duckdb/src/storage/serialization/serialize_create_info.cpp +11 -8
  1186. package/src/duckdb/src/storage/serialization/serialize_expression.cpp +1 -1
  1187. package/src/duckdb/src/storage/serialization/serialize_extension_install_info.cpp +2 -0
  1188. package/src/duckdb/src/storage/serialization/serialize_logical_operator.cpp +3 -3
  1189. package/src/duckdb/src/storage/serialization/serialize_nodes.cpp +19 -5
  1190. package/src/duckdb/src/storage/serialization/serialize_parse_info.cpp +21 -1
  1191. package/src/duckdb/src/storage/serialization/serialize_parsed_expression.cpp +4 -2
  1192. package/src/duckdb/src/storage/serialization/serialize_query_node.cpp +2 -2
  1193. package/src/duckdb/src/storage/serialization/serialize_storage.cpp +2 -0
  1194. package/src/duckdb/src/storage/serialization/serialize_tableref.cpp +8 -4
  1195. package/src/duckdb/src/storage/serialization/serialize_types.cpp +4 -4
  1196. package/src/duckdb/src/storage/single_file_block_manager.cpp +170 -34
  1197. package/src/duckdb/src/storage/standard_buffer_manager.cpp +221 -64
  1198. package/src/duckdb/src/storage/statistics/column_statistics.cpp +4 -3
  1199. package/src/duckdb/src/storage/statistics/distinct_statistics.cpp +36 -26
  1200. package/src/duckdb/src/storage/statistics/numeric_stats.cpp +4 -15
  1201. package/src/duckdb/src/storage/statistics/string_stats.cpp +14 -8
  1202. package/src/duckdb/src/storage/statistics/struct_stats.cpp +2 -1
  1203. package/src/duckdb/src/storage/storage_info.cpp +34 -9
  1204. package/src/duckdb/src/storage/storage_manager.cpp +147 -74
  1205. package/src/duckdb/src/storage/table/array_column_data.cpp +37 -17
  1206. package/src/duckdb/src/storage/table/chunk_info.cpp +38 -0
  1207. package/src/duckdb/src/storage/table/column_checkpoint_state.cpp +10 -6
  1208. package/src/duckdb/src/storage/table/column_data.cpp +252 -31
  1209. package/src/duckdb/src/storage/table/column_data_checkpointer.cpp +2 -12
  1210. package/src/duckdb/src/storage/table/column_segment.cpp +63 -34
  1211. package/src/duckdb/src/storage/table/list_column_data.cpp +34 -15
  1212. package/src/duckdb/src/storage/table/row_group.cpp +228 -120
  1213. package/src/duckdb/src/storage/table/row_group_collection.cpp +122 -120
  1214. package/src/duckdb/src/storage/table/row_version_manager.cpp +27 -1
  1215. package/src/duckdb/src/storage/table/scan_state.cpp +101 -18
  1216. package/src/duckdb/src/storage/table/standard_column_data.cpp +20 -34
  1217. package/src/duckdb/src/storage/table/struct_column_data.cpp +39 -42
  1218. package/src/duckdb/src/storage/table/table_statistics.cpp +2 -1
  1219. package/src/duckdb/src/storage/table/update_segment.cpp +9 -8
  1220. package/src/duckdb/src/storage/table/validity_column_data.cpp +2 -2
  1221. package/src/duckdb/src/storage/table_index_list.cpp +8 -7
  1222. package/src/duckdb/src/storage/temporary_file_manager.cpp +11 -9
  1223. package/src/duckdb/src/storage/temporary_memory_manager.cpp +227 -39
  1224. package/src/duckdb/src/storage/wal_replay.cpp +68 -28
  1225. package/src/duckdb/src/storage/write_ahead_log.cpp +56 -47
  1226. package/src/duckdb/src/transaction/cleanup_state.cpp +9 -1
  1227. package/src/duckdb/src/transaction/commit_state.cpp +7 -170
  1228. package/src/duckdb/src/transaction/duck_transaction.cpp +87 -19
  1229. package/src/duckdb/src/transaction/duck_transaction_manager.cpp +65 -10
  1230. package/src/duckdb/src/transaction/meta_transaction.cpp +18 -3
  1231. package/src/duckdb/src/transaction/transaction_context.cpp +21 -17
  1232. package/src/duckdb/src/transaction/undo_buffer.cpp +20 -14
  1233. package/src/duckdb/src/transaction/wal_write_state.cpp +292 -0
  1234. package/src/duckdb/src/verification/prepared_statement_verifier.cpp +0 -1
  1235. package/src/duckdb/third_party/brotli/common/brotli_constants.h +204 -0
  1236. package/src/duckdb/third_party/brotli/common/brotli_platform.h +543 -0
  1237. package/src/duckdb/third_party/brotli/common/constants.cpp +17 -0
  1238. package/src/duckdb/third_party/brotli/common/context.cpp +156 -0
  1239. package/src/duckdb/third_party/brotli/common/context.h +110 -0
  1240. package/src/duckdb/third_party/brotli/common/dictionary.cpp +5912 -0
  1241. package/src/duckdb/third_party/brotli/common/dictionary.h +60 -0
  1242. package/src/duckdb/third_party/brotli/common/platform.cpp +24 -0
  1243. package/src/duckdb/third_party/brotli/common/shared_dictionary.cpp +517 -0
  1244. package/src/duckdb/third_party/brotli/common/shared_dictionary_internal.h +71 -0
  1245. package/src/duckdb/third_party/brotli/common/transform.cpp +287 -0
  1246. package/src/duckdb/third_party/brotli/common/transform.h +77 -0
  1247. package/src/duckdb/third_party/brotli/common/version.h +51 -0
  1248. package/src/duckdb/third_party/brotli/dec/bit_reader.cpp +74 -0
  1249. package/src/duckdb/third_party/brotli/dec/bit_reader.h +419 -0
  1250. package/src/duckdb/third_party/brotli/dec/decode.cpp +2758 -0
  1251. package/src/duckdb/third_party/brotli/dec/huffman.cpp +338 -0
  1252. package/src/duckdb/third_party/brotli/dec/huffman.h +118 -0
  1253. package/src/duckdb/third_party/brotli/dec/prefix.h +733 -0
  1254. package/src/duckdb/third_party/brotli/dec/state.cpp +178 -0
  1255. package/src/duckdb/third_party/brotli/dec/state.h +386 -0
  1256. package/src/duckdb/third_party/brotli/enc/backward_references.cpp +3775 -0
  1257. package/src/duckdb/third_party/brotli/enc/backward_references.h +36 -0
  1258. package/src/duckdb/third_party/brotli/enc/backward_references_hq.cpp +935 -0
  1259. package/src/duckdb/third_party/brotli/enc/backward_references_hq.h +92 -0
  1260. package/src/duckdb/third_party/brotli/enc/bit_cost.cpp +410 -0
  1261. package/src/duckdb/third_party/brotli/enc/bit_cost.h +60 -0
  1262. package/src/duckdb/third_party/brotli/enc/block_splitter.cpp +1653 -0
  1263. package/src/duckdb/third_party/brotli/enc/block_splitter.h +48 -0
  1264. package/src/duckdb/third_party/brotli/enc/brotli_bit_stream.cpp +1431 -0
  1265. package/src/duckdb/third_party/brotli/enc/brotli_bit_stream.h +85 -0
  1266. package/src/duckdb/third_party/brotli/enc/brotli_hash.h +4352 -0
  1267. package/src/duckdb/third_party/brotli/enc/brotli_params.h +47 -0
  1268. package/src/duckdb/third_party/brotli/enc/cluster.cpp +1025 -0
  1269. package/src/duckdb/third_party/brotli/enc/cluster.h +1017 -0
  1270. package/src/duckdb/third_party/brotli/enc/command.cpp +24 -0
  1271. package/src/duckdb/third_party/brotli/enc/command.h +187 -0
  1272. package/src/duckdb/third_party/brotli/enc/compound_dictionary.cpp +209 -0
  1273. package/src/duckdb/third_party/brotli/enc/compound_dictionary.h +75 -0
  1274. package/src/duckdb/third_party/brotli/enc/compress_fragment.cpp +796 -0
  1275. package/src/duckdb/third_party/brotli/enc/compress_fragment.h +82 -0
  1276. package/src/duckdb/third_party/brotli/enc/compress_fragment_two_pass.cpp +653 -0
  1277. package/src/duckdb/third_party/brotli/enc/compress_fragment_two_pass.h +68 -0
  1278. package/src/duckdb/third_party/brotli/enc/dictionary_hash.cpp +1844 -0
  1279. package/src/duckdb/third_party/brotli/enc/dictionary_hash.h +21 -0
  1280. package/src/duckdb/third_party/brotli/enc/encode.cpp +1990 -0
  1281. package/src/duckdb/third_party/brotli/enc/encoder_dict.cpp +636 -0
  1282. package/src/duckdb/third_party/brotli/enc/encoder_dict.h +153 -0
  1283. package/src/duckdb/third_party/brotli/enc/entropy_encode.cpp +500 -0
  1284. package/src/duckdb/third_party/brotli/enc/entropy_encode.h +119 -0
  1285. package/src/duckdb/third_party/brotli/enc/entropy_encode_static.h +538 -0
  1286. package/src/duckdb/third_party/brotli/enc/fast_log.cpp +101 -0
  1287. package/src/duckdb/third_party/brotli/enc/fast_log.h +63 -0
  1288. package/src/duckdb/third_party/brotli/enc/find_match_length.h +68 -0
  1289. package/src/duckdb/third_party/brotli/enc/histogram.cpp +96 -0
  1290. package/src/duckdb/third_party/brotli/enc/histogram.h +210 -0
  1291. package/src/duckdb/third_party/brotli/enc/literal_cost.cpp +176 -0
  1292. package/src/duckdb/third_party/brotli/enc/literal_cost.h +28 -0
  1293. package/src/duckdb/third_party/brotli/enc/memory.cpp +190 -0
  1294. package/src/duckdb/third_party/brotli/enc/memory.h +127 -0
  1295. package/src/duckdb/third_party/brotli/enc/metablock.cpp +1225 -0
  1296. package/src/duckdb/third_party/brotli/enc/metablock.h +102 -0
  1297. package/src/duckdb/third_party/brotli/enc/prefix.h +50 -0
  1298. package/src/duckdb/third_party/brotli/enc/quality.h +202 -0
  1299. package/src/duckdb/third_party/brotli/enc/ringbuffer.h +164 -0
  1300. package/src/duckdb/third_party/brotli/enc/state.h +106 -0
  1301. package/src/duckdb/third_party/brotli/enc/static_dict.cpp +538 -0
  1302. package/src/duckdb/third_party/brotli/enc/static_dict.h +37 -0
  1303. package/src/duckdb/third_party/brotli/enc/static_dict_lut.h +5862 -0
  1304. package/src/duckdb/third_party/brotli/enc/utf8_util.cpp +81 -0
  1305. package/src/duckdb/third_party/brotli/enc/utf8_util.h +29 -0
  1306. package/src/duckdb/third_party/brotli/enc/write_bits.h +84 -0
  1307. package/src/duckdb/third_party/brotli/include/brotli/decode.h +405 -0
  1308. package/src/duckdb/third_party/brotli/include/brotli/encode.h +489 -0
  1309. package/src/duckdb/third_party/brotli/include/brotli/port.h +238 -0
  1310. package/src/duckdb/third_party/brotli/include/brotli/shared_dictionary.h +96 -0
  1311. package/src/duckdb/third_party/brotli/include/brotli/types.h +83 -0
  1312. package/src/duckdb/third_party/fast_float/fast_float/fast_float.h +20 -4
  1313. package/src/duckdb/third_party/fmt/include/fmt/format.h +54 -10
  1314. package/src/duckdb/third_party/fsst/fsst.h +2 -2
  1315. package/src/duckdb/third_party/fsst/libfsst.hpp +2 -2
  1316. package/src/duckdb/third_party/httplib/httplib.hpp +6763 -5580
  1317. package/src/duckdb/third_party/hyperloglog/hyperloglog.cpp +13 -30
  1318. package/src/duckdb/third_party/hyperloglog/hyperloglog.hpp +8 -2
  1319. package/src/duckdb/third_party/libpg_query/include/nodes/nodes.hpp +1 -0
  1320. package/src/duckdb/third_party/libpg_query/include/nodes/parsenodes.hpp +22 -9
  1321. package/src/duckdb/third_party/libpg_query/include/parser/gram.hpp +1041 -554
  1322. package/src/duckdb/third_party/libpg_query/include/parser/kwlist.hpp +1 -0
  1323. package/src/duckdb/third_party/libpg_query/postgres_parser.cpp +2 -1
  1324. package/src/duckdb/third_party/libpg_query/src_backend_parser_gram.cpp +21605 -21752
  1325. package/src/duckdb/third_party/libpg_query/src_backend_parser_scan.cpp +538 -299
  1326. package/src/duckdb/third_party/mbedtls/include/mbedtls/mbedtls_config.h +1 -0
  1327. package/src/duckdb/third_party/mbedtls/include/mbedtls_wrapper.hpp +36 -12
  1328. package/src/duckdb/third_party/mbedtls/library/md.cpp +6 -6
  1329. package/src/duckdb/third_party/mbedtls/library/sha1.cpp +2 -0
  1330. package/src/duckdb/third_party/mbedtls/library/sha256.cpp +3 -0
  1331. package/src/duckdb/third_party/mbedtls/mbedtls_wrapper.cpp +99 -47
  1332. package/src/duckdb/third_party/pcg/pcg_extras.hpp +1 -1
  1333. package/src/duckdb/third_party/re2/re2/prog.cc +2 -2
  1334. package/src/duckdb/third_party/snappy/snappy-internal.h +398 -0
  1335. package/src/duckdb/third_party/snappy/snappy-sinksource.cc +111 -9
  1336. package/src/duckdb/third_party/snappy/snappy-sinksource.h +158 -0
  1337. package/src/duckdb/third_party/snappy/snappy-stubs-internal.h +523 -3
  1338. package/src/duckdb/third_party/snappy/snappy-stubs-public.h +34 -1
  1339. package/src/duckdb/third_party/snappy/snappy.cc +2626 -0
  1340. package/src/duckdb/third_party/snappy/snappy.h +223 -0
  1341. package/src/duckdb/third_party/snappy/snappy_version.hpp +11 -0
  1342. package/src/duckdb/third_party/utf8proc/include/utf8proc.hpp +69 -101
  1343. package/src/duckdb/third_party/utf8proc/include/utf8proc_wrapper.hpp +53 -0
  1344. package/src/duckdb/third_party/utf8proc/utf8proc.cpp +627 -678
  1345. package/src/duckdb/third_party/utf8proc/utf8proc_data.cpp +15008 -12868
  1346. package/src/duckdb/third_party/utf8proc/utf8proc_wrapper.cpp +185 -29
  1347. package/src/duckdb/ub_extension_json_json_functions.cpp +6 -0
  1348. package/src/duckdb/ub_src_catalog_default.cpp +4 -0
  1349. package/src/duckdb/ub_src_common.cpp +7 -1
  1350. package/src/duckdb/ub_src_common_arrow.cpp +10 -0
  1351. package/src/duckdb/ub_src_common_enums.cpp +2 -0
  1352. package/src/duckdb/ub_src_common_tree_renderer.cpp +10 -0
  1353. package/src/duckdb/ub_src_common_types.cpp +2 -0
  1354. package/src/duckdb/ub_src_core_functions_aggregate_holistic.cpp +4 -0
  1355. package/src/duckdb/ub_src_core_functions_aggregate_nested.cpp +2 -0
  1356. package/src/duckdb/ub_src_core_functions_scalar_generic.cpp +2 -0
  1357. package/src/duckdb/ub_src_core_functions_scalar_list.cpp +2 -4
  1358. package/src/duckdb/ub_src_core_functions_scalar_map.cpp +2 -0
  1359. package/src/duckdb/ub_src_core_functions_scalar_string.cpp +4 -0
  1360. package/src/duckdb/ub_src_execution_index_art.cpp +5 -3
  1361. package/src/duckdb/ub_src_execution_operator_csv_scanner_scanner.cpp +2 -0
  1362. package/src/duckdb/ub_src_execution_operator_helper.cpp +4 -0
  1363. package/src/duckdb/ub_src_function.cpp +4 -0
  1364. package/src/duckdb/ub_src_function_cast.cpp +2 -0
  1365. package/src/duckdb/ub_src_function_scalar_generic.cpp +4 -0
  1366. package/src/duckdb/ub_src_function_scalar_list.cpp +0 -2
  1367. package/src/duckdb/ub_src_function_scalar_string.cpp +2 -0
  1368. package/src/duckdb/ub_src_function_table.cpp +2 -0
  1369. package/src/duckdb/ub_src_function_table_arrow.cpp +2 -0
  1370. package/src/duckdb/ub_src_function_table_system.cpp +2 -0
  1371. package/src/duckdb/ub_src_main.cpp +4 -0
  1372. package/src/duckdb/ub_src_main_buffered_data.cpp +4 -0
  1373. package/src/duckdb/ub_src_main_capi.cpp +10 -0
  1374. package/src/duckdb/ub_src_main_chunk_scan_state.cpp +2 -0
  1375. package/src/duckdb/ub_src_main_relation.cpp +2 -0
  1376. package/src/duckdb/ub_src_main_secret.cpp +2 -0
  1377. package/src/duckdb/ub_src_optimizer.cpp +8 -0
  1378. package/src/duckdb/ub_src_optimizer_compressed_materialization.cpp +2 -0
  1379. package/src/duckdb/ub_src_optimizer_pushdown.cpp +2 -0
  1380. package/src/duckdb/ub_src_optimizer_rule.cpp +2 -0
  1381. package/src/duckdb/ub_src_parallel.cpp +4 -0
  1382. package/src/duckdb/ub_src_parser_tableref.cpp +2 -0
  1383. package/src/duckdb/ub_src_planner.cpp +2 -0
  1384. package/src/duckdb/ub_src_planner_binder_expression.cpp +2 -0
  1385. package/src/duckdb/ub_src_planner_binder_tableref.cpp +4 -0
  1386. package/src/duckdb/ub_src_storage_statistics.cpp +0 -2
  1387. package/src/duckdb/ub_src_transaction.cpp +2 -0
  1388. package/test/columns.test.ts +1 -1
  1389. package/test/prepare.test.ts +1 -1
  1390. package/test/test_all_types.test.ts +1 -1
@@ -26,6 +26,2630 @@
26
26
  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
27
  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
28
 
29
+ #include "snappy_version.hpp"
30
+
31
+ #if SNAPPY_NEW_VERSION
32
+
33
+ #include "snappy-internal.h"
34
+ #include "snappy-sinksource.h"
35
+ #include "snappy.h"
36
+ #if !defined(SNAPPY_HAVE_BMI2)
37
+ // __BMI2__ is defined by GCC and Clang. Visual Studio doesn't target BMI2
38
+ // specifically, but it does define __AVX2__ when AVX2 support is available.
39
+ // Fortunately, AVX2 was introduced in Haswell, just like BMI2.
40
+ //
41
+ // BMI2 is not defined as a subset of AVX2 (unlike SSSE3 and AVX above). So,
42
+ // GCC and Clang can build code with AVX2 enabled but BMI2 disabled, in which
43
+ // case issuing BMI2 instructions results in a compiler error.
44
+ #if defined(__BMI2__) || (defined(_MSC_VER) && defined(__AVX2__))
45
+ #define SNAPPY_HAVE_BMI2 1
46
+ #else
47
+ #define SNAPPY_HAVE_BMI2 0
48
+ #endif
49
+ #endif // !defined(SNAPPY_HAVE_BMI2)
50
+
51
+ #if !defined(SNAPPY_HAVE_X86_CRC32)
52
+ #if defined(__SSE4_2__)
53
+ #define SNAPPY_HAVE_X86_CRC32 1
54
+ #else
55
+ #define SNAPPY_HAVE_X86_CRC32 0
56
+ #endif
57
+ #endif // !defined(SNAPPY_HAVE_X86_CRC32)
58
+
59
+ #if !defined(SNAPPY_HAVE_NEON_CRC32)
60
+ #if SNAPPY_HAVE_NEON && defined(__ARM_FEATURE_CRC32)
61
+ #define SNAPPY_HAVE_NEON_CRC32 1
62
+ #else
63
+ #define SNAPPY_HAVE_NEON_CRC32 0
64
+ #endif
65
+ #endif // !defined(SNAPPY_HAVE_NEON_CRC32)
66
+
67
+ #if SNAPPY_HAVE_BMI2 || SNAPPY_HAVE_X86_CRC32
68
+ // Please do not replace with <x86intrin.h>. or with headers that assume more
69
+ // advanced SSE versions without checking with all the OWNERS.
70
+ #include <immintrin.h>
71
+ #elif SNAPPY_HAVE_NEON_CRC32
72
+ #include <arm_acle.h>
73
+ #endif
74
+
75
+ #include <algorithm>
76
+ #include <array>
77
+ #include <cstddef>
78
+ #include <cstdint>
79
+ #include <cstdio>
80
+ #include <cstring>
81
+ #include <memory>
82
+ #include <string>
83
+ #include <utility>
84
+ #include <vector>
85
+
86
+ namespace duckdb_snappy {
87
+
88
+ namespace {
89
+
90
+ // The amount of slop bytes writers are using for unconditional copies.
91
+ constexpr int kSlopBytes = 64;
92
+
93
+ using internal::char_table;
94
+ using internal::COPY_1_BYTE_OFFSET;
95
+ using internal::COPY_2_BYTE_OFFSET;
96
+ using internal::COPY_4_BYTE_OFFSET;
97
+ using internal::kMaximumTagLength;
98
+ using internal::LITERAL;
99
+ #if SNAPPY_HAVE_VECTOR_BYTE_SHUFFLE
100
+ using internal::V128;
101
+ using internal::V128_Load;
102
+ using internal::V128_LoadU;
103
+ using internal::V128_Shuffle;
104
+ using internal::V128_StoreU;
105
+ using internal::V128_DupChar;
106
+ #endif
107
+
108
+ // We translate the information encoded in a tag through a lookup table to a
109
+ // format that requires fewer instructions to decode. Effectively we store
110
+ // the length minus the tag part of the offset. The lowest significant byte
111
+ // thus stores the length. While total length - offset is given by
112
+ // entry - ExtractOffset(type). The nice thing is that the subtraction
113
+ // immediately sets the flags for the necessary check that offset >= length.
114
+ // This folds the cmp with sub. We engineer the long literals and copy-4 to
115
+ // always fail this check, so their presence doesn't affect the fast path.
116
+ // To prevent literals from triggering the guard against offset < length (offset
117
+ // does not apply to literals) the table is giving them a spurious offset of
118
+ // 256.
119
+ inline constexpr int16_t MakeEntry(int16_t len, int16_t offset) {
120
+ return len - (offset << 8);
121
+ }
122
+
123
+ inline constexpr int16_t LengthMinusOffset(int data, int type) {
124
+ return type == 3 ? 0xFF // copy-4 (or type == 3)
125
+ : type == 2 ? MakeEntry(data + 1, 0) // copy-2
126
+ : type == 1 ? MakeEntry((data & 7) + 4, data >> 3) // copy-1
127
+ : data < 60 ? MakeEntry(data + 1, 1) // note spurious offset.
128
+ : 0xFF; // long literal
129
+ }
130
+
131
+ inline constexpr int16_t LengthMinusOffset(uint8_t tag) {
132
+ return LengthMinusOffset(tag >> 2, tag & 3);
133
+ }
134
+
135
+ template <size_t... Ints>
136
+ struct index_sequence {};
137
+
138
+ template <std::size_t N, size_t... Is>
139
+ struct make_index_sequence : make_index_sequence<N - 1, N - 1, Is...> {};
140
+
141
+ template <size_t... Is>
142
+ struct make_index_sequence<0, Is...> : index_sequence<Is...> {};
143
+
144
+ template <size_t... seq>
145
+ constexpr std::array<int16_t, 256> MakeTable(index_sequence<seq...>) {
146
+ return std::array<int16_t, 256>{LengthMinusOffset(seq)...};
147
+ }
148
+
149
+ alignas(64) const std::array<int16_t, 256> kLengthMinusOffset =
150
+ MakeTable(make_index_sequence<256>{});
151
+
152
+ // Given a table of uint16_t whose size is mask / 2 + 1, return a pointer to the
153
+ // relevant entry, if any, for the given bytes. Any hash function will do,
154
+ // but a good hash function reduces the number of collisions and thus yields
155
+ // better compression for compressible input.
156
+ //
157
+ // REQUIRES: mask is 2 * (table_size - 1), and table_size is a power of two.
158
+ inline uint16_t* TableEntry(uint16_t* table, uint32_t bytes, uint32_t mask) {
159
+ // Our choice is quicker-and-dirtier than the typical hash function;
160
+ // empirically, that seems beneficial. The upper bits of kMagic * bytes are a
161
+ // higher-quality hash than the lower bits, so when using kMagic * bytes we
162
+ // also shift right to get a higher-quality end result. There's no similar
163
+ // issue with a CRC because all of the output bits of a CRC are equally good
164
+ // "hashes." So, a CPU instruction for CRC, if available, tends to be a good
165
+ // choice.
166
+ #if SNAPPY_HAVE_NEON_CRC32
167
+ // We use mask as the second arg to the CRC function, as it's about to
168
+ // be used anyway; it'd be equally correct to use 0 or some constant.
169
+ // Mathematically, _mm_crc32_u32 (or similar) is a function of the
170
+ // xor of its arguments.
171
+ const uint32_t hash = __crc32cw(bytes, mask);
172
+ #elif SNAPPY_HAVE_X86_CRC32
173
+ const uint32_t hash = _mm_crc32_u32(bytes, mask);
174
+ #else
175
+ constexpr uint32_t kMagic = 0x1e35a7bd;
176
+ const uint32_t hash = (kMagic * bytes) >> (31 - kMaxHashTableBits);
177
+ #endif
178
+ return reinterpret_cast<uint16_t*>(reinterpret_cast<uintptr_t>(table) +
179
+ (hash & mask));
180
+ }
181
+
182
+ inline uint16_t* TableEntry4ByteMatch(uint16_t* table, uint32_t bytes,
183
+ uint32_t mask) {
184
+ constexpr uint32_t kMagic = 2654435761U;
185
+ const uint32_t hash = (kMagic * bytes) >> (32 - kMaxHashTableBits);
186
+ return reinterpret_cast<uint16_t*>(reinterpret_cast<uintptr_t>(table) +
187
+ (hash & mask));
188
+ }
189
+
190
+ inline uint16_t* TableEntry8ByteMatch(uint16_t* table, uint64_t bytes,
191
+ uint32_t mask) {
192
+ constexpr uint64_t kMagic = 58295818150454627ULL;
193
+ const uint32_t hash = (kMagic * bytes) >> (64 - kMaxHashTableBits);
194
+ return reinterpret_cast<uint16_t*>(reinterpret_cast<uintptr_t>(table) +
195
+ (hash & mask));
196
+ }
197
+
198
+ } // namespace
199
+
200
+ size_t MaxCompressedLength(size_t source_bytes) {
201
+ // Compressed data can be defined as:
202
+ // compressed := item* literal*
203
+ // item := literal* copy
204
+ //
205
+ // The trailing literal sequence has a space blowup of at most 62/60
206
+ // since a literal of length 60 needs one tag byte + one extra byte
207
+ // for length information.
208
+ //
209
+ // Item blowup is trickier to measure. Suppose the "copy" op copies
210
+ // 4 bytes of data. Because of a special check in the encoding code,
211
+ // we produce a 4-byte copy only if the offset is < 65536. Therefore
212
+ // the copy op takes 3 bytes to encode, and this type of item leads
213
+ // to at most the 62/60 blowup for representing literals.
214
+ //
215
+ // Suppose the "copy" op copies 5 bytes of data. If the offset is big
216
+ // enough, it will take 5 bytes to encode the copy op. Therefore the
217
+ // worst case here is a one-byte literal followed by a five-byte copy.
218
+ // I.e., 6 bytes of input turn into 7 bytes of "compressed" data.
219
+ //
220
+ // This last factor dominates the blowup, so the final estimate is:
221
+ return 32 + source_bytes + source_bytes / 6;
222
+ }
223
+
224
+ namespace {
225
+
226
+ void UnalignedCopy64(const void* src, void* dst) {
227
+ char tmp[8];
228
+ std::memcpy(tmp, src, 8);
229
+ std::memcpy(dst, tmp, 8);
230
+ }
231
+
232
+ void UnalignedCopy128(const void* src, void* dst) {
233
+ // std::memcpy() gets vectorized when the appropriate compiler options are
234
+ // used. For example, x86 compilers targeting SSE2+ will optimize to an SSE2
235
+ // load and store.
236
+ char tmp[16];
237
+ std::memcpy(tmp, src, 16);
238
+ std::memcpy(dst, tmp, 16);
239
+ }
240
+
241
+ template <bool use_16bytes_chunk>
242
+ inline void ConditionalUnalignedCopy128(const char* src, char* dst) {
243
+ if (use_16bytes_chunk) {
244
+ UnalignedCopy128(src, dst);
245
+ } else {
246
+ UnalignedCopy64(src, dst);
247
+ UnalignedCopy64(src + 8, dst + 8);
248
+ }
249
+ }
250
+
251
+ // Copy [src, src+(op_limit-op)) to [op, (op_limit-op)) a byte at a time. Used
252
+ // for handling COPY operations where the input and output regions may overlap.
253
+ // For example, suppose:
254
+ // src == "ab"
255
+ // op == src + 2
256
+ // op_limit == op + 20
257
+ // After IncrementalCopySlow(src, op, op_limit), the result will have eleven
258
+ // copies of "ab"
259
+ // ababababababababababab
260
+ // Note that this does not match the semantics of either std::memcpy() or
261
+ // std::memmove().
262
+ inline char* IncrementalCopySlow(const char* src, char* op,
263
+ char* const op_limit) {
264
+ // TODO: Remove pragma when LLVM is aware this
265
+ // function is only called in cold regions and when cold regions don't get
266
+ // vectorized or unrolled.
267
+ #ifdef __clang__
268
+ #pragma clang loop unroll(disable)
269
+ #endif
270
+ while (op < op_limit) {
271
+ *op++ = *src++;
272
+ }
273
+ return op_limit;
274
+ }
275
+
276
+ #if SNAPPY_HAVE_VECTOR_BYTE_SHUFFLE
277
+
278
+ // Computes the bytes for shuffle control mask (please read comments on
279
+ // 'pattern_generation_masks' as well) for the given index_offset and
280
+ // pattern_size. For example, when the 'offset' is 6, it will generate a
281
+ // repeating pattern of size 6. So, the first 16 byte indexes will correspond to
282
+ // the pattern-bytes {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3} and the
283
+ // next 16 byte indexes will correspond to the pattern-bytes {4, 5, 0, 1, 2, 3,
284
+ // 4, 5, 0, 1, 2, 3, 4, 5, 0, 1}. These byte index sequences are generated by
285
+ // calling MakePatternMaskBytes(0, 6, index_sequence<16>()) and
286
+ // MakePatternMaskBytes(16, 6, index_sequence<16>()) respectively.
287
+ template <size_t... indexes>
288
+ inline constexpr std::array<char, sizeof...(indexes)> MakePatternMaskBytes(
289
+ int index_offset, int pattern_size, index_sequence<indexes...>) {
290
+ return {static_cast<char>((index_offset + indexes) % pattern_size)...};
291
+ }
292
+
293
+ // Computes the shuffle control mask bytes array for given pattern-sizes and
294
+ // returns an array.
295
+ template <size_t... pattern_sizes_minus_one>
296
+ inline constexpr std::array<std::array<char, sizeof(V128)>,
297
+ sizeof...(pattern_sizes_minus_one)>
298
+ MakePatternMaskBytesTable(int index_offset,
299
+ index_sequence<pattern_sizes_minus_one...>) {
300
+ return {
301
+ MakePatternMaskBytes(index_offset, pattern_sizes_minus_one + 1,
302
+ make_index_sequence</*indexes=*/sizeof(V128)>())...};
303
+ }
304
+
305
+ // This is an array of shuffle control masks that can be used as the source
306
+ // operand for PSHUFB to permute the contents of the destination XMM register
307
+ // into a repeating byte pattern.
308
+ alignas(16) constexpr std::array<std::array<char, sizeof(V128)>,
309
+ 16> pattern_generation_masks =
310
+ MakePatternMaskBytesTable(
311
+ /*index_offset=*/0,
312
+ /*pattern_sizes_minus_one=*/make_index_sequence<16>());
313
+
314
+ // Similar to 'pattern_generation_masks', this table is used to "rotate" the
315
+ // pattern so that we can copy the *next 16 bytes* consistent with the pattern.
316
+ // Basically, pattern_reshuffle_masks is a continuation of
317
+ // pattern_generation_masks. It follows that, pattern_reshuffle_masks is same as
318
+ // pattern_generation_masks for offsets 1, 2, 4, 8 and 16.
319
+ alignas(16) constexpr std::array<std::array<char, sizeof(V128)>,
320
+ 16> pattern_reshuffle_masks =
321
+ MakePatternMaskBytesTable(
322
+ /*index_offset=*/16,
323
+ /*pattern_sizes_minus_one=*/make_index_sequence<16>());
324
+
325
+ SNAPPY_ATTRIBUTE_ALWAYS_INLINE
326
+ static inline V128 LoadPattern(const char* src, const size_t pattern_size) {
327
+ V128 generation_mask = V128_Load(reinterpret_cast<const V128*>(
328
+ pattern_generation_masks[pattern_size - 1].data()));
329
+ // Uninitialized bytes are masked out by the shuffle mask.
330
+ // TODO: remove annotation and macro defs once MSan is fixed.
331
+ SNAPPY_ANNOTATE_MEMORY_IS_INITIALIZED(src + pattern_size, 16 - pattern_size);
332
+ return V128_Shuffle(V128_LoadU(reinterpret_cast<const V128*>(src)),
333
+ generation_mask);
334
+ }
335
+
336
+ SNAPPY_ATTRIBUTE_ALWAYS_INLINE
337
+ static inline std::pair<V128 /* pattern */, V128 /* reshuffle_mask */>
338
+ LoadPatternAndReshuffleMask(const char* src, const size_t pattern_size) {
339
+ V128 pattern = LoadPattern(src, pattern_size);
340
+
341
+ // This mask will generate the next 16 bytes in-place. Doing so enables us to
342
+ // write data by at most 4 V128_StoreU.
343
+ //
344
+ // For example, suppose pattern is: abcdefabcdefabcd
345
+ // Shuffling with this mask will generate: efabcdefabcdefab
346
+ // Shuffling again will generate: cdefabcdefabcdef
347
+ V128 reshuffle_mask = V128_Load(reinterpret_cast<const V128*>(
348
+ pattern_reshuffle_masks[pattern_size - 1].data()));
349
+ return {pattern, reshuffle_mask};
350
+ }
351
+
352
+ #endif // SNAPPY_HAVE_VECTOR_BYTE_SHUFFLE
353
+
354
+ // Fallback for when we need to copy while extending the pattern, for example
355
+ // copying 10 bytes from 3 positions back abc -> abcabcabcabca.
356
+ //
357
+ // REQUIRES: [dst - offset, dst + 64) is a valid address range.
358
+ SNAPPY_ATTRIBUTE_ALWAYS_INLINE
359
+ static inline bool Copy64BytesWithPatternExtension(char* dst, size_t offset) {
360
+ #if SNAPPY_HAVE_VECTOR_BYTE_SHUFFLE
361
+ if (SNAPPY_PREDICT_TRUE(offset <= 16)) {
362
+ switch (offset) {
363
+ case 0:
364
+ return false;
365
+ case 1: {
366
+ // TODO: Ideally we should memset, move back once the
367
+ // codegen issues are fixed.
368
+ V128 pattern = V128_DupChar(dst[-1]);
369
+ for (int i = 0; i < 4; i++) {
370
+ V128_StoreU(reinterpret_cast<V128*>(dst + 16 * i), pattern);
371
+ }
372
+ return true;
373
+ }
374
+ case 2:
375
+ case 4:
376
+ case 8:
377
+ case 16: {
378
+ V128 pattern = LoadPattern(dst - offset, offset);
379
+ for (int i = 0; i < 4; i++) {
380
+ V128_StoreU(reinterpret_cast<V128*>(dst + 16 * i), pattern);
381
+ }
382
+ return true;
383
+ }
384
+ default: {
385
+ auto pattern_and_reshuffle_mask =
386
+ LoadPatternAndReshuffleMask(dst - offset, offset);
387
+ V128 pattern = pattern_and_reshuffle_mask.first;
388
+ V128 reshuffle_mask = pattern_and_reshuffle_mask.second;
389
+ for (int i = 0; i < 4; i++) {
390
+ V128_StoreU(reinterpret_cast<V128*>(dst + 16 * i), pattern);
391
+ pattern = V128_Shuffle(pattern, reshuffle_mask);
392
+ }
393
+ return true;
394
+ }
395
+ }
396
+ }
397
+ #else
398
+ if (SNAPPY_PREDICT_TRUE(offset < 16)) {
399
+ if (SNAPPY_PREDICT_FALSE(offset == 0)) return false;
400
+ // Extend the pattern to the first 16 bytes.
401
+ // The simpler formulation of `dst[i - offset]` induces undefined behavior.
402
+ for (int i = 0; i < 16; i++) dst[i] = (dst - offset)[i];
403
+ // Find a multiple of pattern >= 16.
404
+ static std::array<uint8_t, 16> pattern_sizes = []() {
405
+ std::array<uint8_t, 16> res;
406
+ for (int i = 1; i < 16; i++) res[i] = (16 / i + 1) * i;
407
+ return res;
408
+ }();
409
+ offset = pattern_sizes[offset];
410
+ for (int i = 1; i < 4; i++) {
411
+ std::memcpy(dst + i * 16, dst + i * 16 - offset, 16);
412
+ }
413
+ return true;
414
+ }
415
+ #endif // SNAPPY_HAVE_VECTOR_BYTE_SHUFFLE
416
+
417
+ // Very rare.
418
+ for (int i = 0; i < 4; i++) {
419
+ std::memcpy(dst + i * 16, dst + i * 16 - offset, 16);
420
+ }
421
+ return true;
422
+ }
423
+
424
+ // Copy [src, src+(op_limit-op)) to [op, op_limit) but faster than
425
+ // IncrementalCopySlow. buf_limit is the address past the end of the writable
426
+ // region of the buffer.
427
+ inline char* IncrementalCopy(const char* src, char* op, char* const op_limit,
428
+ char* const buf_limit) {
429
+ #if SNAPPY_HAVE_VECTOR_BYTE_SHUFFLE
430
+ constexpr int big_pattern_size_lower_bound = 16;
431
+ #else
432
+ constexpr int big_pattern_size_lower_bound = 8;
433
+ #endif
434
+
435
+ // Terminology:
436
+ //
437
+ // slop = buf_limit - op
438
+ // pat = op - src
439
+ // len = op_limit - op
440
+ assert(src < op);
441
+ assert(op < op_limit);
442
+ assert(op_limit <= buf_limit);
443
+ // NOTE: The copy tags use 3 or 6 bits to store the copy length, so len <= 64.
444
+ assert(op_limit - op <= 64);
445
+ // NOTE: In practice the compressor always emits len >= 4, so it is ok to
446
+ // assume that to optimize this function, but this is not guaranteed by the
447
+ // compression format, so we have to also handle len < 4 in case the input
448
+ // does not satisfy these conditions.
449
+
450
+ size_t pattern_size = op - src;
451
+ // The cases are split into different branches to allow the branch predictor,
452
+ // FDO, and static prediction hints to work better. For each input we list the
453
+ // ratio of invocations that match each condition.
454
+ //
455
+ // input slop < 16 pat < 8 len > 16
456
+ // ------------------------------------------
457
+ // html|html4|cp 0% 1.01% 27.73%
458
+ // urls 0% 0.88% 14.79%
459
+ // jpg 0% 64.29% 7.14%
460
+ // pdf 0% 2.56% 58.06%
461
+ // txt[1-4] 0% 0.23% 0.97%
462
+ // pb 0% 0.96% 13.88%
463
+ // bin 0.01% 22.27% 41.17%
464
+ //
465
+ // It is very rare that we don't have enough slop for doing block copies. It
466
+ // is also rare that we need to expand a pattern. Small patterns are common
467
+ // for incompressible formats and for those we are plenty fast already.
468
+ // Lengths are normally not greater than 16 but they vary depending on the
469
+ // input. In general if we always predict len <= 16 it would be an ok
470
+ // prediction.
471
+ //
472
+ // In order to be fast we want a pattern >= 16 bytes (or 8 bytes in non-SSE)
473
+ // and an unrolled loop copying 1x 16 bytes (or 2x 8 bytes in non-SSE) at a
474
+ // time.
475
+
476
+ // Handle the uncommon case where pattern is less than 16 (or 8 in non-SSE)
477
+ // bytes.
478
+ if (pattern_size < big_pattern_size_lower_bound) {
479
+ #if SNAPPY_HAVE_VECTOR_BYTE_SHUFFLE
480
+ // Load the first eight bytes into an 128-bit XMM register, then use PSHUFB
481
+ // to permute the register's contents in-place into a repeating sequence of
482
+ // the first "pattern_size" bytes.
483
+ // For example, suppose:
484
+ // src == "abc"
485
+ // op == op + 3
486
+ // After V128_Shuffle(), "pattern" will have five copies of "abc"
487
+ // followed by one byte of slop: abcabcabcabcabca.
488
+ //
489
+ // The non-SSE fallback implementation suffers from store-forwarding stalls
490
+ // because its loads and stores partly overlap. By expanding the pattern
491
+ // in-place, we avoid the penalty.
492
+
493
+ // Typically, the op_limit is the gating factor so try to simplify the loop
494
+ // based on that.
495
+ if (SNAPPY_PREDICT_TRUE(op_limit <= buf_limit - 15)) {
496
+ auto pattern_and_reshuffle_mask =
497
+ LoadPatternAndReshuffleMask(src, pattern_size);
498
+ V128 pattern = pattern_and_reshuffle_mask.first;
499
+ V128 reshuffle_mask = pattern_and_reshuffle_mask.second;
500
+
501
+ // There is at least one, and at most four 16-byte blocks. Writing four
502
+ // conditionals instead of a loop allows FDO to layout the code with
503
+ // respect to the actual probabilities of each length.
504
+ // TODO: Replace with loop with trip count hint.
505
+ V128_StoreU(reinterpret_cast<V128*>(op), pattern);
506
+
507
+ if (op + 16 < op_limit) {
508
+ pattern = V128_Shuffle(pattern, reshuffle_mask);
509
+ V128_StoreU(reinterpret_cast<V128*>(op + 16), pattern);
510
+ }
511
+ if (op + 32 < op_limit) {
512
+ pattern = V128_Shuffle(pattern, reshuffle_mask);
513
+ V128_StoreU(reinterpret_cast<V128*>(op + 32), pattern);
514
+ }
515
+ if (op + 48 < op_limit) {
516
+ pattern = V128_Shuffle(pattern, reshuffle_mask);
517
+ V128_StoreU(reinterpret_cast<V128*>(op + 48), pattern);
518
+ }
519
+ return op_limit;
520
+ }
521
+ char* const op_end = buf_limit - 15;
522
+ if (SNAPPY_PREDICT_TRUE(op < op_end)) {
523
+ auto pattern_and_reshuffle_mask =
524
+ LoadPatternAndReshuffleMask(src, pattern_size);
525
+ V128 pattern = pattern_and_reshuffle_mask.first;
526
+ V128 reshuffle_mask = pattern_and_reshuffle_mask.second;
527
+
528
+ // This code path is relatively cold however so we save code size
529
+ // by avoiding unrolling and vectorizing.
530
+ //
531
+ // TODO: Remove pragma when when cold regions don't get
532
+ // vectorized or unrolled.
533
+ #ifdef __clang__
534
+ #pragma clang loop unroll(disable)
535
+ #endif
536
+ do {
537
+ V128_StoreU(reinterpret_cast<V128*>(op), pattern);
538
+ pattern = V128_Shuffle(pattern, reshuffle_mask);
539
+ op += 16;
540
+ } while (SNAPPY_PREDICT_TRUE(op < op_end));
541
+ }
542
+ return IncrementalCopySlow(op - pattern_size, op, op_limit);
543
+ #else // !SNAPPY_HAVE_VECTOR_BYTE_SHUFFLE
544
+ // If plenty of buffer space remains, expand the pattern to at least 8
545
+ // bytes. The way the following loop is written, we need 8 bytes of buffer
546
+ // space if pattern_size >= 4, 11 bytes if pattern_size is 1 or 3, and 10
547
+ // bytes if pattern_size is 2. Precisely encoding that is probably not
548
+ // worthwhile; instead, invoke the slow path if we cannot write 11 bytes
549
+ // (because 11 are required in the worst case).
550
+ if (SNAPPY_PREDICT_TRUE(op <= buf_limit - 11)) {
551
+ while (pattern_size < 8) {
552
+ UnalignedCopy64(src, op);
553
+ op += pattern_size;
554
+ pattern_size *= 2;
555
+ }
556
+ if (SNAPPY_PREDICT_TRUE(op >= op_limit)) return op_limit;
557
+ } else {
558
+ return IncrementalCopySlow(src, op, op_limit);
559
+ }
560
+ #endif // SNAPPY_HAVE_VECTOR_BYTE_SHUFFLE
561
+ }
562
+ assert(pattern_size >= big_pattern_size_lower_bound);
563
+ constexpr bool use_16bytes_chunk = big_pattern_size_lower_bound == 16;
564
+
565
+ // Copy 1x 16 bytes (or 2x 8 bytes in non-SSE) at a time. Because op - src can
566
+ // be < 16 in non-SSE, a single UnalignedCopy128 might overwrite data in op.
567
+ // UnalignedCopy64 is safe because expanding the pattern to at least 8 bytes
568
+ // guarantees that op - src >= 8.
569
+ //
570
+ // Typically, the op_limit is the gating factor so try to simplify the loop
571
+ // based on that.
572
+ if (SNAPPY_PREDICT_TRUE(op_limit <= buf_limit - 15)) {
573
+ // There is at least one, and at most four 16-byte blocks. Writing four
574
+ // conditionals instead of a loop allows FDO to layout the code with respect
575
+ // to the actual probabilities of each length.
576
+ // TODO: Replace with loop with trip count hint.
577
+ ConditionalUnalignedCopy128<use_16bytes_chunk>(src, op);
578
+ if (op + 16 < op_limit) {
579
+ ConditionalUnalignedCopy128<use_16bytes_chunk>(src + 16, op + 16);
580
+ }
581
+ if (op + 32 < op_limit) {
582
+ ConditionalUnalignedCopy128<use_16bytes_chunk>(src + 32, op + 32);
583
+ }
584
+ if (op + 48 < op_limit) {
585
+ ConditionalUnalignedCopy128<use_16bytes_chunk>(src + 48, op + 48);
586
+ }
587
+ return op_limit;
588
+ }
589
+
590
+ // Fall back to doing as much as we can with the available slop in the
591
+ // buffer. This code path is relatively cold however so we save code size by
592
+ // avoiding unrolling and vectorizing.
593
+ //
594
+ // TODO: Remove pragma when when cold regions don't get vectorized
595
+ // or unrolled.
596
+ #ifdef __clang__
597
+ #pragma clang loop unroll(disable)
598
+ #endif
599
+ for (char* op_end = buf_limit - 16; op < op_end; op += 16, src += 16) {
600
+ ConditionalUnalignedCopy128<use_16bytes_chunk>(src, op);
601
+ }
602
+ if (op >= op_limit) return op_limit;
603
+
604
+ // We only take this branch if we didn't have enough slop and we can do a
605
+ // single 8 byte copy.
606
+ if (SNAPPY_PREDICT_FALSE(op <= buf_limit - 8)) {
607
+ UnalignedCopy64(src, op);
608
+ src += 8;
609
+ op += 8;
610
+ }
611
+ return IncrementalCopySlow(src, op, op_limit);
612
+ }
613
+
614
+ } // namespace
615
+
616
+ template <bool allow_fast_path>
617
+ static inline char* EmitLiteral(char* op, const char* literal, int len) {
618
+ // The vast majority of copies are below 16 bytes, for which a
619
+ // call to std::memcpy() is overkill. This fast path can sometimes
620
+ // copy up to 15 bytes too much, but that is okay in the
621
+ // main loop, since we have a bit to go on for both sides:
622
+ //
623
+ // - The input will always have kInputMarginBytes = 15 extra
624
+ // available bytes, as long as we're in the main loop, and
625
+ // if not, allow_fast_path = false.
626
+ // - The output will always have 32 spare bytes (see
627
+ // MaxCompressedLength).
628
+ assert(len > 0); // Zero-length literals are disallowed
629
+ int n = len - 1;
630
+ if (allow_fast_path && len <= 16) {
631
+ // Fits in tag byte
632
+ *op++ = LITERAL | (n << 2);
633
+
634
+ UnalignedCopy128(literal, op);
635
+ return op + len;
636
+ }
637
+
638
+ if (n < 60) {
639
+ // Fits in tag byte
640
+ *op++ = LITERAL | (n << 2);
641
+ } else {
642
+ int count = (Bits::Log2Floor(n) >> 3) + 1;
643
+ assert(count >= 1);
644
+ assert(count <= 4);
645
+ *op++ = LITERAL | ((59 + count) << 2);
646
+ // Encode in upcoming bytes.
647
+ // Write 4 bytes, though we may care about only 1 of them. The output buffer
648
+ // is guaranteed to have at least 3 more spaces left as 'len >= 61' holds
649
+ // here and there is a std::memcpy() of size 'len' below.
650
+ LittleEndian::Store32(op, n);
651
+ op += count;
652
+ }
653
+ // When allow_fast_path is true, we can overwrite up to 16 bytes.
654
+ if (allow_fast_path) {
655
+ char* destination = op;
656
+ const char* source = literal;
657
+ const char* end = destination + len;
658
+ do {
659
+ std::memcpy(destination, source, 16);
660
+ destination += 16;
661
+ source += 16;
662
+ } while (destination < end);
663
+ } else {
664
+ std::memcpy(op, literal, len);
665
+ }
666
+ return op + len;
667
+ }
668
+
669
+ template <bool len_less_than_12>
670
+ static inline char* EmitCopyAtMost64(char* op, size_t offset, size_t len) {
671
+ assert(len <= 64);
672
+ assert(len >= 4);
673
+ assert(offset < 65536);
674
+ assert(len_less_than_12 == (len < 12));
675
+
676
+ if (len_less_than_12) {
677
+ uint32_t u = (len << 2) + (offset << 8);
678
+ uint32_t copy1 = COPY_1_BYTE_OFFSET - (4 << 2) + ((offset >> 3) & 0xe0);
679
+ uint32_t copy2 = COPY_2_BYTE_OFFSET - (1 << 2);
680
+ // It turns out that offset < 2048 is a difficult to predict branch.
681
+ // `perf record` shows this is the highest percentage of branch misses in
682
+ // benchmarks. This code produces branch free code, the data dependency
683
+ // chain that bottlenecks the throughput is so long that a few extra
684
+ // instructions are completely free (IPC << 6 because of data deps).
685
+ u += offset < 2048 ? copy1 : copy2;
686
+ LittleEndian::Store32(op, u);
687
+ op += offset < 2048 ? 2 : 3;
688
+ } else {
689
+ // Write 4 bytes, though we only care about 3 of them. The output buffer
690
+ // is required to have some slack, so the extra byte won't overrun it.
691
+ uint32_t u = COPY_2_BYTE_OFFSET + ((len - 1) << 2) + (offset << 8);
692
+ LittleEndian::Store32(op, u);
693
+ op += 3;
694
+ }
695
+ return op;
696
+ }
697
+
698
+ template <bool len_less_than_12>
699
+ static inline char* EmitCopy(char* op, size_t offset, size_t len) {
700
+ assert(len_less_than_12 == (len < 12));
701
+ if (len_less_than_12) {
702
+ return EmitCopyAtMost64</*len_less_than_12=*/true>(op, offset, len);
703
+ } else {
704
+ // A special case for len <= 64 might help, but so far measurements suggest
705
+ // it's in the noise.
706
+
707
+ // Emit 64 byte copies but make sure to keep at least four bytes reserved.
708
+ while (SNAPPY_PREDICT_FALSE(len >= 68)) {
709
+ op = EmitCopyAtMost64</*len_less_than_12=*/false>(op, offset, 64);
710
+ len -= 64;
711
+ }
712
+
713
+ // One or two copies will now finish the job.
714
+ if (len > 64) {
715
+ op = EmitCopyAtMost64</*len_less_than_12=*/false>(op, offset, 60);
716
+ len -= 60;
717
+ }
718
+
719
+ // Emit remainder.
720
+ if (len < 12) {
721
+ op = EmitCopyAtMost64</*len_less_than_12=*/true>(op, offset, len);
722
+ } else {
723
+ op = EmitCopyAtMost64</*len_less_than_12=*/false>(op, offset, len);
724
+ }
725
+ return op;
726
+ }
727
+ }
728
+
729
+ bool GetUncompressedLength(const char* start, size_t n, size_t* result) {
730
+ uint32_t v = 0;
731
+ const char* limit = start + n;
732
+ if (Varint::Parse32WithLimit(start, limit, &v) != NULL) {
733
+ *result = v;
734
+ return true;
735
+ } else {
736
+ return false;
737
+ }
738
+ }
739
+
740
+ namespace {
741
+ uint32_t CalculateTableSize(uint32_t input_size) {
742
+ static_assert(
743
+ kMaxHashTableSize >= kMinHashTableSize,
744
+ "kMaxHashTableSize should be greater or equal to kMinHashTableSize.");
745
+ if (input_size > kMaxHashTableSize) {
746
+ return kMaxHashTableSize;
747
+ }
748
+ if (input_size < kMinHashTableSize) {
749
+ return kMinHashTableSize;
750
+ }
751
+ // This is equivalent to Log2Ceiling(input_size), assuming input_size > 1.
752
+ // 2 << Log2Floor(x - 1) is equivalent to 1 << (1 + Log2Floor(x - 1)).
753
+ return 2u << Bits::Log2Floor(input_size - 1);
754
+ }
755
+ } // namespace
756
+
757
+ namespace internal {
758
+ WorkingMemory::WorkingMemory(size_t input_size) {
759
+ const size_t max_fragment_size = std::min(input_size, kBlockSize);
760
+ const size_t table_size = CalculateTableSize(max_fragment_size);
761
+ size_ = table_size * sizeof(*table_) + max_fragment_size +
762
+ MaxCompressedLength(max_fragment_size);
763
+ mem_ = std::allocator<char>().allocate(size_);
764
+ table_ = reinterpret_cast<uint16_t*>(mem_);
765
+ input_ = mem_ + table_size * sizeof(*table_);
766
+ output_ = input_ + max_fragment_size;
767
+ }
768
+
769
+ WorkingMemory::~WorkingMemory() {
770
+ std::allocator<char>().deallocate(mem_, size_);
771
+ }
772
+
773
+ uint16_t* WorkingMemory::GetHashTable(size_t fragment_size,
774
+ int* table_size) const {
775
+ const size_t htsize = CalculateTableSize(fragment_size);
776
+ memset(table_, 0, htsize * sizeof(*table_));
777
+ *table_size = htsize;
778
+ return table_;
779
+ }
780
+ } // end namespace internal
781
+
782
+ // Flat array compression that does not emit the "uncompressed length"
783
+ // prefix. Compresses "input" string to the "*op" buffer.
784
+ //
785
+ // REQUIRES: "input" is at most "kBlockSize" bytes long.
786
+ // REQUIRES: "op" points to an array of memory that is at least
787
+ // "MaxCompressedLength(input.size())" in size.
788
+ // REQUIRES: All elements in "table[0..table_size-1]" are initialized to zero.
789
+ // REQUIRES: "table_size" is a power of two
790
+ //
791
+ // Returns an "end" pointer into "op" buffer.
792
+ // "end - op" is the compressed size of "input".
793
+ namespace internal {
794
+ char* CompressFragment(const char* input, size_t input_size, char* op,
795
+ uint16_t* table, const int table_size) {
796
+ // "ip" is the input pointer, and "op" is the output pointer.
797
+ const char* ip = input;
798
+ assert(input_size <= kBlockSize);
799
+ assert((table_size & (table_size - 1)) == 0); // table must be power of two
800
+ const uint32_t mask = 2 * (table_size - 1);
801
+ const char* ip_end = input + input_size;
802
+ const char* base_ip = ip;
803
+
804
+ const size_t kInputMarginBytes = 15;
805
+ if (SNAPPY_PREDICT_TRUE(input_size >= kInputMarginBytes)) {
806
+ const char* ip_limit = input + input_size - kInputMarginBytes;
807
+
808
+ for (uint32_t preload = LittleEndian::Load32(ip + 1);;) {
809
+ // Bytes in [next_emit, ip) will be emitted as literal bytes. Or
810
+ // [next_emit, ip_end) after the main loop.
811
+ const char* next_emit = ip++;
812
+ uint64_t data = LittleEndian::Load64(ip);
813
+ // The body of this loop calls EmitLiteral once and then EmitCopy one or
814
+ // more times. (The exception is that when we're close to exhausting
815
+ // the input we goto emit_remainder.)
816
+ //
817
+ // In the first iteration of this loop we're just starting, so
818
+ // there's nothing to copy, so calling EmitLiteral once is
819
+ // necessary. And we only start a new iteration when the
820
+ // current iteration has determined that a call to EmitLiteral will
821
+ // precede the next call to EmitCopy (if any).
822
+ //
823
+ // Step 1: Scan forward in the input looking for a 4-byte-long match.
824
+ // If we get close to exhausting the input then goto emit_remainder.
825
+ //
826
+ // Heuristic match skipping: If 32 bytes are scanned with no matches
827
+ // found, start looking only at every other byte. If 32 more bytes are
828
+ // scanned (or skipped), look at every third byte, etc.. When a match is
829
+ // found, immediately go back to looking at every byte. This is a small
830
+ // loss (~5% performance, ~0.1% density) for compressible data due to more
831
+ // bookkeeping, but for non-compressible data (such as JPEG) it's a huge
832
+ // win since the compressor quickly "realizes" the data is incompressible
833
+ // and doesn't bother looking for matches everywhere.
834
+ //
835
+ // The "skip" variable keeps track of how many bytes there are since the
836
+ // last match; dividing it by 32 (ie. right-shifting by five) gives the
837
+ // number of bytes to move ahead for each iteration.
838
+ uint32_t skip = 32;
839
+
840
+ const char* candidate;
841
+ if (ip_limit - ip >= 16) {
842
+ auto delta = ip - base_ip;
843
+ for (int j = 0; j < 4; ++j) {
844
+ for (int k = 0; k < 4; ++k) {
845
+ int i = 4 * j + k;
846
+ // These for-loops are meant to be unrolled. So we can freely
847
+ // special case the first iteration to use the value already
848
+ // loaded in preload.
849
+ uint32_t dword = i == 0 ? preload : static_cast<uint32_t>(data);
850
+ assert(dword == LittleEndian::Load32(ip + i));
851
+ uint16_t* table_entry = TableEntry(table, dword, mask);
852
+ candidate = base_ip + *table_entry;
853
+ assert(candidate >= base_ip);
854
+ assert(candidate < ip + i);
855
+ *table_entry = delta + i;
856
+ if (SNAPPY_PREDICT_FALSE(LittleEndian::Load32(candidate) == dword)) {
857
+ *op = LITERAL | (i << 2);
858
+ UnalignedCopy128(next_emit, op + 1);
859
+ ip += i;
860
+ op = op + i + 2;
861
+ goto emit_match;
862
+ }
863
+ data >>= 8;
864
+ }
865
+ data = LittleEndian::Load64(ip + 4 * j + 4);
866
+ }
867
+ ip += 16;
868
+ skip += 16;
869
+ }
870
+ while (true) {
871
+ assert(static_cast<uint32_t>(data) == LittleEndian::Load32(ip));
872
+ uint16_t* table_entry = TableEntry(table, data, mask);
873
+ uint32_t bytes_between_hash_lookups = skip >> 5;
874
+ skip += bytes_between_hash_lookups;
875
+ const char* next_ip = ip + bytes_between_hash_lookups;
876
+ if (SNAPPY_PREDICT_FALSE(next_ip > ip_limit)) {
877
+ ip = next_emit;
878
+ goto emit_remainder;
879
+ }
880
+ candidate = base_ip + *table_entry;
881
+ assert(candidate >= base_ip);
882
+ assert(candidate < ip);
883
+
884
+ *table_entry = ip - base_ip;
885
+ if (SNAPPY_PREDICT_FALSE(static_cast<uint32_t>(data) ==
886
+ LittleEndian::Load32(candidate))) {
887
+ break;
888
+ }
889
+ data = LittleEndian::Load32(next_ip);
890
+ ip = next_ip;
891
+ }
892
+
893
+ // Step 2: A 4-byte match has been found. We'll later see if more
894
+ // than 4 bytes match. But, prior to the match, input
895
+ // bytes [next_emit, ip) are unmatched. Emit them as "literal bytes."
896
+ assert(next_emit + 16 <= ip_end);
897
+ op = EmitLiteral</*allow_fast_path=*/true>(op, next_emit, ip - next_emit);
898
+
899
+ // Step 3: Call EmitCopy, and then see if another EmitCopy could
900
+ // be our next move. Repeat until we find no match for the
901
+ // input immediately after what was consumed by the last EmitCopy call.
902
+ //
903
+ // If we exit this loop normally then we need to call EmitLiteral next,
904
+ // though we don't yet know how big the literal will be. We handle that
905
+ // by proceeding to the next iteration of the main loop. We also can exit
906
+ // this loop via goto if we get close to exhausting the input.
907
+ emit_match:
908
+ do {
909
+ // We have a 4-byte match at ip, and no need to emit any
910
+ // "literal bytes" prior to ip.
911
+ const char* base = ip;
912
+ std::pair<size_t, bool> p =
913
+ FindMatchLength(candidate + 4, ip + 4, ip_end, &data);
914
+ size_t matched = 4 + p.first;
915
+ ip += matched;
916
+ size_t offset = base - candidate;
917
+ assert(0 == memcmp(base, candidate, matched));
918
+ if (p.second) {
919
+ op = EmitCopy</*len_less_than_12=*/true>(op, offset, matched);
920
+ } else {
921
+ op = EmitCopy</*len_less_than_12=*/false>(op, offset, matched);
922
+ }
923
+ if (SNAPPY_PREDICT_FALSE(ip >= ip_limit)) {
924
+ goto emit_remainder;
925
+ }
926
+ // Expect 5 bytes to match
927
+ assert((data & 0xFFFFFFFFFF) ==
928
+ (LittleEndian::Load64(ip) & 0xFFFFFFFFFF));
929
+ // We are now looking for a 4-byte match again. We read
930
+ // table[Hash(ip, mask)] for that. To improve compression,
931
+ // we also update table[Hash(ip - 1, mask)] and table[Hash(ip, mask)].
932
+ *TableEntry(table, LittleEndian::Load32(ip - 1), mask) =
933
+ ip - base_ip - 1;
934
+ uint16_t* table_entry = TableEntry(table, data, mask);
935
+ candidate = base_ip + *table_entry;
936
+ *table_entry = ip - base_ip;
937
+ // Measurements on the benchmarks have shown the following probabilities
938
+ // for the loop to exit (ie. avg. number of iterations is reciprocal).
939
+ // BM_Flat/6 txt1 p = 0.3-0.4
940
+ // BM_Flat/7 txt2 p = 0.35
941
+ // BM_Flat/8 txt3 p = 0.3-0.4
942
+ // BM_Flat/9 txt3 p = 0.34-0.4
943
+ // BM_Flat/10 pb p = 0.4
944
+ // BM_Flat/11 gaviota p = 0.1
945
+ // BM_Flat/12 cp p = 0.5
946
+ // BM_Flat/13 c p = 0.3
947
+ } while (static_cast<uint32_t>(data) == LittleEndian::Load32(candidate));
948
+ // Because the least significant 5 bytes matched, we can utilize data
949
+ // for the next iteration.
950
+ preload = data >> 8;
951
+ }
952
+ }
953
+
954
+ emit_remainder:
955
+ // Emit the remaining bytes as a literal
956
+ if (ip < ip_end) {
957
+ op = EmitLiteral</*allow_fast_path=*/false>(op, ip, ip_end - ip);
958
+ }
959
+
960
+ return op;
961
+ }
962
+
963
+ char* CompressFragmentDoubleHash(const char* input, size_t input_size, char* op,
964
+ uint16_t* table, const int table_size,
965
+ uint16_t* table2, const int table_size2) {
966
+ (void)table_size2;
967
+ assert(table_size == table_size2);
968
+ // "ip" is the input pointer, and "op" is the output pointer.
969
+ const char* ip = input;
970
+ assert(input_size <= kBlockSize);
971
+ assert((table_size & (table_size - 1)) == 0); // table must be power of two
972
+ const uint32_t mask = 2 * (table_size - 1);
973
+ const char* ip_end = input + input_size;
974
+ const char* base_ip = ip;
975
+
976
+ const size_t kInputMarginBytes = 15;
977
+ if (SNAPPY_PREDICT_TRUE(input_size >= kInputMarginBytes)) {
978
+ const char* ip_limit = input + input_size - kInputMarginBytes;
979
+
980
+ for (;;) {
981
+ const char* next_emit = ip++;
982
+ uint64_t data = LittleEndian::Load64(ip);
983
+ uint32_t skip = 512;
984
+
985
+ const char* candidate;
986
+ uint32_t candidate_length;
987
+ while (true) {
988
+ assert(static_cast<uint32_t>(data) == LittleEndian::Load32(ip));
989
+ uint16_t* table_entry2 = TableEntry8ByteMatch(table2, data, mask);
990
+ uint32_t bytes_between_hash_lookups = skip >> 9;
991
+ skip++;
992
+ const char* next_ip = ip + bytes_between_hash_lookups;
993
+ if (SNAPPY_PREDICT_FALSE(next_ip > ip_limit)) {
994
+ ip = next_emit;
995
+ goto emit_remainder;
996
+ }
997
+ candidate = base_ip + *table_entry2;
998
+ assert(candidate >= base_ip);
999
+ assert(candidate < ip);
1000
+
1001
+ *table_entry2 = ip - base_ip;
1002
+ if (SNAPPY_PREDICT_FALSE(static_cast<uint32_t>(data) ==
1003
+ LittleEndian::Load32(candidate))) {
1004
+ candidate_length =
1005
+ FindMatchLengthPlain(candidate + 4, ip + 4, ip_end) + 4;
1006
+ break;
1007
+ }
1008
+
1009
+ uint16_t* table_entry = TableEntry4ByteMatch(table, data, mask);
1010
+ candidate = base_ip + *table_entry;
1011
+ assert(candidate >= base_ip);
1012
+ assert(candidate < ip);
1013
+
1014
+ *table_entry = ip - base_ip;
1015
+ if (SNAPPY_PREDICT_FALSE(static_cast<uint32_t>(data) ==
1016
+ LittleEndian::Load32(candidate))) {
1017
+ candidate_length =
1018
+ FindMatchLengthPlain(candidate + 4, ip + 4, ip_end) + 4;
1019
+ table_entry2 =
1020
+ TableEntry8ByteMatch(table2, LittleEndian::Load64(ip + 1), mask);
1021
+ auto candidate2 = base_ip + *table_entry2;
1022
+ size_t candidate_length2 =
1023
+ FindMatchLengthPlain(candidate2, ip + 1, ip_end);
1024
+ if (candidate_length2 > candidate_length) {
1025
+ *table_entry2 = ip - base_ip;
1026
+ candidate = candidate2;
1027
+ candidate_length = candidate_length2;
1028
+ ++ip;
1029
+ }
1030
+ break;
1031
+ }
1032
+ data = LittleEndian::Load64(next_ip);
1033
+ ip = next_ip;
1034
+ }
1035
+ // Backtrack to the point it matches fully.
1036
+ while (ip > next_emit && candidate > base_ip &&
1037
+ *(ip - 1) == *(candidate - 1)) {
1038
+ --ip;
1039
+ --candidate;
1040
+ ++candidate_length;
1041
+ }
1042
+ *TableEntry8ByteMatch(table2, LittleEndian::Load64(ip + 1), mask) =
1043
+ ip - base_ip + 1;
1044
+ *TableEntry8ByteMatch(table2, LittleEndian::Load64(ip + 2), mask) =
1045
+ ip - base_ip + 2;
1046
+ *TableEntry4ByteMatch(table, LittleEndian::Load32(ip + 1), mask) =
1047
+ ip - base_ip + 1;
1048
+ // Step 2: A 4-byte or 8-byte match has been found.
1049
+ // We'll later see if more than 4 bytes match. But, prior to the match,
1050
+ // input bytes [next_emit, ip) are unmatched. Emit them as
1051
+ // "literal bytes."
1052
+ assert(next_emit + 16 <= ip_end);
1053
+ if (ip - next_emit > 0) {
1054
+ op = EmitLiteral</*allow_fast_path=*/true>(op, next_emit,
1055
+ ip - next_emit);
1056
+ }
1057
+ // Step 3: Call EmitCopy, and then see if another EmitCopy could
1058
+ // be our next move. Repeat until we find no match for the
1059
+ // input immediately after what was consumed by the last EmitCopy call.
1060
+ //
1061
+ // If we exit this loop normally then we need to call EmitLiteral next,
1062
+ // though we don't yet know how big the literal will be. We handle that
1063
+ // by proceeding to the next iteration of the main loop. We also can exit
1064
+ // this loop via goto if we get close to exhausting the input.
1065
+ do {
1066
+ // We have a 4-byte match at ip, and no need to emit any
1067
+ // "literal bytes" prior to ip.
1068
+ const char* base = ip;
1069
+ ip += candidate_length;
1070
+ size_t offset = base - candidate;
1071
+ if (candidate_length < 12) {
1072
+ op =
1073
+ EmitCopy</*len_less_than_12=*/true>(op, offset, candidate_length);
1074
+ } else {
1075
+ op = EmitCopy</*len_less_than_12=*/false>(op, offset,
1076
+ candidate_length);
1077
+ }
1078
+ if (SNAPPY_PREDICT_FALSE(ip >= ip_limit)) {
1079
+ goto emit_remainder;
1080
+ }
1081
+ // We are now looking for a 4-byte match again. We read
1082
+ // table[Hash(ip, mask)] for that. To improve compression,
1083
+ // we also update several previous table entries.
1084
+ if (ip - base_ip > 7) {
1085
+ *TableEntry8ByteMatch(table2, LittleEndian::Load64(ip - 7), mask) =
1086
+ ip - base_ip - 7;
1087
+ *TableEntry8ByteMatch(table2, LittleEndian::Load64(ip - 4), mask) =
1088
+ ip - base_ip - 4;
1089
+ }
1090
+ *TableEntry8ByteMatch(table2, LittleEndian::Load64(ip - 3), mask) =
1091
+ ip - base_ip - 3;
1092
+ *TableEntry8ByteMatch(table2, LittleEndian::Load64(ip - 2), mask) =
1093
+ ip - base_ip - 2;
1094
+ *TableEntry4ByteMatch(table, LittleEndian::Load32(ip - 2), mask) =
1095
+ ip - base_ip - 2;
1096
+ *TableEntry4ByteMatch(table, LittleEndian::Load32(ip - 1), mask) =
1097
+ ip - base_ip - 1;
1098
+
1099
+ uint16_t* table_entry =
1100
+ TableEntry8ByteMatch(table2, LittleEndian::Load64(ip), mask);
1101
+ candidate = base_ip + *table_entry;
1102
+ *table_entry = ip - base_ip;
1103
+ if (LittleEndian::Load32(ip) == LittleEndian::Load32(candidate)) {
1104
+ candidate_length =
1105
+ FindMatchLengthPlain(candidate + 4, ip + 4, ip_end) + 4;
1106
+ continue;
1107
+ }
1108
+ table_entry =
1109
+ TableEntry4ByteMatch(table, LittleEndian::Load32(ip), mask);
1110
+ candidate = base_ip + *table_entry;
1111
+ *table_entry = ip - base_ip;
1112
+ if (LittleEndian::Load32(ip) == LittleEndian::Load32(candidate)) {
1113
+ candidate_length =
1114
+ FindMatchLengthPlain(candidate + 4, ip + 4, ip_end) + 4;
1115
+ continue;
1116
+ }
1117
+ break;
1118
+ } while (true);
1119
+ }
1120
+ }
1121
+
1122
+ emit_remainder:
1123
+ // Emit the remaining bytes as a literal
1124
+ if (ip < ip_end) {
1125
+ op = EmitLiteral</*allow_fast_path=*/false>(op, ip, ip_end - ip);
1126
+ }
1127
+
1128
+ return op;
1129
+ }
1130
+ } // end namespace internal
1131
+
1132
+ static inline void Report(int token, const char *algorithm, size_t
1133
+ compressed_size, size_t uncompressed_size) {
1134
+ // TODO: Switch to [[maybe_unused]] when we can assume C++17.
1135
+ (void)token;
1136
+ (void)algorithm;
1137
+ (void)compressed_size;
1138
+ (void)uncompressed_size;
1139
+ }
1140
+
1141
+ // Signature of output types needed by decompression code.
1142
+ // The decompression code is templatized on a type that obeys this
1143
+ // signature so that we do not pay virtual function call overhead in
1144
+ // the middle of a tight decompression loop.
1145
+ //
1146
+ // class DecompressionWriter {
1147
+ // public:
1148
+ // // Called before decompression
1149
+ // void SetExpectedLength(size_t length);
1150
+ //
1151
+ // // For performance a writer may choose to donate the cursor variable to the
1152
+ // // decompression function. The decompression will inject it in all its
1153
+ // // function calls to the writer. Keeping the important output cursor as a
1154
+ // // function local stack variable allows the compiler to keep it in
1155
+ // // register, which greatly aids performance by avoiding loads and stores of
1156
+ // // this variable in the fast path loop iterations.
1157
+ // T GetOutputPtr() const;
1158
+ //
1159
+ // // At end of decompression the loop donates the ownership of the cursor
1160
+ // // variable back to the writer by calling this function.
1161
+ // void SetOutputPtr(T op);
1162
+ //
1163
+ // // Called after decompression
1164
+ // bool CheckLength() const;
1165
+ //
1166
+ // // Called repeatedly during decompression
1167
+ // // Each function get a pointer to the op (output pointer), that the writer
1168
+ // // can use and update. Note it's important that these functions get fully
1169
+ // // inlined so that no actual address of the local variable needs to be
1170
+ // // taken.
1171
+ // bool Append(const char* ip, size_t length, T* op);
1172
+ // bool AppendFromSelf(uint32_t offset, size_t length, T* op);
1173
+ //
1174
+ // // The rules for how TryFastAppend differs from Append are somewhat
1175
+ // // convoluted:
1176
+ // //
1177
+ // // - TryFastAppend is allowed to decline (return false) at any
1178
+ // // time, for any reason -- just "return false" would be
1179
+ // // a perfectly legal implementation of TryFastAppend.
1180
+ // // The intention is for TryFastAppend to allow a fast path
1181
+ // // in the common case of a small append.
1182
+ // // - TryFastAppend is allowed to read up to <available> bytes
1183
+ // // from the input buffer, whereas Append is allowed to read
1184
+ // // <length>. However, if it returns true, it must leave
1185
+ // // at least five (kMaximumTagLength) bytes in the input buffer
1186
+ // // afterwards, so that there is always enough space to read the
1187
+ // // next tag without checking for a refill.
1188
+ // // - TryFastAppend must always return decline (return false)
1189
+ // // if <length> is 61 or more, as in this case the literal length is not
1190
+ // // decoded fully. In practice, this should not be a big problem,
1191
+ // // as it is unlikely that one would implement a fast path accepting
1192
+ // // this much data.
1193
+ // //
1194
+ // bool TryFastAppend(const char* ip, size_t available, size_t length, T* op);
1195
+ // };
1196
+
1197
+ static inline uint32_t ExtractLowBytes(const uint32_t& v, int n) {
1198
+ assert(n >= 0);
1199
+ assert(n <= 4);
1200
+ #if SNAPPY_HAVE_BMI2
1201
+ return _bzhi_u32(v, 8 * n);
1202
+ #else
1203
+ // This needs to be wider than uint32_t otherwise `mask << 32` will be
1204
+ // undefined.
1205
+ uint64_t mask = 0xffffffff;
1206
+ return v & ~(mask << (8 * n));
1207
+ #endif
1208
+ }
1209
+
1210
+ static inline bool LeftShiftOverflows(uint8_t value, uint32_t shift) {
1211
+ assert(shift < 32);
1212
+ static const uint8_t masks[] = {
1213
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
1214
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
1215
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
1216
+ 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe};
1217
+ return (value & masks[shift]) != 0;
1218
+ }
1219
+
1220
+ inline bool Copy64BytesWithPatternExtension(ptrdiff_t dst, size_t offset) {
1221
+ // TODO: Switch to [[maybe_unused]] when we can assume C++17.
1222
+ (void)dst;
1223
+ return offset != 0;
1224
+ }
1225
+
1226
+ // Copies between size bytes and 64 bytes from src to dest. size cannot exceed
1227
+ // 64. More than size bytes, but never exceeding 64, might be copied if doing
1228
+ // so gives better performance. [src, src + size) must not overlap with
1229
+ // [dst, dst + size), but [src, src + 64) may overlap with [dst, dst + 64).
1230
+ void MemCopy64(char* dst, const void* src, size_t size) {
1231
+ // Always copy this many bytes. If that's below size then copy the full 64.
1232
+ constexpr int kShortMemCopy = 32;
1233
+
1234
+ assert(size <= 64);
1235
+ assert(std::less_equal<const void*>()(static_cast<const char*>(src) + size,
1236
+ dst) ||
1237
+ std::less_equal<const void*>()(dst + size, src));
1238
+
1239
+ // We know that src and dst are at least size bytes apart. However, because we
1240
+ // might copy more than size bytes the copy still might overlap past size.
1241
+ // E.g. if src and dst appear consecutively in memory (src + size >= dst).
1242
+ // TODO: Investigate wider copies on other platforms.
1243
+ #if defined(__x86_64__) && defined(__AVX__)
1244
+ assert(kShortMemCopy <= 32);
1245
+ __m256i data = _mm256_lddqu_si256(static_cast<const __m256i *>(src));
1246
+ _mm256_storeu_si256(reinterpret_cast<__m256i *>(dst), data);
1247
+ // Profiling shows that nearly all copies are short.
1248
+ if (SNAPPY_PREDICT_FALSE(size > kShortMemCopy)) {
1249
+ data = _mm256_lddqu_si256(static_cast<const __m256i *>(src) + 1);
1250
+ _mm256_storeu_si256(reinterpret_cast<__m256i *>(dst) + 1, data);
1251
+ }
1252
+ #else
1253
+ std::memmove(dst, src, kShortMemCopy);
1254
+ // Profiling shows that nearly all copies are short.
1255
+ if (SNAPPY_PREDICT_FALSE(size > kShortMemCopy)) {
1256
+ std::memmove(dst + kShortMemCopy,
1257
+ static_cast<const uint8_t*>(src) + kShortMemCopy,
1258
+ 64 - kShortMemCopy);
1259
+ }
1260
+ #endif
1261
+ }
1262
+
1263
+ void MemCopy64(ptrdiff_t dst, const void* src, size_t size) {
1264
+ // TODO: Switch to [[maybe_unused]] when we can assume C++17.
1265
+ (void)dst;
1266
+ (void)src;
1267
+ (void)size;
1268
+ }
1269
+
1270
+ void ClearDeferred(const void** deferred_src, size_t* deferred_length,
1271
+ uint8_t* safe_source) {
1272
+ *deferred_src = safe_source;
1273
+ *deferred_length = 0;
1274
+ }
1275
+
1276
+ void DeferMemCopy(const void** deferred_src, size_t* deferred_length,
1277
+ const void* src, size_t length) {
1278
+ *deferred_src = src;
1279
+ *deferred_length = length;
1280
+ }
1281
+
1282
+ SNAPPY_ATTRIBUTE_ALWAYS_INLINE
1283
+ inline size_t AdvanceToNextTagARMOptimized(const uint8_t** ip_p, size_t* tag) {
1284
+ const uint8_t*& ip = *ip_p;
1285
+ // This section is crucial for the throughput of the decompression loop.
1286
+ // The latency of an iteration is fundamentally constrained by the
1287
+ // following data chain on ip.
1288
+ // ip -> c = Load(ip) -> delta1 = (c & 3) -> ip += delta1 or delta2
1289
+ // delta2 = ((c >> 2) + 1) ip++
1290
+ // This is different from X86 optimizations because ARM has conditional add
1291
+ // instruction (csinc) and it removes several register moves.
1292
+ const size_t tag_type = *tag & 3;
1293
+ const bool is_literal = (tag_type == 0);
1294
+ if (is_literal) {
1295
+ size_t next_literal_tag = (*tag >> 2) + 1;
1296
+ *tag = ip[next_literal_tag];
1297
+ ip += next_literal_tag + 1;
1298
+ } else {
1299
+ *tag = ip[tag_type];
1300
+ ip += tag_type + 1;
1301
+ }
1302
+ return tag_type;
1303
+ }
1304
+
1305
+ SNAPPY_ATTRIBUTE_ALWAYS_INLINE
1306
+ inline size_t AdvanceToNextTagX86Optimized(const uint8_t** ip_p, size_t* tag) {
1307
+ const uint8_t*& ip = *ip_p;
1308
+ // This section is crucial for the throughput of the decompression loop.
1309
+ // The latency of an iteration is fundamentally constrained by the
1310
+ // following data chain on ip.
1311
+ // ip -> c = Load(ip) -> ip1 = ip + 1 + (c & 3) -> ip = ip1 or ip2
1312
+ // ip2 = ip + 2 + (c >> 2)
1313
+ // This amounts to 8 cycles.
1314
+ // 5 (load) + 1 (c & 3) + 1 (lea ip1, [ip + (c & 3) + 1]) + 1 (cmov)
1315
+ size_t literal_len = *tag >> 2;
1316
+ size_t tag_type = *tag;
1317
+ bool is_literal;
1318
+ #if defined(__GCC_ASM_FLAG_OUTPUTS__) && defined(__x86_64__)
1319
+ // TODO clang misses the fact that the (c & 3) already correctly
1320
+ // sets the zero flag.
1321
+ asm("and $3, %k[tag_type]\n\t"
1322
+ : [tag_type] "+r"(tag_type), "=@ccz"(is_literal)
1323
+ :: "cc");
1324
+ #else
1325
+ tag_type &= 3;
1326
+ is_literal = (tag_type == 0);
1327
+ #endif
1328
+ // TODO
1329
+ // This is code is subtle. Loading the values first and then cmov has less
1330
+ // latency then cmov ip and then load. However clang would move the loads
1331
+ // in an optimization phase, volatile prevents this transformation.
1332
+ // Note that we have enough slop bytes (64) that the loads are always valid.
1333
+ size_t tag_literal =
1334
+ static_cast<const volatile uint8_t*>(ip)[1 + literal_len];
1335
+ size_t tag_copy = static_cast<const volatile uint8_t*>(ip)[tag_type];
1336
+ *tag = is_literal ? tag_literal : tag_copy;
1337
+ const uint8_t* ip_copy = ip + 1 + tag_type;
1338
+ const uint8_t* ip_literal = ip + 2 + literal_len;
1339
+ ip = is_literal ? ip_literal : ip_copy;
1340
+ #if defined(__GNUC__) && defined(__x86_64__)
1341
+ // TODO Clang is "optimizing" zero-extension (a totally free
1342
+ // operation) this means that after the cmov of tag, it emits another movzb
1343
+ // tag, byte(tag). It really matters as it's on the core chain. This dummy
1344
+ // asm, persuades clang to do the zero-extension at the load (it's automatic)
1345
+ // removing the expensive movzb.
1346
+ asm("" ::"r"(tag_copy));
1347
+ #endif
1348
+ return tag_type;
1349
+ }
1350
+
1351
+ // Extract the offset for copy-1 and copy-2 returns 0 for literals or copy-4.
1352
+ inline uint32_t ExtractOffset(uint32_t val, size_t tag_type) {
1353
+ // For x86 non-static storage works better. For ARM static storage is better.
1354
+ // TODO: Once the array is recognized as a register, improve the
1355
+ // readability for x86.
1356
+ #if defined(__x86_64__)
1357
+ constexpr uint64_t kExtractMasksCombined = 0x0000FFFF00FF0000ull;
1358
+ uint16_t result;
1359
+ memcpy(&result,
1360
+ reinterpret_cast<const char*>(&kExtractMasksCombined) + 2 * tag_type,
1361
+ sizeof(result));
1362
+ return val & result;
1363
+ #elif defined(__aarch64__)
1364
+ constexpr uint64_t kExtractMasksCombined = 0x0000FFFF00FF0000ull;
1365
+ return val & static_cast<uint32_t>(
1366
+ (kExtractMasksCombined >> (tag_type * 16)) & 0xFFFF);
1367
+ #else
1368
+ static constexpr uint32_t kExtractMasks[4] = {0, 0xFF, 0xFFFF, 0};
1369
+ return val & kExtractMasks[tag_type];
1370
+ #endif
1371
+ };
1372
+
1373
+ // Core decompression loop, when there is enough data available.
1374
+ // Decompresses the input buffer [ip, ip_limit) into the output buffer
1375
+ // [op, op_limit_min_slop). Returning when either we are too close to the end
1376
+ // of the input buffer, or we exceed op_limit_min_slop or when a exceptional
1377
+ // tag is encountered (literal of length > 60) or a copy-4.
1378
+ // Returns {ip, op} at the points it stopped decoding.
1379
+ // TODO This function probably does not need to be inlined, as it
1380
+ // should decode large chunks at a time. This allows runtime dispatch to
1381
+ // implementations based on CPU capability (BMI2 / perhaps 32 / 64 byte memcpy).
1382
+ template <typename T>
1383
+ std::pair<const uint8_t*, ptrdiff_t> DecompressBranchless(
1384
+ const uint8_t* ip, const uint8_t* ip_limit, ptrdiff_t op, T op_base,
1385
+ ptrdiff_t op_limit_min_slop) {
1386
+ // If deferred_src is invalid point it here.
1387
+ uint8_t safe_source[64];
1388
+ const void* deferred_src;
1389
+ size_t deferred_length;
1390
+ ClearDeferred(&deferred_src, &deferred_length, safe_source);
1391
+
1392
+ // We unroll the inner loop twice so we need twice the spare room.
1393
+ op_limit_min_slop -= kSlopBytes;
1394
+ if (2 * (kSlopBytes + 1) < ip_limit - ip && op < op_limit_min_slop) {
1395
+ const uint8_t* const ip_limit_min_slop = ip_limit - 2 * kSlopBytes - 1;
1396
+ ip++;
1397
+ // ip points just past the tag and we are touching at maximum kSlopBytes
1398
+ // in an iteration.
1399
+ size_t tag = ip[-1];
1400
+ #if defined(__clang__) && defined(__aarch64__)
1401
+ // Workaround for https://bugs.llvm.org/show_bug.cgi?id=51317
1402
+ // when loading 1 byte, clang for aarch64 doesn't realize that it(ldrb)
1403
+ // comes with free zero-extension, so clang generates another
1404
+ // 'and xn, xm, 0xff' before it use that as the offset. This 'and' is
1405
+ // redundant and can be removed by adding this dummy asm, which gives
1406
+ // clang a hint that we're doing the zero-extension at the load.
1407
+ asm("" ::"r"(tag));
1408
+ #endif
1409
+ do {
1410
+ // The throughput is limited by instructions, unrolling the inner loop
1411
+ // twice reduces the amount of instructions checking limits and also
1412
+ // leads to reduced mov's.
1413
+
1414
+ SNAPPY_PREFETCH(ip + 128);
1415
+ for (int i = 0; i < 2; i++) {
1416
+ const uint8_t* old_ip = ip;
1417
+ assert(tag == ip[-1]);
1418
+ // For literals tag_type = 0, hence we will always obtain 0 from
1419
+ // ExtractLowBytes. For literals offset will thus be kLiteralOffset.
1420
+ ptrdiff_t len_minus_offset = kLengthMinusOffset[tag];
1421
+ uint32_t next;
1422
+ #if defined(__aarch64__)
1423
+ size_t tag_type = AdvanceToNextTagARMOptimized(&ip, &tag);
1424
+ // We never need more than 16 bits. Doing a Load16 allows the compiler
1425
+ // to elide the masking operation in ExtractOffset.
1426
+ next = LittleEndian::Load16(old_ip);
1427
+ #else
1428
+ size_t tag_type = AdvanceToNextTagX86Optimized(&ip, &tag);
1429
+ next = LittleEndian::Load32(old_ip);
1430
+ #endif
1431
+ size_t len = len_minus_offset & 0xFF;
1432
+ ptrdiff_t extracted = ExtractOffset(next, tag_type);
1433
+ ptrdiff_t len_min_offset = len_minus_offset - extracted;
1434
+ if (SNAPPY_PREDICT_FALSE(len_minus_offset > extracted)) {
1435
+ if (SNAPPY_PREDICT_FALSE(len & 0x80)) {
1436
+ // Exceptional case (long literal or copy 4).
1437
+ // Actually doing the copy here is negatively impacting the main
1438
+ // loop due to compiler incorrectly allocating a register for
1439
+ // this fallback. Hence we just break.
1440
+ break_loop:
1441
+ ip = old_ip;
1442
+ goto exit;
1443
+ }
1444
+ // Only copy-1 or copy-2 tags can get here.
1445
+ assert(tag_type == 1 || tag_type == 2);
1446
+ std::ptrdiff_t delta = (op + deferred_length) + len_min_offset - len;
1447
+ // Guard against copies before the buffer start.
1448
+ // Execute any deferred MemCopy since we write to dst here.
1449
+ MemCopy64(op_base + op, deferred_src, deferred_length);
1450
+ op += deferred_length;
1451
+ ClearDeferred(&deferred_src, &deferred_length, safe_source);
1452
+ if (SNAPPY_PREDICT_FALSE(delta < 0 ||
1453
+ !Copy64BytesWithPatternExtension(
1454
+ op_base + op, len - len_min_offset))) {
1455
+ goto break_loop;
1456
+ }
1457
+ // We aren't deferring this copy so add length right away.
1458
+ op += len;
1459
+ continue;
1460
+ }
1461
+ std::ptrdiff_t delta = (op + deferred_length) + len_min_offset - len;
1462
+ if (SNAPPY_PREDICT_FALSE(delta < 0)) {
1463
+ // Due to the spurious offset in literals have this will trigger
1464
+ // at the start of a block when op is still smaller than 256.
1465
+ if (tag_type != 0) goto break_loop;
1466
+ MemCopy64(op_base + op, deferred_src, deferred_length);
1467
+ op += deferred_length;
1468
+ DeferMemCopy(&deferred_src, &deferred_length, old_ip, len);
1469
+ continue;
1470
+ }
1471
+
1472
+ // For copies we need to copy from op_base + delta, for literals
1473
+ // we need to copy from ip instead of from the stream.
1474
+ const void* from =
1475
+ tag_type ? reinterpret_cast<void*>(op_base + delta) : old_ip;
1476
+ MemCopy64(op_base + op, deferred_src, deferred_length);
1477
+ op += deferred_length;
1478
+ DeferMemCopy(&deferred_src, &deferred_length, from, len);
1479
+ }
1480
+ } while (ip < ip_limit_min_slop &&
1481
+ static_cast<ptrdiff_t>(op + deferred_length) < op_limit_min_slop);
1482
+ exit:
1483
+ ip--;
1484
+ assert(ip <= ip_limit);
1485
+ }
1486
+ // If we deferred a copy then we can perform. If we are up to date then we
1487
+ // might not have enough slop bytes and could run past the end.
1488
+ if (deferred_length) {
1489
+ MemCopy64(op_base + op, deferred_src, deferred_length);
1490
+ op += deferred_length;
1491
+ ClearDeferred(&deferred_src, &deferred_length, safe_source);
1492
+ }
1493
+ return {ip, op};
1494
+ }
1495
+
1496
+ // Helper class for decompression
1497
+ class SnappyDecompressor {
1498
+ private:
1499
+ Source* reader_; // Underlying source of bytes to decompress
1500
+ const char* ip_; // Points to next buffered byte
1501
+ const char* ip_limit_; // Points just past buffered bytes
1502
+ // If ip < ip_limit_min_maxtaglen_ it's safe to read kMaxTagLength from
1503
+ // buffer.
1504
+ const char* ip_limit_min_maxtaglen_;
1505
+ uint32_t peeked_; // Bytes peeked from reader (need to skip)
1506
+ bool eof_; // Hit end of input without an error?
1507
+ char scratch_[kMaximumTagLength]; // See RefillTag().
1508
+
1509
+ // Ensure that all of the tag metadata for the next tag is available
1510
+ // in [ip_..ip_limit_-1]. Also ensures that [ip,ip+4] is readable even
1511
+ // if (ip_limit_ - ip_ < 5).
1512
+ //
1513
+ // Returns true on success, false on error or end of input.
1514
+ bool RefillTag();
1515
+
1516
+ void ResetLimit(const char* ip) {
1517
+ ip_limit_min_maxtaglen_ =
1518
+ ip_limit_ - std::min<ptrdiff_t>(ip_limit_ - ip, kMaximumTagLength - 1);
1519
+ }
1520
+
1521
+ public:
1522
+ explicit SnappyDecompressor(Source* reader)
1523
+ : reader_(reader), ip_(NULL), ip_limit_(NULL), peeked_(0), eof_(false) {}
1524
+
1525
+ ~SnappyDecompressor() {
1526
+ // Advance past any bytes we peeked at from the reader
1527
+ reader_->Skip(peeked_);
1528
+ }
1529
+
1530
+ // Returns true iff we have hit the end of the input without an error.
1531
+ bool eof() const { return eof_; }
1532
+
1533
+ // Read the uncompressed length stored at the start of the compressed data.
1534
+ // On success, stores the length in *result and returns true.
1535
+ // On failure, returns false.
1536
+ bool ReadUncompressedLength(uint32_t* result) {
1537
+ assert(ip_ == NULL); // Must not have read anything yet
1538
+ // Length is encoded in 1..5 bytes
1539
+ *result = 0;
1540
+ uint32_t shift = 0;
1541
+ while (true) {
1542
+ if (shift >= 32) return false;
1543
+ size_t n;
1544
+ const char* ip = reader_->Peek(&n);
1545
+ if (n == 0) return false;
1546
+ const unsigned char c = *(reinterpret_cast<const unsigned char*>(ip));
1547
+ reader_->Skip(1);
1548
+ uint32_t val = c & 0x7f;
1549
+ if (LeftShiftOverflows(static_cast<uint8_t>(val), shift)) return false;
1550
+ *result |= val << shift;
1551
+ if (c < 128) {
1552
+ break;
1553
+ }
1554
+ shift += 7;
1555
+ }
1556
+ return true;
1557
+ }
1558
+
1559
+ // Process the next item found in the input.
1560
+ // Returns true if successful, false on error or end of input.
1561
+ template <class Writer>
1562
+ #if defined(__GNUC__) && defined(__x86_64__)
1563
+ __attribute__((aligned(32)))
1564
+ #endif
1565
+ void
1566
+ DecompressAllTags(Writer* writer) {
1567
+ const char* ip = ip_;
1568
+ ResetLimit(ip);
1569
+ auto op = writer->GetOutputPtr();
1570
+ // We could have put this refill fragment only at the beginning of the loop.
1571
+ // However, duplicating it at the end of each branch gives the compiler more
1572
+ // scope to optimize the <ip_limit_ - ip> expression based on the local
1573
+ // context, which overall increases speed.
1574
+ #define MAYBE_REFILL() \
1575
+ if (SNAPPY_PREDICT_FALSE(ip >= ip_limit_min_maxtaglen_)) { \
1576
+ ip_ = ip; \
1577
+ if (SNAPPY_PREDICT_FALSE(!RefillTag())) goto exit; \
1578
+ ip = ip_; \
1579
+ ResetLimit(ip); \
1580
+ } \
1581
+ preload = static_cast<uint8_t>(*ip)
1582
+
1583
+ // At the start of the for loop below the least significant byte of preload
1584
+ // contains the tag.
1585
+ uint32_t preload;
1586
+ MAYBE_REFILL();
1587
+ for (;;) {
1588
+ {
1589
+ ptrdiff_t op_limit_min_slop;
1590
+ auto op_base = writer->GetBase(&op_limit_min_slop);
1591
+ if (op_base) {
1592
+ auto res =
1593
+ DecompressBranchless(reinterpret_cast<const uint8_t*>(ip),
1594
+ reinterpret_cast<const uint8_t*>(ip_limit_),
1595
+ op - op_base, op_base, op_limit_min_slop);
1596
+ ip = reinterpret_cast<const char*>(res.first);
1597
+ op = op_base + res.second;
1598
+ MAYBE_REFILL();
1599
+ }
1600
+ }
1601
+ const uint8_t c = static_cast<uint8_t>(preload);
1602
+ ip++;
1603
+
1604
+ // Ratio of iterations that have LITERAL vs non-LITERAL for different
1605
+ // inputs.
1606
+ //
1607
+ // input LITERAL NON_LITERAL
1608
+ // -----------------------------------
1609
+ // html|html4|cp 23% 77%
1610
+ // urls 36% 64%
1611
+ // jpg 47% 53%
1612
+ // pdf 19% 81%
1613
+ // txt[1-4] 25% 75%
1614
+ // pb 24% 76%
1615
+ // bin 24% 76%
1616
+ if (SNAPPY_PREDICT_FALSE((c & 0x3) == LITERAL)) {
1617
+ size_t literal_length = (c >> 2) + 1u;
1618
+ if (writer->TryFastAppend(ip, ip_limit_ - ip, literal_length, &op)) {
1619
+ assert(literal_length < 61);
1620
+ ip += literal_length;
1621
+ // NOTE: There is no MAYBE_REFILL() here, as TryFastAppend()
1622
+ // will not return true unless there's already at least five spare
1623
+ // bytes in addition to the literal.
1624
+ preload = static_cast<uint8_t>(*ip);
1625
+ continue;
1626
+ }
1627
+ if (SNAPPY_PREDICT_FALSE(literal_length >= 61)) {
1628
+ // Long literal.
1629
+ const size_t literal_length_length = literal_length - 60;
1630
+ literal_length =
1631
+ ExtractLowBytes(LittleEndian::Load32(ip), literal_length_length) +
1632
+ 1;
1633
+ ip += literal_length_length;
1634
+ }
1635
+
1636
+ size_t avail = ip_limit_ - ip;
1637
+ while (avail < literal_length) {
1638
+ if (!writer->Append(ip, avail, &op)) goto exit;
1639
+ literal_length -= avail;
1640
+ reader_->Skip(peeked_);
1641
+ size_t n;
1642
+ ip = reader_->Peek(&n);
1643
+ avail = n;
1644
+ peeked_ = avail;
1645
+ if (avail == 0) goto exit;
1646
+ ip_limit_ = ip + avail;
1647
+ ResetLimit(ip);
1648
+ }
1649
+ if (!writer->Append(ip, literal_length, &op)) goto exit;
1650
+ ip += literal_length;
1651
+ MAYBE_REFILL();
1652
+ } else {
1653
+ if (SNAPPY_PREDICT_FALSE((c & 3) == COPY_4_BYTE_OFFSET)) {
1654
+ const size_t copy_offset = LittleEndian::Load32(ip);
1655
+ const size_t length = (c >> 2) + 1;
1656
+ ip += 4;
1657
+
1658
+ if (!writer->AppendFromSelf(copy_offset, length, &op)) goto exit;
1659
+ } else {
1660
+ const ptrdiff_t entry = kLengthMinusOffset[c];
1661
+ preload = LittleEndian::Load32(ip);
1662
+ const uint32_t trailer = ExtractLowBytes(preload, c & 3);
1663
+ const uint32_t length = entry & 0xff;
1664
+ assert(length > 0);
1665
+
1666
+ // copy_offset/256 is encoded in bits 8..10. By just fetching
1667
+ // those bits, we get copy_offset (since the bit-field starts at
1668
+ // bit 8).
1669
+ const uint32_t copy_offset = trailer - entry + length;
1670
+ if (!writer->AppendFromSelf(copy_offset, length, &op)) goto exit;
1671
+
1672
+ ip += (c & 3);
1673
+ // By using the result of the previous load we reduce the critical
1674
+ // dependency chain of ip to 4 cycles.
1675
+ preload >>= (c & 3) * 8;
1676
+ if (ip < ip_limit_min_maxtaglen_) continue;
1677
+ }
1678
+ MAYBE_REFILL();
1679
+ }
1680
+ }
1681
+ #undef MAYBE_REFILL
1682
+ exit:
1683
+ writer->SetOutputPtr(op);
1684
+ }
1685
+ };
1686
+
1687
+ constexpr uint32_t CalculateNeeded(uint8_t tag) {
1688
+ return ((tag & 3) == 0 && tag >= (60 * 4))
1689
+ ? (tag >> 2) - 58
1690
+ : (0x05030201 >> ((tag * 8) & 31)) & 0xFF;
1691
+ }
1692
+
1693
+ #if __cplusplus >= 201402L
1694
+ constexpr bool VerifyCalculateNeeded() {
1695
+ for (int i = 0; i < 1; i++) {
1696
+ if (CalculateNeeded(i) != (char_table[i] >> 11) + 1) return false;
1697
+ }
1698
+ return true;
1699
+ }
1700
+
1701
+ // Make sure CalculateNeeded is correct by verifying it against the established
1702
+ // table encoding the number of added bytes needed.
1703
+ static_assert(VerifyCalculateNeeded(), "");
1704
+ #endif // c++14
1705
+
1706
+ bool SnappyDecompressor::RefillTag() {
1707
+ const char* ip = ip_;
1708
+ if (ip == ip_limit_) {
1709
+ // Fetch a new fragment from the reader
1710
+ reader_->Skip(peeked_); // All peeked bytes are used up
1711
+ size_t n;
1712
+ ip = reader_->Peek(&n);
1713
+ peeked_ = n;
1714
+ eof_ = (n == 0);
1715
+ if (eof_) return false;
1716
+ ip_limit_ = ip + n;
1717
+ }
1718
+
1719
+ // Read the tag character
1720
+ assert(ip < ip_limit_);
1721
+ const unsigned char c = *(reinterpret_cast<const unsigned char*>(ip));
1722
+ // At this point make sure that the data for the next tag is consecutive.
1723
+ // For copy 1 this means the next 2 bytes (tag and 1 byte offset)
1724
+ // For copy 2 the next 3 bytes (tag and 2 byte offset)
1725
+ // For copy 4 the next 5 bytes (tag and 4 byte offset)
1726
+ // For all small literals we only need 1 byte buf for literals 60...63 the
1727
+ // length is encoded in 1...4 extra bytes.
1728
+ const uint32_t needed = CalculateNeeded(c);
1729
+ assert(needed <= sizeof(scratch_));
1730
+
1731
+ // Read more bytes from reader if needed
1732
+ uint32_t nbuf = ip_limit_ - ip;
1733
+ if (nbuf < needed) {
1734
+ // Stitch together bytes from ip and reader to form the word
1735
+ // contents. We store the needed bytes in "scratch_". They
1736
+ // will be consumed immediately by the caller since we do not
1737
+ // read more than we need.
1738
+ std::memmove(scratch_, ip, nbuf);
1739
+ reader_->Skip(peeked_); // All peeked bytes are used up
1740
+ peeked_ = 0;
1741
+ while (nbuf < needed) {
1742
+ size_t length;
1743
+ const char* src = reader_->Peek(&length);
1744
+ if (length == 0) return false;
1745
+ uint32_t to_add = std::min<uint32_t>(needed - nbuf, length);
1746
+ std::memcpy(scratch_ + nbuf, src, to_add);
1747
+ nbuf += to_add;
1748
+ reader_->Skip(to_add);
1749
+ }
1750
+ assert(nbuf == needed);
1751
+ ip_ = scratch_;
1752
+ ip_limit_ = scratch_ + needed;
1753
+ } else if (nbuf < kMaximumTagLength) {
1754
+ // Have enough bytes, but move into scratch_ so that we do not
1755
+ // read past end of input
1756
+ std::memmove(scratch_, ip, nbuf);
1757
+ reader_->Skip(peeked_); // All peeked bytes are used up
1758
+ peeked_ = 0;
1759
+ ip_ = scratch_;
1760
+ ip_limit_ = scratch_ + nbuf;
1761
+ } else {
1762
+ // Pass pointer to buffer returned by reader_.
1763
+ ip_ = ip;
1764
+ }
1765
+ return true;
1766
+ }
1767
+
1768
+ template <typename Writer>
1769
+ static bool InternalUncompress(Source* r, Writer* writer) {
1770
+ // Read the uncompressed length from the front of the compressed input
1771
+ SnappyDecompressor decompressor(r);
1772
+ uint32_t uncompressed_len = 0;
1773
+ if (!decompressor.ReadUncompressedLength(&uncompressed_len)) return false;
1774
+
1775
+ return InternalUncompressAllTags(&decompressor, writer, r->Available(),
1776
+ uncompressed_len);
1777
+ }
1778
+
1779
+ template <typename Writer>
1780
+ static bool InternalUncompressAllTags(SnappyDecompressor* decompressor,
1781
+ Writer* writer, uint32_t compressed_len,
1782
+ uint32_t uncompressed_len) {
1783
+ int token = 0;
1784
+ Report(token, "snappy_uncompress", compressed_len, uncompressed_len);
1785
+
1786
+ writer->SetExpectedLength(uncompressed_len);
1787
+
1788
+ // Process the entire input
1789
+ decompressor->DecompressAllTags(writer);
1790
+ writer->Flush();
1791
+ return (decompressor->eof() && writer->CheckLength());
1792
+ }
1793
+
1794
+ bool GetUncompressedLength(Source* source, uint32_t* result) {
1795
+ SnappyDecompressor decompressor(source);
1796
+ return decompressor.ReadUncompressedLength(result);
1797
+ }
1798
+
1799
+ size_t Compress(Source* reader, Sink* writer) {
1800
+ return Compress(reader, writer, CompressionOptions{});
1801
+ }
1802
+
1803
+ size_t Compress(Source* reader, Sink* writer, CompressionOptions options) {
1804
+ assert(options.level == 1 || options.level == 2);
1805
+ int token = 0;
1806
+ size_t written = 0;
1807
+ size_t N = reader->Available();
1808
+ const size_t uncompressed_size = N;
1809
+ char ulength[Varint::kMax32];
1810
+ char* p = Varint::Encode32(ulength, N);
1811
+ writer->Append(ulength, p - ulength);
1812
+ written += (p - ulength);
1813
+
1814
+ internal::WorkingMemory wmem(N);
1815
+
1816
+ while (N > 0) {
1817
+ // Get next block to compress (without copying if possible)
1818
+ size_t fragment_size;
1819
+ const char* fragment = reader->Peek(&fragment_size);
1820
+ assert(fragment_size != 0); // premature end of input
1821
+ const size_t num_to_read = std::min(N, kBlockSize);
1822
+ size_t bytes_read = fragment_size;
1823
+
1824
+ size_t pending_advance = 0;
1825
+ if (bytes_read >= num_to_read) {
1826
+ // Buffer returned by reader is large enough
1827
+ pending_advance = num_to_read;
1828
+ fragment_size = num_to_read;
1829
+ } else {
1830
+ char* scratch = wmem.GetScratchInput();
1831
+ std::memcpy(scratch, fragment, bytes_read);
1832
+ reader->Skip(bytes_read);
1833
+
1834
+ while (bytes_read < num_to_read) {
1835
+ fragment = reader->Peek(&fragment_size);
1836
+ size_t n = std::min<size_t>(fragment_size, num_to_read - bytes_read);
1837
+ std::memcpy(scratch + bytes_read, fragment, n);
1838
+ bytes_read += n;
1839
+ reader->Skip(n);
1840
+ }
1841
+ assert(bytes_read == num_to_read);
1842
+ fragment = scratch;
1843
+ fragment_size = num_to_read;
1844
+ }
1845
+ assert(fragment_size == num_to_read);
1846
+
1847
+ // Get encoding table for compression
1848
+ int table_size;
1849
+ uint16_t* table = wmem.GetHashTable(num_to_read, &table_size);
1850
+
1851
+ // Compress input_fragment and append to dest
1852
+ int max_output = MaxCompressedLength(num_to_read);
1853
+
1854
+ // Since we encode kBlockSize regions followed by a region
1855
+ // which is <= kBlockSize in length, a previously allocated
1856
+ // scratch_output[] region is big enough for this iteration.
1857
+ // Need a scratch buffer for the output, in case the byte sink doesn't
1858
+ // have room for us directly.
1859
+ char* dest = writer->GetAppendBuffer(max_output, wmem.GetScratchOutput());
1860
+ char* end = nullptr;
1861
+ if (options.level == 1) {
1862
+ end = internal::CompressFragment(fragment, fragment_size, dest, table,
1863
+ table_size);
1864
+ } else if (options.level == 2) {
1865
+ end = internal::CompressFragmentDoubleHash(
1866
+ fragment, fragment_size, dest, table, table_size >> 1,
1867
+ table + (table_size >> 1), table_size >> 1);
1868
+ }
1869
+ writer->Append(dest, end - dest);
1870
+ written += (end - dest);
1871
+
1872
+ N -= num_to_read;
1873
+ reader->Skip(pending_advance);
1874
+ }
1875
+
1876
+ Report(token, "snappy_compress", written, uncompressed_size);
1877
+ return written;
1878
+ }
1879
+
1880
+ // -----------------------------------------------------------------------
1881
+ // IOVec interfaces
1882
+ // -----------------------------------------------------------------------
1883
+
1884
+ // A `Source` implementation that yields the contents of an `iovec` array. Note
1885
+ // that `total_size` is the total number of bytes to be read from the elements
1886
+ // of `iov` (_not_ the total number of elements in `iov`).
1887
+ class SnappyIOVecReader : public Source {
1888
+ public:
1889
+ SnappyIOVecReader(const struct iovec* iov, size_t total_size)
1890
+ : curr_iov_(iov),
1891
+ curr_pos_(total_size > 0 ? reinterpret_cast<const char*>(iov->iov_base)
1892
+ : nullptr),
1893
+ curr_size_remaining_(total_size > 0 ? iov->iov_len : 0),
1894
+ total_size_remaining_(total_size) {
1895
+ // Skip empty leading `iovec`s.
1896
+ if (total_size > 0 && curr_size_remaining_ == 0) Advance();
1897
+ }
1898
+
1899
+ ~SnappyIOVecReader() override = default;
1900
+
1901
+ size_t Available() const override { return total_size_remaining_; }
1902
+
1903
+ const char* Peek(size_t* len) override {
1904
+ *len = curr_size_remaining_;
1905
+ return curr_pos_;
1906
+ }
1907
+
1908
+ void Skip(size_t n) override {
1909
+ while (n >= curr_size_remaining_ && n > 0) {
1910
+ n -= curr_size_remaining_;
1911
+ Advance();
1912
+ }
1913
+ curr_size_remaining_ -= n;
1914
+ total_size_remaining_ -= n;
1915
+ curr_pos_ += n;
1916
+ }
1917
+
1918
+ private:
1919
+ // Advances to the next nonempty `iovec` and updates related variables.
1920
+ void Advance() {
1921
+ do {
1922
+ assert(total_size_remaining_ >= curr_size_remaining_);
1923
+ total_size_remaining_ -= curr_size_remaining_;
1924
+ if (total_size_remaining_ == 0) {
1925
+ curr_pos_ = nullptr;
1926
+ curr_size_remaining_ = 0;
1927
+ return;
1928
+ }
1929
+ ++curr_iov_;
1930
+ curr_pos_ = reinterpret_cast<const char*>(curr_iov_->iov_base);
1931
+ curr_size_remaining_ = curr_iov_->iov_len;
1932
+ } while (curr_size_remaining_ == 0);
1933
+ }
1934
+
1935
+ // The `iovec` currently being read.
1936
+ const struct iovec* curr_iov_;
1937
+ // The location in `curr_iov_` currently being read.
1938
+ const char* curr_pos_;
1939
+ // The amount of unread data in `curr_iov_`.
1940
+ size_t curr_size_remaining_;
1941
+ // The amount of unread data in the entire input array.
1942
+ size_t total_size_remaining_;
1943
+ };
1944
+
1945
+ // A type that writes to an iovec.
1946
+ // Note that this is not a "ByteSink", but a type that matches the
1947
+ // Writer template argument to SnappyDecompressor::DecompressAllTags().
1948
+ class SnappyIOVecWriter {
1949
+ private:
1950
+ // output_iov_end_ is set to iov + count and used to determine when
1951
+ // the end of the iovs is reached.
1952
+ const struct iovec* output_iov_end_;
1953
+
1954
+ #if !defined(NDEBUG)
1955
+ const struct iovec* output_iov_;
1956
+ #endif // !defined(NDEBUG)
1957
+
1958
+ // Current iov that is being written into.
1959
+ const struct iovec* curr_iov_;
1960
+
1961
+ // Pointer to current iov's write location.
1962
+ char* curr_iov_output_;
1963
+
1964
+ // Remaining bytes to write into curr_iov_output.
1965
+ size_t curr_iov_remaining_;
1966
+
1967
+ // Total bytes decompressed into output_iov_ so far.
1968
+ size_t total_written_;
1969
+
1970
+ // Maximum number of bytes that will be decompressed into output_iov_.
1971
+ size_t output_limit_;
1972
+
1973
+ static inline char* GetIOVecPointer(const struct iovec* iov, size_t offset) {
1974
+ return reinterpret_cast<char*>(iov->iov_base) + offset;
1975
+ }
1976
+
1977
+ public:
1978
+ // Does not take ownership of iov. iov must be valid during the
1979
+ // entire lifetime of the SnappyIOVecWriter.
1980
+ inline SnappyIOVecWriter(const struct iovec* iov, size_t iov_count)
1981
+ : output_iov_end_(iov + iov_count),
1982
+ #if !defined(NDEBUG)
1983
+ output_iov_(iov),
1984
+ #endif // !defined(NDEBUG)
1985
+ curr_iov_(iov),
1986
+ curr_iov_output_(iov_count ? reinterpret_cast<char*>(iov->iov_base)
1987
+ : nullptr),
1988
+ curr_iov_remaining_(iov_count ? iov->iov_len : 0),
1989
+ total_written_(0),
1990
+ output_limit_(-1) {
1991
+ }
1992
+
1993
+ inline void SetExpectedLength(size_t len) { output_limit_ = len; }
1994
+
1995
+ inline bool CheckLength() const { return total_written_ == output_limit_; }
1996
+
1997
+ inline bool Append(const char* ip, size_t len, char**) {
1998
+ if (total_written_ + len > output_limit_) {
1999
+ return false;
2000
+ }
2001
+
2002
+ return AppendNoCheck(ip, len);
2003
+ }
2004
+
2005
+ char* GetOutputPtr() { return nullptr; }
2006
+ char* GetBase(ptrdiff_t*) { return nullptr; }
2007
+ void SetOutputPtr(char* op) {
2008
+ // TODO: Switch to [[maybe_unused]] when we can assume C++17.
2009
+ (void)op;
2010
+ }
2011
+
2012
+ inline bool AppendNoCheck(const char* ip, size_t len) {
2013
+ while (len > 0) {
2014
+ if (curr_iov_remaining_ == 0) {
2015
+ // This iovec is full. Go to the next one.
2016
+ if (curr_iov_ + 1 >= output_iov_end_) {
2017
+ return false;
2018
+ }
2019
+ ++curr_iov_;
2020
+ curr_iov_output_ = reinterpret_cast<char*>(curr_iov_->iov_base);
2021
+ curr_iov_remaining_ = curr_iov_->iov_len;
2022
+ }
2023
+
2024
+ const size_t to_write = std::min(len, curr_iov_remaining_);
2025
+ std::memcpy(curr_iov_output_, ip, to_write);
2026
+ curr_iov_output_ += to_write;
2027
+ curr_iov_remaining_ -= to_write;
2028
+ total_written_ += to_write;
2029
+ ip += to_write;
2030
+ len -= to_write;
2031
+ }
2032
+
2033
+ return true;
2034
+ }
2035
+
2036
+ inline bool TryFastAppend(const char* ip, size_t available, size_t len,
2037
+ char**) {
2038
+ const size_t space_left = output_limit_ - total_written_;
2039
+ if (len <= 16 && available >= 16 + kMaximumTagLength && space_left >= 16 &&
2040
+ curr_iov_remaining_ >= 16) {
2041
+ // Fast path, used for the majority (about 95%) of invocations.
2042
+ UnalignedCopy128(ip, curr_iov_output_);
2043
+ curr_iov_output_ += len;
2044
+ curr_iov_remaining_ -= len;
2045
+ total_written_ += len;
2046
+ return true;
2047
+ }
2048
+
2049
+ return false;
2050
+ }
2051
+
2052
+ inline bool AppendFromSelf(size_t offset, size_t len, char**) {
2053
+ // See SnappyArrayWriter::AppendFromSelf for an explanation of
2054
+ // the "offset - 1u" trick.
2055
+ if (offset - 1u >= total_written_) {
2056
+ return false;
2057
+ }
2058
+ const size_t space_left = output_limit_ - total_written_;
2059
+ if (len > space_left) {
2060
+ return false;
2061
+ }
2062
+
2063
+ // Locate the iovec from which we need to start the copy.
2064
+ const iovec* from_iov = curr_iov_;
2065
+ size_t from_iov_offset = curr_iov_->iov_len - curr_iov_remaining_;
2066
+ while (offset > 0) {
2067
+ if (from_iov_offset >= offset) {
2068
+ from_iov_offset -= offset;
2069
+ break;
2070
+ }
2071
+
2072
+ offset -= from_iov_offset;
2073
+ --from_iov;
2074
+ #if !defined(NDEBUG)
2075
+ assert(from_iov >= output_iov_);
2076
+ #endif // !defined(NDEBUG)
2077
+ from_iov_offset = from_iov->iov_len;
2078
+ }
2079
+
2080
+ // Copy <len> bytes starting from the iovec pointed to by from_iov_index to
2081
+ // the current iovec.
2082
+ while (len > 0) {
2083
+ assert(from_iov <= curr_iov_);
2084
+ if (from_iov != curr_iov_) {
2085
+ const size_t to_copy =
2086
+ std::min(from_iov->iov_len - from_iov_offset, len);
2087
+ AppendNoCheck(GetIOVecPointer(from_iov, from_iov_offset), to_copy);
2088
+ len -= to_copy;
2089
+ if (len > 0) {
2090
+ ++from_iov;
2091
+ from_iov_offset = 0;
2092
+ }
2093
+ } else {
2094
+ size_t to_copy = curr_iov_remaining_;
2095
+ if (to_copy == 0) {
2096
+ // This iovec is full. Go to the next one.
2097
+ if (curr_iov_ + 1 >= output_iov_end_) {
2098
+ return false;
2099
+ }
2100
+ ++curr_iov_;
2101
+ curr_iov_output_ = reinterpret_cast<char*>(curr_iov_->iov_base);
2102
+ curr_iov_remaining_ = curr_iov_->iov_len;
2103
+ continue;
2104
+ }
2105
+ if (to_copy > len) {
2106
+ to_copy = len;
2107
+ }
2108
+ assert(to_copy > 0);
2109
+
2110
+ IncrementalCopy(GetIOVecPointer(from_iov, from_iov_offset),
2111
+ curr_iov_output_, curr_iov_output_ + to_copy,
2112
+ curr_iov_output_ + curr_iov_remaining_);
2113
+ curr_iov_output_ += to_copy;
2114
+ curr_iov_remaining_ -= to_copy;
2115
+ from_iov_offset += to_copy;
2116
+ total_written_ += to_copy;
2117
+ len -= to_copy;
2118
+ }
2119
+ }
2120
+
2121
+ return true;
2122
+ }
2123
+
2124
+ inline void Flush() {}
2125
+ };
2126
+
2127
+ bool RawUncompressToIOVec(const char* compressed, size_t compressed_length,
2128
+ const struct iovec* iov, size_t iov_cnt) {
2129
+ ByteArraySource reader(compressed, compressed_length);
2130
+ return RawUncompressToIOVec(&reader, iov, iov_cnt);
2131
+ }
2132
+
2133
+ bool RawUncompressToIOVec(Source* compressed, const struct iovec* iov,
2134
+ size_t iov_cnt) {
2135
+ SnappyIOVecWriter output(iov, iov_cnt);
2136
+ return InternalUncompress(compressed, &output);
2137
+ }
2138
+
2139
+ // -----------------------------------------------------------------------
2140
+ // Flat array interfaces
2141
+ // -----------------------------------------------------------------------
2142
+
2143
+ // A type that writes to a flat array.
2144
+ // Note that this is not a "ByteSink", but a type that matches the
2145
+ // Writer template argument to SnappyDecompressor::DecompressAllTags().
2146
+ class SnappyArrayWriter {
2147
+ private:
2148
+ char* base_;
2149
+ char* op_;
2150
+ char* op_limit_;
2151
+ // If op < op_limit_min_slop_ then it's safe to unconditionally write
2152
+ // kSlopBytes starting at op.
2153
+ char* op_limit_min_slop_;
2154
+
2155
+ public:
2156
+ inline explicit SnappyArrayWriter(char* dst)
2157
+ : base_(dst),
2158
+ op_(dst),
2159
+ op_limit_(dst),
2160
+ op_limit_min_slop_(dst) {} // Safe default see invariant.
2161
+
2162
+ inline void SetExpectedLength(size_t len) {
2163
+ op_limit_ = op_ + len;
2164
+ // Prevent pointer from being past the buffer.
2165
+ op_limit_min_slop_ = op_limit_ - std::min<size_t>(kSlopBytes - 1, len);
2166
+ }
2167
+
2168
+ inline bool CheckLength() const { return op_ == op_limit_; }
2169
+
2170
+ char* GetOutputPtr() { return op_; }
2171
+ char* GetBase(ptrdiff_t* op_limit_min_slop) {
2172
+ *op_limit_min_slop = op_limit_min_slop_ - base_;
2173
+ return base_;
2174
+ }
2175
+ void SetOutputPtr(char* op) { op_ = op; }
2176
+
2177
+ inline bool Append(const char* ip, size_t len, char** op_p) {
2178
+ char* op = *op_p;
2179
+ const size_t space_left = op_limit_ - op;
2180
+ if (space_left < len) return false;
2181
+ std::memcpy(op, ip, len);
2182
+ *op_p = op + len;
2183
+ return true;
2184
+ }
2185
+
2186
+ inline bool TryFastAppend(const char* ip, size_t available, size_t len,
2187
+ char** op_p) {
2188
+ char* op = *op_p;
2189
+ const size_t space_left = op_limit_ - op;
2190
+ if (len <= 16 && available >= 16 + kMaximumTagLength && space_left >= 16) {
2191
+ // Fast path, used for the majority (about 95%) of invocations.
2192
+ UnalignedCopy128(ip, op);
2193
+ *op_p = op + len;
2194
+ return true;
2195
+ } else {
2196
+ return false;
2197
+ }
2198
+ }
2199
+
2200
+ SNAPPY_ATTRIBUTE_ALWAYS_INLINE
2201
+ inline bool AppendFromSelf(size_t offset, size_t len, char** op_p) {
2202
+ assert(len > 0);
2203
+ char* const op = *op_p;
2204
+ assert(op >= base_);
2205
+ char* const op_end = op + len;
2206
+
2207
+ // Check if we try to append from before the start of the buffer.
2208
+ if (SNAPPY_PREDICT_FALSE(static_cast<size_t>(op - base_) < offset))
2209
+ return false;
2210
+
2211
+ if (SNAPPY_PREDICT_FALSE((kSlopBytes < 64 && len > kSlopBytes) ||
2212
+ op >= op_limit_min_slop_ || offset < len)) {
2213
+ if (op_end > op_limit_ || offset == 0) return false;
2214
+ *op_p = IncrementalCopy(op - offset, op, op_end, op_limit_);
2215
+ return true;
2216
+ }
2217
+ std::memmove(op, op - offset, kSlopBytes);
2218
+ *op_p = op_end;
2219
+ return true;
2220
+ }
2221
+ inline size_t Produced() const {
2222
+ assert(op_ >= base_);
2223
+ return op_ - base_;
2224
+ }
2225
+ inline void Flush() {}
2226
+ };
2227
+
2228
+ bool RawUncompress(const char* compressed, size_t compressed_length,
2229
+ char* uncompressed) {
2230
+ ByteArraySource reader(compressed, compressed_length);
2231
+ return RawUncompress(&reader, uncompressed);
2232
+ }
2233
+
2234
+ bool RawUncompress(Source* compressed, char* uncompressed) {
2235
+ SnappyArrayWriter output(uncompressed);
2236
+ return InternalUncompress(compressed, &output);
2237
+ }
2238
+
2239
+ bool Uncompress(const char* compressed, size_t compressed_length,
2240
+ std::string* uncompressed) {
2241
+ size_t ulength;
2242
+ if (!GetUncompressedLength(compressed, compressed_length, &ulength)) {
2243
+ return false;
2244
+ }
2245
+ // On 32-bit builds: max_size() < kuint32max. Check for that instead
2246
+ // of crashing (e.g., consider externally specified compressed data).
2247
+ if (ulength > uncompressed->max_size()) {
2248
+ return false;
2249
+ }
2250
+ STLStringResizeUninitialized(uncompressed, ulength);
2251
+ return RawUncompress(compressed, compressed_length,
2252
+ string_as_array(uncompressed));
2253
+ }
2254
+
2255
+ // A Writer that drops everything on the floor and just does validation
2256
+ class SnappyDecompressionValidator {
2257
+ private:
2258
+ size_t expected_;
2259
+ size_t produced_;
2260
+
2261
+ public:
2262
+ inline SnappyDecompressionValidator() : expected_(0), produced_(0) {}
2263
+ inline void SetExpectedLength(size_t len) { expected_ = len; }
2264
+ size_t GetOutputPtr() { return produced_; }
2265
+ size_t GetBase(ptrdiff_t* op_limit_min_slop) {
2266
+ *op_limit_min_slop = std::numeric_limits<ptrdiff_t>::max() - kSlopBytes + 1;
2267
+ return 1;
2268
+ }
2269
+ void SetOutputPtr(size_t op) { produced_ = op; }
2270
+ inline bool CheckLength() const { return expected_ == produced_; }
2271
+ inline bool Append(const char* ip, size_t len, size_t* produced) {
2272
+ // TODO: Switch to [[maybe_unused]] when we can assume C++17.
2273
+ (void)ip;
2274
+
2275
+ *produced += len;
2276
+ return *produced <= expected_;
2277
+ }
2278
+ inline bool TryFastAppend(const char* ip, size_t available, size_t length,
2279
+ size_t* produced) {
2280
+ // TODO: Switch to [[maybe_unused]] when we can assume C++17.
2281
+ (void)ip;
2282
+ (void)available;
2283
+ (void)length;
2284
+ (void)produced;
2285
+
2286
+ return false;
2287
+ }
2288
+ inline bool AppendFromSelf(size_t offset, size_t len, size_t* produced) {
2289
+ // See SnappyArrayWriter::AppendFromSelf for an explanation of
2290
+ // the "offset - 1u" trick.
2291
+ if (*produced <= offset - 1u) return false;
2292
+ *produced += len;
2293
+ return *produced <= expected_;
2294
+ }
2295
+ inline void Flush() {}
2296
+ };
2297
+
2298
+ bool IsValidCompressedBuffer(const char* compressed, size_t compressed_length) {
2299
+ ByteArraySource reader(compressed, compressed_length);
2300
+ SnappyDecompressionValidator writer;
2301
+ return InternalUncompress(&reader, &writer);
2302
+ }
2303
+
2304
+ bool IsValidCompressed(Source* compressed) {
2305
+ SnappyDecompressionValidator writer;
2306
+ return InternalUncompress(compressed, &writer);
2307
+ }
2308
+
2309
+ void RawCompress(const char* input, size_t input_length, char* compressed,
2310
+ size_t* compressed_length) {
2311
+ RawCompress(input, input_length, compressed, compressed_length,
2312
+ CompressionOptions{});
2313
+ }
2314
+
2315
+ void RawCompress(const char* input, size_t input_length, char* compressed,
2316
+ size_t* compressed_length, CompressionOptions options) {
2317
+ ByteArraySource reader(input, input_length);
2318
+ UncheckedByteArraySink writer(compressed);
2319
+ Compress(&reader, &writer, options);
2320
+
2321
+ // Compute how many bytes were added
2322
+ *compressed_length = (writer.CurrentDestination() - compressed);
2323
+ }
2324
+
2325
+ void RawCompressFromIOVec(const struct iovec* iov, size_t uncompressed_length,
2326
+ char* compressed, size_t* compressed_length) {
2327
+ RawCompressFromIOVec(iov, uncompressed_length, compressed, compressed_length,
2328
+ CompressionOptions{});
2329
+ }
2330
+
2331
+ void RawCompressFromIOVec(const struct iovec* iov, size_t uncompressed_length,
2332
+ char* compressed, size_t* compressed_length,
2333
+ CompressionOptions options) {
2334
+ SnappyIOVecReader reader(iov, uncompressed_length);
2335
+ UncheckedByteArraySink writer(compressed);
2336
+ Compress(&reader, &writer, options);
2337
+
2338
+ // Compute how many bytes were added.
2339
+ *compressed_length = writer.CurrentDestination() - compressed;
2340
+ }
2341
+
2342
+ size_t Compress(const char* input, size_t input_length,
2343
+ std::string* compressed) {
2344
+ return Compress(input, input_length, compressed, CompressionOptions{});
2345
+ }
2346
+
2347
+ size_t Compress(const char* input, size_t input_length, std::string* compressed,
2348
+ CompressionOptions options) {
2349
+ // Pre-grow the buffer to the max length of the compressed output
2350
+ STLStringResizeUninitialized(compressed, MaxCompressedLength(input_length));
2351
+
2352
+ size_t compressed_length;
2353
+ RawCompress(input, input_length, string_as_array(compressed),
2354
+ &compressed_length, options);
2355
+ compressed->erase(compressed_length);
2356
+ return compressed_length;
2357
+ }
2358
+
2359
+ size_t CompressFromIOVec(const struct iovec* iov, size_t iov_cnt,
2360
+ std::string* compressed) {
2361
+ return CompressFromIOVec(iov, iov_cnt, compressed, CompressionOptions{});
2362
+ }
2363
+
2364
+ size_t CompressFromIOVec(const struct iovec* iov, size_t iov_cnt,
2365
+ std::string* compressed, CompressionOptions options) {
2366
+ // Compute the number of bytes to be compressed.
2367
+ size_t uncompressed_length = 0;
2368
+ for (size_t i = 0; i < iov_cnt; ++i) {
2369
+ uncompressed_length += iov[i].iov_len;
2370
+ }
2371
+
2372
+ // Pre-grow the buffer to the max length of the compressed output.
2373
+ STLStringResizeUninitialized(compressed, MaxCompressedLength(
2374
+ uncompressed_length));
2375
+
2376
+ size_t compressed_length;
2377
+ RawCompressFromIOVec(iov, uncompressed_length, string_as_array(compressed),
2378
+ &compressed_length, options);
2379
+ compressed->erase(compressed_length);
2380
+ return compressed_length;
2381
+ }
2382
+
2383
+ // -----------------------------------------------------------------------
2384
+ // Sink interface
2385
+ // -----------------------------------------------------------------------
2386
+
2387
+ // A type that decompresses into a Sink. The template parameter
2388
+ // Allocator must export one method "char* Allocate(int size);", which
2389
+ // allocates a buffer of "size" and appends that to the destination.
2390
+ template <typename Allocator>
2391
+ class SnappyScatteredWriter {
2392
+ Allocator allocator_;
2393
+
2394
+ // We need random access into the data generated so far. Therefore
2395
+ // we keep track of all of the generated data as an array of blocks.
2396
+ // All of the blocks except the last have length kBlockSize.
2397
+ std::vector<char*> blocks_;
2398
+ size_t expected_;
2399
+
2400
+ // Total size of all fully generated blocks so far
2401
+ size_t full_size_;
2402
+
2403
+ // Pointer into current output block
2404
+ char* op_base_; // Base of output block
2405
+ char* op_ptr_; // Pointer to next unfilled byte in block
2406
+ char* op_limit_; // Pointer just past block
2407
+ // If op < op_limit_min_slop_ then it's safe to unconditionally write
2408
+ // kSlopBytes starting at op.
2409
+ char* op_limit_min_slop_;
2410
+
2411
+ inline size_t Size() const { return full_size_ + (op_ptr_ - op_base_); }
2412
+
2413
+ bool SlowAppend(const char* ip, size_t len);
2414
+ bool SlowAppendFromSelf(size_t offset, size_t len);
2415
+
2416
+ public:
2417
+ inline explicit SnappyScatteredWriter(const Allocator& allocator)
2418
+ : allocator_(allocator),
2419
+ full_size_(0),
2420
+ op_base_(NULL),
2421
+ op_ptr_(NULL),
2422
+ op_limit_(NULL),
2423
+ op_limit_min_slop_(NULL) {}
2424
+ char* GetOutputPtr() { return op_ptr_; }
2425
+ char* GetBase(ptrdiff_t* op_limit_min_slop) {
2426
+ *op_limit_min_slop = op_limit_min_slop_ - op_base_;
2427
+ return op_base_;
2428
+ }
2429
+ void SetOutputPtr(char* op) { op_ptr_ = op; }
2430
+
2431
+ inline void SetExpectedLength(size_t len) {
2432
+ assert(blocks_.empty());
2433
+ expected_ = len;
2434
+ }
2435
+
2436
+ inline bool CheckLength() const { return Size() == expected_; }
2437
+
2438
+ // Return the number of bytes actually uncompressed so far
2439
+ inline size_t Produced() const { return Size(); }
2440
+
2441
+ inline bool Append(const char* ip, size_t len, char** op_p) {
2442
+ char* op = *op_p;
2443
+ size_t avail = op_limit_ - op;
2444
+ if (len <= avail) {
2445
+ // Fast path
2446
+ std::memcpy(op, ip, len);
2447
+ *op_p = op + len;
2448
+ return true;
2449
+ } else {
2450
+ op_ptr_ = op;
2451
+ bool res = SlowAppend(ip, len);
2452
+ *op_p = op_ptr_;
2453
+ return res;
2454
+ }
2455
+ }
2456
+
2457
+ inline bool TryFastAppend(const char* ip, size_t available, size_t length,
2458
+ char** op_p) {
2459
+ char* op = *op_p;
2460
+ const int space_left = op_limit_ - op;
2461
+ if (length <= 16 && available >= 16 + kMaximumTagLength &&
2462
+ space_left >= 16) {
2463
+ // Fast path, used for the majority (about 95%) of invocations.
2464
+ UnalignedCopy128(ip, op);
2465
+ *op_p = op + length;
2466
+ return true;
2467
+ } else {
2468
+ return false;
2469
+ }
2470
+ }
2471
+
2472
+ inline bool AppendFromSelf(size_t offset, size_t len, char** op_p) {
2473
+ char* op = *op_p;
2474
+ assert(op >= op_base_);
2475
+ // Check if we try to append from before the start of the buffer.
2476
+ if (SNAPPY_PREDICT_FALSE((kSlopBytes < 64 && len > kSlopBytes) ||
2477
+ static_cast<size_t>(op - op_base_) < offset ||
2478
+ op >= op_limit_min_slop_ || offset < len)) {
2479
+ if (offset == 0) return false;
2480
+ if (SNAPPY_PREDICT_FALSE(static_cast<size_t>(op - op_base_) < offset ||
2481
+ op + len > op_limit_)) {
2482
+ op_ptr_ = op;
2483
+ bool res = SlowAppendFromSelf(offset, len);
2484
+ *op_p = op_ptr_;
2485
+ return res;
2486
+ }
2487
+ *op_p = IncrementalCopy(op - offset, op, op + len, op_limit_);
2488
+ return true;
2489
+ }
2490
+ // Fast path
2491
+ char* const op_end = op + len;
2492
+ std::memmove(op, op - offset, kSlopBytes);
2493
+ *op_p = op_end;
2494
+ return true;
2495
+ }
2496
+
2497
+ // Called at the end of the decompress. We ask the allocator
2498
+ // write all blocks to the sink.
2499
+ inline void Flush() { allocator_.Flush(Produced()); }
2500
+ };
2501
+
2502
+ template <typename Allocator>
2503
+ bool SnappyScatteredWriter<Allocator>::SlowAppend(const char* ip, size_t len) {
2504
+ size_t avail = op_limit_ - op_ptr_;
2505
+ while (len > avail) {
2506
+ // Completely fill this block
2507
+ std::memcpy(op_ptr_, ip, avail);
2508
+ op_ptr_ += avail;
2509
+ assert(op_limit_ - op_ptr_ == 0);
2510
+ full_size_ += (op_ptr_ - op_base_);
2511
+ len -= avail;
2512
+ ip += avail;
2513
+
2514
+ // Bounds check
2515
+ if (full_size_ + len > expected_) return false;
2516
+
2517
+ // Make new block
2518
+ size_t bsize = std::min<size_t>(kBlockSize, expected_ - full_size_);
2519
+ op_base_ = allocator_.Allocate(bsize);
2520
+ op_ptr_ = op_base_;
2521
+ op_limit_ = op_base_ + bsize;
2522
+ op_limit_min_slop_ = op_limit_ - std::min<size_t>(kSlopBytes - 1, bsize);
2523
+
2524
+ blocks_.push_back(op_base_);
2525
+ avail = bsize;
2526
+ }
2527
+
2528
+ std::memcpy(op_ptr_, ip, len);
2529
+ op_ptr_ += len;
2530
+ return true;
2531
+ }
2532
+
2533
+ template <typename Allocator>
2534
+ bool SnappyScatteredWriter<Allocator>::SlowAppendFromSelf(size_t offset,
2535
+ size_t len) {
2536
+ // Overflow check
2537
+ // See SnappyArrayWriter::AppendFromSelf for an explanation of
2538
+ // the "offset - 1u" trick.
2539
+ const size_t cur = Size();
2540
+ if (offset - 1u >= cur) return false;
2541
+ if (expected_ - cur < len) return false;
2542
+
2543
+ // Currently we shouldn't ever hit this path because Compress() chops the
2544
+ // input into blocks and does not create cross-block copies. However, it is
2545
+ // nice if we do not rely on that, since we can get better compression if we
2546
+ // allow cross-block copies and thus might want to change the compressor in
2547
+ // the future.
2548
+ // TODO Replace this with a properly optimized path. This is not
2549
+ // triggered right now. But this is so super slow, that it would regress
2550
+ // performance unacceptably if triggered.
2551
+ size_t src = cur - offset;
2552
+ char* op = op_ptr_;
2553
+ while (len-- > 0) {
2554
+ char c = blocks_[src >> kBlockLog][src & (kBlockSize - 1)];
2555
+ if (!Append(&c, 1, &op)) {
2556
+ op_ptr_ = op;
2557
+ return false;
2558
+ }
2559
+ src++;
2560
+ }
2561
+ op_ptr_ = op;
2562
+ return true;
2563
+ }
2564
+
2565
+ class SnappySinkAllocator {
2566
+ public:
2567
+ explicit SnappySinkAllocator(Sink* dest) : dest_(dest) {}
2568
+
2569
+ char* Allocate(int size) {
2570
+ Datablock block(new char[size], size);
2571
+ blocks_.push_back(block);
2572
+ return block.data;
2573
+ }
2574
+
2575
+ // We flush only at the end, because the writer wants
2576
+ // random access to the blocks and once we hand the
2577
+ // block over to the sink, we can't access it anymore.
2578
+ // Also we don't write more than has been actually written
2579
+ // to the blocks.
2580
+ void Flush(size_t size) {
2581
+ size_t size_written = 0;
2582
+ for (Datablock& block : blocks_) {
2583
+ size_t block_size = std::min<size_t>(block.size, size - size_written);
2584
+ dest_->AppendAndTakeOwnership(block.data, block_size,
2585
+ &SnappySinkAllocator::Deleter, NULL);
2586
+ size_written += block_size;
2587
+ }
2588
+ blocks_.clear();
2589
+ }
2590
+
2591
+ private:
2592
+ struct Datablock {
2593
+ char* data;
2594
+ size_t size;
2595
+ Datablock(char* p, size_t s) : data(p), size(s) {}
2596
+ };
2597
+
2598
+ static void Deleter(void* arg, const char* bytes, size_t size) {
2599
+ // TODO: Switch to [[maybe_unused]] when we can assume C++17.
2600
+ (void)arg;
2601
+ (void)size;
2602
+
2603
+ delete[] bytes;
2604
+ }
2605
+
2606
+ Sink* dest_;
2607
+ std::vector<Datablock> blocks_;
2608
+
2609
+ // Note: copying this object is allowed
2610
+ };
2611
+
2612
+ size_t UncompressAsMuchAsPossible(Source* compressed, Sink* uncompressed) {
2613
+ SnappySinkAllocator allocator(uncompressed);
2614
+ SnappyScatteredWriter<SnappySinkAllocator> writer(allocator);
2615
+ InternalUncompress(compressed, &writer);
2616
+ return writer.Produced();
2617
+ }
2618
+
2619
+ bool Uncompress(Source* compressed, Sink* uncompressed) {
2620
+ // Read the uncompressed length from the front of the compressed input
2621
+ SnappyDecompressor decompressor(compressed);
2622
+ uint32_t uncompressed_len = 0;
2623
+ if (!decompressor.ReadUncompressedLength(&uncompressed_len)) {
2624
+ return false;
2625
+ }
2626
+
2627
+ char c;
2628
+ size_t allocated_size;
2629
+ char* buf = uncompressed->GetAppendBufferVariable(1, uncompressed_len, &c, 1,
2630
+ &allocated_size);
2631
+
2632
+ const size_t compressed_len = compressed->Available();
2633
+ // If we can get a flat buffer, then use it, otherwise do block by block
2634
+ // uncompression
2635
+ if (allocated_size >= uncompressed_len) {
2636
+ SnappyArrayWriter writer(buf);
2637
+ bool result = InternalUncompressAllTags(&decompressor, &writer,
2638
+ compressed_len, uncompressed_len);
2639
+ uncompressed->Append(buf, writer.Produced());
2640
+ return result;
2641
+ } else {
2642
+ SnappySinkAllocator allocator(uncompressed);
2643
+ SnappyScatteredWriter<SnappySinkAllocator> writer(allocator);
2644
+ return InternalUncompressAllTags(&decompressor, &writer, compressed_len,
2645
+ uncompressed_len);
2646
+ }
2647
+ }
2648
+
2649
+ } // namespace duckdb_snappy
2650
+
2651
+ #else // #if SNAPPY_NEW_VERSION
2652
+
29
2653
  #include "snappy.h"
30
2654
  #include "snappy-internal.h"
31
2655
  #include "snappy-sinksource.h"
@@ -1660,3 +4284,5 @@ bool Uncompress(Source* compressed, Sink* uncompressed) {
1660
4284
  }
1661
4285
 
1662
4286
  } // namespace duckdb_snappy
4287
+
4288
+ #endif // #if SNAPPY_NEW_VERSION # else