OmniSciDB  ba1bac9284
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PointN.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 PointN : public Codegen {
24  public:
25  PointN(const Analyzer::GeoOperator* geo_operator,
26  const Catalog_Namespace::Catalog* catalog)
27  : Codegen(geo_operator, catalog) {
28  // set is nullable to true, because an index outside of the linestring will return
29  // null
30  // note we could probably just set this based on the operator type, as the operator
31  // type needs to match
32  this->is_nullable_ = true;
33  }
34 
35  size_t size() const final { return 2; }
36 
38 
39  const Analyzer::Expr* getPositionOperand() const final { return nullptr; }
40 
41  const Analyzer::Expr* getOperand(const size_t index) final {
42  CHECK_EQ(operator_->size(), size_t(2));
43  CHECK_LT(index, operator_->size());
44 
45  if (index == 0) {
46  const auto arg = operator_->getOperand(index);
47  // first argument should be geo literal
48  const auto geo_arg_expr = dynamic_cast<const Analyzer::GeoExpr*>(arg);
49  CHECK(geo_arg_expr) << arg->toString();
50  return arg;
51  } else {
52  return operator_->getOperand(index);
53  }
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  llvm::Value* pos_lv,
60  CgenState* cgen_state) final {
61  CHECK_EQ(arg_lvs.size(), size_t(3)); // ptr, size, index
62  auto& builder = cgen_state->ir_builder_;
63  const auto index_lv = arg_lvs[2];
64  const auto geo_size_lv = arg_lvs[1];
65  const auto is_null_lv = builder.CreateNot(
66  builder.CreateICmp(llvm::ICmpInst::ICMP_SLT, index_lv, geo_size_lv));
67  return std::make_tuple(arg_lvs, is_null_lv);
68  }
69 
70  std::vector<llvm::Value*> codegen(const std::vector<llvm::Value*>& args,
71  CodeGenerator::NullCheckCodegen* nullcheck_codegen,
72  CgenState* cgen_state) final {
73  CHECK_EQ(args.size(), size_t(3)); // ptr, size, index
74  const auto& geo_ti = getOperand(0)->get_type_info();
75  CHECK(geo_ti.is_geometry());
76 
77  llvm::Value* array_buff_cast{nullptr};
78 
79  auto& builder = cgen_state->ir_builder_;
80  if (geo_ti.get_compression() == kENCODING_GEOINT) {
81  array_buff_cast = builder.CreateBitCast(
82  args.front(), llvm::Type::getInt32PtrTy(cgen_state->context_));
83  } else {
84  array_buff_cast = builder.CreateBitCast(
85  args.front(), llvm::Type::getDoublePtrTy(cgen_state->context_));
86  }
87 
88  const auto index_lv = args[2];
89  auto array_offset_lv =
90  builder.CreateGEP(array_buff_cast, index_lv, operator_->getName() + "_Offset");
91  CHECK(nullcheck_codegen);
92  auto ret_lv = nullcheck_codegen->finalize(
93  llvm::ConstantPointerNull::get(
94  geo_ti.get_compression() == kENCODING_GEOINT
95  ? llvm::Type::getInt32PtrTy(cgen_state->context_)
96  : llvm::Type::getDoublePtrTy(cgen_state->context_)),
97  array_offset_lv);
98  const auto geo_size_lv = args[1];
99  return {ret_lv, geo_size_lv};
100  }
101 };
102 
103 } // namespace spatial_type
#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
const Analyzer::Expr * getOperand(const size_t index) final
Definition: PointN.h:41
const Analyzer::Expr * getPositionOperand() const final
Definition: PointN.h:39
#define const
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: PointN.h:57
SQLTypeInfo getNullType() const final
Definition: PointN.h:37
std::vector< llvm::Value * > codegen(const std::vector< llvm::Value * > &args, CodeGenerator::NullCheckCodegen *nullcheck_codegen, CgenState *cgen_state) final
Definition: PointN.h:70
const std::string & getName() const
Definition: Analyzer.h:1749
PointN(const Analyzer::GeoOperator *geo_operator, const Catalog_Namespace::Catalog *catalog)
Definition: PointN.h:25
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:78
#define CHECK_LT(x, y)
Definition: Logger.h:216
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:64
size_t size() const
Definition: Analyzer.cpp:3445
size_t size() const final
Definition: PointN.h:35
#define CHECK(condition)
Definition: Logger.h:206
Analyzer::Expr * getOperand(const size_t index) const
Definition: Analyzer.cpp:3449