Skip to main content

vortex_array/scalar_fn/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Scalar function vtable machinery.
5//!
6//! This module contains the [`ScalarFnVTable`] trait and all built-in scalar function
7//! implementations. Expressions ([`crate::expr::Expression`]) reference scalar functions
8//! at each node.
9
10use vortex_session::registry::Id;
11
12use crate::scalar_fn::fns::byte_length::ByteLength;
13use crate::scalar_fn::fns::ext_storage::ExtStorage;
14use crate::scalar_fn::fns::get_item::GetItem;
15use crate::scalar_fn::fns::literal::Literal;
16
17mod vtable;
18pub use vtable::*;
19
20mod plugin;
21pub use plugin::*;
22
23mod foreign;
24pub use foreign::*;
25
26mod typed;
27pub use typed::*;
28
29mod erased;
30pub use erased::*;
31
32mod options;
33pub use options::*;
34
35mod signature;
36pub use signature::*;
37
38pub mod fns;
39pub mod internal;
40pub mod session;
41
42/// A unique identifier for a scalar function.
43pub type ScalarFnId = Id;
44
45/// Private module to seal [`typed::DynScalarFn`].
46mod sealed {
47    use crate::scalar_fn::ScalarFnVTable;
48    use crate::scalar_fn::typed::TypedScalarFnInstance;
49
50    /// Marker trait to prevent external implementations of [`super::typed::DynScalarFn`].
51    pub(crate) trait Sealed {}
52
53    /// This can be the **only** implementor for [`super::typed::DynScalarFn`].
54    impl<V: ScalarFnVTable> Sealed for TypedScalarFnInstance<V> {}
55}
56
57/// A scalar function has a negative cost if applying it to an array and
58/// canonicalizing is cheaper than canonicalizing an array and applying it.
59///
60/// Example of negative cost expressions are byte_length(), ext_storage(), and get_item() since
61/// they don't depend on input size.
62///
63/// Example of non-negative cost expression is like() as it's linear over
64/// individual input.
65pub fn is_negative_cost(id: ScalarFnId) -> bool {
66    id == ScalarFnVTable::id(&ByteLength)
67        || id == ScalarFnVTable::id(&ExtStorage)
68        || id == ScalarFnVTable::id(&GetItem)
69        || id == ScalarFnVTable::id(&Literal)
70}