@perspective-dev/client 5.2.0 → 5.3.1

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.
@@ -14,7 +14,9 @@ use std::io::Cursor;
14
14
 
15
15
  use arrow_array::cast::AsArray;
16
16
  use arrow_array::types::*;
17
- use arrow_array::{Array as _, ArrowPrimitiveType, DictionaryArray, PrimitiveArray, StringArray};
17
+ use arrow_array::{
18
+ Array as _, ArrayRef, ArrowPrimitiveType, DictionaryArray, PrimitiveArray, StringArray,
19
+ };
18
20
  use arrow_ipc::reader::StreamReader;
19
21
  use arrow_schema::{DataType, TimeUnit};
20
22
  use js_sys::{Array, Function, JsString, Uint8Array};
@@ -74,6 +76,33 @@ fn zero_invalid_slots<T: ArrowPrimitiveType>(arr: &PrimitiveArray<T>) {
74
76
  }
75
77
  }
76
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
+
77
106
  /// Decode an Arrow IPC batch and call `callback` once with all columns.
78
107
  ///
79
108
  /// Callback signature:
@@ -108,12 +137,14 @@ pub(crate) async fn decode_and_call(
108
137
  let js_validities = Array::new_with_length(num_cols as u32);
109
138
  let js_dicts = Array::new_with_length(num_cols as u32);
110
139
 
111
- // Storage for type-conversion buffers (Int64/Date32/Timestamp and
112
- // `float32` narrowing). These MUST outlive the callback because
113
- // `js_sys::*Array::view()` creates zero-copy views into their heap
114
- // memory. Using `Box<[T]>` (rather than `Vec<T>`) yields a stable
115
- // data pointer that won't move when the outer Vec grows, so a view
116
- // created before the push stays valid.
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();
117
148
  let mut f32_storage: Vec<Box<[f32]>> = Vec::new();
118
149
  let mut f64_storage: Vec<Box<[f64]>> = Vec::new();
119
150
 
@@ -133,6 +164,24 @@ pub(crate) async fn decode_and_call(
133
164
  js_names.set(col_idx as u32, JsString::from(field.name().as_str()).into());
134
165
 
135
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
+ },
136
185
  DataType::UInt32 => {
137
186
  let typed = col.as_primitive::<UInt32Type>();
138
187
  zero_invalid_slots(typed);
@@ -218,6 +267,46 @@ pub(crate) async fn decode_and_call(
218
267
 
219
268
  js_dicts.set(col_idx as u32, JsValue::NULL);
220
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
+ },
221
310
  DataType::Dictionary(..) => {
222
311
  let dict = col
223
312
  .as_any()
@@ -279,9 +368,9 @@ pub(crate) async fn decode_and_call(
279
368
  )?;
280
369
 
281
370
  // If the callback returned a Promise, await it before releasing the
282
- // batch — zero-copy TypedArray views into `batch`/`f32_storage`/
283
- // `f64_storage` must remain valid for the full lifetime of the
284
- // awaited work.
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.
285
374
  if ret.is_instance_of::<js_sys::Promise>() {
286
375
  let promise: js_sys::Promise = ret.unchecked_into();
287
376
  wasm_bindgen_futures::JsFuture::from(promise).await?;
@@ -289,6 +378,7 @@ pub(crate) async fn decode_and_call(
289
378
 
290
379
  // Keep storage alive until after the callback (and its awaited
291
380
  // promise, if any) returns.
381
+ drop(i32_storage);
292
382
  drop(f32_storage);
293
383
  drop(f64_storage);
294
384
 
@@ -10,6 +10,7 @@
10
10
  // ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11
11
  // ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
12
 
13
+ use std::cell::RefCell;
13
14
  use std::future::Future;
14
15
  use std::pin::Pin;
15
16
 
@@ -25,13 +26,64 @@ use wasm_bindgen_futures::{JsFuture, future_to_promise};
25
26
 
26
27
  use super::errors::*;
27
28
 
29
+ #[wasm_bindgen]
30
+ extern "C" {
31
+ /// A DevTools task handle from `console.createTask` — Chrome's Async
32
+ /// Stack Tagging API. Bound manually, as `js_sys` does not expose it.
33
+ type JsConsoleTask;
34
+
35
+ #[wasm_bindgen(method, catch)]
36
+ fn run(this: &JsConsoleTask, f: &js_sys::Function) -> Result<JsValue, JsValue>;
37
+ }
38
+
39
+ #[wasm_bindgen]
40
+ extern "C" {
41
+ #[wasm_bindgen(variadic, js_namespace = console , js_name = createTask, catch)]
42
+ pub fn createTask(name: JsValue) -> Result<JsValue, JsValue>;
43
+ }
44
+
45
+ /// Create a DevTools task, or `None` where the API is unavailable (it is
46
+ /// Chrome-only).
47
+ fn create_task(name: &str) -> Option<JsConsoleTask> {
48
+ thread_local! {
49
+ static CREATE_TASK: Option<(JsValue, js_sys::Function)> = (|| {
50
+ let global = js_sys::global();
51
+ let console = js_sys::Reflect::get(&global, &"console".into()).ok()?;
52
+ let create = js_sys::Reflect::get(&console, &"createTask".into()).ok()?;
53
+ Some((console, create.dyn_into().ok()?))
54
+ })();
55
+ }
56
+
57
+ CREATE_TASK.with(|x| {
58
+ let (console, create) = x.as_ref()?;
59
+ Some(create.call1(console, &name.into()).ok()?.unchecked_into())
60
+ })
61
+ }
62
+
63
+ thread_local! {
64
+ /// Scoped poll thunks for [`TRAMPOLINE`], a stack because tagged polls
65
+ /// nest (an `ApiFuture` awaiting another `ApiFuture`).
66
+ static POLL_STACK: RefCell<Vec<*mut (dyn FnMut() + 'static)>> = RefCell::new(Vec::new());
67
+
68
+ /// The single reusable JS closure handed to [`JsConsoleTask::run`].
69
+ static TRAMPOLINE: Closure<dyn Fn()> = Closure::new(|| {
70
+ let thunk = POLL_STACK.with(|x| x.borrow().last().copied());
71
+ if let Some(thunk) = thunk {
72
+ unsafe { (*thunk)() }
73
+ }
74
+ });
75
+ }
76
+
28
77
  /// A newtype wrapper for a `Future` trait object which supports being
29
78
  /// marshalled to a `JsPromise`.
30
79
  ///
31
80
  /// This avoids implementing an API which requires type casting to
32
81
  /// and from `JsValue` and the associated loss of type safety.
33
82
  #[must_use]
34
- pub struct ApiFuture<T>(Pin<Box<dyn Future<Output = ApiResult<T>>>>)
83
+ pub struct ApiFuture<T>(
84
+ Pin<Box<dyn Future<Output = ApiResult<T>>>>,
85
+ Option<JsConsoleTask>,
86
+ )
35
87
  where
36
88
  Result<T, JsValue>: IntoJsResult + 'static;
37
89
 
@@ -44,13 +96,17 @@ where
44
96
  /// `Promise`, either explicitly or implcitly (when exposed via
45
97
  /// `wasm_bindgen`).
46
98
  pub fn new<U: Future<Output = ApiResult<T>> + 'static>(x: U) -> Self {
47
- Self(Box::pin(x))
99
+ Self::new_named("perspective", x)
100
+ }
101
+
102
+ /// [`Self::new`] with an explicit DevTools task label, shown at the async
103
+ /// gap in tagged stack traces.
104
+ pub fn new_named<U: Future<Output = ApiResult<T>> + 'static>(name: &str, x: U) -> Self {
105
+ Self(Box::pin(x), create_task(name))
48
106
  }
49
107
 
50
108
  pub fn new_throttled<U: Future<Output = ApiResult<T>> + 'static>(x: U) -> ApiFuture<()> {
51
- ApiFuture::<()>(Box::pin(
52
- async move { x.await.ignore_view_delete().map(|_| ()) },
53
- ))
109
+ ApiFuture::<()>::new(async move { x.await.ignore_view_delete().map(|_| ()) })
54
110
  }
55
111
  }
56
112
 
@@ -65,6 +121,11 @@ where
65
121
  drop(js_sys::Promise::from(Self::new(x)))
66
122
  }
67
123
 
124
+ /// [`Self::spawn`] with an explicit DevTools task label.
125
+ pub fn spawn_named<U: Future<Output = ApiResult<T>> + 'static>(name: &str, x: U) {
126
+ drop(js_sys::Promise::from(Self::new_named(name, x)))
127
+ }
128
+
68
129
  pub fn spawn_throttled<U: Future<Output = ApiResult<T>> + 'static>(x: U) {
69
130
  drop(js_sys::Promise::from(Self::new_throttled(x)))
70
131
  }
@@ -94,7 +155,8 @@ where
94
155
  Result<T, JsValue>: IntoJsResult + 'static,
95
156
  {
96
157
  fn from(fut: ApiFuture<T>) -> Self {
97
- future_to_promise(async move { Ok(fut.0.await?).into_js_result() })
158
+ // Await `fut` NOT `fut.0` or the stack frame is lost in Chrome.
159
+ future_to_promise(async move { Ok(fut.await?).into_js_result() })
98
160
  }
99
161
  }
100
162
 
@@ -145,8 +207,33 @@ where
145
207
  self: Pin<&mut Self>,
146
208
  cx: &mut std::task::Context<'_>,
147
209
  ) -> std::task::Poll<Self::Output> {
148
- let mut fut = unsafe { self.map_unchecked_mut(|s| &mut s.0) };
149
- fut.as_mut().poll(cx)
210
+ // SAFETY: structural projection only — neither field is moved, and
211
+ // the inner future stays behind its own `Pin<Box<_>>`.
212
+ let Self(inner, task) = unsafe { self.get_unchecked_mut() };
213
+ let Some(task) = task else {
214
+ return inner.as_mut().poll(cx);
215
+ };
216
+
217
+ let mut result = None;
218
+ let mut thunk = || result = Some(inner.as_mut().poll(cx));
219
+
220
+ // SAFETY: the lifetime is erased for storage only. The entry is
221
+ // pushed and popped within this scope, and `run` invokes the
222
+ // trampoline synchronously, so the pointer never outlives `thunk`.
223
+ let short: *mut (dyn FnMut() + '_) = &mut thunk;
224
+ let long: *mut (dyn FnMut() + 'static) = unsafe { std::mem::transmute(short) };
225
+ POLL_STACK.with(|x| x.borrow_mut().push(long));
226
+ let _ = TRAMPOLINE.with(|x| task.run(x.as_ref().unchecked_ref()));
227
+ POLL_STACK.with(|x| x.borrow_mut().pop());
228
+
229
+ #[allow(clippy::drop_non_drop)]
230
+ drop(thunk);
231
+
232
+ match result {
233
+ Some(result) => result,
234
+ // `run` threw without invoking the trampoline.
235
+ None => inner.as_mut().poll(cx),
236
+ }
150
237
  }
151
238
  }
152
239
 
@@ -359,7 +359,18 @@ export class ClickhouseHandler implements perspective.VirtualServerHandler {
359
359
  viewId: string,
360
360
  config: ViewConfigUpdate,
361
361
  ) {
362
- const query = this.sqlBuilder.tableMakeView(tableId, viewId, config);
362
+ // Window order keys need column types for `range` frame emission.
363
+ const schema = Object.keys(config.windows ?? {}).length
364
+ ? await this.tableSchema(tableId)
365
+ : undefined;
366
+
367
+ const query = this.sqlBuilder.tableMakeView(
368
+ tableId,
369
+ viewId,
370
+ config,
371
+ schema,
372
+ );
373
+
363
374
  await runQuery(this.db, query, { execute: true });
364
375
  }
365
376
 
@@ -371,7 +371,18 @@ export class DuckDBHandler implements perspective.VirtualServerHandler {
371
371
  viewId: string,
372
372
  config: ViewConfigUpdate,
373
373
  ) {
374
- const query = this.sqlBuilder.tableMakeView(tableId, viewId, config);
374
+ // Window order keys need column types for `range` frame emission.
375
+ const schema = Object.keys(config.windows ?? {}).length
376
+ ? await this.tableSchema(tableId)
377
+ : undefined;
378
+
379
+ const query = this.sqlBuilder.tableMakeView(
380
+ tableId,
381
+ viewId,
382
+ config,
383
+ schema,
384
+ );
385
+
375
386
  await runQuery(this.db, query);
376
387
  }
377
388