All files / src/abap/2_statements/expressions sql_aggregation.ts

100% Statements 71/71
100% Branches 1/1
100% Functions 1/1
100% Lines 71/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 721x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import {seq, altPrio, tok, Expression, optPrio, ver} from "../combi";
import {ParenLeft, ParenLeftW, WParenRightW, WParenRight, ParenRightW} from "../../1_lexer/tokens";
import {IStatementRunnable} from "../statement_runnable";
import {Version} from "../../../version";
import {SQLFunctionInput} from "./sql_function_input";
import {SQLFieldName} from "./sql_field_name";
import {SQLOver} from "./sql_over";
 
const lparen = altPrio(tok(ParenLeft), tok(ParenLeftW));
const rparen = altPrio(tok(WParenRightW), tok(WParenRight), tok(ParenRightW));
 
export class SQLAggregation extends Expression {
  public getRunnable(): IStatementRunnable {
    const arg = altPrio(ver(Version.v740sp08, SQLFunctionInput), SQLFieldName);
    const avgRparen = altPrio(tok(WParenRightW), tok(WParenRight), tok(ParenRightW));
    const lenDecimals = seq(tok(ParenLeftW), SQLFunctionInput, ",", SQLFunctionInput, tok(WParenRightW));
    const avgCastType = altPrio(
      seq("DEC", lenDecimals),
      seq("CURR", lenDecimals),
      seq("QUAN", lenDecimals),
      "D16N",
      "D34N",
      "FLTP",
    );
 
    const count = seq("COUNT", lparen, optPrio("DISTINCT"), altPrio("*", arg), rparen, optPrio(SQLOver));
    const max = seq("MAX", lparen, optPrio("DISTINCT"), arg, rparen, optPrio(SQLOver));
    const min = seq("MIN", lparen, optPrio("DISTINCT"), arg, rparen, optPrio(SQLOver));
    const sum = seq("SUM", lparen, optPrio("DISTINCT"), arg, rparen, optPrio(SQLOver));
    const avg = seq("AVG", tok(ParenLeftW), optPrio("DISTINCT"), arg,
                    optPrio(ver(Version.v751, seq("AS", avgCastType))), avgRparen, optPrio(SQLOver));
    const rank = ver(Version.v757, seq(altPrio("ROW_NUMBER", "RANK", "DENSE_RANK"), lparen, rparen, SQLOver));
    const leadLag = ver(Version.v757, seq(altPrio("LEAD", "LAG"),
                                          tok(ParenLeftW),
                                          SQLFunctionInput,
                                          optPrio(seq(",", SQLFunctionInput, optPrio(seq(",", SQLFunctionInput)))),
                                          rparen,
                                          SQLOver));
    const firstLastValue = ver(Version.v757, seq(altPrio("FIRST_VALUE", "LAST_VALUE"),
                                                 tok(ParenLeftW), SQLFunctionInput, rparen,
                                                 SQLOver));
    const stringAgg = ver(Version.v757, seq("STRING_AGG",
                                            tok(ParenLeftW),
                                            SQLFunctionInput,
                                            optPrio(seq(",", SQLFunctionInput)),
                                            rparen,
                                            optPrio(SQLOver)));
    const ntile = ver(Version.v757, seq("NTILE", tok(ParenLeftW), SQLFunctionInput, rparen, SQLOver));
    const corr = ver(Version.v757, seq(altPrio("CORR_SPEARMAN", "CORR"),
                                       tok(ParenLeftW), SQLFunctionInput, ",", SQLFunctionInput, rparen,
                                       optPrio(SQLOver)));
    const stat = ver(Version.v757, seq(altPrio("PRODUCT", "MEDIAN", "VAR", "STDDEV"),
                                       tok(ParenLeftW), SQLFunctionInput, rparen,
                                       optPrio(SQLOver)));
 
    return altPrio(
      rank,
      leadLag,
      firstLastValue,
      stringAgg,
      ntile,
      corr,
      stat,
      count,
      max,
      min,
      sum,
      avg,
    );
  }
}