QueryEngine/RelAlgDagBuilder.cpp
// Detect both window function operators and window function operators embedded in case
// statements (for null handling)
Detect both window function operators and window function operators embedded in case
statements (for null handling)
Detect both window function operators and window function operators embedded in case statements ( for null handling )
bool is_window_function_operator(const RexScalar* rex) {
  if (dynamic_cast<const RexWindowFunctionOperator*>(rex)) {
    return true;
  }
  const auto rex_cast = dynamic_cast<const RexOperator*>(rex);
  if (rex_cast && rex_cast->getOperator() == kCAST) {
    CHECK_EQ(rex_cast->size(), size_t(1));
    return is_window_function_operator(rex_cast->getOperand(0));
  }
  if (is_window_function_sum(rex)) {
    return true;
  }
  const RexOperator* divide_operator = dynamic_cast<const RexOperator*>(rex);
  if (divide_operator && divide_operator->getOperator() == kDIVIDE) {
    CHECK_EQ(divide_operator->size(), size_t(2));
    const auto case_operator =
        dynamic_cast<const RexCase*>(divide_operator->getOperand(0));
    const auto second_window =
        dynamic_cast<const RexWindowFunctionOperator*>(divide_operator->getOperand(1));
    if (case_operator && second_window &&
        second_window->getKind() == SqlWindowFunctionKind::COUNT) {
      if (is_window_function_sum(case_operator)) {
        return true;
      }
    }
  }
  return false;
}
bool is_window_function_operator ( const RexScalar * rex ) { if ( dynamic_cast < const RexWindowFunctionOperator * > ( rex ) ) { return true ; } const auto rex_cast = dynamic_cast < const RexOperator * > ( rex ) ; if ( rex_cast && rex_cast -> getOperator ( ) == kCAST ) { CHECK_EQ ( rex_cast -> size ( ) , size_t ( 1 ) ) ; return is_window_function_operator ( rex_cast -> getOperand ( 0 ) ) ; } if ( is_window_function_sum ( rex ) ) { return true ; } const RexOperator * divide_operator = dynamic_cast < const RexOperator * > ( rex ) ; if ( divide_operator && divide_operator -> getOperator ( ) == kDIVIDE ) { CHECK_EQ ( divide_operator -> size ( ) , size_t ( 2 ) ) ; const auto case_operator = dynamic_cast < const RexCase * > ( divide_operator -> getOperand ( 0 ) ) ; const auto second_window = dynamic_cast < const RexWindowFunctionOperator * > ( divide_operator -> getOperand ( 1 ) ) ; if ( case_operator && second_window && second_window -> getKind ( ) == SqlWindowFunctionKind :: COUNT ) { if ( is_window_function_sum ( case_operator ) ) { return true ; } } } return false ; }
Detect both window function operators and window function operators embedded in case
statements (for null handling)
Detect both window function operators and window function operators embedded in case statements ( for null handling )
// unwrap from casts, if they exist // Check for Window Function AVG: // (CASE WHEN count > 0 THEN sum ELSE 0) / COUNT