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