prql: Variable replacement doesn't respect precedence

In this example, the resulting code for diff should be c - a - b or c - (a + b)

from foo
select [
   sum = (a+b),
   diff = c-sum  # actually outputs "c - a + b AS diff"
]

(Tested with current VScode plugin and Playground)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 21 (21 by maintainers)

Most upvoted comments

Opened a new issue with the most recent comment, since this is now smaller is scope now that the associativity issue is fixed

Update: Even simpler test case

# Minimal example for tax increase
# pq28.prql

# fun1 & fun2 divide their "n" param by "d"
func fun1 n d -> s' {n} / {d} '
func fun2 n d -> s' {n} / ({d}) '

from albums
select [
  a = 1/100.0,
  b = 1/a,          # correct
  c = (fun1 1 a ),  # incorrect
  d = (fun2 1 a ),  # correct - fun2 wraps denominator in (...)
]
take 1

Playground yields (21Nov2022)

SELECT
  1 / 100.0 AS a,
  1 / (1 / 100.0) AS b,
  1 / 1 / 100.0 AS c,
  1 / (1 / 100.0) AS d
FROM
  albums
LIMIT
  1

SQL output shows:

a	b	c	d
0.01	100.0	0.01	100.0