OmniSciDB  a5dc49c757
 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 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 #pragma once
17 
19 
20 #include "Geospatial/Compression.h"
21 
22 namespace spatial_type {
23 
24 class AreaPerimeter : public Codegen {
25  public:
26  AreaPerimeter(const Analyzer::GeoOperator* geo_operator) : Codegen(geo_operator) {
27  CHECK_EQ(operator_->size(), size_t(1));
28  const auto& ti = operator_->get_type_info();
29  is_nullable_ = !ti.get_notnull();
30  }
31 
32  size_t size() const final { return 1; }
33 
34  SQLTypeInfo getNullType() const final { return SQLTypeInfo(kINT); }
35 
36  std::tuple<std::vector<llvm::Value*>, llvm::Value*> codegenLoads(
37  const std::vector<llvm::Value*>& arg_lvs,
38  const std::vector<llvm::Value*>& pos_lvs,
39  CgenState* cgen_state) final {
40  CHECK_EQ(pos_lvs.size(), size());
41  const auto operand = getOperand(0);
42  CHECK(operand);
43  const auto& operand_ti = operand->get_type_info();
44 
45  std::string size_fn_name = "array_size";
46  if (is_nullable_) {
47  size_fn_name += "_nullable";
48  }
49 
50  std::vector<llvm::Value*> operand_lvs;
51  // iterate over column inputs
52  if (dynamic_cast<const Analyzer::ColumnVar*>(operand)) {
53  for (size_t i = 0; i < arg_lvs.size(); i++) {
54  auto lv = arg_lvs[i];
55  operand_lvs.push_back(
56  cgen_state->emitExternalCall("array_buff",
57  llvm::Type::getInt8PtrTy(cgen_state->context_),
58  {lv, pos_lvs.front()}));
59  const auto ptr_type = llvm::dyn_cast_or_null<llvm::PointerType>(lv->getType());
60  CHECK(ptr_type);
61  const auto elem_type = ptr_type->getPointerElementType();
62  CHECK(elem_type);
63  auto const is_coords = (i == 0);
64  auto const shift = log2_bytes(is_coords ? 1 : 4);
65  std::vector<llvm::Value*> array_sz_args{
66  lv, pos_lvs.front(), cgen_state->llInt(shift)};
67  if (is_nullable_) { // TODO: should we do this for all arguments, or just points?
68  array_sz_args.push_back(
69  cgen_state->llInt(static_cast<int32_t>(inline_int_null_value<int32_t>())));
70  }
71  operand_lvs.push_back(cgen_state->emitExternalCall(
72  size_fn_name, get_int_type(32, cgen_state->context_), array_sz_args));
73  }
74  } else {
75  operand_lvs = arg_lvs;
76  }
77  CHECK_EQ(operand_lvs.size(),
78  size_t(2 * operand_ti.get_physical_coord_cols())); // array ptr and size
79 
80  // use the points array size argument for nullability
81  return std::make_tuple(operand_lvs, is_nullable_ ? operand_lvs[1] : nullptr);
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  auto operand_lvs = args;
89 
90  const auto& operand_ti = getOperand(0)->get_type_info();
91  CHECK(operand_ti.get_type() == kPOLYGON || operand_ti.get_type() == kMULTIPOLYGON);
92  const bool is_geodesic =
93  operand_ti.get_subtype() == kGEOGRAPHY && operand_ti.get_output_srid() == 4326;
94 
95  std::string func_name = getName() + suffix(operand_ti.get_type());
96  if (is_geodesic && getName() == "ST_Perimeter") {
97  func_name += "_Geodesic";
98  }
99 
100  // push back ic, isr, osr for now
101  operand_lvs.push_back(
102  cgen_state->llInt(Geospatial::get_compression_scheme(operand_ti))); // ic
103  operand_lvs.push_back(cgen_state->llInt(operand_ti.get_input_srid())); // in srid
104  auto output_srid = operand_ti.get_output_srid();
105  if (const auto srid_override = operator_->getOutputSridOverride()) {
106  output_srid = *srid_override;
107  }
108  operand_lvs.push_back(cgen_state->llInt(output_srid)); // out srid
109 
110  const auto& ret_ti = operator_->get_type_info();
111  CHECK(ret_ti.get_type() == kDOUBLE || ret_ti.get_type() == kFLOAT);
112 
113  auto ret = cgen_state->emitExternalCall(
114  func_name,
115  ret_ti.get_type() == kDOUBLE ? llvm::Type::getDoubleTy(cgen_state->context_)
116  : llvm::Type::getFloatTy(cgen_state->context_),
117  operand_lvs);
118  if (is_nullable_) {
119  CHECK(nullcheck_codegen);
120  ret = nullcheck_codegen->finalize(cgen_state->inlineFpNull(ret_ti), ret);
121  }
122  return {ret};
123  }
124 };
125 
126 } // namespace spatial_type
#define CHECK_EQ(x, y)
Definition: Logger.h:301
AreaPerimeter(const Analyzer::GeoOperator *geo_operator)
Definition: AreaPerimeter.h:26
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:75
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:36
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:84
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
size_t size() const final
Definition: AreaPerimeter.h:32
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:69
size_t size() const
Definition: Analyzer.cpp:4211
#define CHECK(condition)
Definition: Logger.h:291
virtual const Analyzer::Expr * getOperand(const size_t index)
Definition: Codegen.cpp:64
uint32_t log2_bytes(const uint32_t bytes)
Definition: Execute.h:198
Definition: sqltypes.h:72
SQLTypeInfo getNullType() const final
Definition: AreaPerimeter.h:34