OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DateTimeTranslator.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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"
21 
22 #include "DateTimeUtils.h"
23 
24 #include <memory>
25 #include <string>
26 
27 using namespace DateTimeUtils;
28 
30  public:
31  static inline int64_t getExtractFromTimeConstantValue(const int64_t& timeval,
32  const ExtractField& field,
33  const SQLTypeInfo& ti) {
34  if (ti.is_high_precision_timestamp()) {
35  if (is_subsecond_extract_field(field)) {
36  const auto result =
38  return result.second ? ExtractFromTime(field,
39  result.first == kDIVIDE
40  ? floor_div(timeval, result.second)
41  : timeval * result.second)
42  : ExtractFromTime(field, timeval);
43  } else {
44  return ExtractFromTime(
46  }
47  } else if (is_subsecond_extract_field(field)) {
48  return ExtractFromTime(field,
49  timeval * get_extract_timestamp_precision_scale(field));
50  }
51  return ExtractFromTime(field, timeval);
52  }
53 
54  static inline int64_t getDateTruncConstantValue(const int64_t& timeval,
55  const DatetruncField& field,
56  const SQLTypeInfo& ti) {
57  if (ti.is_high_precision_timestamp()) {
58  if (is_subsecond_datetrunc_field(field)) {
59  int64_t date_truncate = DateTruncate(field, timeval);
61  if (result != -1) {
62  date_truncate -= unsigned_mod(date_truncate, result);
63  }
64  return date_truncate;
65  } else {
66  const int64_t scale = get_timestamp_precision_scale(ti.get_dimension());
67  return DateTruncate(field, floor_div(timeval, scale)) * scale;
68  }
69  }
70  return DateTruncate(field, timeval);
71  }
72 
73  protected:
74  static inline std::shared_ptr<Analyzer::Constant> getNumericConstant(
75  const int64_t scale) {
76  Datum d{0};
77  d.bigintval = scale;
78  return makeExpr<Analyzer::Constant>(SQLTypeInfo(kBIGINT, false), false, d);
79  }
80 };
81 
82 class ExtractExpr : protected DateTimeTranslator {
83  public:
84  ExtractExpr(const std::shared_ptr<Analyzer::Expr> expr, const ExtractField& field)
85  : from_expr_(expr), field_(field) {}
86  ExtractExpr(const std::shared_ptr<Analyzer::Expr> expr, const std::string& field)
87  : from_expr_(expr), field_(to_extract_field(field)) {}
88 
89  static std::shared_ptr<Analyzer::Expr> generate(const std::shared_ptr<Analyzer::Expr>,
90  const std::string&);
91  static std::shared_ptr<Analyzer::Expr> generate(const std::shared_ptr<Analyzer::Expr>,
92  const ExtractField&);
93 
94  const std::shared_ptr<Analyzer::Expr> generate() const {
95  return generate(from_expr_, field_);
96  }
97 
98  private:
99  static ExtractField to_extract_field(const std::string& field);
100 
101  std::shared_ptr<Analyzer::Expr> from_expr_;
103 };
104 
106  public:
107  DateTruncExpr(const std::shared_ptr<Analyzer::Expr> expr, const DatetruncField& field)
108  : from_expr_(expr), field_(field) {}
109  DateTruncExpr(const std::shared_ptr<Analyzer::Expr> expr, const std::string& field)
110  : from_expr_(expr), field_(to_datetrunc_field(field)) {}
111 
112  static std::shared_ptr<Analyzer::Expr> generate(const std::shared_ptr<Analyzer::Expr>,
113  const std::string&);
114  static std::shared_ptr<Analyzer::Expr> generate(const std::shared_ptr<Analyzer::Expr>,
115  const DatetruncField&);
116 
117  const std::shared_ptr<Analyzer::Expr> generate() const {
118  return generate(from_expr_, field_);
119  }
120 
121  private:
122  static DatetruncField to_datetrunc_field(const std::string& field);
123 
124  std::shared_ptr<Analyzer::Expr> from_expr_;
126 };
Defines data structures for the semantic analysis phase of query processing.
DateTruncExpr(const std::shared_ptr< Analyzer::Expr > expr, const DatetruncField &field)
constexpr int64_t get_extract_timestamp_precision_scale(const ExtractField field)
Definition: DateTimeUtils.h:81
DEVICE int64_t DateTruncate(DatetruncField field, const int64_t timeval)
DEVICE int64_t floor_div(int64_t const dividend, int64_t const divisor)
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:33
DatetruncField
Definition: DateTruncate.h:27
constexpr bool is_subsecond_datetrunc_field(const DatetruncField field)
const std::shared_ptr< Analyzer::Expr > generate() const
int64_t bigintval
Definition: Datum.h:76
static std::shared_ptr< Analyzer::Constant > getNumericConstant(const int64_t scale)
std::shared_ptr< Analyzer::Expr > from_expr_
DEVICE int64_t unsigned_mod(int64_t const dividend, int64_t const divisor)
ExtractExpr(const std::shared_ptr< Analyzer::Expr > expr, const std::string &field)
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:393
DEVICE int64_t ExtractFromTime(ExtractField field, const int64_t timeval)
ExtractField
DatetruncField field_
std::shared_ptr< Analyzer::Expr > from_expr_
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
Definition: DateTimeUtils.h:51
bool is_high_precision_timestamp() const
Definition: sqltypes.h:1036
static int64_t getDateTruncConstantValue(const int64_t &timeval, const DatetruncField &field, const SQLTypeInfo &ti)
Definition: Datum.h:71
DateTruncExpr(const std::shared_ptr< Analyzer::Expr > expr, const std::string &field)
constexpr bool is_subsecond_extract_field(const ExtractField &field)
Definition: DateTimeUtils.h:95
const std::shared_ptr< Analyzer::Expr > generate() const
const int64_t get_datetrunc_high_precision_scale(const DatetruncField &field, const int32_t dimen)