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
// Copyright 2025, 2026 Query Farm LLC - https://query.farm
//! Table (producer) function model: generate output batches without input.
//!
//! The function creates a
//! per-execution [`TableProducer`] whose `produce` is called repeatedly; it
//! emits one batch per tick and calls `out.finish()` when exhausted.
use SchemaRef;
use Result;
use crate;
/// Cardinality estimate for a table function.
/// A per-execution producer. Holds the function's mutable scan state.
///
/// Returns the next batch, or `None` when the scan is exhausted. The dispatch
/// adapter applies projection / auto-filter pushdown to each batch before
/// emitting, so producers stay free of that concern. `out` is provided only
/// for `client_log` — do NOT emit through it (the adapter emits the returned
/// batch).
/// A table (producer) VGI function: generates rows with no row input.
///
/// A table function is a *factory*: at bind time it resolves an output schema
/// ([`on_bind`](Self::on_bind)), and for each execution it builds a
/// [`TableProducer`] ([`producer`](Self::producer)) that yields output batches
/// until exhausted. Implement [`name`](Self::name), [`metadata`](Self::metadata),
/// [`argument_specs`](Self::argument_specs), [`on_bind`](Self::on_bind), and
/// [`producer`](Self::producer); everything else (cardinality, statistics,
/// parallelism, secrets) has a default. Projection and pushed-down filters are
/// applied to each emitted batch by the framework, so producers don't handle
/// them. Register with [`Worker::register_table`](crate::Worker::register_table).
///
/// # Examples
///
/// ```
/// use std::sync::Arc;
///
/// use arrow_array::{ArrayRef, Int64Array, RecordBatch};
/// use arrow_schema::{DataType, Field, Schema, SchemaRef};
/// use vgi::function::{ArgSpec, BindParams, BindResponse, FunctionMetadata, ProcessParams};
/// use vgi::table_function::{TableFunction, TableProducer};
/// use vgi_rpc::{OutputCollector, Result, RpcError};
///
/// /// `count_to(n)` — emit a single `value` column 0..n.
/// struct CountTo;
///
/// struct CountProducer {
/// schema: SchemaRef,
/// n: i64,
/// done: bool,
/// }
///
/// impl TableProducer for CountProducer {
/// fn next_batch(&mut self, _out: &mut OutputCollector) -> Result<Option<RecordBatch>> {
/// if self.done {
/// return Ok(None);
/// }
/// self.done = true;
/// let col: ArrayRef = Arc::new((0..self.n).collect::<Int64Array>());
/// let batch = RecordBatch::try_new(self.schema.clone(), vec![col])
/// .map_err(|e| RpcError::runtime_error(e.to_string()))?;
/// Ok(Some(batch))
/// }
/// }
///
/// impl TableFunction for CountTo {
/// fn name(&self) -> &str {
/// "count_to"
/// }
/// fn metadata(&self) -> FunctionMetadata {
/// FunctionMetadata::default()
/// }
/// fn argument_specs(&self) -> Vec<ArgSpec> {
/// vec![ArgSpec::const_arg("n", 0, "int64", "Upper bound (exclusive)")]
/// }
/// fn on_bind(&self, _params: &BindParams) -> Result<BindResponse> {
/// let schema = Arc::new(Schema::new(vec![Field::new("value", DataType::Int64, true)]));
/// Ok(BindResponse { output_schema: schema, opaque_data: Vec::new() })
/// }
/// fn producer(&self, params: &ProcessParams) -> Result<Box<dyn TableProducer>> {
/// Ok(Box::new(CountProducer {
/// schema: params.output_schema.clone(),
/// n: params.arguments.const_i64(0).unwrap_or(0),
/// done: false,
/// }))
/// }
/// }
/// ```
/// Narrow a full schema to the projected columns (`projection_ids`).