MCPcopy
hub / github.com/ponyorm/pony / aggregate

Method aggregate

pony/orm/sqltranslation.py:1524–1565  ·  view source on GitHub ↗
(monad, func_name, distinct=None, sep=None)

Source from the content-addressed store, hash-verified

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)

Callers 10

callMethod · 0.45
callMethod · 0.45
callMethod · 0.45
callMethod · 0.45
callMethod · 0.45
call_sumMethod · 0.45
call_minMethod · 0.45
call_maxMethod · 0.45
call_avgMethod · 0.45
call_group_concatMethod · 0.45

Calls 7

throwFunction · 0.90
distinct_from_monadFunction · 0.85
type2strFunction · 0.85
appendMethod · 0.80
to_realMethod · 0.45
getsqlMethod · 0.45
newMethod · 0.45

Tested by

no test coverage detected