OmniSciDB  0bd2ec9cf4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RelAlgDagBuilder.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <iterator>
20 #include <memory>
21 #include <unordered_map>
22 
23 #include <rapidjson/document.h>
24 #include <boost/core/noncopyable.hpp>
25 
26 #include "Catalog/Catalog.h"
27 #include "Shared/ConfigResolve.h"
29 
33 
34 using ColumnNameList = std::vector<std::string>;
35 
36 class Rex {
37  public:
38  virtual std::string toString() const = 0;
39 
40  virtual ~Rex() {}
41 };
42 
43 class RexScalar : public Rex {};
44 
45 // For internal use of the abstract interpreter only. The result after abstract
46 // interpretation will not have any references to 'RexAbstractInput' objects.
47 class RexAbstractInput : public RexScalar {
48  public:
49  RexAbstractInput(const unsigned in_index) : in_index_(in_index) {}
50 
51  unsigned getIndex() const { return in_index_; }
52 
53  void setIndex(const unsigned in_index) const { in_index_ = in_index; }
54 
55  std::string toString() const override {
56  return "(RexAbstractInput " + std::to_string(in_index_) + ")";
57  }
58 
59  private:
60  mutable unsigned in_index_;
61 };
62 
63 class RexLiteral : public RexScalar {
64  public:
65  RexLiteral(const int64_t val,
66  const SQLTypes type,
67  const SQLTypes target_type,
68  const unsigned scale,
69  const unsigned precision,
70  const unsigned type_scale,
71  const unsigned type_precision)
72  : literal_(val)
73  , type_(type)
74  , target_type_(target_type)
75  , scale_(scale)
76  , precision_(precision)
77  , type_scale_(type_scale)
78  , type_precision_(type_precision) {
79  CHECK(type == kDECIMAL || type == kINTERVAL_DAY_TIME ||
80  type == kINTERVAL_YEAR_MONTH || is_datetime(type));
81  }
82 
83  RexLiteral(const double val,
84  const SQLTypes type,
85  const SQLTypes target_type,
86  const unsigned scale,
87  const unsigned precision,
88  const unsigned type_scale,
89  const unsigned type_precision)
90  : literal_(val)
91  , type_(type)
92  , target_type_(target_type)
93  , scale_(scale)
94  , precision_(precision)
95  , type_scale_(type_scale)
96  , type_precision_(type_precision) {
97  CHECK_EQ(kDOUBLE, type);
98  }
99 
100  RexLiteral(const std::string& val,
101  const SQLTypes type,
102  const SQLTypes target_type,
103  const unsigned scale,
104  const unsigned precision,
105  const unsigned type_scale,
106  const unsigned type_precision)
107  : literal_(val)
108  , type_(type)
109  , target_type_(target_type)
110  , scale_(scale)
111  , precision_(precision)
112  , type_scale_(type_scale)
113  , type_precision_(type_precision) {
114  CHECK_EQ(kTEXT, type);
115  }
116 
117  RexLiteral(const bool val,
118  const SQLTypes type,
119  const SQLTypes target_type,
120  const unsigned scale,
121  const unsigned precision,
122  const unsigned type_scale,
123  const unsigned type_precision)
124  : literal_(val)
125  , type_(type)
126  , target_type_(target_type)
127  , scale_(scale)
128  , precision_(precision)
129  , type_scale_(type_scale)
130  , type_precision_(type_precision) {
131  CHECK_EQ(kBOOLEAN, type);
132  }
133 
134  RexLiteral(const SQLTypes target_type)
135  : literal_(nullptr)
136  , type_(kNULLT)
137  , target_type_(target_type)
138  , scale_(0)
139  , precision_(0)
140  , type_scale_(0)
141  , type_precision_(0) {}
142 
143  template <class T>
144  T getVal() const {
145  const auto ptr = boost::get<T>(&literal_);
146  CHECK(ptr);
147  return *ptr;
148  }
149 
150  SQLTypes getType() const { return type_; }
151 
152  SQLTypes getTargetType() const { return target_type_; }
153 
154  unsigned getScale() const { return scale_; }
155 
156  unsigned getPrecision() const { return precision_; }
157 
158  unsigned getTypeScale() const { return type_scale_; }
159 
160  unsigned getTypePrecision() const { return type_precision_; }
161 
162  std::string toString() const override {
163  return "(RexLiteral " + boost::lexical_cast<std::string>(literal_) + ")";
164  }
165 
166  std::unique_ptr<RexLiteral> deepCopy() const {
167  switch (literal_.which()) {
168  case 0: {
169  int64_t val = getVal<int64_t>();
170  return std::make_unique<RexLiteral>(
172  }
173  case 1: {
174  double val = getVal<double>();
175  return std::make_unique<RexLiteral>(
177  }
178  case 2: {
179  auto val = getVal<std::string>();
180  return std::make_unique<RexLiteral>(
182  }
183  case 3: {
184  bool val = getVal<bool>();
185  return std::make_unique<RexLiteral>(
187  }
188  case 4: {
189  return std::make_unique<RexLiteral>(target_type_);
190  }
191  default:
192  CHECK(false);
193  }
194  return nullptr;
195  }
196 
197  private:
198  const boost::variant<int64_t, double, std::string, bool, void*> literal_;
201  const unsigned scale_;
202  const unsigned precision_;
203  const unsigned type_scale_;
204  const unsigned type_precision_;
205 };
206 
207 using RexLiteralArray = std::vector<RexLiteral>;
208 using TupleContentsArray = std::vector<RexLiteralArray>;
209 
210 class RexOperator : public RexScalar {
211  public:
212  RexOperator(const SQLOps op,
213  std::vector<std::unique_ptr<const RexScalar>>& operands,
214  const SQLTypeInfo& type)
215  : op_(op), operands_(std::move(operands)), type_(type) {}
216 
217  virtual std::unique_ptr<const RexOperator> getDisambiguated(
218  std::vector<std::unique_ptr<const RexScalar>>& operands) const {
219  return std::unique_ptr<const RexOperator>(new RexOperator(op_, operands, type_));
220  }
221 
222  size_t size() const { return operands_.size(); }
223 
224  const RexScalar* getOperand(const size_t idx) const {
225  CHECK(idx < operands_.size());
226  return operands_[idx].get();
227  }
228 
229  const RexScalar* getOperandAndRelease(const size_t idx) const {
230  CHECK(idx < operands_.size());
231  return operands_[idx].release();
232  }
233 
234  SQLOps getOperator() const { return op_; }
235 
236  const SQLTypeInfo& getType() const { return type_; }
237 
238  std::string toString() const override {
239  std::string result = "(RexOperator " + std::to_string(op_);
240  for (const auto& operand : operands_) {
241  result += " " + operand->toString();
242  }
243  return result + ")";
244  };
245 
246  protected:
247  const SQLOps op_;
248  mutable std::vector<std::unique_ptr<const RexScalar>> operands_;
250 };
251 
252 class RelAlgNode;
253 
254 class ExecutionResult;
255 
256 class RexSubQuery : public RexScalar {
257  public:
258  RexSubQuery(const std::shared_ptr<const RelAlgNode> ra)
259  : type_(new SQLTypeInfo(kNULLT, false))
260  , result_(new std::shared_ptr<const ExecutionResult>(nullptr))
261  , ra_(ra) {}
262 
263  // for deep copy
264  RexSubQuery(std::shared_ptr<SQLTypeInfo> type,
265  std::shared_ptr<std::shared_ptr<const ExecutionResult>> result,
266  const std::shared_ptr<const RelAlgNode> ra)
267  : type_(type), result_(result), ra_(ra) {}
268 
269  RexSubQuery(const RexSubQuery&) = delete;
270 
271  RexSubQuery& operator=(const RexSubQuery&) = delete;
272 
273  RexSubQuery(RexSubQuery&&) = delete;
274 
275  RexSubQuery& operator=(RexSubQuery&&) = delete;
276 
277  const SQLTypeInfo& getType() const {
278  CHECK_NE(kNULLT, type_->get_type());
279  return *(type_.get());
280  }
281 
282  std::shared_ptr<const ExecutionResult> getExecutionResult() const {
283  CHECK(result_);
284  CHECK(result_.get());
285  return *(result_.get());
286  }
287 
288  const RelAlgNode* getRelAlg() const { return ra_.get(); }
289 
290  std::string toString() const override {
291  return "(RexSubQuery " + std::to_string(reinterpret_cast<const uint64_t>(this)) + ")";
292  }
293 
294  std::unique_ptr<RexSubQuery> deepCopy() const;
295 
296  void setExecutionResult(const std::shared_ptr<const ExecutionResult> result);
297 
298  private:
299  std::shared_ptr<SQLTypeInfo> type_;
300  std::shared_ptr<std::shared_ptr<const ExecutionResult>> result_;
301  const std::shared_ptr<const RelAlgNode> ra_;
302 };
303 
304 // The actual input node understood by the Executor.
305 // The in_index_ is relative to the output of node_.
306 class RexInput : public RexAbstractInput {
307  public:
308  RexInput(const RelAlgNode* node, const unsigned in_index)
309  : RexAbstractInput(in_index), node_(node) {}
310 
311  const RelAlgNode* getSourceNode() const { return node_; }
312 
313  // This isn't great, but we need it for coalescing nodes to Compound since
314  // RexInput in descendents need to be rebound to the newly created Compound.
315  // Maybe create a fresh RA tree with the required changes after each coalescing?
316  void setSourceNode(const RelAlgNode* node) const { node_ = node; }
317 
318  bool operator==(const RexInput& that) const {
319  return getSourceNode() == that.getSourceNode() && getIndex() == that.getIndex();
320  }
321 
322  std::string toString() const override {
323  return "(RexInput " + std::to_string(getIndex()) + " " +
324  std::to_string(reinterpret_cast<const uint64_t>(node_)) + ")";
325  }
326 
327  std::unique_ptr<RexInput> deepCopy() const {
328  return std::make_unique<RexInput>(node_, getIndex());
329  }
330 
331  private:
332  mutable const RelAlgNode* node_;
333 };
334 
335 namespace std {
336 
337 template <>
338 struct hash<RexInput> {
339  size_t operator()(const RexInput& rex_in) const {
340  auto addr = rex_in.getSourceNode();
341  return *reinterpret_cast<const size_t*>(may_alias_ptr(&addr)) ^ rex_in.getIndex();
342  }
343 };
344 
345 } // namespace std
346 
347 // Not a real node created by Calcite. Created by us because CaseExpr is a node in our
348 // Analyzer.
349 class RexCase : public RexScalar {
350  public:
351  RexCase(std::vector<std::pair<std::unique_ptr<const RexScalar>,
352  std::unique_ptr<const RexScalar>>>& expr_pair_list,
353  std::unique_ptr<const RexScalar>& else_expr)
354  : expr_pair_list_(std::move(expr_pair_list)), else_expr_(std::move(else_expr)) {}
355 
356  size_t branchCount() const { return expr_pair_list_.size(); }
357 
358  const RexScalar* getWhen(const size_t idx) const {
359  CHECK(idx < expr_pair_list_.size());
360  return expr_pair_list_[idx].first.get();
361  }
362 
363  const RexScalar* getThen(const size_t idx) const {
364  CHECK(idx < expr_pair_list_.size());
365  return expr_pair_list_[idx].second.get();
366  }
367 
368  const RexScalar* getElse() const { return else_expr_.get(); }
369 
370  std::string toString() const override {
371  std::string ret = "(RexCase";
372  for (const auto& expr_pair : expr_pair_list_) {
373  ret += " " + expr_pair.first->toString() + " -> " + expr_pair.second->toString();
374  }
375  if (else_expr_) {
376  ret += " else " + else_expr_->toString();
377  }
378  ret += ")";
379  return ret;
380  }
381 
382  private:
383  std::vector<
384  std::pair<std::unique_ptr<const RexScalar>, std::unique_ptr<const RexScalar>>>
386  std::unique_ptr<const RexScalar> else_expr_;
387 };
388 
390  public:
391  using ConstRexScalarPtr = std::unique_ptr<const RexScalar>;
392  using ConstRexScalarPtrVector = std::vector<ConstRexScalarPtr>;
393 
394  RexFunctionOperator(const std::string& name,
395  ConstRexScalarPtrVector& operands,
396  const SQLTypeInfo& ti)
397  : RexOperator(kFUNCTION, operands, ti), name_(name) {}
398 
399  std::unique_ptr<const RexOperator> getDisambiguated(
400  std::vector<std::unique_ptr<const RexScalar>>& operands) const override {
401  return std::unique_ptr<const RexOperator>(
402  new RexFunctionOperator(name_, operands, getType()));
403  }
404 
405  const std::string& getName() const { return name_; }
406 
407  std::string toString() const override {
408  auto result = "(RexFunctionOperator " + name_;
409  for (const auto& operand : operands_) {
410  result += (" " + operand->toString());
411  }
412  return result + ")";
413  }
414 
415  private:
416  const std::string name_;
417 };
418 
420 
421 enum class NullSortedPosition { First, Last };
422 
423 class SortField {
424  public:
425  SortField(const size_t field,
426  const SortDirection sort_dir,
427  const NullSortedPosition nulls_pos)
428  : field_(field), sort_dir_(sort_dir), nulls_pos_(nulls_pos) {}
429 
430  bool operator==(const SortField& that) const {
431  return field_ == that.field_ && sort_dir_ == that.sort_dir_ &&
432  nulls_pos_ == that.nulls_pos_;
433  }
434 
435  size_t getField() const { return field_; }
436 
437  SortDirection getSortDir() const { return sort_dir_; }
438 
440 
441  std::string toString() const {
442  return "(" + std::to_string(field_) + " " +
443  (sort_dir_ == SortDirection::Ascending ? "asc" : "desc") + " " +
444  (nulls_pos_ == NullSortedPosition::First ? "nulls_first" : "nulls_last") + ")";
445  }
446 
447  private:
448  const size_t field_;
451 };
452 
454  public:
455  struct RexWindowBound {
456  bool unbounded;
457  bool preceding;
458  bool following;
460  std::shared_ptr<const RexScalar> offset;
462  };
463 
465  ConstRexScalarPtrVector& operands,
466  ConstRexScalarPtrVector& partition_keys,
467  ConstRexScalarPtrVector& order_keys,
468  const std::vector<SortField> collation,
469  const RexWindowBound& lower_bound,
470  const RexWindowBound& upper_bound,
471  const bool is_rows,
472  const SQLTypeInfo& ti)
473  : RexFunctionOperator(sql_window_function_to_str(kind), operands, ti)
474  , kind_(kind)
475  , partition_keys_(std::move(partition_keys))
476  , order_keys_(std::move(order_keys))
477  , collation_(collation)
478  , lower_bound_(lower_bound)
479  , upper_bound_(upper_bound)
480  , is_rows_(is_rows) {}
481 
482  SqlWindowFunctionKind getKind() const { return kind_; }
483 
485 
487  return std::move(partition_keys_);
488  }
489 
491  return std::move(order_keys_);
492  }
493 
495 
496  const std::vector<SortField>& getCollation() const { return collation_; }
497 
498  const RexWindowBound& getLowerBound() const { return lower_bound_; }
499 
500  const RexWindowBound& getUpperBound() const { return upper_bound_; }
501 
502  bool isRows() const { return is_rows_; }
503 
504  std::unique_ptr<const RexOperator> disambiguatedOperands(
505  ConstRexScalarPtrVector& operands,
506  ConstRexScalarPtrVector& partition_keys,
507  ConstRexScalarPtrVector& order_keys,
508  const std::vector<SortField>& collation) const {
509  return std::unique_ptr<const RexOperator>(
511  operands,
512  partition_keys,
513  order_keys,
514  collation,
515  getLowerBound(),
516  getUpperBound(),
517  isRows(),
518  getType()));
519  }
520 
521  std::string toString() const override {
522  auto result = "(RexWindowFunctionOperator " + getName();
523  for (const auto& operand : operands_) {
524  result += (" " + operand->toString());
525  }
526  result += " partition[";
527  for (const auto& partition_key : partition_keys_) {
528  result += (" " + partition_key->toString());
529  }
530  result += "]";
531  result += " order[";
532  for (const auto& order_key : order_keys_) {
533  result += (" " + order_key->toString());
534  }
535  result += "]";
536  return result + ")";
537  }
538 
539  private:
543  const std::vector<SortField> collation_;
546  const bool is_rows_;
547 };
548 
549 // Not a real node created by Calcite. Created by us because targets of a query
550 // should reference the group by expressions instead of creating completely new one.
551 class RexRef : public RexScalar {
552  public:
553  RexRef(const size_t index) : index_(index) {}
554 
555  size_t getIndex() const { return index_; }
556 
557  std::string toString() const override {
558  return "(RexRef " + std::to_string(index_) + ")";
559  }
560 
561  std::unique_ptr<RexRef> deepCopy() const { return std::make_unique<RexRef>(index_); }
562 
563  private:
564  const size_t index_;
565 };
566 
567 class RexAgg : public Rex {
568  public:
569  RexAgg(const SQLAgg agg,
570  const bool distinct,
571  const SQLTypeInfo& type,
572  const std::vector<size_t>& operands)
573  : agg_(agg), distinct_(distinct), type_(type), operands_(operands) {}
574 
575  std::string toString() const override {
576  auto result = "(RexAgg " + std::to_string(agg_) + " " + std::to_string(distinct_) +
577  " " + type_.get_type_name() + " " + type_.get_compression_name();
578  for (auto operand : operands_) {
579  result += " " + std::to_string(operand);
580  }
581  return result + ")";
582  }
583 
584  SQLAgg getKind() const { return agg_; }
585 
586  bool isDistinct() const { return distinct_; }
587 
588  size_t size() const { return operands_.size(); }
589 
590  size_t getOperand(size_t idx) const { return operands_[idx]; }
591 
592  const SQLTypeInfo& getType() const { return type_; }
593 
594  std::unique_ptr<RexAgg> deepCopy() const {
595  return std::make_unique<RexAgg>(agg_, distinct_, type_, operands_);
596  }
597 
598  private:
599  const SQLAgg agg_;
600  const bool distinct_;
602  const std::vector<size_t> operands_;
603 };
604 
605 class RelAlgNode {
606  public:
608 
609  virtual ~RelAlgNode() {}
610 
612  context_data_ = nullptr;
613  targets_metainfo_ = {};
614  }
615 
616  void setContextData(const void* context_data) const {
618  context_data_ = context_data;
619  }
620 
621  void setOutputMetainfo(const std::vector<TargetMetaInfo>& targets_metainfo) const {
622  targets_metainfo_ = targets_metainfo;
623  }
624 
625  const std::vector<TargetMetaInfo>& getOutputMetainfo() const {
626  return targets_metainfo_;
627  }
628 
629  unsigned getId() const { return id_; }
630 
631  bool hasContextData() const { return !(context_data_ == nullptr); }
632 
633  const void* getContextData() const {
635  return context_data_;
636  }
637 
638  const size_t inputCount() const { return inputs_.size(); }
639 
640  const RelAlgNode* getInput(const size_t idx) const {
641  CHECK(idx < inputs_.size());
642  return inputs_[idx].get();
643  }
644 
645  std::shared_ptr<const RelAlgNode> getAndOwnInput(const size_t idx) const {
646  CHECK(idx < inputs_.size());
647  return inputs_[idx];
648  }
649 
650  void addManagedInput(std::shared_ptr<const RelAlgNode> input) {
651  inputs_.push_back(input);
652  }
653 
654  bool hasInput(const RelAlgNode* needle) const {
655  for (auto& input_ptr : inputs_) {
656  if (input_ptr.get() == needle) {
657  return true;
658  }
659  }
660  return false;
661  }
662 
663  virtual void replaceInput(std::shared_ptr<const RelAlgNode> old_input,
664  std::shared_ptr<const RelAlgNode> input) {
665  for (auto& input_ptr : inputs_) {
666  if (input_ptr == old_input) {
667  input_ptr = input;
668  break;
669  }
670  }
671  }
672 
673  bool isNop() const { return is_nop_; }
674 
675  void markAsNop() { is_nop_ = true; }
676 
677  virtual std::string toString() const = 0;
678 
679  virtual size_t size() const = 0;
680 
681  virtual std::shared_ptr<RelAlgNode> deepCopy() const = 0;
682 
683  static void resetRelAlgFirstId() noexcept;
684 
685  protected:
686  std::vector<std::shared_ptr<const RelAlgNode>> inputs_;
687  const unsigned id_;
688 
689  private:
690  mutable const void* context_data_;
691  bool is_nop_;
692  mutable std::vector<TargetMetaInfo> targets_metainfo_;
693  static thread_local unsigned crt_id_;
694 };
695 
696 class RelScan : public RelAlgNode {
697  public:
698  RelScan(const TableDescriptor* td, const std::vector<std::string>& field_names)
699  : td_(td), field_names_(field_names) {}
700 
701  size_t size() const override { return field_names_.size(); }
702 
703  const TableDescriptor* getTableDescriptor() const { return td_; }
704 
705  const std::vector<std::string>& getFieldNames() const { return field_names_; }
706 
707  const std::string getFieldName(const size_t i) const { return field_names_[i]; }
708 
709  std::string toString() const override {
710  return "(RelScan<" + std::to_string(reinterpret_cast<uint64_t>(this)) + "> " +
711  td_->tableName + ")";
712  }
713 
714  std::shared_ptr<RelAlgNode> deepCopy() const override {
715  CHECK(false);
716  return nullptr;
717  };
718 
719  private:
720  const TableDescriptor* td_;
721  const std::vector<std::string> field_names_;
722 };
723 
725  public:
726  ModifyManipulationTarget(bool const update_via_select = false,
727  bool const delete_via_select = false,
728  bool const varlen_update_required = false,
729  TableDescriptor const* table_descriptor = nullptr,
730  ColumnNameList target_columns = ColumnNameList())
731  : is_update_via_select_(update_via_select)
732  , is_delete_via_select_(delete_via_select)
733  , varlen_update_required_(varlen_update_required)
734  , table_descriptor_(table_descriptor)
735  , target_columns_(target_columns) {}
736 
737  void setUpdateViaSelectFlag() const { is_update_via_select_ = true; }
738  void setDeleteViaSelectFlag() const { is_delete_via_select_ = true; }
740  varlen_update_required_ = required;
741  }
742 
743  TableDescriptor const* getModifiedTableDescriptor() const { return table_descriptor_; }
745  table_descriptor_ = td;
746  }
747 
748  auto const isUpdateViaSelect() const { return is_update_via_select_; }
749  auto const isDeleteViaSelect() const { return is_delete_via_select_; }
750  auto const isVarlenUpdateRequired() const { return varlen_update_required_; }
751 
752  int getTargetColumnCount() const { return target_columns_.size(); }
753  void setTargetColumns(ColumnNameList const& target_columns) const {
754  target_columns_ = target_columns;
755  }
756  ColumnNameList const& getTargetColumns() const { return target_columns_; }
757 
758  template <typename VALIDATION_FUNCTOR>
759  bool validateTargetColumns(VALIDATION_FUNCTOR validator) const {
760  for (auto const& column_name : target_columns_) {
761  if (validator(column_name) == false) {
762  return false;
763  }
764  }
765  return true;
766  }
767 
768  private:
769  mutable bool is_update_via_select_ = false;
770  mutable bool is_delete_via_select_ = false;
771  mutable bool varlen_update_required_ = false;
772  mutable TableDescriptor const* table_descriptor_ = nullptr;
774 };
775 
777  public:
778  friend class RelModify;
779  using ConstRexScalarPtr = std::unique_ptr<const RexScalar>;
780  using ConstRexScalarPtrVector = std::vector<ConstRexScalarPtr>;
781 
782  // Takes memory ownership of the expressions.
783  RelProject(std::vector<std::unique_ptr<const RexScalar>>& scalar_exprs,
784  const std::vector<std::string>& fields,
785  std::shared_ptr<const RelAlgNode> input)
787  , scalar_exprs_(std::move(scalar_exprs))
788  , fields_(fields) {
789  inputs_.push_back(input);
790  }
791 
792  void setExpressions(std::vector<std::unique_ptr<const RexScalar>>& exprs) const {
793  scalar_exprs_ = std::move(exprs);
794  }
795 
796  // True iff all the projected expressions are inputs. If true,
797  // this node can be elided and merged into the previous node
798  // since it's just a subset and / or permutation of its outputs.
799  bool isSimple() const {
800  for (const auto& expr : scalar_exprs_) {
801  if (!dynamic_cast<const RexInput*>(expr.get())) {
802  return false;
803  }
804  }
805  return true;
806  }
807 
808  bool isIdentity() const;
809 
810  bool isRenaming() const;
811 
812  size_t size() const override { return scalar_exprs_.size(); }
813 
814  const RexScalar* getProjectAt(const size_t idx) const {
815  CHECK(idx < scalar_exprs_.size());
816  return scalar_exprs_[idx].get();
817  }
818 
819  const RexScalar* getProjectAtAndRelease(const size_t idx) const {
820  CHECK(idx < scalar_exprs_.size());
821  return scalar_exprs_[idx].release();
822  }
823 
824  std::vector<std::unique_ptr<const RexScalar>> getExpressionsAndRelease() {
825  return std::move(scalar_exprs_);
826  }
827 
828  const std::vector<std::string>& getFields() const { return fields_; }
829  void setFields(std::vector<std::string>& fields) { fields_ = std::move(fields); }
830 
831  const std::string getFieldName(const size_t i) const { return fields_[i]; }
832 
833  void replaceInput(std::shared_ptr<const RelAlgNode> old_input,
834  std::shared_ptr<const RelAlgNode> input) override;
835 
836  void appendInput(std::string new_field_name,
837  std::unique_ptr<const RexScalar> new_input);
838 
839  std::string toString() const override {
840  std::string result =
841  "(RelProject<" + std::to_string(reinterpret_cast<uint64_t>(this)) + ">";
842  for (const auto& scalar_expr : scalar_exprs_) {
843  result += " " + scalar_expr->toString();
844  }
845  return result + ")";
846  }
847 
848  std::shared_ptr<RelAlgNode> deepCopy() const override;
849 
850  private:
851  template <typename EXPR_VISITOR_FUNCTOR>
852  void visitScalarExprs(EXPR_VISITOR_FUNCTOR visitor_functor) const {
853  for (int i = 0; i < static_cast<int>(scalar_exprs_.size()); i++) {
854  visitor_functor(i);
855  }
856  }
857 
860  scalar_exprs_.emplace_back(std::make_unique<RexFunctionOperator const>(
861  std::string("OFFSET_IN_FRAGMENT"), transient_vector, SQLTypeInfo(kINT, false)));
862  fields_.emplace_back("EXPR$DELETE_OFFSET_IN_FRAGMENT");
863  }
864 
865  mutable std::vector<std::unique_ptr<const RexScalar>> scalar_exprs_;
866  mutable std::vector<std::string> fields_;
867 };
868 
869 class RelAggregate : public RelAlgNode {
870  public:
871  // Takes ownership of the aggregate expressions.
872  RelAggregate(const size_t groupby_count,
873  std::vector<std::unique_ptr<const RexAgg>>& agg_exprs,
874  const std::vector<std::string>& fields,
875  std::shared_ptr<const RelAlgNode> input)
876  : groupby_count_(groupby_count), agg_exprs_(std::move(agg_exprs)), fields_(fields) {
877  inputs_.push_back(input);
878  }
879 
880  size_t size() const override { return groupby_count_ + agg_exprs_.size(); }
881 
882  const size_t getGroupByCount() const { return groupby_count_; }
883 
884  const size_t getAggExprsCount() const { return agg_exprs_.size(); }
885 
886  const std::vector<std::string>& getFields() const { return fields_; }
887  void setFields(std::vector<std::string>& new_fields) {
888  fields_ = std::move(new_fields);
889  }
890 
891  const std::string getFieldName(const size_t i) const { return fields_[i]; }
892 
893  std::vector<const RexAgg*> getAggregatesAndRelease() {
894  std::vector<const RexAgg*> result;
895  for (auto& agg_expr : agg_exprs_) {
896  result.push_back(agg_expr.release());
897  }
898  return result;
899  }
900 
901  std::vector<std::unique_ptr<const RexAgg>> getAggExprsAndRelease() {
902  return std::move(agg_exprs_);
903  }
904 
905  const std::vector<std::unique_ptr<const RexAgg>>& getAggExprs() const {
906  return agg_exprs_;
907  }
908 
909  void setAggExprs(std::vector<std::unique_ptr<const RexAgg>>& agg_exprs) {
910  agg_exprs_ = std::move(agg_exprs);
911  }
912 
913  std::string toString() const override {
914  std::string result = "(RelAggregate<" +
915  std::to_string(reinterpret_cast<uint64_t>(this)) + ">(groups: [";
916  for (size_t group_index = 0; group_index < groupby_count_; ++group_index) {
917  result += " " + std::to_string(group_index);
918  }
919  result += " ] aggs: [";
920  for (const auto& agg_expr : agg_exprs_) {
921  result += " " + agg_expr->toString();
922  }
923  return result + " ])";
924  }
925 
926  std::shared_ptr<RelAlgNode> deepCopy() const override;
927 
928  private:
929  const size_t groupby_count_;
930  std::vector<std::unique_ptr<const RexAgg>> agg_exprs_;
931  std::vector<std::string> fields_;
932 };
933 
934 class RelJoin : public RelAlgNode {
935  public:
936  RelJoin(std::shared_ptr<const RelAlgNode> lhs,
937  std::shared_ptr<const RelAlgNode> rhs,
938  std::unique_ptr<const RexScalar>& condition,
939  const JoinType join_type)
940  : condition_(std::move(condition)), join_type_(join_type) {
941  inputs_.push_back(lhs);
942  inputs_.push_back(rhs);
943  }
944 
945  JoinType getJoinType() const { return join_type_; }
946 
947  const RexScalar* getCondition() const { return condition_.get(); }
948 
949  const RexScalar* getAndReleaseCondition() const { return condition_.release(); }
950 
951  void setCondition(std::unique_ptr<const RexScalar>& condition) {
952  CHECK(condition);
953  condition_ = std::move(condition);
954  }
955 
956  void replaceInput(std::shared_ptr<const RelAlgNode> old_input,
957  std::shared_ptr<const RelAlgNode> input) override;
958 
959  std::string toString() const override {
960  std::string result =
961  "(RelJoin<" + std::to_string(reinterpret_cast<uint64_t>(this)) + ">(";
962  result += condition_ ? condition_->toString() : "null";
963  result += " " + std::to_string(static_cast<int>(join_type_));
964  return result + ")";
965  }
966 
967  size_t size() const override { return inputs_[0]->size() + inputs_[1]->size(); }
968 
969  std::shared_ptr<RelAlgNode> deepCopy() const override;
970 
971  private:
972  mutable std::unique_ptr<const RexScalar> condition_;
974 };
975 
976 class RelFilter : public RelAlgNode {
977  public:
978  RelFilter(std::unique_ptr<const RexScalar>& filter,
979  std::shared_ptr<const RelAlgNode> input)
980  : filter_(std::move(filter)) {
981  CHECK(filter_);
982  inputs_.push_back(input);
983  }
984 
985  const RexScalar* getCondition() const { return filter_.get(); }
986 
987  const RexScalar* getAndReleaseCondition() { return filter_.release(); }
988 
989  void setCondition(std::unique_ptr<const RexScalar>& condition) {
990  CHECK(condition);
991  filter_ = std::move(condition);
992  }
993 
994  size_t size() const override { return inputs_[0]->size(); }
995 
996  void replaceInput(std::shared_ptr<const RelAlgNode> old_input,
997  std::shared_ptr<const RelAlgNode> input) override;
998 
999  std::string toString() const override {
1000  std::string result =
1001  "(RelFilter<" + std::to_string(reinterpret_cast<uint64_t>(this)) + ">(";
1002  result += filter_ ? filter_->toString() : "null";
1003  return result + ")";
1004  }
1005 
1006  std::shared_ptr<RelAlgNode> deepCopy() const override;
1007 
1008  private:
1009  std::unique_ptr<const RexScalar> filter_;
1010 };
1011 
1012 // Synthetic node to assist execution of left-deep join relational algebra.
1014  public:
1015  RelLeftDeepInnerJoin(const std::shared_ptr<RelFilter>& filter,
1016  std::vector<std::shared_ptr<const RelAlgNode>> inputs,
1017  std::vector<std::shared_ptr<const RelJoin>>& original_joins);
1018 
1019  const RexScalar* getInnerCondition() const;
1020 
1021  const RexScalar* getOuterCondition(const size_t nesting_level) const;
1022 
1023  std::string toString() const override;
1024 
1025  size_t size() const override;
1026 
1027  std::shared_ptr<RelAlgNode> deepCopy() const override;
1028 
1029  bool coversOriginalNode(const RelAlgNode* node) const;
1030 
1031  private:
1032  std::unique_ptr<const RexScalar> condition_;
1033  std::vector<std::unique_ptr<const RexScalar>> outer_conditions_per_level_;
1034  const std::shared_ptr<RelFilter> original_filter_;
1035  const std::vector<std::shared_ptr<const RelJoin>> original_joins_;
1036 };
1037 
1038 // The 'RelCompound' node combines filter and on the fly aggregate computation.
1039 // It's the result of combining a sequence of 'RelFilter' (optional), 'RelProject',
1040 // 'RelAggregate' (optional) and a simple 'RelProject' (optional) into a single node
1041 // which can be efficiently executed with no intermediate buffers.
1043  public:
1044  // 'target_exprs_' are either scalar expressions owned by 'scalar_sources_'
1045  // or aggregate expressions owned by 'agg_exprs_', with the arguments
1046  // owned by 'scalar_sources_'.
1047  RelCompound(std::unique_ptr<const RexScalar>& filter_expr,
1048  const std::vector<const Rex*>& target_exprs,
1049  const size_t groupby_count,
1050  const std::vector<const RexAgg*>& agg_exprs,
1051  const std::vector<std::string>& fields,
1052  std::vector<std::unique_ptr<const RexScalar>>& scalar_sources,
1053  const bool is_agg,
1054  bool update_disguised_as_select = false,
1055  bool delete_disguised_as_select = false,
1056  bool varlen_update_required = false,
1057  TableDescriptor const* manipulation_target_table = nullptr,
1058  ColumnNameList target_columns = ColumnNameList())
1059  : ModifyManipulationTarget(update_disguised_as_select,
1060  delete_disguised_as_select,
1061  varlen_update_required,
1062  manipulation_target_table,
1063  target_columns)
1064  , filter_expr_(std::move(filter_expr))
1065  , target_exprs_(target_exprs)
1066  , groupby_count_(groupby_count)
1067  , fields_(fields)
1068  , is_agg_(is_agg)
1069  , scalar_sources_(std::move(scalar_sources)) {
1070  CHECK_EQ(fields.size(), target_exprs.size());
1071  for (auto agg_expr : agg_exprs) {
1072  agg_exprs_.emplace_back(agg_expr);
1073  }
1074  }
1075 
1076  void replaceInput(std::shared_ptr<const RelAlgNode> old_input,
1077  std::shared_ptr<const RelAlgNode> input) override;
1078 
1079  size_t size() const override { return target_exprs_.size(); }
1080 
1081  const RexScalar* getFilterExpr() const { return filter_expr_.get(); }
1082 
1083  void setFilterExpr(std::unique_ptr<const RexScalar>& new_expr) {
1084  filter_expr_ = std::move(new_expr);
1085  }
1086 
1087  const Rex* getTargetExpr(const size_t i) const { return target_exprs_[i]; }
1088 
1089  const std::vector<std::string>& getFields() const { return fields_; }
1090 
1091  const std::string getFieldName(const size_t i) const { return fields_[i]; }
1092 
1093  const size_t getScalarSourcesSize() const { return scalar_sources_.size(); }
1094 
1095  const RexScalar* getScalarSource(const size_t i) const {
1096  return scalar_sources_[i].get();
1097  }
1098 
1099  void setScalarSources(std::vector<std::unique_ptr<const RexScalar>>& new_sources) {
1100  CHECK_EQ(new_sources.size(), scalar_sources_.size());
1101  scalar_sources_ = std::move(new_sources);
1102  }
1103 
1104  const size_t getGroupByCount() const { return groupby_count_; }
1105 
1106  bool isAggregate() const { return is_agg_; }
1107 
1108  std::string toString() const override {
1109  std::string result =
1110  "(RelCompound<" + std::to_string(reinterpret_cast<uint64_t>(this)) + ">(";
1111  result += (filter_expr_ ? filter_expr_->toString() : "null") + " ";
1112  for (const auto target_expr : target_exprs_) {
1113  result += target_expr->toString() + " ";
1114  }
1115  result += "groups: [";
1116  for (size_t group_index = 0; group_index < groupby_count_; ++group_index) {
1117  result += " " + std::to_string(group_index);
1118  }
1119  result += " ] sources: [";
1120  for (const auto& scalar_source : scalar_sources_) {
1121  result += " " + scalar_source->toString();
1122  }
1123  return result + " ])";
1124  }
1125 
1126  std::shared_ptr<RelAlgNode> deepCopy() const override;
1127 
1128  private:
1129  std::unique_ptr<const RexScalar> filter_expr_;
1130  const std::vector<const Rex*> target_exprs_;
1131  const size_t groupby_count_;
1132  std::vector<std::unique_ptr<const RexAgg>> agg_exprs_;
1133  const std::vector<std::string> fields_;
1134  const bool is_agg_;
1135  std::vector<std::unique_ptr<const RexScalar>>
1136  scalar_sources_; // building blocks for group_indices_ and agg_exprs_; not actually
1137  // projected, just owned
1138 };
1139 
1140 class RelSort : public RelAlgNode {
1141  public:
1142  RelSort(const std::vector<SortField>& collation,
1143  const size_t limit,
1144  const size_t offset,
1145  std::shared_ptr<const RelAlgNode> input)
1146  : collation_(collation), limit_(limit), offset_(offset) {
1147  inputs_.push_back(input);
1148  }
1149 
1150  bool operator==(const RelSort& that) const {
1151  return limit_ == that.limit_ && offset_ == that.offset_ &&
1152  empty_result_ == that.empty_result_ && hasEquivCollationOf(that);
1153  }
1154 
1155  size_t collationCount() const { return collation_.size(); }
1156 
1157  SortField getCollation(const size_t i) const {
1158  CHECK_LT(i, collation_.size());
1159  return collation_[i];
1160  }
1161 
1162  void setCollation(std::vector<SortField>&& collation) {
1163  collation_ = std::move(collation);
1164  }
1165 
1166  void setEmptyResult(bool emptyResult) { empty_result_ = emptyResult; }
1167 
1168  bool isEmptyResult() const { return empty_result_; }
1169 
1170  size_t getLimit() const { return limit_; }
1171 
1172  size_t getOffset() const { return offset_; }
1173 
1174  std::string toString() const override {
1175  std::string result =
1176  "(RelSort<" + std::to_string(reinterpret_cast<uint64_t>(this)) + ">(";
1177  result += "limit: " + std::to_string(limit_) + " ";
1178  result += "offset: " + std::to_string(offset_) + " ";
1179  result += "empty_result: " + std::to_string(empty_result_) + " ";
1180  result += "collation: [ ";
1181  for (const auto& sort_field : collation_) {
1182  result += sort_field.toString() + " ";
1183  }
1184  result += "]";
1185  return result + ")";
1186  }
1187 
1188  size_t size() const override { return inputs_[0]->size(); }
1189 
1190  std::shared_ptr<RelAlgNode> deepCopy() const override;
1191 
1192  private:
1193  std::vector<SortField> collation_;
1194  const size_t limit_;
1195  const size_t offset_;
1197 
1198  bool hasEquivCollationOf(const RelSort& that) const;
1199 };
1200 
1201 class RelModify : public RelAlgNode {
1202  public:
1203  enum class ModifyOperation { Insert, Delete, Update };
1204  using RelAlgNodeInputPtr = std::shared_ptr<const RelAlgNode>;
1205  using TargetColumnList = std::vector<std::string>;
1206 
1207  static std::string yieldModifyOperationString(ModifyOperation const op) {
1208  switch (op) {
1209  case ModifyOperation::Delete:
1210  return "DELETE";
1211  case ModifyOperation::Insert:
1212  return "INSERT";
1213  case ModifyOperation::Update:
1214  return "UPDATE";
1215  default:
1216  break;
1217  }
1218  throw std::runtime_error("Unexpected ModifyOperation enum encountered.");
1219  }
1220 
1221  static ModifyOperation yieldModifyOperationEnum(std::string const& op_string) {
1222  if (op_string == "INSERT") {
1223  return ModifyOperation::Insert;
1224  } else if (op_string == "DELETE") {
1225  return ModifyOperation::Delete;
1226  } else if (op_string == "UPDATE") {
1227  return ModifyOperation::Update;
1228  }
1229 
1230  throw std::runtime_error(
1231  std::string("Unsupported logical modify operation encountered " + op_string));
1232  }
1233 
1235  TableDescriptor const* const td,
1236  bool flattened,
1237  std::string const& op_string,
1238  TargetColumnList const& target_column_list,
1239  RelAlgNodeInputPtr input)
1240  : catalog_(cat)
1241  , table_descriptor_(td)
1242  , flattened_(flattened)
1243  , operation_(yieldModifyOperationEnum(op_string))
1244  , target_column_list_(target_column_list) {
1245  inputs_.push_back(input);
1246  }
1247 
1249  TableDescriptor const* const td,
1250  bool flattened,
1251  ModifyOperation op,
1252  TargetColumnList const& target_column_list,
1253  RelAlgNodeInputPtr input)
1254  : catalog_(cat)
1255  , table_descriptor_(td)
1256  , flattened_(flattened)
1257  , operation_(op)
1258  , target_column_list_(target_column_list) {
1259  inputs_.push_back(input);
1260  }
1261 
1262  TableDescriptor const* const getTableDescriptor() const { return table_descriptor_; }
1263  bool const isFlattened() const { return flattened_; }
1264  ModifyOperation getOperation() const { return operation_; }
1265  TargetColumnList const& getUpdateColumnNames() { return target_column_list_; }
1266  int getUpdateColumnCount() const { return target_column_list_.size(); }
1267 
1268  size_t size() const override { return 0; }
1269  std::shared_ptr<RelAlgNode> deepCopy() const override {
1270  return std::make_shared<RelModify>(catalog_,
1271  table_descriptor_,
1272  flattened_,
1273  operation_,
1274  target_column_list_,
1275  inputs_[0]);
1276  }
1277 
1278  std::string toString() const override {
1279  std::ostringstream result_stream;
1280  result_stream << std::boolalpha
1281  << "(RelModify<" + std::to_string(reinterpret_cast<uint64_t>(this)) +
1282  "> "
1283  << table_descriptor_->tableName << " flattened= " << flattened_
1284  << " operation= " << yieldModifyOperationString(operation_) << ")";
1285 
1286  return result_stream.str();
1287  }
1288 
1290  RelProject const* previous_project_node =
1291  dynamic_cast<RelProject const*>(inputs_[0].get());
1292  CHECK(previous_project_node != nullptr);
1293 
1294  previous_project_node->setUpdateViaSelectFlag();
1295  // remove the offset column in the projection for update handling
1296  target_column_list_.pop_back();
1297 
1298  previous_project_node->setModifiedTableDescriptor(table_descriptor_);
1299  previous_project_node->setTargetColumns(target_column_list_);
1300 
1301  int target_update_column_expr_start = 0;
1302  int target_update_column_expr_end = (int)(target_column_list_.size() - 1);
1303  CHECK(target_update_column_expr_start >= 0);
1304  CHECK(target_update_column_expr_end >= 0);
1305 
1306  bool varlen_update_required = false;
1307 
1308  auto varlen_scan_visitor = [this,
1309  &varlen_update_required,
1310  target_update_column_expr_start,
1311  target_update_column_expr_end](int index) {
1312  if (index >= target_update_column_expr_start &&
1313  index <= target_update_column_expr_end) {
1314  auto target_index = index - target_update_column_expr_start;
1315 
1316  auto* column_desc = catalog_.getMetadataForColumn(
1317  table_descriptor_->tableId, target_column_list_[target_index]);
1318  CHECK(column_desc);
1319 
1320  if (table_descriptor_->nShards) {
1321  const auto shard_cd =
1322  catalog_.getShardColumnMetadataForTable(table_descriptor_);
1323  CHECK(shard_cd);
1324  if ((column_desc->columnName == shard_cd->columnName)) {
1325  throw std::runtime_error("UPDATE of a shard key is currently unsupported.");
1326  }
1327  }
1328 
1329  // Check for valid types
1330  if (is_feature_enabled<VarlenUpdates>()) {
1331  if (column_desc->columnType.is_varlen()) {
1332  varlen_update_required = true;
1333  }
1334 
1335  if (column_desc->columnType.is_geometry()) {
1336  throw std::runtime_error("UPDATE of a geo column is unsupported.");
1337  }
1338  } else {
1339  if (column_desc->columnType.is_varlen()) {
1340  throw std::runtime_error(
1341  "UPDATE of a none-encoded string, geo, or array column is unsupported.");
1342  }
1343  }
1344  }
1345  };
1346 
1347  previous_project_node->visitScalarExprs(varlen_scan_visitor);
1348  previous_project_node->setVarlenUpdateRequired(varlen_update_required);
1349  }
1350 
1352  RelProject const* previous_project_node =
1353  dynamic_cast<RelProject const*>(inputs_[0].get());
1354  CHECK(previous_project_node != nullptr);
1355  previous_project_node->setDeleteViaSelectFlag();
1356  previous_project_node->injectOffsetInFragmentExpr();
1357  previous_project_node->setModifiedTableDescriptor(table_descriptor_);
1358  }
1359 
1360  private:
1366 };
1367 
1369  public:
1370  RelTableFunction(const std::string& function_name,
1371  std::shared_ptr<const RelAlgNode> input,
1372  std::vector<std::string>& fields,
1373  std::vector<const Rex*> col_inputs,
1374  std::vector<std::unique_ptr<const RexScalar>>& table_func_inputs,
1375  std::vector<std::unique_ptr<const RexScalar>>& target_exprs)
1376  : function_name_(function_name)
1377  , fields_(fields)
1378  , col_inputs_(col_inputs)
1379  , table_func_inputs_(std::move(table_func_inputs))
1380  , target_exprs_(std::move(target_exprs)) {
1381  inputs_.emplace_back(input);
1382  }
1383 
1384  void replaceInput(std::shared_ptr<const RelAlgNode> old_input,
1385  std::shared_ptr<const RelAlgNode> input) override;
1386 
1387  std::string getFunctionName() const { return function_name_; }
1388 
1389  size_t size() const override { return target_exprs_.size(); }
1390 
1391  size_t getTableFuncInputsSize() const { return table_func_inputs_.size(); }
1392 
1393  size_t getColInputsSize() const { return col_inputs_.size(); }
1394 
1395  const RexScalar* getTableFuncInputAt(const size_t idx) const {
1396  CHECK_LT(idx, table_func_inputs_.size());
1397  return table_func_inputs_[idx].get();
1398  }
1399 
1400  const RexScalar* getTableFuncInputAtAndRelease(const size_t idx) {
1401  CHECK_LT(idx, table_func_inputs_.size());
1402  return table_func_inputs_[idx].release();
1403  }
1404 
1405  void setTableFuncInputs(std::vector<std::unique_ptr<const RexScalar>>& exprs) {
1406  table_func_inputs_ = std::move(exprs);
1407  }
1408 
1409  std::string getFieldName(const size_t idx) const {
1410  CHECK_LT(idx, fields_.size());
1411  return fields_[idx];
1412  }
1413 
1414  std::shared_ptr<RelAlgNode> deepCopy() const override;
1415 
1416  std::string toString() const override {
1417  std::string result = "RelTableFunction<" +
1418  std::to_string(reinterpret_cast<uint64_t>(this)) + ">(" +
1419  function_name_ + " ";
1420 
1421  result += "targets: " + std::to_string(target_exprs_.size());
1422  result += "inputs: [";
1423  for (size_t i = 0; i < target_exprs_.size(); ++i) {
1424  result += target_exprs_[i]->toString();
1425  if (i < target_exprs_.size() - 1) {
1426  result += ", ";
1427  }
1428  }
1429  result += "]";
1430 
1431  return result;
1432  }
1433 
1434  private:
1435  std::string function_name_;
1436  std::vector<std::string> fields_;
1437 
1438  std::vector<const Rex*>
1439  col_inputs_; // owned by `table_func_inputs_`, but allows picking out the specific
1440  // input columns vs other table function inputs (e.g. literals)
1441  std::vector<std::unique_ptr<const RexScalar>> table_func_inputs_;
1442 
1443  std::vector<std::unique_ptr<const RexScalar>>
1444  target_exprs_; // Note: these should all be RexRef but are stored as RexScalar for
1445  // consistency
1446 };
1447 
1449  public:
1450  RelLogicalValues(const std::vector<TargetMetaInfo>& tuple_type)
1451  : tuple_type_(tuple_type) {}
1452 
1453  const std::vector<TargetMetaInfo> getTupleType() const { return tuple_type_; }
1454 
1455  std::string toString() const override {
1456  std::string ret =
1457  "(RelLogicalValues<" + std::to_string(reinterpret_cast<uint64_t>(this)) + ">";
1458  for (const auto& target_meta_info : tuple_type_) {
1459  ret += " (" + target_meta_info.get_resname() + " " +
1460  target_meta_info.get_type_info().get_type_name() + ")";
1461  }
1462  ret += " )";
1463  return ret;
1464  }
1465 
1466  size_t size() const override { return tuple_type_.size(); }
1467 
1468  std::shared_ptr<RelAlgNode> deepCopy() const override {
1469  return std::make_shared<RelLogicalValues>(tuple_type_);
1470  }
1471 
1472  private:
1473  const std::vector<TargetMetaInfo> tuple_type_;
1474 };
1475 
1476 class QueryNotSupported : public std::runtime_error {
1477  public:
1478  QueryNotSupported(const std::string& reason) : std::runtime_error(reason) {}
1479 };
1480 
1490 class RelAlgDagBuilder : public boost::noncopyable {
1491  public:
1492  RelAlgDagBuilder() = delete;
1493 
1500  RelAlgDagBuilder(const std::string& query_ra,
1501  const Catalog_Namespace::Catalog& cat,
1502  const RenderInfo* render_info);
1503 
1513  RelAlgDagBuilder(RelAlgDagBuilder& root_dag_builder,
1514  const rapidjson::Value& query_ast,
1515  const Catalog_Namespace::Catalog& cat,
1516  const RenderInfo* render_opts);
1517 
1521  const RelAlgNode& getRootNode() const {
1522  CHECK(nodes_.size());
1523  const auto& last_ptr = nodes_.back();
1524  CHECK(last_ptr);
1525  return *last_ptr;
1526  }
1527 
1528  std::shared_ptr<const RelAlgNode> getRootNodeShPtr() const {
1529  CHECK(nodes_.size());
1530  return nodes_.back();
1531  }
1532 
1537  void registerSubquery(std::shared_ptr<RexSubQuery> subquery) {
1538  subqueries_.push_back(subquery);
1539  }
1540 
1544  const std::vector<std::shared_ptr<RexSubQuery>>& getSubqueries() const {
1545  return subqueries_;
1546  }
1547 
1551  void resetQueryExecutionState();
1552 
1553  private:
1554  void build(const rapidjson::Value& query_ast, RelAlgDagBuilder& root_dag_builder);
1555 
1557  std::vector<std::shared_ptr<RelAlgNode>> nodes_;
1558  std::vector<std::shared_ptr<RexSubQuery>> subqueries_;
1560 };
1561 
1562 std::string tree_string(const RelAlgNode*, const size_t indent = 0);
1563 
1564 using RANodeOutput = std::vector<RexInput>;
1565 
1566 RANodeOutput get_node_output(const RelAlgNode* ra_node);
const size_t getGroupByCount() const
std::string toString() const override
std::vector< std::shared_ptr< const RelAlgNode > > inputs_
std::string toString() const override
std::shared_ptr< const RelAlgNode > getRootNodeShPtr() const
bool is_agg(const Analyzer::Expr *expr)
catalog_(nullptr)
SortField getCollation(const size_t i) const
std::unique_ptr< const RexScalar > condition_
const RexScalar * getThen(const size_t idx) const
SQLAgg
Definition: sqldefs.h:71
#define CHECK_EQ(x, y)
Definition: Logger.h:201
std::unique_ptr< const RexScalar > ConstRexScalarPtr
std::vector< std::unique_ptr< const RexScalar > > getExpressionsAndRelease()
size_t getOffset() const
void setFields(std::vector< std::string > &fields)
void setVarlenUpdateRequired(bool required) const
std::vector< std::unique_ptr< const RexScalar > > outer_conditions_per_level_
const size_t limit_
bool const isFlattened() const
std::unique_ptr< RexSubQuery > deepCopy() const
ConstRexScalarPtrVector getPartitionKeysAndRelease() const
JoinType
Definition: sqldefs.h:107
std::string toString() const override
int getUpdateColumnCount() const
ColumnNameList target_columns_
std::vector< std::unique_ptr< const RexScalar > > table_func_inputs_
std::string toString() const override
const Rex * getTargetExpr(const size_t i) const
RexLiteral(const std::string &val, const SQLTypes type, const SQLTypes target_type, const unsigned scale, const unsigned precision, const unsigned type_scale, const unsigned type_precision)
SQLAgg getKind() const
const std::shared_ptr< const RelAlgNode > ra_
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:81
size_t size() const override
const size_t index_
SQLTypes
Definition: sqltypes.h:41
std::shared_ptr< RelAlgNode > deepCopy() const override
const std::string name_
const RexScalar * getFilterExpr() const
size_t size() const override
const unsigned type_scale_
size_t getOperand(size_t idx) const
std::string toString() const override
std::string toString() const override
const RexScalar * getElse() const
RexOperator(const SQLOps op, std::vector< std::unique_ptr< const RexScalar >> &operands, const SQLTypeInfo &type)
RelCompound(std::unique_ptr< const RexScalar > &filter_expr, const std::vector< const Rex * > &target_exprs, const size_t groupby_count, const std::vector< const RexAgg * > &agg_exprs, const std::vector< std::string > &fields, std::vector< std::unique_ptr< const RexScalar >> &scalar_sources, const bool is_agg, bool update_disguised_as_select=false, bool delete_disguised_as_select=false, bool varlen_update_required=false, TableDescriptor const *manipulation_target_table=nullptr, ColumnNameList target_columns=ColumnNameList())
size_t getIndex() const
static thread_local unsigned crt_id_
void setCondition(std::unique_ptr< const RexScalar > &condition)
void setTargetColumns(ColumnNameList const &target_columns) const
std::string function_name_
const std::string getFieldName(const size_t i) const
std::unique_ptr< RexRef > deepCopy() const
void setEmptyResult(bool emptyResult)
std::unique_ptr< const RexScalar > ConstRexScalarPtr
const JoinType join_type_
NullSortedPosition
void applyDeleteModificationsToInputNode()
bool operator==(const SortField &that) const
std::vector< std::string > TargetColumnList
size_t size() const override
const SQLTypeInfo & getType() const
size_t size() const
const RexScalar * getOperand(const size_t idx) const
const RexWindowBound upper_bound_
const SqlWindowFunctionKind kind_
std::string toString() const override
size_t size() const override
std::vector< const Rex * > col_inputs_
const std::vector< SortField > & getCollation() const
SQLOps
Definition: sqldefs.h:29
const std::vector< std::string > fields_
SortDirection getSortDir() const
const SQLTypeInfo & getType() const
std::string toString() const override
void applyUpdateModificationsToInputNode()
std::string toString() const override
const boost::variant< int64_t, double, std::string, bool, void * > literal_
std::string getFieldName(const size_t idx) const
const RexScalar * getCondition() const
std::shared_ptr< std::shared_ptr< const ExecutionResult > > result_
const std::vector< std::shared_ptr< RexSubQuery > > & getSubqueries() const
RexSubQuery & operator=(const RexSubQuery &)=delete
std::unique_ptr< const RexScalar > else_expr_
const std::vector< TargetMetaInfo > getTupleType() const
void addManagedInput(std::shared_ptr< const RelAlgNode > input)
unsigned getScale() const
const std::vector< TargetMetaInfo > tuple_type_
RexSubQuery(std::shared_ptr< SQLTypeInfo > type, std::shared_ptr< std::shared_ptr< const ExecutionResult >> result, const std::shared_ptr< const RelAlgNode > ra)
constexpr auto is_datetime(T sql_type)
Definition: sqltypes.h:194
size_t getField() const
std::vector< std::string > fields_
std::vector< std::unique_ptr< const RexAgg > > getAggExprsAndRelease()
RexInput(const RelAlgNode *node, const unsigned in_index)
const RexScalar * getWhen(const size_t idx) const
const void * context_data_
void setFilterExpr(std::unique_ptr< const RexScalar > &new_expr)
const RexScalar * getCondition() const
std::shared_ptr< RelAlgNode > deepCopy() const override
bool empty_result_
virtual ~Rex()
std::vector< ConstRexScalarPtr > ConstRexScalarPtrVector
const Catalog_Namespace::Catalog & cat_
size_t operator()(const RexInput &rex_in) const
const RexScalar * getOperandAndRelease(const size_t idx) const
std::vector< std::unique_ptr< const RexScalar > > scalar_sources_
virtual std::shared_ptr< RelAlgNode > deepCopy() const =0
std::shared_ptr< const RelAlgNode > getAndOwnInput(const size_t idx) const
virtual std::unique_ptr< const RexOperator > getDisambiguated(std::vector< std::unique_ptr< const RexScalar >> &operands) const
const SQLOps op_
const std::string getFieldName(const size_t i) const
std::string to_string(char const *&&v)
TableDescriptor const *const getTableDescriptor() const
const SQLAgg agg_
const size_t groupby_count_
unsigned getTypePrecision() const
const std::string getFieldName(const size_t i) const
std::vector< SortField > collation_
std::string get_type_name() const
Definition: sqltypes.h:429
std::string toString() const override
std::string toString() const override
size_t getColInputsSize() const
std::vector< RexLiteral > RexLiteralArray
void setDeleteViaSelectFlag() const
This file contains the class specification and related data structures for Catalog.
const size_t getScalarSourcesSize() const
std::vector< std::shared_ptr< RexSubQuery > > subqueries_
void setExpressions(std::vector< std::unique_ptr< const RexScalar >> &exprs) const
TargetColumnList const & getUpdateColumnNames()
const RenderInfo * render_info_
RexLiteral(const double val, const SQLTypes type, const SQLTypes target_type, const unsigned scale, const unsigned precision, const unsigned type_scale, const unsigned type_precision)
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
const unsigned precision_
unsigned getIndex() const
bool isNop() const
std::string toString() const override
virtual ~RelAlgNode()
SQLOps getOperator() const
std::vector< std::shared_ptr< RelAlgNode > > nodes_
std::shared_ptr< const RelAlgNode > RelAlgNodeInputPtr
CHECK(cgen_state)
unsigned getId() const
const RexScalar * getTableFuncInputAtAndRelease(const size_t idx)
const SQLTypeInfo & getType() const
const bool distinct_
ColumnNameList const & getTargetColumns() const
std::unique_ptr< const RexOperator > disambiguatedOperands(ConstRexScalarPtrVector &operands, ConstRexScalarPtrVector &partition_keys, ConstRexScalarPtrVector &order_keys, const std::vector< SortField > &collation) const
const NullSortedPosition nulls_pos_
const size_t offset_
#define CHECK_NE(x, y)
Definition: Logger.h:202
RexCase(std::vector< std::pair< std::unique_ptr< const RexScalar >, std::unique_ptr< const RexScalar >>> &expr_pair_list, std::unique_ptr< const RexScalar > &else_expr)
NullSortedPosition getNullsPosition() const
void setIndex(const unsigned in_index) const
T getVal() const
const SQLTypeInfo type_
const TableDescriptor * table_descriptor_
const std::vector< std::shared_ptr< const RelJoin > > original_joins_
std::shared_ptr< const RexScalar > offset
std::vector< std::unique_ptr< const RexScalar > > scalar_exprs_
size_t size() const override
std::shared_ptr< SQLTypeInfo > type_
size_t size() const override
const RexScalar * getAndReleaseCondition()
size_t branchCount() const
std::string sql_window_function_to_str(const SqlWindowFunctionKind kind)
RexSubQuery(const std::shared_ptr< const RelAlgNode > ra)
const RelAlgNode * getInput(const size_t idx) const
RexAbstractInput(const unsigned in_index)
RelFilter(std::unique_ptr< const RexScalar > &filter, std::shared_ptr< const RelAlgNode > input)
const int32_t groups_buffer_size return nullptr
Catalog_Namespace::Catalog const & catalog_
const unsigned type_precision_
const RelAlgNode & getRootNode() const
std::string toString() const override
RelAggregate(const size_t groupby_count, std::vector< std::unique_ptr< const RexAgg >> &agg_exprs, const std::vector< std::string > &fields, std::shared_ptr< const RelAlgNode > input)
std::unique_ptr< const RexScalar > filter_
void setCondition(std::unique_ptr< const RexScalar > &condition)
bool isSimple() const
std::vector< std::unique_ptr< const RexScalar > > operands_
const size_t groupby_count_
std::vector< std::string > fields_
RelSort(const std::vector< SortField > &collation, const size_t limit, const size_t offset, std::shared_ptr< const RelAlgNode > input)
const RexScalar * getProjectAtAndRelease(const size_t idx) const
void visitScalarExprs(EXPR_VISITOR_FUNCTOR visitor_functor) const
std::shared_ptr< RelAlgNode > deepCopy() const override
const RelAlgNode * node_
size_t getTableFuncInputsSize() const
std::unique_ptr< RexLiteral > deepCopy() const
virtual void replaceInput(std::shared_ptr< const RelAlgNode > old_input, std::shared_ptr< const RelAlgNode > input)
std::string toString() const
static std::string yieldModifyOperationString(ModifyOperation const op)
ModifyOperation getOperation() const
std::unique_ptr< RexInput > deepCopy() const
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
void setModifiedTableDescriptor(TableDescriptor const *td) const
size_t size() const override
const SQLTypes type_
static ModifyOperation yieldModifyOperationEnum(std::string const &op_string)
void setScalarSources(std::vector< std::unique_ptr< const RexScalar >> &new_sources)
std::vector< std::unique_ptr< const RexAgg > > agg_exprs_
std::vector< TargetMetaInfo > targets_metainfo_
const std::vector< std::unique_ptr< const RexAgg > > & getAggExprs() const
bool isEmptyResult() const
const RelAlgNode * getRelAlg() const
size_t size() const override
SortDirection
RexWindowFunctionOperator(const SqlWindowFunctionKind kind, ConstRexScalarPtrVector &operands, ConstRexScalarPtrVector &partition_keys, ConstRexScalarPtrVector &order_keys, const std::vector< SortField > collation, const RexWindowBound &lower_bound, const RexWindowBound &upper_bound, const bool is_rows, const SQLTypeInfo &ti)
const RexScalar * getProjectAt(const size_t idx) const
const std::vector< SortField > collation_
const std::vector< std::string > & getFields() const
#define CHECK_LT(x, y)
Definition: Logger.h:203
Definition: sqltypes.h:55
bool hasInput(const RelAlgNode *needle) const
const std::vector< std::string > & getFieldNames() const
const std::vector< std::string > & getFields() const
void setCollation(std::vector< SortField > &&collation)
RelLogicalValues(const std::vector< TargetMetaInfo > &tuple_type)
const ConstRexScalarPtrVector & getPartitionKeys() const
const RexWindowBound & getLowerBound() const
std::string toString() const override
const void * getContextData() const
const std::vector< const Rex * > target_exprs_
void setTableFuncInputs(std::vector< std::unique_ptr< const RexScalar >> &exprs)
std::vector< std::unique_ptr< const RexAgg > > agg_exprs_
SortField(const size_t field, const SortDirection sort_dir, const NullSortedPosition nulls_pos)
std::unique_ptr< const RexScalar > filter_expr_
void setSourceNode(const RelAlgNode *node) const
void resetQueryExecutionState()
std::vector< ConstRexScalarPtr > ConstRexScalarPtrVector
ConstRexScalarPtrVector order_keys_
SqlWindowFunctionKind getKind() const
const size_t getGroupByCount() const
RelTableFunction(const std::string &function_name, std::shared_ptr< const RelAlgNode > input, std::vector< std::string > &fields, std::vector< const Rex * > col_inputs, std::vector< std::unique_ptr< const RexScalar >> &table_func_inputs, std::vector< std::unique_ptr< const RexScalar >> &target_exprs)
size_t collationCount() const
RelModify(Catalog_Namespace::Catalog const &cat, TableDescriptor const *const td, bool flattened, ModifyOperation op, TargetColumnList const &target_column_list, RelAlgNodeInputPtr input)
virtual size_t size() const =0
const RelAlgNode * getSourceNode() const
auto const isDeleteViaSelect() const
void registerSubquery(std::shared_ptr< RexSubQuery > subquery)
void setExecutionResult(const std::shared_ptr< const ExecutionResult > result)
bool operator==(const RelSort &that) const
bool isAggregate() const
size_t getLimit() const
std::string toString() const override
std::unique_ptr< const RexOperator > getDisambiguated(std::vector< std::unique_ptr< const RexScalar >> &operands) const override
std::string tree_string(const RelAlgNode *ra, const size_t indent)
RexLiteral(const bool val, const SQLTypes type, const SQLTypes target_type, const unsigned scale, const unsigned precision, const unsigned type_scale, const unsigned type_precision)
std::vector< std::pair< std::unique_ptr< const RexScalar >, std::unique_ptr< const RexScalar > > > expr_pair_list_
SqlWindowFunctionKind
Definition: sqldefs.h:82
RexFunctionOperator(const std::string &name, ConstRexScalarPtrVector &operands, const SQLTypeInfo &ti)
const SQLTypes target_type_
std::unique_ptr< const RexScalar > condition_
const RexWindowBound & getUpperBound() const
const std::vector< std::string > field_names_
const RexScalar * getTableFuncInputAt(const size_t idx) const
unsigned getPrecision() const
std::string getFunctionName() const
std::string toString() const override
virtual std::string toString() const =0
const SortDirection sort_dir_
std::vector< const RexAgg * > getAggregatesAndRelease()
const std::vector< std::string > & getFields() const
bool isDistinct() const
bool g_enable_watchdog false
Definition: Execute.cpp:71
virtual std::string toString() const =0
ModifyOperation operation_
ConstRexScalarPtrVector getOrderKeysAndRelease() const
bool operator==(const RexInput &that) const
const ConstRexScalarPtrVector & getOrderKeys() const
RelProject(std::vector< std::unique_ptr< const RexScalar >> &scalar_exprs, const std::vector< std::string > &fields, std::shared_ptr< const RelAlgNode > input)
auto const isUpdateViaSelect() const
void setOutputMetainfo(const std::vector< TargetMetaInfo > &targets_metainfo) const
SQLTypes getTargetType() const
RANodeOutput get_node_output(const RelAlgNode *ra_node)
std::string toString() const override
size_t size() const
ConstRexScalarPtrVector partition_keys_
std::vector< RexLiteralArray > TupleContentsArray
const SQLTypeInfo type_
const std::vector< size_t > operands_
std::string toString() const override
const std::shared_ptr< RelFilter > original_filter_
void injectOffsetInFragmentExpr() const
std::vector< std::string > ColumnNameList
ModifyManipulationTarget(bool const update_via_select=false, bool const delete_via_select=false, bool const varlen_update_required=false, TableDescriptor const *table_descriptor=nullptr, ColumnNameList target_columns=ColumnNameList())
RexLiteral(const SQLTypes target_type)
QueryNotSupported(const std::string &reason)
Definition: sqltypes.h:48
size_t size() const override
std::vector< RexInput > RANodeOutput
const size_t getAggExprsCount() const
const std::string & getName() const
const size_t inputCount() const
specifies the content in-memory of a row in the table metadata table
auto const isVarlenUpdateRequired() const
RexAgg(const SQLAgg agg, const bool distinct, const SQLTypeInfo &type, const std::vector< size_t > &operands)
unsigned getTypeScale() const
const RexScalar * getAndReleaseCondition() const
const size_t field_
std::string toString() const override
std::unique_ptr< RexAgg > deepCopy() const
const RexWindowBound lower_bound_
RexLiteral(const int64_t val, const SQLTypes type, const SQLTypes target_type, const unsigned scale, const unsigned precision, const unsigned type_scale, const unsigned type_precision)
void setUpdateViaSelectFlag() const
RelScan(const TableDescriptor *td, const std::vector< std::string > &field_names)
SQLTypes getType() const
size_t size() const override
const std::vector< TargetMetaInfo > & getOutputMetainfo() const
const unsigned scale_
const TableDescriptor * getTableDescriptor() const
std::shared_ptr< const ExecutionResult > getExecutionResult() const
void setContextData(const void *context_data) const
TargetColumnList target_column_list_
void setAggExprs(std::vector< std::unique_ptr< const RexAgg >> &agg_exprs)
std::vector< std::string > fields_
JoinType getJoinType() const
bool hasContextData() const
RelJoin(std::shared_ptr< const RelAlgNode > lhs, std::shared_ptr< const RelAlgNode > rhs, std::unique_ptr< const RexScalar > &condition, const JoinType join_type)
RelModify(Catalog_Namespace::Catalog const &cat, TableDescriptor const *const td, bool flattened, std::string const &op_string, TargetColumnList const &target_column_list, RelAlgNodeInputPtr input)
void setFields(std::vector< std::string > &new_fields)
RexRef(const size_t index)
TableDescriptor const * getModifiedTableDescriptor() const
std::string get_compression_name() const
Definition: sqltypes.h:454
const std::string getFieldName(const size_t i) const
const RexScalar * getScalarSource(const size_t i) const
std::vector< std::unique_ptr< const RexScalar > > target_exprs_
const bool is_agg_
bool validateTargetColumns(VALIDATION_FUNCTOR validator) const
std::string toString() const override
const unsigned id_
static void resetRelAlgFirstId() noexcept