Why aggregate functions in PostgreSQL do not work with boolean data type

11,572

Solution 1

Because by definition TRUE equals 1 and FALSE equals 0 I do not understand why casting is necessary.

Per the docs you have quoted in your question, a boolean is not, by definition, 1 for TRUE and 0 for FALSE. It's not true in C either, where TRUE is anything non-zero.

For that matter, nor is it for languages that mimic C in this respect, of which there are many. Nor is it for languages such as Ruby, where anything non-Nil/non-False evaluates to True, including zero and empty strings. Nor is it for POSIX shell and variations thereof, where testing a return code yields TRUE if it is zero, and FALSE for anything non-zero.

Point is, a boolean is a boolean, with all sorts of colorful implementation details from a platform to the next; not an integer.

It's unclear how you were expecting Postgres to average true/false values. I'm suspicious that many if any platform will yield a result for that.

Even summing booleans is awkward: would expecting Postgres to OR the input values, or to count TRUE values?

At any rate, there are some boolean aggregate functions, namely bool_or() and bool_and(). These replace the more standard any() and some(). The reason Postgres deviates from the standard here is due to potential ambiguity. Per the docs:

SELECT b1 = ANY((SELECT b2 FROM t2 ...)) FROM t1 ...;

Here ANY can be considered either as introducing a subquery, or as being an aggregate function, if the subquery returns one row with a Boolean value.

http://www.postgresql.org/docs/current/static/functions-aggregate.html

Solution 2

Here are some possibilities

select max(c::int)::boolean, min(c::int)::boolean, bool_or(c) as max_b,bool_and(c) as min_b from
(
        select false as c
  union select true
  union select null
) t

Solution 3

Here is how one can achieve max(boolean)

CREATE AGGREGATE max(boolean) (
  SFUNC=boolor_statefunc,
  STYPE=bool,
  SORTOP=">"
);  

where "boolor_statefunc" is built in function

Share:
11,572
Tomas Greif
Author by

Tomas Greif

Updated on June 05, 2022

Comments

  • Tomas Greif
    Tomas Greif almost 2 years

    Why we cannot use boolean values in aggregate functions without casting to some integer type first? In many cases it makes perfect sense to calculate sum, average or correlation from columns of boolean data type.

    Consider the following example where boolean input has to be always casted to int in order to make it work:

    select
       sum(boolinput::int),
       avg(boolinput::int),
       max(boolinput::int),
       min(boolinput::int),
       stddev(boolinput::int),
       corr(boolinput::int,boolinputb::int)   
    from
       (select 
          (random() > .5)::boolean as boolinput,
          (random() > .5)::boolean as boolinputB 
        from 
          generate_series(1,100)
       ) a
    

    From PostgreSQL documentation:

    Valid literal values for the "true" state are: TRUE 't' 'true' 'y' 'yes' 'on' '1'

    For the "false" state, the following values can be used: FALSE 'f' 'false' 'n' 'no' 'off' '0'

    Because by definition TRUE equals 1 and FALSE equals 0 I do not understand why casting is necessary.

    Allowing boolean in aggregation would have also interesting side effects - we can for example simplify many case statements:

    Current version (clean and easy to understand):

    select sum(case when gs > 50 then 1 else 0 end) from generate_series(1,100) gs;
    

    Using old fashioned casting operator :::

    select sum((gs > 50)::int) from generate_series(1,100) gs;
    

    Direct aggregation of boolean values (not working currently):

    select sum(gs > 50) from generate_series(1,100) gs;
    

    Is direct aggregation of boolean values possible in other DBMSs? Why this is not possible in PostgreSQL?

  • Tomas Greif
    Tomas Greif over 10 years
    Once there is a rule how to convert boolean to int (and there is such one in PostgreSQL) then there is no issue with aggregate functions definition, even with average, correlation and any other function. Some tools (not DBMSs) can work with boolean type this way (e.g. R).
  • Denis de Bernardy
    Denis de Bernardy over 10 years
    I suspect that case when true then 1 else 0 end should be case $2 when true then 1 else 0 end.
  • Xodarap
    Xodarap over 5 years
    I think this should actually be select $1 + (case when $2 then 1 else 0 end). It is currently hardcoded to always be true