varpulis-core 0.11.0

Core types and AST for VPL
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! Static registries of builtin and aggregate functions.

/// Builtin scalar functions available in VPL expressions.
pub static BUILTIN_FUNCTIONS: &[&str] = &[
    // Math
    "abs",
    "sqrt",
    "floor",
    "ceil",
    "round",
    "log",
    "min",
    "max",
    "clamp",
    // String
    "len",
    "trim",
    "to_upper",
    "to_lower",
    "contains",
    "starts_with",
    "ends_with",
    "replace",
    "split",
    "join",
    "concat",
    // Collection
    "first",
    "last",
    "push",
    "pop",
    "reverse",
    "sort",
    "unique",
    "flatten",
    "zip",
    "range",
    "keys",
    "values",
    // Type conversion
    "to_string",
    "to_int",
    "to_float",
    "type_of",
    // Time
    "now",
    "timestamp",
    "format",
    "parse",
    // Utility
    "print",
    "coalesce",
    "if_null",
];

/// Aggregate functions used in `.aggregate()` operations.
pub static AGGREGATE_FUNCTIONS: &[&str] = &[
    "count",
    "sum",
    "avg",
    "min",
    "max",
    "stddev",
    "first",
    "last",
    "count_distinct",
    "ema",
    "median",
    "percentile",
    "p50",
    "p95",
    "p99",
];

/// Aggregate functions that require at least one field argument.
pub static AGGREGATE_REQUIRES_FIELD: &[&str] = &[
    "sum", "avg", "min", "max", "stddev", "median", "p50", "p95", "p99",
];

/// Aggregate functions that require exactly two arguments (field + period/quantile).
pub static AGGREGATE_REQUIRES_TWO_ARGS: &[&str] = &["ema", "percentile"];

/// Valid parameter names for `.log()`.
pub static LOG_PARAMS: &[&str] = &["level", "message", "data"];

/// Valid parameter names for `.alert()`.
pub static ALERT_PARAMS: &[&str] = &["webhook", "message"];

/// Valid parameter names for `.watermark()`.
pub static WATERMARK_PARAMS: &[&str] = &["out_of_order"];

/// Valid parameter names for `.forecast()`.
pub static FORECAST_PARAMS: &[&str] = &[
    "confidence",
    "horizon",
    "warmup",
    "max_depth",
    "hawkes",
    "conformal",
    "mode",
];

/// Valid parameter names for `.enrich()`.
pub static ENRICH_PARAMS: &[&str] = &["key", "fields", "cache_ttl", "timeout", "fallback"];

/// Built-in variables available after `.forecast()`.
pub static FORECAST_BUILTIN_VARS: &[&str] = &[
    "forecast_probability",
    "forecast_confidence",
    "forecast_time",
    "forecast_state",
    "forecast_context_depth",
    "forecast_lower",
    "forecast_upper",
];

/// Built-in variables available after `.enrich()`.
pub static ENRICH_BUILTIN_VARS: &[&str] = &["enrich_status", "enrich_latency_ms"];

/// Connector types that support request-response lookups for `.enrich()`.
pub static ENRICH_COMPATIBLE_TYPES: &[&str] = &["http", "database", "redis"];

/// Valid parameter names for `.concurrent()`.
pub static CONCURRENT_PARAMS: &[&str] = &["workers", "partition_key"];

/// Known connector types for validation.
pub static KNOWN_CONNECTOR_TYPES: &[&str] = &[
    "mqtt",
    "kafka",
    "nats",
    "http",
    "console",
    "websocket",
    "file",
    "database",
    "redis",
    "redis_stream",
    "pulsar",
    "postgres_cdc",
];

/// Check if a connector type is known.
pub fn is_known_connector_type(name: &str) -> bool {
    KNOWN_CONNECTOR_TYPES.contains(&name)
}

/// Check if a function name is a known builtin (scalar or aggregate).
pub fn is_known_function(name: &str) -> bool {
    BUILTIN_FUNCTIONS.contains(&name) || AGGREGATE_FUNCTIONS.contains(&name)
}

/// Check if a function name is a known aggregate function.
pub fn is_aggregate_function(name: &str) -> bool {
    AGGREGATE_FUNCTIONS.contains(&name)
}

// =========================================================================
// Connector parameter schemas
// =========================================================================

/// Type of a connector parameter value.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParamType {
    /// String parameter.
    Str,
    /// Integer parameter.
    Int,
    /// Boolean parameter.
    Bool,
    /// Array of strings parameter.
    StrArray,
}

/// Whether a parameter is valid for source (.from()), sink (.to()), or both.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParamContext {
    /// Valid only for `.from()` (source) operations.
    Source,
    /// Valid only for `.to()` (sink) operations.
    Sink,
    /// Valid for both source and sink operations.
    Both,
}

/// Schema definition for a single connector parameter.
#[derive(Debug, Clone)]
pub struct ConnectorParamDef {
    /// Parameter name.
    pub name: &'static str,
    /// Expected value type.
    pub param_type: ParamType,
    /// Whether this parameter is required.
    pub required: bool,
    /// Human-readable description for diagnostics.
    pub description: &'static str,
    /// Whether valid for source, sink, or both contexts.
    pub context: ParamContext,
}

impl ConnectorParamDef {
    /// Check if this param is valid in a given context.
    pub fn valid_in(&self, ctx: ParamContext) -> bool {
        self.context == ParamContext::Both || self.context == ctx
    }
}

static MQTT_PARAMS: &[ConnectorParamDef] = &[
    ConnectorParamDef {
        name: "host",
        param_type: ParamType::Str,
        required: false,
        description: "MQTT broker hostname",
        context: ParamContext::Both,
    },
    ConnectorParamDef {
        name: "port",
        param_type: ParamType::Int,
        required: false,
        description: "MQTT broker port",
        context: ParamContext::Both,
    },
    ConnectorParamDef {
        name: "url",
        param_type: ParamType::Str,
        required: false,
        description: "MQTT broker URL",
        context: ParamContext::Both,
    },
    ConnectorParamDef {
        name: "topic",
        param_type: ParamType::Str,
        required: false,
        description: "MQTT topic to subscribe/publish",
        context: ParamContext::Both,
    },
    ConnectorParamDef {
        name: "client_id",
        param_type: ParamType::Str,
        required: false,
        description: "Dedicated MQTT client ID (creates separate connection)",
        context: ParamContext::Both,
    },
    ConnectorParamDef {
        name: "qos",
        param_type: ParamType::Int,
        required: false,
        description: "QoS level (0, 1, 2)",
        context: ParamContext::Both,
    },
];

static KAFKA_PARAMS: &[ConnectorParamDef] = &[
    ConnectorParamDef {
        name: "brokers",
        param_type: ParamType::StrArray,
        required: false,
        description: "Kafka broker addresses (array of strings)",
        context: ParamContext::Both,
    },
    ConnectorParamDef {
        name: "topic",
        param_type: ParamType::Str,
        required: false,
        description: "Kafka topic",
        context: ParamContext::Both,
    },
    ConnectorParamDef {
        name: "group_id",
        param_type: ParamType::Str,
        required: false,
        description: "Consumer group ID",
        context: ParamContext::Both,
    },
    ConnectorParamDef {
        name: "partition",
        param_type: ParamType::Int,
        required: false,
        description: "Partition number",
        context: ParamContext::Both,
    },
    ConnectorParamDef {
        name: "auto_offset_reset",
        param_type: ParamType::Str,
        required: false,
        description: "Where to start consuming if no offset is committed: 'earliest' or 'latest' (default: latest). Source-only; sinks ignore.",
        context: ParamContext::Both,
    },
];

static HTTP_PARAMS: &[ConnectorParamDef] = &[ConnectorParamDef {
    name: "base_url",
    param_type: ParamType::Str,
    required: false,
    description: "HTTP base URL for the endpoint",
    context: ParamContext::Both,
}];

static CONSOLE_PARAMS: &[ConnectorParamDef] = &[ConnectorParamDef {
    name: "topic",
    param_type: ParamType::Str,
    required: false,
    description: "Label prefix for console output",
    context: ParamContext::Sink,
}];

static NATS_PARAMS: &[ConnectorParamDef] = &[
    ConnectorParamDef {
        name: "url",
        param_type: ParamType::Str,
        required: false,
        description: "NATS server URL",
        context: ParamContext::Both,
    },
    ConnectorParamDef {
        name: "topic",
        param_type: ParamType::Str,
        required: false,
        description: "NATS subject to subscribe/publish",
        context: ParamContext::Both,
    },
    ConnectorParamDef {
        name: "queue_group",
        param_type: ParamType::Str,
        required: false,
        description: "Queue group for load-balanced consumption",
        context: ParamContext::Both,
    },
];

static PULSAR_PARAMS: &[ConnectorParamDef] = &[
    ConnectorParamDef {
        name: "url",
        param_type: ParamType::Str,
        required: false,
        description: "Pulsar service URL",
        context: ParamContext::Both,
    },
    ConnectorParamDef {
        name: "topic",
        param_type: ParamType::Str,
        required: false,
        description: "Pulsar topic name",
        context: ParamContext::Both,
    },
    ConnectorParamDef {
        name: "subscription",
        param_type: ParamType::Str,
        required: false,
        description: "Consumer subscription name",
        context: ParamContext::Source,
    },
    ConnectorParamDef {
        name: "consumer_name",
        param_type: ParamType::Str,
        required: false,
        description: "Consumer name identifier",
        context: ParamContext::Source,
    },
    ConnectorParamDef {
        name: "batch_size",
        param_type: ParamType::Int,
        required: false,
        description: "Consumer batch size",
        context: ParamContext::Source,
    },
    ConnectorParamDef {
        name: "token",
        param_type: ParamType::Str,
        required: false,
        description: "Authentication token",
        context: ParamContext::Both,
    },
];

static REDIS_STREAM_PARAMS: &[ConnectorParamDef] = &[
    ConnectorParamDef {
        name: "url",
        param_type: ParamType::Str,
        required: false,
        description: "Redis server URL",
        context: ParamContext::Both,
    },
    ConnectorParamDef {
        name: "stream_key",
        param_type: ParamType::Str,
        required: false,
        description: "Redis stream key name",
        context: ParamContext::Both,
    },
    ConnectorParamDef {
        name: "group",
        param_type: ParamType::Str,
        required: false,
        description: "Consumer group name",
        context: ParamContext::Source,
    },
    ConnectorParamDef {
        name: "consumer",
        param_type: ParamType::Str,
        required: false,
        description: "Consumer name within the group",
        context: ParamContext::Source,
    },
    ConnectorParamDef {
        name: "batch_size",
        param_type: ParamType::Int,
        required: false,
        description: "Number of messages to read per batch",
        context: ParamContext::Source,
    },
    ConnectorParamDef {
        name: "max_len",
        param_type: ParamType::Int,
        required: false,
        description: "Approximate max stream length for XADD MAXLEN ~",
        context: ParamContext::Sink,
    },
];

/// Look up the parameter schema for a connector type.
///
/// Returns `None` for unknown connector types (forward-compatible).
pub fn connector_params_for_type(connector_type: &str) -> Option<&'static [ConnectorParamDef]> {
    match connector_type {
        "mqtt" => Some(MQTT_PARAMS),
        "kafka" => Some(KAFKA_PARAMS),
        "http" => Some(HTTP_PARAMS),
        "nats" => Some(NATS_PARAMS),
        "console" => Some(CONSOLE_PARAMS),
        "pulsar" => Some(PULSAR_PARAMS),
        "redis_stream" => Some(REDIS_STREAM_PARAMS),
        "postgres_cdc" => Some(POSTGRES_CDC_PARAMS),
        _ => None,
    }
}

static POSTGRES_CDC_PARAMS: &[ConnectorParamDef] = &[
    ConnectorParamDef {
        name: "host",
        param_type: ParamType::Str,
        required: true,
        description: "PostgreSQL hostname",
        context: ParamContext::Source,
    },
    ConnectorParamDef {
        name: "port",
        param_type: ParamType::Int,
        required: false,
        description: "PostgreSQL port (default 5432)",
        context: ParamContext::Source,
    },
    ConnectorParamDef {
        name: "dbname",
        param_type: ParamType::Str,
        required: true,
        description: "Database name",
        context: ParamContext::Source,
    },
    ConnectorParamDef {
        name: "user",
        param_type: ParamType::Str,
        required: false,
        description: "Database user (default \"postgres\")",
        context: ParamContext::Source,
    },
    ConnectorParamDef {
        name: "password",
        param_type: ParamType::Str,
        required: false,
        description: "Database password",
        context: ParamContext::Source,
    },
    ConnectorParamDef {
        name: "slot_name",
        param_type: ParamType::Str,
        required: false,
        description: "Logical replication slot name",
        context: ParamContext::Source,
    },
    ConnectorParamDef {
        name: "publication",
        param_type: ParamType::Str,
        required: false,
        description: "PostgreSQL publication name",
        context: ParamContext::Source,
    },
];