@perspective-dev/client 5.6.0 → 5.7.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 (143) hide show
  1. package/README.md +5 -1
  2. package/dist/cdn/perspective.js +3 -2
  3. package/dist/cdn/perspective.js.map +4 -4
  4. package/dist/esm/perspective.browser.d.ts +71 -17
  5. package/dist/esm/perspective.js +2 -2
  6. package/dist/esm/perspective.js.map +4 -4
  7. package/dist/esm/perspective.node.d.ts +20 -3
  8. package/dist/esm/perspective.node.js +694 -34
  9. package/dist/esm/perspective.node.js.map +4 -4
  10. package/dist/esm/public_types.d.ts +31 -0
  11. package/dist/esm/virtual_servers/clickhouse.d.ts +37 -0
  12. package/dist/esm/virtual_servers/clickhouse.js +1 -1
  13. package/dist/esm/virtual_servers/clickhouse.js.map +3 -3
  14. package/dist/esm/virtual_servers/duckdb.d.ts +14 -9
  15. package/dist/esm/virtual_servers/duckdb.js +1 -1
  16. package/dist/esm/virtual_servers/duckdb.js.map +3 -3
  17. package/dist/esm/wasm/cdn.d.ts +12 -0
  18. package/dist/esm/wasm/decompress.d.ts +1 -0
  19. package/dist/esm/wasm/migrate.d.ts +40 -0
  20. package/dist/esm/wasm/registry.d.ts +123 -0
  21. package/dist/esm/wasm/registry.js +3 -0
  22. package/dist/esm/wasm/registry.js.map +7 -0
  23. package/{src/ts/ts-rs/AggSpec.ts → dist/ts-rs/AggSpec.d.ts} +1 -1
  24. package/{src/ts/ts-rs/Features.ts → dist/ts-rs/Features.d.ts} +16 -6
  25. package/{src/ts/ts-rs/Filter.ts → dist/ts-rs/Filter.d.ts} +1 -1
  26. package/{src/ts/ts-rs/FilterTerm.ts → dist/ts-rs/FilterTerm.d.ts} +1 -1
  27. package/{src/ts/ts-rs/JoinOptions.ts → dist/ts-rs/JoinOptions.d.ts} +1 -1
  28. package/dist/ts-rs/ListFlatten.d.ts +3 -0
  29. package/dist/ts-rs/MapFlatten.d.ts +3 -0
  30. package/{src/ts/ts-rs/OnRemoveData.ts → dist/ts-rs/OnRemoveData.d.ts} +1 -1
  31. package/{src/ts/ts-rs/OnUpdateData.ts → dist/ts-rs/OnUpdateData.d.ts} +1 -1
  32. package/{src/ts/ts-rs/OnUpdateOptions.ts → dist/ts-rs/OnUpdateOptions.d.ts} +1 -1
  33. package/{src/ts/ts-rs/Sort.ts → dist/ts-rs/Sort.d.ts} +1 -1
  34. package/dist/ts-rs/StructFlatten.d.ts +3 -0
  35. package/dist/ts-rs/TableIndex.d.ts +9 -0
  36. package/dist/ts-rs/TableInitOptions.d.ts +65 -0
  37. package/{src/ts/ts-rs/UpdateOptions.ts → dist/ts-rs/UpdateOptions.d.ts} +1 -1
  38. package/{src/ts/ts-rs/ViewConfig.ts → dist/ts-rs/ViewConfig.d.ts} +8 -8
  39. package/{src/ts/ts-rs/ViewConfigUpdate.ts → dist/ts-rs/ViewConfigUpdate.d.ts} +8 -8
  40. package/{src/ts/ts-rs/WindowAggSpec.ts → dist/ts-rs/WindowAggSpec.d.ts} +1 -1
  41. package/{src/ts/ts-rs/WindowSort.ts → dist/ts-rs/WindowSort.d.ts} +1 -1
  42. package/{src/ts/ts-rs/WindowSpec.ts → dist/ts-rs/WindowSpec.d.ts} +1 -1
  43. package/{src/ts/ts-rs/Windows.ts → dist/ts-rs/Windows.d.ts} +1 -1
  44. package/dist/wasm/perspective-js.d.ts +121 -54
  45. package/dist/wasm/perspective-js.js +123 -20
  46. package/dist/wasm/perspective-js.wasm +0 -0
  47. package/dist/wasm/perspective-js.wasm.d.ts +10 -6
  48. package/dist/wasm/perspective-migrate-js.d.ts +41 -0
  49. package/dist/wasm/perspective-migrate-js.js +524 -0
  50. package/dist/wasm/perspective-migrate-js.wasm +0 -0
  51. package/dist/wasm/perspective-migrate-js.wasm.d.ts +8 -0
  52. package/package.json +30 -15
  53. package/dist/esm/ts-rs/AggSpec.d.ts +0 -8
  54. package/dist/esm/ts-rs/Aggregate.d.ts +0 -1
  55. package/dist/esm/ts-rs/ColumnType.d.ts +0 -4
  56. package/dist/esm/ts-rs/ColumnWindow.d.ts +0 -4
  57. package/dist/esm/ts-rs/DeleteOptions.d.ts +0 -6
  58. package/dist/esm/ts-rs/Expressions.d.ts +0 -3
  59. package/dist/esm/ts-rs/Features.d.ts +0 -71
  60. package/dist/esm/ts-rs/Filter.d.ts +0 -2
  61. package/dist/esm/ts-rs/FilterReducer.d.ts +0 -1
  62. package/dist/esm/ts-rs/FilterTerm.d.ts +0 -2
  63. package/dist/esm/ts-rs/GroupRollupMode.d.ts +0 -1
  64. package/dist/esm/ts-rs/JoinOptions.d.ts +0 -9
  65. package/dist/esm/ts-rs/JoinType.d.ts +0 -1
  66. package/dist/esm/ts-rs/ListFlatten.d.ts +0 -1
  67. package/dist/esm/ts-rs/OnRemoveData.d.ts +0 -6
  68. package/dist/esm/ts-rs/OnUpdateData.d.ts +0 -6
  69. package/dist/esm/ts-rs/OnUpdateMode.d.ts +0 -9
  70. package/dist/esm/ts-rs/OnUpdateOptions.d.ts +0 -7
  71. package/dist/esm/ts-rs/Scalar.d.ts +0 -5
  72. package/dist/esm/ts-rs/Sort.d.ts +0 -2
  73. package/dist/esm/ts-rs/SortDir.d.ts +0 -1
  74. package/dist/esm/ts-rs/SplitRollupMode.d.ts +0 -9
  75. package/dist/esm/ts-rs/SystemInfo.d.ts +0 -40
  76. package/dist/esm/ts-rs/TableInitOptions.d.ts +0 -45
  77. package/dist/esm/ts-rs/TableReadFormat.d.ts +0 -7
  78. package/dist/esm/ts-rs/TypedArrayWindow.d.ts +0 -33
  79. package/dist/esm/ts-rs/UpdateOptions.d.ts +0 -8
  80. package/dist/esm/ts-rs/ViewConfig.d.ts +0 -24
  81. package/dist/esm/ts-rs/ViewConfigUpdate.d.ts +0 -116
  82. package/dist/esm/ts-rs/ViewOnRemoveResp.d.ts +0 -4
  83. package/dist/esm/ts-rs/ViewOnUpdateResp.d.ts +0 -4
  84. package/dist/esm/ts-rs/ViewWindow.d.ts +0 -30
  85. package/dist/esm/ts-rs/WindowAggSpec.d.ts +0 -25
  86. package/dist/esm/ts-rs/WindowSort.d.ts +0 -7
  87. package/dist/esm/ts-rs/WindowSortDir.d.ts +0 -6
  88. package/dist/esm/ts-rs/WindowSpec.d.ts +0 -47
  89. package/dist/esm/ts-rs/Windows.d.ts +0 -10
  90. package/src/rust/client.rs +0 -590
  91. package/src/rust/generic_sql_model.rs +0 -276
  92. package/src/rust/lib.rs +0 -99
  93. package/src/rust/table.rs +0 -397
  94. package/src/rust/table_data.rs +0 -164
  95. package/src/rust/typed_array.rs +0 -386
  96. package/src/rust/utils/browser.rs +0 -35
  97. package/src/rust/utils/console_logger.rs +0 -237
  98. package/src/rust/utils/errors.rs +0 -274
  99. package/src/rust/utils/futures.rs +0 -274
  100. package/src/rust/utils/json.rs +0 -252
  101. package/src/rust/utils/local_poll_loop.rs +0 -63
  102. package/src/rust/utils/mod.rs +0 -32
  103. package/src/rust/utils/serde.rs +0 -46
  104. package/src/rust/utils/trace_allocator.rs +0 -98
  105. package/src/rust/view.rs +0 -463
  106. package/src/rust/virtual_server.rs +0 -991
  107. package/src/ts/perspective-server.worker.ts +0 -74
  108. package/src/ts/perspective.browser.ts +0 -380
  109. package/src/ts/perspective.cdn.ts +0 -28
  110. package/src/ts/perspective.node.ts +0 -556
  111. package/src/ts/ts-rs/ListFlatten.ts +0 -3
  112. package/src/ts/ts-rs/TableInitOptions.ts +0 -42
  113. package/src/ts/virtual_server.ts +0 -83
  114. package/src/ts/virtual_servers/clickhouse.ts +0 -533
  115. package/src/ts/virtual_servers/duckdb.ts +0 -549
  116. package/src/ts/wasm/browser.ts +0 -134
  117. package/src/ts/wasm/cdn.ts +0 -32
  118. package/src/ts/wasm/decompress.ts +0 -64
  119. package/src/ts/wasm/emscripten_api.ts +0 -91
  120. package/src/ts/wasm/engine.ts +0 -309
  121. package/src/ts/wasm/features.ts +0 -31
  122. package/src/ts/wasm/perspective-server.poly.ts +0 -460
  123. package/src/ts/websocket.ts +0 -95
  124. package/tsconfig.json +0 -22
  125. /package/{src/ts/ts-rs/Aggregate.ts → dist/ts-rs/Aggregate.d.ts} +0 -0
  126. /package/{src/ts/ts-rs/ColumnType.ts → dist/ts-rs/ColumnType.d.ts} +0 -0
  127. /package/{src/ts/ts-rs/ColumnWindow.ts → dist/ts-rs/ColumnWindow.d.ts} +0 -0
  128. /package/{src/ts/ts-rs/DeleteOptions.ts → dist/ts-rs/DeleteOptions.d.ts} +0 -0
  129. /package/{src/ts/ts-rs/Expressions.ts → dist/ts-rs/Expressions.d.ts} +0 -0
  130. /package/{src/ts/ts-rs/FilterReducer.ts → dist/ts-rs/FilterReducer.d.ts} +0 -0
  131. /package/{src/ts/ts-rs/GroupRollupMode.ts → dist/ts-rs/GroupRollupMode.d.ts} +0 -0
  132. /package/{src/ts/ts-rs/JoinType.ts → dist/ts-rs/JoinType.d.ts} +0 -0
  133. /package/{src/ts/ts-rs/OnUpdateMode.ts → dist/ts-rs/OnUpdateMode.d.ts} +0 -0
  134. /package/{src/ts/ts-rs/Scalar.ts → dist/ts-rs/Scalar.d.ts} +0 -0
  135. /package/{src/ts/ts-rs/SortDir.ts → dist/ts-rs/SortDir.d.ts} +0 -0
  136. /package/{src/ts/ts-rs/SplitRollupMode.ts → dist/ts-rs/SplitRollupMode.d.ts} +0 -0
  137. /package/{src/ts/ts-rs/SystemInfo.ts → dist/ts-rs/SystemInfo.d.ts} +0 -0
  138. /package/{src/ts/ts-rs/TableReadFormat.ts → dist/ts-rs/TableReadFormat.d.ts} +0 -0
  139. /package/{src/ts/ts-rs/TypedArrayWindow.ts → dist/ts-rs/TypedArrayWindow.d.ts} +0 -0
  140. /package/{src/ts/ts-rs/ViewOnRemoveResp.ts → dist/ts-rs/ViewOnRemoveResp.d.ts} +0 -0
  141. /package/{src/ts/ts-rs/ViewOnUpdateResp.ts → dist/ts-rs/ViewOnUpdateResp.d.ts} +0 -0
  142. /package/{src/ts/ts-rs/ViewWindow.ts → dist/ts-rs/ViewWindow.d.ts} +0 -0
  143. /package/{src/ts/ts-rs/WindowSortDir.ts → dist/ts-rs/WindowSortDir.d.ts} +0 -0
@@ -1,386 +0,0 @@
1
- // ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2
- // ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
3
- // ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
4
- // ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
5
- // ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
6
- // ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7
- // ┃ Copyright (c) 2017, the Perspective Authors. ┃
8
- // ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9
- // ┃ This file is part of the Perspective library, distributed under the terms ┃
10
- // ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11
- // ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
-
13
- use std::io::Cursor;
14
-
15
- use arrow_array::cast::AsArray;
16
- use arrow_array::types::*;
17
- use arrow_array::{
18
- Array as _, ArrayRef, ArrowPrimitiveType, DictionaryArray, PrimitiveArray, StringArray,
19
- };
20
- use arrow_ipc::reader::StreamReader;
21
- use arrow_schema::{DataType, TimeUnit};
22
- use js_sys::{Array, Function, JsString, Uint8Array};
23
- use perspective_client::ViewWindow;
24
- use ts_rs::TS;
25
- use wasm_bindgen::JsCast;
26
- use wasm_bindgen::prelude::*;
27
-
28
- #[wasm_bindgen]
29
- unsafe extern "C" {
30
- #[wasm_bindgen(typescript_type = "TypedArrayWindow")]
31
- #[derive(Clone)]
32
- pub type JsTypedArrayWindow;
33
- }
34
-
35
- /// Options for `with_typed_arrays`, extending `ViewWindow` with
36
- /// typed-array-specific options.
37
- #[derive(Default, serde::Deserialize, TS)]
38
- pub struct TypedArrayWindow {
39
- #[serde(flatten)]
40
- pub view_window: ViewWindow,
41
-
42
- /// When `true`, Float64/Date32/Timestamp columns are output as
43
- /// `Float32Array` instead of `Float64Array`.
44
- #[serde(default)]
45
- pub float32: bool,
46
- }
47
-
48
- impl From<TypedArrayWindow> for ViewWindow {
49
- fn from(w: TypedArrayWindow) -> Self {
50
- w.view_window
51
- }
52
- }
53
-
54
- fn zero_invalid_slots<T: ArrowPrimitiveType>(arr: &PrimitiveArray<T>) {
55
- let Some(nulls) = arr.nulls() else { return };
56
- let ptr = arr.values().as_ptr() as *mut T::Native;
57
- let chunks = nulls.inner().bit_chunks();
58
- let mut base = 0;
59
- for chunk in chunks.iter() {
60
- if chunk != u64::MAX {
61
- for bit in 0..64 {
62
- if chunk & (1 << bit) == 0 {
63
- unsafe { ptr.add(base + bit).write(T::default_value()) };
64
- }
65
- }
66
- }
67
-
68
- base += 64;
69
- }
70
-
71
- let rem = chunks.remainder_bits();
72
- for bit in 0..chunks.remainder_len() {
73
- if rem & (1 << bit) == 0 {
74
- unsafe { ptr.add(base + bit).write(T::default_value()) };
75
- }
76
- }
77
- }
78
-
79
- /// Emit a sub-32-bit integer column as an `Int32Array`. Every Arrow
80
- /// integer width the engine emits below 32 bits (`i8`/`u8`/`i16`/`u16`)
81
- /// widens losslessly, so consumers see ONE integer representation
82
- /// regardless of the column's storage width — and, like `Int32`, one
83
- /// the `float32` flag never narrows. Needs a copy; `Box<[i32]>` gives
84
- /// the stable data pointer the zero-copy `view` requires, exactly as
85
- /// the `f32`/`f64` conversion buffers do.
86
- fn set_widened_i32<T>(
87
- col: &ArrayRef,
88
- col_idx: usize,
89
- js_values: &Array,
90
- js_dicts: &Array,
91
- storage: &mut Vec<Box<[i32]>>,
92
- ) where
93
- T: ArrowPrimitiveType,
94
- T::Native: Into<i32>,
95
- {
96
- let typed = col.as_primitive::<T>();
97
- zero_invalid_slots(typed);
98
- let vals: Box<[i32]> = typed.values().iter().map(|&v| v.into()).collect();
99
-
100
- let arr = unsafe { js_sys::Int32Array::view(&vals) };
101
- storage.push(vals);
102
- js_values.set(col_idx as u32, arr.into());
103
- js_dicts.set(col_idx as u32, JsValue::NULL);
104
- }
105
-
106
- /// Decode an Arrow IPC batch and call `callback` once with all columns.
107
- ///
108
- /// Callback signature:
109
- /// ```js
110
- /// callback(names: string[], values: TypedArray[], validities: (Uint8Array|null)[], dictionaries: (string[]|null)[]) => void | Promise<void>
111
- /// ```
112
- ///
113
- /// If the callback returns a `Promise`, it is awaited before the Arrow
114
- /// batch (and therefore the zero-copy typed-array views into it) is
115
- /// dropped. A synchronous callback returning `undefined` is supported
116
- /// with no promise-handling overhead.
117
- pub(crate) async fn decode_and_call(
118
- arrow: &[u8],
119
- float32: bool,
120
- callback: &Function,
121
- ) -> Result<(), JsValue> {
122
- let cursor = Cursor::new(arrow);
123
- let reader = StreamReader::try_new(cursor, None)
124
- .map_err(|e| JsValue::from_str(&format!("Arrow decode error: {e}")))?;
125
-
126
- let batch = reader
127
- .into_iter()
128
- .next()
129
- .ok_or_else(|| JsValue::from_str("Arrow IPC contained no record batches"))?
130
- .map_err(|e| JsValue::from_str(&format!("Arrow batch error: {e}")))?;
131
-
132
- let schema = batch.schema();
133
- let num_cols = batch.num_columns();
134
-
135
- let js_names = Array::new_with_length(num_cols as u32);
136
- let js_values = Array::new_with_length(num_cols as u32);
137
- let js_validities = Array::new_with_length(num_cols as u32);
138
- let js_dicts = Array::new_with_length(num_cols as u32);
139
-
140
- // Storage for type-conversion buffers (narrow-int/bool widening,
141
- // Int64/UInt64/Date32/Timestamp and `float32` narrowing). These
142
- // MUST outlive the callback because `js_sys::*Array::view()`
143
- // creates zero-copy views into their heap memory. Using `Box<[T]>`
144
- // (rather than `Vec<T>`) yields a stable data pointer that won't
145
- // move when the outer Vec grows, so a view created before the push
146
- // stays valid.
147
- let mut i32_storage: Vec<Box<[i32]>> = Vec::new();
148
- let mut f32_storage: Vec<Box<[f32]>> = Vec::new();
149
- let mut f64_storage: Vec<Box<[f64]>> = Vec::new();
150
-
151
- // The bytes under a NULL slot in the source Arrow are UNDEFINED —
152
- // the perspective engine zero-fills them but e.g. DuckDB's Arrow
153
- // output leaves NaN (which, unlike the garbage a consumer merely
154
- // *displays* wrong, poisons any consumer that aggregates, e.g. the
155
- // treemap's bottom-up value sums). `zero_invalid_slots` normalizes
156
- // every column in place — forcing invalid slots to 0 so all backends
157
- // present the same value contract — without giving up the zero-copy
158
- // view.
159
- for col_idx in 0..num_cols {
160
- let field = schema.field(col_idx);
161
- let col = batch.column(col_idx);
162
- let validity = col.nulls().map(|nulls| nulls.validity());
163
-
164
- js_names.set(col_idx as u32, JsString::from(field.name().as_str()).into());
165
-
166
- match col.data_type() {
167
- DataType::Int8 => {
168
- set_widened_i32::<Int8Type>(col, col_idx, &js_values, &js_dicts, &mut i32_storage);
169
- },
170
- DataType::UInt8 => {
171
- set_widened_i32::<UInt8Type>(col, col_idx, &js_values, &js_dicts, &mut i32_storage);
172
- },
173
- DataType::Int16 => {
174
- set_widened_i32::<Int16Type>(col, col_idx, &js_values, &js_dicts, &mut i32_storage);
175
- },
176
- DataType::UInt16 => {
177
- set_widened_i32::<UInt16Type>(
178
- col,
179
- col_idx,
180
- &js_values,
181
- &js_dicts,
182
- &mut i32_storage,
183
- );
184
- },
185
- DataType::UInt32 => {
186
- let typed = col.as_primitive::<UInt32Type>();
187
- zero_invalid_slots(typed);
188
- let arr = unsafe { js_sys::Uint32Array::view(typed.values().as_ref()) };
189
- js_values.set(col_idx as u32, arr.into());
190
- js_dicts.set(col_idx as u32, JsValue::NULL);
191
- },
192
- DataType::Int32 => {
193
- let typed = col.as_primitive::<Int32Type>();
194
- zero_invalid_slots(typed);
195
- let arr = unsafe { js_sys::Int32Array::view(typed.values().as_ref()) };
196
- js_values.set(col_idx as u32, arr.into());
197
- js_dicts.set(col_idx as u32, JsValue::NULL);
198
- },
199
- DataType::Float32 => {
200
- let typed = col.as_primitive::<Float32Type>();
201
- zero_invalid_slots(typed);
202
- let arr = unsafe { js_sys::Float32Array::view(typed.values().as_ref()) };
203
- js_values.set(col_idx as u32, arr.into());
204
- js_dicts.set(col_idx as u32, JsValue::NULL);
205
- },
206
- DataType::Float64 => {
207
- let typed = col.as_primitive::<Float64Type>();
208
- zero_invalid_slots(typed);
209
- if float32 {
210
- let vals: Box<[f32]> = typed.values().iter().map(|&v| v as f32).collect();
211
-
212
- let arr = unsafe { js_sys::Float32Array::view(&vals) };
213
- f32_storage.push(vals);
214
- js_values.set(col_idx as u32, arr.into());
215
- } else {
216
- let arr = unsafe { js_sys::Float64Array::view(typed.values().as_ref()) };
217
-
218
- js_values.set(col_idx as u32, arr.into());
219
- }
220
-
221
- js_dicts.set(col_idx as u32, JsValue::NULL);
222
- },
223
- DataType::Date32 => {
224
- // Datetime values are always emitted as Float64 — narrowing
225
- // epoch-ms to f32 collapses ~256 ms of resolution at modern
226
- // timestamps, so the `float32` flag is intentionally ignored
227
- // for date/timestamp columns.
228
- let typed = col.as_primitive::<Date32Type>();
229
- zero_invalid_slots(typed);
230
- let vals: Box<[f64]> = typed
231
- .values()
232
- .iter()
233
- .map(|&v| v as f64 * 86_400_000.0)
234
- .collect();
235
-
236
- let arr = unsafe { js_sys::Float64Array::view(&vals) };
237
- f64_storage.push(vals);
238
- js_values.set(col_idx as u32, arr.into());
239
- js_dicts.set(col_idx as u32, JsValue::NULL);
240
- },
241
- DataType::Timestamp(TimeUnit::Millisecond, _) => {
242
- let typed = col.as_primitive::<TimestampMillisecondType>();
243
- zero_invalid_slots(typed);
244
- let vals: Box<[f64]> = typed.values().iter().map(|&v| v as f64).collect();
245
-
246
- let arr = unsafe { js_sys::Float64Array::view(&vals) };
247
- f64_storage.push(vals);
248
- js_values.set(col_idx as u32, arr.into());
249
- js_dicts.set(col_idx as u32, JsValue::NULL);
250
- },
251
- DataType::Int64 => {
252
- let typed = col.as_primitive::<Int64Type>();
253
- zero_invalid_slots(typed);
254
- if float32 {
255
- let vals: Box<[f32]> = typed.values().iter().map(|&v| v as f32).collect();
256
-
257
- let arr = unsafe { js_sys::Float32Array::view(&vals) };
258
- f32_storage.push(vals);
259
- js_values.set(col_idx as u32, arr.into());
260
- } else {
261
- let vals: Box<[f64]> = typed.values().iter().map(|&v| v as f64).collect();
262
-
263
- let arr = unsafe { js_sys::Float64Array::view(&vals) };
264
- f64_storage.push(vals);
265
- js_values.set(col_idx as u32, arr.into());
266
- }
267
-
268
- js_dicts.set(col_idx as u32, JsValue::NULL);
269
- },
270
- // Neither `u64` nor `i64` fits `Int32Array`, so both widen
271
- // to float (narrowed by `float32` like the other float
272
- // columns) and lose exactness past 2^53. `UInt64` is not an
273
- // exotic case: `get_simple_accumulator_type` promotes EVERY
274
- // unsigned width to `DTYPE_UINT64`, so the `sum` of any
275
- // unsigned column in a `group_by` view lands here.
276
- DataType::UInt64 => {
277
- let typed = col.as_primitive::<UInt64Type>();
278
- zero_invalid_slots(typed);
279
- if float32 {
280
- let vals: Box<[f32]> = typed.values().iter().map(|&v| v as f32).collect();
281
-
282
- let arr = unsafe { js_sys::Float32Array::view(&vals) };
283
- f32_storage.push(vals);
284
- js_values.set(col_idx as u32, arr.into());
285
- } else {
286
- let vals: Box<[f64]> = typed.values().iter().map(|&v| v as f64).collect();
287
-
288
- let arr = unsafe { js_sys::Float64Array::view(&vals) };
289
- f64_storage.push(vals);
290
- js_values.set(col_idx as u32, arr.into());
291
- }
292
-
293
- js_dicts.set(col_idx as u32, JsValue::NULL);
294
- },
295
- DataType::Boolean => {
296
- // Bit-packed, so `zero_invalid_slots` (a `PrimitiveArray`
297
- // memory rewrite) cannot apply — the `is_valid` test
298
- // below enforces the same "invalid slots read 0"
299
- // contract while materializing.
300
- let typed = col.as_boolean();
301
- let vals: Box<[i32]> = (0..typed.len())
302
- .map(|i| i32::from(typed.is_valid(i) && typed.value(i)))
303
- .collect();
304
-
305
- let arr = unsafe { js_sys::Int32Array::view(&vals) };
306
- i32_storage.push(vals);
307
- js_values.set(col_idx as u32, arr.into());
308
- js_dicts.set(col_idx as u32, JsValue::NULL);
309
- },
310
- DataType::Dictionary(..) => {
311
- let dict = col
312
- .as_any()
313
- .downcast_ref::<DictionaryArray<Int32Type>>()
314
- .ok_or_else(|| {
315
- JsValue::from_str(&format!(
316
- "Unsupported dictionary key type for typed array: {}",
317
- col.data_type()
318
- ))
319
- })?;
320
-
321
- let keys = dict.keys();
322
- zero_invalid_slots(keys);
323
- let arr = unsafe { js_sys::Int32Array::view(keys.values().as_ref()) };
324
- js_values.set(col_idx as u32, arr.into());
325
-
326
- let values = dict
327
- .values()
328
- .as_any()
329
- .downcast_ref::<StringArray>()
330
- .ok_or_else(|| {
331
- JsValue::from_str(&format!(
332
- "Unsupported dictionary value type for typed array: {}",
333
- dict.values().data_type()
334
- ))
335
- })?;
336
-
337
- let js_dict = Array::new_with_length(values.len() as u32);
338
- for i in 0..values.len() {
339
- js_dict.set(i as u32, JsValue::from_str(values.value(i)));
340
- }
341
- js_dicts.set(col_idx as u32, js_dict.into());
342
- },
343
- dt => {
344
- return Err(JsValue::from_str(&format!(
345
- "Unsupported column type for typed array: {dt}"
346
- )));
347
- },
348
- }
349
-
350
- // SAFETY: Validity bitmap is owned by `batch` which outlives the
351
- // callback — safe to view zero-copy.
352
- let js_validity = validity.map(|v| unsafe { Uint8Array::view(v) });
353
- js_validities.set(
354
- col_idx as u32,
355
- js_validity
356
- .as_ref()
357
- .map(JsValue::from)
358
- .unwrap_or(JsValue::NULL),
359
- );
360
- }
361
-
362
- let ret = callback.call4(
363
- &JsValue::UNDEFINED,
364
- &js_names.into(),
365
- &js_values.into(),
366
- &js_validities.into(),
367
- &js_dicts.into(),
368
- )?;
369
-
370
- // If the callback returned a Promise, await it before releasing the
371
- // batch — zero-copy TypedArray views into `batch` and the
372
- // `i32`/`f32`/`f64` conversion buffers must remain valid for the
373
- // full lifetime of the awaited work.
374
- if ret.is_instance_of::<js_sys::Promise>() {
375
- let promise: js_sys::Promise = ret.unchecked_into();
376
- wasm_bindgen_futures::JsFuture::from(promise).await?;
377
- }
378
-
379
- // Keep storage alive until after the callback (and its awaited
380
- // promise, if any) returns.
381
- drop(i32_storage);
382
- drop(f32_storage);
383
- drop(f64_storage);
384
-
385
- Ok(())
386
- }
@@ -1,35 +0,0 @@
1
- // ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2
- // ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
3
- // ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
4
- // ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
5
- // ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
6
- // ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7
- // ┃ Copyright (c) 2017, the Perspective Authors. ┃
8
- // ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9
- // ┃ This file is part of the Perspective library, distributed under the terms ┃
10
- // ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11
- // ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
-
13
- /// Convenient short-name functions for common browser globals. These types are
14
- /// not `send` and cannot be made static.
15
- pub mod global {
16
- pub fn window() -> web_sys::Window {
17
- web_sys::window().unwrap()
18
- }
19
-
20
- pub fn document() -> web_sys::Document {
21
- window().document().unwrap()
22
- }
23
-
24
- pub fn performance() -> web_sys::Performance {
25
- window().performance().unwrap()
26
- }
27
-
28
- pub fn body() -> web_sys::HtmlElement {
29
- document().body().unwrap()
30
- }
31
-
32
- pub fn navigator() -> web_sys::Navigator {
33
- window().navigator()
34
- }
35
- }
@@ -1,237 +0,0 @@
1
- // ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2
- // ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
3
- // ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
4
- // ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
5
- // ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
6
- // ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7
- // ┃ Copyright (c) 2017, the Perspective Authors. ┃
8
- // ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9
- // ┃ This file is part of the Perspective library, distributed under the terms ┃
10
- // ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11
- // ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
-
13
- use std::fmt::{Debug, Write};
14
- use std::sync::OnceLock;
15
-
16
- use tracing::Subscriber;
17
- use tracing::field::{Field, Visit};
18
- use tracing_subscriber::Layer;
19
- use tracing_subscriber::layer::Context;
20
- use tracing_subscriber::registry::LookupSpan;
21
- use wasm_bindgen::intern;
22
- use wasm_bindgen::prelude::*;
23
-
24
- use crate::utils::*;
25
-
26
- /// A struct to implement the `Visit` visitor pattern trait to process
27
- /// `tracing::Event`s.
28
- #[derive(Default)]
29
- struct LogLineBuffer {
30
- value: String,
31
- is_tail: bool,
32
- }
33
-
34
- impl Visit for LogLineBuffer {
35
- fn record_debug(&mut self, field: &Field, value: &dyn Debug) {
36
- if field.name() == "message" {
37
- if !self.value.is_empty() {
38
- self.value = format!("{:?}\n{}", value, self.value)
39
- } else {
40
- self.value = format!("{value:?}")
41
- }
42
- } else {
43
- if self.is_tail {
44
- writeln!(self.value).unwrap();
45
- } else {
46
- write!(self.value, " ").unwrap();
47
- self.is_tail = true;
48
- }
49
-
50
- write!(self.value, "{} = {:?};", field.name(), value).unwrap();
51
- }
52
- }
53
- }
54
-
55
- #[extend::ext]
56
- impl tracing::Level {
57
- /// Convert a `tracing::Level` to an equivalent 4-arg call to the
58
- /// browser console via the `web_sys::console` module.
59
- fn web_logger_4(&self) -> fn(&JsValue, &JsValue, &JsValue, &JsValue) {
60
- match *self {
61
- tracing::Level::TRACE => web_sys::console::trace_4,
62
- tracing::Level::DEBUG => web_sys::console::debug_4,
63
- tracing::Level::INFO => web_sys::console::info_4,
64
- tracing::Level::WARN => web_sys::console::warn_4,
65
- tracing::Level::ERROR => web_sys::console::error_4,
66
- }
67
- }
68
-
69
- fn web_logger_1(&self) -> fn(&JsValue) {
70
- match *self {
71
- tracing::Level::TRACE => web_sys::console::trace_1,
72
- tracing::Level::DEBUG => web_sys::console::debug_1,
73
- tracing::Level::INFO => web_sys::console::info_1,
74
- tracing::Level::WARN => web_sys::console::warn_1,
75
- tracing::Level::ERROR => web_sys::console::error_1,
76
- }
77
- }
78
-
79
- /// Return a pretty color theme for a `tracing::Level`.
80
- fn web_log_color(&self) -> &'static str {
81
- match *self {
82
- tracing::Level::TRACE => "background: #005F73; color: #000",
83
- tracing::Level::DEBUG => "background: #0A9396; color: #000",
84
- tracing::Level::INFO => "background: #E9D8A6; color: #000",
85
- tracing::Level::WARN => "background: #EE9B00; color: #000",
86
- tracing::Level::ERROR => "background: #AE2012; color: #000",
87
- }
88
- }
89
- }
90
-
91
- static IS_CHROME: OnceLock<bool> = OnceLock::new();
92
-
93
- fn detect_chrome() -> bool {
94
- if let Some(window) = web_sys::window() {
95
- window.get("chrome").is_some()
96
- } else {
97
- true
98
- }
99
- }
100
-
101
- #[extend::ext]
102
- impl<'a> tracing::Metadata<'a> {
103
- /// Log a message in the style of `WasmLogger`.
104
- fn console_log(&self, msg: &str) {
105
- let level = self.level();
106
- let origin = self
107
- .module_path()
108
- .and_then(|file| self.line().map(|ln| format!("{}:{}", &file[11..], ln)))
109
- .unwrap_or_default();
110
-
111
- if *IS_CHROME.get_or_init(detect_chrome) {
112
- level.web_logger_4()(
113
- &format!("%c {level} %c {origin}%c {msg} ").into(),
114
- &level.web_log_color().into(),
115
- &intern("color: gray; font-style: italic").into(),
116
- &intern("color: inherit").into(),
117
- );
118
- } else {
119
- level.web_logger_1()(&format!("{origin} {msg}").into());
120
- }
121
- }
122
- }
123
-
124
- /// A custom logger modelled afer the `tracing_wasm` crate.
125
- struct WasmLogger {
126
- max_level: tracing::Level,
127
- }
128
-
129
- impl Default for WasmLogger {
130
- fn default() -> Self {
131
- Self {
132
- max_level: tracing::Level::WARN,
133
- }
134
- }
135
- }
136
-
137
- impl<S: Subscriber + for<'a> LookupSpan<'a>> Layer<S> for WasmLogger {
138
- fn on_event(
139
- &self,
140
- event: &tracing::Event<'_>,
141
- _ctx: tracing_subscriber::layer::Context<'_, S>,
142
- ) {
143
- let mut recorder = LogLineBuffer::default();
144
- event.record(&mut recorder);
145
- event.metadata().console_log(&recorder.value);
146
- }
147
-
148
- fn enabled(&self, metadata: &tracing::Metadata<'_>, _: Context<'_, S>) -> bool {
149
- let level = metadata.level();
150
- level <= &self.max_level
151
- }
152
-
153
- fn on_new_span(
154
- &self,
155
- attrs: &tracing::span::Attributes<'_>,
156
- id: &tracing::Id,
157
- ctx: Context<'_, S>,
158
- ) {
159
- let mut new_debug_record = LogLineBuffer::default();
160
- attrs.record(&mut new_debug_record);
161
- if let Some(span_ref) = ctx.span(id) {
162
- span_ref
163
- .extensions_mut()
164
- .insert::<LogLineBuffer>(new_debug_record);
165
- }
166
-
167
- global::performance()
168
- .mark(&format!("t{:x}", id.into_u64()))
169
- .unwrap();
170
- }
171
-
172
- fn on_record(&self, id: &tracing::Id, values: &tracing::span::Record<'_>, ctx: Context<'_, S>) {
173
- if let Some(span_ref) = ctx.span(id)
174
- && let Some(debug_record) = span_ref.extensions_mut().get_mut::<LogLineBuffer>()
175
- {
176
- values.record(debug_record);
177
- }
178
- }
179
-
180
- fn on_close(&self, id: tracing::Id, ctx: Context<'_, S>) {
181
- if let Some(span_ref) = ctx.span(&id) {
182
- let perf = global::performance();
183
- let meta = span_ref.metadata();
184
- let mark = format!("t{:x}", id.into_u64());
185
- let start = perf
186
- .get_entries_by_name_with_entry_type(&mark, "mark")
187
- .at(-1)
188
- .unchecked_into::<web_sys::PerformanceMark>()
189
- .start_time();
190
-
191
- meta.console_log(&format!("{:.0}ms", perf.now() - start));
192
- let msg = format!(
193
- "\"{}\" {} {}",
194
- meta.name(),
195
- meta.module_path().unwrap_or_default(),
196
- span_ref
197
- .extensions()
198
- .get::<LogLineBuffer>()
199
- .map(|x| &x.value[..])
200
- .unwrap_or_default(),
201
- );
202
-
203
- perf.measure_with_start_mark(&msg, &mark).unwrap();
204
- }
205
- }
206
- }
207
-
208
- /// Configure `WasmLogger` as a global default for tracing.
209
- ///
210
- /// This operation will conflict with any other library which sets a global
211
- /// default `tracing::Subscriber`, so it should not be called when `perspective`
212
- /// is used as a library from a larger app; in this case the app itself should
213
- /// configure `tracing` explicitly.
214
- pub fn set_global_logging() {
215
- static INIT_LOGGING: OnceLock<()> = OnceLock::new();
216
- INIT_LOGGING.get_or_init(|| {
217
- if !*IS_CHROME.get_or_init(detect_chrome) {
218
- web_sys::console::warn_1(
219
- &"Unknown browser detected. Some features may not work, and performance may be \
220
- degraded."
221
- .into(),
222
- );
223
- }
224
-
225
- use tracing_subscriber::layer::SubscriberExt;
226
- let filter = tracing_subscriber::filter::filter_fn(|meta| {
227
- meta.module_path()
228
- .as_ref()
229
- .map(|x| x.starts_with("perspective"))
230
- .unwrap_or_default()
231
- });
232
-
233
- let layer = WasmLogger::default().with_filter(filter);
234
- let subscriber = tracing_subscriber::Registry::default().with(layer);
235
- tracing::subscriber::set_global_default(subscriber).unwrap();
236
- });
237
- }