Code Monkey home page Code Monkey logo

Comments (2)

cisaacson avatar cisaacson commented on July 4, 2024

I have confirmed this bug as well. Here is another more complex example with several operators, LIKE is the only one that sometimes puts a 'binary' 'and' between the 'left' and 'right'. It seems to be dependent on where the LIKE is in the order of statements too, if the LIKE expression is last it works fine. If you view the AST in a JSON viewer it is very easy to see the issue.

SELECT a, b
FROM x, y
WHERE d = 'X'
  AND e BETWEEN '1/1/2018' AND '1/31/2018'
  AND f != 'NEW'
  AND g >= 1000000
  AND (h IS NULL
  OR j IS NOT NULL)
  AND k LIKE 'Jones%'
  AND l IN ('CA', 'OR', 'NY')
{"type":"statement","variant":"list","statement":[{"type":"statement","variant":"select","result":[{"type":"identifier","variant":"column","name":"a"},{"type":"identifier","variant":"column","name":"b"}],"from":{"type":"map","variant":"join","source":{"type":"identifier","variant":"table","name":"x"},"map":[{"type":"join","variant":"cross join","source":{"type":"identifier","variant":"table","name":"y"}}]},"where":[{"type":"expression","format":"binary","variant":"operation","operation":"and","left":{"type":"expression","format":"binary","variant":"operation","operation":"and","left":{"type":"expression","format":"binary","variant":"operation","operation":"and","left":{"type":"expression","format":"binary","variant":"operation","operation":"and","left":{"type":"expression","format":"binary","variant":"operation","operation":"and","left":{"type":"expression","format":"binary","variant":"operation","operation":"=","left":{"type":"identifier","variant":"column","name":"d"},"right":{"type":"literal","variant":"text","value":"X"}},"right":{"type":"expression","format":"binary","variant":"operation","operation":"between","right":{"type":"expression","format":"binary","variant":"operation","operation":"and","left":{"type":"literal","variant":"text","value":"1/1/2018"},"right":{"type":"literal","variant":"text","value":"1/31/2018"}},"left":{"type":"identifier","variant":"column","name":"e"}}},"right":{"type":"expression","format":"binary","variant":"operation","operation":"!=","left":{"type":"identifier","variant":"column","name":"f"},"right":{"type":"literal","variant":"text","value":"NEW"}}},"right":{"type":"expression","format":"binary","variant":"operation","operation":">=","left":{"type":"identifier","variant":"column","name":"g"},"right":{"type":"literal","variant":"decimal","value":"1000000"}}},"right":{"type":"expression","format":"binary","variant":"operation","operation":"or","left":{"type":"expression","format":"binary","variant":"operation","operation":"is","left":{"type":"identifier","variant":"column","name":"h"},"right":{"type":"literal","variant":"null","value":"null"}},"right":{"type":"expression","format":"binary","variant":"operation","operation":"is not","left":{"type":"identifier","variant":"column","name":"j"},"right":{"type":"literal","variant":"null","value":"null"}}}},"right":{"type":"expression","format":"binary","variant":"operation","operation":"like","right":{"type":"expression","format":"binary","variant":"operation","operation":"and","left":{"type":"literal","variant":"text","value":"Jones%"},"right":{"type":"expression","format":"binary","variant":"operation","operation":"in","right":{"type":"expression","variant":"list","expression":[{"type":"literal","variant":"text","value":"CA"},{"type":"literal","variant":"text","value":"OR"},{"type":"literal","variant":"text","value":"NY"}]},"left":{"type":"identifier","variant":"column","name":"l"}}},"left":{"type":"identifier","variant":"column","name":"k"}}}]}]}

from sqlite-parser.

eatonphil avatar eatonphil commented on July 4, 2024

Here is another minimal example (just two like expressions joined with an and), first with parens (correct), second (wrong one) without parens:

> const { statement: [{ where: [where7] }] }  = parser("select 1 where (description like 'foo') and (description like 'bar')");
undefined
> where7
{ type: 'expression',
  format: 'binary',
  variant: 'operation',
  operation: 'and',
  left:
   { type: 'expression',
     format: 'binary',
     variant: 'operation',
     operation: 'like',
     right: { type: 'literal', variant: 'text', value: 'foo' },
     left: { type: 'identifier', variant: 'column', name: 'description' } },
  right:
   { type: 'expression',
     format: 'binary',
     variant: 'operation',
     operation: 'like',
     right: { type: 'literal', variant: 'text', value: 'bar' },
     left: { type: 'identifier', variant: 'column', name: 'description' } } }
> const { statement: [{ where: [where9] }] }  = parser("select 1 where description like 'foo' and description like 'bar'");
undefined
> where9
{ type: 'expression',
  format: 'binary',
  variant: 'operation',
  operation: 'like',
  right:
   { type: 'expression',
     format: 'binary',
     variant: 'operation',
     operation: 'and',
     left: { type: 'literal', variant: 'text', value: 'foo' },
     right:
      { type: 'expression',
        format: 'binary',
        variant: 'operation',
        operation: 'like',
        right: [Object],
        left: [Object] } },
  left: { type: 'identifier', variant: 'column', name: 'description' } }

from sqlite-parser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.