OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StartEndPoint.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 StartEndPoint : public Codegen {
24  public:
25  StartEndPoint(const Analyzer::GeoOperator* geo_operator,
26  const Catalog_Namespace::Catalog* catalog)
27  : Codegen(geo_operator, catalog) {
28  CHECK_EQ(operator_->size(), size_t(1));
29  // nulls not supported yet
30  this->is_nullable_ = false;
31  }
32 
33  size_t size() const final { return 1; }
34 
36 
37  // returns arguments lvs and null lv
38  std::tuple<std::vector<llvm::Value*>, llvm::Value*> codegenLoads(
39  const std::vector<llvm::Value*>& arg_lvs,
40  const std::vector<llvm::Value*>& pos_lvs,
41  CgenState* cgen_state) final {
42  CHECK_EQ(pos_lvs.size(), size());
43  // TODO: add null handling
44  if (arg_lvs.size() == size_t(1)) {
45  // col byte stream from column on disk
46  auto operand = getOperand(0);
47  CHECK(operand);
48  const auto& geo_ti = operand->get_type_info();
49  CHECK(geo_ti.get_type() == kLINESTRING);
50 
51  std::vector<llvm::Value*> array_operand_lvs;
52  array_operand_lvs.push_back(
53  cgen_state->emitExternalCall("array_buff",
54  llvm::Type::getInt8PtrTy(cgen_state->context_),
55  {arg_lvs.front(), pos_lvs.front()}));
56  const bool is_nullable = !geo_ti.get_notnull();
57  std::string size_fn_name = "array_size";
58  if (is_nullable) {
59  size_fn_name += "_nullable";
60  }
61  uint32_t elem_sz = 1; // TINYINT coords array
62  std::vector<llvm::Value*> array_sz_args{
63  arg_lvs.front(), pos_lvs.front(), cgen_state->llInt(log2_bytes(elem_sz))};
64  if (is_nullable) {
65  array_sz_args.push_back(
66  cgen_state->llInt(static_cast<int32_t>(inline_int_null_value<int32_t>())));
67  }
68  array_operand_lvs.push_back(cgen_state->emitExternalCall(
69  size_fn_name, get_int_type(32, cgen_state->context_), array_sz_args));
70  return std::make_tuple(array_operand_lvs, nullptr);
71  }
72  CHECK_EQ(arg_lvs.size(), size_t(2)); // ptr, size
73  return std::make_tuple(arg_lvs, nullptr);
74  }
75 
76  std::vector<llvm::Value*> codegen(const std::vector<llvm::Value*>& args,
77  CodeGenerator::NullCheckCodegen* nullcheck_codegen,
78  CgenState* cgen_state,
79  const CompilationOptions& co) final {
80  CHECK_EQ(args.size(), size_t(2)); // ptr, size
81  const auto& geo_ti = getOperand(0)->get_type_info();
82  CHECK(geo_ti.is_geometry());
83 
84  auto& builder = cgen_state->ir_builder_;
85  llvm::Value* array_buff_cast{nullptr};
86  int32_t elem_size_bytes = 0;
87  if (geo_ti.get_compression() == kENCODING_GEOINT) {
88  array_buff_cast = builder.CreateBitCast(
89  args.front(), llvm::Type::getInt32PtrTy(cgen_state->context_));
90  elem_size_bytes = 4; // 4-byte ints
91  } else {
92  array_buff_cast = builder.CreateBitCast(
93  args.front(), llvm::Type::getDoublePtrTy(cgen_state->context_));
94  elem_size_bytes = 8; // doubles
95  }
96  CHECK_GT(elem_size_bytes, 0);
97  const bool is_end_point = getName() == "ST_EndPoint";
98  const auto num_elements_lv =
99  builder.CreateSDiv(args.back(), cgen_state->llInt(elem_size_bytes));
100  const auto index_lv =
101  is_end_point ? builder.CreateSub(num_elements_lv, cgen_state->llInt(int32_t(2)))
102  : cgen_state->llInt(int32_t(0));
103  auto array_offset_lv =
104  builder.CreateGEP(array_buff_cast, index_lv, operator_->getName() + "_Offset");
105  return {array_offset_lv, args.back()};
106  }
107 };
108 
109 } // 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)
#define CHECK_GT(x, y)
Definition: Logger.h:221
std::vector< llvm::Value * > codegen(const std::vector< llvm::Value * > &args, CodeGenerator::NullCheckCodegen *nullcheck_codegen, CgenState *cgen_state, const CompilationOptions &co) final
Definition: StartEndPoint.h:76
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: StartEndPoint.h:38
size_t size() const final
Definition: StartEndPoint.h:33
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:70
size_t size() const
Definition: Analyzer.cpp:3640
#define CHECK(condition)
Definition: Logger.h:209
virtual const Analyzer::Expr * getOperand(const size_t index)
Definition: Codegen.cpp:63
uint32_t log2_bytes(const uint32_t bytes)
Definition: Execute.h:174
StartEndPoint(const Analyzer::GeoOperator *geo_operator, const Catalog_Namespace::Catalog *catalog)
Definition: StartEndPoint.h:25
SQLTypeInfo getNullType() const final
Definition: StartEndPoint.h:35