datafusion_match_recognize/core/
metadata.rs1use std::fmt::{self, Display, Formatter};
23use std::str::FromStr;
24use std::sync::Arc;
25
26use datafusion::arrow::datatypes::{DataType, Field};
27
28pub const MR_INTERNAL_PREFIX: &str = "__mr_";
30const MR_SYMBOL_PREFIX: &str = "__mr_symbol_";
32const MR_CLASSIFIER_BITS_PREFIX: &str = "__mr_classifier_";
34const MR_MANAGED_AGGREGATE_PREFIX: &str = "__mr_managed_agg_";
36pub const MR_STATIC_EXTRACTION_PREFIX: &str = "__mr_static_";
38
39pub const MIN_VALID_MATCH_SEQUENCE_NUMBER: i64 = 0;
41pub const MAX_VALID_MATCH_SEQUENCE_NUMBER: i64 = (1i64 << 62) - 1;
43
44#[derive(Debug, Clone)]
45struct MetadataDescriptor {
46 name: &'static str,
47 data_type: DataType,
48 nullable: bool,
49 measure_function_name: &'static str,
50}
51
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
54pub enum MatchMetadataColumn {
55 Classifier,
57 MatchNumber,
59 MatchSequenceNumber,
61 Excluded,
63 PathId,
65}
66
67impl MatchMetadataColumn {
68 fn descriptor(self) -> MetadataDescriptor {
69 match self {
70 Self::Classifier => MetadataDescriptor {
71 name: "__mr_classifier",
72 data_type: DataType::Utf8,
73 nullable: true,
74 measure_function_name: "classifier",
75 },
76 Self::MatchNumber => MetadataDescriptor {
77 name: "__mr_match_number",
78 data_type: DataType::Int64,
79 nullable: true,
80 measure_function_name: "match_number",
81 },
82 Self::MatchSequenceNumber => MetadataDescriptor {
83 name: "__mr_match_sequence_number",
84 data_type: DataType::Int64,
85 nullable: true,
86 measure_function_name: "match_sequence_number",
87 },
88 Self::Excluded => MetadataDescriptor {
89 name: "__mr_excluded",
90 data_type: DataType::Boolean,
91 nullable: true,
92 measure_function_name: "excluded",
93 },
94 Self::PathId => MetadataDescriptor {
95 name: "__mr_path_id",
96 data_type: DataType::Int64,
97 nullable: false,
98 measure_function_name: "path_id",
99 },
100 }
101 }
102
103 pub fn data_type(&self) -> DataType {
105 self.descriptor().data_type
106 }
107
108 pub fn is_nullable(&self) -> bool {
110 self.descriptor().nullable
111 }
112
113 pub fn measure_function_name(&self) -> &'static str {
117 self.descriptor().measure_function_name
118 }
119
120 pub const fn all() -> [Self; 5] {
122 [
123 Self::Classifier,
124 Self::MatchNumber,
125 Self::MatchSequenceNumber,
126 Self::Excluded,
127 Self::PathId,
128 ]
129 }
130
131 pub const fn public_output() -> [Self; 4] {
135 [
136 Self::Classifier,
137 Self::MatchNumber,
138 Self::MatchSequenceNumber,
139 Self::Excluded,
140 ]
141 }
142
143 pub fn arrow_field(self) -> Arc<Field> {
145 let descriptor = self.descriptor();
146 Arc::new(Field::new(
147 descriptor.name,
148 descriptor.data_type,
149 descriptor.nullable,
150 ))
151 }
152}
153
154impl AsRef<str> for MatchMetadataColumn {
155 fn as_ref(&self) -> &str {
156 self.descriptor().name
157 }
158}
159
160impl FromStr for MatchMetadataColumn {
161 type Err = ();
162 fn from_str(s: &str) -> Result<Self, Self::Err> {
163 Self::all()
164 .into_iter()
165 .find(|column| column.as_ref() == s)
166 .ok_or(())
167 }
168}
169
170impl Display for MatchMetadataColumn {
171 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
172 f.write_str(self.as_ref())
173 }
174}
175
176pub fn symbol_col_name(symbol: &str) -> String {
178 format!("{MR_SYMBOL_PREFIX}{}", symbol.to_ascii_lowercase())
179}
180
181pub fn is_reserved_internal_column_name(name: &str) -> bool {
183 name.get(..MR_INTERNAL_PREFIX.len())
184 .is_some_and(|prefix| prefix.eq_ignore_ascii_case(MR_INTERNAL_PREFIX))
185}
186
187pub fn classifier_bits_col_name(symbol: &str) -> String {
189 format!("{MR_CLASSIFIER_BITS_PREFIX}{}", symbol.to_ascii_lowercase())
190}
191
192fn classifier_bits_field(symbol: &str) -> Arc<Field> {
193 Arc::new(Field::new(
194 classifier_bits_col_name(symbol),
195 DataType::Boolean,
196 true,
197 ))
198}
199
200pub fn classifier_bits_arrow_fields(symbols: &[String]) -> Vec<Arc<Field>> {
205 symbols
206 .iter()
207 .map(|symbol| classifier_bits_field(symbol))
208 .collect()
209}
210
211pub fn static_predicate_and_col_name(id: usize) -> String {
213 format!("{MR_STATIC_EXTRACTION_PREFIX}predicate_and_{id}")
214}
215
216pub fn static_predicate_or_col_name(id: usize) -> String {
218 format!("{MR_STATIC_EXTRACTION_PREFIX}predicate_or_{id}")
219}
220
221pub fn static_scalar_col_name(id: usize) -> String {
223 format!("{MR_STATIC_EXTRACTION_PREFIX}scalar_{id}")
224}
225
226pub fn managed_aggregate_col_name(id: usize) -> String {
228 format!("{MR_MANAGED_AGGREGATE_PREFIX}{id}")
229}
230
231#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
233pub enum StaticPredicateKind {
234 BooleanAnd,
236 BooleanOr,
238}
239
240#[cfg(test)]
241mod tests {
242 use super::*;
243
244 #[test]
245 fn public_output_metadata_excludes_internal_path_id() {
246 assert!(
247 !MatchMetadataColumn::public_output().contains(&MatchMetadataColumn::PathId)
248 );
249 assert!(MatchMetadataColumn::all().contains(&MatchMetadataColumn::PathId));
250 }
251
252 #[test]
253 fn internal_column_reservation_checks_case_and_prefix_boundaries() {
254 for (name, reserved) in [
255 ("__mr_match_number", true),
256 ("__MR_classifier_a", true),
257 ("__Mr_symbol_a", true),
258 ("__mr", false),
259 ("user___mr_match_number", false),
260 ("match_number", false),
261 ("", false),
262 ] {
263 assert_eq!(is_reserved_internal_column_name(name), reserved, "{name}");
264 }
265 }
266}