src/backend/bridge/dml/expr/expr_transformer.cpp
/**
 * @brief Helper function: re-map Postgres's builtin function
 * to proper expression type in Peloton
 *
 * @param pg_func_id  PG Function Id used to lookup function in fmrg_builtin[]
 * (see Postgres source file 'fmgrtab.cpp')
 * @param args  The argument list in PG ExprState
 * @return            Corresponding expression tree in peloton.
 */
@brief Helper function: re-map Postgres's builtin function
to proper expression type in Peloton

@param pg_func_id  PG Function Id used to lookup function in fmrg_builtin[]

@param args  The argument list in PG ExprState
@return            Corresponding expression tree in peloton.
@brief Helper function : re - map Postgres ' s builtin function to proper expression type in Peloton @param pg_func_id PG Function Id used to lookup function in fmrg_builtin [] @param args The argument list in PG ExprState @return Corresponding expression tree in peloton .
expression::AbstractExpression *ExprTransformer::ReMapPgFunc(Oid pg_func_id,
                                                             ValueType ret_type,
                                                             List *args) {
  assert(pg_func_id > 0);
  auto itr = kPgFuncMap.find(pg_func_id);
  if (itr == kPgFuncMap.end()) {
    LOG_ERROR("Unsupported PG Op Function ID : %u (check fmgrtab.cpp)",
              pg_func_id);
    return nullptr;
  }
  PltFuncMetaInfo func_meta = itr->second;
  if (func_meta.exprtype == EXPRESSION_TYPE_CAST) {
    return expression::ExpressionUtil::CastFactory(VALUE_TYPE_INVALID, nullptr);
  }
  assert(list_length(args) <=
         EXPRESSION_MAX_ARG_NUM);  
  assert(func_meta.nargs <= EXPRESSION_MAX_ARG_NUM);
  expression::AbstractExpression *children[EXPRESSION_MAX_ARG_NUM] = {};
  int i = 0;
  ListCell *arg;
  foreach (arg, args) {
    ExprState *argstate = (ExprState *)lfirst(arg);
    if (i >= func_meta.nargs) break;
    children[i] = TransformExpr(argstate);
    i++;
  }
  auto plt_exprtype = func_meta.exprtype;
  switch (plt_exprtype) {
    case EXPRESSION_TYPE_COMPARE_EQUAL:
    case EXPRESSION_TYPE_COMPARE_NOTEQUAL:
    case EXPRESSION_TYPE_COMPARE_GREATERTHAN:
    case EXPRESSION_TYPE_COMPARE_LESSTHAN:
    case EXPRESSION_TYPE_COMPARE_GREATERTHANOREQUALTO:
    case EXPRESSION_TYPE_COMPARE_LESSTHANOREQUALTO:
    case EXPRESSION_TYPE_COMPARE_LIKE:
    case EXPRESSION_TYPE_COMPARE_NOTLIKE:
      ASSERT(ret_type == VALUE_TYPE_BOOLEAN);
      return expression::ExpressionUtil::ComparisonFactory(
          plt_exprtype, children[0], children[1]);
    case EXPRESSION_TYPE_OPERATOR_PLUS:
    case EXPRESSION_TYPE_OPERATOR_MINUS:
    case EXPRESSION_TYPE_OPERATOR_MULTIPLY:
    case EXPRESSION_TYPE_OPERATOR_DIVIDE:
    case EXPRESSION_TYPE_OPERATOR_MOD:
    case EXPRESSION_TYPE_SUBSTR:
    case EXPRESSION_TYPE_ASCII:
    case EXPRESSION_TYPE_OCTET_LEN:
    case EXPRESSION_TYPE_CHAR:
    case EXPRESSION_TYPE_CHAR_LEN:
    case EXPRESSION_TYPE_SPACE:
    case EXPRESSION_TYPE_CONCAT:
    case EXPRESSION_TYPE_OVERLAY:
    case EXPRESSION_TYPE_LEFT:
    case EXPRESSION_TYPE_RIGHT:
    case EXPRESSION_TYPE_RTRIM:
    case EXPRESSION_TYPE_LTRIM:
    case EXPRESSION_TYPE_BTRIM:
    case EXPRESSION_TYPE_REPLACE:
    case EXPRESSION_TYPE_REPEAT:
    case EXPRESSION_TYPE_POSITION:
    case EXPRESSION_TYPE_EXTRACT:
    case EXPRESSION_TYPE_DATE_TO_TIMESTAMP:
    case EXPRESSION_TYPE_OPERATOR_UNARY_MINUS:
      return expression::ExpressionUtil::OperatorFactory(
          plt_exprtype, ret_type, children[0], children[1], children[2],
          children[3]);
    default:
      LOG_ERROR(
          "This Peloton ExpressionType is in our map but not transformed here "
          ": %u",
          plt_exprtype);
  }
  return nullptr;
}
expression :: AbstractExpression * ExprTransformer :: ReMapPgFunc ( Oid pg_func_id , ValueType ret_type , List * args ) { assert ( pg_func_id > 0 ) ; auto itr = kPgFuncMap . find ( pg_func_id ) ; if ( itr == kPgFuncMap . end ( ) ) { LOG_ERROR ( " " , pg_func_id ) ; return nullptr ; } PltFuncMetaInfo func_meta = itr -> second ; if ( func_meta . exprtype == EXPRESSION_TYPE_CAST ) { return expression :: ExpressionUtil :: CastFactory ( VALUE_TYPE_INVALID , nullptr ) ; } assert ( list_length ( args ) <= EXPRESSION_MAX_ARG_NUM ) ; assert ( func_meta . nargs <= EXPRESSION_MAX_ARG_NUM ) ; expression :: AbstractExpression * children [ EXPRESSION_MAX_ARG_NUM ] = { } ; int i = 0 ; ListCell * arg ; foreach ( arg , args ) { ExprState * argstate = ( ExprState * ) lfirst ( arg ) ; if ( i >= func_meta . nargs ) break ; children [ i ] = TransformExpr ( argstate ) ; i ++ ; } auto plt_exprtype = func_meta . exprtype ; switch ( plt_exprtype ) { case EXPRESSION_TYPE_COMPARE_EQUAL : case EXPRESSION_TYPE_COMPARE_NOTEQUAL : case EXPRESSION_TYPE_COMPARE_GREATERTHAN : case EXPRESSION_TYPE_COMPARE_LESSTHAN : case EXPRESSION_TYPE_COMPARE_GREATERTHANOREQUALTO : case EXPRESSION_TYPE_COMPARE_LESSTHANOREQUALTO : case EXPRESSION_TYPE_COMPARE_LIKE : case EXPRESSION_TYPE_COMPARE_NOTLIKE : ASSERT ( ret_type == VALUE_TYPE_BOOLEAN ) ; return expression :: ExpressionUtil :: ComparisonFactory ( plt_exprtype , children [ 0 ] , children [ 1 ] ) ; case EXPRESSION_TYPE_OPERATOR_PLUS : case EXPRESSION_TYPE_OPERATOR_MINUS : case EXPRESSION_TYPE_OPERATOR_MULTIPLY : case EXPRESSION_TYPE_OPERATOR_DIVIDE : case EXPRESSION_TYPE_OPERATOR_MOD : case EXPRESSION_TYPE_SUBSTR : case EXPRESSION_TYPE_ASCII : case EXPRESSION_TYPE_OCTET_LEN : case EXPRESSION_TYPE_CHAR : case EXPRESSION_TYPE_CHAR_LEN : case EXPRESSION_TYPE_SPACE : case EXPRESSION_TYPE_CONCAT : case EXPRESSION_TYPE_OVERLAY : case EXPRESSION_TYPE_LEFT : case EXPRESSION_TYPE_RIGHT : case EXPRESSION_TYPE_RTRIM : case EXPRESSION_TYPE_LTRIM : case EXPRESSION_TYPE_BTRIM : case EXPRESSION_TYPE_REPLACE : case EXPRESSION_TYPE_REPEAT : case EXPRESSION_TYPE_POSITION : case EXPRESSION_TYPE_EXTRACT : case EXPRESSION_TYPE_DATE_TO_TIMESTAMP : case EXPRESSION_TYPE_OPERATOR_UNARY_MINUS : return expression :: ExpressionUtil :: OperatorFactory ( plt_exprtype , ret_type , children [ 0 ] , children [ 1 ] , children [ 2 ] , children [ 3 ] ) ; default : LOG_ERROR ( " " " " , plt_exprtype ) ; } return nullptr ; }
@brief Helper function: re-map Postgres's builtin function
 to proper expression type in Peloton
@brief Helper function : re - map Postgres ' s builtin function to proper expression type in Peloton
// Perform lookup // it is a cast, but we need casted type and the child expr. // so we just create an empty cast expr and return it. // mperron some string functions have 4 children // Hopefully it has at most three parameters // Extract function arguments (at most four) // Construct the corresponding Peloton expression // These are all boolean expressions