OmniSciDB  ba1bac9284
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NPoints.h
Go to the documentation of this file.
1 /*
2  * Copyright 2021 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 
20 
21 namespace spatial_type {
22 
23 class NPoints : public Codegen {
24  public:
25  NPoints(const Analyzer::GeoOperator* geo_operator,
26  const Catalog_Namespace::Catalog* catalog)
27  : Codegen(geo_operator, catalog) {}
28 
29  size_t size() const final { return 1; }
30 
32 
34  // we expect this to only be called after codegening the loads
36  return operand_owned_.get();
37  }
38 
39  const Analyzer::Expr* getOperand(const size_t index) final {
40  CHECK_EQ(index, size_t(0));
41  if (operand_owned_) {
42  return operand_owned_.get();
43  }
44 
45  const auto operand = operator_->getOperand(0);
46  auto col_var = dynamic_cast<const Analyzer::ColumnVar*>(operand);
47  CHECK(col_var);
48 
49  geo_ti_ = col_var->get_type_info();
52 
53  // create a new operand which is just the coords and codegen it
54  const auto coords_column_id = col_var->get_column_id() + 1; // + 1 for coords
55  auto coords_cd =
56  get_column_descriptor(coords_column_id, col_var->get_table_id(), *cat_);
57  CHECK(coords_cd);
58 
59  operand_owned_ = std::make_unique<Analyzer::ColumnVar>(coords_cd->columnType,
60  col_var->get_table_id(),
61  coords_column_id,
62  col_var->get_rte_idx());
63  return operand_owned_.get();
64  }
65 
66  std::tuple<std::vector<llvm::Value*>, llvm::Value*> codegenLoads(
67  const std::vector<llvm::Value*>& arg_lvs,
68  llvm::Value* pos_lv,
69  CgenState* cgen_state) final {
70  CHECK_EQ(arg_lvs.size(), size_t(1));
71  auto& argument_lv = arg_lvs.front();
72  std::string fn_name("array_size");
73 
74  const auto& elem_ti = getOperand(0)->get_type_info().get_elem_type();
75  std::vector<llvm::Value*> array_size_args{
76  argument_lv, pos_lv, cgen_state->llInt(log2_bytes(elem_ti.get_logical_size()))};
77 
78  const bool is_nullable = isNullable();
79 
80  if (is_nullable) {
81  fn_name += "_nullable";
82  array_size_args.push_back(cgen_state->inlineIntNull(getTypeInfo()));
83  }
84  const auto coords_arr_sz_lv = cgen_state->emitExternalCall(
85  fn_name, get_int_type(32, cgen_state->context_), array_size_args);
86  return std::make_tuple(std::vector<llvm::Value*>{coords_arr_sz_lv}, coords_arr_sz_lv);
87  }
88 
89  std::vector<llvm::Value*> codegen(const std::vector<llvm::Value*>& args,
90  CodeGenerator::NullCheckCodegen* nullcheck_codegen,
91  CgenState* cgen_state) final {
92  CHECK_EQ(args.size(), size_t(1));
93 
94  // divide the coord size by the constant compression value and return it
95  auto& builder = cgen_state->ir_builder_;
96  llvm::Value* conversion_constant{nullptr};
98  conversion_constant = cgen_state->llInt(4);
99  } else {
100  conversion_constant = cgen_state->llInt(8);
101  }
102  CHECK(conversion_constant);
103  const auto total_num_pts = builder.CreateUDiv(args.front(), conversion_constant);
104  const auto ret = builder.CreateUDiv(total_num_pts, cgen_state->llInt(2));
105  if (isNullable()) {
106  CHECK(nullcheck_codegen);
107  return {nullcheck_codegen->finalize(cgen_state->inlineIntNull(getTypeInfo()), ret)};
108  } else {
109  return {ret};
110  }
111  }
112 
113  protected:
115  std::unique_ptr<Analyzer::ColumnVar> operand_owned_;
116 };
117 
118 } // namespace spatial_type
std::unique_ptr< Analyzer::ColumnVar > operand_owned_
Definition: NPoints.h:115
#define CHECK_EQ(x, y)
Definition: Logger.h:214
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:102
#define const
size_t size() const final
Definition: NPoints.h:29
SQLTypeInfo getNullType() const final
Definition: NPoints.h:31
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
auto getTypeInfo() const
Definition: Codegen.h:36
const Analyzer::Expr * getOperand(const size_t index) final
Definition: NPoints.h:39
NPoints(const Analyzer::GeoOperator *geo_operator, const Catalog_Namespace::Catalog *catalog)
Definition: NPoints.h:25
std::tuple< std::vector< llvm::Value * >, llvm::Value * > codegenLoads(const std::vector< llvm::Value * > &arg_lvs, llvm::Value *pos_lv, CgenState *cgen_state) final
Definition: NPoints.h:66
const Analyzer::Expr * getPositionOperand() const final
Definition: NPoints.h:33
const Catalog_Namespace::Catalog * cat_
Definition: Codegen.h:65
auto isNullable() const
Definition: Codegen.h:34
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:78
std::vector< llvm::Value * > codegen(const std::vector< llvm::Value * > &args, CodeGenerator::NullCheckCodegen *nullcheck_codegen, CgenState *cgen_state) final
Definition: NPoints.h:89
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:322
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:64
SQLTypeInfo geo_ti_
Definition: NPoints.h:114
#define CHECK(condition)
Definition: Logger.h:206
bool is_geometry() const
Definition: sqltypes.h:501
Analyzer::Expr * getOperand(const size_t index) const
Definition: Analyzer.cpp:3449
uint32_t log2_bytes(const uint32_t bytes)
Definition: Execute.h:177
Definition: sqltypes.h:44
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:321
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:713
const ColumnDescriptor * get_column_descriptor(const int col_id, const int table_id, const Catalog_Namespace::Catalog &cat)
Definition: Execute.h:192