OmniSciDB  a5dc49c757
 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 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 NRings : public Codegen {
24  public:
25  NRings(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  const auto& geo_ti = col_var->get_type_info();
42  CHECK(geo_ti.is_geometry());
43  is_nullable_ = !geo_ti.get_notnull();
44 
45  // create a new operand which is just the ring sizes and codegen it
46  auto column_key = col_var->getColumnKey();
47  column_key.column_id = column_key.column_id + 2; // + 2 for ring sizes
48  const auto ring_sizes_cd = get_column_descriptor(column_key);
49  CHECK(ring_sizes_cd);
50 
51  operand_owned_ = std::make_unique<Analyzer::ColumnVar>(
52  ring_sizes_cd->columnType, column_key, col_var->get_rte_idx());
53  return operand_owned_.get();
54  }
55 
56  // returns arguments lvs and null lv
57  std::tuple<std::vector<llvm::Value*>, llvm::Value*> codegenLoads(
58  const std::vector<llvm::Value*>& arg_lvs,
59  const std::vector<llvm::Value*>& pos_lvs,
60  CgenState* cgen_state) final {
61  CHECK_EQ(pos_lvs.size(), size());
62  CHECK_EQ(arg_lvs.size(), size_t(1));
63  auto& argument_lv = arg_lvs.front();
64  std::string fn_name("array_size");
65 
66  const auto& elem_ti = getOperand(0)->get_type_info().get_elem_type();
67  std::vector<llvm::Value*> array_size_args{
68  argument_lv,
69  pos_lvs.front(),
70  cgen_state->llInt(log2_bytes(elem_ti.get_logical_size()))};
71 
72  const bool is_nullable = isNullable();
73 
74  if (is_nullable) {
75  fn_name += "_nullable";
76  array_size_args.push_back(cgen_state->inlineIntNull(getTypeInfo()));
77  }
78  const auto total_num_rings_lv = cgen_state->emitExternalCall(
79  fn_name, get_int_type(32, cgen_state->context_), array_size_args);
80  return std::make_tuple(std::vector<llvm::Value*>{total_num_rings_lv},
81  total_num_rings_lv);
82  }
83 
84  std::vector<llvm::Value*> codegen(const std::vector<llvm::Value*>& args,
85  CodeGenerator::NullCheckCodegen* nullcheck_codegen,
86  CgenState* cgen_state,
87  const CompilationOptions& co) final {
88  CHECK_EQ(args.size(), size_t(1));
89  if (isNullable()) {
90  CHECK(nullcheck_codegen);
91  return {nullcheck_codegen->finalize(cgen_state->inlineIntNull(getTypeInfo()),
92  args.front())};
93  }
94  return {args.front()};
95  }
96 
97  protected:
98  std::unique_ptr<Analyzer::ColumnVar> operand_owned_;
99 };
100 
101 } // namespace spatial_type
#define CHECK_EQ(x, y)
Definition: Logger.h:301
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
auto getTypeInfo() const
Definition: Codegen.h:34
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:57
const ColumnDescriptor * get_column_descriptor(const shared::ColumnKey &column_key)
Definition: Execute.h:213
size_t size() const final
Definition: NRings.h:27
auto isNullable() const
Definition: Codegen.h:32
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
SQLTypeInfo getNullType() const final
Definition: NRings.h:29
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:69
#define CHECK(condition)
Definition: Logger.h:291
Analyzer::Expr * getOperand(const size_t index) const
Definition: Analyzer.cpp:4215
const Analyzer::Expr * getOperand(const size_t index) final
Definition: NRings.h:31
uint32_t log2_bytes(const uint32_t bytes)
Definition: Execute.h:198
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:84
Definition: sqltypes.h:72
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:977
NRings(const Analyzer::GeoOperator *geo_operator)
Definition: NRings.h:25
std::unique_ptr< Analyzer::ColumnVar > operand_owned_
Definition: NRings.h:98