OmniSciDB  ab4938a6a3
Parser::FunctionRef Class Reference

#include <ParserNode.h>

+ Inheritance diagram for Parser::FunctionRef:
+ Collaboration diagram for Parser::FunctionRef:

Public Member Functions

 FunctionRef (std::string *n)
 
 FunctionRef (std::string *n, Expr *a)
 
 FunctionRef (std::string *n, bool d, Expr *a)
 
const std::string * get_name () const
 
bool get_distinct () const
 
Exprget_arg () const
 
std::shared_ptr< Analyzer::Expranalyze (const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query, TlistRefType allow_tlist_ref=TLIST_NONE) const override
 
std::string to_string () const override
 
- Public Member Functions inherited from Parser::Node
virtual ~Node ()
 

Private Attributes

std::unique_ptr< std::string > name
 
bool distinct
 
std::unique_ptr< Exprarg
 

Additional Inherited Members

- Public Types inherited from Parser::Expr
enum  TlistRefType { TLIST_NONE, TLIST_REF, TLIST_COPY }
 

Detailed Description

Definition at line 615 of file ParserNode.h.

Constructor & Destructor Documentation

◆ FunctionRef() [1/3]

Parser::FunctionRef::FunctionRef ( std::string *  n)
inlineexplicit

Definition at line 617 of file ParserNode.h.

617 : name(n), distinct(false), arg(nullptr) {}
std::unique_ptr< Expr > arg
Definition: ParserNode.h:632
std::unique_ptr< std::string > name
Definition: ParserNode.h:630

◆ FunctionRef() [2/3]

Parser::FunctionRef::FunctionRef ( std::string *  n,
Expr a 
)
inline

Definition at line 618 of file ParserNode.h.

618 : name(n), distinct(false), arg(a) {}
std::unique_ptr< Expr > arg
Definition: ParserNode.h:632
std::unique_ptr< std::string > name
Definition: ParserNode.h:630

◆ FunctionRef() [3/3]

Parser::FunctionRef::FunctionRef ( std::string *  n,
bool  d,
Expr a 
)
inline

Definition at line 619 of file ParserNode.h.

619 : name(n), distinct(d), arg(a) {}
std::unique_ptr< Expr > arg
Definition: ParserNode.h:632
std::unique_ptr< std::string > name
Definition: ParserNode.h:630

Member Function Documentation

◆ analyze()

std::shared_ptr< Analyzer::Expr > Parser::FunctionRef::analyze ( const Catalog_Namespace::Catalog catalog,
Analyzer::Query query,
TlistRefType  allow_tlist_ref = TLIST_NONE 
) const
overridevirtual

Implements Parser::Expr.

Definition at line 769 of file ParserNode.cpp.

References SQLTypeInfo::get_compression(), SQLTypeInfo::get_elem_type(), Analyzer::Query::get_num_aggs(), SQLTypeInfo::get_type(), anonymous_namespace{RelAlgOptimizer.cpp}::is_distinct(), SQLTypeInfo::is_integer(), SQLTypeInfo::is_string(), kARRAY, kAVG, kBIGINT, kCOUNT, kDOUBLE, kENCODING_DICT, kMAX, kMIN, kSUM, kUNNEST, and Analyzer::Query::set_num_aggs().

772  {
773  SQLTypeInfo result_type;
774  SQLAgg agg_type;
775  std::shared_ptr<Analyzer::Expr> arg_expr;
776  bool is_distinct = false;
777  if (boost::iequals(*name, "count")) {
778  result_type = SQLTypeInfo(kBIGINT, false);
779  agg_type = kCOUNT;
780  if (arg) {
781  arg_expr = arg->analyze(catalog, query, allow_tlist_ref);
782  const SQLTypeInfo& ti = arg_expr->get_type_info();
783  if (ti.is_string() && (ti.get_compression() != kENCODING_DICT || !distinct)) {
784  throw std::runtime_error(
785  "Strings must be dictionary-encoded in COUNT(DISTINCT).");
786  }
787  if (ti.get_type() == kARRAY && !distinct) {
788  throw std::runtime_error("Only COUNT(DISTINCT) is supported on arrays.");
789  }
790  }
791  is_distinct = distinct;
792  } else {
793  if (!arg) {
794  throw std::runtime_error("Cannot compute " + *name + " with argument '*'.");
795  }
796  if (boost::iequals(*name, "min")) {
797  agg_type = kMIN;
798  arg_expr = arg->analyze(catalog, query, allow_tlist_ref);
799  arg_expr = arg_expr->decompress();
800  result_type = arg_expr->get_type_info();
801  } else if (boost::iequals(*name, "max")) {
802  agg_type = kMAX;
803  arg_expr = arg->analyze(catalog, query, allow_tlist_ref);
804  arg_expr = arg_expr->decompress();
805  result_type = arg_expr->get_type_info();
806  } else if (boost::iequals(*name, "avg")) {
807  agg_type = kAVG;
808  arg_expr = arg->analyze(catalog, query, allow_tlist_ref);
809  if (!arg_expr->get_type_info().is_number()) {
810  throw std::runtime_error("Cannot compute AVG on non-number-type arguments.");
811  }
812  arg_expr = arg_expr->decompress();
813  result_type = SQLTypeInfo(kDOUBLE, false);
814  } else if (boost::iequals(*name, "sum")) {
815  agg_type = kSUM;
816  arg_expr = arg->analyze(catalog, query, allow_tlist_ref);
817  if (!arg_expr->get_type_info().is_number()) {
818  throw std::runtime_error("Cannot compute SUM on non-number-type arguments.");
819  }
820  arg_expr = arg_expr->decompress();
821  result_type = arg_expr->get_type_info().is_integer() ? SQLTypeInfo(kBIGINT, false)
822  : arg_expr->get_type_info();
823  } else if (boost::iequals(*name, "unnest")) {
824  arg_expr = arg->analyze(catalog, query, allow_tlist_ref);
825  const SQLTypeInfo& arg_ti = arg_expr->get_type_info();
826  if (arg_ti.get_type() != kARRAY) {
827  throw std::runtime_error(arg->to_string() + " is not of array type.");
828  }
829  return makeExpr<Analyzer::UOper>(arg_ti.get_elem_type(), false, kUNNEST, arg_expr);
830  } else {
831  throw std::runtime_error("invalid function name: " + *name);
832  }
833  if (arg_expr->get_type_info().is_string() ||
834  arg_expr->get_type_info().get_type() == kARRAY) {
835  throw std::runtime_error(
836  "Only COUNT(DISTINCT ) aggregate is supported on strings and arrays.");
837  }
838  }
839  int naggs = query.get_num_aggs();
840  query.set_num_aggs(naggs + 1);
841  return makeExpr<Analyzer::AggExpr>(
842  result_type, agg_type, arg_expr, is_distinct, nullptr);
843 }
SQLAgg
Definition: sqldefs.h:71
bool is_string() const
Definition: sqltypes.h:409
bool is_integer() const
Definition: sqltypes.h:411
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:266
Definition: sqldefs.h:73
std::unique_ptr< Expr > arg
Definition: ParserNode.h:632
Definition: sqldefs.h:75
void set_num_aggs(int a)
Definition: Analyzer.h:1547
Definition: sqldefs.h:76
int get_num_aggs() const
Definition: Analyzer.h:1518
std::unique_ptr< std::string > name
Definition: ParserNode.h:630
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:617
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:258
Definition: sqldefs.h:74
Definition: sqldefs.h:72
bool is_distinct(const size_t input_idx, const RelAlgNode *node)
+ Here is the call graph for this function:

◆ get_arg()

Expr* Parser::FunctionRef::get_arg ( ) const
inline

Definition at line 622 of file ParserNode.h.

References to_string().

622 { return arg.get(); }
std::unique_ptr< Expr > arg
Definition: ParserNode.h:632
+ Here is the call graph for this function:

◆ get_distinct()

bool Parser::FunctionRef::get_distinct ( ) const
inline

Definition at line 621 of file ParserNode.h.

621 { return distinct; }

◆ get_name()

const std::string* Parser::FunctionRef::get_name ( ) const
inline

Definition at line 620 of file ParserNode.h.

620 { return name.get(); }
std::unique_ptr< std::string > name
Definition: ParserNode.h:630

◆ to_string()

std::string Parser::FunctionRef::to_string ( ) const
overridevirtual

Implements Parser::Expr.

Definition at line 1404 of file ParserNode.cpp.

1404  {
1405  std::string str = *name + "(";
1406  if (distinct) {
1407  str += "DISTINCT ";
1408  }
1409  if (arg == nullptr) {
1410  str += "*)";
1411  } else {
1412  str += arg->to_string() + ")";
1413  }
1414  return str;
1415 }
std::unique_ptr< Expr > arg
Definition: ParserNode.h:632
std::unique_ptr< std::string > name
Definition: ParserNode.h:630

Member Data Documentation

◆ arg

std::unique_ptr<Expr> Parser::FunctionRef::arg
private

Definition at line 632 of file ParserNode.h.

◆ distinct

bool Parser::FunctionRef::distinct
private

Definition at line 631 of file ParserNode.h.

◆ name

std::unique_ptr<std::string> Parser::FunctionRef::name
private

Definition at line 630 of file ParserNode.h.


The documentation for this class was generated from the following files: