OmniSciDB  b24e664e58
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DateTimeTranslator.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "../Analyzer/Analyzer.h"
20 
21 #include "DateTimeUtils.h"
22 
23 #include <memory>
24 #include <string>
25 
26 using namespace DateTimeUtils;
27 
28 namespace Analyzer {
29 
30 class Expr;
31 
32 } // namespace Analyzer
33 
35  public:
36  static inline int64_t getExtractFromTimeConstantValue(const int64_t& timeval,
37  const ExtractField& field,
38  const SQLTypeInfo& ti) {
39  if (ti.is_high_precision_timestamp()) {
40  if (is_subsecond_extract_field(field)) {
41  const auto result =
43  return result.second
44  ? ExtractFromTime(field,
45  result.first == kDIVIDE ? timeval / result.second
46  : timeval * result.second)
47  : ExtractFromTime(field, timeval);
48  } else {
49  return ExtractFromTime(
50  field, timeval / get_timestamp_precision_scale(ti.get_dimension()));
51  }
52  } else if (is_subsecond_extract_field(field)) {
53  return ExtractFromTime(field,
54  timeval * get_extract_timestamp_precision_scale(field));
55  }
56  return ExtractFromTime(field, timeval);
57  }
58 
59  static inline int64_t getDateTruncConstantValue(const int64_t& timeval,
60  const DatetruncField& field,
61  const SQLTypeInfo& ti) {
62  if (ti.is_high_precision_timestamp()) {
63  if (is_subsecond_datetrunc_field(field)) {
65  return result != -1 ? (DateTruncate(field, timeval) / result) * result
66  : DateTruncate(field, timeval);
67  } else {
68  const int64_t scale = get_timestamp_precision_scale(ti.get_dimension());
69  return DateTruncate(field, timeval / scale) * scale;
70  }
71  }
72  return DateTruncate(field, timeval);
73  }
74 
75  protected:
76  static inline std::shared_ptr<Analyzer::Constant> getNumericConstant(
77  const int64_t scale) {
78  Datum d{0};
79  d.bigintval = scale;
80  return makeExpr<Analyzer::Constant>(SQLTypeInfo(kBIGINT, false), false, d);
81  }
82 };
83 
84 class ExtractExpr : protected DateTimeTranslator {
85  public:
86  ExtractExpr(const std::shared_ptr<Analyzer::Expr> expr, const ExtractField& field)
87  : from_expr_(expr), field_(field) {}
88  ExtractExpr(const std::shared_ptr<Analyzer::Expr> expr, const std::string& field)
89  : from_expr_(expr), field_(to_extract_field(field)) {}
90 
91  static std::shared_ptr<Analyzer::Expr> generate(const std::shared_ptr<Analyzer::Expr>,
92  const std::string&);
93  static std::shared_ptr<Analyzer::Expr> generate(const std::shared_ptr<Analyzer::Expr>,
94  const ExtractField&);
95 
96  const std::shared_ptr<Analyzer::Expr> generate() const {
97  return generate(from_expr_, field_);
98  }
99 
100  private:
101  static ExtractField to_extract_field(const std::string& field);
102 
103  std::shared_ptr<Analyzer::Expr> from_expr_;
105 };
106 
108  public:
109  DateTruncExpr(const std::shared_ptr<Analyzer::Expr> expr, const DatetruncField& field)
110  : from_expr_(expr), field_(field) {}
111  DateTruncExpr(const std::shared_ptr<Analyzer::Expr> expr, const std::string& field)
112  : from_expr_(expr), field_(to_datetrunc_field(field)) {}
113 
114  static std::shared_ptr<Analyzer::Expr> generate(const std::shared_ptr<Analyzer::Expr>,
115  const std::string&);
116  static std::shared_ptr<Analyzer::Expr> generate(const std::shared_ptr<Analyzer::Expr>,
117  const DatetruncField&);
118 
119  const std::shared_ptr<Analyzer::Expr> generate() const {
120  return generate(from_expr_, field_);
121  }
122 
123  private:
124  static DatetruncField to_datetrunc_field(const std::string& field);
125 
126  std::shared_ptr<Analyzer::Expr> from_expr_;
128 };
DateTruncExpr(const std::shared_ptr< Analyzer::Expr > expr, const DatetruncField &field)
NEVER_INLINE DEVICE int64_t DateTruncate(DatetruncField field, const int64_t timeval)
constexpr int64_t get_extract_timestamp_precision_scale(const ExtractField field)
Definition: DateTimeUtils.h:78
const std::pair< SQLOps, int64_t > get_extract_high_precision_adjusted_scale(const ExtractField &field, const int32_t dimen)
ExtractField field_
static int64_t getExtractFromTimeConstantValue(const int64_t &timeval, const ExtractField &field, const SQLTypeInfo &ti)
ExtractExpr(const std::shared_ptr< Analyzer::Expr > expr, const ExtractField &field)
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
DatetruncField
Definition: DateTruncate.h:42
constexpr bool is_subsecond_datetrunc_field(const DatetruncField field)
const std::shared_ptr< Analyzer::Expr > generate() const
int64_t bigintval
Definition: sqltypes.h:129
NEVER_INLINE DEVICE int64_t ExtractFromTime(ExtractField field, const int64_t timeval)
static std::shared_ptr< Analyzer::Constant > getNumericConstant(const int64_t scale)
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
std::shared_ptr< Analyzer::Expr > from_expr_
ExtractExpr(const std::shared_ptr< Analyzer::Expr > expr, const std::string &field)
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:328
ExtractField
DatetruncField field_
std::shared_ptr< Analyzer::Expr > from_expr_
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
Definition: DateTimeUtils.h:48
static int64_t getDateTruncConstantValue(const int64_t &timeval, const DatetruncField &field, const SQLTypeInfo &ti)
DateTruncExpr(const std::shared_ptr< Analyzer::Expr > expr, const std::string &field)
constexpr bool is_subsecond_extract_field(const ExtractField &field)
Definition: DateTimeUtils.h:92
const std::shared_ptr< Analyzer::Expr > generate() const
const int64_t get_datetrunc_high_precision_scale(const DatetruncField &field, const int32_t dimen)