OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RelAlgTranslatorGeo.cpp
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 
17 #include <memory>
18 #include <vector>
19 
20 #include "Geospatial/Compression.h"
21 #include "Geospatial/Types.h"
25 
27 
28 std::vector<std::shared_ptr<Analyzer::Expr>> RelAlgTranslator::translateGeoColumn(
29  const RexInput* rex_input,
30  SQLTypeInfo& ti,
31  const bool with_bounds,
32  const bool expand_geo_col) const {
33  std::vector<std::shared_ptr<Analyzer::Expr>> args;
34  const auto source = rex_input->getSourceNode();
35  const auto it_rte_idx = input_to_nest_level_.find(source);
36  CHECK(it_rte_idx != input_to_nest_level_.end());
37  const int rte_idx = it_rte_idx->second;
38  const auto& in_metainfo = source->getOutputMetainfo();
39 
40  int32_t db_id{0};
41  int32_t table_id{0};
42  int column_id{-1};
43  const Catalog_Namespace::Catalog* catalog{nullptr};
44  const auto scan_source = dynamic_cast<const RelScan*>(source);
45  if (scan_source) {
46  // We're at leaf (scan) level and not supposed to have input metadata,
47  // the name and type information come directly from the catalog.
48  CHECK(in_metainfo.empty());
49 
50  const auto td = scan_source->getTableDescriptor();
51  table_id = td->tableId;
52 
53  catalog = &scan_source->getCatalog();
54  db_id = catalog->getDatabaseId();
55  const auto gcd =
56  catalog->getMetadataForColumnBySpi(table_id, rex_input->getIndex() + 1);
57  CHECK(gcd);
58  ti = gcd->columnType;
59  column_id = gcd->columnId;
60 
61  } else {
62  // Likely backed by a temp table. Read the table ID from the source node and negate it
63  // (see RelAlgTranslator::translateInput)
64  table_id = -source->getId();
65 
66  if (with_bounds) {
67  throw QueryNotSupported(
68  "Geospatial columns not yet supported in intermediate results.");
69  }
70 
71  CHECK(!in_metainfo.empty());
72  CHECK_GE(rte_idx, 0);
73  column_id = rex_input->getIndex();
74  CHECK_LT(static_cast<size_t>(column_id), in_metainfo.size());
75  ti = in_metainfo[column_id].get_type_info();
76  if (expand_geo_col && ti.is_geometry()) {
77  throw QueryNotSupported(
78  "Geospatial columns not yet supported in this temporary table context.");
79  }
80  }
81 
82  if (!IS_GEO(ti.get_type())) {
83  throw QueryNotSupported(
84  "Geospatial expression and operator require geospatial column as their input "
85  "argument(s)");
86  }
87 
88  // Return geo column reference. The geo column may be expanded if required for extension
89  // function arguments. Otherwise, the geo column reference will be translated into
90  // physical columns as required. Bounds column will be added if present and requested.
91  if (expand_geo_col) {
92  for (auto i = 0; i < ti.get_physical_coord_cols(); i++) {
93  CHECK(catalog);
94  const auto pcd = catalog->getMetadataForColumnBySpi(
95  table_id, SPIMAP_GEO_PHYSICAL_INPUT(rex_input->getIndex(), i + 1));
96  auto pcol_ti = pcd->columnType;
97  args.push_back(std::make_shared<Analyzer::ColumnVar>(
98  pcol_ti, shared::ColumnKey{db_id, table_id, pcd->columnId}, rte_idx));
99  }
100  } else {
101  args.push_back(std::make_shared<Analyzer::ColumnVar>(
102  ti, shared::ColumnKey{db_id, table_id, column_id}, rte_idx));
103  }
104  if (with_bounds && ti.has_bounds()) {
105  CHECK(catalog);
106  const auto bounds_cd = catalog->getMetadataForColumnBySpi(
107  table_id,
108  SPIMAP_GEO_PHYSICAL_INPUT(rex_input->getIndex(),
109  ti.get_physical_coord_cols() + 1));
110  auto bounds_ti = bounds_cd->columnType;
111  args.push_back(std::make_shared<Analyzer::ColumnVar>(
112  bounds_ti, shared::ColumnKey{db_id, table_id, bounds_cd->columnId}, rte_idx));
113  }
114  return args;
115 }
116 
117 std::vector<std::shared_ptr<Analyzer::Expr>> RelAlgTranslator::translateGeoLiteral(
118  const RexLiteral* rex_literal,
119  SQLTypeInfo& ti,
120  bool with_bounds) const {
121  CHECK(rex_literal);
122  if (rex_literal->getType() != kTEXT) {
123  throw std::runtime_error("Geo literals must be strings");
124  }
125 
126  // TODO: use geo conversion here
127  const auto e = translateLiteral(rex_literal);
128  auto wkt = std::dynamic_pointer_cast<Analyzer::Constant>(e);
129  CHECK(wkt);
130  std::vector<double> coords;
131  std::vector<double> bounds;
132  std::vector<int> ring_sizes;
133  std::vector<int> poly_rings;
134  int32_t srid = ti.get_output_srid();
135  const bool validate_with_geos_if_available = false;
136  if (!Geospatial::GeoTypesFactory::getGeoColumns(*wkt->get_constval().stringval,
137  ti,
138  coords,
139  bounds,
140  ring_sizes,
141  poly_rings,
142  validate_with_geos_if_available)) {
143  throw QueryNotSupported("Could not read geometry from text");
144  }
146  ti.set_input_srid(srid);
147  ti.set_output_srid(srid);
148  // Compress geo literals by default
149  if (srid == 4326) {
151  ti.set_comp_param(32);
152  }
153 
154  std::vector<std::shared_ptr<Analyzer::Expr>> args;
155 
156  std::vector<uint8_t> compressed_coords = Geospatial::compress_coords(coords, ti);
157  std::list<std::shared_ptr<Analyzer::Expr>> compressed_coords_exprs;
158  for (auto cc : compressed_coords) {
159  Datum d;
160  d.tinyintval = cc;
161  auto e = makeExpr<Analyzer::Constant>(kTINYINT, false, d);
162  compressed_coords_exprs.push_back(e);
163  }
164  SQLTypeInfo arr_ti = SQLTypeInfo(kARRAY, true);
165  arr_ti.set_subtype(kTINYINT);
166  arr_ti.set_size(compressed_coords.size() * sizeof(int8_t));
167  arr_ti.set_compression(ti.get_compression());
168  arr_ti.set_comp_param((ti.get_compression() == kENCODING_GEOINT) ? 32 : 64);
169  args.push_back(makeExpr<Analyzer::Constant>(arr_ti, false, compressed_coords_exprs));
170 
171  auto lit_type = ti.get_type();
172  if (lit_type == kMULTILINESTRING || lit_type == kPOLYGON || lit_type == kMULTIPOLYGON) {
173  // [linest]ring sizes
174  std::list<std::shared_ptr<Analyzer::Expr>> ring_size_exprs;
175  for (auto c : ring_sizes) {
176  Datum d;
177  d.intval = c;
178  auto e = makeExpr<Analyzer::Constant>(kINT, false, d);
179  ring_size_exprs.push_back(e);
180  }
181  SQLTypeInfo arr_ti = SQLTypeInfo(kARRAY, true);
182  arr_ti.set_subtype(kINT);
183  arr_ti.set_size(ring_sizes.size() * sizeof(int32_t));
184  args.push_back(makeExpr<Analyzer::Constant>(arr_ti, false, ring_size_exprs));
185 
186  // poly rings
187  if (lit_type == kMULTIPOLYGON) {
188  std::list<std::shared_ptr<Analyzer::Expr>> poly_rings_exprs;
189  for (auto c : poly_rings) {
190  Datum d;
191  d.intval = c;
192  auto e = makeExpr<Analyzer::Constant>(kINT, false, d);
193  poly_rings_exprs.push_back(e);
194  }
195  SQLTypeInfo arr_ti = SQLTypeInfo(kARRAY, true);
196  arr_ti.set_subtype(kINT);
197  arr_ti.set_size(poly_rings.size() * sizeof(int32_t));
198  args.push_back(makeExpr<Analyzer::Constant>(arr_ti, false, poly_rings_exprs));
199  }
200  }
201 
202  if (with_bounds && ti.has_bounds()) {
203  // bounds
204  std::list<std::shared_ptr<Analyzer::Expr>> bounds_exprs;
205  for (auto b : bounds) {
206  Datum d;
207  d.doubleval = b;
208  auto e = makeExpr<Analyzer::Constant>(kDOUBLE, false, d);
209  bounds_exprs.push_back(e);
210  }
211  SQLTypeInfo arr_ti = SQLTypeInfo(kARRAY, true);
212  arr_ti.set_subtype(kDOUBLE);
213  arr_ti.set_size(bounds.size() * sizeof(double));
214  args.push_back(makeExpr<Analyzer::Constant>(arr_ti, false, bounds_exprs));
215  }
216 
217  return args;
218 }
219 
220 namespace {
221 
222 std::string suffix(SQLTypes type) {
223  if (type == kPOINT) {
224  return std::string("_Point");
225  }
226  if (type == kMULTIPOINT) {
227  return std::string("_MultiPoint");
228  }
229  if (type == kLINESTRING) {
230  return std::string("_LineString");
231  }
232  if (type == kMULTILINESTRING) {
233  return std::string("_MultiLineString");
234  }
235  if (type == kPOLYGON) {
236  return std::string("_Polygon");
237  }
238  if (type == kMULTIPOLYGON) {
239  return std::string("_MultiPolygon");
240  }
241  throw QueryNotSupported("Unsupported argument type");
242 }
243 
245  CHECK(geo);
246  switch (geo->getType()) {
248  return kPOINT;
249  }
251  return kMULTIPOINT;
252  }
254  return kLINESTRING;
255  }
257  return kMULTILINESTRING;
258  }
260  return kPOLYGON;
261  }
263  return kMULTIPOLYGON;
264  }
265  default:
266  UNREACHABLE();
267  return kNULLT;
268  }
269 }
270 
271 } // namespace
272 
273 std::vector<std::shared_ptr<Analyzer::Expr>> RelAlgTranslator::translateGeoFunctionArg(
274  const RexScalar* rex_scalar,
275  SQLTypeInfo& arg_ti,
276  const bool with_bounds,
277  const bool expand_geo_col,
278  const bool is_projection,
279  const bool use_geo_expressions,
280  const bool try_to_compress,
281  const bool allow_gdal_transforms) const {
282  std::vector<std::shared_ptr<Analyzer::Expr>> geoargs;
283 
284  const auto rex_input = dynamic_cast<const RexInput*>(rex_scalar);
285  if (rex_input) {
286  const auto input = translateInput(rex_input);
287  const auto column = dynamic_cast<const Analyzer::ColumnVar*>(input.get());
288  if (!column || !column->get_type_info().is_geometry()) {
289  throw QueryNotSupported("Geo function is expecting a geo column argument");
290  }
291  if (use_geo_expressions) {
292  arg_ti = column->get_type_info();
293  return {makeExpr<Analyzer::GeoColumnVar>(column, with_bounds)};
294  }
295  return translateGeoColumn(rex_input, arg_ti, with_bounds, expand_geo_col);
296  }
297  const auto rex_function = dynamic_cast<const RexFunctionOperator*>(rex_scalar);
298  if (rex_function) {
299  if (rex_function->getName() == "ST_Transform"sv) {
300  CHECK_EQ(size_t(2), rex_function->size());
301  const auto rex_scalar0 =
302  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
303  if (!rex_scalar0) {
304  throw QueryNotSupported(rex_function->getName() + ": unexpected first argument");
305  }
306 
307  const auto rex_literal =
308  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
309  if (!rex_literal) {
310  throw QueryNotSupported(rex_function->getName() +
311  ": second argument is expected to be a literal");
312  }
313  const auto e = translateLiteral(rex_literal);
314  auto ce = std::dynamic_pointer_cast<Analyzer::Constant>(e);
315  if (!ce || !e->get_type_info().is_integer()) {
316  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
317  }
318  int32_t srid = 0;
319  if (e->get_type_info().get_type() == kSMALLINT) {
320  srid = static_cast<int32_t>(ce->get_constval().smallintval);
321  } else if (e->get_type_info().get_type() == kTINYINT) {
322  srid = static_cast<int32_t>(ce->get_constval().tinyintval);
323  } else if (e->get_type_info().get_type() == kINT) {
324  srid = static_cast<int32_t>(ce->get_constval().intval);
325  } else {
326  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
327  }
328  bool allow_result_gdal_transform = false;
329  const auto rex_function0 = dynamic_cast<const RexFunctionOperator*>(rex_scalar0);
330  if (rex_function0 && func_resolve(rex_function0->getName(),
331  "ST_Intersection"sv,
332  "ST_Difference"sv,
333  "ST_Union"sv,
334  "ST_Buffer"sv,
335  "ST_ConcaveHull"sv,
336  "ST_ConvexHull"sv)) {
337  // TODO: the design of geo operators currently doesn't allow input srid overrides.
338  // For example, in case of ST_Area(ST_Transform(ST_Buffer(geo_column,0), 900913))
339  // we can ask geos runtime to transform ST_Buffer's output from 4326 to 900913,
340  // however, ST_Area geo operator would still rely on the first arg's typeinfo
341  // to codegen srid arg values in the ST_Area_ extension function call. And it will
342  // still pick up that transform so the coords will be transformed to 900913 twice.
343 
344  // Sink result transform into geos runtime
345  // allow_result_gdal_transform = true;
346  }
347  if (!allow_gdal_transforms && !allow_result_gdal_transform) {
348  if (srid != 900913 && ((use_geo_expressions || is_projection) && srid != 4326 &&
350  throw QueryNotSupported(rex_function->getName() + ": unsupported output SRID " +
351  std::to_string(srid));
352  }
353  }
354  arg_ti.set_output_srid(srid); // Forward output srid down to argument translation
355  bool arg0_use_geo_expressions = is_projection ? true : use_geo_expressions;
356  if (allow_gdal_transforms) {
357  arg0_use_geo_expressions = false;
358  }
359  auto arg0 = translateGeoFunctionArg(rex_scalar0,
360  arg_ti,
361  with_bounds,
362  expand_geo_col,
363  is_projection,
364  arg0_use_geo_expressions);
365 
366  if (use_geo_expressions) {
367  CHECK_EQ(arg0.size(), size_t(1));
368  auto arg0_ti = arg0.front()->get_type_info(); // make a copy so we can override
369  arg0_ti.set_output_srid(srid);
370  if (arg0_ti.get_type() == kPOINT) {
371  // the output type is going to be fully transformed, so set the input srid to
372  // the output srid
373  const auto input_srid = arg0_ti.get_input_srid();
374  arg0_ti.set_input_srid(srid);
375  // geo transforms projections leave the result decompressed in a register
376  arg0_ti.set_compression(kENCODING_NONE);
377  arg0_ti.set_comp_param(0);
378  // reset recursive arg_ti, as the output type of transform will be properly
379  // transformed to the desired SRID
380  arg_ti.set_output_srid(srid);
381  arg_ti.set_input_srid(srid);
382  return {makeExpr<Analyzer::GeoTransformOperator>(
383  arg0_ti, rex_function->getName(), arg0, input_srid, srid)};
384  } else {
385  if (auto geo_constant =
386  std::dynamic_pointer_cast<Analyzer::GeoConstant>(arg0.front())) {
387  // fold transform
388  auto cast_geo_constant = geo_constant->add_cast(arg0_ti);
389  // update return type info
390  arg_ti = cast_geo_constant->get_type_info();
391  return {cast_geo_constant};
392  } else if (auto col_var =
393  std::dynamic_pointer_cast<Analyzer::ColumnVar>(arg0.front())) {
394  const auto& col_ti = col_var->get_type_info();
395  CHECK(col_ti.is_geometry());
396  if (col_ti.get_type() != kPOINT) {
397  arg_ti.set_input_srid(col_ti.get_input_srid());
398  // fall through to transform code below
399  }
400  } else {
401  if (!allow_gdal_transforms && !allow_result_gdal_transform) {
402  throw std::runtime_error(
403  "Transform on non-POINT geospatial types not yet supported in this "
404  "context.");
405  }
406  }
407  }
408  }
409 
410  if (arg_ti.get_input_srid() > 0) {
411  if (!allow_gdal_transforms && !allow_result_gdal_transform) {
412  if (arg_ti.get_input_srid() != 4326) {
413  throw QueryNotSupported(rex_function->getName() +
414  ": unsupported input SRID " +
415  std::to_string(arg_ti.get_input_srid()));
416  }
417  }
418  // Established that the input SRID is valid
419  if (allow_result_gdal_transform) {
420  // If gdal transform has been allowed, then it has been sunk into geos runtime.
421  // The returning geometry has already been transformed, de-register transform.
422  if (arg_ti.get_input_srid() != srid) {
423  arg_ti.set_input_srid(srid);
424  }
425  }
426  arg_ti.set_output_srid(srid);
427  } else {
428  throw QueryNotSupported(rex_function->getName() +
429  ": unexpected input SRID, unable to transform");
430  }
431  return arg0;
432  } else if (func_resolve(
433  rex_function->getName(), "ST_GeomFromText"sv, "ST_GeogFromText"sv)) {
434  CHECK(rex_function->size() == size_t(1) || rex_function->size() == size_t(2));
435  if (use_geo_expressions) {
436  int32_t srid = 0;
437  if (rex_function->size() == 2) {
438  // user supplied srid
439  const auto rex_literal =
440  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
441  if (!rex_literal) {
442  throw QueryNotSupported(rex_function->getName() +
443  ": second argument is expected to be a literal");
444  }
445  const auto e = translateLiteral(rex_literal);
446  auto ce = std::dynamic_pointer_cast<Analyzer::Constant>(e);
447  if (!ce || !e->get_type_info().is_integer()) {
448  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
449  }
450  if (e->get_type_info().get_type() == kSMALLINT) {
451  srid = static_cast<int32_t>(ce->get_constval().smallintval);
452  } else if (e->get_type_info().get_type() == kTINYINT) {
453  srid = static_cast<int32_t>(ce->get_constval().tinyintval);
454  } else if (e->get_type_info().get_type() == kINT) {
455  srid = static_cast<int32_t>(ce->get_constval().intval);
456  } else {
457  throw QueryNotSupported(rex_function->getName() + " expecting integer SRID");
458  }
459  if (srid != 0 && srid != 4326 && srid != 900913) {
460  throw QueryNotSupported(rex_function->getName() + ": unsupported SRID " +
461  std::to_string(srid));
462  }
463  }
464  arg_ti.set_input_srid(srid); // Input SRID
465  // leave the output srid unset in case a transform was above us
466 
467  if (rex_function->getName() == "ST_GeogFromText"sv) {
468  arg_ti.set_subtype(kGEOGRAPHY);
469  } else {
470  arg_ti.set_subtype(kGEOMETRY);
471  }
472 
473  auto func_args = translateGeoFunctionArg(rex_function->getOperand(0),
474  arg_ti,
475  with_bounds,
476  expand_geo_col,
478  use_geo_expressions);
479  CHECK_GE(func_args.size(), size_t(1));
480  return func_args;
481  }
482 
483  // First - register srid, then send it to geo literal translation
484  int32_t srid = 0;
485  if (rex_function->size() == 2) {
486  const auto rex_literal =
487  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
488  if (!rex_literal) {
489  throw QueryNotSupported(rex_function->getName() +
490  ": second argument is expected to be a literal");
491  }
492  const auto e = translateLiteral(rex_literal);
493  auto ce = std::dynamic_pointer_cast<Analyzer::Constant>(e);
494  if (!ce || !e->get_type_info().is_integer()) {
495  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
496  }
497  if (e->get_type_info().get_type() == kSMALLINT) {
498  srid = static_cast<int32_t>(ce->get_constval().smallintval);
499  } else if (e->get_type_info().get_type() == kTINYINT) {
500  srid = static_cast<int32_t>(ce->get_constval().tinyintval);
501  } else if (e->get_type_info().get_type() == kINT) {
502  srid = static_cast<int32_t>(ce->get_constval().intval);
503  } else {
504  throw QueryNotSupported(rex_function->getName() + " expecting integer SRID");
505  }
506  if (srid != 0 && srid != 4326 && srid != 900913) {
507  throw QueryNotSupported(rex_function->getName() + ": unsupported SRID " +
508  std::to_string(srid));
509  }
510  }
511  arg_ti.set_input_srid(srid); // Input SRID
512  arg_ti.set_output_srid(srid); // Output SRID is the same - no transform
513 
514  const auto rex_literal =
515  dynamic_cast<const RexLiteral*>(rex_function->getOperand(0));
516  if (!rex_literal) {
517  throw QueryNotSupported(rex_function->getName() +
518  " expects a string literal as first argument");
519  }
520  auto arg0 = translateGeoLiteral(rex_literal, arg_ti, with_bounds);
521  arg_ti.set_subtype((rex_function->getName() == "ST_GeogFromText"sv) ? kGEOGRAPHY
522  : kGEOMETRY);
523  return arg0;
524  } else if (rex_function->getName() == "ST_PointN"sv) {
525  // uses geo expressions
526  const auto rex_scalar0 =
527  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
528  if (!rex_scalar0) {
529  throw QueryNotSupported(rex_function->getName() +
530  ": expects scalar as first argument");
531  }
532  auto arg0 = translateGeoFunctionArg(rex_scalar0,
533  arg_ti,
534  with_bounds,
535  expand_geo_col,
536  /*is_projection=*/false,
537  /*use_geo_expressions=*/true);
538  CHECK_EQ(arg0.size(), size_t(1));
539  CHECK(arg0.front());
540  if (arg0.front()->get_type_info().get_type() != kLINESTRING) {
541  throw QueryNotSupported(rex_function->getName() +
542  " expects LINESTRING as first argument");
543  }
544  const auto rex_literal =
545  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
546  if (!rex_literal) {
547  throw QueryNotSupported(rex_function->getName() +
548  ": second argument is expected to be a literal");
549  }
550  const auto e = translateLiteral(rex_literal);
551  if (!e ||
552  !shared::is_any<kSMALLINT, kTINYINT, kINT>(e->get_type_info().get_type())) {
553  throw QueryNotSupported(rex_function->getName() +
554  " expecting integer index as second argument");
555  }
556  arg0.push_back(e);
557  auto oper_ti =
558  arg0.front()->get_type_info(); // make a copy so we can reset nullness and type
559  oper_ti.set_type(kPOINT);
560  oper_ti.set_notnull(false);
561 
562  arg_ti = oper_ti; // TODO: remove
563 
564  return {makeExpr<Analyzer::GeoOperator>(oper_ti, rex_function->getName(), arg0)};
565 
566  } else if (rex_function->getName() == "ST_StartPoint"sv ||
567  rex_function->getName() == "ST_EndPoint"sv) {
568  std::vector<std::shared_ptr<Analyzer::Expr>> args;
569  CHECK_EQ(size_t(1), rex_function->size());
570  const auto arg_exprs = translateGeoFunctionArg(rex_function->getOperand(0),
571  arg_ti,
572  with_bounds,
573  expand_geo_col,
575  /*use_geo_expressions=*/true);
576  CHECK_EQ(arg_exprs.size(), size_t(1));
577  CHECK(arg_exprs.front());
578  const auto arg_expr_ti = arg_exprs.front()->get_type_info();
579  if (arg_expr_ti.get_type() != kLINESTRING) {
580  throw QueryNotSupported(rex_function->getName() +
581  " expected LINESTRING argument. Received " +
582  arg_expr_ti.toString());
583  }
584  args.push_back(arg_exprs.front());
585 
586  auto oper_ti = args.back()->get_type_info(); // make a copy so we can override type
587  oper_ti.set_type(kPOINT);
588 
589  arg_ti = oper_ti; // TODO: remove
590 
591  return {makeExpr<Analyzer::GeoOperator>(oper_ti, rex_function->getName(), args)};
592  } else if (rex_function->getName() == "ST_SRID"sv) {
593  CHECK_EQ(size_t(1), rex_function->size());
594  const auto rex_scalar0 =
595  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
596  if (!rex_scalar0) {
597  throw QueryNotSupported(rex_function->getName() +
598  ": expects scalar as first argument");
599  }
600  auto arg0 =
601  translateGeoFunctionArg(rex_scalar0, arg_ti, with_bounds, expand_geo_col);
602  if (!IS_GEO(arg_ti.get_type())) {
603  throw QueryNotSupported(rex_function->getName() + " expects geometry argument");
604  }
605  return arg0;
606  } else if (rex_function->getName() == "ST_SetSRID"sv) {
607  CHECK_EQ(size_t(2), rex_function->size());
608  const auto rex_literal =
609  dynamic_cast<const RexLiteral*>(rex_function->getOperand(1));
610  if (!rex_literal) {
611  throw QueryNotSupported(rex_function->getName() +
612  ": second argument is expected to be a literal");
613  }
614  const auto e = translateLiteral(rex_literal);
615  auto ce = std::dynamic_pointer_cast<Analyzer::Constant>(e);
616  if (!ce || !e->get_type_info().is_integer()) {
617  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
618  }
619  int32_t srid = 0;
620  if (e->get_type_info().get_type() == kSMALLINT) {
621  srid = static_cast<int32_t>(ce->get_constval().smallintval);
622  } else if (e->get_type_info().get_type() == kTINYINT) {
623  srid = static_cast<int32_t>(ce->get_constval().tinyintval);
624  } else if (e->get_type_info().get_type() == kINT) {
625  srid = static_cast<int32_t>(ce->get_constval().intval);
626  } else {
627  throw QueryNotSupported(rex_function->getName() + ": expecting integer SRID");
628  }
629 
630  const auto rex_scalar0 =
631  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
632  if (!rex_scalar0) {
633  throw QueryNotSupported(rex_function->getName() +
634  ": expects scalar as first argument");
635  }
636 
637  // Only convey the request to compress if dealing with 4326 geo
638  auto arg0 = translateGeoFunctionArg(rex_scalar0,
639  arg_ti,
640  with_bounds,
641  expand_geo_col,
642  is_projection,
643  use_geo_expressions,
644  (try_to_compress && (srid == 4326)));
645 
646  CHECK(!arg0.empty() && arg0.front());
647  if (!IS_GEO(arg_ti.get_type()) && !use_geo_expressions) {
648  throw QueryNotSupported(rex_function->getName() + " expects geometry argument");
649  }
650  arg_ti.set_input_srid(srid); // Input SRID
651  arg_ti.set_output_srid(srid); // Output SRID is the same - no transform
652  if (auto geo_expr = std::dynamic_pointer_cast<Analyzer::GeoExpr>(arg0.front())) {
653  CHECK_EQ(arg0.size(), size_t(1));
654  auto ti = geo_expr->get_type_info();
655  ti.set_input_srid(srid);
656  ti.set_output_srid(srid);
657  return {geo_expr->add_cast(ti)};
658  }
659  return arg0;
660  } else if (rex_function->getName() == "CastToGeography"sv) {
661  CHECK_EQ(size_t(1), rex_function->size());
662  const auto rex_scalar0 =
663  dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
664  if (!rex_scalar0) {
665  throw QueryNotSupported(rex_function->getName() +
666  ": expects scalar as first argument");
667  }
668  auto arg0 = translateGeoFunctionArg(rex_scalar0,
669  arg_ti,
670  with_bounds,
671  expand_geo_col,
672  /*is_projection=*/false,
673  use_geo_expressions);
674  CHECK(!arg0.empty());
675  if (auto geo_expr = std::dynamic_pointer_cast<Analyzer::GeoExpr>(arg0.front())) {
676  auto arg_ti = geo_expr->get_type_info(); // make a copy
677  arg_ti.set_subtype(kGEOGRAPHY);
678  return {geo_expr->add_cast(arg_ti)};
679  }
680  if (use_geo_expressions) {
681  arg_ti = arg0.front()->get_type_info();
682  arg_ti.set_subtype(kGEOGRAPHY);
683  arg0.front()->set_type_info(arg_ti);
684  }
685  if (!IS_GEO(arg_ti.get_type())) {
686  throw QueryNotSupported(rex_function->getName() + " expects geometry argument");
687  }
688  if (arg_ti.get_output_srid() != 4326) {
689  throw QueryNotSupported(rex_function->getName() +
690  " expects geometry with SRID=4326");
691  }
692  arg_ti.set_subtype(kGEOGRAPHY);
693  return arg0;
694  } else if (rex_function->getName() == "ST_Point"sv) {
695  CHECK_EQ(size_t(2), rex_function->size());
696  arg_ti.set_type(kPOINT);
697  arg_ti.set_subtype(kGEOMETRY);
698  arg_ti.set_input_srid(0);
699  arg_ti.set_output_srid(0);
701 
702  auto coord1 = translateScalarRex(rex_function->getOperand(0));
703  auto coord2 = translateScalarRex(rex_function->getOperand(1));
704  auto d_ti = SQLTypeInfo(kDOUBLE, false);
705  auto cast_coord1 = coord1->add_cast(d_ti);
706  auto cast_coord2 = coord2->add_cast(d_ti);
707  // First try to fold to geo literal
708  auto folded_coord1 = fold_expr(cast_coord1.get());
709  auto folded_coord2 = fold_expr(cast_coord2.get());
710  auto const_coord1 = std::dynamic_pointer_cast<Analyzer::Constant>(folded_coord1);
711  auto const_coord2 = std::dynamic_pointer_cast<Analyzer::Constant>(folded_coord2);
712  if (const_coord1 && const_coord2 && !use_geo_expressions) {
713  CHECK(const_coord1->get_type_info().get_type() == kDOUBLE);
714  CHECK(const_coord2->get_type_info().get_type() == kDOUBLE);
715  std::string wkt = "POINT(" +
716  std::to_string(const_coord1->get_constval().doubleval) + " " +
717  std::to_string(const_coord2->get_constval().doubleval) + ")";
718  RexLiteral rex_literal{wkt, kTEXT, kNULLT, 0, 0, 0, 0};
719  auto args = translateGeoLiteral(&rex_literal, arg_ti, false);
720  CHECK(arg_ti.get_type() == kPOINT);
721  return args;
722  }
723  const auto is_local_alloca = !is_projection;
724  if (!is_local_alloca || use_geo_expressions) {
725  if (try_to_compress) {
726  arg_ti.set_input_srid(4326);
727  arg_ti.set_output_srid(4326);
728  }
729  return {makeExpr<Analyzer::GeoOperator>(
730  arg_ti,
731  rex_function->getName(),
732  std::vector<std::shared_ptr<Analyzer::Expr>>{folded_coord1, folded_coord2})};
733  }
734  // Couldn't fold to geo literal, construct [and compress] on the fly
735  auto da_ti = SQLTypeInfo(kARRAY, true);
736  da_ti.set_subtype(kDOUBLE);
737  da_ti.set_size(16);
738  if (try_to_compress) {
739  // Switch to compressed coord array
740  da_ti.set_subtype(kINT);
741  da_ti.set_size(8);
742  da_ti.set_input_srid(4326);
743  da_ti.set_output_srid(4326);
744  da_ti.set_compression(kENCODING_GEOINT);
745  da_ti.set_comp_param(32);
746  // Register point compression
747  arg_ti.set_input_srid(4326);
748  arg_ti.set_output_srid(4326);
750  arg_ti.set_comp_param(32);
751  }
752  auto cast_coords = {folded_coord1, folded_coord2};
753  auto ae = makeExpr<Analyzer::ArrayExpr>(da_ti, cast_coords, false, is_local_alloca);
754  SQLTypeInfo tia_ti = da_ti;
755  tia_ti.set_subtype(kTINYINT);
756  return {makeExpr<Analyzer::UOper>(tia_ti, false, kCAST, ae)};
757  } else if (rex_function->getName() == "ST_Centroid"sv) {
758  CHECK_EQ(size_t(1), rex_function->size());
759  arg_ti.set_type(kPOINT);
760  arg_ti.set_subtype(kGEOMETRY);
761  arg_ti.set_input_srid(0);
762  arg_ti.set_output_srid(0);
764 
765  SQLTypeInfo geo_ti;
766  int legacy_transform_srid = 0; // discard
767  auto geoargs = translateGeoFunctionArg(rex_function->getOperand(0),
768  geo_ti,
769  /*with_bounds=*/false,
770  /*expand_geo_col=*/true,
771  /*is_projection=*/false,
772  /*use_geo_expressions=*/true);
773  CHECK_EQ(geoargs.size(), size_t(1));
774  if (geo_ti.get_output_srid() > 0) {
775  // Pick up the arg's srid
776  arg_ti.set_input_srid(geo_ti.get_output_srid());
777  arg_ti.set_output_srid(geo_ti.get_output_srid());
778  }
779  if (try_to_compress) {
780  // Point compression is requested by a higher level [4326] operation
781  if (geo_ti.get_output_srid() == 0) {
782  // srid-less geo is considered and is forced to be 4326
783  arg_ti.set_input_srid(4326);
784  arg_ti.set_output_srid(4326);
785  } else {
786  CHECK_EQ(arg_ti.get_output_srid(), 4326);
787  }
789  arg_ti.set_comp_param(32);
790  }
791  if (geo_ti.get_input_srid() != geo_ti.get_output_srid() &&
792  geo_ti.get_output_srid() > 0 &&
793  std::dynamic_pointer_cast<Analyzer::ColumnVar>(geoargs.front())) {
794  // Centroid argument is transformed before use,
795  // pass the transform to the geo operator
796  legacy_transform_srid = geo_ti.get_output_srid();
797  }
798  return {makeExpr<Analyzer::GeoOperator>(
799  arg_ti,
800  rex_function->getName(),
801  std::vector<std::shared_ptr<Analyzer::Expr>>{geoargs.front()},
802  legacy_transform_srid > 0 ? std::make_optional<int>(legacy_transform_srid)
803  : std::nullopt)};
804  } else if (func_resolve(rex_function->getName(), "ST_ConvexHull"sv)) {
805  CHECK_EQ(size_t(1), rex_function->size());
806  // What geo type will the constructor return? Could be anything.
807  return {translateUnaryGeoConstructor(rex_function, arg_ti, with_bounds)};
808  } else if (func_resolve(rex_function->getName(),
809  "ST_Intersection"sv,
810  "ST_Difference"sv,
811  "ST_Union"sv,
812  "ST_Buffer"sv,
813  "ST_ConcaveHull"sv)) {
814  CHECK_EQ(size_t(2), rex_function->size());
815  // What geo type will the constructor return? Could be anything.
816  return {translateBinaryGeoConstructor(rex_function, arg_ti, with_bounds)};
817  } else if (func_resolve(rex_function->getName(), "ST_IsEmpty"sv, "ST_IsValid"sv)) {
818  CHECK_EQ(size_t(1), rex_function->size());
819  return {translateUnaryGeoPredicate(rex_function, arg_ti, with_bounds)};
820  } else if (func_resolve(rex_function->getName(), "ST_Equals"sv)) {
821  CHECK_EQ(size_t(2), rex_function->size());
822  return {translateBinaryGeoPredicate(rex_function, arg_ti, with_bounds)};
823  } else {
824  throw QueryNotSupported("Unsupported argument: " + rex_function->getName());
825  }
826  }
827  const auto rex_literal = dynamic_cast<const RexLiteral*>(rex_scalar);
828  if (rex_literal) {
829  if (use_geo_expressions) {
830  const auto translated_literal = translateLiteral(rex_literal);
831  auto const translated_literal_type = translated_literal->get_type_info().get_type();
832  if (!IS_STRING(translated_literal_type) && !IS_GEO(translated_literal_type)) {
833  // This stops crashes in the createGeoType call below due to datum.stringval
834  // being uninitialized when the datum isn't even a string, let alone a geo string
835  // There needs to be specific handling for ST_NumGeometries in the code above
836  // but I don't know what category it would fall over (it's not GEOS, and it
837  // returns an INT, not a BOOL or other geo)
838  // simon.eves 8/15/22
839  throw QueryNotSupported("Geospatial function requires geo literal.");
840  }
841  const auto constant_expr =
842  dynamic_cast<const Analyzer::Constant*>(translated_literal.get());
843  CHECK(constant_expr);
844  if (constant_expr->get_is_null()) {
845  // TODO: we could lift this limitation by assuming a minimum type per function
846  throw QueryNotSupported("Geospatial functions require typed nulls.");
847  }
848  const auto& datum = constant_expr->get_constval();
849  CHECK(datum.stringval);
850  const bool validate_with_geos_if_available = false;
851  auto geospatial_base = Geospatial::GeoTypesFactory::createGeoType(
852  *datum.stringval, validate_with_geos_if_available);
853  CHECK(geospatial_base);
854  SQLTypeInfo ti;
855  ti.set_type(get_ti_from_geo(geospatial_base.get()));
856  if (arg_ti.get_subtype() == kGEOGRAPHY) {
858  } else {
860  }
861  ti.set_input_srid(arg_ti.get_input_srid());
862  ti.set_output_srid(arg_ti.get_output_srid() == 0 ? arg_ti.get_input_srid()
863  : arg_ti.get_output_srid());
864  // TODO: remove dependence on arg_ti
865  if (ti.get_output_srid() == 4326 || arg_ti.get_compression() == kENCODING_GEOINT) {
867  ti.set_comp_param(32);
868  }
869  ti.set_notnull(true);
870  // Before removing dependence on arg_ti need to note that ST_Transform uses it
871  // as a vehicle to pass transform output SRID to its args.
872  // arg_ti is also expected to be filled with relevant data, which wasn't done here.
873  // Not filling arg_ti with the geo constant data (which went to ti instead)
874  // resulted in GeoConstant::add_cast adopting a corrupt type info,
875  // which later killed codegen. Need to complete arg_ti composition:
876  arg_ti = ti;
877  return {makeExpr<Analyzer::GeoConstant>(std::move(geospatial_base), ti)};
878  }
879  return translateGeoLiteral(rex_literal, arg_ti, with_bounds);
880  }
881  throw QueryNotSupported("Geo function argument not supported");
882 }
883 
884 std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateGeoProjection(
885  const RexFunctionOperator* rex_function,
886  SQLTypeInfo& ti,
887  const bool with_bounds) const {
888  // note that this is a bit of a misnomer, as ST_SetSRID embedded in a transform will
889  // eventually use geo expressions -- just not here
890  const bool use_geo_projections = !(rex_function->getName() == "ST_GeomFromText" ||
891  rex_function->getName() == "ST_GeogFromText" ||
892  rex_function->getName() == "ST_SetSRID");
893  auto geoargs = translateGeoFunctionArg(rex_function,
894  ti,
895  /*with_bounds=*/false,
896  /*expand_geo_col=*/true,
897  /*is_projection=*/true,
898  /*use_geo_expressions=*/use_geo_projections);
899  CHECK(!geoargs.empty());
900  if (std::dynamic_pointer_cast<const Analyzer::GeoExpr>(geoargs.front()) &&
901  !geoargs.front()->get_type_info().is_array()) {
902  if (rex_function->getName() == "ST_Transform" &&
903  std::dynamic_pointer_cast<const Analyzer::GeoConstant>(geoargs.front())) {
904  return makeExpr<Analyzer::GeoUOper>(
905  Geospatial::GeoBase::GeoOp::kPROJECTION, ti, ti, geoargs);
906  }
907  // GeoExpression
908  return geoargs.front();
909  }
910  bool allow_gdal_transform = false;
911  if (rex_function->getName() == "ST_Transform") {
912  const auto rex_scalar0 = dynamic_cast<const RexScalar*>(rex_function->getOperand(0));
913  const auto rex_function0 = dynamic_cast<const RexFunctionOperator*>(rex_scalar0);
914  if (rex_function0 && func_resolve(rex_function0->getName(),
915  "ST_Intersection"sv,
916  "ST_Difference"sv,
917  "ST_Union"sv,
918  "ST_Buffer"sv,
919  "ST_ConcaveHull"sv,
920  "ST_ConvexHull"sv)) {
921  // Allow projection of gdal-transformed geos outputs
922  allow_gdal_transform = true;
923  }
924  }
925  if (use_geo_projections && !allow_gdal_transform) {
926  throw std::runtime_error("Geospatial projection for function " +
927  rex_function->toString(RelRexToStringConfig::defaults()) +
928  " not yet supported in this context");
929  }
930  return makeExpr<Analyzer::GeoUOper>(
931  Geospatial::GeoBase::GeoOp::kPROJECTION, ti, ti, geoargs);
932 }
933 
934 std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateBinaryGeoConstructor(
935  const RexFunctionOperator* rex_function,
936  SQLTypeInfo& ti,
937  const bool with_bounds) const {
938 #ifndef ENABLE_GEOS
939  throw QueryNotSupported(rex_function->getName() +
940  " geo constructor requires enabled GEOS support");
941 #endif
943  if (rex_function->getName() == "ST_Difference"sv) {
945  } else if (rex_function->getName() == "ST_Union"sv) {
947  } else if (rex_function->getName() == "ST_Buffer"sv) {
949  } else if (rex_function->getName() == "ST_ConcaveHull"sv) {
951  }
952 
955  SQLTypeInfo arg0_ti;
956  SQLTypeInfo arg1_ti;
957  if (func_resolve(rex_function->getName(),
958  "ST_Intersection"sv,
959  "ST_Difference"sv,
960  "ST_Union"sv,
961  "ST_Buffer"sv,
962  "ST_ConcaveHull"sv)) {
963  // First arg: geometry
964  geoargs0 = translateGeoFunctionArg(rex_function->getOperand(0),
965  arg0_ti,
966  false,
967  true,
968  true,
969  false,
970  false,
971  /* allow_gdal_transforms = */ true);
972  }
973  if (func_resolve(rex_function->getName(),
974  "ST_Intersection"sv,
975  "ST_Difference"sv,
976  "ST_Union"sv)) {
977  // Second arg: geometry
978  geoargs1 = translateGeoFunctionArg(rex_function->getOperand(1),
979  arg1_ti,
980  false,
981  true,
982  true,
983  false,
984  false,
985  /* allow_gdal_transforms = */ true);
986  if (arg0_ti.get_output_srid() != arg1_ti.get_output_srid()) {
987  throw QueryNotSupported(rex_function->getName() +
988  " geo constructor requires arguments with matching srids");
989  }
990  } else if (func_resolve(rex_function->getName(), "ST_Buffer"sv, "ST_ConcaveHull"sv)) {
991  // Second arg: double scalar
992  auto param_expr = translateScalarRex(rex_function->getOperand(1));
993  arg1_ti = SQLTypeInfo(kDOUBLE, false);
994  if (param_expr->get_type_info().get_type() != kDOUBLE) {
995  param_expr = param_expr->add_cast(arg1_ti);
996  }
997  geoargs1 = {param_expr};
998  }
999 
1000  // Record the optional transform request that can be sent by an ecompassing TRANSFORM
1001  auto srid = ti.get_output_srid();
1002  // Build the typeinfo of the constructed geometry
1003  SQLTypeInfo arg_ti = arg0_ti;
1004  arg_ti.set_type(kMULTIPOLYGON);
1005  arg_ti.set_subtype(kGEOMETRY);
1006  arg_ti.set_compression(kENCODING_NONE); // Constructed geometries are not compressed
1007  arg_ti.set_comp_param(0);
1008  arg_ti.set_input_srid(arg0_ti.get_output_srid());
1009  if (srid > 0) {
1010  if (arg_ti.get_input_srid() > 0) {
1011  // Constructed geometry to be transformed to srid given by encompassing transform
1012  arg_ti.set_output_srid(srid);
1013  } else {
1014  throw QueryNotSupported("Transform of geo constructor " + rex_function->getName() +
1015  " requires its argument(s) to have a valid srid");
1016  }
1017  } else {
1018  arg_ti.set_output_srid(arg_ti.get_input_srid()); // No encompassing transform
1019  }
1020  // If there was an output transform, it's now embedded into arg_ti and the geo operator.
1021  // Now de-register the transform from the return typeinfo:
1022  ti = arg_ti;
1024  return makeExpr<Analyzer::GeoBinOper>(op, arg_ti, arg0_ti, arg1_ti, geoargs0, geoargs1);
1025 }
1026 
1027 std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateUnaryGeoPredicate(
1028  const RexFunctionOperator* rex_function,
1029  SQLTypeInfo& ti,
1030  const bool with_bounds) const {
1031 #ifndef ENABLE_GEOS
1032  throw QueryNotSupported(rex_function->getName() +
1033  " geo predicate requires enabled GEOS support");
1034 #endif
1035  SQLTypeInfo arg_ti;
1036  auto geoargs =
1037  translateGeoFunctionArg(rex_function->getOperand(0), arg_ti, false, true, true);
1038  ti = SQLTypeInfo(kBOOLEAN, false);
1039  auto op = (rex_function->getName() == "ST_IsEmpty"sv)
1042  return makeExpr<Analyzer::GeoUOper>(op, ti, arg_ti, geoargs);
1043 }
1044 
1045 std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateBinaryGeoPredicate(
1046  const RexFunctionOperator* rex_function,
1047  SQLTypeInfo& ti,
1048  const bool with_bounds) const {
1049  if (rex_function->getName() != "ST_Equals"sv) {
1050  throw QueryNotSupported(rex_function->getName() + " geo predicate is not supported");
1051  }
1052 #ifndef ENABLE_GEOS
1053  throw QueryNotSupported(rex_function->getName() +
1054  " geo predicate requires enabled GEOS support");
1055 #endif
1056  SQLTypeInfo arg0_ti;
1057  auto geoargs0 =
1058  translateGeoFunctionArg(rex_function->getOperand(0), arg0_ti, false, true, true);
1059  SQLTypeInfo arg1_ti;
1060  auto geoargs1 =
1061  translateGeoFunctionArg(rex_function->getOperand(1), arg1_ti, false, true, true);
1062  ti = SQLTypeInfo(kBOOLEAN, false);
1064  return makeExpr<Analyzer::GeoBinOper>(op, ti, arg0_ti, arg1_ti, geoargs0, geoargs1);
1065 }
1066 
1067 std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateUnaryGeoConstructor(
1068  const RexFunctionOperator* rex_function,
1069  SQLTypeInfo& ti,
1070  const bool with_bounds) const {
1071 #ifndef ENABLE_GEOS
1072  throw QueryNotSupported(rex_function->getName() +
1073  " geo constructor requires enabled GEOS support");
1074 #endif
1076 
1077  Analyzer::ExpressionPtrVector geoargs0{};
1078  SQLTypeInfo arg0_ti;
1079  if (func_resolve(rex_function->getName(), "ST_ConvexHull"sv)) {
1080  // First arg: geometry
1081  geoargs0 = translateGeoFunctionArg(rex_function->getOperand(0),
1082  arg0_ti,
1083  false,
1084  true,
1085  true,
1086  false,
1087  false,
1088  /* allow_gdal_transforms = */ true);
1089  }
1090 
1091  // Record the optional transform request that can be sent by an ecompassing TRANSFORM
1092  auto srid = ti.get_output_srid();
1093  // Build the typeinfo of the constructed geometry
1094  SQLTypeInfo arg_ti = arg0_ti;
1095  arg_ti.set_type(kMULTIPOLYGON);
1096  arg_ti.set_subtype(kGEOMETRY);
1097  arg_ti.set_compression(kENCODING_NONE); // Constructed geometries are not compressed
1098  arg_ti.set_comp_param(0);
1099  arg_ti.set_input_srid(arg0_ti.get_output_srid());
1100  if (srid > 0) {
1101  if (arg_ti.get_input_srid() > 0) {
1102  // Constructed geometry to be transformed to srid given by encompassing transform
1103  arg_ti.set_output_srid(srid);
1104  } else {
1105  throw QueryNotSupported("Transform of geo constructor " + rex_function->getName() +
1106  " requires its argument(s) to have a valid srid");
1107  }
1108  } else {
1109  arg_ti.set_output_srid(arg_ti.get_input_srid()); // No encompassing transform
1110  }
1111  // If there was an output transform, it's now embedded into arg_ti and the geo operator.
1112  // Now de-register the transform from the return typeinfo:
1113  ti = arg_ti;
1115  return makeExpr<Analyzer::GeoUOper>(op, arg_ti, arg0_ti, geoargs0);
1116 }
1117 
1118 std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateUnaryGeoFunction(
1119  const RexFunctionOperator* rex_function) const {
1120  CHECK_EQ(size_t(1), rex_function->size());
1121 
1122  std::string specialized_geofunc{rex_function->getName()};
1123 
1124  // Geo function calls which do not need the coords col but do need cols associated
1125  // with physical coords (e.g. ring_sizes / poly_rings)
1126  if (rex_function->getName() == "ST_NRings"sv) {
1127  SQLTypeInfo arg_ti;
1128  auto geoargs = translateGeoFunctionArg(rex_function->getOperand(0),
1129  arg_ti,
1130  /*with_bounds=*/false,
1131  /*expand_geo_col=*/true,
1132  /*is_projection=*/false,
1133  /*use_geo_expressions=*/true);
1134  if (!IS_GEO_POLY(arg_ti.get_type())) {
1135  throw QueryNotSupported(rex_function->getName() +
1136  " expects a POLYGON or MULTIPOLYGON");
1137  }
1138  CHECK_EQ(geoargs.size(), size_t(1));
1139  arg_ti = rex_function->getType(); // TODO: remove
1140  return makeExpr<Analyzer::GeoOperator>(
1141  rex_function->getType(),
1142  rex_function->getName(),
1143  std::vector<std::shared_ptr<Analyzer::Expr>>{geoargs.front()});
1144  } else if (rex_function->getName() == "ST_NumGeometries"sv) {
1145  SQLTypeInfo arg_ti;
1146  auto geoargs = translateGeoFunctionArg(rex_function->getOperand(0),
1147  arg_ti,
1148  /*with_bounds=*/false,
1149  /*expand_geo_col=*/true,
1150  /*is_projection=*/false,
1151  /*use_geo_expressions=*/true);
1152  if (!IS_GEO(arg_ti.get_type())) {
1153  throw QueryNotSupported(rex_function->getName() + " expects a geo parameter");
1154  }
1155  CHECK_EQ(geoargs.size(), size_t(1));
1156  arg_ti = rex_function->getType(); // TODO: remove
1157  return makeExpr<Analyzer::GeoOperator>(
1158  rex_function->getType(),
1159  rex_function->getName(),
1160  std::vector<std::shared_ptr<Analyzer::Expr>>{geoargs.front()});
1161  } else if (rex_function->getName() == "ST_NPoints"sv) {
1162  SQLTypeInfo arg_ti;
1163  auto geoargs = translateGeoFunctionArg(rex_function->getOperand(0),
1164  arg_ti,
1165  /*with_bounds=*/false,
1166  /*expand_geo_col=*/true,
1167  /*is_projection=*/false,
1168  /*use_geo_expressions=*/true);
1169  CHECK_EQ(geoargs.size(), size_t(1));
1170  auto expr_ti = rex_function->getType();
1171  expr_ti.set_notnull(arg_ti.get_notnull());
1172  return makeExpr<Analyzer::GeoOperator>(
1173  expr_ti,
1174  rex_function->getName(),
1175  std::vector<std::shared_ptr<Analyzer::Expr>>{geoargs.front()});
1176  } else if (func_resolve(rex_function->getName(), "ST_Perimeter"sv, "ST_Area"sv)) {
1177  SQLTypeInfo arg_ti;
1178  int legacy_transform_srid = 0; // discard
1179  auto geoargs = translateGeoFunctionArg(rex_function->getOperand(0),
1180  arg_ti,
1181  /*with_bounds=*/false,
1182  /*expand_geo_col=*/true,
1183  /*is_projection=*/false,
1184  /*use_geo_expressions=*/true);
1185  CHECK_EQ(geoargs.size(), size_t(1));
1186  if (arg_ti.get_input_srid() != arg_ti.get_output_srid() &&
1187  arg_ti.get_output_srid() > 0 &&
1188  std::dynamic_pointer_cast<Analyzer::ColumnVar>(geoargs.front())) {
1189  // legacy transform
1190  legacy_transform_srid = arg_ti.get_output_srid();
1191  // Reset the transform, transform will be given to the operator as an override
1192  arg_ti = geoargs.front()->get_type_info();
1193  }
1194  if (!IS_GEO_POLY(arg_ti.get_type())) {
1195  throw QueryNotSupported(rex_function->getName() +
1196  " expects a POLYGON or MULTIPOLYGON");
1197  }
1198  return makeExpr<Analyzer::GeoOperator>(
1199  rex_function->getType(),
1200  rex_function->getName(),
1201  std::vector<std::shared_ptr<Analyzer::Expr>>{geoargs.front()},
1202  legacy_transform_srid > 0 ? std::make_optional<int>(legacy_transform_srid)
1203  : std::nullopt);
1204  }
1205 
1206  // Accessor for poly bounds for in-situ poly render queries
1207  if (func_resolve(rex_function->getName(), "HeavyDB_Geo_PolyBoundsPtr"sv)) {
1208  SQLTypeInfo arg_ti;
1209  // get geo column plus bounds only (not expanded)
1210  auto geoargs =
1211  translateGeoFunctionArg(rex_function->getOperand(0), arg_ti, true, false, false);
1212  // this function only works on polys
1213  if (!IS_GEO_POLY(arg_ti.get_type())) {
1214  throw QueryNotSupported(rex_function->getName() +
1215  " expects a POLYGON or MULTIPOLYGON");
1216  }
1217  // only need the bounds argument (last), discard the rest
1218  geoargs.erase(geoargs.begin(), geoargs.end() - 1);
1219  // done
1220  return makeExpr<Analyzer::FunctionOper>(
1221  rex_function->getType(), specialized_geofunc, geoargs);
1222  }
1223 
1224  // start to move geo expressions above the generic translation call, as geo expression
1225  // error handling can differ
1226  if (func_resolve(rex_function->getName(), "ST_X"sv, "ST_Y"sv)) {
1227  SQLTypeInfo arg_ti;
1228  auto new_geoargs = translateGeoFunctionArg(rex_function->getOperand(0),
1229  arg_ti,
1230  /*with_bounds=*/false,
1231  /*expand_geo_col=*/true,
1232  /*is_projection=*/true,
1233  /*use_geo_expressions=*/true);
1234  CHECK_EQ(new_geoargs.size(), size_t(1));
1235  CHECK(new_geoargs.front());
1236  const auto& arg_expr_ti = new_geoargs.front()->get_type_info();
1237  if (arg_expr_ti.get_type() != kPOINT) {
1238  throw QueryNotSupported(rex_function->getName() + " expects a POINT");
1239  }
1240  auto function_ti = rex_function->getType();
1241  if (std::dynamic_pointer_cast<Analyzer::GeoOperator>(new_geoargs.front())) {
1242  function_ti.set_notnull(false);
1243  }
1244  if (std::dynamic_pointer_cast<Analyzer::GeoConstant>(new_geoargs.front())) {
1245  // TODO(adb): fixup null handling
1246  function_ti.set_notnull(true);
1247  }
1248  return makeExpr<Analyzer::GeoOperator>(
1249  function_ti,
1250  rex_function->getName(),
1251  std::vector<std::shared_ptr<Analyzer::Expr>>{new_geoargs.front()});
1252  }
1253 
1254  // All functions below use geo col as reference and expand it as necessary
1255  SQLTypeInfo arg_ti;
1256  bool with_bounds = true;
1257  auto geoargs =
1258  translateGeoFunctionArg(rex_function->getOperand(0), arg_ti, with_bounds, false);
1259 
1260  if (rex_function->getName() == "ST_SRID"sv) {
1261  Datum output_srid;
1262  output_srid.intval = arg_ti.get_output_srid();
1263  return makeExpr<Analyzer::Constant>(kINT, false, output_srid);
1264  }
1265 
1266  if (func_resolve(
1267  rex_function->getName(), "ST_XMin"sv, "ST_YMin"sv, "ST_XMax"sv, "ST_YMax"sv)) {
1268  // If type has bounds - use them, otherwise look at coords
1269  if (arg_ti.has_bounds()) {
1270  // Only need the bounds argument, discard the rest
1271  geoargs.erase(geoargs.begin(), geoargs.end() - 1);
1272 
1273  // Supply srids too - transformed geo would have a transformed bounding box
1274  Datum input_srid;
1275  input_srid.intval = arg_ti.get_input_srid();
1276  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid));
1277  Datum output_srid;
1278  output_srid.intval = arg_ti.get_output_srid();
1279  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, output_srid));
1280 
1281  specialized_geofunc += "_Bounds"s;
1282  return makeExpr<Analyzer::FunctionOper>(
1283  rex_function->getType(), specialized_geofunc, geoargs);
1284  }
1285  }
1286 
1287  // Unless overriden, function is assumed to be interested in the first geoarg only,
1288  // which may be a geo object (e.g. geo column), or a coord array (e.g. geo literal)
1289  auto discard_after_arg = 1;
1290 
1291  if (rex_function->getName() == "ST_Length"sv) {
1292  if (arg_ti.get_type() != kLINESTRING && arg_ti.get_type() != kMULTILINESTRING) {
1293  throw QueryNotSupported(rex_function->getName() +
1294  " expects LINESTRING or MULTILINESTRING");
1295  }
1296  if (arg_ti.get_type() == kMULTILINESTRING) {
1297  auto ti0 = geoargs[0]->get_type_info();
1298  if (ti0.get_type() == kARRAY && ti0.get_subtype() == kTINYINT) {
1299  // Received expanded geo: widen the reach to grab linestring size array as well
1300  discard_after_arg = 2;
1301  }
1302  }
1303  specialized_geofunc += suffix(arg_ti.get_type());
1304  if (arg_ti.get_subtype() == kGEOGRAPHY && arg_ti.get_output_srid() == 4326) {
1305  if (arg_ti.get_type() == kMULTILINESTRING) {
1306  throw QueryNotSupported(rex_function->getName() +
1307  " Geodesic is not supported for MULTILINESTRING");
1308  }
1309  specialized_geofunc += "_Geodesic"s;
1310  }
1311  }
1312 
1313  geoargs.erase(geoargs.begin() + discard_after_arg, geoargs.end());
1314 
1315  // Add input compression mode and SRID args to enable on-the-fly
1316  // decompression/transforms
1317  Datum input_compression;
1318  input_compression.intval = Geospatial::get_compression_scheme(arg_ti);
1319  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
1320  Datum input_srid;
1321  input_srid.intval = arg_ti.get_input_srid();
1322  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid));
1323 
1324  // Add output SRID arg to enable on-the-fly transforms
1325  Datum output_srid;
1326  output_srid.intval = arg_ti.get_output_srid();
1327  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, output_srid));
1328 
1329  return makeExpr<Analyzer::FunctionOper>(
1330  rex_function->getType(), specialized_geofunc, geoargs);
1331 }
1332 
1333 std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateBinaryGeoFunction(
1334  const RexFunctionOperator* rex_function) const {
1335  auto function_name = rex_function->getName();
1336  auto return_type = rex_function->getType();
1337 
1338  if (function_name == "ST_IntersectsBox"sv) {
1339  // Bounding box intersection is the only implementation supported for now, only
1340  // translate bounds
1341  CHECK_EQ(size_t(2), rex_function->size());
1342  auto extract_geo_bounds_from_input =
1343  [this, &rex_function](const size_t index) -> std::shared_ptr<Analyzer::Expr> {
1344  const auto rex_input =
1345  dynamic_cast<const RexInput*>(rex_function->getOperand(index));
1346  if (rex_input) {
1347  SQLTypeInfo ti;
1348  const auto exprs = translateGeoColumn(rex_input, ti, true, false);
1349  CHECK_GT(exprs.size(), size_t(0));
1350  if (ti.get_type() == kPOINT) {
1351  throw std::runtime_error(
1352  "ST_IntersectsBox is not supported for point arguments.");
1353  } else {
1354  return exprs.back();
1355  }
1356  } else {
1357  throw std::runtime_error(
1358  "Only inputs are supported as arguments to ST_IntersectsBox for now.");
1359  }
1360  };
1361  std::vector<std::shared_ptr<Analyzer::Expr>> geo_args;
1362  geo_args.push_back(extract_geo_bounds_from_input(0));
1363  geo_args.push_back(extract_geo_bounds_from_input(1));
1364 
1365  return makeExpr<Analyzer::FunctionOper>(return_type, function_name, geo_args);
1366  }
1367 
1368  if (function_name == "ST_Distance"sv || function_name == "ST_MaxDistance"sv) {
1369  CHECK_EQ(size_t(2), rex_function->size());
1370  std::vector<std::shared_ptr<Analyzer::Expr>> args;
1371  int legacy_transform_srid = 0;
1372  for (size_t i = 0; i < rex_function->size(); i++) {
1373  SQLTypeInfo arg0_ti; // discard
1374  auto geoargs = translateGeoFunctionArg(rex_function->getOperand(i),
1375  arg0_ti,
1376  /*with_bounds=*/false, // TODO
1377  /*expand_geo_col=*/false,
1378  /*is_projection = */ false,
1379  /*use_geo_expressions=*/true);
1380  if (arg0_ti.get_input_srid() != arg0_ti.get_output_srid() &&
1381  arg0_ti.get_output_srid() > 0 &&
1382  std::dynamic_pointer_cast<Analyzer::ColumnVar>(geoargs.front())) {
1383  // legacy transform
1384  CHECK(legacy_transform_srid == 0 ||
1385  legacy_transform_srid == arg0_ti.get_output_srid());
1386  legacy_transform_srid = arg0_ti.get_output_srid();
1387  }
1388  args.insert(args.end(), geoargs.begin(), geoargs.end());
1389  }
1390  return makeExpr<Analyzer::GeoOperator>(
1391  SQLTypeInfo(kDOUBLE, /*not_null=*/false),
1392  function_name,
1393  args,
1394  legacy_transform_srid > 0 ? std::make_optional<int>(legacy_transform_srid)
1395  : std::nullopt);
1396  }
1397 
1398  bool swap_args = false;
1399  bool with_bounds = false;
1400  bool negate_result = false;
1401  Analyzer::ExpressionPtr threshold_expr = nullptr;
1402  Analyzer::ExpressionPtr compare_expr = nullptr;
1403  if (function_name == "ST_DWithin"sv) {
1404  CHECK_EQ(size_t(3), rex_function->size());
1405  function_name = "ST_Distance";
1406  return_type = SQLTypeInfo(kDOUBLE, false);
1407  // Inject ST_DWithin's short-circuiting threshold into ST_MaxDistance
1408  threshold_expr = translateScalarRex(rex_function->getOperand(2));
1409  } else if (function_name == "ST_Equals"sv) {
1410  // Translate ST_Equals(g1,g2) to ST_Distance(g1,g2)<=0.0
1411  CHECK_EQ(size_t(2), rex_function->size());
1412  function_name = "ST_Distance";
1413  return_type = SQLTypeInfo(kDOUBLE, false);
1414  threshold_expr = nullptr;
1415  Datum d;
1416  d.doubleval = 0.0;
1417  compare_expr = makeExpr<Analyzer::Constant>(kDOUBLE, false, d);
1418  } else if (function_name == "ST_DFullyWithin"sv) {
1419  CHECK_EQ(size_t(3), rex_function->size());
1420  function_name = "ST_MaxDistance";
1421  return_type = SQLTypeInfo(kDOUBLE, false);
1422  // TODO: inject ST_DFullyWithin's short-circuiting threshold into ST_MaxDistance
1423  threshold_expr = nullptr;
1424  } else if (function_name == "ST_Distance"sv) {
1425  // TODO: pick up an outside short-circuiting threshold and inject into ST_Distance
1426  threshold_expr = nullptr;
1427  } else if (function_name == "ST_MaxDistance"sv) {
1428  // TODO: pick up an outside short-circuiting threshold and inject into
1429  // ST_MaxDistance
1430  threshold_expr = nullptr;
1431  } else {
1432  CHECK_EQ(size_t(2), rex_function->size());
1433  }
1434  if (function_name == "ST_Within"sv) {
1435  function_name = "ST_Contains";
1436  swap_args = true;
1437  } else if (function_name == "ST_Disjoint"sv) {
1438  function_name = "ST_Intersects";
1439  negate_result = true;
1440  }
1441  if (func_resolve(
1442  function_name, "ST_Contains"sv, "ST_Intersects"sv, "ST_Approx_Overlaps"sv)) {
1443  with_bounds = true;
1444  }
1445 
1446  std::vector<std::shared_ptr<Analyzer::Expr>> geoargs;
1447  SQLTypeInfo arg0_ti;
1448  SQLTypeInfo arg1_ti;
1449 
1450  // Proactively try to compress the first arg of ST_Intersects to preempt arg swap
1451  bool try_to_compress_arg0 = g_enable_geo_ops_on_uncompressed_coords &&
1452  func_resolve(function_name, "ST_Intersects"sv);
1453 
1454  auto geoargs0 = translateGeoFunctionArg(rex_function->getOperand(swap_args ? 1 : 0),
1455  arg0_ti,
1456  with_bounds,
1457  false,
1458  false,
1459  false,
1460  try_to_compress_arg0);
1461  geoargs.insert(geoargs.end(), geoargs0.begin(), geoargs0.end());
1462 
1463  // If first arg is compressed, try to compress the second one to be able to
1464  // switch to faster implementations working directly on uncompressed coords
1465  bool try_to_compress_arg1 =
1467  func_resolve(function_name, "ST_Contains"sv, "ST_Intersects"sv) &&
1468  arg0_ti.get_compression() == kENCODING_GEOINT &&
1469  arg0_ti.get_output_srid() == 4326);
1470 
1471  auto geoargs1 = translateGeoFunctionArg(rex_function->getOperand(swap_args ? 0 : 1),
1472  arg1_ti,
1473  with_bounds,
1474  false,
1475  false,
1476  false,
1477  try_to_compress_arg1);
1478  geoargs.insert(geoargs.end(), geoargs1.begin(), geoargs1.end());
1479 
1480  if (arg0_ti.get_subtype() != kNULLT && arg0_ti.get_subtype() != arg1_ti.get_subtype()) {
1481  throw QueryNotSupported(rex_function->getName() +
1482  " accepts either two GEOGRAPHY or two GEOMETRY arguments");
1483  }
1484  // Check SRID match if at least one is set/valid
1485  if ((arg0_ti.get_output_srid() > 0 || arg1_ti.get_output_srid() > 0) &&
1486  arg0_ti.get_output_srid() != arg1_ti.get_output_srid()) {
1487  throw QueryNotSupported(rex_function->getName() + " cannot accept different SRIDs");
1488  }
1489  if (compare_expr) {
1490  // We could fold the check to false here if argument geo types are different, e.g.
1491  // POLYGON vs POINT. However, tiny POLYGON could be "spatially" equal to a POINT.
1492  if (arg0_ti.get_type() != kPOINT || arg1_ti.get_type() != kPOINT) {
1493  // ST_Equals is translated to a simple distance check for POINTs,
1494  // otherwise geometries are passed to GEOS's Equals
1495  return nullptr;
1496  }
1497  // Look at POINT compression modes.
1498  if (arg0_ti.get_compression() != arg1_ti.get_compression()) {
1499  if ((arg0_ti.get_compression() == kENCODING_GEOINT &&
1500  arg0_ti.get_comp_param() == 32 &&
1501  arg1_ti.get_compression() == kENCODING_NONE) ||
1502  (arg0_ti.get_compression() == kENCODING_NONE &&
1503  arg1_ti.get_compression() == kENCODING_GEOINT &&
1504  arg0_ti.get_comp_param() == 32)) {
1505  // Spatial equality comparison of a compressed point vs uncompressed point.
1506  // Introduce tolerance into distance calculation and comparison, translate
1507  // ST_Equals(g1,g2) to ST_Distance(g1,g2,thereshold=tolerance)<=tolerance
1508  Datum tolerance;
1509  // Tolerance representing 0.44" to cover shifts due to GEOINT(32) compression
1510  tolerance.doubleval = TOLERANCE_GEOINT32;
1511  threshold_expr = makeExpr<Analyzer::Constant>(kDOUBLE, false, tolerance);
1512  compare_expr = threshold_expr;
1513  } else {
1514  throw QueryNotSupported(
1515  rex_function->getName() +
1516  " unable to calculate compression tolerance for arguments");
1517  }
1518  }
1519  }
1520  if (arg0_ti.get_type() == kMULTILINESTRING || arg1_ti.get_type() == kMULTILINESTRING) {
1521  throw QueryNotSupported(rex_function->getName() +
1522  " currently doesn't support this argument combination");
1523  }
1524 
1525  auto can_use_compressed_coords = [](const SQLTypeInfo& i0_ti,
1526  const Analyzer::ExpressionPtrVector& i0_operands,
1527  const SQLTypeInfo& i1_ti,
1528  const Analyzer::ExpressionPtrVector& i1_operands) {
1529  const bool i0_is_poly =
1530  i0_ti.get_type() == kPOLYGON || i0_ti.get_type() == kMULTIPOLYGON;
1531  const bool i1_is_point = i1_ti.get_type() == kPOINT;
1532  const bool i1_is_literal =
1533  i1_operands.size() == 1 && std::dynamic_pointer_cast<const Analyzer::Constant>(
1534  i1_operands.front()) != nullptr;
1535  return (i0_is_poly && !i1_is_literal && i1_is_point &&
1536  i0_ti.get_compression() == kENCODING_GEOINT &&
1537  i0_ti.get_input_srid() == i0_ti.get_output_srid() &&
1538  i0_ti.get_compression() == i1_ti.get_compression() &&
1539  i1_ti.get_input_srid() == i1_ti.get_output_srid());
1540  };
1541  if (g_enable_geo_ops_on_uncompressed_coords && function_name == "ST_Contains"sv) {
1542  if (can_use_compressed_coords(arg0_ti, geoargs0, arg1_ti, geoargs1)) {
1543  // Switch to Contains implementation working directly on uncompressed coords
1544  function_name = "ST_cContains";
1545  }
1546  }
1547  if (g_enable_geo_ops_on_uncompressed_coords && function_name == "ST_Intersects"sv) {
1548  if (can_use_compressed_coords(arg0_ti, geoargs0, arg1_ti, geoargs1)) {
1549  // Switch to Intersects implementation working directly on uncompressed coords
1550  function_name = "ST_cIntersects";
1551  } else if (can_use_compressed_coords(arg1_ti, geoargs1, arg0_ti, geoargs0)) {
1552  // Switch to Intersects implementation working on uncompressed coords, swapped args
1553  function_name = "ST_cIntersects";
1554  geoargs.clear();
1555  geoargs.insert(geoargs.end(), geoargs1.begin(), geoargs1.end());
1556  geoargs.insert(geoargs.end(), geoargs0.begin(), geoargs0.end());
1557  auto tmp_ti = arg0_ti;
1558  arg0_ti = arg1_ti;
1559  arg1_ti = tmp_ti;
1560  }
1561  }
1562 
1563  std::string specialized_geofunc{function_name + suffix(arg0_ti.get_type()) +
1564  suffix(arg1_ti.get_type())};
1565 
1566  if (arg0_ti.get_subtype() == kGEOGRAPHY && arg0_ti.get_output_srid() == 4326) {
1567  // Need to call geodesic runtime functions
1568  if (function_name == "ST_Distance"sv) {
1569  if ((arg0_ti.get_type() == kPOINT && arg1_ti.get_type() == kPOINT) ||
1570  (arg0_ti.get_type() == kLINESTRING && arg1_ti.get_type() == kPOINT) ||
1571  (arg0_ti.get_type() == kPOINT && arg1_ti.get_type() == kLINESTRING)) {
1572  // Geodesic distance between points
1573  specialized_geofunc += "_Geodesic"s;
1574  } else {
1575  throw QueryNotSupported(function_name +
1576  " currently doesn't accept non-POINT geographies");
1577  }
1578  } else if (rex_function->getName() == "ST_Contains"sv) {
1579  // We currently don't have a geodesic implementation of ST_Contains,
1580  // allowing calls to a [less precise] cartesian implementation.
1581  } else {
1582  throw QueryNotSupported(function_name + " doesn't accept geographies");
1583  }
1584  } else if (function_name == "ST_Distance"sv && rex_function->size() == 3) {
1585  if (arg0_ti.get_type() == kPOINT && arg1_ti.get_type() == kPOINT) {
1586  // Cartesian distance between points used by ST_DWithin - switch to faster Squared
1587  specialized_geofunc += "_Squared"s;
1588  }
1589  }
1590 
1591  // Add first input's compression mode and SRID args to enable on-the-fly
1592  // decompression/transforms
1593  Datum input_compression0;
1594  input_compression0.intval = Geospatial::get_compression_scheme(arg0_ti);
1595  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression0));
1596  Datum input_srid0;
1597  input_srid0.intval = arg0_ti.get_input_srid();
1598  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid0));
1599 
1600  // Add second input's compression mode and SRID args to enable on-the-fly
1601  // decompression/transforms
1602  Datum input_compression1;
1603  input_compression1.intval = Geospatial::get_compression_scheme(arg1_ti);
1604  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression1));
1605  Datum input_srid1;
1606  input_srid1.intval = arg1_ti.get_input_srid();
1607  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid1));
1608 
1609  // Add output SRID arg to enable on-the-fly transforms
1610  Datum output_srid;
1611  output_srid.intval = arg0_ti.get_output_srid();
1612  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, output_srid));
1613 
1614  // Some geo distance functions will be injected with a short-circuit threshold.
1615  // Threshold value would come from Geo comparison operations or from other outer
1616  // geo operations, e.g. ST_DWithin
1617  // At this point, only ST_Distance_LineString_LineString requires a threshold arg.
1618  // TODO: Other combinations that involve LINESTRING, POLYGON and MULTIPOLYGON args
1619  // TODO: Inject threshold into ST_MaxDistance
1620  if (function_name == "ST_Distance"sv && arg0_ti.get_subtype() != kGEOGRAPHY &&
1621  (arg0_ti.get_type() != kPOINT || arg1_ti.get_type() != kPOINT)) {
1622  if (threshold_expr) {
1623  if (threshold_expr->get_type_info().get_type() != kDOUBLE) {
1624  const auto& threshold_ti = SQLTypeInfo(kDOUBLE, false);
1625  threshold_expr = threshold_expr->add_cast(threshold_ti);
1626  }
1627  threshold_expr = fold_expr(threshold_expr.get());
1628  } else {
1629  Datum d;
1630  d.doubleval = 0.0;
1631  threshold_expr = makeExpr<Analyzer::Constant>(kDOUBLE, false, d);
1632  }
1633  geoargs.push_back(threshold_expr);
1634  }
1635 
1636  auto result =
1637  makeExpr<Analyzer::FunctionOper>(return_type, specialized_geofunc, geoargs);
1638  if (negate_result) {
1639  return makeExpr<Analyzer::UOper>(kBOOLEAN, kNOT, result);
1640  }
1641  if (compare_expr) {
1642  return makeExpr<Analyzer::BinOper>(kBOOLEAN, kLE, kONE, result, compare_expr);
1643  }
1644  return result;
1645 }
1646 
1647 std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateTernaryGeoFunction(
1648  const RexFunctionOperator* rex_function) const {
1649  CHECK_EQ(size_t(3), rex_function->size());
1650 
1651  auto distance_expr = translateScalarRex(rex_function->getOperand(2));
1652  const auto& distance_ti = SQLTypeInfo(kDOUBLE, false);
1653  if (distance_expr->get_type_info().get_type() != kDOUBLE) {
1654  distance_expr = distance_expr->add_cast(distance_ti);
1655  }
1656 
1657  auto function_name = rex_function->getName();
1658  if (function_name == "ST_DWithin"sv) {
1659  auto return_type = rex_function->getType();
1660  bool swap_args = false;
1661  bool with_bounds = true;
1662  SQLTypeInfo arg0_ti;
1663  SQLTypeInfo arg1_ti;
1664 
1665  auto geoargs0 =
1666  translateGeoFunctionArg(rex_function->getOperand(0), arg0_ti, with_bounds, false);
1667  auto geoargs1 =
1668  translateGeoFunctionArg(rex_function->getOperand(1), arg1_ti, with_bounds, false);
1669  if (arg0_ti.get_subtype() != arg1_ti.get_subtype()) {
1670  throw QueryNotSupported(rex_function->getName() +
1671  " cannot accept mixed GEOMETRY/GEOGRAPHY arguments");
1672  }
1673  auto is_geodesic = false;
1674  if (arg0_ti.get_subtype() == kGEOGRAPHY) {
1675  if (arg0_ti.get_type() == kPOINT && arg1_ti.get_type() == kPOINT) {
1676  is_geodesic = true;
1677  } else {
1678  throw QueryNotSupported(
1679  rex_function->getName() +
1680  " in geodesic form can only accept POINT GEOGRAPHY arguments");
1681  }
1682  }
1683  // Check SRID match if at least one is set/valid
1684  if ((arg0_ti.get_output_srid() > 0 || arg1_ti.get_output_srid() > 0) &&
1685  arg0_ti.get_output_srid() != arg1_ti.get_output_srid()) {
1686  throw QueryNotSupported(rex_function->getName() + " cannot accept different SRIDs");
1687  }
1688 
1689  if ((arg1_ti.get_type() == kPOINT && arg0_ti.get_type() != kPOINT) ||
1690  (arg1_ti.get_type() == kLINESTRING && arg0_ti.get_type() == kPOLYGON) ||
1691  (arg1_ti.get_type() == kPOLYGON && arg0_ti.get_type() == kMULTIPOLYGON)) {
1692  // Swap arguments and use single implementation per arg pair
1693  swap_args = true;
1694  }
1695 
1696  // First input's compression mode and SRID args to enable on-the-fly
1697  // decompression/transforms
1698  Datum input_compression0;
1699  input_compression0.intval = Geospatial::get_compression_scheme(arg0_ti);
1700  Datum input_srid0;
1701  input_srid0.intval = arg0_ti.get_input_srid();
1702 
1703  // Second input's compression mode and SRID args to enable on-the-fly
1704  // decompression/transforms
1705  Datum input_compression1;
1706  input_compression1.intval = Geospatial::get_compression_scheme(arg1_ti);
1707  Datum input_srid1;
1708  input_srid1.intval = arg1_ti.get_input_srid();
1709 
1710  // Output SRID arg to enable on-the-fly transforms
1711  Datum output_srid;
1712  output_srid.intval = arg0_ti.get_output_srid();
1713 
1714  std::string specialized_geofunc{function_name};
1715  std::vector<std::shared_ptr<Analyzer::Expr>> geoargs;
1716  if (swap_args) {
1717  specialized_geofunc += suffix(arg1_ti.get_type()) + suffix(arg0_ti.get_type());
1718  geoargs.insert(geoargs.end(), geoargs1.begin(), geoargs1.end());
1719  geoargs.insert(geoargs.end(), geoargs0.begin(), geoargs0.end());
1720  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression1));
1721  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid1));
1722  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression0));
1723  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid0));
1724  } else {
1725  specialized_geofunc += suffix(arg0_ti.get_type()) + suffix(arg1_ti.get_type());
1726  if (is_geodesic) {
1727  specialized_geofunc += "_Geodesic"s;
1728  }
1729  geoargs.insert(geoargs.end(), geoargs0.begin(), geoargs0.end());
1730  geoargs.insert(geoargs.end(), geoargs1.begin(), geoargs1.end());
1731  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression0));
1732  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid0));
1733  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression1));
1734  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid1));
1735  }
1736  geoargs.push_back(makeExpr<Analyzer::Constant>(kINT, false, output_srid));
1737  // Also add the within distance
1738  geoargs.push_back(distance_expr);
1739 
1740  auto result =
1741  makeExpr<Analyzer::FunctionOper>(return_type, specialized_geofunc, geoargs);
1742  return result;
1743  }
1744 
1745  // Otherwise translate function as binary geo to get distance,
1746  // with optional short-circuiting threshold held in the third operand
1747  const auto geo_distance = translateBinaryGeoFunction(rex_function);
1748  // and generate the comparison
1749  return makeExpr<Analyzer::BinOper>(kBOOLEAN, kLE, kONE, geo_distance, distance_expr);
1750 }
1751 
1752 std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateGeoComparison(
1753  const RexOperator* rex_operator) const {
1754  if (rex_operator->size() != size_t(2)) {
1755  return nullptr;
1756  }
1757 
1758  auto geo_distance_expr = translateScalarRex(rex_operator->getOperand(0));
1759  auto func_oper = dynamic_cast<Analyzer::GeoOperator*>(geo_distance_expr.get());
1760  if (func_oper && func_oper->getName() == "ST_Distance"sv) {
1761  const auto& distance_ti = SQLTypeInfo(kDOUBLE, false);
1762  auto distance_expr = translateScalarRex(rex_operator->getOperand(1));
1763  if (distance_expr->get_type_info().get_type() != kDOUBLE) {
1764  distance_expr = distance_expr->add_cast(distance_ti);
1765  }
1766  distance_expr = fold_expr(distance_expr.get());
1767  return makeExpr<Analyzer::BinOper>(
1768  kBOOLEAN, rex_operator->getOperator(), kONE, geo_distance_expr, distance_expr);
1769  }
1770  return nullptr;
1771 }
1772 
1773 std::shared_ptr<Analyzer::Expr> RelAlgTranslator::translateFunctionWithGeoArg(
1774  const RexFunctionOperator* rex_function) const {
1775  std::string specialized_geofunc{rex_function->getName()};
1776  if (func_resolve(rex_function->getName(),
1777  "convert_meters_to_pixel_width"sv,
1778  "convert_meters_to_pixel_height"sv)) {
1779  CHECK_EQ(rex_function->size(), 6u);
1780  SQLTypeInfo arg_ti;
1781  std::vector<std::shared_ptr<Analyzer::Expr>> args;
1782  args.push_back(translateScalarRex(rex_function->getOperand(0)));
1783  auto geoargs =
1784  translateGeoFunctionArg(rex_function->getOperand(1), arg_ti, false, false);
1785  // only works on points
1786  if (arg_ti.get_type() != kPOINT) {
1787  throw QueryNotSupported(rex_function->getName() +
1788  " expects a point for the second argument");
1789  }
1790 
1791  args.insert(args.end(), geoargs.begin(), geoargs.begin() + 1);
1792 
1793  // Add compression information
1794  Datum input_compression;
1795  input_compression.intval = Geospatial::get_compression_scheme(arg_ti);
1796  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
1797  if (arg_ti.get_input_srid() != 4326) {
1798  throw QueryNotSupported(
1799  rex_function->getName() +
1800  " currently only supports points of with SRID WGS84/EPSG:4326");
1801  }
1802  Datum input_srid;
1803  input_srid.intval = arg_ti.get_input_srid();
1804  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_srid));
1805  Datum output_srid;
1806  // Forcing web-mercator projection for now
1807  // TODO(croot): check that the input-to-output conversion routines exist?
1808  output_srid.intval =
1809  arg_ti.get_output_srid() != 900913 ? 900913 : arg_ti.get_output_srid();
1810  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, output_srid));
1811 
1812  args.push_back(translateScalarRex(rex_function->getOperand(2)));
1813  args.push_back(translateScalarRex(rex_function->getOperand(3)));
1814  args.push_back(translateScalarRex(rex_function->getOperand(4)));
1815  args.push_back(translateScalarRex(rex_function->getOperand(5)));
1816  return makeExpr<Analyzer::FunctionOper>(
1817  rex_function->getType(), specialized_geofunc, args);
1818  } else if (rex_function->getName() == "is_point_in_view"sv) {
1819  CHECK_EQ(rex_function->size(), 5u);
1820  SQLTypeInfo arg_ti;
1821  std::vector<std::shared_ptr<Analyzer::Expr>> args;
1822  auto geoargs =
1823  translateGeoFunctionArg(rex_function->getOperand(0), arg_ti, false, false);
1824  // only works on points
1825  if (arg_ti.get_type() != kPOINT) {
1826  throw QueryNotSupported(rex_function->getName() +
1827  " expects a point for the second argument");
1828  }
1829 
1830  args.insert(args.end(), geoargs.begin(), geoargs.begin() + 1);
1831 
1832  // Add compression information
1833  Datum input_compression;
1834  input_compression.intval = Geospatial::get_compression_scheme(arg_ti);
1835  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
1836  if (arg_ti.get_input_srid() != 4326) {
1837  throw QueryNotSupported(
1838  rex_function->getName() +
1839  " currently only supports points of with SRID WGS84/EPSG:4326");
1840  }
1841  args.push_back(translateScalarRex(rex_function->getOperand(1)));
1842  args.push_back(translateScalarRex(rex_function->getOperand(2)));
1843  args.push_back(translateScalarRex(rex_function->getOperand(3)));
1844  args.push_back(translateScalarRex(rex_function->getOperand(4)));
1845  return makeExpr<Analyzer::FunctionOper>(
1846  rex_function->getType(), specialized_geofunc, args);
1847  } else if (rex_function->getName() == "is_point_size_in_view"sv) {
1848  CHECK_EQ(rex_function->size(), 6u);
1849  SQLTypeInfo arg_ti;
1850  std::vector<std::shared_ptr<Analyzer::Expr>> args;
1851  auto geoargs =
1852  translateGeoFunctionArg(rex_function->getOperand(0), arg_ti, false, false);
1853  // only works on points
1854  if (arg_ti.get_type() != kPOINT) {
1855  throw QueryNotSupported(rex_function->getName() +
1856  " expects a point for the second argument");
1857  }
1858 
1859  args.insert(args.end(), geoargs.begin(), geoargs.begin() + 1);
1860 
1861  // Add compression information
1862  Datum input_compression;
1863  input_compression.intval = Geospatial::get_compression_scheme(arg_ti);
1864  args.push_back(makeExpr<Analyzer::Constant>(kINT, false, input_compression));
1865  if (arg_ti.get_input_srid() != 4326) {
1866  throw QueryNotSupported(
1867  rex_function->getName() +
1868  " currently only supports points of with SRID WGS84/EPSG:4326");
1869  }
1870  args.push_back(translateScalarRex(rex_function->getOperand(1)));
1871  args.push_back(translateScalarRex(rex_function->getOperand(2)));
1872  args.push_back(translateScalarRex(rex_function->getOperand(3)));
1873  args.push_back(translateScalarRex(rex_function->getOperand(4)));
1874  args.push_back(translateScalarRex(rex_function->getOperand(5)));
1875  return makeExpr<Analyzer::FunctionOper>(
1876  rex_function->getType(), specialized_geofunc, args);
1877  }
1878  CHECK(false);
1879  return nullptr;
1880 }
1881 
1883  const RexOperator* rex_operator) const {
1884  CHECK_EQ(rex_operator->size(), 2u);
1885 
1886  auto translate_input =
1887  [&](const RexScalar* operand) -> std::shared_ptr<Analyzer::Expr> {
1888  const auto input = dynamic_cast<const RexInput*>(operand);
1889  CHECK(input);
1890 
1891  SQLTypeInfo ti;
1892  const auto exprs = translateGeoColumn(input, ti, true, false);
1893  CHECK_GT(exprs.size(), 0u);
1894  if (ti.get_type() == kPOINT) {
1895  return exprs.front();
1896  } else {
1897  return exprs.back();
1898  }
1899  };
1900 
1901  SQLQualifier sql_qual{kONE};
1902  SQLOps sql_op{kBBOX_INTERSECT};
1903  return makeExpr<Analyzer::BinOper>(SQLTypeInfo(kBOOLEAN, false),
1904  false,
1905  sql_op,
1906  sql_qual,
1907  translate_input(rex_operator->getOperand(1)),
1908  translate_input(rex_operator->getOperand(0)));
1909 }
int8_t tinyintval
Definition: Datum.h:73
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:392
void set_compression(EncodingType c)
Definition: sqltypes.h:481
void set_size(int s)
Definition: sqltypes.h:478
#define CHECK_EQ(x, y)
Definition: Logger.h:301
auto func_resolve
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoColumn(const RexInput *, SQLTypeInfo &, const bool with_bounds, const bool expand_geo_col) const
std::shared_ptr< Analyzer::Expr > translateBinaryGeoPredicate(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:143
SQLTypes
Definition: sqltypes.h:65
#define SPIMAP_GEO_PHYSICAL_INPUT(c, i)
Definition: Catalog.h:84
SQLQualifier
Definition: sqldefs.h:74
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
const SQLTypeInfo & getType() const
Definition: RelAlgDag.h:378
size_t size() const
Definition: RelAlgDag.h:364
const RexScalar * getOperand(const size_t idx) const
Definition: RelAlgDag.h:366
SQLOps
Definition: sqldefs.h:31
Definition: sqldefs.h:37
int32_t get_compression_scheme(const SQLTypeInfo &ti)
Definition: Compression.cpp:23
#define UNREACHABLE()
Definition: Logger.h:338
HOST DEVICE void set_subtype(SQLTypes st)
Definition: sqltypes.h:471
#define CHECK_GE(x, y)
Definition: Logger.h:306
Definition: sqldefs.h:51
std::shared_ptr< Analyzer::Expr > ExpressionPtr
Definition: Analyzer.h:184
bool g_enable_geo_ops_on_uncompressed_coords
Definition: Execute.cpp:125
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:391
#define CHECK_GT(x, y)
Definition: Logger.h:305
std::shared_ptr< Analyzer::Expr > translateGeoProjection(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
int32_t intval
Definition: Datum.h:75
#define TOLERANCE_GEOINT32
std::string suffix(SQLTypes type)
Definition: Codegen.cpp:75
std::string to_string(char const *&&v)
std::shared_ptr< Analyzer::Expr > translateInput(const RexInput *) const
std::shared_ptr< Analyzer::Expr > translateUnaryGeoFunction(const RexFunctionOperator *) const
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoLiteral(const RexLiteral *, SQLTypeInfo &, bool) const
void set_input_srid(int d)
Definition: sqltypes.h:474
unsigned getIndex() const
Definition: RelAlgDag.h:174
std::vector< uint8_t > compress_coords(const std::vector< double > &coords, const SQLTypeInfo &ti)
Definition: Compression.cpp:52
static std::shared_ptr< Analyzer::Expr > translateLiteral(const RexLiteral *)
SQLOps getOperator() const
Definition: RelAlgDag.h:376
bool has_bounds() const
Definition: sqltypes.h:457
static bool getGeoColumns(const std::string &wkt_or_wkb_hex, SQLTypeInfo &ti, std::vector< double > &coords, std::vector< double > &bounds, std::vector< int > &ring_sizes, std::vector< int > &poly_rings, const bool validate_with_geos_if_available)
Definition: Types.cpp:1121
void set_output_srid(int s)
Definition: sqltypes.h:476
const std::unordered_map< const RelAlgNode *, int > input_to_nest_level_
void set_comp_param(int p)
Definition: sqltypes.h:482
#define CHECK_LT(x, y)
Definition: Logger.h:303
Definition: sqltypes.h:79
std::shared_ptr< Analyzer::Expr > translateUnaryGeoPredicate(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
Definition: sqldefs.h:74
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:399
static std::unique_ptr< GeoBase > createGeoType(const std::string &wkt_or_wkb_hex, const bool validate_with_geos_if_available)
Definition: Types.cpp:1085
std::shared_ptr< Analyzer::Expr > translateUnaryGeoConstructor(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
bool is_projection(const RelAlgExecutionUnit &ra_exe_unit)
static RelRexToStringConfig defaults()
Definition: RelAlgDag.h:78
const RelAlgNode * getSourceNode() const
Definition: RelAlgDag.h:1056
static bool isUtm(unsigned const srid)
Definition: Transform.h:42
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoFunctionArg(const RexScalar *rex_scalar, SQLTypeInfo &arg_ti, const bool with_bounds, const bool expand_geo_col, const bool is_projection=false, const bool use_geo_expressions=false, const bool try_to_compress=false, const bool allow_gdal_transforms=false) const
#define IS_STRING(T)
Definition: sqltypes.h:309
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:402
HOST DEVICE int get_input_srid() const
Definition: sqltypes.h:395
void set_notnull(bool n)
Definition: sqltypes.h:477
#define CHECK(condition)
Definition: Logger.h:291
std::shared_ptr< Analyzer::Expr > translateTernaryGeoFunction(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateBinaryGeoFunction(const RexFunctionOperator *) const
std::vector< ExpressionPtr > ExpressionPtrVector
Definition: Analyzer.h:186
std::string toString(RelRexToStringConfig config=RelRexToStringConfig::defaults()) const override
Definition: RelAlgDag.h:508
std::shared_ptr< Analyzer::Expr > translateFunctionWithGeoArg(const RexFunctionOperator *) const
Definition: sqltypes.h:72
const std::string & getName() const
Definition: RelAlgDag.h:506
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:398
Definition: Datum.h:71
std::shared_ptr< Analyzer::Expr > translateGeoComparison(const RexOperator *) const
int get_physical_coord_cols() const
Definition: sqltypes.h:451
#define IS_GEO(T)
Definition: sqltypes.h:310
std::shared_ptr< Analyzer::Expr > translateBinaryGeoConstructor(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
Definition: sqldefs.h:41
SQLTypes get_ti_from_geo(const Geospatial::GeoBase *geo)
Definition: Analyzer.cpp:4007
std::shared_ptr< Analyzer::Expr > fold_expr(const Analyzer::Expr *expr)
std::shared_ptr< Analyzer::Expr > translateGeoBoundingBoxIntersectOper(const RexOperator *) const
double doubleval
Definition: Datum.h:78
HOST DEVICE int get_output_srid() const
Definition: sqltypes.h:397
virtual GeoType getType() const =0
#define IS_GEO_POLY(T)
Definition: sqltypes.h:315
HOST DEVICE void set_type(SQLTypes t)
Definition: sqltypes.h:470