OmniSciDB  72c90bc290
 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 
20 
21 namespace spatial_type {
22 
23 class NPoints : public Codegen {
24  public:
25  NPoints(const Analyzer::GeoOperator* geo_operator) : Codegen(geo_operator) {}
26 
27  size_t size() const final { return 1; }
28 
29  SQLTypeInfo getNullType() const final { return SQLTypeInfo(kINT); }
30 
31  const Analyzer::Expr* getOperand(const size_t index) final {
32  CHECK_EQ(index, size_t(0));
33  if (operand_owned_) {
34  return operand_owned_.get();
35  }
36 
37  const auto operand = operator_->getOperand(0);
38  auto col_var = dynamic_cast<const Analyzer::ColumnVar*>(operand);
39  CHECK(col_var);
40 
41  geo_ti_ = col_var->get_type_info();
44 
45  // create a new operand which is just the coords and codegen it
46  auto column_key = col_var->getColumnKey();
47  column_key.column_id = column_key.column_id + 1; // + 1 for coords
48  const auto coords_cd = get_column_descriptor(column_key);
49  CHECK(coords_cd);
50 
51  operand_owned_ = std::make_unique<Analyzer::ColumnVar>(
52  coords_cd->columnType, column_key, col_var->get_rte_idx());
53  return operand_owned_.get();
54  }
55 
56  std::tuple<std::vector<llvm::Value*>, llvm::Value*> codegenLoads(
57  const std::vector<llvm::Value*>& arg_lvs,
58  const std::vector<llvm::Value*>& pos_lvs,
59  CgenState* cgen_state) final {
60  CHECK_EQ(arg_lvs.size(), size_t(1));
61  auto& argument_lv = arg_lvs.front();
62  std::string fn_name("array_size");
63 
64  const auto& elem_ti = getOperand(0)->get_type_info().get_elem_type();
65  std::vector<llvm::Value*> array_size_args{
66  argument_lv,
67  pos_lvs.front(),
68  cgen_state->llInt(log2_bytes(elem_ti.get_logical_size()))};
69 
70  const bool is_nullable = isNullable();
71 
72  if (is_nullable) {
73  fn_name += "_nullable";
74  array_size_args.push_back(cgen_state->inlineIntNull(getTypeInfo()));
75  }
76  const auto coords_arr_sz_lv = cgen_state->emitExternalCall(
77  fn_name, get_int_type(32, cgen_state->context_), array_size_args);
78  return std::make_tuple(std::vector<llvm::Value*>{coords_arr_sz_lv}, coords_arr_sz_lv);
79  }
80 
81  std::vector<llvm::Value*> codegen(const std::vector<llvm::Value*>& args,
82  CodeGenerator::NullCheckCodegen* nullcheck_codegen,
83  CgenState* cgen_state,
84  const CompilationOptions& co) final {
85  CHECK_EQ(args.size(), size_t(1));
86 
87  // divide the coord size by the constant compression value and return it
88  auto& builder = cgen_state->ir_builder_;
89  llvm::Value* conversion_constant{nullptr};
91  conversion_constant = cgen_state->llInt(4);
92  } else {
93  conversion_constant = cgen_state->llInt(8);
94  }
95  CHECK(conversion_constant);
96  const auto total_num_pts = builder.CreateUDiv(args.front(), conversion_constant);
97  const auto ret = builder.CreateUDiv(total_num_pts, cgen_state->llInt(2));
98  if (isNullable()) {
99  CHECK(nullcheck_codegen);
100  return {nullcheck_codegen->finalize(cgen_state->inlineIntNull(getTypeInfo()), ret)};
101  } else {
102  return {ret};
103  }
104  }
105 
106  protected:
108  std::unique_ptr<Analyzer::ColumnVar> operand_owned_;
109 };
110 
111 } // namespace spatial_type
std::unique_ptr< Analyzer::ColumnVar > operand_owned_
Definition: NPoints.h:108
#define CHECK_EQ(x, y)
Definition: Logger.h:301
size_t size() const final
Definition: NPoints.h:27
SQLTypeInfo getNullType() const final
Definition: NPoints.h:29
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
auto getTypeInfo() const
Definition: Codegen.h:34
const Analyzer::Expr * getOperand(const size_t index) final
Definition: NPoints.h:31
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::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:56
NPoints(const Analyzer::GeoOperator *geo_operator)
Definition: NPoints.h:25
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:399
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:81
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:67
SQLTypeInfo geo_ti_
Definition: NPoints.h:107
#define CHECK(condition)
Definition: Logger.h:291
bool is_geometry() const
Definition: sqltypes.h:595
Analyzer::Expr * getOperand(const size_t index) const
Definition: Analyzer.cpp:4186
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:975