OmniSciDB  29e35f4d58
RelAlgTranslatorGeo.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, 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 #include "../Shared/geo_types.h"
18 #include "ExpressionRewrite.h"
19 #include "RelAlgTranslator.h"
20 
21 std::vector<std::shared_ptr<Analyzer::Expr>> RelAlgTranslator::translateGeoColumn(
22  const RexInput* rex_input,
23  SQLTypeInfo& ti,
24  const bool with_bounds,
25  const bool with_render_group,
26  const bool expand_geo_col) const {
27  std::vector<std::shared_ptr<Analyzer::Expr>> args;
28  const auto source = rex_input->getSourceNode();
29  const auto it_rte_idx = input_to_nest_level_.find(source);
30  CHECK(it_rte_idx != input_to_nest_level_.end());
31  const int rte_idx = it_rte_idx->second;
32  const auto& in_metainfo = source->getOutputMetainfo();
33 
34  int32_t table_id{0};
35  int column_id{-1};
36  const auto scan_source = dynamic_cast<const RelScan*>(source);
37  if (scan_source) {
38  // We're at leaf (scan) level and not supposed to have input metadata,
39  // the name and type information come directly from the catalog.
40  CHECK(in_metainfo.empty());
41 
42  const auto td = scan_source->getTableDescriptor();
43  table_id = td->tableId;
44 
45  const auto gcd = cat_.getMetadataForColumnBySpi(table_id, rex_input->getIndex() + 1);
46  CHECK(gcd);
47  ti = gcd->columnType;
48  column_id = gcd->columnId;
49 
50  } else {
51  // Likely backed by a temp table. Read the table ID from the source node and negate it
52  // (see RelAlgTranslator::translateInput)
53  table_id = -source->getId();
54 
55  CHECK(!expand_geo_col);
56 
57  CHECK(!in_metainfo.empty());
58  CHECK_GE(rte_idx, 0);
59  column_id = rex_input->getIndex();
60  CHECK_LT(static_cast<size_t>(column_id), in_metainfo.size());
61  ti = in_metainfo[column_id].get_type_info();
62  }
63  CHECK(IS_GEO(ti.get_type()));
64 
65  // Return geo column reference. The geo column may be expanded if required for extension
66  // function arguments. Otherwise, the geo column reference will be translated into
67  // physical columns as required. Bounds column will be added if present and requested.
68  if (expand_geo_col) {
69  for (auto i = 0; i < ti.get_physical_coord_cols(); i++) {
70  const auto pcd = cat_.getMetadataForColumnBySpi(
71  table_id, SPIMAP_GEO_PHYSICAL_INPUT(rex_input->getIndex(), i + 1));
72  auto pcol_ti = pcd->columnType;
73  args.push_back(std::make_shared<Analyzer::ColumnVar>(
74  pcol_ti, table_id, pcd->columnId, rte_idx));
75  }
76  } else {
77  args.push_back(
78  std::make_shared<Analyzer::ColumnVar>(ti, table_id, column_id, rte_idx));
79  }
80  if (with_bounds && ti.has_bounds()) {
81  const auto bounds_cd = cat_.getMetadataForColumnBySpi(
82  table_id,
84  ti.get_physical_coord_cols() + 1));
85  auto bounds_ti = bounds_cd->columnType;
86  args.push_back(std::make_shared<Analyzer::ColumnVar>(
87  bounds_ti, table_id, bounds_cd->columnId, rte_idx));
88  }
89  if (with_render_group && ti.has_render_group()) {
90  const auto render_group_cd = cat_.getMetadataForColumnBySpi(
91  table_id,
93  ti.get_physical_coord_cols() + 2));
94  auto render_group_ti = render_group_cd->columnType;
95  args.push_back(std::make_shared<Analyzer::ColumnVar>(
96  render_group_ti, table_id, render_group_cd->columnId, rte_idx));
97  }
98  return args;
99 }
100 
101 namespace Importer_NS {
102 
103 std::vector<uint8_t> compress_coords(std::vector<double>& coords, const SQLTypeInfo& ti);
104 
105 } // namespace Importer_NS
106 
107 std::vector<std::shared_ptr<Analyzer::Expr>> RelAlgTranslator::translateGeoLiteral(
108  const RexLiteral* rex_literal,
109  SQLTypeInfo& ti,
110  bool with_bounds) const {
111  CHECK(rex_literal);
112  if (rex_literal->getType() != kTEXT) {
113  throw std::runtime_error("Geo literals must be strings");
114  }
115  const auto e = translateLiteral(rex_literal);
116  auto wkt = std::dynamic_pointer_cast<Analyzer::Constant>(e);
117  CHECK(wkt);
118  std::vector<double> coords;
119  std::vector<double> bounds;
120  std::vector<int> ring_sizes;
121  std::vector<int> poly_rings;
122  int32_t srid = ti.get_output_srid();
124  *wkt->get_constval().stringval, ti, coords, bounds, ring_sizes, poly_rings)) {
125  throw QueryNotSupported("Could not read geometry from text");
126  }
128  ti.set_input_srid(srid);
129  ti.set_output_srid(srid);
130  // Compress geo literals by default
131  if (srid == 4326) {
133  ti.set_comp_param(32);
134  }
135 
136  std::vector<std::shared_ptr<Analyzer::Expr>> args;
137 
138  std::vector<uint8_t> compressed_coords = Importer_NS::compress_coords(coords, ti);
139  std::list<std::shared_ptr<Analyzer::Expr>> compressed_coords_exprs;
140  for (auto cc : compressed_coords) {
141  Datum d;
142  d.tinyintval = cc;
143  auto e = makeExpr<Analyzer::Constant>(kTINYINT, false, d);
144  compressed_coords_exprs.push_back(e);
145  }
146  SQLTypeInfo arr_ti = SQLTypeInfo(kARRAY, true);
147  arr_ti.set_subtype(kTINYINT);
148  arr_ti.set_size(compressed_coords.size() * sizeof(int8_t));
149  arr_ti.set_compression(ti.get_compression());
150  arr_ti.set_comp_param((ti.get_compression() == kENCODING_GEOINT) ? 32 : 64);
151  args.push_back(makeExpr<Analyzer::Constant>(arr_ti, false, compressed_coords_exprs));
152 
153  auto lit_type = ti.get_type();
154  if (lit_type == kPOLYGON || lit_type == kMULTIPOLYGON) {
155  // ring sizes
156  std::list<std::shared_ptr<Analyzer::Expr>> ring_size_exprs;
157  for (auto c : ring_sizes) {
158  Datum d;
159  d.intval = c;
160  auto e = makeExpr<Analyzer::Constant>(kINT, false, d);
161  ring_size_exprs.push_back(e);
162  }
163  SQLTypeInfo arr_ti = SQLTypeInfo(kARRAY, true);
164  arr_ti.set_subtype(kINT);
165  arr_ti.set_size(ring_sizes.size() * sizeof(int32_t));
166  args.push_back(makeExpr<Analyzer::Constant>(arr_ti, false, ring_size_exprs));
167 
168  // poly rings
169  if (lit_type == kMULTIPOLYGON) {
170  std::list<std::shared_ptr<Analyzer::Expr>> poly_rings_exprs;
171  for (auto c : poly_rings) {
172  Datum d;
173  d.intval = c;
174  auto e = makeExpr<Analyzer::Constant>(kINT, false, d);
175  poly_rings_exprs.push_back(e);
176  }
177  SQLTypeInfo arr_ti = SQLTypeInfo(kARRAY, true);
178  arr_ti.set_subtype(kINT);
179  arr_ti.set_size(poly_rings.size() * sizeof(int32_t));
180  args.push_back(makeExpr<Analyzer::Constant>(arr_ti, false, poly_rings_exprs));
181  }
182  }
183 
184  if (with_bounds && ti.has_bounds()) {
185  // bounds
186  std::list<std::shared_ptr<Analyzer::Expr>> bounds_exprs;
187  for (auto b : bounds) {
188  Datum d;
189  d.doubleval = b;
190  auto e = makeExpr<Analyzer::Constant>(kDOUBLE, false, d);
191  bounds_exprs.push_back(e);
192  }
193  SQLTypeInfo arr_ti = SQLTypeInfo(kARRAY, true);
194  arr_ti.set_subtype(kDOUBLE);
195  arr_ti.set_size(bounds.size() * sizeof(double));
196  args.push_back(makeExpr<Analyzer::Constant>(arr_ti, false, bounds_exprs));
197  }
198 
199  return args;
200 }
201 
202 std::vector<std::shared_ptr<Analyzer::Expr>> RelAlgTranslator::translateGeoFunctionArg(
203  const RexScalar* rex_scalar,
204  SQLTypeInfo& arg_ti,
205  int32_t& lindex,
206  const bool with_bounds,
207  const bool with_render_group,
208  const bool expand_geo_col,
209  const bool is_projection) const {
210  std::vector<std::shared_ptr<Analyzer::Expr>> geoargs;
211 
212  const auto rex_input = dynamic_cast<const RexInput*>(rex_scalar);
213  if (rex_input) {
214  const auto input = translateInput(rex_input);
215  const auto column = dynamic_cast<const Analyzer::ColumnVar*>(input.get());
216  if (!column || !column->get_type_info().is_geometry()) {
217  throw QueryNotSupported("Geo function is expecting a geo column argument");
218  }
219  return translateGeoColumn(
220  rex_input, arg_ti, with_bounds, with_render_group, expand_geo_col);
221  }
222  const auto rex_function = dynamic_cast<const RexFunctionOperator*>(rex_scalar);
223  if (rex_function) {
224  if (rex_function->getName() == "ST_Transform"sv) {
225  CHECK_EQ(size_t(2), rex_function->size());
226  const auto rex_scalar0 =
227  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
228  if (!rex_scalar0) {
229  throw QueryNotSupported(rex_function->getName() + ": unexpected first argument");
230  }
231  auto arg0 = translateGeoFunctionArg(
232  rex_scalar0, arg_ti, lindex, with_bounds, with_render_group, expand_geo_col);
233 
234  const auto rex_literal =
235  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
236  if (!rex_literal) {
237  throw QueryNotSupported(rex_function->getName() +
238  ": second argument is expected to be a literal");
239  }
240  const auto e = translateLiteral(rex_literal);
241  auto ce = std::dynamic_pointer_cast<Analyzer::Constant>(e);
242  if (!ce || !e->get_type_info().is_integer()) {
243  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
244  }
245  int32_t srid = 0;
246  if (e->get_type_info().get_type() == kSMALLINT) {
247  srid = static_cast<int32_t>(ce->get_constval().smallintval);
248  } else if (e->get_type_info().get_type() == kTINYINT) {
249  srid = static_cast<int32_t>(ce->get_constval().tinyintval);
250  } else if (e->get_type_info().get_type() == kINT) {
251  srid = static_cast<int32_t>(ce->get_constval().intval);
252  } else {
253  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
254  }
255  if (srid != 900913) {
256  throw QueryNotSupported(rex_function->getName() + ": unsupported output SRID " +
257  std::to_string(srid));
258  }
259  if (arg_ti.get_input_srid() > 0) {
260  if (arg_ti.get_input_srid() != 4326) {
261  throw QueryNotSupported(rex_function->getName() + ": unsupported input SRID " +
262  std::to_string(arg_ti.get_input_srid()));
263  }
264  arg_ti.set_output_srid(
265  srid); // We have a valid input SRID, register the output SRID for transform
266  } else {
267  throw QueryNotSupported(rex_function->getName() +
268  ": unexpected input SRID, unable to transform");
269  }
270  return arg0;
271  } else if (func_resolve(
272  rex_function->getName(), "ST_GeomFromText"sv, "ST_GeogFromText"sv)) {
273  CHECK(rex_function->size() == size_t(1) || rex_function->size() == size_t(2));
274  // First - register srid, then send it to geo literal translation
275  int32_t srid = 0;
276  if (rex_function->size() == 2) {
277  const auto rex_literal =
278  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
279  if (!rex_literal) {
280  throw QueryNotSupported(rex_function->getName() +
281  ": second argument is expected to be a literal");
282  }
283  const auto e = translateLiteral(rex_literal);
284  auto ce = std::dynamic_pointer_cast<Analyzer::Constant>(e);
285  if (!ce || !e->get_type_info().is_integer()) {
286  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
287  }
288  if (e->get_type_info().get_type() == kSMALLINT) {
289  srid = static_cast<int32_t>(ce->get_constval().smallintval);
290  } else if (e->get_type_info().get_type() == kTINYINT) {
291  srid = static_cast<int32_t>(ce->get_constval().tinyintval);
292  } else if (e->get_type_info().get_type() == kINT) {
293  srid = static_cast<int32_t>(ce->get_constval().intval);
294  } else {
295  throw QueryNotSupported(rex_function->getName() + " expecting integer SRID");
296  }
297  if (srid != 0 && srid != 4326 && srid != 900913) {
298  throw QueryNotSupported(rex_function->getName() + ": unsupported SRID " +
299  std::to_string(srid));
300  }
301  }
302  arg_ti.set_input_srid(srid); // Input SRID
303  arg_ti.set_output_srid(srid); // Output SRID is the same - no transform
304 
305  const auto rex_literal =
306  dynamic_cast<const RexLiteral*>(rex_function->getOperand(0));
307  if (!rex_literal) {
308  throw QueryNotSupported(rex_function->getName() +
309  " expects a string literal as first argument");
310  }
311  auto arg0 = translateGeoLiteral(rex_literal, arg_ti, with_bounds);
312  arg_ti.set_subtype((rex_function->getName() == "ST_GeogFromText"sv) ? kGEOGRAPHY
313  : kGEOMETRY);
314  return arg0;
315  } else if (rex_function->getName() == "ST_PointN"sv) {
316  CHECK_EQ(size_t(2), rex_function->size());
317  const auto rex_scalar0 =
318  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
319  if (!rex_scalar0) {
320  throw QueryNotSupported(rex_function->getName() +
321  ": expects scalar as first argument");
322  }
323  auto arg0 = translateGeoFunctionArg(
324  rex_scalar0, arg_ti, lindex, with_bounds, with_render_group, expand_geo_col);
325  if (arg_ti.get_type() != kLINESTRING) {
326  throw QueryNotSupported(rex_function->getName() +
327  " expects LINESTRING as first argument");
328  }
329  const auto rex_literal =
330  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
331  if (!rex_literal) {
332  throw QueryNotSupported(rex_function->getName() +
333  ": second argument is expected to be a literal");
334  }
335  const auto e = translateLiteral(rex_literal);
336  auto ce = std::dynamic_pointer_cast<Analyzer::Constant>(e);
337  if (!ce || !e->get_type_info().is_integer()) {
338  throw QueryNotSupported(rex_function->getName() +
339  ": expecting integer index as second argument");
340  }
341  int32_t index = 0;
342  if (e->get_type_info().get_type() == kSMALLINT) {
343  index = static_cast<int32_t>(ce->get_constval().smallintval);
344  } else if (e->get_type_info().get_type() == kTINYINT) {
345  index = static_cast<int32_t>(ce->get_constval().tinyintval);
346  } else if (e->get_type_info().get_type() == kINT) {
347  index = static_cast<int32_t>(ce->get_constval().intval);
348  } else {
349  throw QueryNotSupported(rex_function->getName() + " expecting integer index");
350  }
351  if (lindex != 0) {
352  throw QueryNotSupported(rex_function->getName() +
353  ": LINESTRING is already indexed");
354  }
355  if (index == 0) {
356  throw QueryNotSupported(rex_function->getName() + ": invalid index");
357  }
358  lindex = index;
359  return arg0;
360  } else if (rex_function->getName() == "ST_StartPoint"sv) {
361  CHECK_EQ(size_t(1), rex_function->size());
362  const auto rex_scalar0 =
363  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
364  if (!rex_scalar0) {
365  throw QueryNotSupported(rex_function->getName() +
366  ": expects scalar as first argument");
367  }
368  auto arg0 = translateGeoFunctionArg(
369  rex_scalar0, arg_ti, lindex, with_bounds, with_render_group, expand_geo_col);
370  if (arg_ti.get_type() != kLINESTRING) {
371  throw QueryNotSupported(rex_function->getName() +
372  " expects LINESTRING as first argument");
373  }
374  if (lindex != 0) {
375  throw QueryNotSupported(rex_function->getName() +
376  ": LINESTRING is already indexed");
377  }
378  lindex = 1;
379  return arg0;
380  } else if (rex_function->getName() == "ST_EndPoint"sv) {
381  CHECK_EQ(size_t(1), rex_function->size());
382  const auto rex_scalar0 =
383  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
384  if (!rex_scalar0) {
385  throw QueryNotSupported(rex_function->getName() +
386  ": expects scalar as first argument");
387  }
388  auto arg0 = translateGeoFunctionArg(
389  rex_scalar0, arg_ti, lindex, with_bounds, with_render_group, expand_geo_col);
390  if (arg_ti.get_type() != kLINESTRING) {
391  throw QueryNotSupported(rex_function->getName() +
392  " expects LINESTRING as first argument");
393  }
394  if (lindex != 0) {
395  throw QueryNotSupported(rex_function->getName() +
396  ": LINESTRING is already indexed");
397  }
398  lindex = -1;
399  return arg0;
400  } else if (rex_function->getName() == "ST_SRID"sv) {
401  CHECK_EQ(size_t(1), rex_function->size());
402  const auto rex_scalar0 =
403  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
404  if (!rex_scalar0) {
405  throw QueryNotSupported(rex_function->getName() +
406  ": expects scalar as first argument");
407  }
408  auto arg0 = translateGeoFunctionArg(
409  rex_scalar0, arg_ti, lindex, with_bounds, with_render_group, expand_geo_col);
410  if (!IS_GEO(arg_ti.get_type())) {
411  throw QueryNotSupported(rex_function->getName() + " expects geometry argument");
412  }
413  return arg0;
414  } else if (rex_function->getName() == "ST_SetSRID"sv) {
415  CHECK_EQ(size_t(2), rex_function->size());
416  const auto rex_scalar0 =
417  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
418  if (!rex_scalar0) {
419  throw QueryNotSupported(rex_function->getName() +
420  ": expects scalar as first argument");
421  }
422  auto arg0 = translateGeoFunctionArg(rex_scalar0,
423  arg_ti,
424  lindex,
425  with_bounds,
426  with_render_group,
427  expand_geo_col,
428  is_projection);
429  if (!IS_GEO(arg_ti.get_type())) {
430  throw QueryNotSupported(rex_function->getName() + " expects geometry argument");
431  }
432  const auto rex_literal =
433  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
434  if (!rex_literal) {
435  throw QueryNotSupported(rex_function->getName() +
436  ": second argument is expected to be a literal");
437  }
438  const auto e = translateLiteral(rex_literal);
439  auto ce = std::dynamic_pointer_cast<Analyzer::Constant>(e);
440  if (!ce || !e->get_type_info().is_integer()) {
441  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
442  }
443  int32_t srid = 0;
444  if (e->get_type_info().get_type() == kSMALLINT) {
445  srid = static_cast<int32_t>(ce->get_constval().smallintval);
446  } else if (e->get_type_info().get_type() == kTINYINT) {
447  srid = static_cast<int32_t>(ce->get_constval().tinyintval);
448  } else if (e->get_type_info().get_type() == kINT) {
449  srid = static_cast<int32_t>(ce->get_constval().intval);
450  } else {
451  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
452  }
453  arg_ti.set_input_srid(srid); // Input SRID
454  arg_ti.set_output_srid(srid); // Output SRID is the same - no transform
455  return arg0;
456  } else if (rex_function->getName() == "CastToGeography"sv) {
457  CHECK_EQ(size_t(1), rex_function->size());
458  const auto rex_scalar0 =
459  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
460  if (!rex_scalar0) {
461  throw QueryNotSupported(rex_function->getName() +
462  ": expects scalar as first argument");
463  }
464  auto arg0 = translateGeoFunctionArg(
465  rex_scalar0, arg_ti, lindex, with_bounds, with_render_group, expand_geo_col);
466  if (!IS_GEO(arg_ti.get_type())) {
467  throw QueryNotSupported(rex_function->getName() + " expects geometry argument");
468  }
469  if (arg_ti.get_output_srid() != 4326) {
470  throw QueryNotSupported(rex_function->getName() +
471  " expects geometry with SRID=4326");
472  }
473  arg_ti.set_subtype(kGEOGRAPHY);
474  return arg0;
475  } else if (rex_function->getName() == "ST_Point"sv) {
476  CHECK_EQ(size_t(2), rex_function->size());
477  arg_ti.set_type(kPOINT);
478  arg_ti.set_subtype(kGEOMETRY);
479  arg_ti.set_input_srid(0);
480  arg_ti.set_output_srid(0);
482 
483  auto coord1 = translateScalarRex(rex_function->getOperand(0));
484  auto coord2 = translateScalarRex(rex_function->getOperand(1));
485  auto d_ti = SQLTypeInfo(kDOUBLE, true);
486  auto cast_coord1 = coord1->add_cast(d_ti);
487  auto cast_coord2 = coord2->add_cast(d_ti);
488  // First try to fold to geo literal
489  auto fold1 = fold_expr(cast_coord1.get());
490  auto fold2 = fold_expr(cast_coord2.get());
491  auto const_coord1 = std::dynamic_pointer_cast<Analyzer::Constant>(fold1);
492  auto const_coord2 = std::dynamic_pointer_cast<Analyzer::Constant>(fold2);
493  if (const_coord1 && const_coord2) {
494  CHECK(const_coord1->get_type_info().get_type() == kDOUBLE);
495  CHECK(const_coord2->get_type_info().get_type() == kDOUBLE);
496  std::string wkt = "POINT(" +
497  std::to_string(const_coord1->get_constval().doubleval) + " " +
498  std::to_string(const_coord2->get_constval().doubleval) + ")";
499  RexLiteral rex_literal{wkt, kTEXT, kNULLT, 0, 0, 0, 0};
500  auto args = translateGeoLiteral(&rex_literal, arg_ti, false);
501  CHECK(arg_ti.get_type() == kPOINT);
502  return args;
503  }
504  // Couldn't fold to geo literal, construct on the fly
505  auto da_ti = SQLTypeInfo(kARRAY, true);
506  da_ti.set_subtype(kDOUBLE);
507  da_ti.set_size(16);
508  auto cast_coords = {cast_coord1, cast_coord2};
509  auto is_local_alloca = !is_projection;
510  auto ae =
511  makeExpr<Analyzer::ArrayExpr>(da_ti, cast_coords, 0, false, is_local_alloca);
512  // cast it to tinyint[16]
513  SQLTypeInfo tia_ti = SQLTypeInfo(kARRAY, true);
514  tia_ti.set_subtype(kTINYINT);
515  tia_ti.set_size(16);
516  return {makeExpr<Analyzer::UOper>(tia_ti, false, kCAST, ae)};
517  } else {
518  throw QueryNotSupported("Unsupported argument: " + rex_function->getName());
519  }
520  }
521  const auto rex_literal = dynamic_cast<const RexLiteral*>(rex_scalar);
522  if (rex_literal) {
523  return translateGeoLiteral(rex_literal, arg_ti, with_bounds);
524  }
525  throw QueryNotSupported("Geo function argument not supported");
526 }
527 
528 namespace {
529 
530 std::string suffix(SQLTypes type) {
531  if (type == kPOINT) {
532  return std::string("_Point");
533  }
534  if (type == kLINESTRING) {
535  return std::string("_LineString");
536  }
537  if (type == kPOLYGON) {
538  return std::string("_Polygon");
539  }
540  if (type == kMULTIPOLYGON) {
541  return std::string("_MultiPolygon");
542  }
543  throw QueryNotSupported("Unsupported argument type");
544 }
545 
546 } // namespace
547 
548 std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateGeoConstructor(
549  const RexFunctionOperator* rex_function) const {
550  if (func_resolve(rex_function->getName(),
551  "ST_GeomFromText"sv,
552  "ST_GeogFromText"sv,
553  "ST_Point"sv,
554  "ST_SetSRID"sv)) {
555  SQLTypeInfo arg_ti;
556  int32_t lindex = 0;
557  auto geoargs =
558  translateGeoFunctionArg(rex_function, arg_ti, lindex, false, false, true, true);
559  return makeExpr<Analyzer::GeoExpr>(arg_ti, geoargs);
560  }
561  throw QueryNotSupported(rex_function->getName() +
562  " geo constructor is not supported in this context");
563 }
564 
565 std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateUnaryGeoFunction(
566  const RexFunctionOperator* rex_function) const {
567  CHECK_EQ(size_t(1), rex_function->size());
568 
569  int32_t lindex = 0;
570  std::string specialized_geofunc{rex_function->getName()};
571 
572  // Geo function calls which do not need the coords col but do need cols associated with
573  // physical coords (e.g. ring_sizes / poly_rings)
574  if (rex_function->getName() == "ST_NRings"sv) {
575  SQLTypeInfo arg_ti;
576  auto geoargs = translateGeoFunctionArg(
577  rex_function->getOperand(0), arg_ti, lindex, false, false, true);
578  if (arg_ti.get_type() == kPOLYGON) {
579  CHECK_EQ(geoargs.size(), 2u);
580  geoargs.erase(geoargs.begin(), geoargs.begin() + 1); // remove the coords
581  return makeExpr<Analyzer::FunctionOper>(
582  rex_function->getType(), specialized_geofunc, geoargs);
583  } else if (arg_ti.get_type() == kMULTIPOLYGON) {
584  CHECK_EQ(geoargs.size(), 3u);
585  geoargs.erase(geoargs.begin(), geoargs.begin() + 1); // remove the coords
586  geoargs.erase(geoargs.begin() + 1, geoargs.end()); // remove the poly_rings
587  return makeExpr<Analyzer::FunctionOper>(
588  rex_function->getType(), specialized_geofunc, geoargs);
589  } else {
590  throw QueryNotSupported(rex_function->getName() +
591  " expects a POLYGON or MULTIPOLYGON");
592  }
593  } else if (rex_function->getName() == "ST_NPoints"sv) {
594  SQLTypeInfo arg_ti;
595  auto geoargs = translateGeoFunctionArg(
596  rex_function->getOperand(0), arg_ti, lindex, false, false, true);
597  geoargs.erase(geoargs.begin() + 1, geoargs.end()); // remove all but coords
598  // Add compression information
599  Datum input_compression;
600  input_compression.intval =
601  (arg_ti.get_compression() == kENCODING_GEOINT && arg_ti.get_comp_param() == 32)
602  ? 1
603  : 0;
604  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
605  return makeExpr<Analyzer::FunctionOper>(
606  rex_function->getType(), specialized_geofunc, geoargs);
607  } else if (func_resolve(rex_function->getName(), "ST_Perimeter"sv, "ST_Area"sv)) {
608  SQLTypeInfo arg_ti;
609  auto geoargs = translateGeoFunctionArg(
610  rex_function->getOperand(0), arg_ti, lindex, false, false, true);
611  if (arg_ti.get_type() != kPOLYGON && arg_ti.get_type() != kMULTIPOLYGON) {
612  throw QueryNotSupported(rex_function->getName() +
613  " expects a POLYGON or MULTIPOLYGON");
614  }
615  specialized_geofunc += suffix(arg_ti.get_type());
616  if (arg_ti.get_subtype() == kGEOGRAPHY && arg_ti.get_output_srid() == 4326) {
617  specialized_geofunc += "_Geodesic"s;
618  }
619  // Add compression information
620  Datum input_compression;
621  input_compression.intval =
622  (arg_ti.get_compression() == kENCODING_GEOINT && arg_ti.get_comp_param() == 32)
623  ? 1
624  : 0;
625  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
626  Datum input_srid;
627  input_srid.intval = arg_ti.get_input_srid();
628  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid));
629  Datum output_srid;
630  output_srid.intval = arg_ti.get_output_srid();
631  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, output_srid));
632  return makeExpr<Analyzer::FunctionOper>(
633  rex_function->getType(), specialized_geofunc, geoargs);
634  }
635 
636  // Accessors for poly bounds and render group for in-situ poly render queries
637  if (func_resolve(rex_function->getName(),
638  "MapD_GeoPolyBoundsPtr"sv /* deprecated */,
639  "OmniSci_Geo_PolyBoundsPtr"sv)) {
640  SQLTypeInfo arg_ti;
641  // get geo column plus bounds only (not expanded)
642  auto geoargs = translateGeoFunctionArg(
643  rex_function->getOperand(0), arg_ti, lindex, true, false, false);
644  // this function only works on polys
645  if (!IS_GEO_POLY(arg_ti.get_type())) {
646  throw QueryNotSupported(rex_function->getName() +
647  " expects a POLYGON or MULTIPOLYGON");
648  }
649  // only need the bounds argument (last), discard the rest
650  geoargs.erase(geoargs.begin(), geoargs.end() - 1);
651  // done
652  return makeExpr<Analyzer::FunctionOper>(
653  rex_function->getType(), specialized_geofunc, geoargs);
654  } else if (func_resolve(rex_function->getName(),
655  "MapD_GeoPolyRenderGroup"sv /* deprecated */,
656  "OmniSci_Geo_PolyRenderGroup"sv)) {
657  SQLTypeInfo arg_ti;
658  // get geo column plus render_group only (not expanded)
659  auto geoargs = translateGeoFunctionArg(
660  rex_function->getOperand(0), arg_ti, lindex, false, true, false);
661  // this function only works on polys
662  if (!IS_GEO_POLY(arg_ti.get_type())) {
663  throw QueryNotSupported(rex_function->getName() +
664  " expects a POLYGON or MULTIPOLYGON");
665  }
666  // only need the render_group argument (last), discard the rest
667  geoargs.erase(geoargs.begin(), geoargs.end() - 1);
668  // done
669  return makeExpr<Analyzer::FunctionOper>(
670  rex_function->getType(), specialized_geofunc, geoargs);
671  }
672 
673  // All functions below use geo col as reference and expand it as necessary
674  SQLTypeInfo arg_ti;
675  bool with_bounds = true;
676  auto geoargs = translateGeoFunctionArg(
677  rex_function->getOperand(0), arg_ti, lindex, with_bounds, false, false);
678 
679  if (rex_function->getName() == "ST_SRID"sv) {
680  Datum output_srid;
681  output_srid.intval = arg_ti.get_output_srid();
682  return makeExpr<Analyzer::Constant>(kINT, false, output_srid);
683  }
684 
685  if (func_resolve(
686  rex_function->getName(), "ST_XMin"sv, "ST_YMin"sv, "ST_XMax"sv, "ST_YMax"sv)) {
687  // If type has bounds - use them, otherwise look at coords
688  if (arg_ti.has_bounds()) {
689  if (lindex != 0) {
690  throw QueryNotSupported(rex_function->getName() +
691  " doesn't support indexed LINESTRINGs");
692  }
693  // Only need the bounds argument, discard the rest
694  geoargs.erase(geoargs.begin(), geoargs.end() - 1);
695 
696  // Supply srids too - transformed geo would have a transformed bounding box
697  Datum input_srid;
698  input_srid.intval = arg_ti.get_input_srid();
699  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid));
700  Datum output_srid;
701  output_srid.intval = arg_ti.get_output_srid();
702  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, output_srid));
703 
704  specialized_geofunc += "_Bounds"s;
705  return makeExpr<Analyzer::FunctionOper>(
706  rex_function->getType(), specialized_geofunc, geoargs);
707  }
708  }
709 
710  // All geo function calls translated below only need the coords, extras e.g. ring_sizes
711  // are dropped. Specialize for other/new functions if needed.
712  geoargs.erase(geoargs.begin() + 1, geoargs.end());
713 
714  if (func_resolve(rex_function->getName(), "ST_X"sv, "ST_Y"sv)) {
715  if (arg_ti.get_type() == kLINESTRING) {
716  if (lindex == 0) {
717  throw QueryNotSupported(
718  rex_function->getName() +
719  " expects a POINT, use LINESTRING accessor, e.g. ST_POINTN");
720  }
721  Datum index;
722  index.intval = lindex;
723  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, index));
724  } else if (arg_ti.get_type() != kPOINT) {
725  throw QueryNotSupported(rex_function->getName() + " expects a POINT");
726  }
727  specialized_geofunc += suffix(arg_ti.get_type());
728  } else if (rex_function->getName() == "ST_Length"sv) {
729  if (arg_ti.get_type() != kLINESTRING || lindex != 0) {
730  throw QueryNotSupported(rex_function->getName() + " expects unindexed LINESTRING");
731  }
732  specialized_geofunc += suffix(arg_ti.get_type());
733  if (arg_ti.get_subtype() == kGEOGRAPHY && arg_ti.get_output_srid() == 4326) {
734  specialized_geofunc += "_Geodesic"s;
735  }
736  }
737 
738  // Add input compression mode and SRID args to enable on-the-fly
739  // decompression/transforms
740  Datum input_compression;
741  input_compression.intval =
742  (arg_ti.get_compression() == kENCODING_GEOINT && arg_ti.get_comp_param() == 32) ? 1
743  : 0;
744  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
745  Datum input_srid;
746  input_srid.intval = arg_ti.get_input_srid();
747  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid));
748 
749  // Add output SRID arg to enable on-the-fly transforms
750  Datum output_srid;
751  output_srid.intval = arg_ti.get_output_srid();
752  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, output_srid));
753 
754  return makeExpr<Analyzer::FunctionOper>(
755  rex_function->getType(), specialized_geofunc, geoargs);
756 }
757 
758 std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateBinaryGeoFunction(
759  const RexFunctionOperator* rex_function) const {
760  auto function_name = rex_function->getName();
761  auto return_type = rex_function->getType();
762  bool swap_args = false;
763  bool with_bounds = false;
764  bool negate_result = false;
765  if (function_name == "ST_DWithin"sv) {
766  CHECK_EQ(size_t(3), rex_function->size());
767  function_name = "ST_Distance";
768  return_type = SQLTypeInfo(kDOUBLE, false);
769  } else if (function_name == "ST_DFullyWithin"sv) {
770  CHECK_EQ(size_t(3), rex_function->size());
771  function_name = "ST_MaxDistance";
772  return_type = SQLTypeInfo(kDOUBLE, false);
773  } else {
774  CHECK_EQ(size_t(2), rex_function->size());
775  }
776  if (function_name == "ST_Within"sv) {
777  function_name = "ST_Contains";
778  swap_args = true;
779  } else if (function_name == "ST_Disjoint"sv) {
780  function_name = "ST_Intersects";
781  negate_result = true;
782  }
783  if (func_resolve(function_name, "ST_Contains"sv, "ST_Intersects"sv)) {
784  with_bounds = true;
785  }
786 
787  std::vector<std::shared_ptr<Analyzer::Expr>> geoargs;
788  SQLTypeInfo arg0_ti;
789  SQLTypeInfo arg1_ti;
790  int32_t lindex0 = 0;
791  int32_t lindex1 = 0;
792 
793  auto geoargs0 = translateGeoFunctionArg(rex_function->getOperand(swap_args ? 1 : 0),
794  arg0_ti,
795  lindex0,
796  with_bounds,
797  false,
798  false);
799  if (arg0_ti.get_type() == kLINESTRING) {
800  Datum index;
801  index.intval = lindex0;
802  geoargs0.push_back(makeExpr<Analyzer::Constant>(kINT, false, index));
803  }
804  geoargs.insert(geoargs.end(), geoargs0.begin(), geoargs0.end());
805  auto geoargs1 = translateGeoFunctionArg(rex_function->getOperand(swap_args ? 0 : 1),
806  arg1_ti,
807  lindex1,
808  with_bounds,
809  false,
810  false);
811  if (arg1_ti.get_type() == kLINESTRING) {
812  Datum index;
813  index.intval = lindex1;
814  geoargs1.push_back(makeExpr<Analyzer::Constant>(kINT, false, index));
815  }
816  geoargs.insert(geoargs.end(), geoargs1.begin(), geoargs1.end());
817 
818  if (arg0_ti.get_subtype() != kNULLT && arg0_ti.get_subtype() != arg1_ti.get_subtype()) {
819  throw QueryNotSupported(rex_function->getName() +
820  " accepts either two GEOGRAPHY or two GEOMETRY arguments");
821  }
822  // Check SRID match if at least one is set/valid
823  if ((arg0_ti.get_output_srid() > 0 || arg1_ti.get_output_srid() > 0) &&
824  arg0_ti.get_output_srid() != arg1_ti.get_output_srid()) {
825  throw QueryNotSupported(rex_function->getName() + " cannot accept different SRIDs");
826  }
827 
828  std::string specialized_geofunc{function_name + suffix(arg0_ti.get_type()) +
829  suffix(arg1_ti.get_type())};
830 
831  if (arg0_ti.get_subtype() == kGEOGRAPHY && arg0_ti.get_output_srid() == 4326) {
832  // Need to call geodesic runtime functions
833  if (function_name == "ST_Distance"sv) {
834  if ((arg0_ti.get_type() == kPOINT ||
835  (arg0_ti.get_type() == kLINESTRING && lindex0 != 0)) &&
836  (arg1_ti.get_type() == kPOINT ||
837  (arg1_ti.get_type() == kLINESTRING && lindex1 != 0))) {
838  // Geodesic distance between points (or indexed linestrings)
839  specialized_geofunc += "_Geodesic"s;
840  } else {
841  throw QueryNotSupported(function_name +
842  " currently doesn't accept non-POINT geographies");
843  }
844  } else if (rex_function->getName() == "ST_Contains"sv) {
845  // We currently don't have a geodesic implementation of ST_Contains,
846  // allowing calls to a [less precise] cartesian implementation.
847  } else {
848  throw QueryNotSupported(function_name + " doesn't accept geographies");
849  }
850  } else if (function_name == "ST_Distance"sv && rex_function->size() == 3) {
851  if (arg0_ti.get_type() == kPOINT && arg1_ti.get_type() == kPOINT) {
852  // Cartesian distance between points used by ST_DWithin - switch to faster Squared
853  specialized_geofunc += "_Squared"s;
854  }
855  }
856 
857  // Add first input's compression mode and SRID args to enable on-the-fly
858  // decompression/transforms
859  Datum input_compression0;
860  input_compression0.intval =
861  (arg0_ti.get_compression() == kENCODING_GEOINT && arg0_ti.get_comp_param() == 32)
862  ? 1
863  : 0;
864  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression0));
865  Datum input_srid0;
866  input_srid0.intval = arg0_ti.get_input_srid();
867  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid0));
868 
869  // Add second input's compression mode and SRID args to enable on-the-fly
870  // decompression/transforms
871  Datum input_compression1;
872  input_compression1.intval =
873  (arg1_ti.get_compression() == kENCODING_GEOINT && arg1_ti.get_comp_param() == 32)
874  ? 1
875  : 0;
876  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression1));
877  Datum input_srid1;
878  input_srid1.intval = arg1_ti.get_input_srid();
879  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid1));
880 
881  // Add output SRID arg to enable on-the-fly transforms
882  Datum output_srid;
883  output_srid.intval = arg0_ti.get_output_srid();
884  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, output_srid));
885 
886  auto result =
887  makeExpr<Analyzer::FunctionOper>(return_type, specialized_geofunc, geoargs);
888  if (negate_result) {
889  return makeExpr<Analyzer::UOper>(kBOOLEAN, kNOT, result);
890  }
891  return result;
892 }
893 
894 std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateTernaryGeoFunction(
895  const RexFunctionOperator* rex_function) const {
896  CHECK_EQ(size_t(3), rex_function->size());
897 
898  auto distance_expr = translateScalarRex(rex_function->getOperand(2));
899  const auto& distance_ti = SQLTypeInfo(kDOUBLE, false);
900  if (distance_expr->get_type_info().get_type() != kDOUBLE) {
901  distance_expr->add_cast(distance_ti);
902  }
903 
904  // Translate the geo distance function call portion
905  const auto geo_distance_expr = translateBinaryGeoFunction(rex_function);
906 
907  if (rex_function->getName() == "ST_DWithin") {
908  auto func_oper = dynamic_cast<Analyzer::FunctionOper*>(geo_distance_expr.get());
909  if (func_oper && func_oper->getName() == "ST_Distance_Point_Point_Squared"sv) {
910  // Point_Point combination will yield geo_distance squared which is faster,
911  // need to compare it with distance squared
912  distance_expr = makeExpr<Analyzer::BinOper>(distance_ti,
913  distance_expr->get_contains_agg(),
914  kMULTIPLY,
915  kONE,
916  distance_expr,
917  distance_expr);
918  distance_expr = fold_expr(distance_expr.get());
919  }
920  }
921 
922  return makeExpr<Analyzer::BinOper>(
923  kBOOLEAN, kLE, kONE, geo_distance_expr, distance_expr);
924 }
925 
926 std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateGeoComparison(
927  const RexOperator* rex_operator) const {
928  if (rex_operator->size() != size_t(2)) {
929  return nullptr;
930  }
931 
932  auto geo_distance_expr = translateScalarRex(rex_operator->getOperand(0));
933  auto func_oper = dynamic_cast<Analyzer::FunctionOper*>(geo_distance_expr.get());
934  if (func_oper && func_oper->getName() == "ST_Distance_Point_Point"sv) {
935  const auto& distance_ti = SQLTypeInfo(kDOUBLE, false);
936  std::vector<std::shared_ptr<Analyzer::Expr>> geoargs;
937  for (size_t i = 0; i < func_oper->getArity(); i++) {
938  geoargs.push_back(func_oper->getOwnArg(i));
939  }
940  geo_distance_expr = makeExpr<Analyzer::FunctionOper>(
941  distance_ti, "ST_Distance_Point_Point_Squared"s, geoargs);
942  auto distance_expr = translateScalarRex(rex_operator->getOperand(1));
943  if (distance_expr->get_type_info().get_type() != kDOUBLE) {
944  distance_expr->add_cast(distance_ti);
945  }
946  distance_expr = makeExpr<Analyzer::BinOper>(distance_ti,
947  distance_expr->get_contains_agg(),
948  kMULTIPLY,
949  kONE,
950  distance_expr,
951  distance_expr);
952  distance_expr = fold_expr(distance_expr.get());
953  return makeExpr<Analyzer::BinOper>(
954  kBOOLEAN, rex_operator->getOperator(), kONE, geo_distance_expr, distance_expr);
955  }
956  return nullptr;
957 }
958 
959 std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateFunctionWithGeoArg(
960  const RexFunctionOperator* rex_function) const {
961  int32_t lindex = 0;
962  std::string specialized_geofunc{rex_function->getName()};
963  if (func_resolve(rex_function->getName(),
964  "convert_meters_to_pixel_width"sv,
965  "convert_meters_to_pixel_height"sv)) {
966  CHECK_EQ(rex_function->size(), 6u);
967  SQLTypeInfo arg_ti;
968  std::vector<std::shared_ptr<Analyzer::Expr>> args;
969  args.push_back(translateScalarRex(rex_function->getOperand(0)));
970  auto geoargs = translateGeoFunctionArg(
971  rex_function->getOperand(1), arg_ti, lindex, false, true, false);
972  // only works on points
973  if (arg_ti.get_type() != kPOINT) {
974  throw QueryNotSupported(rex_function->getName() +
975  " expects a point for the second argument");
976  }
977 
978  args.insert(args.end(), geoargs.begin(), geoargs.begin() + 1);
979 
980  // Add compression information
981  Datum input_compression;
982  input_compression.intval =
983  (arg_ti.get_compression() == kENCODING_GEOINT && arg_ti.get_comp_param() == 32)
984  ? 1
985  : 0;
986  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
987  if (arg_ti.get_input_srid() != 4326) {
988  throw QueryNotSupported(
989  rex_function->getName() +
990  " currently only supports points of with SRID WGS84/EPSG:4326");
991  }
992  Datum input_srid;
993  input_srid.intval = arg_ti.get_input_srid();
994  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid));
995  Datum output_srid;
996  // Forcing web-mercator projection for now
997  // TODO(croot): check that the input-to-output conversion routines exist?
998  output_srid.intval =
999  arg_ti.get_output_srid() != 900913 ? 900913 : arg_ti.get_output_srid();
1000  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, output_srid));
1001 
1002  args.push_back(translateScalarRex(rex_function->getOperand(2)));
1003  args.push_back(translateScalarRex(rex_function->getOperand(3)));
1004  args.push_back(translateScalarRex(rex_function->getOperand(4)));
1005  args.push_back(translateScalarRex(rex_function->getOperand(5)));
1006  return makeExpr<Analyzer::FunctionOper>(
1007  rex_function->getType(), specialized_geofunc, args);
1008  } else if (rex_function->getName() == "is_point_in_view"sv) {
1009  CHECK_EQ(rex_function->size(), 5u);
1010  SQLTypeInfo arg_ti;
1011  std::vector<std::shared_ptr<Analyzer::Expr>> args;
1012  auto geoargs = translateGeoFunctionArg(
1013  rex_function->getOperand(0), arg_ti, lindex, false, true, false);
1014  // only works on points
1015  if (arg_ti.get_type() != kPOINT) {
1016  throw QueryNotSupported(rex_function->getName() +
1017  " expects a point for the second argument");
1018  }
1019 
1020  args.insert(args.end(), geoargs.begin(), geoargs.begin() + 1);
1021 
1022  // Add compression information
1023  Datum input_compression;
1024  input_compression.intval =
1025  (arg_ti.get_compression() == kENCODING_GEOINT && arg_ti.get_comp_param() == 32)
1026  ? 1
1027  : 0;
1028  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
1029  if (arg_ti.get_input_srid() != 4326) {
1030  throw QueryNotSupported(
1031  rex_function->getName() +
1032  " currently only supports points of with SRID WGS84/EPSG:4326");
1033  }
1034  args.push_back(translateScalarRex(rex_function->getOperand(1)));
1035  args.push_back(translateScalarRex(rex_function->getOperand(2)));
1036  args.push_back(translateScalarRex(rex_function->getOperand(3)));
1037  args.push_back(translateScalarRex(rex_function->getOperand(4)));
1038  return makeExpr<Analyzer::FunctionOper>(
1039  rex_function->getType(), specialized_geofunc, args);
1040  } else if (rex_function->getName() == "is_point_size_in_view"sv) {
1041  CHECK_EQ(rex_function->size(), 6u);
1042  SQLTypeInfo arg_ti;
1043  std::vector<std::shared_ptr<Analyzer::Expr>> args;
1044  auto geoargs = translateGeoFunctionArg(
1045  rex_function->getOperand(0), arg_ti, lindex, false, true, false);
1046  // only works on points
1047  if (arg_ti.get_type() != kPOINT) {
1048  throw QueryNotSupported(rex_function->getName() +
1049  " expects a point for the second argument");
1050  }
1051 
1052  args.insert(args.end(), geoargs.begin(), geoargs.begin() + 1);
1053 
1054  // Add compression information
1055  Datum input_compression;
1056  input_compression.intval =
1057  (arg_ti.get_compression() == kENCODING_GEOINT && arg_ti.get_comp_param() == 32)
1058  ? 1
1059  : 0;
1060  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
1061  if (arg_ti.get_input_srid() != 4326) {
1062  throw QueryNotSupported(
1063  rex_function->getName() +
1064  " currently only supports points of with SRID WGS84/EPSG:4326");
1065  }
1066  args.push_back(translateScalarRex(rex_function->getOperand(1)));
1067  args.push_back(translateScalarRex(rex_function->getOperand(2)));
1068  args.push_back(translateScalarRex(rex_function->getOperand(3)));
1069  args.push_back(translateScalarRex(rex_function->getOperand(4)));
1070  args.push_back(translateScalarRex(rex_function->getOperand(5)));
1071  return makeExpr<Analyzer::FunctionOper>(
1072  rex_function->getType(), specialized_geofunc, args);
1073  }
1074  CHECK(false);
1075  return nullptr;
1076 }
1077 
1078 std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateGeoOverlapsOper(
1079  const RexOperator* rex_operator) const {
1080  CHECK_EQ(rex_operator->size(), 2u);
1081 
1082  auto translate_input =
1083  [&](const RexScalar* operand) -> std::shared_ptr<Analyzer::Expr> {
1084  const auto input = dynamic_cast<const RexInput*>(operand);
1085  CHECK(input);
1086 
1087  SQLTypeInfo ti;
1088  const auto exprs = translateGeoColumn(input, ti, true, false, false);
1089  CHECK_GT(exprs.size(), 0u);
1090  if (ti.get_type() == kPOINT) {
1091  return exprs.front();
1092  } else {
1093  return exprs.back();
1094  }
1095  };
1096 
1097  SQLQualifier sql_qual{kONE};
1098  SQLOps sql_op{kOVERLAPS};
1099  return makeExpr<Analyzer::BinOper>(SQLTypeInfo(kBOOLEAN, false),
1100  false,
1101  sql_op,
1102  sql_qual,
1103  translate_input(rex_operator->getOperand(1)),
1104  translate_input(rex_operator->getOperand(0)));
1105 }
int8_t tinyintval
Definition: sqltypes.h:126
std::shared_ptr< Analyzer::Expr > translateGeoComparison(const RexOperator *) const
SQLOps getOperator() const
#define CHECK_EQ(x, y)
Definition: Logger.h:201
auto func_resolve
int get_physical_coord_cols() const
Definition: sqltypes.h:362
std::shared_ptr< Analyzer::Expr > translateGeoOverlapsOper(const RexOperator *) const
SQLTypes
Definition: sqltypes.h:41
#define SPIMAP_GEO_PHYSICAL_INPUT(c, i)
Definition: Catalog.h:70
SQLQualifier
Definition: sqldefs.h:69
SQLOps
Definition: sqldefs.h:29
Definition: sqldefs.h:35
void set_size(int s)
Definition: sqltypes.h:424
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
std::vector< uint8_t > compress_coords(std::vector< double > &coords, const SQLTypeInfo &ti)
Definition: Importer.cpp:1422
void set_input_srid(int d)
Definition: sqltypes.h:420
std::shared_ptr< Analyzer::Expr > translateFunctionWithGeoArg(const RexFunctionOperator *) const
#define CHECK_GE(x, y)
Definition: Logger.h:206
Definition: sqldefs.h:49
HOST DEVICE void set_type(SQLTypes t)
Definition: sqltypes.h:416
std::shared_ptr< Analyzer::Expr > translateUnaryGeoFunction(const RexFunctionOperator *) const
#define CHECK_GT(x, y)
Definition: Logger.h:205
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:334
void set_compression(EncodingType c)
Definition: sqltypes.h:426
int32_t intval
Definition: sqltypes.h:128
std::string to_string(char const *&&v)
void set_output_srid(int s)
Definition: sqltypes.h:422
HOST DEVICE void set_subtype(SQLTypes st)
Definition: sqltypes.h:417
std::shared_ptr< Analyzer::Expr > translateBinaryGeoFunction(const RexFunctionOperator *) const
static std::shared_ptr< Analyzer::Expr > translateLiteral(const RexLiteral *)
const RelAlgNode * getSourceNode() const
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:327
HOST DEVICE int get_input_srid() const
Definition: sqltypes.h:330
bool has_render_group() const
Definition: sqltypes.h:406
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoFunctionArg(const RexScalar *rex_scalar, SQLTypeInfo &arg_ti, int32_t &lindex, const bool with_bounds, const bool with_render_group, const bool expand_geo_col, const bool is_projection=false) const
std::shared_ptr< Analyzer::Expr > translateGeoConstructor(const RexFunctionOperator *) const
const RexScalar * getOperand(const size_t idx) const
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoLiteral(const RexLiteral *, SQLTypeInfo &, bool) const
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
const std::unordered_map< const RelAlgNode *, int > input_to_nest_level_
#define CHECK_LT(x, y)
Definition: Logger.h:203
Definition: sqltypes.h:55
HOST DEVICE int get_output_srid() const
Definition: sqltypes.h:332
Definition: sqldefs.h:69
unsigned getIndex() const
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoColumn(const RexInput *, SQLTypeInfo &, const bool with_bounds, const bool with_render_group, const bool expand_geo_col) const
static bool getGeoColumns(const std::string &wkt, SQLTypeInfo &ti, std::vector< double > &coords, std::vector< double > &bounds, std::vector< int > &ring_sizes, std::vector< int > &poly_rings, const bool promote_poly_to_mpoly=false)
Definition: geo_types.cpp:459
void set_comp_param(int p)
Definition: sqltypes.h:427
const ColumnDescriptor * getMetadataForColumnBySpi(const int tableId, const size_t spi) const
Definition: Catalog.cpp:1464
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:335
#define CHECK(condition)
Definition: Logger.h:193
const SQLTypeInfo & getType() const
Definition: sqltypes.h:48
SQLTypeInfo columnType
const std::string & getName() const
std::shared_ptr< Analyzer::Expr > translateTernaryGeoFunction(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateInput(const RexInput *) const
#define IS_GEO(T)
Definition: sqltypes.h:167
Definition: sqldefs.h:39
bool has_bounds() const
Definition: sqltypes.h:395
size_t size() const
std::shared_ptr< Analyzer::Expr > fold_expr(const Analyzer::Expr *expr)
double doubleval
Definition: sqltypes.h:131
const Catalog_Namespace::Catalog & cat_
#define IS_GEO_POLY(T)
Definition: sqltypes.h:171