1use gitql_ast::types::any::AnyType;
2use gitql_ast::types::boolean::BoolType;
3use gitql_ast::types::date::DateType;
4use gitql_ast::types::datetime::DateTimeType;
5use gitql_ast::types::integer::IntType;
6use gitql_ast::types::range::RangeType;
7use gitql_core::signature::Signature;
8use gitql_core::signature::StandardFunction;
9use gitql_core::values::boolean::BoolValue;
10use gitql_core::values::range::RangeValue;
11use gitql_core::values::Value;
12
13use std::collections::HashMap;
14
15#[inline(always)]
16pub fn register_std_range_functions(map: &mut HashMap<&'static str, StandardFunction>) {
17 map.insert("int4range", int4range);
18 map.insert("daterange", daterange);
19 map.insert("tsrange", tsrange);
20 map.insert("isempty", isempty);
21}
22
23#[inline(always)]
24pub fn register_std_range_function_signatures(map: &mut HashMap<&'static str, Signature>) {
25 map.insert(
26 "int4range",
27 Signature {
28 parameters: vec![Box::new(IntType), Box::new(IntType)],
29 return_type: Box::new(RangeType {
30 base: Box::new(IntType),
31 }),
32 },
33 );
34 map.insert(
35 "daterange",
36 Signature {
37 parameters: vec![Box::new(DateType), Box::new(DateType)],
38 return_type: Box::new(RangeType {
39 base: Box::new(DateType),
40 }),
41 },
42 );
43 map.insert(
44 "tsrange",
45 Signature {
46 parameters: vec![Box::new(DateTimeType), Box::new(DateTimeType)],
47 return_type: Box::new(RangeType {
48 base: Box::new(DateTimeType),
49 }),
50 },
51 );
52 map.insert(
53 "isempty",
54 Signature {
55 parameters: vec![Box::new(RangeType {
56 base: Box::new(AnyType),
57 })],
58 return_type: Box::new(BoolType),
59 },
60 );
61}
62
63pub fn int4range(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
64 Box::new(RangeValue {
65 start: inputs[0].clone(),
66 end: inputs[1].clone(),
67 base_type: Box::new(IntType),
68 })
69}
70
71pub fn daterange(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
72 Box::new(RangeValue {
73 start: inputs[0].clone(),
74 end: inputs[1].clone(),
75 base_type: Box::new(DateType),
76 })
77}
78
79pub fn tsrange(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
80 Box::new(RangeValue {
81 start: inputs[0].clone(),
82 end: inputs[1].clone(),
83 base_type: Box::new(DateTimeType),
84 })
85}
86
87pub fn isempty(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
88 let range = inputs[0].as_range().unwrap();
89 Box::new(BoolValue {
90 value: range.0.equals(&range.1),
91 })
92}