Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: verify operator precedence of ~ #14814

Closed
maddyblue opened this issue Apr 11, 2017 · 16 comments
Closed

sql: verify operator precedence of ~ #14814

maddyblue opened this issue Apr 11, 2017 · 16 comments
Assignees
Milestone

Comments

@maddyblue
Copy link
Contributor

postegres:

postgres=# select ~0+1;
 ?column? 
----------
       -2
(1 row)

postgres=# select (~0)+1;
 ?column? 
----------
        0
(1 row)

cockroach:

root@:26257/> select ~0+1;
+-----------+
| (~ 0) + 1 |
+-----------+
|         0 |
+-----------+
(1 row)

Seems like ~ may need to go lower. See #2305.

@maddyblue maddyblue added this to the 1.0 milestone Apr 11, 2017
@dianasaur323
Copy link
Contributor

assigning to @cuongdo for him to reassign.

@maddyblue
Copy link
Contributor Author

I'll take this.

@dianasaur323
Copy link
Contributor

dianasaur323 commented Apr 12, 2017 via email

@knz
Copy link
Contributor

knz commented Apr 13, 2017

So this issue makes me sad. In increasing order of importance:

  1. no other language has unary operators at a lower precedence level than binary
  2. our internal policy was "make CockroachDB look and feel like pg unless we had a good reason not to". here we have a good reason not to: it is ugly.
  3. is there a client app that is broken because of this? If not, why is there a problem?

@knz
Copy link
Contributor

knz commented Apr 13, 2017

Also, why is this a v1.0 issue?

@dianasaur323
Copy link
Contributor

I don't think this is necessarily a must-have for the v1.0, but my sense is that for some of these clean-ups on SQL language, if they don't take too much time, we can try squeezing them in for usability reasons. I don't feel strongly about keeping this in the 1.0 though. Re matching PostgreSQL, that seems to be the path we have taken?

@cuongdo
Copy link
Contributor

cuongdo commented Apr 13, 2017

This would be a backward-incompatible change, so we'd need to find a way mitigate that if we address this. Even more so after 1.0.

Also, if we decide to diverge from PG (which I believe is fine is some circumstances), we need a way of documenting those differences for those who use PG drivers and ORMs with us and expect PG semantics.

@tamird
Copy link
Contributor

tamird commented Apr 13, 2017 via email

@jordanlewis
Copy link
Member

This is a simple change with a big payoff. We generally claim compatibility with Postgres - why diverge here? Users who expect Postgres compatibility will probably expect that they can paste a complicated query that uses this feature and see no difference. However, bit not is unintuitive enough that they might notice no difference but still experience incorrect results and have a hard time tracking down the issue.

Then again, how frequently do people really use ~ in SQL queries? Perhaps the rarity of use is sufficient to justify divergence in the name of normal unary operator behavior.

@tamird
Copy link
Contributor

tamird commented Apr 13, 2017 via email

@dianasaur323
Copy link
Contributor

Given the comments, it sounds like we should do it before the 1.0 and to maintain PostgreSQL compatibility.

@maddyblue
Copy link
Contributor Author

I agree with everyone. It is surprising if a unary operator has a lower precedence than a binary operator, suggesting we shouldn't change this. I also agree that maintaining Postgres compat makes sense in general, and this concern weighs higher in my mind.

We are advertising Postgres ORM support. This means we need to closely support a Postgres dialect, which must include things like operator precedence and associativity. Is there an ORM that depends on the above behavior being correct? I'm not sure. But if there is and we differ, then that application will get back incorrect results without knowing why, and without an error message.

Overall: I think we should match Postgres' operators because we advertise ORM support.

@knz
Copy link
Contributor

knz commented Apr 13, 2017

What about not supporting this unary operator for now, and simply support a built-in function to negate numbers instead. Let's see if anyone requests the operator in the future.
I'd be more comfortable with no operator at all (and a built-in function).

@maddyblue
Copy link
Contributor Author

That's fine with me.

@jordanlewis
Copy link
Member

This option is quite tempting. This way applications will be certain to encounter errors if they try to use bitwise not. ISTM that legitimate use of ~ is rare enough that we will not be hurting 99% of application developers by doing this.

@dianasaur323
Copy link
Contributor

Closing as addressed by #14908. Please re-open if you disagree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants