OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NRings.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 NRings : public Codegen {
24  public:
25  NRings(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 
33  const Analyzer::Expr* getOperand(const size_t index) final {
34  CHECK_EQ(index, size_t(0));
35  if (operand_owned_) {
36  return operand_owned_.get();
37  }
38 
39  const auto operand = operator_->getOperand(0);
40  auto col_var = dynamic_cast<const Analyzer::ColumnVar*>(operand);
41  CHECK(col_var);
42 
43  const auto& geo_ti = col_var->get_type_info();
44  CHECK(geo_ti.is_geometry());
45  is_nullable_ = !geo_ti.get_notnull();
46 
47  // create a new operand which is just the ring sizes and codegen it
48  const auto ring_sizes_column_id = col_var->get_column_id() + 2; // + 2 for ring sizes
49  auto ring_sizes_cd =
50  get_column_descriptor(ring_sizes_column_id, col_var->get_table_id(), *cat_);
51  CHECK(ring_sizes_cd);
52 
53  operand_owned_ = std::make_unique<Analyzer::ColumnVar>(ring_sizes_cd->columnType,
54  col_var->get_table_id(),
55  ring_sizes_column_id,
56  col_var->get_rte_idx());
57  return operand_owned_.get();
58  }
59 
60  // returns arguments lvs and null lv
61  std::tuple<std::vector<llvm::Value*>, llvm::Value*> codegenLoads(
62  const std::vector<llvm::Value*>& arg_lvs,
63  const std::vector<llvm::Value*>& pos_lvs,
64  CgenState* cgen_state) final {
65  CHECK_EQ(pos_lvs.size(), size());
66  CHECK_EQ(arg_lvs.size(), size_t(1));
67  auto& argument_lv = arg_lvs.front();
68  std::string fn_name("array_size");
69 
70  const auto& elem_ti = getOperand(0)->get_type_info().get_elem_type();
71  std::vector<llvm::Value*> array_size_args{
72  argument_lv,
73  pos_lvs.front(),
74  cgen_state->llInt(log2_bytes(elem_ti.get_logical_size()))};
75 
76  const bool is_nullable = isNullable();
77 
78  if (is_nullable) {
79  fn_name += "_nullable";
80  array_size_args.push_back(cgen_state->inlineIntNull(getTypeInfo()));
81  }
82  const auto total_num_rings_lv = cgen_state->emitExternalCall(
83  fn_name, get_int_type(32, cgen_state->context_), array_size_args);
84  return std::make_tuple(std::vector<llvm::Value*>{total_num_rings_lv},
85  total_num_rings_lv);
86  }
87 
88  std::vector<llvm::Value*> codegen(const std::vector<llvm::Value*>& args,
89  CodeGenerator::NullCheckCodegen* nullcheck_codegen,
90  CgenState* cgen_state,
91  const CompilationOptions& co) final {
92  CHECK_EQ(args.size(), size_t(1));
93  if (isNullable()) {
94  CHECK(nullcheck_codegen);
95  return {nullcheck_codegen->finalize(cgen_state->inlineIntNull(getTypeInfo()),
96  args.front())};
97  }
98  return {args.front()};
99  }
100 
101  protected:
102  std::unique_ptr<Analyzer::ColumnVar> operand_owned_;
103 };
104 
105 } // namespace spatial_type
#define CHECK_EQ(x, y)
Definition: Logger.h:217
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:111
#define const
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
auto getTypeInfo() const
Definition: Codegen.h:37
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: NRings.h:61
NRings(const Analyzer::GeoOperator *geo_operator, const Catalog_Namespace::Catalog *catalog)
Definition: NRings.h:25
size_t size() const final
Definition: NRings.h:29
const Catalog_Namespace::Catalog * cat_
Definition: Codegen.h:71
auto isNullable() const
Definition: Codegen.h:35
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
SQLTypeInfo getNullType() const final
Definition: NRings.h:31
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:70
#define CHECK(condition)
Definition: Logger.h:209
Analyzer::Expr * getOperand(const size_t index) const
Definition: Analyzer.cpp:3644
const Analyzer::Expr * getOperand(const size_t index) final
Definition: NRings.h:33
uint32_t log2_bytes(const uint32_t bytes)
Definition: Execute.h:174
std::vector< llvm::Value * > codegen(const std::vector< llvm::Value * > &args, CodeGenerator::NullCheckCodegen *nullcheck_codegen, CgenState *cgen_state, const CompilationOptions &co) final
Definition: NRings.h:88
Definition: sqltypes.h:45
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:850
const ColumnDescriptor * get_column_descriptor(const int col_id, const int table_id, const Catalog_Namespace::Catalog &cat)
Definition: Execute.h:189
std::unique_ptr< Analyzer::ColumnVar > operand_owned_
Definition: NRings.h:102