nodedb-query 0.0.0-beta.1

Shared query evaluation engine for NodeDB — expressions, filters, functions, aggregations, window functions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! `ts_delta`, `ts_interpolate`, `ts_lag`, `ts_lead`, `ts_rank` window UDFs.

use std::any::Any;
use std::sync::Arc;

use datafusion::arrow::array::{
    Array, ArrayRef, Float64Array, Int64Array, StringArray, UInt64Array,
};
use datafusion::arrow::datatypes::{DataType, Field, FieldRef};
use datafusion::common::Result as DfResult;
use datafusion::error::DataFusionError;
use datafusion::logical_expr::function::WindowUDFFieldArgs;
use datafusion::logical_expr::{
    PartitionEvaluator, Signature, TypeSignature, Volatility, WindowUDFImpl,
};
use datafusion_functions_window_common::partition::PartitionEvaluatorArgs;

use crate::ts_functions;

use super::helpers::{
    extract_f64_values, extract_timestamps_ns, f64_to_array, option_f64_to_array,
};

// ── ts_delta ─────────────────────────────────────────────────────────────

/// `ts_delta(value) OVER (ORDER BY time)` — consecutive-sample difference.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TsDeltaUdwf {
    signature: Signature,
}

impl TsDeltaUdwf {
    pub fn new() -> Self {
        Self {
            signature: Signature::one_of(vec![TypeSignature::Any(1)], Volatility::Immutable),
        }
    }
}

impl Default for TsDeltaUdwf {
    fn default() -> Self {
        Self::new()
    }
}

impl WindowUDFImpl for TsDeltaUdwf {
    fn as_any(&self) -> &dyn Any {
        self
    }
    fn name(&self) -> &str {
        "ts_delta"
    }
    fn signature(&self) -> &Signature {
        &self.signature
    }

    fn partition_evaluator(
        &self,
        _args: PartitionEvaluatorArgs,
    ) -> DfResult<Box<dyn PartitionEvaluator>> {
        Ok(Box::new(DeltaEvaluator))
    }

    fn field(&self, field_args: WindowUDFFieldArgs) -> DfResult<FieldRef> {
        Ok(Arc::new(Field::new(
            field_args.name(),
            DataType::Float64,
            true,
        )))
    }
}

#[derive(Debug)]
struct DeltaEvaluator;

impl PartitionEvaluator for DeltaEvaluator {
    fn evaluate_all(&mut self, values: &[ArrayRef], _num_rows: usize) -> DfResult<ArrayRef> {
        let vals = extract_f64_values(&values[0])?;
        Ok(option_f64_to_array(ts_functions::ts_delta(&vals)))
    }
}

// ── ts_interpolate ───────────────────────────────────────────────────────

/// `ts_interpolate(value, time, method) OVER (ORDER BY time)` — gap fill.
///
/// `method` is a string: `'linear'`, `'prev'`/`'previous'`/`'locf'`,
/// `'next'`/`'nocb'`, or `'zero'`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TsInterpolateUdwf {
    signature: Signature,
}

impl TsInterpolateUdwf {
    pub fn new() -> Self {
        Self {
            signature: Signature::one_of(vec![TypeSignature::Any(3)], Volatility::Immutable),
        }
    }
}

impl Default for TsInterpolateUdwf {
    fn default() -> Self {
        Self::new()
    }
}

impl WindowUDFImpl for TsInterpolateUdwf {
    fn as_any(&self) -> &dyn Any {
        self
    }
    fn name(&self) -> &str {
        "ts_interpolate"
    }
    fn signature(&self) -> &Signature {
        &self.signature
    }

    fn partition_evaluator(
        &self,
        _args: PartitionEvaluatorArgs,
    ) -> DfResult<Box<dyn PartitionEvaluator>> {
        Ok(Box::new(InterpolateEvaluator))
    }

    fn field(&self, field_args: WindowUDFFieldArgs) -> DfResult<FieldRef> {
        Ok(Arc::new(Field::new(
            field_args.name(),
            DataType::Float64,
            false,
        )))
    }
}

#[derive(Debug)]
struct InterpolateEvaluator;

impl PartitionEvaluator for InterpolateEvaluator {
    fn evaluate_all(&mut self, values: &[ArrayRef], _num_rows: usize) -> DfResult<ArrayRef> {
        let arr = &values[0];
        let f64_arr = arr.as_any().downcast_ref::<Float64Array>().ok_or_else(|| {
            DataFusionError::Internal("ts_interpolate: expected Float64 value column".into())
        })?;
        // Build Option<f64> vec respecting nulls.
        let opt_vals: Vec<Option<f64>> = (0..f64_arr.len())
            .map(|i| {
                if f64_arr.is_null(i) {
                    None
                } else {
                    Some(f64_arr.value(i))
                }
            })
            .collect();

        let ts = extract_timestamps_ns(&values[1])?;

        let method_str = extract_string_scalar(&values[2])?;
        let method = ts_functions::InterpolateMethod::parse(&method_str).ok_or_else(|| {
            DataFusionError::Plan(format!(
                "ts_interpolate: unknown method '{method_str}'. \
                 Use 'linear', 'prev', 'next', or 'zero'"
            ))
        })?;

        let result = ts_functions::ts_interpolate(&opt_vals, &ts, method);
        Ok(f64_to_array(result))
    }
}

// ── ts_lag ───────────────────────────────────────────────────────────────

/// `ts_lag(value, offset) OVER (ORDER BY time)` — previous value.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TsLagUdwf {
    signature: Signature,
}

impl TsLagUdwf {
    pub fn new() -> Self {
        Self {
            signature: Signature::one_of(
                vec![TypeSignature::Any(2), TypeSignature::Any(1)],
                Volatility::Immutable,
            ),
        }
    }
}

impl Default for TsLagUdwf {
    fn default() -> Self {
        Self::new()
    }
}

impl WindowUDFImpl for TsLagUdwf {
    fn as_any(&self) -> &dyn Any {
        self
    }
    fn name(&self) -> &str {
        "ts_lag"
    }
    fn signature(&self) -> &Signature {
        &self.signature
    }

    fn partition_evaluator(
        &self,
        _args: PartitionEvaluatorArgs,
    ) -> DfResult<Box<dyn PartitionEvaluator>> {
        Ok(Box::new(LagLeadEvaluator { is_lead: false }))
    }

    fn field(&self, field_args: WindowUDFFieldArgs) -> DfResult<FieldRef> {
        Ok(Arc::new(Field::new(
            field_args.name(),
            DataType::Float64,
            true,
        )))
    }
}

// ── ts_lead ──────────────────────────────────────────────────────────────

/// `ts_lead(value, offset) OVER (ORDER BY time)` — next value.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TsLeadUdwf {
    signature: Signature,
}

impl TsLeadUdwf {
    pub fn new() -> Self {
        Self {
            signature: Signature::one_of(
                vec![TypeSignature::Any(2), TypeSignature::Any(1)],
                Volatility::Immutable,
            ),
        }
    }
}

impl Default for TsLeadUdwf {
    fn default() -> Self {
        Self::new()
    }
}

impl WindowUDFImpl for TsLeadUdwf {
    fn as_any(&self) -> &dyn Any {
        self
    }
    fn name(&self) -> &str {
        "ts_lead"
    }
    fn signature(&self) -> &Signature {
        &self.signature
    }

    fn partition_evaluator(
        &self,
        _args: PartitionEvaluatorArgs,
    ) -> DfResult<Box<dyn PartitionEvaluator>> {
        Ok(Box::new(LagLeadEvaluator { is_lead: true }))
    }

    fn field(&self, field_args: WindowUDFFieldArgs) -> DfResult<FieldRef> {
        Ok(Arc::new(Field::new(
            field_args.name(),
            DataType::Float64,
            true,
        )))
    }
}

#[derive(Debug)]
struct LagLeadEvaluator {
    is_lead: bool,
}

impl PartitionEvaluator for LagLeadEvaluator {
    fn evaluate_all(&mut self, values: &[ArrayRef], num_rows: usize) -> DfResult<ArrayRef> {
        let vals = extract_f64_values(&values[0])?;
        let offset = if values.len() > 1 {
            extract_usize_scalar(&values[1])?
        } else {
            1
        };

        let mut result: Vec<Option<f64>> = Vec::with_capacity(num_rows);
        for i in 0..num_rows {
            let src = if self.is_lead {
                i.checked_add(offset)
            } else {
                i.checked_sub(offset)
            };
            match src {
                Some(idx) if idx < vals.len() => result.push(Some(vals[idx])),
                _ => result.push(None),
            }
        }
        Ok(option_f64_to_array(result))
    }
}

// ── ts_rank ──────────────────────────────────────────────────────────────

/// `ts_rank(value) OVER (ORDER BY time)` — ordinal rank within partition.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TsRankUdwf {
    signature: Signature,
}

impl TsRankUdwf {
    pub fn new() -> Self {
        Self {
            signature: Signature::one_of(vec![TypeSignature::Any(1)], Volatility::Immutable),
        }
    }
}

impl Default for TsRankUdwf {
    fn default() -> Self {
        Self::new()
    }
}

impl WindowUDFImpl for TsRankUdwf {
    fn as_any(&self) -> &dyn Any {
        self
    }
    fn name(&self) -> &str {
        "ts_rank"
    }
    fn signature(&self) -> &Signature {
        &self.signature
    }

    fn partition_evaluator(
        &self,
        _args: PartitionEvaluatorArgs,
    ) -> DfResult<Box<dyn PartitionEvaluator>> {
        Ok(Box::new(RankEvaluator))
    }

    fn field(&self, field_args: WindowUDFFieldArgs) -> DfResult<FieldRef> {
        Ok(Arc::new(Field::new(
            field_args.name(),
            DataType::Float64,
            false,
        )))
    }
}

#[derive(Debug)]
struct RankEvaluator;

impl PartitionEvaluator for RankEvaluator {
    fn evaluate_all(&mut self, values: &[ArrayRef], num_rows: usize) -> DfResult<ArrayRef> {
        let vals = extract_f64_values(&values[0])?;
        // Sort indices by value, assign ranks (1-based, ties share rank).
        let mut indices: Vec<usize> = (0..vals.len()).collect();
        indices.sort_by(|&a, &b| {
            vals[a]
                .partial_cmp(&vals[b])
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        let mut ranks = vec![0.0_f64; num_rows];
        let mut rank = 1_usize;
        for (pos, &idx) in indices.iter().enumerate() {
            if pos > 0 && (vals[idx] - vals[indices[pos - 1]]).abs() > f64::EPSILON {
                rank = pos + 1;
            }
            ranks[idx] = rank as f64;
        }
        Ok(f64_to_array(ranks))
    }
}

// ── Helpers ──────────────────────────────────────────────────────────────

fn extract_usize_scalar(arr: &ArrayRef) -> DfResult<usize> {
    if let Some(a) = arr.as_any().downcast_ref::<Int64Array>()
        && !a.is_empty()
    {
        return Ok(a.value(0) as usize);
    }
    if let Some(a) = arr.as_any().downcast_ref::<UInt64Array>()
        && !a.is_empty()
    {
        return Ok(a.value(0) as usize);
    }
    Err(DataFusionError::Internal("expected integer offset".into()))
}

fn extract_string_scalar(arr: &ArrayRef) -> DfResult<String> {
    if let Some(a) = arr.as_any().downcast_ref::<StringArray>()
        && !a.is_empty()
        && !a.is_null(0)
    {
        return Ok(a.value(0).to_string());
    }
    Err(DataFusionError::Internal(
        "ts_interpolate: expected Utf8 method string".into(),
    ))
}