prql icon indicating copy to clipboard operation
prql copied to clipboard

Table aliases

Open aljazerzen opened this issue 4 years ago • 8 comments

So as SQL does, we can support table aliases. This will come useful when doing joins because of possible duplicate columns names.

To be consistent with derive, I would suggest:

from e:employees
select [e.emp_no, e.gender]

With joins:

from e:employees
join s:salaries side:left [s.emp_no = e.emp_no]
select [e.gender, s.salary]

Problem here is how to parse function parameters. If you don't use aliases, from and join have 1 and 2 positional parameters, respectfully. If you do, they have 0 and 1. This is not consistent with current rules around function parameters.

Any other suggestions?

Also, do you think we even need aliases?

aljazerzen avatar Mar 22 '22 15:03 aljazerzen

Also, do you think we even need aliases?

I'll start with the bigger question!

I have a personal opinion, but with less confidence than other ideas. So I would be fine to go ahead with this if people would like it.

My opinion is that we should try and reduce the need for aliases. Their role is to make queries terser when we have identically named columns in multiple tables, and:

  1. Having dozens of highly normalized tables is much less common since the growth of analytical, columnar DBs. It's now much more common to have fewer, wider tables.
    • We should be making PRQL accessible to everyone, but the focus should be on people who are likely to use it soon, which is likely to be people who are also using other newer tools.
    • (though these can also be used for nested columns, which are more common)
  2. The pipeline approach means it's easier to select a canonical representation of a column and use that for the remainder of the query. e.g adding a derive is easy: from x | join y [x.id=y.id] side:left | derive id: x.id. (But doesn't work if you actually need access to both columns later).
  3. Terseness is often overrated — much of the time, it's easier to read a query when it has the actual table names than e and et and te etc...

That said, as above, if people want this, I'm fine adding it in.

I think @aljazerzen 's syntax is great. Probably we would add a space so it's like:

-from e:employees
+from e: employees
-join s:salaries side:left [s.emp_no = e.emp_no]
+join s: salaries side:left [s.emp_no = e.emp_no]
select [e.gender, s.salary]

...and then it's treated as an assign rather than a named_arg (the syntax differences between these aren't ideal, but I think a reasonable tradeoff, as discussed in https://github.com/max-sixty/prql/issues/53, and still open to iterating on those)

Problem here is how to parse function parameters. If you don't use aliases, from and join have 1 and 2 positional parameters

I think this is OK actually — the e: employees will get parsed as a single assign argument. (the e:employees would be a named arg like e=employees, which would indeed be inconsistent).

What do others think?

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

I'm strongly against adding aliases if we can avoid it. I would love to have the generated SQL take care of the aliasing for me. Making things easier/more succinct is where PRQL shines and why people will use it , adding aliases adds to the noise and readability.

qorrect avatar Mar 22 '22 20:03 qorrect

I also believe that its easier and more readable without aliases, but unfortunately, in some cases, it just gets too long and even ambiguous.

Select names of all employees with names of their managers.

from employees
join dept_emp [emp_no]
join dept_manager [dept_no]
join employees [dept_manager.emp_no = ?.emp_no] # we want manager names from employees table
select [
  employees.first_name,
  employees.last_name,
  ?.first_name, # cannot reference managers...
  ?.last_name,
]

With aliases:

from e:employees
join dept_emp [emp_no]
join dm: dept_manager [dept_no]
join m:employees [dm.emp_no = m.emp_no]
select [
  e.first_name,
  e.last_name,
  m.first_name,
  m.last_name,
]

So I think that even on a modern, normalized databases there is need for aliases. It's just how to do it to prevent users for using it too much :smile:

aljazerzen avatar Mar 22 '22 20:03 aljazerzen

I see , thank you for the example very clean.

Well the syntax is great, a little worried about the required space, maybe that can be addressed in tooling ( auto add space when it sees a join assign ( anything besides the word side )

qorrect avatar Mar 22 '22 21:03 qorrect

Definitely agree with @aljazerzen 's point that we need table references. I'm a bit less confident we need table aliases though.

(forgive me for repeating, but as above — still happy for this feature to go in if people want it)

IIUC, this should work with references, or am I missing something?

from employees
join dept_emp [emp_no]
join dept_manager [dept_no]
-join employees [dept_manager.emp_no = ?.emp_no] # we want manager names from employees table
+join employees [dept_manager.emp_no = dept_manager.emp_no] # we want manager names from employees table
select [
  employees.first_name,
  employees.last_name,
-  ?.first_name, # cannot reference managers...
-  ?.last_name,
+  dept_manager.first_name, # cannot reference managers...
+  dept_manager.last_name,
]

Though the argument that the names get long is valid

max-sixty avatar Mar 22 '22 23:03 max-sixty

No, this would not work on the employees database. dept_manager is just a many-to-many middle table, containing only dept_no and emp_no, which reference department and employees.

The problem lies in the fact that the query contains two employees tables: one joined with dept_emp (to fetch data about employee) and one joined with dept_manager (to fetch data about the manager, who is also an employee).

aljazerzen avatar Mar 23 '22 07:03 aljazerzen

OK, yes, I agree — in cases where tables are repeated, aliases are required. Thanks @aljazerzen .

I'll continue the discussion on the syntax in https://github.com/max-sixty/prql/issues/53

max-sixty avatar Mar 23 '22 17:03 max-sixty

Merged notation is now:

...
derive [
  mng_no: dm.emp_no
]
join managers:employees [emp_no] side:left
derive [mng_name: s"managers.first_name || ' ' || managers.last_name"]
select [mng_name, managers.gender, salary_avg, salary_sd]

aljazerzen avatar Mar 28 '22 18:03 aljazerzen