AlaSQL / alasql

AlaSQL.js - JavaScript SQL database for browser and Node.js. Handles both traditional relational tables and nested JSON data (NoSQL). Export, store, and import data from localStorage, IndexedDB, or Excel.
http://alasql.org
MIT License
7.01k stars 649 forks source link

Incorrect empty array returned for syntactically incorrect SELECT query #1814

Open davidevico opened 11 months ago

davidevico commented 11 months ago

We have some code to dinamically generate a SQL-like string that we pass to AlaSQL to run. Sometimes the WHERE clause gets compiled with what would be incorrect SQL code. In that case, AlaSQL returns us an empty array, whereas we would expect a syntax error.

For example, you can reproduce this behavior in one of the JSFiddle found in the Readme: demo. The sample defines a query on line 7 SELECT * FROM cities WHERE population < 3500000 ORDER BY population DESC If you run the sample, this query correctly returns a couple of results in the right-hand panel.

Case 1: If you update the query to something like SELECT * FROM cities WHERE hi In the right-hand panel the result of this query is an empty array.

Case 2: If you remove the WHERE clause altogether SELECT * FROM cities WHERE You get a syntax error in the console, as expected: Uncaught SyntaxError: Parse error on line 1: ... * FROM cities WHERE -----------------------^ Expecting 'LITERAL', 'BRALITERAL', 'LPAR', 'NUMBER', 'STRING', 'SHARP', 'DOLLAR', 'AT', 'VALUE', 'COLON', 'NOT', 'IF', 'PLUS', 'STAR', 'QUESTION', 'FIRST', 'LAST', 'CURRENT_TIMESTAMP', 'JAVASCRIPT', 'NEW', 'CAST', 'CONVERT', 'SUM', 'TOTAL', 'COUNT', 'MIN', 'MAX', 'AVG', 'AGGR', 'ARRAY', 'REPLACE', 'DATEADD', 'DATEDIFF', 'TIMESTAMPDIFF', 'INTERVAL', 'TRUE', 'FALSE', 'NSTRING', 'NULL', 'EXISTS', 'ARRAYLBRA', 'BRAQUESTION', 'CASE', 'TILDA', 'MINUS', 'ATLBRA', 'LCUR', got 'EOF' at _.parseError (alasql@2:2:243814) at fs.parse (alasql@2:2:239172) at L.parse (alasql@2:2:269078) at L.dexec (alasql@2:2:270683) at L.exec (alasql@2:2:270431) at L (alasql@2:2:1961) at ?editor_console=true:118:11

Case 3: If you update the query to something similar to this: SELECT * FROM cities WHERE 'hi' You get four records as a result, so it seems as if the WHERE condition is sintactically correct and no filter is applied.

Case 4: Updating the query to a mixed situation like the following SELECT * FROM cities WHERE hi population < 3500000 ORDER BY population DESC We get a similar syntax error to case 2, this is also what we would expect.

We would expect case 1 to throw a similar syntax error to case 2. Is this the expected behavior? Am I missing something? Thanks!

davidevico commented 9 months ago

Hi @mathiasrw, did you have a chance to review this issue?

mathiasrw commented 9 months ago

Hi @davidevico

I would love to review and merge a PR for this, bur dont have time to dive deeper at the moment.

I dont think its hard to solve. The hard part is to dive into the code and find the right spot where we determine that the AST is including a reference to an undefined entity