MCPcopy Create free account
hub / github.com/apache/impala / DoTrunc

Function DoTrunc

be/src/exprs/udf-builtins.cc:283–385  ·  view source on GitHub ↗

Used by both TRUNC and DATE_TRUNC functions to perform the truncation

Source from the content-addressed store, hash-verified

281
282// Used by both TRUNC and DATE_TRUNC functions to perform the truncation
283TimestampVal DoTrunc(
284 const TimestampValue& ts, TruncUnit trunc_unit, FunctionContext* ctx) {
285 const date& orig_date = ts.date();
286 const time_duration& orig_time = ts.time();
287 TimestampValue ret;
288 TimestampVal ret_val;
289
290 // check for invalid or malformed timestamps
291 switch (trunc_unit) {
292 case TruncUnit::MILLENNIUM:
293 // for millenium <= 2000 year value goes to 1001 (outside the supported range)
294 if (orig_date.is_special()) return TimestampVal::null();
295 if (orig_date.year() <= 2000) return TimestampVal::null();
296 break;
297 case TruncUnit::CENTURY:
298 // for century <= 1400 year value goes to 1301 (outside the supported range)
299 if (orig_date.is_special()) return TimestampVal::null();
300 if (orig_date.year() <= 1400) return TimestampVal::null();
301 break;
302 case TruncUnit::WEEK:
303 // anything less than 1400-1-6 we have to move to year 1399
304 if (orig_date.is_special()) return TimestampVal::null();
305 if (orig_date < date(1400, 1, 6)) return TimestampVal::null();
306 break;
307 case TruncUnit::YEAR:
308 case TruncUnit::QUARTER:
309 case TruncUnit::MONTH:
310 case TruncUnit::WW:
311 case TruncUnit::W:
312 case TruncUnit::DAY:
313 case TruncUnit::DAY_OF_WEEK:
314 case TruncUnit::DECADE:
315 if (orig_date.is_special()) return TimestampVal::null();
316 break;
317 case TruncUnit::HOUR:
318 case TruncUnit::MINUTE:
319 case TruncUnit::SECOND:
320 case TruncUnit::MILLISECONDS:
321 case TruncUnit::MICROSECONDS:
322 if (orig_time.is_special()) return TimestampVal::null();
323 break;
324 case TruncUnit::UNIT_INVALID:
325 DCHECK(false);
326 }
327
328 switch(trunc_unit) {
329 case TruncUnit::YEAR:
330 ret = TruncYear(orig_date);
331 break;
332 case TruncUnit::QUARTER:
333 ret = TruncQuarter(orig_date);
334 break;
335 case TruncUnit::MONTH:
336 ret = TruncMonth(orig_date);
337 break;
338 case TruncUnit::WW:
339 ret = TruncWW(orig_date);
340 break;

Callers

nothing calls this directly

Calls 15

TruncYearFunction · 0.85
TruncQuarterFunction · 0.85
TruncMonthFunction · 0.85
TruncWWFunction · 0.85
TruncWFunction · 0.85
TruncDayFunction · 0.85
TruncDayOfWeekFunction · 0.85
TruncHourFunction · 0.85
TruncMinuteFunction · 0.85
TruncMillenniumFunction · 0.85
TruncCenturyFunction · 0.85
TruncDecadeFunction · 0.85

Tested by

no test coverage detected