Skip to content

Commit

Permalink
Let EXISTS work subqueries to fix #1904 (#1952)
Browse files Browse the repository at this point in the history
Co-authored-by: asundukov <asundukov>
  • Loading branch information
alsundukov authored Jul 31, 2024
1 parent 2d383d9 commit 85c24d4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/alasqlparser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,8 @@ Select
yy.extend($$,$5); yy.extend($$,$6);yy.extend($$,$7);
yy.extend($$,$8); yy.extend($$,$9); yy.extend($$,$10);
$$ = $1;
/* if(yy.exists) $$.exists = yy.exists;
delete yy.exists;
if(yy.queries) $$.queries = yy.queries;
if(yy.exists) $$.exists = yy.exists.slice();
/* if(yy.queries) $$.queries = yy.queries;
delete yy.queries;
*/ }
| SEARCH SearchSelector* IntoClause SearchFrom?
Expand Down
5 changes: 2 additions & 3 deletions src/alasqlparser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions test/test1904.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
if (typeof exports === 'object') {
var assert = require('assert');
var alasql = require('..');
}

describe('Test 1937: EXISTS in SQL Queries and SET Statements', function () {
before(function () {
alasql('create database test1937');
alasql('use test1937');
alasql('DROP TABLE IF EXISTS one');
alasql('CREATE TABLE one (a INT)');
alasql('INSERT INTO one VALUES (1),(2),(3),(4),(5)');
});

after(function () {
alasql('drop database test1937');
});

it('Nested EXISTS in subquery', function (done) {
const res = alasql(
'SELECT EXISTS(SELECT a FROM one WHERE 0) AS main_exists, * FROM (SELECT EXISTS(SELECT a FROM one) AS sub_exists, a FROM one)'
);
assert.deepEqual(
[
{main_exists: false, a: 1, sub_exists: true},
{main_exists: false, a: 2, sub_exists: true},
{main_exists: false, a: 3, sub_exists: true},
{main_exists: false, a: 4, sub_exists: true},
{main_exists: false, a: 5, sub_exists: true},
],
res
);
done();
});

it('EXISTS in SET statement', function (done) {
const res = alasql(
`SET @existsLessThan3 = (SELECT EXISTS(SELECT a FROM one WHERE a < 3));
SET @existsGreaterThan10 = (SELECT EXISTS(SELECT a FROM one WHERE a > 10));
SELECT @existsLessThan3, @existsGreaterThan10;`
);
assert.deepEqual([{'@existsLessThan3': true, '@existsGreaterThan10': false}], res[2]);
done();
});
});

0 comments on commit 85c24d4

Please sign in to comment.