@lix-js/sdk 0.6.0-preview.1 → 0.6.0-preview.3

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 (205) hide show
  1. package/SKILL.md +304 -320
  2. package/dist/engine-wasm/wasm/lix_engine.d.ts +5 -0
  3. package/dist/engine-wasm/wasm/lix_engine.js +9 -13
  4. package/dist/engine-wasm/wasm/lix_engine.wasm +0 -0
  5. package/dist/engine-wasm/wasm/lix_engine.wasm.d.ts +1 -0
  6. package/dist/generated/builtin-schemas.d.ts +87 -162
  7. package/dist/generated/builtin-schemas.js +139 -236
  8. package/dist/open-lix.d.ts +103 -14
  9. package/dist/open-lix.js +3 -0
  10. package/dist/sqlite/index.js +99 -22
  11. package/dist-engine-src/README.md +18 -0
  12. package/dist-engine-src/src/backend/kv.rs +358 -0
  13. package/dist-engine-src/src/backend/mod.rs +12 -0
  14. package/dist-engine-src/src/backend/testing.rs +658 -0
  15. package/dist-engine-src/src/backend/types.rs +96 -0
  16. package/dist-engine-src/src/binary_cas/chunking.rs +31 -0
  17. package/dist-engine-src/src/binary_cas/codec.rs +346 -0
  18. package/dist-engine-src/src/binary_cas/context.rs +139 -0
  19. package/dist-engine-src/src/binary_cas/kv.rs +1063 -0
  20. package/dist-engine-src/src/binary_cas/mod.rs +11 -0
  21. package/dist-engine-src/src/binary_cas/types.rs +121 -0
  22. package/dist-engine-src/src/catalog/context.rs +412 -0
  23. package/dist-engine-src/src/catalog/mod.rs +10 -0
  24. package/dist-engine-src/src/catalog/schema.rs +4 -0
  25. package/dist-engine-src/src/catalog/snapshot.rs +1114 -0
  26. package/dist-engine-src/src/cel/context.rs +86 -0
  27. package/dist-engine-src/src/cel/error.rs +19 -0
  28. package/dist-engine-src/src/cel/mod.rs +8 -0
  29. package/dist-engine-src/src/cel/provider.rs +9 -0
  30. package/dist-engine-src/src/cel/runtime.rs +167 -0
  31. package/dist-engine-src/src/cel/value.rs +50 -0
  32. package/dist-engine-src/src/commit_graph/context.rs +901 -0
  33. package/dist-engine-src/src/commit_graph/mod.rs +11 -0
  34. package/dist-engine-src/src/commit_graph/types.rs +109 -0
  35. package/dist-engine-src/src/commit_graph/walker.rs +756 -0
  36. package/dist-engine-src/src/commit_store/codec.rs +887 -0
  37. package/dist-engine-src/src/commit_store/context.rs +944 -0
  38. package/dist-engine-src/src/commit_store/materialization.rs +84 -0
  39. package/dist-engine-src/src/commit_store/mod.rs +16 -0
  40. package/dist-engine-src/src/commit_store/storage.rs +600 -0
  41. package/dist-engine-src/src/commit_store/types.rs +215 -0
  42. package/dist-engine-src/src/common/error.rs +313 -0
  43. package/dist-engine-src/src/common/fingerprint.rs +3 -0
  44. package/dist-engine-src/src/common/fs_path.rs +1336 -0
  45. package/dist-engine-src/src/common/identity.rs +145 -0
  46. package/dist-engine-src/src/common/json_pointer.rs +67 -0
  47. package/dist-engine-src/src/common/metadata.rs +40 -0
  48. package/dist-engine-src/src/common/mod.rs +23 -0
  49. package/dist-engine-src/src/common/types.rs +105 -0
  50. package/dist-engine-src/src/common/wire.rs +222 -0
  51. package/dist-engine-src/src/domain.rs +324 -0
  52. package/dist-engine-src/src/engine.rs +225 -0
  53. package/dist-engine-src/src/entity_identity.rs +405 -0
  54. package/dist-engine-src/src/functions/context.rs +292 -0
  55. package/dist-engine-src/src/functions/deterministic.rs +113 -0
  56. package/dist-engine-src/src/functions/mod.rs +18 -0
  57. package/dist-engine-src/src/functions/provider.rs +130 -0
  58. package/dist-engine-src/src/functions/state.rs +336 -0
  59. package/dist-engine-src/src/functions/types.rs +37 -0
  60. package/dist-engine-src/src/init.rs +558 -0
  61. package/dist-engine-src/src/json_store/compression.rs +77 -0
  62. package/dist-engine-src/src/json_store/context.rs +423 -0
  63. package/dist-engine-src/src/json_store/encoded.rs +15 -0
  64. package/dist-engine-src/src/json_store/mod.rs +12 -0
  65. package/dist-engine-src/src/json_store/store.rs +1109 -0
  66. package/dist-engine-src/src/json_store/types.rs +217 -0
  67. package/dist-engine-src/src/lib.rs +62 -0
  68. package/dist-engine-src/src/live_state/context.rs +2019 -0
  69. package/dist-engine-src/src/live_state/mod.rs +15 -0
  70. package/dist-engine-src/src/live_state/overlay.rs +75 -0
  71. package/dist-engine-src/src/live_state/reader.rs +23 -0
  72. package/dist-engine-src/src/live_state/types.rs +222 -0
  73. package/dist-engine-src/src/live_state/visibility.rs +223 -0
  74. package/dist-engine-src/src/plugin/archive.rs +438 -0
  75. package/dist-engine-src/src/plugin/component.rs +183 -0
  76. package/dist-engine-src/src/plugin/install.rs +619 -0
  77. package/dist-engine-src/src/plugin/manifest.rs +516 -0
  78. package/dist-engine-src/src/plugin/materializer.rs +477 -0
  79. package/dist-engine-src/src/plugin/mod.rs +33 -0
  80. package/dist-engine-src/src/plugin/plugin_manifest.json +118 -0
  81. package/dist-engine-src/src/plugin/storage.rs +74 -0
  82. package/dist-engine-src/src/schema/annotations/defaults.rs +275 -0
  83. package/dist-engine-src/src/schema/annotations/mod.rs +1 -0
  84. package/dist-engine-src/src/schema/builtin/lix_account.json +21 -0
  85. package/dist-engine-src/src/schema/builtin/lix_active_account.json +29 -0
  86. package/dist-engine-src/src/schema/builtin/lix_binary_blob_ref.json +29 -0
  87. package/dist-engine-src/src/schema/builtin/lix_change.json +63 -0
  88. package/dist-engine-src/src/schema/builtin/lix_change_author.json +45 -0
  89. package/dist-engine-src/src/schema/builtin/lix_commit.json +24 -0
  90. package/dist-engine-src/src/schema/builtin/lix_commit_edge.json +53 -0
  91. package/dist-engine-src/src/schema/builtin/lix_directory_descriptor.json +52 -0
  92. package/dist-engine-src/src/schema/builtin/lix_file_descriptor.json +52 -0
  93. package/dist-engine-src/src/schema/builtin/lix_key_value.json +40 -0
  94. package/dist-engine-src/src/schema/builtin/lix_label.json +29 -0
  95. package/dist-engine-src/src/schema/builtin/lix_label_assignment.json +74 -0
  96. package/dist-engine-src/src/schema/builtin/lix_registered_schema.json +25 -0
  97. package/dist-engine-src/src/schema/builtin/lix_version_descriptor.json +34 -0
  98. package/dist-engine-src/src/schema/builtin/lix_version_ref.json +48 -0
  99. package/dist-engine-src/src/schema/builtin/mod.rs +222 -0
  100. package/dist-engine-src/src/schema/compatibility.rs +787 -0
  101. package/dist-engine-src/src/schema/definition.json +187 -0
  102. package/dist-engine-src/src/schema/definition.rs +742 -0
  103. package/dist-engine-src/src/schema/key.rs +138 -0
  104. package/dist-engine-src/src/schema/mod.rs +20 -0
  105. package/dist-engine-src/src/schema/seed.rs +14 -0
  106. package/dist-engine-src/src/schema/tests.rs +780 -0
  107. package/dist-engine-src/src/session/context.rs +364 -0
  108. package/dist-engine-src/src/session/create_version.rs +88 -0
  109. package/dist-engine-src/src/session/execute.rs +478 -0
  110. package/dist-engine-src/src/session/merge/analysis.rs +102 -0
  111. package/dist-engine-src/src/session/merge/apply.rs +23 -0
  112. package/dist-engine-src/src/session/merge/conflicts.rs +63 -0
  113. package/dist-engine-src/src/session/merge/mod.rs +11 -0
  114. package/dist-engine-src/src/session/merge/stats.rs +65 -0
  115. package/dist-engine-src/src/session/merge/version.rs +427 -0
  116. package/dist-engine-src/src/session/mod.rs +27 -0
  117. package/dist-engine-src/src/session/optimization9_sql2_bench.rs +100 -0
  118. package/dist-engine-src/src/session/switch_version.rs +109 -0
  119. package/dist-engine-src/src/sql2/change_provider.rs +331 -0
  120. package/dist-engine-src/src/sql2/classify.rs +182 -0
  121. package/dist-engine-src/src/sql2/context.rs +311 -0
  122. package/dist-engine-src/src/sql2/directory_history_provider.rs +631 -0
  123. package/dist-engine-src/src/sql2/directory_provider.rs +2453 -0
  124. package/dist-engine-src/src/sql2/dml.rs +148 -0
  125. package/dist-engine-src/src/sql2/entity_history_provider.rs +440 -0
  126. package/dist-engine-src/src/sql2/entity_provider.rs +3211 -0
  127. package/dist-engine-src/src/sql2/error.rs +216 -0
  128. package/dist-engine-src/src/sql2/execute.rs +3440 -0
  129. package/dist-engine-src/src/sql2/file_history_provider.rs +910 -0
  130. package/dist-engine-src/src/sql2/file_provider.rs +3679 -0
  131. package/dist-engine-src/src/sql2/filesystem_planner.rs +1490 -0
  132. package/dist-engine-src/src/sql2/filesystem_predicates.rs +159 -0
  133. package/dist-engine-src/src/sql2/filesystem_visibility.rs +383 -0
  134. package/dist-engine-src/src/sql2/history_projection.rs +56 -0
  135. package/dist-engine-src/src/sql2/history_provider.rs +412 -0
  136. package/dist-engine-src/src/sql2/history_route.rs +657 -0
  137. package/dist-engine-src/src/sql2/lix_state_provider.rs +2512 -0
  138. package/dist-engine-src/src/sql2/mod.rs +46 -0
  139. package/dist-engine-src/src/sql2/predicate_typecheck.rs +246 -0
  140. package/dist-engine-src/src/sql2/public_bind/assignment.rs +46 -0
  141. package/dist-engine-src/src/sql2/public_bind/capability.rs +41 -0
  142. package/dist-engine-src/src/sql2/public_bind/dml.rs +166 -0
  143. package/dist-engine-src/src/sql2/public_bind/mod.rs +25 -0
  144. package/dist-engine-src/src/sql2/public_bind/table.rs +168 -0
  145. package/dist-engine-src/src/sql2/read_only.rs +63 -0
  146. package/dist-engine-src/src/sql2/record_batch.rs +17 -0
  147. package/dist-engine-src/src/sql2/result_metadata.rs +29 -0
  148. package/dist-engine-src/src/sql2/runtime.rs +60 -0
  149. package/dist-engine-src/src/sql2/session.rs +132 -0
  150. package/dist-engine-src/src/sql2/udfs/common.rs +295 -0
  151. package/dist-engine-src/src/sql2/udfs/lix_active_version_commit_id.rs +53 -0
  152. package/dist-engine-src/src/sql2/udfs/lix_empty_blob.rs +47 -0
  153. package/dist-engine-src/src/sql2/udfs/lix_json.rs +100 -0
  154. package/dist-engine-src/src/sql2/udfs/lix_json_get.rs +99 -0
  155. package/dist-engine-src/src/sql2/udfs/lix_json_get_text.rs +99 -0
  156. package/dist-engine-src/src/sql2/udfs/lix_text_decode.rs +82 -0
  157. package/dist-engine-src/src/sql2/udfs/lix_text_encode.rs +85 -0
  158. package/dist-engine-src/src/sql2/udfs/lix_timestamp.rs +76 -0
  159. package/dist-engine-src/src/sql2/udfs/lix_uuid_v7.rs +76 -0
  160. package/dist-engine-src/src/sql2/udfs/mod.rs +89 -0
  161. package/dist-engine-src/src/sql2/udfs/public_call.rs +211 -0
  162. package/dist-engine-src/src/sql2/version_provider.rs +1202 -0
  163. package/dist-engine-src/src/sql2/version_scope.rs +394 -0
  164. package/dist-engine-src/src/sql2/write_normalization.rs +345 -0
  165. package/dist-engine-src/src/storage/context.rs +356 -0
  166. package/dist-engine-src/src/storage/mod.rs +14 -0
  167. package/dist-engine-src/src/storage/read_scope.rs +88 -0
  168. package/dist-engine-src/src/storage/types.rs +501 -0
  169. package/dist-engine-src/src/storage_bench.rs +4863 -0
  170. package/dist-engine-src/src/test_support.rs +228 -0
  171. package/dist-engine-src/src/tracked_state/by_file_index.rs +98 -0
  172. package/dist-engine-src/src/tracked_state/codec.rs +2085 -0
  173. package/dist-engine-src/src/tracked_state/context.rs +1867 -0
  174. package/dist-engine-src/src/tracked_state/diff.rs +686 -0
  175. package/dist-engine-src/src/tracked_state/materialization.rs +403 -0
  176. package/dist-engine-src/src/tracked_state/materializer.rs +488 -0
  177. package/dist-engine-src/src/tracked_state/merge.rs +492 -0
  178. package/dist-engine-src/src/tracked_state/mod.rs +32 -0
  179. package/dist-engine-src/src/tracked_state/storage.rs +375 -0
  180. package/dist-engine-src/src/tracked_state/tree.rs +3187 -0
  181. package/dist-engine-src/src/tracked_state/types.rs +231 -0
  182. package/dist-engine-src/src/transaction/commit.rs +1484 -0
  183. package/dist-engine-src/src/transaction/context.rs +1548 -0
  184. package/dist-engine-src/src/transaction/live_state_overlay.rs +35 -0
  185. package/dist-engine-src/src/transaction/mod.rs +13 -0
  186. package/dist-engine-src/src/transaction/normalization.rs +890 -0
  187. package/dist-engine-src/src/transaction/prep.rs +37 -0
  188. package/dist-engine-src/src/transaction/schema_resolver.rs +149 -0
  189. package/dist-engine-src/src/transaction/staging.rs +1731 -0
  190. package/dist-engine-src/src/transaction/types.rs +460 -0
  191. package/dist-engine-src/src/transaction/validation.rs +5830 -0
  192. package/dist-engine-src/src/untracked_state/codec.rs +307 -0
  193. package/dist-engine-src/src/untracked_state/context.rs +98 -0
  194. package/dist-engine-src/src/untracked_state/materialization.rs +63 -0
  195. package/dist-engine-src/src/untracked_state/mod.rs +15 -0
  196. package/dist-engine-src/src/untracked_state/storage.rs +396 -0
  197. package/dist-engine-src/src/untracked_state/types.rs +146 -0
  198. package/dist-engine-src/src/version/context.rs +40 -0
  199. package/dist-engine-src/src/version/lifecycle.rs +221 -0
  200. package/dist-engine-src/src/version/mod.rs +13 -0
  201. package/dist-engine-src/src/version/refs.rs +330 -0
  202. package/dist-engine-src/src/version/stage_rows.rs +67 -0
  203. package/dist-engine-src/src/version/types.rs +21 -0
  204. package/dist-engine-src/src/wasm/mod.rs +60 -0
  205. package/package.json +68 -64
@@ -0,0 +1,82 @@
1
+ use std::any::Any;
2
+ use std::sync::Arc;
3
+
4
+ use datafusion::arrow::array::StringArray;
5
+ use datafusion::arrow::datatypes::DataType;
6
+ use datafusion::common::{plan_err, Result, ScalarValue};
7
+ use datafusion::logical_expr::{
8
+ ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
9
+ };
10
+
11
+ use super::common::{decode_utf8_value, scalar_inputs, validate_utf8_encoding_arg};
12
+
13
+ #[derive(Debug, Clone, PartialEq, Eq, Hash)]
14
+ pub(super) struct LixTextDecode {
15
+ signature: Signature,
16
+ }
17
+
18
+ impl LixTextDecode {
19
+ pub(super) fn new() -> Self {
20
+ Self {
21
+ signature: Signature::one_of(
22
+ vec![Signature::any(1, Volatility::Immutable).type_signature],
23
+ Volatility::Immutable,
24
+ ),
25
+ }
26
+ }
27
+ }
28
+
29
+ impl ScalarUDFImpl for LixTextDecode {
30
+ fn as_any(&self) -> &dyn Any {
31
+ self
32
+ }
33
+
34
+ fn name(&self) -> &str {
35
+ "lix_text_decode"
36
+ }
37
+
38
+ fn signature(&self) -> &Signature {
39
+ &self.signature
40
+ }
41
+
42
+ fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
43
+ Ok(DataType::Utf8)
44
+ }
45
+
46
+ fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
47
+ if !(1..=2).contains(&args.args.len()) {
48
+ return plan_err!("lix_text_decode requires 1 or 2 arguments");
49
+ }
50
+ validate_utf8_encoding_arg(self.name(), args.args.get(1))?;
51
+
52
+ let scalar_inputs = scalar_inputs(&args.args);
53
+ let arrays = ColumnarValue::values_to_arrays(&args.args)?;
54
+ let input = &arrays[0];
55
+ let len = input.len();
56
+
57
+ let mut values = Vec::with_capacity(len);
58
+ for row in 0..len {
59
+ values.push(decode_utf8_value(input.as_ref(), row)?);
60
+ }
61
+ if scalar_inputs {
62
+ Ok(ColumnarValue::Scalar(ScalarValue::Utf8(
63
+ values.into_iter().next().flatten(),
64
+ )))
65
+ } else {
66
+ Ok(ColumnarValue::Array(Arc::new(StringArray::from(values))))
67
+ }
68
+ }
69
+ }
70
+
71
+ #[cfg(test)]
72
+ mod tests {
73
+ use super::super::test_support::single_text;
74
+
75
+ #[tokio::test]
76
+ async fn decodes_utf8_binary_to_text() {
77
+ assert_eq!(
78
+ single_text("SELECT lix_text_decode(X'416461')").await,
79
+ Some("Ada".to_string())
80
+ );
81
+ }
82
+ }
@@ -0,0 +1,85 @@
1
+ use std::any::Any;
2
+
3
+ use datafusion::arrow::datatypes::DataType;
4
+ use datafusion::common::{plan_err, Result, ScalarValue};
5
+ use datafusion::logical_expr::{
6
+ ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
7
+ };
8
+
9
+ use super::common::{
10
+ array_ref, binary_array_from_owned, encode_utf8_value, scalar_inputs,
11
+ validate_utf8_encoding_arg,
12
+ };
13
+
14
+ #[derive(Debug, Clone, PartialEq, Eq, Hash)]
15
+ pub(super) struct LixTextEncode {
16
+ signature: Signature,
17
+ }
18
+
19
+ impl LixTextEncode {
20
+ pub(super) fn new() -> Self {
21
+ Self {
22
+ signature: Signature::one_of(
23
+ vec![Signature::any(1, Volatility::Immutable).type_signature],
24
+ Volatility::Immutable,
25
+ ),
26
+ }
27
+ }
28
+ }
29
+
30
+ impl ScalarUDFImpl for LixTextEncode {
31
+ fn as_any(&self) -> &dyn Any {
32
+ self
33
+ }
34
+
35
+ fn name(&self) -> &str {
36
+ "lix_text_encode"
37
+ }
38
+
39
+ fn signature(&self) -> &Signature {
40
+ &self.signature
41
+ }
42
+
43
+ fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
44
+ Ok(DataType::Binary)
45
+ }
46
+
47
+ fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
48
+ if !(1..=2).contains(&args.args.len()) {
49
+ return plan_err!("lix_text_encode requires 1 or 2 arguments");
50
+ }
51
+ validate_utf8_encoding_arg(self.name(), args.args.get(1))?;
52
+
53
+ let scalar_inputs = scalar_inputs(&args.args);
54
+ let arrays = ColumnarValue::values_to_arrays(&args.args)?;
55
+ let input = &arrays[0];
56
+ let len = input.len();
57
+
58
+ let mut values = Vec::with_capacity(len);
59
+ for row in 0..len {
60
+ values.push(encode_utf8_value(input.as_ref(), row)?);
61
+ }
62
+ if scalar_inputs {
63
+ Ok(ColumnarValue::Scalar(ScalarValue::Binary(
64
+ values.into_iter().next().flatten(),
65
+ )))
66
+ } else {
67
+ Ok(ColumnarValue::Array(array_ref(binary_array_from_owned(
68
+ &values,
69
+ ))))
70
+ }
71
+ }
72
+ }
73
+
74
+ #[cfg(test)]
75
+ mod tests {
76
+ use super::super::test_support::single_binary;
77
+
78
+ #[tokio::test]
79
+ async fn encodes_utf8_text_to_binary() {
80
+ assert_eq!(
81
+ single_binary("SELECT lix_text_encode('Ada')").await,
82
+ Some(b"Ada".to_vec())
83
+ );
84
+ }
85
+ }
@@ -0,0 +1,76 @@
1
+ use std::any::Any;
2
+
3
+ use datafusion::arrow::datatypes::DataType;
4
+ use datafusion::common::{plan_err, Result, ScalarValue};
5
+ use datafusion::logical_expr::{
6
+ ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
7
+ };
8
+
9
+ use crate::functions::FunctionProviderHandle;
10
+
11
+ #[derive(Clone)]
12
+ pub(super) struct LixTimestamp {
13
+ pub(super) functions: FunctionProviderHandle,
14
+ }
15
+
16
+ impl PartialEq for LixTimestamp {
17
+ fn eq(&self, _other: &Self) -> bool {
18
+ true
19
+ }
20
+ }
21
+
22
+ impl Eq for LixTimestamp {}
23
+
24
+ impl std::hash::Hash for LixTimestamp {
25
+ fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
26
+ self.name().hash(state);
27
+ }
28
+ }
29
+
30
+ impl std::fmt::Debug for LixTimestamp {
31
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32
+ f.debug_struct("LixTimestamp").finish()
33
+ }
34
+ }
35
+
36
+ impl ScalarUDFImpl for LixTimestamp {
37
+ fn as_any(&self) -> &dyn Any {
38
+ self
39
+ }
40
+
41
+ fn name(&self) -> &str {
42
+ "lix_timestamp"
43
+ }
44
+
45
+ fn signature(&self) -> &Signature {
46
+ static SIGNATURE: std::sync::LazyLock<Signature> =
47
+ std::sync::LazyLock::new(|| Signature::nullary(Volatility::Volatile));
48
+ &SIGNATURE
49
+ }
50
+
51
+ fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
52
+ Ok(DataType::Utf8)
53
+ }
54
+
55
+ fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
56
+ if !args.args.is_empty() {
57
+ return plan_err!("lix_timestamp requires no arguments");
58
+ }
59
+ Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(
60
+ self.functions.call_timestamp(),
61
+ ))))
62
+ }
63
+ }
64
+
65
+ #[cfg(test)]
66
+ mod tests {
67
+ use super::super::test_support::single_text;
68
+
69
+ #[tokio::test]
70
+ async fn returns_timestamp_text() {
71
+ let value = single_text("SELECT lix_timestamp()")
72
+ .await
73
+ .expect("timestamp should not be null");
74
+ assert!(!value.is_empty());
75
+ }
76
+ }
@@ -0,0 +1,76 @@
1
+ use std::any::Any;
2
+
3
+ use datafusion::arrow::datatypes::DataType;
4
+ use datafusion::common::{plan_err, Result, ScalarValue};
5
+ use datafusion::logical_expr::{
6
+ ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
7
+ };
8
+
9
+ use crate::functions::FunctionProviderHandle;
10
+
11
+ #[derive(Clone)]
12
+ pub(super) struct LixUuidV7 {
13
+ pub(super) functions: FunctionProviderHandle,
14
+ }
15
+
16
+ impl PartialEq for LixUuidV7 {
17
+ fn eq(&self, _other: &Self) -> bool {
18
+ true
19
+ }
20
+ }
21
+
22
+ impl Eq for LixUuidV7 {}
23
+
24
+ impl std::hash::Hash for LixUuidV7 {
25
+ fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
26
+ self.name().hash(state);
27
+ }
28
+ }
29
+
30
+ impl std::fmt::Debug for LixUuidV7 {
31
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32
+ f.debug_struct("LixUuidV7").finish()
33
+ }
34
+ }
35
+
36
+ impl ScalarUDFImpl for LixUuidV7 {
37
+ fn as_any(&self) -> &dyn Any {
38
+ self
39
+ }
40
+
41
+ fn name(&self) -> &str {
42
+ "lix_uuid_v7"
43
+ }
44
+
45
+ fn signature(&self) -> &Signature {
46
+ static SIGNATURE: std::sync::LazyLock<Signature> =
47
+ std::sync::LazyLock::new(|| Signature::nullary(Volatility::Volatile));
48
+ &SIGNATURE
49
+ }
50
+
51
+ fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
52
+ Ok(DataType::Utf8)
53
+ }
54
+
55
+ fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
56
+ if !args.args.is_empty() {
57
+ return plan_err!("lix_uuid_v7 requires no arguments");
58
+ }
59
+ Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(
60
+ self.functions.call_uuid_v7(),
61
+ ))))
62
+ }
63
+ }
64
+
65
+ #[cfg(test)]
66
+ mod tests {
67
+ use super::super::test_support::single_text;
68
+
69
+ #[tokio::test]
70
+ async fn returns_uuid_text() {
71
+ let value = single_text("SELECT lix_uuid_v7()")
72
+ .await
73
+ .expect("uuid should not be null");
74
+ assert!(!value.is_empty());
75
+ }
76
+ }
@@ -0,0 +1,89 @@
1
+ mod common;
2
+ mod lix_active_version_commit_id;
3
+ mod lix_empty_blob;
4
+ mod lix_json;
5
+ mod lix_json_get;
6
+ mod lix_json_get_text;
7
+ mod lix_text_decode;
8
+ mod lix_text_encode;
9
+ mod lix_timestamp;
10
+ mod lix_uuid_v7;
11
+ mod public_call;
12
+
13
+ use datafusion::execution::context::SessionContext;
14
+ use datafusion::logical_expr::ScalarUDF;
15
+
16
+ use crate::functions::FunctionProviderHandle;
17
+
18
+ pub(crate) use public_call::validate_public_udf_calls;
19
+
20
+ #[cfg(test)]
21
+ pub(crate) fn system_sql2_function_provider() -> FunctionProviderHandle {
22
+ use crate::functions::{FunctionProvider, SharedFunctionProvider, SystemFunctionProvider};
23
+
24
+ SharedFunctionProvider::new(Box::new(SystemFunctionProvider) as Box<dyn FunctionProvider + Send>)
25
+ }
26
+
27
+ pub(crate) fn register_sql2_functions(
28
+ ctx: &SessionContext,
29
+ functions: FunctionProviderHandle,
30
+ active_version_commit_id: Option<String>,
31
+ ) {
32
+ ctx.register_udf(ScalarUDF::from(
33
+ lix_active_version_commit_id::LixActiveVersionCommitId::new(active_version_commit_id),
34
+ ));
35
+ ctx.register_udf(ScalarUDF::from(lix_json_get::LixJsonGet::new()));
36
+ ctx.register_udf(ScalarUDF::from(lix_json_get_text::LixJsonGetText::new()));
37
+ ctx.register_udf(ScalarUDF::from(lix_text_decode::LixTextDecode::new()));
38
+ ctx.register_udf(ScalarUDF::from(lix_text_encode::LixTextEncode::new()));
39
+ ctx.register_udf(ScalarUDF::from(lix_json::LixJson));
40
+ ctx.register_udf(ScalarUDF::from(lix_empty_blob::LixEmptyBlob));
41
+ ctx.register_udf(ScalarUDF::from(lix_uuid_v7::LixUuidV7 {
42
+ functions: functions.clone(),
43
+ }));
44
+ ctx.register_udf(ScalarUDF::from(lix_timestamp::LixTimestamp { functions }));
45
+ }
46
+
47
+ #[cfg(test)]
48
+ pub(super) mod test_support {
49
+ use datafusion::arrow::array::{Array, BinaryArray, StringArray};
50
+ use datafusion::prelude::SessionContext;
51
+
52
+ use super::{register_sql2_functions, system_sql2_function_provider};
53
+
54
+ pub(super) async fn single_text(sql: &str) -> Option<String> {
55
+ let ctx = SessionContext::new();
56
+ register_sql2_functions(&ctx, system_sql2_function_provider(), None);
57
+ let batches = ctx
58
+ .sql(sql)
59
+ .await
60
+ .expect("query should plan")
61
+ .collect()
62
+ .await
63
+ .expect("query should execute");
64
+ let array = batches[0]
65
+ .column(0)
66
+ .as_any()
67
+ .downcast_ref::<StringArray>()
68
+ .expect("first column should be utf8");
69
+ (!array.is_null(0)).then(|| array.value(0).to_string())
70
+ }
71
+
72
+ pub(super) async fn single_binary(sql: &str) -> Option<Vec<u8>> {
73
+ let ctx = SessionContext::new();
74
+ register_sql2_functions(&ctx, system_sql2_function_provider(), None);
75
+ let batches = ctx
76
+ .sql(sql)
77
+ .await
78
+ .expect("query should plan")
79
+ .collect()
80
+ .await
81
+ .expect("query should execute");
82
+ let array = batches[0]
83
+ .column(0)
84
+ .as_any()
85
+ .downcast_ref::<BinaryArray>()
86
+ .expect("first column should be binary");
87
+ (!array.is_null(0)).then(|| array.value(0).to_vec())
88
+ }
89
+ }
@@ -0,0 +1,211 @@
1
+ use std::ops::ControlFlow;
2
+
3
+ use datafusion::sql::sqlparser::ast::{
4
+ Expr, Function, FunctionArg, FunctionArgExpr, FunctionArguments, ObjectNamePart, Statement,
5
+ Value, Visit, Visitor,
6
+ };
7
+ use datafusion::sql::sqlparser::dialect::GenericDialect;
8
+ use datafusion::sql::sqlparser::parser::Parser;
9
+
10
+ use crate::LixError;
11
+
12
+ pub(crate) fn validate_public_udf_calls(sql: &str) -> Result<(), LixError> {
13
+ let statements = Parser::parse_sql(&GenericDialect {}, sql).map_err(|error| {
14
+ LixError::new(
15
+ LixError::CODE_PARSE_ERROR,
16
+ format!("sql2 SQL parse error: {error}"),
17
+ )
18
+ })?;
19
+
20
+ let mut visitor = PublicUdfCallVisitor;
21
+ match statements.visit(&mut visitor) {
22
+ ControlFlow::Continue(()) => Ok(()),
23
+ ControlFlow::Break(error) => Err(*error),
24
+ }
25
+ }
26
+
27
+ struct PublicUdfCallVisitor;
28
+
29
+ impl Visitor for PublicUdfCallVisitor {
30
+ type Break = Box<LixError>;
31
+
32
+ fn pre_visit_expr(&mut self, expr: &Expr) -> ControlFlow<Self::Break> {
33
+ let Expr::Function(function) = expr else {
34
+ return ControlFlow::Continue(());
35
+ };
36
+
37
+ match validate_public_function_call(function) {
38
+ Ok(()) => ControlFlow::Continue(()),
39
+ Err(error) => ControlFlow::Break(Box::new(error)),
40
+ }
41
+ }
42
+
43
+ fn pre_visit_statement(&mut self, statement: &Statement) -> ControlFlow<Self::Break> {
44
+ match statement {
45
+ Statement::CreateFunction(_) | Statement::DropFunction(_) => ControlFlow::Continue(()),
46
+ _ => ControlFlow::Continue(()),
47
+ }
48
+ }
49
+ }
50
+
51
+ fn validate_public_function_call(function: &Function) -> Result<(), LixError> {
52
+ let Some(name) = public_lix_function_name(function) else {
53
+ return Ok(());
54
+ };
55
+ let arity = function_arity(&function.args);
56
+
57
+ match name {
58
+ "lix_json" => expect_exact_arity(name, arity, 1),
59
+ "lix_empty_blob" => expect_exact_arity(name, arity, 0),
60
+ "lix_timestamp" => expect_exact_arity(name, arity, 0),
61
+ "lix_uuid_v7" => expect_exact_arity(name, arity, 0),
62
+ "lix_active_version_commit_id" => expect_exact_arity(name, arity, 0),
63
+ "lix_text_encode" | "lix_text_decode" => {
64
+ expect_arity_range(name, arity, 1, 2)?;
65
+ validate_literal_utf8_encoding(name, &function.args)
66
+ }
67
+ _ => Ok(()),
68
+ }
69
+ }
70
+
71
+ fn public_lix_function_name(function: &Function) -> Option<&'static str> {
72
+ let part = function.name.0.last()?;
73
+ let ident = match part {
74
+ ObjectNamePart::Identifier(ident) => ident.value.as_str(),
75
+ ObjectNamePart::Function(_) => return None,
76
+ };
77
+ match ident.to_ascii_lowercase().as_str() {
78
+ "lix_json" => Some("lix_json"),
79
+ "lix_empty_blob" => Some("lix_empty_blob"),
80
+ "lix_timestamp" => Some("lix_timestamp"),
81
+ "lix_uuid_v7" => Some("lix_uuid_v7"),
82
+ "lix_active_version_commit_id" => Some("lix_active_version_commit_id"),
83
+ "lix_text_encode" => Some("lix_text_encode"),
84
+ "lix_text_decode" => Some("lix_text_decode"),
85
+ _ => None,
86
+ }
87
+ }
88
+
89
+ fn function_arity(args: &FunctionArguments) -> usize {
90
+ match args {
91
+ FunctionArguments::None => 0,
92
+ FunctionArguments::Subquery(_) => 1,
93
+ FunctionArguments::List(list) => list.args.len(),
94
+ }
95
+ }
96
+
97
+ fn expect_exact_arity(name: &str, actual: usize, expected: usize) -> Result<(), LixError> {
98
+ if actual == expected {
99
+ return Ok(());
100
+ }
101
+
102
+ let expectation = if expected == 0 {
103
+ "no arguments".to_string()
104
+ } else if expected == 1 {
105
+ "exactly 1 argument".to_string()
106
+ } else {
107
+ format!("exactly {expected} arguments")
108
+ };
109
+ Err(invalid_param(format!("{name} requires {expectation}")))
110
+ }
111
+
112
+ fn expect_arity_range(name: &str, actual: usize, min: usize, max: usize) -> Result<(), LixError> {
113
+ if (min..=max).contains(&actual) {
114
+ return Ok(());
115
+ }
116
+ Err(invalid_param(format!(
117
+ "{name} requires {min} or {max} arguments"
118
+ )))
119
+ }
120
+
121
+ fn validate_literal_utf8_encoding(name: &str, args: &FunctionArguments) -> Result<(), LixError> {
122
+ let Some(encoding) = function_arg(args, 1) else {
123
+ return Ok(());
124
+ };
125
+ let Some(value) = string_literal_arg(encoding) else {
126
+ return Ok(());
127
+ };
128
+ let normalized = value.trim().to_ascii_uppercase().replace('-', "");
129
+ if normalized == "UTF8" {
130
+ Ok(())
131
+ } else {
132
+ Err(invalid_param(format!(
133
+ "{name}() only supports UTF8 encoding, got '{value}'"
134
+ )))
135
+ }
136
+ }
137
+
138
+ fn function_arg(args: &FunctionArguments, index: usize) -> Option<&FunctionArg> {
139
+ match args {
140
+ FunctionArguments::List(list) => list.args.get(index),
141
+ _ => None,
142
+ }
143
+ }
144
+
145
+ fn string_literal_arg(arg: &FunctionArg) -> Option<&str> {
146
+ let expr = match arg {
147
+ FunctionArg::Unnamed(FunctionArgExpr::Expr(expr))
148
+ | FunctionArg::Named {
149
+ arg: FunctionArgExpr::Expr(expr),
150
+ ..
151
+ }
152
+ | FunctionArg::ExprNamed {
153
+ arg: FunctionArgExpr::Expr(expr),
154
+ ..
155
+ } => expr,
156
+ _ => return None,
157
+ };
158
+ let Expr::Value(value) = expr else {
159
+ return None;
160
+ };
161
+ match &value.value {
162
+ Value::SingleQuotedString(value)
163
+ | Value::DoubleQuotedString(value)
164
+ | Value::TripleSingleQuotedString(value)
165
+ | Value::TripleDoubleQuotedString(value)
166
+ | Value::EscapedStringLiteral(value)
167
+ | Value::UnicodeStringLiteral(value)
168
+ | Value::NationalStringLiteral(value)
169
+ | Value::SingleQuotedRawStringLiteral(value)
170
+ | Value::DoubleQuotedRawStringLiteral(value)
171
+ | Value::TripleSingleQuotedRawStringLiteral(value)
172
+ | Value::TripleDoubleQuotedRawStringLiteral(value) => Some(value.as_str()),
173
+ Value::DollarQuotedString(value) => Some(value.value.as_str()),
174
+ _ => None,
175
+ }
176
+ }
177
+
178
+ fn invalid_param(message: impl Into<String>) -> LixError {
179
+ LixError::new(LixError::CODE_INVALID_PARAM, message)
180
+ }
181
+
182
+ #[cfg(test)]
183
+ mod tests {
184
+ use super::validate_public_udf_calls;
185
+
186
+ #[test]
187
+ fn rejects_lix_udf_wrong_arity_as_public_invalid_param() {
188
+ let error = validate_public_udf_calls("SELECT lix_uuid_v7('extra')")
189
+ .expect_err("wrong arity should be rejected");
190
+ assert_eq!(error.code, "LIX_INVALID_PARAM");
191
+ assert!(error.message.contains("lix_uuid_v7 requires no arguments"));
192
+ }
193
+
194
+ #[test]
195
+ fn rejects_unsupported_literal_encoding_as_public_invalid_param() {
196
+ let error = validate_public_udf_calls("SELECT lix_text_encode('Ada', 'base64')")
197
+ .expect_err("unsupported encoding should be rejected");
198
+ assert_eq!(error.code, "LIX_INVALID_PARAM");
199
+ assert!(error
200
+ .message
201
+ .contains("lix_text_encode() only supports UTF8 encoding"));
202
+ }
203
+
204
+ #[test]
205
+ fn accepts_valid_public_lix_udf_calls() {
206
+ validate_public_udf_calls(
207
+ "SELECT lix_json('{\"x\":1}'), lix_text_decode(X'416461', 'utf-8')",
208
+ )
209
+ .expect("valid calls should pass public validation");
210
+ }
211
+ }