prql icon indicating copy to clipboard operation
prql copied to clipboard

`count` vs `count *`

Open max-sixty opened this issue 4 years ago • 5 comments

In the example functions, there are two functions with the same name:

func count = s"COUNT(*)"
func count column = s"COUNT({column})"

We need to decide how to handle these.

Options:

  • Don't allow this — count * would be required rather than just count
    • This would be tempting to use in the short term, but has the disadvantage that we'd need to special-case the *, since it's currently an operator (i.e. foo * 3). I don't think it's a CS PhD to implement, but it's the sort of special-casing that causes issues later, both in the code and our ability to provide good error messages.
    • We could find a different symbol than *, but that's also unfamiliar.
  • Multiple dispatch — i.e. the compiler allows for multiple functions with the same name as long as they have different signatures, and it selects the function that matches the signature. In this case the number of positional args defines the signature. So the function definitions above would be correct.
  • Use a named argument — so func count column="*" = s"COUNT({column})". But then people have to write count column:salary, which isn't friendly — and either we do it everywhere, or it's unfamiliar in the count case (there's no sum *).
  • Allow optional positional args, and find a different way of specifying named args. As a reminder, this is our current function specification, and has the advantage that it's extremely simple — only two types of arguments, with named arguments defined by having a default. This is python's.

max-sixty avatar Mar 05 '22 18:03 max-sixty

I vote multi dispatch, based on the number of arguments. Nothing fancy, just match the number of arguments supplied to the number defined.

qorrect avatar Mar 06 '22 17:03 qorrect

As discussed on discord, one downside of multiple dispatch which I forgot to mention, is it preludes partials / currying; e.g.:

func add a b = a + b
func plus_one = add 1
# Does `add 1` curry `add a b`? Or refer to a one-arg version of `add`?

(though I don't think it's that important, and definitely not in the short term)

max-sixty avatar Mar 09 '22 16:03 max-sixty

This is the last item in https://github.com/max-sixty/prql/milestone/1 and I can't think of sufficiently general solution to the problem. I was leaning to count_all, but it's just awkward. I'm fine sacrificing currying, but adding multiple dispatch isn't trivial and doesn't seem to serve any purpose beyond this specific case.

I think I'm going to hack the parser so count * is allowed, and then we can iterate from there.

max-sixty avatar Mar 20 '22 21:03 max-sixty

We're going to have the same issue with select foo_table.*

One approach here that might work is count ., or select foo_table.. I think that could work quite well. Any thoughts?

max-sixty avatar Mar 28 '22 02:03 max-sixty

In my latest patch, both count and count first_name can coexist, so we can parse * only as part of ident or as operator.

aljazerzen avatar Mar 28 '22 09:03 aljazerzen