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