sqlite-parser icon indicating copy to clipboard operation
sqlite-parser copied to clipboard

Confusion about parsing of "not" operator

Open eatonphil opened this issue 7 years ago • 1 comments

Hi! In the following example I'd expect "not" to be a first-class member of the AST. But it appears to just be concatenated with the operator after it. This would imply I've got to then do string parsing of the operation field if I'm trying to discover "not" use:

> const { statement: [{ where: [where5] }] }  = parser('select 1 where processName =  \'/bin/sh\' and id not like \'foo\'');
undefined
> where5
{ type: 'expression',
  format: 'binary',
  variant: 'operation',
  operation: 'and',
  left:
   { type: 'expression',
     format: 'binary',
     variant: 'operation',
     operation: '=',
     left: { type: 'identifier', variant: 'column', name: 'processname' },
     right: { type: 'literal', variant: 'text', value: '/bin/sh' } },
  right:
   { type: 'expression',
     format: 'binary',
     variant: 'operation',
     operation: 'not like',
     right: { type: 'literal', variant: 'text', value: 'foo' },
     left: { type: 'identifier', variant: 'column', name: 'id' } } }

Is this intentional? I'd expect the AST to look more like:

  ...
  right:
   { type: 'expression',
     format: 'unary',
     variant: 'operation',
     operation: 'not',
     expression:
     {  type: 'expression',
        format: 'binary',
        variant: 'operation',
        operation: 'like',
        right: { type: 'literal', variant: 'text', value: 'foo' },
        left: { type: 'identifier', variant: 'column', name: 'id' } } } }

eatonphil avatar Jun 14 '18 14:06 eatonphil

I think that is not in this case. In a not like b that means: not (a like b), but not a not (like b) because not is an unary operator. Then they can parse as a "notlike" b.

There still a problem with not

Here I have an example:

select (not 1) + 1, not (1 + 1), not 1 + 1

If you write it in sqllite http://sqlfiddle.com/#!7/9eecb7/7512 you can see that not 1 + 1 means not (1 + 1) but not (not 1) + 1

emilioplatzer avatar Jan 29 '21 13:01 emilioplatzer