1 /* Copyright (C) 2014 InfiniDB, Inc.
2 
3    This program is free software; you can redistribute it and/or
4    modify it under the terms of the GNU General Public License
5    as published by the Free Software Foundation; version 2 of
6    the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16    MA 02110-1301, USA. */
17 
18 /****************************************************************************
19 * $Id: func_timestampdiff.cpp 3921 2013-06-19 18:59:56Z bwilkinson $
20 *
21 *
22 ****************************************************************************/
23 
24 #include <cstdlib>
25 #include <string>
26 #include <sstream>
27 using namespace std;
28 
29 #include "functioncolumn.h"
30 #include "constantcolumn.h"
31 #include "rowgroup.h"
32 using namespace execplan;
33 
34 #include "dataconvert.h"
35 using namespace dataconvert;
36 
37 #include "functor_dtm.h"
38 #include "funchelpers.h"
39 
40 namespace funcexp
41 {
42 
operationType(FunctionParm & fp,CalpontSystemCatalog::ColType & resultType)43 CalpontSystemCatalog::ColType Func_timestampdiff::operationType( FunctionParm& fp, CalpontSystemCatalog::ColType& resultType )
44 {
45     return resultType;
46 }
47 
getIntVal(rowgroup::Row & row,FunctionParm & parm,bool & isNull,CalpontSystemCatalog::ColType & op_ct)48 int64_t Func_timestampdiff::getIntVal(rowgroup::Row& row,
49                                       FunctionParm& parm,
50                                       bool& isNull,
51                                       CalpontSystemCatalog::ColType& op_ct)
52 {
53     int64_t val1, val2;
54     DateTime dt1, dt2;
55     if (parm[0]->data()->resultType().colDataType == execplan::CalpontSystemCatalog::TIMESTAMP)
56     {
57         TimeStamp timestamp(parm[0]->data()->getTimestampIntVal(row, isNull));
58         int64_t seconds = timestamp.second;
59         MySQLTime m_time;
60         gmtSecToMySQLTime(seconds, m_time, timeZone());
61         dt1.year = m_time.year;
62         dt1.month = m_time.month;
63         dt1.day = m_time.day;
64         dt1.hour = m_time.hour;
65         dt1.minute = m_time.minute;
66         dt1.second = m_time.second;
67         dt1.msecond = timestamp.msecond;
68         val1 = *(reinterpret_cast<int64_t*>(&dt1));
69     }
70     else
71     {
72         val1 = parm[0]->data()->getDatetimeIntVal(row, isNull);
73         dt1.year = (val1 >> 48) & 0xffff;
74         dt1.month = (val1 >> 44) & 0xf;
75         dt1.day = (val1 >> 38) & 0x3f;
76         dt1.hour = (val1 >> 32) & 0x3f;
77         dt1.minute = (val1 >> 26) & 0x3f;
78         dt1.second = (val1 >> 20) & 0x3f;
79         dt1.msecond = val1 & 0xfffff;
80     }
81 
82     if (parm[1]->data()->resultType().colDataType == execplan::CalpontSystemCatalog::TIMESTAMP)
83     {
84         TimeStamp timestamp(parm[1]->data()->getTimestampIntVal(row, isNull));
85         int64_t seconds = timestamp.second;
86         MySQLTime m_time;
87         gmtSecToMySQLTime(seconds, m_time, timeZone());
88         dt2.year = m_time.year;
89         dt2.month = m_time.month;
90         dt2.day = m_time.day;
91         dt2.hour = m_time.hour;
92         dt2.minute = m_time.minute;
93         dt2.second = m_time.second;
94         dt2.msecond = timestamp.msecond;
95         val2 = *(reinterpret_cast<int64_t*>(&dt2));
96     }
97     else
98     {
99         val2 = parm[1]->data()->getDatetimeIntVal(row, isNull);
100         dt2.year = (val2 >> 48) & 0xffff;
101         dt2.month = (val2 >> 44) & 0xf;
102         dt2.day = (val2 >> 38) & 0x3f;
103         dt2.hour = (val2 >> 32) & 0x3f;
104         dt2.minute = (val2 >> 26) & 0x3f;
105         dt2.second = (val2 >> 20) & 0x3f;
106         dt2.msecond = val2 & 0xfffff;
107     }
108 
109     IntervalColumn::interval_type unit = static_cast<IntervalColumn::interval_type>(parm[2]->data()->getIntVal(row, isNull));
110 
111     int64_t diff = 0;
112 
113     // unit: MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR
114     int64_t monthdiff = ((int64_t)dt2.month - (int64_t)dt1.month) +
115                         ((int64_t)dt2.year - (int64_t)dt1.year) * 12;
116 
117     if (unit == IntervalColumn::INTERVAL_YEAR)
118     {
119         diff = monthdiff / 12;
120     }
121     else if (unit == IntervalColumn::INTERVAL_MONTH)
122     {
123         diff = monthdiff;
124 
125         if (dt2.day < dt1.day && monthdiff > 0)
126             diff = monthdiff - 1;
127         else if (dt1.day < dt2.day && monthdiff < 0)
128             diff = monthdiff + 1;
129     }
130     else if (unit == IntervalColumn::INTERVAL_QUARTER)
131     {
132         diff = monthdiff / 3;
133         int daydiff = monthdiff % 3;
134 
135         if (daydiff == 0)
136         {
137             if (dt2.day < dt1.day && monthdiff > 0)
138                 diff --;
139             else if (dt1.day < dt2.day && monthdiff < 0)
140                 diff ++;
141         }
142     }
143     else
144     {
145         int64_t seconds = 0, mseconds = 0;
146         int l_sign = 1;
147         int l_sign3;
148 
149         l_sign3 = helpers::calc_time_diff(val2, val1, l_sign, (long long*)&seconds, (long long*)&mseconds);
150         l_sign3 = (l_sign3 == 0 ? 1 : -1);
151 
152         if (unit == IntervalColumn::INTERVAL_SECOND)
153             diff = l_sign3 * seconds;
154         else if (unit == IntervalColumn::INTERVAL_MICROSECOND)
155             diff = l_sign3 * (seconds * 1000000L + mseconds);
156         else if (unit == IntervalColumn::INTERVAL_MINUTE)
157             diff = l_sign3 * (seconds / 60L);
158         else if (unit == IntervalColumn::INTERVAL_HOUR)
159             diff = l_sign3 * (seconds / 3600L);
160         else if (unit == IntervalColumn::INTERVAL_DAY)
161             diff = l_sign3 * (seconds / (24L * 3600L));
162         else if (unit == IntervalColumn::INTERVAL_WEEK)
163             diff = l_sign3 * (seconds / (24L * 3600L) / 7L);
164     }
165 
166     return diff;
167 }
168 
getStrVal(rowgroup::Row & row,FunctionParm & parm,bool & isNull,CalpontSystemCatalog::ColType & ct)169 string Func_timestampdiff::getStrVal(rowgroup::Row& row,
170                                      FunctionParm& parm,
171                                      bool& isNull,
172                                      CalpontSystemCatalog::ColType& ct)
173 {
174     return intToString(getIntVal(row, parm, isNull, ct));
175 }
176 
getDateIntVal(rowgroup::Row & row,FunctionParm & parm,bool & isNull,CalpontSystemCatalog::ColType & ct)177 int32_t Func_timestampdiff::getDateIntVal(rowgroup::Row& row,
178         FunctionParm& parm,
179         bool& isNull,
180         CalpontSystemCatalog::ColType& ct)
181 {
182     return getIntVal(row, parm, isNull, ct);
183 }
184 
getDatetimeIntVal(rowgroup::Row & row,FunctionParm & parm,bool & isNull,CalpontSystemCatalog::ColType & ct)185 int64_t Func_timestampdiff::getDatetimeIntVal(rowgroup::Row& row,
186         FunctionParm& parm,
187         bool& isNull,
188         CalpontSystemCatalog::ColType& ct)
189 {
190     return getIntVal(row, parm, isNull, ct);
191 }
192 
getTimestampIntVal(rowgroup::Row & row,FunctionParm & parm,bool & isNull,CalpontSystemCatalog::ColType & ct)193 int64_t Func_timestampdiff::getTimestampIntVal(rowgroup::Row& row,
194         FunctionParm& parm,
195         bool& isNull,
196         CalpontSystemCatalog::ColType& ct)
197 {
198     return getIntVal(row, parm, isNull, ct);
199 }
200 
getTimeIntVal(rowgroup::Row & row,FunctionParm & parm,bool & isNull,CalpontSystemCatalog::ColType & ct)201 int64_t Func_timestampdiff::getTimeIntVal(rowgroup::Row& row,
202         FunctionParm& parm,
203         bool& isNull,
204         CalpontSystemCatalog::ColType& ct)
205 {
206     return getIntVal(row, parm, isNull, ct);
207 }
208 
209 } // namespace funcexp
210 // vim:ts=4 sw=4:
211