@lix-js/sdk 0.6.0-preview.5 → 0.6.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 (274) hide show
  1. package/README.md +76 -4
  2. package/dist/errors.d.ts +7 -0
  3. package/dist/errors.js +19 -0
  4. package/dist/index.d.ts +4 -5
  5. package/dist/index.js +3 -3
  6. package/dist/native.d.ts +1 -0
  7. package/dist/native.js +47 -0
  8. package/dist/open-lix.d.ts +38 -207
  9. package/dist/open-lix.js +59 -284
  10. package/dist/result.d.ts +18 -0
  11. package/dist/result.js +48 -0
  12. package/dist/types.d.ts +114 -1
  13. package/dist/value.d.ts +28 -0
  14. package/dist/value.js +245 -0
  15. package/package.json +38 -71
  16. package/SKILL.md +0 -507
  17. package/dist/builtin-schemas.d.ts +0 -1
  18. package/dist/builtin-schemas.js +0 -1
  19. package/dist/engine-wasm/index.d.ts +0 -87
  20. package/dist/engine-wasm/index.js +0 -339
  21. package/dist/engine-wasm/wasm/lix_engine.d.ts +0 -79
  22. package/dist/engine-wasm/wasm/lix_engine.js +0 -833
  23. package/dist/engine-wasm/wasm/lix_engine.wasm +0 -0
  24. package/dist/engine-wasm/wasm/lix_engine.wasm.d.ts +0 -27
  25. package/dist/generated/builtin-schemas.d.ts +0 -427
  26. package/dist/generated/builtin-schemas.js +0 -643
  27. package/dist/sqlite/index.d.ts +0 -12
  28. package/dist/sqlite/index.js +0 -359
  29. package/dist-engine-src/README.md +0 -18
  30. package/dist-engine-src/src/backend/capabilities.rs +0 -67
  31. package/dist-engine-src/src/backend/conformance/baseline.rs +0 -1127
  32. package/dist-engine-src/src/backend/conformance/factory.rs +0 -93
  33. package/dist-engine-src/src/backend/conformance/failure_tests.rs +0 -608
  34. package/dist-engine-src/src/backend/conformance/fixtures.rs +0 -26
  35. package/dist-engine-src/src/backend/conformance/mod.rs +0 -75
  36. package/dist-engine-src/src/backend/conformance/model.rs +0 -28
  37. package/dist-engine-src/src/backend/conformance/model_based.rs +0 -257
  38. package/dist-engine-src/src/backend/conformance/persistence.rs +0 -204
  39. package/dist-engine-src/src/backend/conformance/projection.rs +0 -21
  40. package/dist-engine-src/src/backend/conformance/pushdown.rs +0 -24
  41. package/dist-engine-src/src/backend/conformance/runner.rs +0 -90
  42. package/dist-engine-src/src/backend/conformance/scan.rs +0 -24
  43. package/dist-engine-src/src/backend/conformance/write.rs +0 -16
  44. package/dist-engine-src/src/backend/error.rs +0 -94
  45. package/dist-engine-src/src/backend/in_memory.rs +0 -670
  46. package/dist-engine-src/src/backend/mod.rs +0 -39
  47. package/dist-engine-src/src/backend/predicate.rs +0 -80
  48. package/dist-engine-src/src/backend/traits.rs +0 -260
  49. package/dist-engine-src/src/backend/types.rs +0 -239
  50. package/dist-engine-src/src/binary_cas/chunking.rs +0 -31
  51. package/dist-engine-src/src/binary_cas/codec.rs +0 -346
  52. package/dist-engine-src/src/binary_cas/context.rs +0 -139
  53. package/dist-engine-src/src/binary_cas/kv.rs +0 -1038
  54. package/dist-engine-src/src/binary_cas/mod.rs +0 -11
  55. package/dist-engine-src/src/binary_cas/types.rs +0 -121
  56. package/dist-engine-src/src/branch/context.rs +0 -40
  57. package/dist-engine-src/src/branch/lifecycle.rs +0 -221
  58. package/dist-engine-src/src/branch/mod.rs +0 -13
  59. package/dist-engine-src/src/branch/refs.rs +0 -321
  60. package/dist-engine-src/src/branch/stage_rows.rs +0 -67
  61. package/dist-engine-src/src/branch/types.rs +0 -21
  62. package/dist-engine-src/src/catalog/context.rs +0 -412
  63. package/dist-engine-src/src/catalog/mod.rs +0 -10
  64. package/dist-engine-src/src/catalog/schema.rs +0 -4
  65. package/dist-engine-src/src/catalog/snapshot.rs +0 -1114
  66. package/dist-engine-src/src/cel/context.rs +0 -86
  67. package/dist-engine-src/src/cel/error.rs +0 -19
  68. package/dist-engine-src/src/cel/mod.rs +0 -8
  69. package/dist-engine-src/src/cel/provider.rs +0 -9
  70. package/dist-engine-src/src/cel/runtime.rs +0 -167
  71. package/dist-engine-src/src/cel/value.rs +0 -50
  72. package/dist-engine-src/src/changelog/bench_support.rs +0 -785
  73. package/dist-engine-src/src/changelog/change.rs +0 -1
  74. package/dist-engine-src/src/changelog/codec.rs +0 -497
  75. package/dist-engine-src/src/changelog/commit.rs +0 -1
  76. package/dist-engine-src/src/changelog/context.rs +0 -1614
  77. package/dist-engine-src/src/changelog/mod.rs +0 -29
  78. package/dist-engine-src/src/changelog/store.rs +0 -163
  79. package/dist-engine-src/src/changelog/test_support.rs +0 -54
  80. package/dist-engine-src/src/changelog/types.rs +0 -213
  81. package/dist-engine-src/src/commit_graph/context.rs +0 -944
  82. package/dist-engine-src/src/commit_graph/mod.rs +0 -9
  83. package/dist-engine-src/src/commit_graph/types.rs +0 -89
  84. package/dist-engine-src/src/commit_graph/walker.rs +0 -786
  85. package/dist-engine-src/src/common/error.rs +0 -347
  86. package/dist-engine-src/src/common/fingerprint.rs +0 -3
  87. package/dist-engine-src/src/common/fs_path.rs +0 -1336
  88. package/dist-engine-src/src/common/identity.rs +0 -145
  89. package/dist-engine-src/src/common/json_pointer.rs +0 -67
  90. package/dist-engine-src/src/common/metadata.rs +0 -40
  91. package/dist-engine-src/src/common/mod.rs +0 -23
  92. package/dist-engine-src/src/common/types.rs +0 -105
  93. package/dist-engine-src/src/common/wire.rs +0 -222
  94. package/dist-engine-src/src/domain.rs +0 -320
  95. package/dist-engine-src/src/engine.rs +0 -203
  96. package/dist-engine-src/src/entity_pk.rs +0 -402
  97. package/dist-engine-src/src/functions/context.rs +0 -296
  98. package/dist-engine-src/src/functions/deterministic.rs +0 -113
  99. package/dist-engine-src/src/functions/mod.rs +0 -18
  100. package/dist-engine-src/src/functions/provider.rs +0 -130
  101. package/dist-engine-src/src/functions/state.rs +0 -335
  102. package/dist-engine-src/src/functions/types.rs +0 -37
  103. package/dist-engine-src/src/init.rs +0 -692
  104. package/dist-engine-src/src/json_store/compression.rs +0 -77
  105. package/dist-engine-src/src/json_store/context.rs +0 -172
  106. package/dist-engine-src/src/json_store/encoded.rs +0 -15
  107. package/dist-engine-src/src/json_store/mod.rs +0 -38
  108. package/dist-engine-src/src/json_store/store.rs +0 -494
  109. package/dist-engine-src/src/json_store/types.rs +0 -212
  110. package/dist-engine-src/src/lib.rs +0 -92
  111. package/dist-engine-src/src/live_state/context.rs +0 -1883
  112. package/dist-engine-src/src/live_state/mod.rs +0 -21
  113. package/dist-engine-src/src/live_state/overlay.rs +0 -75
  114. package/dist-engine-src/src/live_state/reader.rs +0 -23
  115. package/dist-engine-src/src/live_state/types.rs +0 -231
  116. package/dist-engine-src/src/live_state/visibility.rs +0 -666
  117. package/dist-engine-src/src/plugin/archive.rs +0 -438
  118. package/dist-engine-src/src/plugin/component.rs +0 -183
  119. package/dist-engine-src/src/plugin/install.rs +0 -619
  120. package/dist-engine-src/src/plugin/manifest.rs +0 -516
  121. package/dist-engine-src/src/plugin/materializer.rs +0 -202
  122. package/dist-engine-src/src/plugin/mod.rs +0 -33
  123. package/dist-engine-src/src/plugin/plugin_manifest.json +0 -119
  124. package/dist-engine-src/src/plugin/storage.rs +0 -74
  125. package/dist-engine-src/src/schema/annotations/defaults.rs +0 -275
  126. package/dist-engine-src/src/schema/annotations/mod.rs +0 -1
  127. package/dist-engine-src/src/schema/builtin/lix_account.json +0 -21
  128. package/dist-engine-src/src/schema/builtin/lix_active_account.json +0 -29
  129. package/dist-engine-src/src/schema/builtin/lix_binary_blob_ref.json +0 -29
  130. package/dist-engine-src/src/schema/builtin/lix_branch_descriptor.json +0 -34
  131. package/dist-engine-src/src/schema/builtin/lix_branch_ref.json +0 -48
  132. package/dist-engine-src/src/schema/builtin/lix_change.json +0 -63
  133. package/dist-engine-src/src/schema/builtin/lix_change_author.json +0 -45
  134. package/dist-engine-src/src/schema/builtin/lix_commit.json +0 -24
  135. package/dist-engine-src/src/schema/builtin/lix_commit_edge.json +0 -53
  136. package/dist-engine-src/src/schema/builtin/lix_directory_descriptor.json +0 -52
  137. package/dist-engine-src/src/schema/builtin/lix_file_descriptor.json +0 -52
  138. package/dist-engine-src/src/schema/builtin/lix_key_value.json +0 -40
  139. package/dist-engine-src/src/schema/builtin/lix_label.json +0 -29
  140. package/dist-engine-src/src/schema/builtin/lix_label_assignment.json +0 -74
  141. package/dist-engine-src/src/schema/builtin/lix_registered_schema.json +0 -25
  142. package/dist-engine-src/src/schema/builtin/mod.rs +0 -220
  143. package/dist-engine-src/src/schema/compatibility.rs +0 -787
  144. package/dist-engine-src/src/schema/definition.json +0 -187
  145. package/dist-engine-src/src/schema/definition.rs +0 -742
  146. package/dist-engine-src/src/schema/key.rs +0 -138
  147. package/dist-engine-src/src/schema/mod.rs +0 -20
  148. package/dist-engine-src/src/schema/seed.rs +0 -14
  149. package/dist-engine-src/src/schema/tests.rs +0 -780
  150. package/dist-engine-src/src/session/context.rs +0 -1059
  151. package/dist-engine-src/src/session/create_branch.rs +0 -94
  152. package/dist-engine-src/src/session/execute.rs +0 -681
  153. package/dist-engine-src/src/session/merge/analysis.rs +0 -108
  154. package/dist-engine-src/src/session/merge/branch.rs +0 -417
  155. package/dist-engine-src/src/session/merge/conflicts.rs +0 -63
  156. package/dist-engine-src/src/session/merge/mod.rs +0 -10
  157. package/dist-engine-src/src/session/merge/stats.rs +0 -61
  158. package/dist-engine-src/src/session/mod.rs +0 -30
  159. package/dist-engine-src/src/session/switch_branch.rs +0 -113
  160. package/dist-engine-src/src/session/transaction.rs +0 -557
  161. package/dist-engine-src/src/sql2/bind/classify.rs +0 -102
  162. package/dist-engine-src/src/sql2/bind/error.rs +0 -5
  163. package/dist-engine-src/src/sql2/bind/expr.rs +0 -29
  164. package/dist-engine-src/src/sql2/bind/mod.rs +0 -12
  165. package/dist-engine-src/src/sql2/bind/public_udf.rs +0 -306
  166. package/dist-engine-src/src/sql2/bind/read.rs +0 -65
  167. package/dist-engine-src/src/sql2/bind/statement.rs +0 -2236
  168. package/dist-engine-src/src/sql2/bind/table.rs +0 -273
  169. package/dist-engine-src/src/sql2/bind/write.rs +0 -86
  170. package/dist-engine-src/src/sql2/branch_scope.rs +0 -436
  171. package/dist-engine-src/src/sql2/catalog/capability.rs +0 -20
  172. package/dist-engine-src/src/sql2/catalog/entity_surface.rs +0 -296
  173. package/dist-engine-src/src/sql2/catalog/mod.rs +0 -15
  174. package/dist-engine-src/src/sql2/catalog/registry.rs +0 -556
  175. package/dist-engine-src/src/sql2/catalog/schema.rs +0 -88
  176. package/dist-engine-src/src/sql2/catalog/surface.rs +0 -41
  177. package/dist-engine-src/src/sql2/change_materialization.rs +0 -122
  178. package/dist-engine-src/src/sql2/context.rs +0 -317
  179. package/dist-engine-src/src/sql2/dml.rs +0 -148
  180. package/dist-engine-src/src/sql2/error.rs +0 -215
  181. package/dist-engine-src/src/sql2/exec/bound_public_write.rs +0 -1593
  182. package/dist-engine-src/src/sql2/exec/datafusion.rs +0 -5266
  183. package/dist-engine-src/src/sql2/exec/fast_write.rs +0 -82
  184. package/dist-engine-src/src/sql2/exec/mod.rs +0 -24
  185. package/dist-engine-src/src/sql2/exec/write.rs +0 -661
  186. package/dist-engine-src/src/sql2/filesystem_planner.rs +0 -1485
  187. package/dist-engine-src/src/sql2/filesystem_predicates.rs +0 -159
  188. package/dist-engine-src/src/sql2/filesystem_visibility.rs +0 -383
  189. package/dist-engine-src/src/sql2/history_projection.rs +0 -56
  190. package/dist-engine-src/src/sql2/history_route.rs +0 -661
  191. package/dist-engine-src/src/sql2/mod.rs +0 -52
  192. package/dist-engine-src/src/sql2/optimize/datafusion.rs +0 -1
  193. package/dist-engine-src/src/sql2/optimize/mod.rs +0 -2
  194. package/dist-engine-src/src/sql2/optimize/simple_write.rs +0 -116
  195. package/dist-engine-src/src/sql2/parse/mod.rs +0 -69
  196. package/dist-engine-src/src/sql2/parse/normalize.rs +0 -1
  197. package/dist-engine-src/src/sql2/plan/branch_scope.rs +0 -24
  198. package/dist-engine-src/src/sql2/plan/mod.rs +0 -5
  199. package/dist-engine-src/src/sql2/plan/predicate.rs +0 -22
  200. package/dist-engine-src/src/sql2/plan/write.rs +0 -147
  201. package/dist-engine-src/src/sql2/predicate_typecheck.rs +0 -504
  202. package/dist-engine-src/src/sql2/providers/branch.rs +0 -1206
  203. package/dist-engine-src/src/sql2/providers/change.rs +0 -445
  204. package/dist-engine-src/src/sql2/providers/directory.rs +0 -2422
  205. package/dist-engine-src/src/sql2/providers/directory_history.rs +0 -645
  206. package/dist-engine-src/src/sql2/providers/entity.rs +0 -1484
  207. package/dist-engine-src/src/sql2/providers/entity_history.rs +0 -452
  208. package/dist-engine-src/src/sql2/providers/file.rs +0 -3686
  209. package/dist-engine-src/src/sql2/providers/file_history.rs +0 -924
  210. package/dist-engine-src/src/sql2/providers/history.rs +0 -426
  211. package/dist-engine-src/src/sql2/providers/lix_state.rs +0 -2542
  212. package/dist-engine-src/src/sql2/providers/mod.rs +0 -508
  213. package/dist-engine-src/src/sql2/read_only.rs +0 -63
  214. package/dist-engine-src/src/sql2/record_batch.rs +0 -17
  215. package/dist-engine-src/src/sql2/result_metadata.rs +0 -29
  216. package/dist-engine-src/src/sql2/runtime.rs +0 -60
  217. package/dist-engine-src/src/sql2/session.rs +0 -83
  218. package/dist-engine-src/src/sql2/storage/constraints.rs +0 -1
  219. package/dist-engine-src/src/sql2/storage/mod.rs +0 -1
  220. package/dist-engine-src/src/sql2/test_support/differential.rs +0 -712
  221. package/dist-engine-src/src/sql2/test_support/generators.rs +0 -354
  222. package/dist-engine-src/src/sql2/test_support/mod.rs +0 -2
  223. package/dist-engine-src/src/sql2/udfs/common.rs +0 -295
  224. package/dist-engine-src/src/sql2/udfs/lix_active_branch_commit_id.rs +0 -53
  225. package/dist-engine-src/src/sql2/udfs/lix_empty_blob.rs +0 -47
  226. package/dist-engine-src/src/sql2/udfs/lix_json.rs +0 -100
  227. package/dist-engine-src/src/sql2/udfs/lix_json_get.rs +0 -99
  228. package/dist-engine-src/src/sql2/udfs/lix_json_get_text.rs +0 -99
  229. package/dist-engine-src/src/sql2/udfs/lix_text_decode.rs +0 -82
  230. package/dist-engine-src/src/sql2/udfs/lix_text_encode.rs +0 -85
  231. package/dist-engine-src/src/sql2/udfs/lix_timestamp.rs +0 -76
  232. package/dist-engine-src/src/sql2/udfs/lix_uuid_v7.rs +0 -76
  233. package/dist-engine-src/src/sql2/udfs/mod.rs +0 -86
  234. package/dist-engine-src/src/sql2/write_normalization.rs +0 -368
  235. package/dist-engine-src/src/storage/conformance.rs +0 -399
  236. package/dist-engine-src/src/storage/context.rs +0 -620
  237. package/dist-engine-src/src/storage/mod.rs +0 -52
  238. package/dist-engine-src/src/storage/point.rs +0 -440
  239. package/dist-engine-src/src/storage/read_scope.rs +0 -67
  240. package/dist-engine-src/src/storage/reader.rs +0 -867
  241. package/dist-engine-src/src/storage/scan.rs +0 -784
  242. package/dist-engine-src/src/storage/spaces.rs +0 -236
  243. package/dist-engine-src/src/storage/stats.rs +0 -80
  244. package/dist-engine-src/src/storage/write_set.rs +0 -962
  245. package/dist-engine-src/src/storage_bench.rs +0 -171
  246. package/dist-engine-src/src/test_support.rs +0 -450
  247. package/dist-engine-src/src/tracked_state/bench_support.rs +0 -394
  248. package/dist-engine-src/src/tracked_state/codec.rs +0 -1183
  249. package/dist-engine-src/src/tracked_state/commit_root_rebuild.rs +0 -358
  250. package/dist-engine-src/src/tracked_state/context.rs +0 -2801
  251. package/dist-engine-src/src/tracked_state/diff.rs +0 -2140
  252. package/dist-engine-src/src/tracked_state/merge.rs +0 -478
  253. package/dist-engine-src/src/tracked_state/mod.rs +0 -35
  254. package/dist-engine-src/src/tracked_state/row_materialization.rs +0 -275
  255. package/dist-engine-src/src/tracked_state/storage.rs +0 -427
  256. package/dist-engine-src/src/tracked_state/tree.rs +0 -3063
  257. package/dist-engine-src/src/tracked_state/types.rs +0 -238
  258. package/dist-engine-src/src/transaction/bench_support.rs +0 -407
  259. package/dist-engine-src/src/transaction/commit.rs +0 -1592
  260. package/dist-engine-src/src/transaction/context.rs +0 -1653
  261. package/dist-engine-src/src/transaction/mod.rs +0 -24
  262. package/dist-engine-src/src/transaction/normalization.rs +0 -877
  263. package/dist-engine-src/src/transaction/prep.rs +0 -37
  264. package/dist-engine-src/src/transaction/schema_resolver.rs +0 -163
  265. package/dist-engine-src/src/transaction/staging.rs +0 -1525
  266. package/dist-engine-src/src/transaction/types.rs +0 -403
  267. package/dist-engine-src/src/transaction/validation.rs +0 -5766
  268. package/dist-engine-src/src/untracked_state/codec.rs +0 -615
  269. package/dist-engine-src/src/untracked_state/context.rs +0 -98
  270. package/dist-engine-src/src/untracked_state/materialization.rs +0 -63
  271. package/dist-engine-src/src/untracked_state/mod.rs +0 -15
  272. package/dist-engine-src/src/untracked_state/storage.rs +0 -898
  273. package/dist-engine-src/src/untracked_state/types.rs +0 -146
  274. package/dist-engine-src/src/wasm/mod.rs +0 -60
@@ -1,1206 +0,0 @@
1
- use std::any::Any;
2
- use std::sync::Arc;
3
-
4
- use async_trait::async_trait;
5
- use datafusion::arrow::array::{ArrayRef, BooleanArray, StringArray, UInt64Array};
6
- use datafusion::arrow::compute::{and, filter_record_batch};
7
- use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef};
8
- use datafusion::arrow::record_batch::RecordBatch;
9
- use datafusion::catalog::{Session, TableProvider};
10
- use datafusion::common::{not_impl_err, DFSchema, DataFusionError, Result, ScalarValue, SchemaExt};
11
- use datafusion::datasource::TableType;
12
- use datafusion::execution::TaskContext;
13
- use datafusion::logical_expr::dml::InsertOp;
14
- use datafusion::logical_expr::{Expr, TableProviderFilterPushDown};
15
- use datafusion::physical_expr::{create_physical_expr, EquivalenceProperties, PhysicalExpr};
16
- use datafusion::physical_plan::execution_plan::{Boundedness, EmissionType, PlanProperties};
17
- use datafusion::physical_plan::stream::RecordBatchStreamAdapter;
18
- use datafusion::physical_plan::{
19
- DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning, SendableRecordBatchStream,
20
- };
21
- use futures_util::{stream, TryStreamExt};
22
- use serde_json::Value as JsonValue;
23
-
24
- use crate::branch::{
25
- branch_descriptor_stage_row, branch_descriptor_tombstone_row, branch_ref_stage_row,
26
- branch_ref_tombstone_row, BranchRefReader,
27
- };
28
- use crate::live_state::{
29
- LiveStateFilter, LiveStateReader, LiveStateScanRequest, MaterializedLiveStateRow,
30
- };
31
- use crate::sql2::dml::{InsertExec, InsertSink};
32
- use crate::sql2::record_batch::record_batch_with_row_count;
33
- use crate::sql2::write_normalization::{InsertCell, SqlCell, UpdateAssignmentValues};
34
- use crate::sql2::{
35
- SqlWriteContext, WriteAccess, WriteContextBranchRefReader, WriteContextLiveStateReader,
36
- };
37
- use crate::transaction::types::{
38
- LogicalPrimaryKey, TransactionWrite, TransactionWriteMode, TransactionWriteOperation,
39
- TransactionWriteOrigin, TransactionWriteRow,
40
- };
41
- use crate::LixError;
42
- use crate::GLOBAL_BRANCH_ID;
43
-
44
- pub(super) async fn register_lix_branch_read_provider(
45
- session: &datafusion::prelude::SessionContext,
46
- surface_name: &str,
47
- live_state: Arc<dyn LiveStateReader>,
48
- branch_ref: Arc<dyn BranchRefReader>,
49
- ) -> Result<(), LixError> {
50
- session
51
- .register_table(
52
- surface_name,
53
- Arc::new(LixBranchProvider::new(live_state, branch_ref)),
54
- )
55
- .map_err(datafusion_error_to_lix_error)?;
56
- Ok(())
57
- }
58
-
59
- pub(super) async fn register_write_provider(
60
- session: &datafusion::prelude::SessionContext,
61
- surface_name: &str,
62
- write_ctx: SqlWriteContext,
63
- ) -> Result<(), LixError> {
64
- session
65
- .register_table(
66
- surface_name,
67
- Arc::new(LixBranchProvider::with_write(write_ctx)),
68
- )
69
- .map_err(datafusion_error_to_lix_error)?;
70
- Ok(())
71
- }
72
-
73
- struct LixBranchProvider {
74
- schema: SchemaRef,
75
- live_state: Arc<dyn LiveStateReader>,
76
- branch_ref: Arc<dyn BranchRefReader>,
77
- write_access: WriteAccess,
78
- }
79
-
80
- impl std::fmt::Debug for LixBranchProvider {
81
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82
- f.debug_struct("LixBranchProvider").finish()
83
- }
84
- }
85
-
86
- impl LixBranchProvider {
87
- fn new(live_state: Arc<dyn LiveStateReader>, branch_ref: Arc<dyn BranchRefReader>) -> Self {
88
- Self {
89
- schema: lix_branch_schema(),
90
- live_state,
91
- branch_ref,
92
- write_access: WriteAccess::read_only(),
93
- }
94
- }
95
-
96
- fn with_write(write_ctx: SqlWriteContext) -> Self {
97
- let live_state = Arc::new(WriteContextLiveStateReader::new(write_ctx.clone()));
98
- let branch_ref = Arc::new(WriteContextBranchRefReader::new(write_ctx.clone()));
99
- Self {
100
- schema: lix_branch_schema(),
101
- live_state,
102
- branch_ref,
103
- write_access: WriteAccess::write(write_ctx),
104
- }
105
- }
106
- }
107
-
108
- #[async_trait]
109
- impl TableProvider for LixBranchProvider {
110
- fn as_any(&self) -> &dyn Any {
111
- self
112
- }
113
-
114
- fn schema(&self) -> SchemaRef {
115
- Arc::clone(&self.schema)
116
- }
117
-
118
- fn table_type(&self) -> TableType {
119
- TableType::Base
120
- }
121
-
122
- fn supports_filters_pushdown(
123
- &self,
124
- filters: &[&Expr],
125
- ) -> Result<Vec<TableProviderFilterPushDown>> {
126
- Ok(filters
127
- .iter()
128
- .map(|_| TableProviderFilterPushDown::Unsupported)
129
- .collect())
130
- }
131
-
132
- async fn scan(
133
- &self,
134
- _state: &dyn Session,
135
- projection: Option<&Vec<usize>>,
136
- _filters: &[Expr],
137
- _limit: Option<usize>,
138
- ) -> Result<Arc<dyn ExecutionPlan>> {
139
- Ok(Arc::new(LixBranchScanExec::new(
140
- Arc::clone(&self.live_state),
141
- Arc::clone(&self.branch_ref),
142
- projected_schema(&self.schema, projection),
143
- projection.cloned(),
144
- )))
145
- }
146
-
147
- async fn insert_into(
148
- &self,
149
- _state: &dyn Session,
150
- input: Arc<dyn ExecutionPlan>,
151
- insert_op: InsertOp,
152
- ) -> Result<Arc<dyn ExecutionPlan>> {
153
- if insert_op != InsertOp::Append {
154
- return not_impl_err!("{insert_op} not implemented for lix_branch yet");
155
- }
156
- let write_ctx = self.write_access.require_write("INSERT into lix_branch")?;
157
- self.schema
158
- .logically_equivalent_names_and_types(&input.schema())?;
159
- let sink = LixBranchInsertSink::new(write_ctx);
160
- Ok(Arc::new(InsertExec::new(input, Arc::new(sink))))
161
- }
162
-
163
- async fn delete_from(
164
- &self,
165
- state: &dyn Session,
166
- filters: Vec<Expr>,
167
- ) -> Result<Arc<dyn ExecutionPlan>> {
168
- let write_ctx = self.write_access.require_write("DELETE FROM lix_branch")?;
169
- let df_schema = DFSchema::try_from(Arc::clone(&self.schema))?;
170
- let physical_filters = filters
171
- .iter()
172
- .map(|expr| create_physical_expr(expr, &df_schema, state.execution_props()))
173
- .collect::<Result<Vec<_>>>()?;
174
- Ok(Arc::new(LixBranchDeleteExec::new(
175
- write_ctx,
176
- Arc::clone(&self.live_state),
177
- Arc::clone(&self.branch_ref),
178
- Arc::clone(&self.schema),
179
- physical_filters,
180
- )))
181
- }
182
-
183
- async fn update(
184
- &self,
185
- state: &dyn Session,
186
- assignments: Vec<(String, Expr)>,
187
- filters: Vec<Expr>,
188
- ) -> Result<Arc<dyn ExecutionPlan>> {
189
- let write_ctx = self.write_access.require_write("UPDATE lix_branch")?;
190
- validate_lix_branch_update_assignments(&assignments)?;
191
- let df_schema = DFSchema::try_from(Arc::clone(&self.schema))?;
192
- let physical_assignments = assignments
193
- .iter()
194
- .map(|(column_name, expr)| {
195
- Ok((
196
- column_name.clone(),
197
- create_physical_expr(expr, &df_schema, state.execution_props())?,
198
- ))
199
- })
200
- .collect::<Result<Vec<_>>>()?;
201
- let physical_filters = filters
202
- .iter()
203
- .map(|expr| create_physical_expr(expr, &df_schema, state.execution_props()))
204
- .collect::<Result<Vec<_>>>()?;
205
- Ok(Arc::new(LixBranchUpdateExec::new(
206
- write_ctx,
207
- Arc::clone(&self.live_state),
208
- Arc::clone(&self.branch_ref),
209
- Arc::clone(&self.schema),
210
- physical_assignments,
211
- physical_filters,
212
- )))
213
- }
214
- }
215
-
216
- struct LixBranchInsertSink {
217
- write_ctx: SqlWriteContext,
218
- }
219
-
220
- impl std::fmt::Debug for LixBranchInsertSink {
221
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
222
- f.debug_struct("LixBranchInsertSink").finish()
223
- }
224
- }
225
-
226
- impl LixBranchInsertSink {
227
- fn new(write_ctx: SqlWriteContext) -> Self {
228
- Self { write_ctx }
229
- }
230
- }
231
-
232
- impl DisplayAs for LixBranchInsertSink {
233
- fn fmt_as(&self, t: DisplayFormatType, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
234
- match t {
235
- DisplayFormatType::Default | DisplayFormatType::Verbose => {
236
- write!(f, "LixBranchInsertSink")
237
- }
238
- DisplayFormatType::TreeRender => write!(f, "LixBranchInsertSink"),
239
- }
240
- }
241
- }
242
-
243
- #[async_trait]
244
- impl InsertSink for LixBranchInsertSink {
245
- async fn write_batches(
246
- &self,
247
- batches: Vec<RecordBatch>,
248
- _context: &Arc<TaskContext>,
249
- ) -> Result<u64> {
250
- let default_commit_id = self
251
- .write_ctx
252
- .load_branch_head(&self.write_ctx.active_branch_id())
253
- .await
254
- .map_err(lix_error_to_datafusion_error)?
255
- .ok_or_else(|| {
256
- DataFusionError::Execution(
257
- "INSERT into lix_branch could not resolve active branch head".to_string(),
258
- )
259
- })?;
260
- let mut rows = Vec::new();
261
- let mut count = 0u64;
262
- for batch in batches {
263
- let branch_rows = branch_insert_rows_from_batch(&batch, &default_commit_id)?;
264
- count = count
265
- .checked_add(u64::try_from(branch_rows.len()).map_err(|_| {
266
- DataFusionError::Execution("INSERT row count overflow".to_string())
267
- })?)
268
- .ok_or_else(|| DataFusionError::Execution("INSERT row count overflow".into()))?;
269
- rows.extend(branch_rows.into_iter().flat_map(branch_insert_stage_rows));
270
- }
271
-
272
- if !rows.is_empty() {
273
- self.write_ctx
274
- .stage_write(TransactionWrite::Rows {
275
- mode: TransactionWriteMode::Insert,
276
- rows,
277
- })
278
- .await
279
- .map_err(lix_error_to_datafusion_error)?;
280
- }
281
-
282
- Ok(count)
283
- }
284
- }
285
-
286
- struct LixBranchDeleteExec {
287
- write_ctx: SqlWriteContext,
288
- active_branch_id: String,
289
- live_state: Arc<dyn LiveStateReader>,
290
- branch_ref: Arc<dyn BranchRefReader>,
291
- table_schema: SchemaRef,
292
- filters: Vec<Arc<dyn PhysicalExpr>>,
293
- result_schema: SchemaRef,
294
- properties: Arc<PlanProperties>,
295
- }
296
-
297
- impl std::fmt::Debug for LixBranchDeleteExec {
298
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
299
- f.debug_struct("LixBranchDeleteExec").finish()
300
- }
301
- }
302
-
303
- impl LixBranchDeleteExec {
304
- fn new(
305
- write_ctx: SqlWriteContext,
306
- live_state: Arc<dyn LiveStateReader>,
307
- branch_ref: Arc<dyn BranchRefReader>,
308
- table_schema: SchemaRef,
309
- filters: Vec<Arc<dyn PhysicalExpr>>,
310
- ) -> Self {
311
- let result_schema = dml_count_schema();
312
- let properties = dml_plan_properties(Arc::clone(&result_schema));
313
- let active_branch_id = write_ctx.active_branch_id();
314
- Self {
315
- write_ctx,
316
- active_branch_id,
317
- live_state,
318
- branch_ref,
319
- table_schema,
320
- filters,
321
- result_schema,
322
- properties: Arc::new(properties),
323
- }
324
- }
325
- }
326
-
327
- impl DisplayAs for LixBranchDeleteExec {
328
- fn fmt_as(&self, t: DisplayFormatType, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
329
- match t {
330
- DisplayFormatType::Default | DisplayFormatType::Verbose => {
331
- write!(f, "LixBranchDeleteExec(filters={})", self.filters.len())
332
- }
333
- DisplayFormatType::TreeRender => write!(f, "LixBranchDeleteExec"),
334
- }
335
- }
336
- }
337
-
338
- impl ExecutionPlan for LixBranchDeleteExec {
339
- fn name(&self) -> &str {
340
- "LixBranchDeleteExec"
341
- }
342
-
343
- fn as_any(&self) -> &dyn Any {
344
- self
345
- }
346
-
347
- fn properties(&self) -> &Arc<PlanProperties> {
348
- &self.properties
349
- }
350
-
351
- fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
352
- Vec::new()
353
- }
354
-
355
- fn with_new_children(
356
- self: Arc<Self>,
357
- children: Vec<Arc<dyn ExecutionPlan>>,
358
- ) -> Result<Arc<dyn ExecutionPlan>> {
359
- if !children.is_empty() {
360
- return Err(DataFusionError::Execution(
361
- "LixBranchDeleteExec does not accept children".to_string(),
362
- ));
363
- }
364
- Ok(self)
365
- }
366
-
367
- fn execute(
368
- &self,
369
- partition: usize,
370
- _context: Arc<TaskContext>,
371
- ) -> Result<SendableRecordBatchStream> {
372
- if partition != 0 {
373
- return Err(DataFusionError::Execution(format!(
374
- "LixBranchDeleteExec only exposes one partition, got {partition}"
375
- )));
376
- }
377
- let write_ctx = self.write_ctx.clone();
378
- let active_branch_id = self.active_branch_id.clone();
379
- let live_state = Arc::clone(&self.live_state);
380
- let branch_ref = Arc::clone(&self.branch_ref);
381
- let filters = self.filters.clone();
382
- let table_schema = Arc::clone(&self.table_schema);
383
- let result_schema = Arc::clone(&self.result_schema);
384
- let stream_schema = Arc::clone(&result_schema);
385
-
386
- let stream = stream::once(async move {
387
- let rows = load_branch_rows(live_state, branch_ref)
388
- .await
389
- .map_err(lix_error_to_datafusion_error)?;
390
- let source_batch = branch_record_batch(&branch_projection_for_scan(None), &rows)?;
391
- let matched_batch = filter_branch_batch(source_batch, &filters)?;
392
- let branch_rows = branch_rows_from_batch(&matched_batch)?;
393
- reject_protected_branch_deletes(&branch_rows, &active_branch_id)?;
394
- let count = u64::try_from(branch_rows.len())
395
- .map_err(|_| DataFusionError::Execution("DELETE row count overflow".to_string()))?;
396
- let rows = branch_rows
397
- .into_iter()
398
- .flat_map(branch_tombstone_rows)
399
- .collect::<Vec<_>>();
400
-
401
- if !rows.is_empty() {
402
- write_ctx
403
- .stage_write(TransactionWrite::Rows {
404
- mode: TransactionWriteMode::Replace,
405
- rows,
406
- })
407
- .await
408
- .map_err(lix_error_to_datafusion_error)?;
409
- }
410
-
411
- let _ = table_schema;
412
- Ok::<_, DataFusionError>(stream::iter(vec![Ok::<RecordBatch, DataFusionError>(
413
- dml_count_batch(Arc::clone(&stream_schema), count)?,
414
- )]))
415
- })
416
- .try_flatten();
417
- Ok(Box::pin(RecordBatchStreamAdapter::new(
418
- result_schema,
419
- stream,
420
- )))
421
- }
422
- }
423
-
424
- struct LixBranchUpdateExec {
425
- write_ctx: SqlWriteContext,
426
- live_state: Arc<dyn LiveStateReader>,
427
- branch_ref: Arc<dyn BranchRefReader>,
428
- table_schema: SchemaRef,
429
- assignments: Vec<(String, Arc<dyn PhysicalExpr>)>,
430
- filters: Vec<Arc<dyn PhysicalExpr>>,
431
- result_schema: SchemaRef,
432
- properties: Arc<PlanProperties>,
433
- }
434
-
435
- impl std::fmt::Debug for LixBranchUpdateExec {
436
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
437
- f.debug_struct("LixBranchUpdateExec").finish()
438
- }
439
- }
440
-
441
- impl LixBranchUpdateExec {
442
- fn new(
443
- write_ctx: SqlWriteContext,
444
- live_state: Arc<dyn LiveStateReader>,
445
- branch_ref: Arc<dyn BranchRefReader>,
446
- table_schema: SchemaRef,
447
- assignments: Vec<(String, Arc<dyn PhysicalExpr>)>,
448
- filters: Vec<Arc<dyn PhysicalExpr>>,
449
- ) -> Self {
450
- let result_schema = dml_count_schema();
451
- let properties = dml_plan_properties(Arc::clone(&result_schema));
452
- Self {
453
- write_ctx,
454
- live_state,
455
- branch_ref,
456
- table_schema,
457
- assignments,
458
- filters,
459
- result_schema,
460
- properties: Arc::new(properties),
461
- }
462
- }
463
- }
464
-
465
- impl DisplayAs for LixBranchUpdateExec {
466
- fn fmt_as(&self, t: DisplayFormatType, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
467
- match t {
468
- DisplayFormatType::Default | DisplayFormatType::Verbose => {
469
- write!(
470
- f,
471
- "LixBranchUpdateExec(assignments={}, filters={})",
472
- self.assignments.len(),
473
- self.filters.len()
474
- )
475
- }
476
- DisplayFormatType::TreeRender => write!(f, "LixBranchUpdateExec"),
477
- }
478
- }
479
- }
480
-
481
- impl ExecutionPlan for LixBranchUpdateExec {
482
- fn name(&self) -> &str {
483
- "LixBranchUpdateExec"
484
- }
485
-
486
- fn as_any(&self) -> &dyn Any {
487
- self
488
- }
489
-
490
- fn properties(&self) -> &Arc<PlanProperties> {
491
- &self.properties
492
- }
493
-
494
- fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
495
- Vec::new()
496
- }
497
-
498
- fn with_new_children(
499
- self: Arc<Self>,
500
- children: Vec<Arc<dyn ExecutionPlan>>,
501
- ) -> Result<Arc<dyn ExecutionPlan>> {
502
- if !children.is_empty() {
503
- return Err(DataFusionError::Execution(
504
- "LixBranchUpdateExec does not accept children".to_string(),
505
- ));
506
- }
507
- Ok(self)
508
- }
509
-
510
- fn execute(
511
- &self,
512
- partition: usize,
513
- _context: Arc<TaskContext>,
514
- ) -> Result<SendableRecordBatchStream> {
515
- if partition != 0 {
516
- return Err(DataFusionError::Execution(format!(
517
- "LixBranchUpdateExec only exposes one partition, got {partition}"
518
- )));
519
- }
520
- let write_ctx = self.write_ctx.clone();
521
- let live_state = Arc::clone(&self.live_state);
522
- let branch_ref = Arc::clone(&self.branch_ref);
523
- let table_schema = Arc::clone(&self.table_schema);
524
- let assignments = self.assignments.clone();
525
- let filters = self.filters.clone();
526
- let result_schema = Arc::clone(&self.result_schema);
527
- let stream_schema = Arc::clone(&result_schema);
528
-
529
- let stream = stream::once(async move {
530
- let rows = load_branch_rows(live_state, branch_ref)
531
- .await
532
- .map_err(lix_error_to_datafusion_error)?;
533
- let source_batch = branch_record_batch(&branch_projection_for_scan(None), &rows)?;
534
- let matched_batch = filter_branch_batch(source_batch, &filters)?;
535
- let branch_rows =
536
- branch_update_rows_from_batch(&matched_batch, &assignments, &table_schema)?;
537
- reject_protected_branch_updates(&branch_rows)?;
538
- let count = u64::try_from(branch_rows.len())
539
- .map_err(|_| DataFusionError::Execution("UPDATE row count overflow".to_string()))?;
540
- let rows = branch_rows
541
- .into_iter()
542
- .flat_map(branch_update_stage_rows)
543
- .collect::<Vec<_>>();
544
-
545
- if !rows.is_empty() {
546
- write_ctx
547
- .stage_write(TransactionWrite::Rows {
548
- mode: TransactionWriteMode::Replace,
549
- rows,
550
- })
551
- .await
552
- .map_err(lix_error_to_datafusion_error)?;
553
- }
554
-
555
- Ok::<_, DataFusionError>(stream::iter(vec![Ok::<RecordBatch, DataFusionError>(
556
- dml_count_batch(Arc::clone(&stream_schema), count)?,
557
- )]))
558
- })
559
- .try_flatten();
560
- Ok(Box::pin(RecordBatchStreamAdapter::new(
561
- result_schema,
562
- stream,
563
- )))
564
- }
565
- }
566
-
567
- struct LixBranchScanExec {
568
- live_state: Arc<dyn LiveStateReader>,
569
- branch_ref: Arc<dyn BranchRefReader>,
570
- schema: SchemaRef,
571
- projection: Option<Vec<usize>>,
572
- properties: Arc<PlanProperties>,
573
- }
574
-
575
- impl std::fmt::Debug for LixBranchScanExec {
576
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
577
- f.debug_struct("LixBranchScanExec").finish()
578
- }
579
- }
580
-
581
- impl LixBranchScanExec {
582
- fn new(
583
- live_state: Arc<dyn LiveStateReader>,
584
- branch_ref: Arc<dyn BranchRefReader>,
585
- schema: SchemaRef,
586
- projection: Option<Vec<usize>>,
587
- ) -> Self {
588
- let properties = PlanProperties::new(
589
- EquivalenceProperties::new(schema.clone()),
590
- Partitioning::UnknownPartitioning(1),
591
- EmissionType::Incremental,
592
- Boundedness::Bounded,
593
- );
594
- Self {
595
- live_state,
596
- branch_ref,
597
- schema,
598
- projection,
599
- properties: Arc::new(properties),
600
- }
601
- }
602
- }
603
-
604
- impl DisplayAs for LixBranchScanExec {
605
- fn fmt_as(&self, t: DisplayFormatType, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
606
- match t {
607
- DisplayFormatType::Default | DisplayFormatType::Verbose => {
608
- write!(f, "LixBranchScanExec")
609
- }
610
- DisplayFormatType::TreeRender => write!(f, "LixBranchScanExec"),
611
- }
612
- }
613
- }
614
-
615
- impl ExecutionPlan for LixBranchScanExec {
616
- fn name(&self) -> &str {
617
- "LixBranchScanExec"
618
- }
619
-
620
- fn as_any(&self) -> &dyn Any {
621
- self
622
- }
623
-
624
- fn properties(&self) -> &Arc<PlanProperties> {
625
- &self.properties
626
- }
627
-
628
- fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
629
- Vec::new()
630
- }
631
-
632
- fn with_new_children(
633
- self: Arc<Self>,
634
- children: Vec<Arc<dyn ExecutionPlan>>,
635
- ) -> Result<Arc<dyn ExecutionPlan>> {
636
- if !children.is_empty() {
637
- return Err(DataFusionError::Execution(
638
- "LixBranchScanExec does not accept children".to_string(),
639
- ));
640
- }
641
- Ok(self)
642
- }
643
-
644
- fn execute(
645
- &self,
646
- partition: usize,
647
- _context: Arc<TaskContext>,
648
- ) -> Result<SendableRecordBatchStream> {
649
- if partition != 0 {
650
- return Err(DataFusionError::Execution(format!(
651
- "LixBranchScanExec only exposes one partition, got {partition}"
652
- )));
653
- }
654
-
655
- let live_state = Arc::clone(&self.live_state);
656
- let branch_ref = Arc::clone(&self.branch_ref);
657
- let projection = branch_projection_for_scan(self.projection.as_ref());
658
- let schema = Arc::clone(&self.schema);
659
- let stream = stream::once(async move {
660
- let rows = load_branch_rows(live_state, branch_ref)
661
- .await
662
- .map_err(lix_error_to_datafusion_error)?;
663
- branch_record_batch(&projection, &rows)
664
- });
665
- Ok(Box::pin(RecordBatchStreamAdapter::new(schema, stream)))
666
- }
667
- }
668
-
669
- #[derive(Debug, Clone, PartialEq, Eq)]
670
- struct BranchRow {
671
- id: String,
672
- name: String,
673
- hidden: bool,
674
- commit_id: String,
675
- }
676
-
677
- #[derive(Debug, Clone, Copy)]
678
- enum BranchColumn {
679
- Id,
680
- Name,
681
- Hidden,
682
- CommitId,
683
- }
684
-
685
- async fn load_branch_rows(
686
- live_state: Arc<dyn LiveStateReader>,
687
- branch_ref: Arc<dyn BranchRefReader>,
688
- ) -> Result<Vec<BranchRow>, LixError> {
689
- let descriptor_rows = live_state
690
- .scan_rows(&LiveStateScanRequest {
691
- filter: LiveStateFilter {
692
- schema_keys: vec!["lix_branch_descriptor".to_string()],
693
- branch_ids: vec![GLOBAL_BRANCH_ID.to_string()],
694
- ..LiveStateFilter::default()
695
- },
696
- projection: Default::default(),
697
- limit: None,
698
- })
699
- .await?;
700
-
701
- let mut out = Vec::new();
702
- for descriptor_row in descriptor_rows {
703
- let descriptor = parse_descriptor(&descriptor_row)?;
704
- let Some(commit_id) = branch_ref.load_head_commit_id(&descriptor.id).await? else {
705
- continue;
706
- };
707
- out.push(BranchRow {
708
- commit_id,
709
- id: descriptor.id,
710
- name: descriptor.name,
711
- hidden: descriptor.hidden,
712
- });
713
- }
714
- Ok(out)
715
- }
716
-
717
- #[derive(Debug, Clone, PartialEq, Eq)]
718
- struct BranchDescriptor {
719
- id: String,
720
- name: String,
721
- hidden: bool,
722
- }
723
-
724
- fn parse_descriptor(row: &MaterializedLiveStateRow) -> Result<BranchDescriptor, LixError> {
725
- let snapshot = parse_snapshot(row, "lix_branch_descriptor")?;
726
- let id = snapshot
727
- .get("id")
728
- .and_then(JsonValue::as_str)
729
- .ok_or_else(|| LixError::new("LIX_ERROR_UNKNOWN", "lix_branch_descriptor is missing id"))?
730
- .to_string();
731
- let name = snapshot
732
- .get("name")
733
- .and_then(JsonValue::as_str)
734
- .ok_or_else(|| LixError::new("LIX_ERROR_UNKNOWN", "lix_branch_descriptor is missing name"))?
735
- .to_string();
736
- let hidden = snapshot
737
- .get("hidden")
738
- .and_then(JsonValue::as_bool)
739
- .unwrap_or(false);
740
- Ok(BranchDescriptor { id, name, hidden })
741
- }
742
-
743
- fn parse_snapshot(row: &MaterializedLiveStateRow, schema_key: &str) -> Result<JsonValue, LixError> {
744
- let snapshot_content = row.snapshot_content.as_deref().ok_or_else(|| {
745
- LixError::new(
746
- "LIX_ERROR_UNKNOWN",
747
- format!("{schema_key} row is missing snapshot_content"),
748
- )
749
- })?;
750
- serde_json::from_str(snapshot_content).map_err(|error| {
751
- LixError::new(
752
- "LIX_ERROR_UNKNOWN",
753
- format!("{schema_key} snapshot_content is invalid JSON: {error}"),
754
- )
755
- })
756
- }
757
-
758
- fn validate_lix_branch_update_assignments(assignments: &[(String, Expr)]) -> Result<()> {
759
- for (column_name, _) in assignments {
760
- match column_name.as_str() {
761
- "name" | "hidden" | "commit_id" => {}
762
- "id" => {
763
- return Err(DataFusionError::Execution(
764
- "UPDATE lix_branch cannot change immutable column 'id'".to_string(),
765
- ));
766
- }
767
- other => {
768
- return Err(DataFusionError::Plan(format!(
769
- "UPDATE lix_branch failed: column '{other}' does not exist"
770
- )));
771
- }
772
- }
773
- }
774
- Ok(())
775
- }
776
-
777
- fn filter_branch_batch(
778
- batch: RecordBatch,
779
- filters: &[Arc<dyn PhysicalExpr>],
780
- ) -> Result<RecordBatch> {
781
- let Some(mask) = evaluate_branch_filters(&batch, filters)? else {
782
- return Ok(batch);
783
- };
784
- Ok(filter_record_batch(&batch, &mask)?)
785
- }
786
-
787
- fn evaluate_branch_filters(
788
- batch: &RecordBatch,
789
- filters: &[Arc<dyn PhysicalExpr>],
790
- ) -> Result<Option<BooleanArray>> {
791
- if filters.is_empty() {
792
- return Ok(None);
793
- }
794
-
795
- let mut combined_mask: Option<BooleanArray> = None;
796
- for filter in filters {
797
- let result = filter.evaluate(batch)?;
798
- let array = result.into_array(batch.num_rows())?;
799
- let bool_array = array
800
- .as_any()
801
- .downcast_ref::<BooleanArray>()
802
- .ok_or_else(|| {
803
- DataFusionError::Execution("lix_branch filter was not boolean".to_string())
804
- })?;
805
- let normalized = bool_array
806
- .iter()
807
- .map(|value| Some(value == Some(true)))
808
- .collect::<BooleanArray>();
809
- combined_mask = Some(match combined_mask {
810
- Some(existing) => and(&existing, &normalized)?,
811
- None => normalized,
812
- });
813
- }
814
- Ok(combined_mask)
815
- }
816
-
817
- fn branch_insert_rows_from_batch(
818
- batch: &RecordBatch,
819
- default_commit_id: &str,
820
- ) -> Result<Vec<BranchRow>> {
821
- (0..batch.num_rows())
822
- .map(|row_index| {
823
- let id = required_string_value(batch, row_index, "id", "INSERT")?;
824
- let name = required_string_value(batch, row_index, "name", "INSERT")?;
825
- let hidden =
826
- optional_bool_value(batch, row_index, "hidden", "INSERT")?.unwrap_or(false);
827
- let commit_id = optional_string_value(batch, row_index, "commit_id", "INSERT")?
828
- .unwrap_or_else(|| default_commit_id.to_string());
829
- Ok(BranchRow {
830
- id,
831
- name,
832
- hidden,
833
- commit_id,
834
- })
835
- })
836
- .collect()
837
- }
838
-
839
- fn branch_rows_from_batch(batch: &RecordBatch) -> Result<Vec<BranchRow>> {
840
- (0..batch.num_rows())
841
- .map(|row_index| {
842
- Ok(BranchRow {
843
- id: required_string_value(batch, row_index, "id", "DELETE")?,
844
- name: required_string_value(batch, row_index, "name", "DELETE")?,
845
- hidden: required_bool_value(batch, row_index, "hidden", "DELETE")?,
846
- commit_id: required_string_value(batch, row_index, "commit_id", "DELETE")?,
847
- })
848
- })
849
- .collect()
850
- }
851
-
852
- fn reject_protected_branch_deletes(rows: &[BranchRow], active_branch_id: &str) -> Result<()> {
853
- for row in rows {
854
- if row.id == GLOBAL_BRANCH_ID {
855
- return Err(DataFusionError::Execution(
856
- "DELETE FROM lix_branch cannot delete the global branch".to_string(),
857
- ));
858
- }
859
- if row.id == active_branch_id {
860
- return Err(DataFusionError::Execution(format!(
861
- "DELETE FROM lix_branch cannot delete active branch '{}'",
862
- row.id
863
- )));
864
- }
865
- }
866
- Ok(())
867
- }
868
-
869
- fn reject_protected_branch_updates(rows: &[BranchRow]) -> Result<()> {
870
- for row in rows {
871
- if row.id == GLOBAL_BRANCH_ID {
872
- return Err(DataFusionError::Execution(
873
- "UPDATE lix_branch cannot update the global branch".to_string(),
874
- ));
875
- }
876
- }
877
- Ok(())
878
- }
879
-
880
- fn branch_update_rows_from_batch(
881
- batch: &RecordBatch,
882
- assignments: &[(String, Arc<dyn PhysicalExpr>)],
883
- table_schema: &SchemaRef,
884
- ) -> Result<Vec<BranchRow>> {
885
- let assignment_values = UpdateAssignmentValues::evaluate(batch, assignments)?;
886
- (0..batch.num_rows())
887
- .map(|row_index| {
888
- Ok(BranchRow {
889
- id: required_string_value(batch, row_index, "id", "UPDATE")?,
890
- name: update_string_value(
891
- batch,
892
- &assignment_values,
893
- table_schema,
894
- row_index,
895
- "name",
896
- )?,
897
- hidden: update_bool_value(
898
- batch,
899
- &assignment_values,
900
- table_schema,
901
- row_index,
902
- "hidden",
903
- )?,
904
- commit_id: update_string_value(
905
- batch,
906
- &assignment_values,
907
- table_schema,
908
- row_index,
909
- "commit_id",
910
- )?,
911
- })
912
- })
913
- .collect()
914
- }
915
-
916
- fn branch_stage_rows(
917
- row: BranchRow,
918
- origin: Option<TransactionWriteOrigin>,
919
- ) -> Vec<TransactionWriteRow> {
920
- vec![
921
- with_origin(
922
- branch_descriptor_stage_row(&row.id, &row.name, row.hidden),
923
- origin.clone(),
924
- ),
925
- with_origin(branch_ref_stage_row(&row.id, &row.commit_id), origin),
926
- ]
927
- }
928
-
929
- fn branch_tombstone_rows(row: BranchRow) -> Vec<TransactionWriteRow> {
930
- let origin = Some(lix_branch_origin(
931
- TransactionWriteOperation::Delete,
932
- &row.id,
933
- ));
934
- vec![
935
- with_origin(branch_descriptor_tombstone_row(&row.id), origin.clone()),
936
- with_origin(branch_ref_tombstone_row(&row.id), origin),
937
- ]
938
- }
939
-
940
- fn branch_insert_stage_rows(row: BranchRow) -> Vec<TransactionWriteRow> {
941
- let origin = lix_branch_origin(TransactionWriteOperation::Insert, &row.id);
942
- branch_stage_rows(row, Some(origin))
943
- }
944
-
945
- fn branch_update_stage_rows(row: BranchRow) -> Vec<TransactionWriteRow> {
946
- let origin = lix_branch_origin(TransactionWriteOperation::Update, &row.id);
947
- branch_stage_rows(row, Some(origin))
948
- }
949
-
950
- fn with_origin(
951
- mut row: TransactionWriteRow,
952
- origin: Option<TransactionWriteOrigin>,
953
- ) -> TransactionWriteRow {
954
- row.origin = origin;
955
- row
956
- }
957
-
958
- fn lix_branch_origin(action: TransactionWriteOperation, branch_id: &str) -> TransactionWriteOrigin {
959
- TransactionWriteOrigin {
960
- surface: "lix_branch".to_string(),
961
- operation: action,
962
- primary_key: Some(LogicalPrimaryKey {
963
- columns: vec!["id".to_string()],
964
- values: vec![branch_id.to_string()],
965
- }),
966
- }
967
- }
968
-
969
- fn update_string_value(
970
- batch: &RecordBatch,
971
- assignment_values: &UpdateAssignmentValues,
972
- table_schema: &SchemaRef,
973
- row_index: usize,
974
- column_name: &str,
975
- ) -> Result<String> {
976
- let column_index = table_schema.index_of(column_name)?;
977
- match assignment_values.assigned_or_existing_cell(batch, row_index, column_name)? {
978
- InsertCell::Omitted => required_string_value(batch, row_index, column_name, "UPDATE"),
979
- InsertCell::Provided(SqlCell::Value(
980
- ScalarValue::Utf8(Some(value))
981
- | ScalarValue::Utf8View(Some(value))
982
- | ScalarValue::LargeUtf8(Some(value)),
983
- )) => Ok(value),
984
- InsertCell::Provided(SqlCell::Null) => Err(DataFusionError::Execution(format!(
985
- "UPDATE lix_branch requires non-null text column '{column_name}'"
986
- ))),
987
- InsertCell::Provided(SqlCell::Value(other)) => Err(DataFusionError::Execution(format!(
988
- "UPDATE lix_branch expected text-compatible column '{column_name}', got {other:?}"
989
- ))),
990
- }
991
- .or_else(|error| {
992
- if batch.column(column_index).is_null(row_index) {
993
- Err(DataFusionError::Execution(format!(
994
- "UPDATE lix_branch requires non-null text column '{column_name}'"
995
- )))
996
- } else {
997
- Err(error)
998
- }
999
- })
1000
- }
1001
-
1002
- fn update_bool_value(
1003
- batch: &RecordBatch,
1004
- assignment_values: &UpdateAssignmentValues,
1005
- table_schema: &SchemaRef,
1006
- row_index: usize,
1007
- column_name: &str,
1008
- ) -> Result<bool> {
1009
- let column_index = table_schema.index_of(column_name)?;
1010
- match assignment_values.assigned_or_existing_cell(batch, row_index, column_name)? {
1011
- InsertCell::Omitted => required_bool_value(batch, row_index, column_name, "UPDATE"),
1012
- InsertCell::Provided(SqlCell::Value(ScalarValue::Boolean(Some(value)))) => Ok(value),
1013
- InsertCell::Provided(SqlCell::Null) => Err(DataFusionError::Execution(format!(
1014
- "UPDATE lix_branch requires non-null boolean column '{column_name}'"
1015
- ))),
1016
- InsertCell::Provided(SqlCell::Value(other)) => Err(DataFusionError::Execution(format!(
1017
- "UPDATE lix_branch expected boolean column '{column_name}', got {other:?}"
1018
- ))),
1019
- }
1020
- .or_else(|error| {
1021
- if batch.column(column_index).is_null(row_index) {
1022
- Err(DataFusionError::Execution(format!(
1023
- "UPDATE lix_branch requires non-null boolean column '{column_name}'"
1024
- )))
1025
- } else {
1026
- Err(error)
1027
- }
1028
- })
1029
- }
1030
-
1031
- fn required_string_value(
1032
- batch: &RecordBatch,
1033
- row_index: usize,
1034
- column_name: &str,
1035
- action: &str,
1036
- ) -> Result<String> {
1037
- optional_string_value(batch, row_index, column_name, action)?.ok_or_else(|| {
1038
- DataFusionError::Execution(format!(
1039
- "{action} lix_branch requires non-null text column '{column_name}'"
1040
- ))
1041
- })
1042
- }
1043
-
1044
- fn optional_string_value(
1045
- batch: &RecordBatch,
1046
- row_index: usize,
1047
- column_name: &str,
1048
- action: &str,
1049
- ) -> Result<Option<String>> {
1050
- match optional_scalar_value(batch, row_index, column_name)? {
1051
- None
1052
- | Some(ScalarValue::Null)
1053
- | Some(ScalarValue::Utf8(None))
1054
- | Some(ScalarValue::Utf8View(None))
1055
- | Some(ScalarValue::LargeUtf8(None)) => Ok(None),
1056
- Some(ScalarValue::Utf8(Some(value)))
1057
- | Some(ScalarValue::Utf8View(Some(value)))
1058
- | Some(ScalarValue::LargeUtf8(Some(value))) => Ok(Some(value)),
1059
- Some(other) => Err(DataFusionError::Execution(format!(
1060
- "{action} lix_branch expected text-compatible column '{column_name}', got {other:?}"
1061
- ))),
1062
- }
1063
- }
1064
-
1065
- fn required_bool_value(
1066
- batch: &RecordBatch,
1067
- row_index: usize,
1068
- column_name: &str,
1069
- action: &str,
1070
- ) -> Result<bool> {
1071
- optional_bool_value(batch, row_index, column_name, action)?.ok_or_else(|| {
1072
- DataFusionError::Execution(format!(
1073
- "{action} lix_branch requires non-null boolean column '{column_name}'"
1074
- ))
1075
- })
1076
- }
1077
-
1078
- fn optional_bool_value(
1079
- batch: &RecordBatch,
1080
- row_index: usize,
1081
- column_name: &str,
1082
- action: &str,
1083
- ) -> Result<Option<bool>> {
1084
- match optional_scalar_value(batch, row_index, column_name)? {
1085
- None | Some(ScalarValue::Null) | Some(ScalarValue::Boolean(None)) => Ok(None),
1086
- Some(ScalarValue::Boolean(Some(value))) => Ok(Some(value)),
1087
- Some(other) => Err(DataFusionError::Execution(format!(
1088
- "{action} lix_branch expected boolean column '{column_name}', got {other:?}"
1089
- ))),
1090
- }
1091
- }
1092
-
1093
- fn optional_scalar_value(
1094
- batch: &RecordBatch,
1095
- row_index: usize,
1096
- column_name: &str,
1097
- ) -> Result<Option<ScalarValue>> {
1098
- let Ok(column_index) = batch.schema().index_of(column_name) else {
1099
- return Ok(None);
1100
- };
1101
- Ok(Some(ScalarValue::try_from_array(
1102
- batch.column(column_index).as_ref(),
1103
- row_index,
1104
- )?))
1105
- }
1106
-
1107
- fn dml_count_schema() -> SchemaRef {
1108
- Arc::new(Schema::new(vec![Field::new(
1109
- "count",
1110
- DataType::UInt64,
1111
- false,
1112
- )]))
1113
- }
1114
-
1115
- fn dml_plan_properties(schema: SchemaRef) -> PlanProperties {
1116
- PlanProperties::new(
1117
- EquivalenceProperties::new(schema),
1118
- Partitioning::UnknownPartitioning(1),
1119
- EmissionType::Final,
1120
- Boundedness::Bounded,
1121
- )
1122
- }
1123
-
1124
- fn dml_count_batch(schema: SchemaRef, count: u64) -> Result<RecordBatch> {
1125
- RecordBatch::try_new(
1126
- schema,
1127
- vec![Arc::new(UInt64Array::from(vec![count])) as ArrayRef],
1128
- )
1129
- .map_err(DataFusionError::from)
1130
- }
1131
-
1132
- pub(super) fn lix_branch_schema() -> SchemaRef {
1133
- Arc::new(Schema::new(vec![
1134
- Field::new("id", DataType::Utf8, false),
1135
- Field::new("name", DataType::Utf8, false),
1136
- Field::new("hidden", DataType::Boolean, false),
1137
- Field::new("commit_id", DataType::Utf8, false),
1138
- ]))
1139
- }
1140
-
1141
- fn branch_projection_for_scan(projection: Option<&Vec<usize>>) -> Vec<BranchColumn> {
1142
- let all_columns = vec![
1143
- BranchColumn::Id,
1144
- BranchColumn::Name,
1145
- BranchColumn::Hidden,
1146
- BranchColumn::CommitId,
1147
- ];
1148
- projection.map_or(all_columns.clone(), |indices| {
1149
- indices
1150
- .iter()
1151
- .filter_map(|index| all_columns.get(*index).copied())
1152
- .collect()
1153
- })
1154
- }
1155
-
1156
- fn projected_schema(schema: &SchemaRef, projection: Option<&Vec<usize>>) -> SchemaRef {
1157
- match projection {
1158
- Some(projection) => Arc::new(schema.project(projection).expect("projection is valid")),
1159
- None => Arc::clone(schema),
1160
- }
1161
- }
1162
-
1163
- fn branch_record_batch(projection: &[BranchColumn], rows: &[BranchRow]) -> Result<RecordBatch> {
1164
- let arrays = projection
1165
- .iter()
1166
- .map(|column| match column {
1167
- BranchColumn::Id => string_array(rows.iter().map(|row| Some(row.id.as_str()))),
1168
- BranchColumn::Name => string_array(rows.iter().map(|row| Some(row.name.as_str()))),
1169
- BranchColumn::Hidden => Arc::new(BooleanArray::from(
1170
- rows.iter().map(|row| row.hidden).collect::<Vec<_>>(),
1171
- )) as ArrayRef,
1172
- BranchColumn::CommitId => {
1173
- string_array(rows.iter().map(|row| Some(row.commit_id.as_str())))
1174
- }
1175
- })
1176
- .collect::<Vec<_>>();
1177
- record_batch_with_row_count(branch_schema(projection), arrays, rows.len()).map_err(|error| {
1178
- DataFusionError::Execution(format!("failed to build lix_branch batch: {error}"))
1179
- })
1180
- }
1181
-
1182
- fn branch_schema(projection: &[BranchColumn]) -> SchemaRef {
1183
- Arc::new(Schema::new(
1184
- projection
1185
- .iter()
1186
- .map(|column| match column {
1187
- BranchColumn::Id => Field::new("id", DataType::Utf8, false),
1188
- BranchColumn::Name => Field::new("name", DataType::Utf8, false),
1189
- BranchColumn::Hidden => Field::new("hidden", DataType::Boolean, false),
1190
- BranchColumn::CommitId => Field::new("commit_id", DataType::Utf8, false),
1191
- })
1192
- .collect::<Vec<_>>(),
1193
- ))
1194
- }
1195
-
1196
- fn string_array<'a>(values: impl Iterator<Item = Option<&'a str>>) -> ArrayRef {
1197
- Arc::new(StringArray::from(values.collect::<Vec<_>>())) as ArrayRef
1198
- }
1199
-
1200
- fn datafusion_error_to_lix_error(error: DataFusionError) -> LixError {
1201
- crate::sql2::error::datafusion_error_to_lix_error(error)
1202
- }
1203
-
1204
- fn lix_error_to_datafusion_error(error: LixError) -> DataFusionError {
1205
- crate::sql2::error::lix_error_to_datafusion_error(error)
1206
- }