@@ -2,6 +2,7 @@ use super::{time_series, Schema, SingleAliasedSource};
2
2
use crate :: planner:: sql_templates:: PlanSqlTemplates ;
3
3
use crate :: planner:: { BaseJoinCondition , BaseMember , VisitorContext } ;
4
4
use cubenativeutils:: CubeError ;
5
+ use lazy_static:: lazy_static;
5
6
6
7
use std:: rc:: Rc ;
7
8
@@ -33,85 +34,55 @@ impl RollingWindowJoinCondition {
33
34
}
34
35
}
35
36
36
- /*
37
- *
38
- offset = offset || 'end';
39
- return this.timeDimensions.map(
40
- d => [d, (dateFrom, dateTo, dateField, dimensionDateFrom, dimensionDateTo, isFromStartToEnd) => {
41
- // dateFrom based window
42
- const conditions = [];
43
- if (trailingInterval !== 'unbounded') {
44
- const startDate = isFromStartToEnd || offset === 'start' ? dateFrom : dateTo;
45
- const trailingStart = trailingInterval ? this.subtractInterval(startDate, trailingInterval) : startDate;
46
- const sign = offset === 'start' ? '>=' : '>';
47
- conditions.push(`${dateField} ${sign} ${trailingStart}`);
48
- }
49
- if (leadingInterval !== 'unbounded') {
50
- const endDate = isFromStartToEnd || offset === 'end' ? dateTo : dateFrom;
51
- const leadingEnd = leadingInterval ? this.addInterval(endDate, leadingInterval) : endDate;
52
- const sign = offset === 'end' ? '<=' : '<';
53
- conditions.push(`${dateField} ${sign} ${leadingEnd}`);
54
- }
55
- return conditions.length ? conditions.join(' AND ') : '1 = 1';
56
- }]
57
- );
58
- */
59
37
pub fn to_sql (
60
38
& self ,
61
39
templates : & PlanSqlTemplates ,
62
40
context : Rc < VisitorContext > ,
63
41
schema : Rc < Schema > ,
64
42
) -> Result < String , CubeError > {
65
43
let mut conditions = vec ! [ ] ;
66
- /* let date_column_alias = if let Some(column) = schema.find_column_for_member(&self.time_dimension.full_name(), &None) {
67
- templates.column_reference(&source, &column.alias.clone())
68
- } else {
69
- dimension.to_sql(context.clone(), schema.clone())
70
- } */
71
44
let date_column_alias =
72
45
self . resolve_time_column_alias ( templates, context. clone ( ) , schema. clone ( ) ) ?;
73
- if let Some ( trailing_interval) = & self . trailing_interval {
74
- if trailing_interval != "unbounded" {
75
- let start_date = if self . offset == "start" {
76
- templates
77
- . column_reference ( & Some ( self . time_series_source . clone ( ) ) , "date_from" ) ?
78
- } else {
79
- templates. column_reference ( & Some ( self . time_series_source . clone ( ) ) , "date_to" ) ?
80
- } ;
81
46
82
- let trailing_start = if let Some ( trailing_interval) = & self . trailing_interval {
83
- format ! ( "{start_date} - interval '{trailing_interval}'" )
84
- } else {
85
- start_date
86
- } ;
47
+ lazy_static ! {
48
+ static ref UNBOUNDED : Option <String > = Some ( "unbounded" . to_string( ) ) ;
49
+ }
87
50
88
- let sign = if self . offset == "start" { ">=" } else { ">" } ;
51
+ if self . trailing_interval != * UNBOUNDED {
52
+ let start_date = if self . offset == "start" {
53
+ templates. column_reference ( & Some ( self . time_series_source . clone ( ) ) , "date_from" ) ?
54
+ } else {
55
+ templates. column_reference ( & Some ( self . time_series_source . clone ( ) ) , "date_to" ) ?
56
+ } ;
89
57
90
- conditions. push ( format ! ( "{date_column_alias} {sign} {trailing_start}" ) ) ;
91
- }
58
+ let trailing_start = if let Some ( trailing_interval) = & self . trailing_interval {
59
+ format ! ( "{start_date} - interval '{trailing_interval}'" )
60
+ } else {
61
+ start_date
62
+ } ;
63
+
64
+ let sign = if self . offset == "start" { ">=" } else { ">" } ;
65
+
66
+ conditions. push ( format ! ( "{date_column_alias} {sign} {trailing_start}" ) ) ;
92
67
}
93
68
94
- if let Some ( leading_interval) = & self . trailing_interval {
95
- if leading_interval != "unbounded" {
96
- let end_date = if self . offset == "end" {
97
- templates. column_reference ( & Some ( self . time_series_source . clone ( ) ) , "date_to" ) ?
98
- } else {
99
- templates
100
- . column_reference ( & Some ( self . time_series_source . clone ( ) ) , "date_from" ) ?
101
- } ;
69
+ if self . leading_interval != * UNBOUNDED {
70
+ let end_date = if self . offset == "end" {
71
+ templates. column_reference ( & Some ( self . time_series_source . clone ( ) ) , "date_to" ) ?
72
+ } else {
73
+ templates. column_reference ( & Some ( self . time_series_source . clone ( ) ) , "date_from" ) ?
74
+ } ;
102
75
103
- let leading_end = if let Some ( leading_interval) = & self . leading_interval {
104
- format ! ( "{end_date} + interval '{leading_interval}'" )
105
- } else {
106
- end_date
107
- } ;
76
+ let leading_end = if let Some ( leading_interval) = & self . leading_interval {
77
+ format ! ( "{end_date} + interval '{leading_interval}'" )
78
+ } else {
79
+ end_date
80
+ } ;
108
81
109
- let sign = if self . offset == "end" { "<=" } else { "<" } ;
82
+ let sign = if self . offset == "end" { "<=" } else { "<" } ;
110
83
111
- conditions. push ( format ! ( "{date_column_alias} {sign} {leading_end}" ) ) ;
112
- }
84
+ conditions. push ( format ! ( "{date_column_alias} {sign} {leading_end}" ) ) ;
113
85
}
114
-
115
86
let result = if conditions. is_empty ( ) {
116
87
templates. always_true ( ) ?
117
88
} else {
0 commit comments