(monad, func_name, distinct=None, sep=None)
| 1522 | result.aggregated = True |
| 1523 | return result |
| 1524 | def aggregate(monad, func_name, distinct=None, sep=None): |
| 1525 | distinct = distinct_from_monad(distinct) |
| 1526 | translator = monad.translator |
| 1527 | if monad.aggregated: throw(TranslationError, 'Aggregated functions cannot be nested. Got: {EXPR}') |
| 1528 | expr_type = monad.type |
| 1529 | # if isinstance(expr_type, SetType): expr_type = expr_type.item_type |
| 1530 | if func_name in ('SUM', 'AVG'): |
| 1531 | if expr_type not in numeric_types: |
| 1532 | if expr_type is Json: monad = monad.to_real() |
| 1533 | else: throw(TypeError, "Function '%s' expects argument of numeric type, got %r in {EXPR}" |
| 1534 | % (func_name, type2str(expr_type))) |
| 1535 | elif func_name in ('MIN', 'MAX'): |
| 1536 | if expr_type not in comparable_types: |
| 1537 | throw(TypeError, "Function '%s' cannot be applied to type %r in {EXPR}" |
| 1538 | % (func_name, type2str(expr_type))) |
| 1539 | elif func_name == 'GROUP_CONCAT': |
| 1540 | if isinstance(expr_type, EntityMeta) and expr_type._pk_is_composite_: |
| 1541 | throw(TypeError, "`group_concat` cannot be used with entity with composite primary key") |
| 1542 | else: assert False # pragma: no cover |
| 1543 | expr = monad.getsql() |
| 1544 | if len(expr) == 1: expr = expr[0] |
| 1545 | elif translator.row_value_syntax: expr = ['ROW'] + expr |
| 1546 | else: throw(NotImplementedError, |
| 1547 | '%s database provider does not support entities ' |
| 1548 | 'with composite primary keys inside aggregate functions. Got: {EXPR} ' |
| 1549 | '(you can suggest us how to write SQL for this query)' |
| 1550 | % translator.dialect) |
| 1551 | if func_name == 'AVG': |
| 1552 | result_type = float |
| 1553 | elif func_name == 'GROUP_CONCAT': |
| 1554 | result_type = str |
| 1555 | else: |
| 1556 | result_type = expr_type |
| 1557 | if distinct is None: |
| 1558 | distinct = getattr(monad, 'forced_distinct', False) and func_name in ('SUM', 'AVG') |
| 1559 | aggr_ast = [ func_name, distinct, expr ] |
| 1560 | if func_name == 'GROUP_CONCAT': |
| 1561 | if sep is not None: |
| 1562 | aggr_ast.append(['VALUE', sep]) |
| 1563 | result = ExprMonad.new(result_type, aggr_ast, nullable=True) |
| 1564 | result.aggregated = True |
| 1565 | return result |
| 1566 | def __call__(monad, *args, **kwargs): throw(TypeError) |
| 1567 | def __getitem__(monad, key): throw(TypeError) |
| 1568 | def __add__(monad, monad2): throw(TypeError) |
no test coverage detected