gitql_std/range/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use gitql_ast::types::any::AnyType;
use gitql_ast::types::boolean::BoolType;
use gitql_ast::types::date::DateType;
use gitql_ast::types::datetime::DateTimeType;
use gitql_ast::types::integer::IntType;
use gitql_ast::types::range::RangeType;
use gitql_core::signature::Function;
use gitql_core::signature::Signature;
use gitql_core::values::base::Value;
use gitql_core::values::boolean::BoolValue;
use gitql_core::values::range::RangeValue;

use std::collections::HashMap;

#[inline(always)]
pub fn register_std_range_functions(map: &mut HashMap<&'static str, Function>) {
    map.insert("int4range", int4range);
    map.insert("daterange", daterange);
    map.insert("tsrange", tsrange);
    map.insert("isempty", isempty);
}

#[inline(always)]
pub fn register_std_range_function_signatures(map: &mut HashMap<&'static str, Signature>) {
    map.insert(
        "int4range",
        Signature {
            parameters: vec![Box::new(IntType), Box::new(IntType)],
            return_type: Box::new(RangeType {
                base: Box::new(IntType),
            }),
        },
    );
    map.insert(
        "daterange",
        Signature {
            parameters: vec![Box::new(DateType), Box::new(DateType)],
            return_type: Box::new(RangeType {
                base: Box::new(DateType),
            }),
        },
    );
    map.insert(
        "tsrange",
        Signature {
            parameters: vec![Box::new(DateTimeType), Box::new(DateTimeType)],
            return_type: Box::new(RangeType {
                base: Box::new(DateTimeType),
            }),
        },
    );
    map.insert(
        "isempty",
        Signature {
            parameters: vec![Box::new(RangeType {
                base: Box::new(AnyType),
            })],
            return_type: Box::new(BoolType),
        },
    );
}

pub fn int4range(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    Box::new(RangeValue {
        start: inputs[0].clone(),
        end: inputs[1].clone(),
        base_type: Box::new(IntType),
    })
}

pub fn daterange(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    Box::new(RangeValue {
        start: inputs[0].clone(),
        end: inputs[1].clone(),
        base_type: Box::new(DateType),
    })
}

pub fn tsrange(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    Box::new(RangeValue {
        start: inputs[0].clone(),
        end: inputs[1].clone(),
        base_type: Box::new(DateTimeType),
    })
}

pub fn isempty(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let range = inputs[0].as_range().unwrap();
    Box::new(BoolValue {
        value: range.0.equals(&range.1),
    })
}