linkrs_query/executor/expression/functions/
signature.rs1use linkrs_core::Value;
6use std::fmt;
7
8#[derive(Debug, Clone, PartialEq, Eq, Hash)]
10pub enum ValueType {
11 Null,
12 Bool,
13 SmallInt,
14 Int,
15 BigInt,
16 Float,
17 Double,
18 Decimal128,
19 String,
20 FixedString,
21 Blob,
22 Date,
23 Time,
24 DateTime,
25 Vertex,
26 Edge,
27 Path,
28 List,
29 Map,
30 Set,
31 Geography,
32 DataSet,
33 Vector,
34 Json,
35 JsonB,
36 Uuid,
37 Interval,
38 Empty,
39 Any,
40}
41
42impl ValueType {
43 pub fn from_value(value: &Value) -> Self {
44 match value {
45 Value::Null(_) => ValueType::Null,
46 Value::Bool(_) => ValueType::Bool,
47 Value::SmallInt(_) => ValueType::SmallInt,
48 Value::Int(_) => ValueType::Int,
49 Value::BigInt(_) => ValueType::BigInt,
50 Value::Float(_) => ValueType::Float,
51 Value::Double(_) => ValueType::Double,
52 Value::Decimal128(_) => ValueType::Decimal128,
53 Value::String(_) => ValueType::String,
54 Value::FixedString(_) => ValueType::FixedString,
55 Value::Blob(_) => ValueType::Blob,
56 Value::Date(_) => ValueType::Date,
57 Value::Time(_) => ValueType::Time,
58 Value::DateTime(_) => ValueType::DateTime,
59 Value::Vertex(_) => ValueType::Vertex,
60 Value::Edge(_) | Value::EdgeHeader(_) => ValueType::Edge,
61 Value::Path(_) => ValueType::Path,
62 Value::List(_) => ValueType::List,
63 Value::Map(_) => ValueType::Map,
64 Value::Set(_) => ValueType::Set,
65 Value::Geography(_) => ValueType::Geography,
66 Value::DataSet(_) => ValueType::DataSet,
67 Value::Vector(_) => ValueType::Vector,
68 Value::Json(_) => ValueType::Json,
69 Value::JsonB(_) => ValueType::JsonB,
70 Value::Uuid(_) => ValueType::Uuid,
71 Value::Interval(_) => ValueType::Interval,
72 Value::Empty => ValueType::Empty,
73 Value::VertexId(_) => ValueType::Vertex,
74 Value::EdgeId(_) => ValueType::Edge,
75 Value::Struct(_) => ValueType::List,
76 Value::Array(_) => ValueType::List,
77 }
78 }
79
80 pub fn is_numeric(&self) -> bool {
81 matches!(
82 self,
83 ValueType::SmallInt
84 | ValueType::Int
85 | ValueType::BigInt
86 | ValueType::Float
87 | ValueType::Double
88 | ValueType::Decimal128
89 )
90 }
91
92 pub fn is_string(&self) -> bool {
93 matches!(self, ValueType::String)
94 }
95
96 pub fn is_collection(&self) -> bool {
97 matches!(self, ValueType::List | ValueType::Map | ValueType::Set)
98 }
99
100 pub fn compatible_with(&self, other: &ValueType) -> bool {
101 self == &ValueType::Empty || other == &ValueType::Empty || self == other
102 }
103}
104
105impl fmt::Display for ValueType {
106 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107 match self {
108 ValueType::Null => write!(f, "NULL"),
109 ValueType::Bool => write!(f, "BOOL"),
110 ValueType::SmallInt => write!(f, "SMALLINT"),
111 ValueType::Int => write!(f, "INT"),
112 ValueType::BigInt => write!(f, "BIGINT"),
113 ValueType::Float => write!(f, "FLOAT"),
114 ValueType::Double => write!(f, "DOUBLE"),
115 ValueType::Decimal128 => write!(f, "DECIMAL128"),
116 ValueType::String => write!(f, "STRING"),
117 ValueType::FixedString => write!(f, "FIXED_STRING"),
118 ValueType::Blob => write!(f, "BLOB"),
119 ValueType::Date => write!(f, "DATE"),
120 ValueType::Time => write!(f, "TIME"),
121 ValueType::DateTime => write!(f, "DATETIME"),
122 ValueType::Vertex => write!(f, "VERTEX"),
123 ValueType::Edge => write!(f, "EDGE"),
124 ValueType::Path => write!(f, "PATH"),
125 ValueType::List => write!(f, "LIST"),
126 ValueType::Map => write!(f, "MAP"),
127 ValueType::Set => write!(f, "SET"),
128 ValueType::Geography => write!(f, "GEOGRAPHY"),
129 ValueType::DataSet => write!(f, "DATASET"),
130 ValueType::Vector => write!(f, "VECTOR"),
131 ValueType::Json => write!(f, "JSON"),
132 ValueType::JsonB => write!(f, "JSONB"),
133 ValueType::Uuid => write!(f, "UUID"),
134 ValueType::Interval => write!(f, "INTERVAL"),
135 ValueType::Empty => write!(f, "EMPTY"),
136 ValueType::Any => write!(f, "ANY"),
137 }
138 }
139}
140
141#[derive(Debug, Clone)]
142pub struct FunctionSignature {
143 pub name: &'static str,
144 pub param_types: Vec<ValueType>,
145 pub return_type: Option<ValueType>,
146 pub is_variadic: bool,
147}
148
149impl FunctionSignature {
150 pub fn new(
151 name: &'static str,
152 param_types: Vec<ValueType>,
153 return_type: Option<ValueType>,
154 is_variadic: bool,
155 ) -> Self {
156 Self {
157 name,
158 param_types,
159 return_type,
160 is_variadic,
161 }
162 }
163}