sql_cli/sql/functions/
integer_limits.rs

1use crate::data::datatable::DataValue;
2use crate::sql::functions::{ArgCount, FunctionCategory, FunctionSignature, SqlFunction};
3use anyhow::Result;
4
5// INT8 / BYTE limits (8-bit)
6pub struct Int8Min;
7impl SqlFunction for Int8Min {
8    fn signature(&self) -> FunctionSignature {
9        FunctionSignature {
10            name: "INT8_MIN",
11            category: FunctionCategory::Mathematical,
12            arg_count: ArgCount::Fixed(0),
13            description: "Minimum value for signed 8-bit integer (-128)",
14            returns: "Integer value",
15            examples: vec!["SELECT INT8_MIN()  -- Returns -128"],
16        }
17    }
18
19    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
20        Ok(DataValue::Integer(-128))
21    }
22}
23
24pub struct Int8Max;
25impl SqlFunction for Int8Max {
26    fn signature(&self) -> FunctionSignature {
27        FunctionSignature {
28            name: "INT8_MAX",
29            category: FunctionCategory::Mathematical,
30            arg_count: ArgCount::Fixed(0),
31            description: "Maximum value for signed 8-bit integer (127)",
32            returns: "Integer value",
33            examples: vec!["SELECT INT8_MAX()  -- Returns 127"],
34        }
35    }
36
37    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
38        Ok(DataValue::Integer(127))
39    }
40}
41
42pub struct Uint8Max;
43impl SqlFunction for Uint8Max {
44    fn signature(&self) -> FunctionSignature {
45        FunctionSignature {
46            name: "UINT8_MAX",
47            category: FunctionCategory::Mathematical,
48            arg_count: ArgCount::Fixed(0),
49            description: "Maximum value for unsigned 8-bit integer (255)",
50            returns: "Integer value",
51            examples: vec!["SELECT UINT8_MAX()  -- Returns 255"],
52        }
53    }
54
55    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
56        Ok(DataValue::Integer(255))
57    }
58}
59
60// INT16 / SHORT limits (16-bit)
61pub struct Int16Min;
62impl SqlFunction for Int16Min {
63    fn signature(&self) -> FunctionSignature {
64        FunctionSignature {
65            name: "INT16_MIN",
66            category: FunctionCategory::Mathematical,
67            arg_count: ArgCount::Fixed(0),
68            description: "Minimum value for signed 16-bit integer (-32768)",
69            returns: "Integer value",
70            examples: vec!["SELECT INT16_MIN()  -- Returns -32768"],
71        }
72    }
73
74    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
75        Ok(DataValue::Integer(-32768))
76    }
77}
78
79pub struct Int16Max;
80impl SqlFunction for Int16Max {
81    fn signature(&self) -> FunctionSignature {
82        FunctionSignature {
83            name: "INT16_MAX",
84            category: FunctionCategory::Mathematical,
85            arg_count: ArgCount::Fixed(0),
86            description: "Maximum value for signed 16-bit integer (32767)",
87            returns: "Integer value",
88            examples: vec!["SELECT INT16_MAX()  -- Returns 32767"],
89        }
90    }
91
92    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
93        Ok(DataValue::Integer(32767))
94    }
95}
96
97pub struct Uint16Max;
98impl SqlFunction for Uint16Max {
99    fn signature(&self) -> FunctionSignature {
100        FunctionSignature {
101            name: "UINT16_MAX",
102            category: FunctionCategory::Mathematical,
103            arg_count: ArgCount::Fixed(0),
104            description: "Maximum value for unsigned 16-bit integer (65535)",
105            returns: "Integer value",
106            examples: vec!["SELECT UINT16_MAX()  -- Returns 65535"],
107        }
108    }
109
110    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
111        Ok(DataValue::Integer(65535))
112    }
113}
114
115// INT32 limits (32-bit)
116pub struct Int32Min;
117impl SqlFunction for Int32Min {
118    fn signature(&self) -> FunctionSignature {
119        FunctionSignature {
120            name: "INT32_MIN",
121            category: FunctionCategory::Mathematical,
122            arg_count: ArgCount::Fixed(0),
123            description: "Minimum value for signed 32-bit integer (-2147483648)",
124            returns: "Integer value",
125            examples: vec!["SELECT INT32_MIN()  -- Returns -2147483648"],
126        }
127    }
128
129    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
130        Ok(DataValue::Integer(-2147483648))
131    }
132}
133
134pub struct Int32Max;
135impl SqlFunction for Int32Max {
136    fn signature(&self) -> FunctionSignature {
137        FunctionSignature {
138            name: "INT32_MAX",
139            category: FunctionCategory::Mathematical,
140            arg_count: ArgCount::Fixed(0),
141            description: "Maximum value for signed 32-bit integer (2147483647)",
142            returns: "Integer value",
143            examples: vec!["SELECT INT32_MAX()  -- Returns 2147483647"],
144        }
145    }
146
147    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
148        Ok(DataValue::Integer(2147483647))
149    }
150}
151
152pub struct Uint32Max;
153impl SqlFunction for Uint32Max {
154    fn signature(&self) -> FunctionSignature {
155        FunctionSignature {
156            name: "UINT32_MAX",
157            category: FunctionCategory::Mathematical,
158            arg_count: ArgCount::Fixed(0),
159            description: "Maximum value for unsigned 32-bit integer (4294967295)",
160            returns: "Integer value",
161            examples: vec!["SELECT UINT32_MAX()  -- Returns 4294967295"],
162        }
163    }
164
165    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
166        Ok(DataValue::Integer(4294967295))
167    }
168}
169
170// INT64 limits (64-bit)
171pub struct Int64Min;
172impl SqlFunction for Int64Min {
173    fn signature(&self) -> FunctionSignature {
174        FunctionSignature {
175            name: "INT64_MIN",
176            category: FunctionCategory::Mathematical,
177            arg_count: ArgCount::Fixed(0),
178            description: "Minimum value for signed 64-bit integer (-9223372036854775808)",
179            returns: "Integer value",
180            examples: vec!["SELECT INT64_MIN()  -- Returns -9223372036854775808"],
181        }
182    }
183
184    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
185        Ok(DataValue::Integer(i64::MIN))
186    }
187}
188
189pub struct Int64Max;
190impl SqlFunction for Int64Max {
191    fn signature(&self) -> FunctionSignature {
192        FunctionSignature {
193            name: "INT64_MAX",
194            category: FunctionCategory::Mathematical,
195            arg_count: ArgCount::Fixed(0),
196            description: "Maximum value for signed 64-bit integer (9223372036854775807)",
197            returns: "Integer value",
198            examples: vec!["SELECT INT64_MAX()  -- Returns 9223372036854775807"],
199        }
200    }
201
202    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
203        Ok(DataValue::Integer(i64::MAX))
204    }
205}
206
207// Alias functions for common names
208pub struct ByteMin;
209impl SqlFunction for ByteMin {
210    fn signature(&self) -> FunctionSignature {
211        FunctionSignature {
212            name: "BYTE_MIN",
213            category: FunctionCategory::Mathematical,
214            arg_count: ArgCount::Fixed(0),
215            description: "Minimum value for unsigned byte (0)",
216            returns: "Integer value",
217            examples: vec!["SELECT BYTE_MIN()  -- Returns 0"],
218        }
219    }
220
221    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
222        Ok(DataValue::Integer(0))
223    }
224}
225
226pub struct ByteMax;
227impl SqlFunction for ByteMax {
228    fn signature(&self) -> FunctionSignature {
229        FunctionSignature {
230            name: "BYTE_MAX",
231            category: FunctionCategory::Mathematical,
232            arg_count: ArgCount::Fixed(0),
233            description: "Maximum value for unsigned byte (255)",
234            returns: "Integer value",
235            examples: vec!["SELECT BYTE_MAX()  -- Returns 255"],
236        }
237    }
238
239    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
240        Ok(DataValue::Integer(255))
241    }
242}
243
244pub struct CharMin;
245impl SqlFunction for CharMin {
246    fn signature(&self) -> FunctionSignature {
247        FunctionSignature {
248            name: "CHAR_MIN",
249            category: FunctionCategory::Mathematical,
250            arg_count: ArgCount::Fixed(0),
251            description: "Minimum value for signed char (-128)",
252            returns: "Integer value",
253            examples: vec!["SELECT CHAR_MIN()  -- Returns -128"],
254        }
255    }
256
257    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
258        Ok(DataValue::Integer(-128))
259    }
260}
261
262pub struct CharMax;
263impl SqlFunction for CharMax {
264    fn signature(&self) -> FunctionSignature {
265        FunctionSignature {
266            name: "CHAR_MAX",
267            category: FunctionCategory::Mathematical,
268            arg_count: ArgCount::Fixed(0),
269            description: "Maximum value for signed char (127)",
270            returns: "Integer value",
271            examples: vec!["SELECT CHAR_MAX()  -- Returns 127"],
272        }
273    }
274
275    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
276        Ok(DataValue::Integer(127))
277    }
278}
279
280pub struct ShortMin;
281impl SqlFunction for ShortMin {
282    fn signature(&self) -> FunctionSignature {
283        FunctionSignature {
284            name: "SHORT_MIN",
285            category: FunctionCategory::Mathematical,
286            arg_count: ArgCount::Fixed(0),
287            description: "Minimum value for signed short (-32768)",
288            returns: "Integer value",
289            examples: vec!["SELECT SHORT_MIN()  -- Returns -32768"],
290        }
291    }
292
293    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
294        Ok(DataValue::Integer(-32768))
295    }
296}
297
298pub struct ShortMax;
299impl SqlFunction for ShortMax {
300    fn signature(&self) -> FunctionSignature {
301        FunctionSignature {
302            name: "SHORT_MAX",
303            category: FunctionCategory::Mathematical,
304            arg_count: ArgCount::Fixed(0),
305            description: "Maximum value for signed short (32767)",
306            returns: "Integer value",
307            examples: vec!["SELECT SHORT_MAX()  -- Returns 32767"],
308        }
309    }
310
311    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
312        Ok(DataValue::Integer(32767))
313    }
314}
315
316pub struct IntMin;
317impl SqlFunction for IntMin {
318    fn signature(&self) -> FunctionSignature {
319        FunctionSignature {
320            name: "INT_MIN",
321            category: FunctionCategory::Mathematical,
322            arg_count: ArgCount::Fixed(0),
323            description: "Minimum value for signed 32-bit int (-2147483648)",
324            returns: "Integer value",
325            examples: vec!["SELECT INT_MIN()  -- Returns -2147483648"],
326        }
327    }
328
329    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
330        Ok(DataValue::Integer(-2147483648))
331    }
332}
333
334pub struct IntMax;
335impl SqlFunction for IntMax {
336    fn signature(&self) -> FunctionSignature {
337        FunctionSignature {
338            name: "INT_MAX",
339            category: FunctionCategory::Mathematical,
340            arg_count: ArgCount::Fixed(0),
341            description: "Maximum value for signed 32-bit int (2147483647)",
342            returns: "Integer value",
343            examples: vec!["SELECT INT_MAX()  -- Returns 2147483647"],
344        }
345    }
346
347    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
348        Ok(DataValue::Integer(2147483647))
349    }
350}
351
352pub struct LongMin;
353impl SqlFunction for LongMin {
354    fn signature(&self) -> FunctionSignature {
355        FunctionSignature {
356            name: "LONG_MIN",
357            category: FunctionCategory::Mathematical,
358            arg_count: ArgCount::Fixed(0),
359            description: "Minimum value for signed 64-bit long (-9223372036854775808)",
360            returns: "Integer value",
361            examples: vec!["SELECT LONG_MIN()  -- Returns -9223372036854775808"],
362        }
363    }
364
365    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
366        Ok(DataValue::Integer(i64::MIN))
367    }
368}
369
370pub struct LongMax;
371impl SqlFunction for LongMax {
372    fn signature(&self) -> FunctionSignature {
373        FunctionSignature {
374            name: "LONG_MAX",
375            category: FunctionCategory::Mathematical,
376            arg_count: ArgCount::Fixed(0),
377            description: "Maximum value for signed 64-bit long (9223372036854775807)",
378            returns: "Integer value",
379            examples: vec!["SELECT LONG_MAX()  -- Returns 9223372036854775807"],
380        }
381    }
382
383    fn evaluate(&self, _args: &[DataValue]) -> Result<DataValue> {
384        Ok(DataValue::Integer(i64::MAX))
385    }
386}
387
388#[cfg(test)]
389mod tests {
390    use super::*;
391
392    #[test]
393    fn test_int8_limits() {
394        assert_eq!(Int8Min.evaluate(&[]).unwrap(), DataValue::Integer(-128));
395        assert_eq!(Int8Max.evaluate(&[]).unwrap(), DataValue::Integer(127));
396        assert_eq!(Uint8Max.evaluate(&[]).unwrap(), DataValue::Integer(255));
397    }
398
399    #[test]
400    fn test_int16_limits() {
401        assert_eq!(Int16Min.evaluate(&[]).unwrap(), DataValue::Integer(-32768));
402        assert_eq!(Int16Max.evaluate(&[]).unwrap(), DataValue::Integer(32767));
403        assert_eq!(Uint16Max.evaluate(&[]).unwrap(), DataValue::Integer(65535));
404    }
405
406    #[test]
407    fn test_int32_limits() {
408        assert_eq!(
409            Int32Min.evaluate(&[]).unwrap(),
410            DataValue::Integer(-2147483648)
411        );
412        assert_eq!(
413            Int32Max.evaluate(&[]).unwrap(),
414            DataValue::Integer(2147483647)
415        );
416        assert_eq!(
417            Uint32Max.evaluate(&[]).unwrap(),
418            DataValue::Integer(4294967295)
419        );
420    }
421
422    #[test]
423    fn test_int64_limits() {
424        assert_eq!(
425            Int64Min.evaluate(&[]).unwrap(),
426            DataValue::Integer(i64::MIN)
427        );
428        assert_eq!(
429            Int64Max.evaluate(&[]).unwrap(),
430            DataValue::Integer(i64::MAX)
431        );
432    }
433
434    #[test]
435    fn test_alias_functions() {
436        assert_eq!(ByteMin.evaluate(&[]).unwrap(), DataValue::Integer(0));
437        assert_eq!(ByteMax.evaluate(&[]).unwrap(), DataValue::Integer(255));
438        assert_eq!(CharMin.evaluate(&[]).unwrap(), DataValue::Integer(-128));
439        assert_eq!(CharMax.evaluate(&[]).unwrap(), DataValue::Integer(127));
440        assert_eq!(ShortMin.evaluate(&[]).unwrap(), DataValue::Integer(-32768));
441        assert_eq!(ShortMax.evaluate(&[]).unwrap(), DataValue::Integer(32767));
442        assert_eq!(
443            IntMin.evaluate(&[]).unwrap(),
444            DataValue::Integer(-2147483648)
445        );
446        assert_eq!(
447            IntMax.evaluate(&[]).unwrap(),
448            DataValue::Integer(2147483647)
449        );
450        assert_eq!(LongMin.evaluate(&[]).unwrap(), DataValue::Integer(i64::MIN));
451        assert_eq!(LongMax.evaluate(&[]).unwrap(), DataValue::Integer(i64::MAX));
452    }
453}