substrait 0.63.0

Cross-Language Serialization for Relational Algebra
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
// SPDX-License-Identifier: Apache-2.0

//! Scalar function definitions with validated signatures and resolved types.
//!
//! This module provides typed wrappers around scalar functions parsed from extension
//! YAML files, validating constraints and resolving type strings to concrete types.

use std::collections::HashMap;

use crate::text::simple_extensions::{
    NullabilityHandling as RawNullabilityHandling, Options as RawOptions,
    ScalarFunction as RawScalarFunction, ScalarFunctionImplsItem as RawImpl, Type as RawType,
    VariadicBehavior as RawVariadicBehavior, VariadicBehaviorParameterConsistency,
};

use super::argument::{ArgumentsItem, ArgumentsItemError};
use super::extensions::TypeContext;
use super::type_ast::{TypeExpr, TypeParseError};
use super::types::{ConcreteType, ExtensionTypeError};
use crate::parse::Parse;
use thiserror::Error;

/// Errors that can occur when parsing scalar functions
#[derive(Debug, Error)]
pub enum ScalarFunctionError {
    /// Scalar function has no implementations
    #[error("Scalar function '{name}' must have at least one implementation")]
    NoImplementations {
        /// The function name
        name: String,
    },
    /// Invalid variadic behavior
    #[error("Variadic behavior {field} must be a non-negative integer, got {value}")]
    InvalidVariadicBehavior {
        /// The field that was invalid (min or max)
        field: String,
        /// The invalid value
        value: f64,
    },
    /// Variadic min is greater than max
    #[error("Variadic min ({min}) must be less than or equal to max ({max})")]
    VariadicMinGreaterThanMax {
        /// The minimum value
        min: u32,
        /// The maximum value
        max: u32,
    },
    /// Error parsing function argument
    #[error("Argument error: {0}")]
    ArgumentError(#[from] ArgumentsItemError),
    /// Error parsing type in function signature
    #[error("Type error: {0}")]
    TypeError(#[from] ExtensionTypeError),
    /// Error parsing type expression
    #[error("Type parse error: {0}")]
    TypeParseError(#[from] TypeParseError),
    /// Feature not yet implemented
    #[error("Not yet implemented: {0}")]
    NotYetImplemented(String),
}

/// A validated scalar function definition with one or more implementations
#[derive(Clone, Debug, PartialEq)]
pub struct ScalarFunction {
    /// Function name
    pub name: String,
    /// Human-readable description
    pub description: Option<String>,
    /// Function implementations (overloads)
    pub impls: Vec<Impl>,
}

impl ScalarFunction {
    /// Parse a scalar function from raw YAML, resolving types with the provided context
    pub(super) fn from_raw(
        raw: RawScalarFunction,
        ctx: &mut TypeContext,
    ) -> Result<Self, ScalarFunctionError> {
        if raw.impls.is_empty() {
            return Err(ScalarFunctionError::NoImplementations { name: raw.name });
        }

        let impls = raw
            .impls
            .into_iter()
            .map(|impl_| Impl::from_raw(impl_, ctx))
            .collect::<Result<Vec<_>, _>>()?;

        Ok(ScalarFunction {
            name: raw.name,
            description: raw.description,
            impls,
        })
    }
}

/// A single function implementation (overload) with signature and resolved types
#[derive(Clone, Debug, PartialEq)]
pub struct Impl {
    /// Function arguments with types and optional names/descriptions
    pub args: Vec<ArgumentsItem>,
    /// Configurable function options (e.g., overflow behavior, rounding modes)
    pub options: Options,
    /// Variadic argument behavior.
    ///
    /// `None` indicates the function is not variadic.
    pub variadic: Option<VariadicBehavior>,
    /// Whether the function output depends on session state (e.g., timezone, locale).
    ///
    /// Defaults to `false` per the Substrait spec.
    pub session_dependent: bool,
    /// Whether the function is deterministic (same inputs always produce same output).
    ///
    /// Defaults to `true` per the Substrait spec.
    pub deterministic: bool,
    /// How the function handles null inputs and produces nullable outputs.
    ///
    /// Defaults to [`NullabilityHandling::Mirror`] per the Substrait spec.
    pub nullability: NullabilityHandling,
    /// Return type resolved to a concrete type
    ///
    /// The raw YAML type string is parsed and validated. Only concrete types
    /// (without type variables) are supported; functions with type variables
    /// are skipped in this basic implementation.
    pub return_type: ConcreteType,
    /// Language-specific implementation code (e.g., SQL, C++, Python)
    ///
    /// Maps language identifiers to implementation source code snippets.
    pub implementation: HashMap<String, String>,
}

impl Impl {
    /// Parse an implementation from raw YAML, resolving types with the provided context
    pub(super) fn from_raw(
        raw: RawImpl,
        ctx: &mut TypeContext,
    ) -> Result<Self, ScalarFunctionError> {
        // Parse and validate the return type
        let return_type = match raw.return_.0 {
            RawType::String(s) => {
                // Multiline strings indicate type derivation expressions
                // See: https://github.com/substrait-io/substrait-rs/issues/449
                if s.contains('\n') {
                    return Err(ScalarFunctionError::NotYetImplemented(
                        "Type derivation expressions - issue #449".to_string(),
                    ));
                }
                let type_expr = TypeExpr::parse(&s)?;
                type_expr.visit_references(&mut |name| ctx.linked(name));
                match ConcreteType::try_from(type_expr) {
                    Ok(concrete) => concrete,
                    Err(ExtensionTypeError::InvalidAnyTypeVariable { .. })
                    | Err(ExtensionTypeError::InvalidParameter(_))
                    | Err(ExtensionTypeError::InvalidParameterKind { .. }) => {
                        // Type has type/parameter variables (any1, L1, P, etc.) - not yet supported
                        // See: https://github.com/substrait-io/substrait-rs/issues/452
                        return Err(ScalarFunctionError::NotYetImplemented(
                            "Type variables in function signatures - issue #452".to_string(),
                        ));
                    }
                    Err(ExtensionTypeError::UnknownTypeName { name }) => {
                        return Err(ScalarFunctionError::TypeError(
                            ExtensionTypeError::UnknownTypeName { name },
                        ));
                    }
                    Err(e) => return Err(ScalarFunctionError::TypeError(e)),
                }
            }
            RawType::Object(_) => {
                // Struct return types (YAML syntactic sugar) are not yet supported
                // See: https://github.com/substrait-io/substrait-rs/issues/450
                return Err(ScalarFunctionError::NotYetImplemented(
                    "Struct return types - issue #450".to_string(),
                ));
            }
        };

        let variadic = raw.variadic.map(|v| v.try_into()).transpose()?;

        let args = match raw.args {
            Some(a) => {
                a.0.into_iter()
                    .map(|raw_arg| raw_arg.parse(ctx))
                    .collect::<Result<Vec<_>, _>>()?
            }
            None => Vec::new(),
        };

        Ok(Impl {
            args,
            options: raw.options.as_ref().map(Options::from).unwrap_or_default(),
            variadic,
            session_dependent: raw.session_dependent.map(|b| b.0).unwrap_or(false),
            deterministic: raw.deterministic.map(|b| b.0).unwrap_or(true),
            nullability: raw
                .nullability
                .map(Into::into)
                .unwrap_or(NullabilityHandling::Mirror),
            return_type,
            implementation: raw
                .implementation
                .map(|i| i.0.into_iter().collect())
                .unwrap_or_default(),
        })
    }
}

/// Validated variadic behavior with min/max constraints
#[derive(Clone, Debug, PartialEq)]
pub struct VariadicBehavior {
    /// Minimum number of arguments
    pub min: u32,
    /// Maximum number of arguments (unlimited when None)
    pub max: Option<u32>,
    /// Whether all variadic parameters must have the same type.
    ///
    /// `None` when the parameter is not specified in the YAML. We cannot assume a default
    /// because the Substrait spec does not define default behavior for missing values
    /// (see <https://github.com/substrait-io/substrait/issues/928>).
    ///
    /// TODO: Once the spec defines default behavior, apply it and change this to a non-Option
    /// type (see issue #454).
    pub parameter_consistency: Option<ParameterConsistency>,
}

/// Specifies whether variadic parameters must have consistent types.
///
/// When a function's last argument is variadic with a type parameter (e.g., `fn(A, B, C...)`),
/// this controls type binding for the variadic arguments.
///
/// See: <https://github.com/substrait-io/substrait/issues/928>
#[derive(Clone, Debug, PartialEq)]
pub enum ParameterConsistency {
    /// All variadic arguments must have the same concrete type.
    ///
    /// For example, if `C` binds to `i32`, all variadic arguments must be `i32`.
    Consistent,
    /// Each variadic argument can have a different type.
    ///
    /// Each instance of the variadic parameter can bind to different types
    /// within the constraints of the type parameter.
    Inconsistent,
}

impl From<VariadicBehaviorParameterConsistency> for ParameterConsistency {
    fn from(raw: VariadicBehaviorParameterConsistency) -> Self {
        match raw {
            VariadicBehaviorParameterConsistency::Consistent => ParameterConsistency::Consistent,
            VariadicBehaviorParameterConsistency::Inconsistent => {
                ParameterConsistency::Inconsistent
            }
        }
    }
}

impl TryFrom<RawVariadicBehavior> for VariadicBehavior {
    type Error = ScalarFunctionError;

    fn try_from(raw: RawVariadicBehavior) -> Result<Self, Self::Error> {
        fn parse_bound(value: f64, field: &str) -> Result<u32, ScalarFunctionError> {
            if value < 0.0 || value.fract() != 0.0 {
                return Err(ScalarFunctionError::InvalidVariadicBehavior {
                    field: field.to_string(),
                    value,
                });
            }
            Ok(value as u32)
        }

        let min = raw
            .min
            .map(|v| parse_bound(v, "min"))
            .transpose()?
            .unwrap_or(0);
        let max = raw.max.map(|v| parse_bound(v, "max")).transpose()?;

        if let Some(max_val) = max {
            if min > max_val {
                return Err(ScalarFunctionError::VariadicMinGreaterThanMax { min, max: max_val });
            }
        }

        Ok(VariadicBehavior {
            min,
            max,
            parameter_consistency: raw.parameter_consistency.map(Into::into),
        })
    }
}

/// How a function handles null inputs and produces nullable outputs
#[derive(Clone, Debug, PartialEq)]
pub enum NullabilityHandling {
    /// Nullability of output mirrors the nullability of input(s)
    Mirror,
    /// Function explicitly declares the nullability of its output
    DeclaredOutput,
    /// Function handles nulls in a custom way per implementation
    Discrete,
}

impl From<RawNullabilityHandling> for NullabilityHandling {
    fn from(raw: RawNullabilityHandling) -> Self {
        match raw {
            RawNullabilityHandling::Mirror => NullabilityHandling::Mirror,
            RawNullabilityHandling::DeclaredOutput => NullabilityHandling::DeclaredOutput,
            RawNullabilityHandling::Discrete => NullabilityHandling::Discrete,
        }
    }
}

/// Validated function options
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Options(pub HashMap<String, Vec<String>>);

impl From<&RawOptions> for Options {
    fn from(raw: &RawOptions) -> Self {
        Options(
            raw.0
                .iter()
                .map(|(k, v)| (k.clone(), v.values.clone()))
                .collect(),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_variadic_invalid_values() {
        let invalid_cases = vec![
            (Some(-1.0), None, "negative min"),
            (None, Some(-2.5), "negative max"),
            (Some(7.2), None, "non-integer min"),
            (None, Some(3.5), "non-integer max"),
            (Some(5.0), Some(3.0), "min greater than max"),
        ];

        for (min, max, description) in invalid_cases {
            let raw = RawVariadicBehavior {
                min,
                max,
                parameter_consistency: None,
            };
            assert!(
                VariadicBehavior::try_from(raw).is_err(),
                "expected error for {}",
                description
            );
        }
    }

    #[test]
    fn test_variadic_valid() {
        let raw = RawVariadicBehavior {
            min: Some(1.0),
            max: Some(5.0),
            parameter_consistency: None,
        };
        let result = VariadicBehavior::try_from(raw).unwrap();
        assert_eq!(result.min, 1);
        assert_eq!(result.max, Some(5));
    }

    #[test]
    fn test_variadic_none_values() {
        let raw = RawVariadicBehavior {
            min: None,
            max: None,
            parameter_consistency: None,
        };
        let result = VariadicBehavior::try_from(raw).unwrap();
        assert_eq!(result.min, 0);
        assert_eq!(result.max, None);
    }

    #[test]
    fn test_no_implementations_error() {
        use crate::text::simple_extensions::ScalarFunction as RawScalarFunction;

        let raw = RawScalarFunction {
            name: "empty_function".to_string(),
            description: None,
            metadata: Default::default(),
            impls: vec![],
        };

        let mut ctx = super::super::extensions::TypeContext::default();
        let result = ScalarFunction::from_raw(raw, &mut ctx);

        assert!(matches!(
            result,
            Err(ScalarFunctionError::NoImplementations { name })
            if name == "empty_function"
        ));
    }

    #[test]
    fn test_scalar_function_with_single_impl() {
        use crate::text::simple_extensions::{
            ReturnValue, ScalarFunction as RawScalarFunction, ScalarFunctionImplsItem, Type,
        };

        let raw = RawScalarFunction {
            name: "add".to_string(),
            description: Some("Addition function".to_string()),
            metadata: Default::default(),
            impls: vec![ScalarFunctionImplsItem {
                args: None,
                options: None,
                variadic: None,
                session_dependent: None,
                deterministic: None,
                nullability: None,
                return_: ReturnValue(Type::String("i32".to_string())),
                implementation: None,
            }],
        };

        let mut ctx = super::super::extensions::TypeContext::default();
        let result = ScalarFunction::from_raw(raw, &mut ctx).unwrap();

        assert_eq!(result.name, "add");
        assert_eq!(result.description, Some("Addition function".to_string()));
        assert_eq!(result.impls.len(), 1);

        // Verify return type is properly parsed to ConcreteType
        use super::super::types::{BasicBuiltinType, ConcreteTypeKind};
        let return_type = &result.impls[0].return_type;
        assert!(!return_type.nullable, "i32 should not be nullable");
        assert!(matches!(
            &return_type.kind,
            ConcreteTypeKind::Builtin(BasicBuiltinType::I32)
        ));
    }

    #[test]
    fn test_options_conversion() {
        use crate::text::simple_extensions::{Options as RawOptions, OptionsValue};
        use indexmap::IndexMap;

        let mut raw_map = IndexMap::new();
        raw_map.insert(
            "overflow".to_string(),
            OptionsValue {
                values: vec!["SILENT".to_string(), "ERROR".to_string()],
                description: None,
            },
        );

        let raw = RawOptions(raw_map);
        let options = Options::from(&raw);

        assert_eq!(options.0.len(), 1);
        assert_eq!(
            options.0.get("overflow").unwrap(),
            &vec!["SILENT".to_string(), "ERROR".to_string()]
        );
    }
}