OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AreaPerimeter.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 #include "Geospatial/Compression.h"
22 
23 namespace spatial_type {
24 
25 class AreaPerimeter : public Codegen {
26  public:
27  AreaPerimeter(const Analyzer::GeoOperator* geo_operator,
28  const Catalog_Namespace::Catalog* catalog)
29  : Codegen(geo_operator, catalog) {
30  CHECK_EQ(operator_->size(), size_t(1));
31  const auto& ti = operator_->get_type_info();
32  is_nullable_ = !ti.get_notnull();
33  }
34 
35  size_t size() const final { return 1; }
36 
38 
39  std::tuple<std::vector<llvm::Value*>, llvm::Value*> codegenLoads(
40  const std::vector<llvm::Value*>& arg_lvs,
41  const std::vector<llvm::Value*>& pos_lvs,
42  CgenState* cgen_state) final {
43  CHECK_EQ(pos_lvs.size(), size());
44  const auto operand = getOperand(0);
45  CHECK(operand);
46  const auto& operand_ti = operand->get_type_info();
47 
48  std::string size_fn_name = "array_size";
49  if (is_nullable_) {
50  size_fn_name += "_nullable";
51  }
52 
53  const uint32_t coords_elem_sz_bytes =
54  operand_ti.get_compression() == kENCODING_GEOINT ? 1 : 8;
55 
56  std::vector<llvm::Value*> operand_lvs;
57  // iterate over column inputs
58  if (dynamic_cast<const Analyzer::ColumnVar*>(operand)) {
59  for (size_t i = 0; i < arg_lvs.size(); i++) {
60  auto lv = arg_lvs[i];
61  operand_lvs.push_back(
62  cgen_state->emitExternalCall("array_buff",
63  llvm::Type::getInt8PtrTy(cgen_state->context_),
64  {lv, pos_lvs.front()}));
65  const auto ptr_type = llvm::dyn_cast_or_null<llvm::PointerType>(lv->getType());
66  CHECK(ptr_type);
67  const auto elem_type = ptr_type->getElementType();
68  CHECK(elem_type);
69  std::vector<llvm::Value*> array_sz_args{
70  lv,
71  pos_lvs.front(),
72  cgen_state->llInt(log2_bytes(i == 0 ? coords_elem_sz_bytes : 4))};
73  if (is_nullable_) { // TODO: should we do this for all arguments, or just points?
74  array_sz_args.push_back(
75  cgen_state->llInt(static_cast<int32_t>(inline_int_null_value<int32_t>())));
76  }
77  operand_lvs.push_back(cgen_state->emitExternalCall(
78  size_fn_name, get_int_type(32, cgen_state->context_), array_sz_args));
79  }
80  } else {
81  operand_lvs = arg_lvs;
82  }
83  CHECK_EQ(operand_lvs.size(),
84  size_t(2 * operand_ti.get_physical_coord_cols())); // array ptr and size
85 
86  // use the points array size argument for nullability
87  return std::make_tuple(operand_lvs, is_nullable_ ? operand_lvs[1] : nullptr);
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  auto operand_lvs = args;
95 
96  const auto& operand_ti = getOperand(0)->get_type_info();
97  CHECK(operand_ti.get_type() == kPOLYGON || operand_ti.get_type() == kMULTIPOLYGON);
98  const bool is_geodesic =
99  operand_ti.get_subtype() == kGEOGRAPHY && operand_ti.get_output_srid() == 4326;
100 
101  std::string func_name = getName() + suffix(operand_ti.get_type());
102  if (is_geodesic && getName() == "ST_Perimeter") {
103  func_name += "_Geodesic";
104  }
105 
106  // push back ic, isr, osr for now
107  operand_lvs.push_back(
108  cgen_state->llInt(Geospatial::get_compression_scheme(operand_ti))); // ic
109  operand_lvs.push_back(cgen_state->llInt(operand_ti.get_input_srid())); // in srid
110  operand_lvs.push_back(cgen_state->llInt(operand_ti.get_output_srid())); // out srid
111 
112  const auto& ret_ti = operator_->get_type_info();
113  CHECK(ret_ti.get_type() == kDOUBLE || ret_ti.get_type() == kFLOAT);
114 
115  auto ret = cgen_state->emitExternalCall(
116  func_name,
117  ret_ti.get_type() == kDOUBLE ? llvm::Type::getDoubleTy(cgen_state->context_)
118  : llvm::Type::getFloatTy(cgen_state->context_),
119  operand_lvs);
120  if (is_nullable_) {
121  CHECK(nullcheck_codegen);
122  ret = nullcheck_codegen->finalize(cgen_state->inlineFpNull(ret_ti), ret);
123  }
124  return {ret};
125  }
126 };
127 
128 } // 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
int32_t get_compression_scheme(const SQLTypeInfo &ti)
Definition: Compression.cpp:23
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
std::string suffix(SQLTypes type)
Definition: Codegen.cpp:68
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: AreaPerimeter.h:39
std::vector< llvm::Value * > codegen(const std::vector< llvm::Value * > &args, CodeGenerator::NullCheckCodegen *nullcheck_codegen, CgenState *cgen_state, const CompilationOptions &co) final
Definition: AreaPerimeter.h:90
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
size_t size() const final
Definition: AreaPerimeter.h:35
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
Definition: sqltypes.h:45
SQLTypeInfo getNullType() const final
Definition: AreaPerimeter.h:37
AreaPerimeter(const Analyzer::GeoOperator *geo_operator, const Catalog_Namespace::Catalog *catalog)
Definition: AreaPerimeter.h:27