Skip to content

Operator Precedence Inconsistencies #683

Closed
@johnedquinn

Description

@johnedquinn

Description

While working on replacing the SqlLexer and SqlParser, I was trying to figure out the order of precedence for operators, and there is a clear inconsistency with the way the current parser handles certain operators.

Also, since we pass all the tests, our tests are also wrong and need to be fixed.

Here's a breakdown:

  • BETWEEN and LIKE have the SAME precedence as <, >, <=, and >=.
  • BETWEEN and LIKE have the SAME precedence as IS.
  • BETWEEN and LIKE have HIGHER precedence than IN.
  • IN has the SAME precedence as =, !=, and <>.

Now, our issue seemingly comes from IS.

  • BETWEEN and LIKE have the SAME precedence as IS.
  • IS has the SAME precedence as IN. This doesnt' make sense because BETWEEN/LIKE have HIGHER precedence than IN.
  • IS has the SAME precedence as <, >, <=, and >=.
  • IS has the SAME precedence as =, !=, and <>.

TLDR

So, TLDR, we have a problem with inconsistency in our grammar. This can be resolved be creating a formal precedence table.

Visual TLDR:

Screen Shot 2022-07-25 at 11 11 21 AM

Textual TLDR:

  • IS has the SAME precedence as BETWEEN/LIKE, but they both have different precedences in comparison to < and IN.

To Reproduce

The following shows that IS and = have the SAME precedence.

PartiQL> a IS BOOL = b
!!
PartiQL> a = b IS BOOL
!!

The following shows that IS and LIKE have the SAME precedence

PartiQL> a IS BOOL LIKE c
!!
PartiQL> a LIKE c IS BOOL
!!

The following shoes that LIKE has HIGHER precedence than =

PartiQL> a = b LIKE c
!!
PartiQL> a LIKE b = c
!!

NOTE: You can see more inconsistencies when you use IN and BETWEEN also.

NOTE: When the AST is printed out with !!, whichever operator is nested is the one with higher precedence (if you test both back and forth). If the LHS is always nested, then we can say that the two operators have equal precedence.

Why

To me, this should be prioritized. PartiQL is a language, and our basic operations don't have an established precedence yet.

To Fix

  • Define a precedence table
  • Fix the parser (either the old one, or just the new one)
  • We need to update documentation to define a formal precedence table to convery to our customers

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions