OmniSciDB  2c44a3935d
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"
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
39  ? ExtractFromTime(field,
40  result.first == kDIVIDE ? timeval / result.second
41  : timeval * result.second)
42  : ExtractFromTime(field, timeval);
43  } else {
44  return ExtractFromTime(
45  field, timeval / get_timestamp_precision_scale(ti.get_dimension()));
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)) {
60  return result != -1 ? (DateTruncate(field, timeval) / result) * result
61  : DateTruncate(field, timeval);
62  } else {
63  const int64_t scale = get_timestamp_precision_scale(ti.get_dimension());
64  return DateTruncate(field, timeval / scale) * scale;
65  }
66  }
67  return DateTruncate(field, timeval);
68  }
69 
70  protected:
71  static inline std::shared_ptr<Analyzer::Constant> getNumericConstant(
72  const int64_t scale) {
73  Datum d{0};
74  d.bigintval = scale;
75  return makeExpr<Analyzer::Constant>(SQLTypeInfo(kBIGINT, false), false, d);
76  }
77 };
78 
79 class ExtractExpr : protected DateTimeTranslator {
80  public:
81  ExtractExpr(const std::shared_ptr<Analyzer::Expr> expr, const ExtractField& field)
82  : from_expr_(expr), field_(field) {}
83  ExtractExpr(const std::shared_ptr<Analyzer::Expr> expr, const std::string& field)
84  : from_expr_(expr), field_(to_extract_field(field)) {}
85 
86  static std::shared_ptr<Analyzer::Expr> generate(const std::shared_ptr<Analyzer::Expr>,
87  const std::string&);
88  static std::shared_ptr<Analyzer::Expr> generate(const std::shared_ptr<Analyzer::Expr>,
89  const ExtractField&);
90 
91  const std::shared_ptr<Analyzer::Expr> generate() const {
92  return generate(from_expr_, field_);
93  }
94 
95  private:
96  static ExtractField to_extract_field(const std::string& field);
97 
98  std::shared_ptr<Analyzer::Expr> from_expr_;
100 };
101 
103  public:
104  DateTruncExpr(const std::shared_ptr<Analyzer::Expr> expr, const DatetruncField& field)
105  : from_expr_(expr), field_(field) {}
106  DateTruncExpr(const std::shared_ptr<Analyzer::Expr> expr, const std::string& field)
107  : from_expr_(expr), field_(to_datetrunc_field(field)) {}
108 
109  static std::shared_ptr<Analyzer::Expr> generate(const std::shared_ptr<Analyzer::Expr>,
110  const std::string&);
111  static std::shared_ptr<Analyzer::Expr> generate(const std::shared_ptr<Analyzer::Expr>,
112  const DatetruncField&);
113 
114  const std::shared_ptr<Analyzer::Expr> generate() const {
115  return generate(from_expr_, field_);
116  }
117 
118  private:
119  static DatetruncField to_datetrunc_field(const std::string& field);
120 
121  std::shared_ptr<Analyzer::Expr> from_expr_;
123 };
Defines data structures for the semantic analysis phase of query processing.
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:81
const std::shared_ptr< Analyzer::Expr > generate() const
bool is_high_precision_timestamp() const
Definition: sqltypes.h:632
const std::pair< SQLOps, int64_t > get_extract_high_precision_adjusted_scale(const ExtractField &field, const int32_t dimen)
def generate(mask, cond_mask, upper_bounds)
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)
int64_t bigintval
Definition: sqltypes.h:136
static std::shared_ptr< Analyzer::Constant > getNumericConstant(const int64_t scale)
std::shared_ptr< Analyzer::Expr > from_expr_
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:259
ExtractExpr(const std::shared_ptr< Analyzer::Expr > expr, const std::string &field)
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
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:95
const std::shared_ptr< Analyzer::Expr > generate() const
const int64_t get_datetrunc_high_precision_scale(const DatetruncField &field, const int32_t dimen)