OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NPoints.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 <regex>
21 
22 namespace spatial_type {
23 
24 class NPoints : public Codegen {
25  public:
26  NPoints(const Analyzer::GeoOperator* geo_operator) : Codegen(geo_operator) {}
27 
28  size_t size() const final { return 1; }
29 
30  SQLTypeInfo getNullType() const final { return SQLTypeInfo(kINT); }
31 
32  const Analyzer::Expr* getOperand(const size_t index) final {
33  CHECK_EQ(index, size_t(0));
34  if (col_var_owned_) {
35  return col_var_owned_.get();
36  }
37  const auto operand = operator_->getOperand(0);
38  geo_ti_ = operand->get_type_info();
41  if (auto col_var = dynamic_cast<const Analyzer::ColumnVar*>(operand)) {
42  // create a new operand which is just the coords and codegen it
43  auto column_key = col_var->getColumnKey();
44  column_key.column_id = column_key.column_id + 1; // + 1 for coords
45  const auto coords_cd = get_column_descriptor(column_key);
46  CHECK(coords_cd);
47  col_var_owned_ = std::make_unique<Analyzer::ColumnVar>(
48  coords_cd->columnType, column_key, col_var->get_rte_idx());
49  return col_var_owned_.get();
50  }
51  return operand;
52  }
53 
54  std::tuple<std::vector<llvm::Value*>, llvm::Value*> codegenLoads(
55  const std::vector<llvm::Value*>& arg_lvs,
56  const std::vector<llvm::Value*>& pos_lvs,
57  CgenState* cgen_state) final {
58  llvm::Value* coords_arr_sz_lv{nullptr};
59  if (auto geo_constant = dynamic_cast<const Analyzer::GeoConstant*>(getOperand(0))) {
60  // count points defined in the WKTString, i.e., POLYGON(1 1, 2 2, 3 3, 1 1)
61  // the validation of the WKTString must be checked before entering this logic
62  std::regex regex("-?[0-9]*\\.?[0-9]+\\s+-?[0-9]*\\.?[0-9]+");
63  auto target = geo_constant->getWKTString();
64  auto pt_begin = std::sregex_iterator(target.begin(), target.end(), regex);
65  auto pt_end = std::sregex_iterator();
66  auto num_pts = std::distance(pt_begin, pt_end);
67  CHECK_GT(num_pts, 0);
68  coords_arr_sz_lv = cgen_state->llInt(16 * num_pts);
69  } else if (arg_lvs.size() == size_t(1)) {
70  std::string fn_name("array_size");
71  bool is_nullable = isNullable();
72  if (auto col_var = dynamic_cast<const Analyzer::ColumnVar*>(getOperand(0))) {
73  auto column_key = col_var->getColumnKey();
74  column_key.column_id = column_key.column_id - 1;
75  const auto type_cd = get_column_descriptor(column_key);
76  CHECK(type_cd);
77  if (type_cd->columnType.get_type() == kPOINT) {
78  fn_name = "point_coord_array_size";
79  }
80  }
81  auto& argument_lv = arg_lvs.front();
82  const auto& elem_ti = getOperand(0)->get_type_info().get_elem_type();
83  std::vector<llvm::Value*> array_size_args{
84  argument_lv,
85  pos_lvs.front(),
86  cgen_state->llInt(log2_bytes(elem_ti.get_logical_size()))};
87  if (is_nullable) {
88  fn_name += "_nullable";
89  array_size_args.push_back(cgen_state->inlineIntNull(getTypeInfo()));
90  }
91  coords_arr_sz_lv = cgen_state->emitExternalCall(
92  fn_name, get_int_type(32, cgen_state->context_), array_size_args);
93  } else if (arg_lvs.size() == size_t(2)) {
94  auto child_geo_oper =
95  dynamic_cast<const Analyzer::GeoOperator*>(operator_->getOperand(0));
96  CHECK(child_geo_oper);
97  if (child_geo_oper->getName() == "ST_Point") {
98  coords_arr_sz_lv = cgen_state->ir_builder_.CreateSelect(
99  cgen_state->emitCall("point_double_is_null", {arg_lvs.front()}),
100  cgen_state->inlineIntNull(getTypeInfo()),
101  cgen_state->llInt(static_cast<int32_t>(16)));
102  } else {
103  CHECK(false) << "Not supported geo operator w/ ST_NPoints: "
104  << child_geo_oper->getName();
105  }
106  }
107  CHECK(coords_arr_sz_lv);
108  return std::make_tuple(std::vector<llvm::Value*>{coords_arr_sz_lv}, coords_arr_sz_lv);
109  }
110 
111  std::vector<llvm::Value*> codegen(const std::vector<llvm::Value*>& args,
112  CodeGenerator::NullCheckCodegen* nullcheck_codegen,
113  CgenState* cgen_state,
114  const CompilationOptions& co) final {
115  CHECK_EQ(args.size(), size_t(1));
116 
117  // divide the coord size by the constant compression value and return it
118  auto& builder = cgen_state->ir_builder_;
119  llvm::Value* conversion_constant{nullptr};
120  if (geo_ti_.get_compression() == kENCODING_GEOINT) {
121  conversion_constant = cgen_state->llInt(4);
122  } else {
123  conversion_constant = cgen_state->llInt(8);
124  }
125  CHECK(conversion_constant);
126  const auto total_num_pts = builder.CreateUDiv(args.front(), conversion_constant);
127  const auto ret = builder.CreateUDiv(total_num_pts, cgen_state->llInt(2));
128  if (isNullable()) {
129  CHECK(nullcheck_codegen);
130  return {nullcheck_codegen->finalize(cgen_state->inlineIntNull(getTypeInfo()), ret)};
131  } else {
132  return {ret};
133  }
134  }
135 
136  protected:
138  std::unique_ptr<Analyzer::ColumnVar> col_var_owned_;
139 };
140 
141 } // namespace spatial_type
#define CHECK_EQ(x, y)
Definition: Logger.h:301
size_t size() const final
Definition: NPoints.h:28
SQLTypeInfo getNullType() const final
Definition: NPoints.h:30
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
#define CHECK_GT(x, y)
Definition: Logger.h:305
auto getTypeInfo() const
Definition: Codegen.h:34
const Analyzer::Expr * getOperand(const size_t index) final
Definition: NPoints.h:32
const ColumnDescriptor * get_column_descriptor(const shared::ColumnKey &column_key)
Definition: Execute.h:213
auto isNullable() const
Definition: Codegen.h:32
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
std::unique_ptr< Analyzer::ColumnVar > col_var_owned_
Definition: NPoints.h:138
std::tuple< std::vector< llvm::Value * >, llvm::Value * > codegenLoads(const std::vector< llvm::Value * > &arg_lvs, const std::vector< llvm::Value * > &pos_lvs, CgenState *cgen_state) final
Definition: NPoints.h:54
NPoints(const Analyzer::GeoOperator *geo_operator)
Definition: NPoints.h:26
std::vector< llvm::Value * > codegen(const std::vector< llvm::Value * > &args, CodeGenerator::NullCheckCodegen *nullcheck_codegen, CgenState *cgen_state, const CompilationOptions &co) final
Definition: NPoints.h:111
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:69
SQLTypeInfo geo_ti_
Definition: NPoints.h:137
#define CHECK(condition)
Definition: Logger.h:291
bool is_geometry() const
Definition: sqltypes.h:597
Analyzer::Expr * getOperand(const size_t index) const
Definition: Analyzer.cpp:4215
uint32_t log2_bytes(const uint32_t bytes)
Definition: Execute.h:198
Definition: sqltypes.h:72
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:398
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:977