glaredb_core 25.6.3

Core functionality for GlareDB
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
use std::task::Context;

use glaredb_error::{DbError, Result};

use crate::arrays::array::Array;
use crate::arrays::array::physical_type::{AddressableMut, MutableScalarStorage, PhysicalI64};
use crate::arrays::batch::Batch;
use crate::arrays::datatype::{DataType, DataTypeId};
use crate::arrays::field::{ColumnSchema, Field};
use crate::execution::operators::{ExecutionProperties, PollExecute, PollFinalize};
use crate::expr;
use crate::functions::Signature;
use crate::functions::documentation::{Category, Documentation};
use crate::functions::function_set::TableFunctionSet;
use crate::functions::table::execute::TableExecuteFunction;
use crate::functions::table::{RawTableFunction, TableFunctionBindState, TableFunctionInput};
use crate::statistics::value::StatisticsValue;

pub const FUNCTION_SET_GENERATE_SERIES: TableFunctionSet = TableFunctionSet {
    name: "generate_series",
    aliases: &[],
    doc: &[&Documentation {
        category: Category::Table,
        description: "Generate a series of values from 'start' to 'end' incrementing by a step of 1. 'start' and 'end' are both inclusive.",
        arguments: &["start", "end"],
        example: None,
    }],
    functions: &[
        // generate_series(start, stop)
        RawTableFunction::new_execute(
            &Signature::new(&[DataTypeId::Int64, DataTypeId::Int64], DataTypeId::Table),
            &GenerateSeriesI64,
        ),
        // generate_series(start, stop, step)
        RawTableFunction::new_execute(
            &Signature::new(
                &[DataTypeId::Int64, DataTypeId::Int64, DataTypeId::Int64],
                DataTypeId::Table,
            ),
            &GenerateSeriesI64,
        ),
    ],
};

#[derive(Debug, Default)]
pub struct GenerateSeriesI64PartitionState {
    /// Current params.
    params: Option<SeriesParams>,
    /// Row in the input we're currently working on.
    current_row: usize,
}

#[derive(Debug, Clone)]
struct SeriesParams {
    curr: i64,
    stop: i64,
    step: i64,
}

impl SeriesParams {
    fn try_new(input: &Batch, row: usize) -> Result<Self> {
        let start = input.arrays[0].get_value(row)?.try_as_i64()?;
        let stop = input.arrays[1].get_value(row)?.try_as_i64()?;
        let step = input.arrays[2].get_value(row)?.try_as_i64()?;

        if step == 0 {
            return Err(DbError::new("Step cannot be zero"));
        }

        Ok(SeriesParams {
            curr: start,
            stop,
            step,
        })
    }

    /// Generate the next set of rows using the current parameters.
    ///
    /// Returns the count of values written to `out`.
    fn generate_next(&mut self, out: &mut Array) -> Result<usize> {
        let mut out = PhysicalI64::get_addressable_mut(&mut out.data)?;

        let mut idx = 0;
        if self.curr <= self.stop && self.step > 0 {
            // Going up.
            while self.curr <= self.stop && idx < out.len() {
                out.put(idx, &self.curr);
                self.curr += self.step;
                idx += 1;
            }
        } else if self.curr >= self.stop && self.step < 0 {
            // Going down.
            while self.curr >= self.stop && idx < out.len() {
                out.put(idx, &self.curr);
                self.curr += self.step;
                idx += 1;
            }
        }

        if idx == 0 {
            // Nothing written.
            return Ok(0);
        }

        // Calculate the start value for the next iteration.
        let last = out.slice.get(idx - 1).expect("value to exist");
        self.curr = *last + self.step;

        Ok(idx)
    }
}

#[derive(Debug, Clone, Copy)]
pub struct GenerateSeriesI64;

impl TableExecuteFunction for GenerateSeriesI64 {
    type BindState = ();

    type OperatorState = ();
    type PartitionState = GenerateSeriesI64PartitionState;

    fn bind(
        &self,
        mut input: TableFunctionInput,
    ) -> Result<TableFunctionBindState<Self::BindState>> {
        if input.positional.len() == 2 {
            // Push constant step value.
            input.positional.push(expr::lit(1_i64).into());
        }

        Ok(TableFunctionBindState {
            state: (),
            input,
            data_schema: ColumnSchema::new([Field::new(
                "generate_series",
                DataType::int64(),
                false,
            )]),
            meta_schema: None,
            cardinality: StatisticsValue::Unknown,
        })
    }

    fn create_execute_operator_state(
        _bind_state: &Self::BindState,
        _props: ExecutionProperties,
    ) -> Result<Self::OperatorState> {
        Ok(())
    }

    fn create_execute_partition_states(
        _op_state: &Self::OperatorState,
        _props: ExecutionProperties,
        partitions: usize,
    ) -> Result<Vec<Self::PartitionState>> {
        let states: Vec<_> = (0..partitions)
            .map(|_| GenerateSeriesI64PartitionState {
                params: None,
                current_row: 0,
            })
            .collect();

        Ok(states)
    }

    fn poll_execute(
        _cx: &mut Context,
        _operator_state: &Self::OperatorState,
        state: &mut Self::PartitionState,
        input: &mut Batch,
        output: &mut Batch,
    ) -> Result<PollExecute> {
        loop {
            if state.params.is_none() {
                // Need to generate params from current row.
                if state.current_row >= input.num_rows() {
                    // Need a new batch.
                    state.current_row = 0;
                    return Ok(PollExecute::NeedsMore);
                }

                // Get params from the current row.
                let params = SeriesParams::try_new(input, state.current_row)?;
                state.params = Some(params);
            }

            let count = state
                .params
                .as_mut()
                .unwrap()
                .generate_next(&mut output.arrays[0])?;

            if count == 0 {
                // Next row.
                state.params = None;
                state.current_row += 1;
                continue;
            }

            output.set_num_rows(count)?;

            // Next poll should execute with the same input batch.
            return Ok(PollExecute::HasMore);
        }
    }

    fn poll_finalize_execute(
        _cx: &mut Context,
        _operator_state: &Self::OperatorState,
        _state: &mut Self::PartitionState,
    ) -> Result<PollFinalize> {
        Ok(PollFinalize::Finalized)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::generate_batch;
    use crate::testutil::arrays::assert_batches_eq;
    use crate::util::task::noop_context;

    #[test]
    fn generate_series_single_row() {
        let mut state = GenerateSeriesI64PartitionState::default();

        // generate_series(1, 5, 1)
        let mut input = generate_batch!([1_i64], [5_i64], [1_i64]);

        let mut output = Batch::new([DataType::int64()], 5).unwrap();
        let poll = GenerateSeriesI64::poll_execute(
            &mut noop_context(),
            &(),
            &mut state,
            &mut input,
            &mut output,
        )
        .unwrap();
        assert_eq!(PollExecute::HasMore, poll);

        let expected = generate_batch!([1_i64, 2, 3, 4, 5]);
        assert_batches_eq(&expected, &output);

        let poll = GenerateSeriesI64::poll_execute(
            &mut noop_context(),
            &(),
            &mut state,
            &mut input,
            &mut output,
        )
        .unwrap();
        assert_eq!(PollExecute::NeedsMore, poll);
    }

    #[test]
    fn generate_series_single_row_out_lacks_capacity() {
        // Same as single row test, just we poll with an output capacity that
        // requires multiple polls to get all output.
        let mut state = GenerateSeriesI64PartitionState::default();

        // generate_series(1, 5, 1)
        let mut input = generate_batch!([1_i64], [5_i64], [1_i64]);

        let mut output = Batch::new([DataType::int64()], 3).unwrap();
        let poll = GenerateSeriesI64::poll_execute(
            &mut noop_context(),
            &(),
            &mut state,
            &mut input,
            &mut output,
        )
        .unwrap();
        assert_eq!(PollExecute::HasMore, poll);

        let expected = generate_batch!([1_i64, 2, 3]);
        assert_batches_eq(&expected, &output);

        let poll = GenerateSeriesI64::poll_execute(
            &mut noop_context(),
            &(),
            &mut state,
            &mut input,
            &mut output,
        )
        .unwrap();
        assert_eq!(PollExecute::HasMore, poll);

        let expected = generate_batch!([4_i64, 5]);
        assert_batches_eq(&expected, &output);
    }

    #[test]
    fn generate_series_single_row_out_lacks_capacity_by_1() {
        // Test off by one...
        let mut state = GenerateSeriesI64PartitionState::default();

        // generate_series(1, 5, 1)
        let mut input = generate_batch!([1_i64], [5_i64], [1_i64]);

        let mut output = Batch::new([DataType::int64()], 4).unwrap();
        let poll = GenerateSeriesI64::poll_execute(
            &mut noop_context(),
            &(),
            &mut state,
            &mut input,
            &mut output,
        )
        .unwrap();
        assert_eq!(PollExecute::HasMore, poll);

        let expected = generate_batch!([1_i64, 2, 3, 4]);
        assert_batches_eq(&expected, &output);

        let poll = GenerateSeriesI64::poll_execute(
            &mut noop_context(),
            &(),
            &mut state,
            &mut input,
            &mut output,
        )
        .unwrap();
        assert_eq!(PollExecute::HasMore, poll);

        let expected = generate_batch!([5_i64]);
        assert_batches_eq(&expected, &output);
    }

    #[test]
    fn generate_series_multiple_rows() {
        let mut state = GenerateSeriesI64PartitionState::default();

        // generate_series(1, 5, 1)
        // generate_series(4, 8, 2)
        let mut input = generate_batch!([1_i64, 4], [5_i64, 8], [1_i64, 2]);

        let mut output = Batch::new([DataType::int64()], 5).unwrap();
        let poll = GenerateSeriesI64::poll_execute(
            &mut noop_context(),
            &(),
            &mut state,
            &mut input,
            &mut output,
        )
        .unwrap();
        assert_eq!(PollExecute::HasMore, poll);

        let expected = generate_batch!([1_i64, 2, 3, 4, 5]);
        assert_batches_eq(&expected, &output);

        let poll = GenerateSeriesI64::poll_execute(
            &mut noop_context(),
            &(),
            &mut state,
            &mut input,
            &mut output,
        )
        .unwrap();
        assert_eq!(PollExecute::HasMore, poll);

        let expected = generate_batch!([4_i64, 6, 8]);
        assert_batches_eq(&expected, &output);
    }

    #[test]
    fn generate_series_neverending_start_gt_stop() {
        let mut state = GenerateSeriesI64PartitionState::default();

        // generate_series(5, 1, 1)
        let mut input = generate_batch!([5_i64], [1_i64], [1_i64]);

        let mut output = Batch::new([DataType::int64()], 5).unwrap();
        let poll = GenerateSeriesI64::poll_execute(
            &mut noop_context(),
            &(),
            &mut state,
            &mut input,
            &mut output,
        )
        .unwrap();
        assert_eq!(PollExecute::NeedsMore, poll);
        assert_eq!(0, output.num_rows());
    }

    #[test]
    fn generate_series_neverending_start_lt_stop() {
        let mut state = GenerateSeriesI64PartitionState::default();

        // generate_series(1, 5, -1)
        let mut input = generate_batch!([1_i64], [5_i64], [-1_i64]);

        let mut output = Batch::new([DataType::int64()], 5).unwrap();
        let poll = GenerateSeriesI64::poll_execute(
            &mut noop_context(),
            &(),
            &mut state,
            &mut input,
            &mut output,
        )
        .unwrap();
        assert_eq!(PollExecute::NeedsMore, poll);
        assert_eq!(0, output.num_rows());
    }
}