KuceraMartin / tyqu

Type-safe Queries
Apache License 2.0
50 stars 3 forks source link

selections with clashing names #16

Closed KuceraMartin closed 1 year ago

KuceraMartin commented 1 year ago

right now we allow something like

from(People).map{ p => (p.firstName.as("name"), p.lastName.as("name") }

and we translate it as

SELECT first_name AS name, last_name AS name
FROM people

which is accepted (at least by Postgres) but weird. If we run this in a subquery and try to access name from the outer query, we get ERROR: column reference "name" is ambiguous.

Solutions:

KuceraMartin commented 1 year ago

could we reuse one macro instead of having 4 different ones?

e.g. use :* n times to implement ++ -> we seem to lose the information about the generic arguments of NamedExpression in the process :/

KuceraMartin commented 1 year ago

overwriting is okay

KuceraMartin commented 1 year ago

put a simplified example in the other repo

KuceraMartin commented 1 year ago

so far managed to implement with custom replace function:

def replace(columns: Tuple, name: String, replacement: Expression[_]): Tuple =
    columns match
      case EmptyTuple => EmptyTuple
      case (h: NamedExpression[_, _]) *: t =>
        (if (h.alias == name) replacement else h) *: replace(t, name, replacement)

but would be nice if we could just use Tuple.map or something like that