From 85c24d42191e892f477925cb90de0fa0b37800b0 Mon Sep 17 00:00:00 2001 From: alsundukov <74598771+alsundukov@users.noreply.github.com> Date: Thu, 1 Aug 2024 02:28:18 +0300 Subject: [PATCH] Let EXISTS work subqueries to fix #1904 (#1952) Co-authored-by: asundukov --- src/alasqlparser.jison | 5 ++--- src/alasqlparser.js | 5 ++--- test/test1904.js | 45 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 test/test1904.js diff --git a/src/alasqlparser.jison b/src/alasqlparser.jison index 8f00ddb5bd..a23638580d 100755 --- a/src/alasqlparser.jison +++ b/src/alasqlparser.jison @@ -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? diff --git a/src/alasqlparser.js b/src/alasqlparser.js index 31279849bd..1299a6426a 100755 --- a/src/alasqlparser.js +++ b/src/alasqlparser.js @@ -145,9 +145,8 @@ case 72: yy.extend(this.$,$$[$0-5]); yy.extend(this.$,$$[$0-4]);yy.extend(this.$,$$[$0-3]); yy.extend(this.$,$$[$0-2]); yy.extend(this.$,$$[$0-1]); yy.extend(this.$,$$[$0]); this.$ = $$[$0-9]; -/* if(yy.exists) this.$.exists = yy.exists; - delete yy.exists; - if(yy.queries) this.$.queries = yy.queries; + if(yy.exists) this.$.exists = yy.exists.slice(); +/* if(yy.queries) this.$.queries = yy.queries; delete yy.queries; */ break; diff --git a/test/test1904.js b/test/test1904.js new file mode 100644 index 0000000000..ab098635b8 --- /dev/null +++ b/test/test1904.js @@ -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(); + }); +});