OmniSciDB  eb3a3d0a03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PointAccessors.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 // ST_X and ST_Y
24 class PointAccessors : public Codegen {
25  public:
27  const Catalog_Namespace::Catalog* catalog)
28  : Codegen(geo_operator, catalog) {
29  CHECK_EQ(operator_->size(), size_t(1));
30  }
31 
32  size_t size() const final { return 1; }
33 
35 
36  // returns arguments lvs and null lv
37  std::tuple<std::vector<llvm::Value*>, llvm::Value*> codegenLoads(
38  const std::vector<llvm::Value*>& arg_lvs,
39  const std::vector<llvm::Value*>& pos_lvs,
40  CgenState* cgen_state) final {
41  CHECK_EQ(pos_lvs.size(), size());
42  auto operand = getOperand(0);
43  CHECK(operand);
44  const auto& geo_ti = operand->get_type_info();
45  CHECK(geo_ti.is_geometry());
46  auto& builder = cgen_state->ir_builder_;
47 
48  llvm::Value* array_buff_ptr{nullptr};
49  llvm::Value* is_null{nullptr};
50  if (arg_lvs.size() == 1) {
51  if (dynamic_cast<const Analyzer::GeoExpr*>(operand)) {
52  const auto ptr_type =
53  llvm::dyn_cast<llvm::PointerType>(arg_lvs.front()->getType());
54  CHECK(ptr_type);
55  const auto is_null_lv =
56  builder.CreateICmp(llvm::CmpInst::ICMP_EQ,
57  arg_lvs.front(),
58  llvm::ConstantPointerNull::get(ptr_type));
59  return std::make_tuple(arg_lvs, is_null_lv);
60  }
61  // col byte stream, get the array buffer ptr and is null attributes and cache
63  arg_lvs.front(), pos_lvs.front(), geo_ti, cgen_state);
64  array_buff_ptr = arr_load_lvs.buffer;
65  is_null = arr_load_lvs.is_null;
66  } else {
67  // ptr and size
68  CHECK_EQ(arg_lvs.size(), size_t(2));
69  if (dynamic_cast<const Analyzer::GeoOperator*>(operand)) {
70  // null check will be if the ptr is a nullptr
71  is_null = builder.CreateICmp(
72  llvm::CmpInst::ICMP_EQ,
73  arg_lvs.front(),
74  llvm::ConstantPointerNull::get( // TODO: check ptr address space
75  geo_ti.get_compression() == kENCODING_GEOINT
76  ? llvm::Type::getInt32PtrTy(cgen_state->context_)
77  : llvm::Type::getDoublePtrTy(cgen_state->context_)));
78  }
79 
80  // TODO: nulls from other types not yet supported
81  array_buff_ptr = arg_lvs.front();
82  }
83  CHECK(array_buff_ptr) << operator_->toString();
84  if (!is_null) {
85  is_nullable_ = false;
86  }
87  return std::make_tuple(std::vector<llvm::Value*>{array_buff_ptr}, is_null);
88  }
89 
90  std::vector<llvm::Value*> codegen(const std::vector<llvm::Value*>& args,
91  CodeGenerator::NullCheckCodegen* nullcheck_codegen,
92  CgenState* cgen_state,
93  const CompilationOptions& co) final {
94  CHECK_EQ(args.size(), size_t(1));
95  const auto array_buff_ptr = args.front();
96 
97  const auto& geo_ti = getOperand(0)->get_type_info();
98  CHECK(geo_ti.is_geometry());
99  auto& builder = cgen_state->ir_builder_;
100 
101  const bool is_x = operator_->getName() == "ST_X";
102  const std::string expr_name = is_x ? "x" : "y";
103 
104  llvm::Value* coord_lv;
105  if (geo_ti.get_compression() == kENCODING_GEOINT) {
106  auto compressed_arr_ptr = builder.CreateBitCast(
107  array_buff_ptr, llvm::Type::getInt32PtrTy(cgen_state->context_));
108  auto coord_index = is_x ? cgen_state->llInt(0) : cgen_state->llInt(1);
109  auto coord_lv_ptr =
110  builder.CreateGEP(compressed_arr_ptr, coord_index, expr_name + "_coord_ptr");
111  auto compressed_coord_lv =
112  builder.CreateLoad(coord_lv_ptr, expr_name + "_coord_compressed");
113 
114  coord_lv =
115  cgen_state->emitExternalCall("decompress_" + expr_name + "_coord_geoint",
116  llvm::Type::getDoubleTy(cgen_state->context_),
117  {compressed_coord_lv});
118  } else {
119  auto coord_arr_ptr = builder.CreateBitCast(
120  array_buff_ptr, llvm::Type::getDoublePtrTy(cgen_state->context_));
121  auto coord_index = is_x ? cgen_state->llInt(0) : cgen_state->llInt(1);
122  auto coord_lv_ptr =
123  builder.CreateGEP(coord_arr_ptr, coord_index, expr_name + "_coord_ptr");
124  coord_lv = builder.CreateLoad(coord_lv_ptr, expr_name + "_coord");
125  }
126 
127  auto ret = coord_lv;
128  if (is_nullable_) {
129  CHECK(nullcheck_codegen);
130  ret = nullcheck_codegen->finalize(cgen_state->inlineFpNull(SQLTypeInfo(kDOUBLE)),
131  ret);
132  }
133  const auto key = operator_->toString();
134  CHECK(cgen_state->geo_target_cache_.insert(std::make_pair(key, ret)).second);
135  return {ret};
136  }
137 };
138 
139 } // 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:102
size_t size() const final
#define const
llvm::Value * buffer
Definition: CgenState.h:32
CONSTEXPR DEVICE bool is_null(const T &value)
const std::string & getName() const
Definition: Analyzer.h:1912
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
SQLTypeInfo getNullType() const final
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:78
static ArrayLoadCodegen codegenGeoArrayLoadAndNullcheck(llvm::Value *byte_stream, llvm::Value *pos, const SQLTypeInfo &ti, CgenState *cgen_state)
Definition: GeoIR.cpp:23
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:70
size_t size() const
Definition: Analyzer.cpp:3629
std::vector< llvm::Value * > codegen(const std::vector< llvm::Value * > &args, CodeGenerator::NullCheckCodegen *nullcheck_codegen, CgenState *cgen_state, const CompilationOptions &co) final
#define CHECK(condition)
Definition: Logger.h:209
virtual const Analyzer::Expr * getOperand(const size_t index)
Definition: Codegen.cpp:63
PointAccessors(const Analyzer::GeoOperator *geo_operator, const Catalog_Namespace::Catalog *catalog)
std::string toString() const override
Definition: Analyzer.cpp:3601