From fe63bd59925f5f081c9babc8db4c73e30ddf230b Mon Sep 17 00:00:00 2001 From: NmN03jain Date: Tue, 15 Aug 2023 13:29:58 +0530 Subject: [PATCH 1/8] Replace fucntio is working now --- src/55functions.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/55functions.js b/src/55functions.js index 48dcfada8a..29daba90b4 100644 --- a/src/55functions.js +++ b/src/55functions.js @@ -454,7 +454,10 @@ Object.keys(alasql._aggrOriginal).forEach(function (k) { // String functions stdfn.REPLACE = function (target, pattern, replacement) { - return (target || '').split(pattern).join(replacement); + if (typeof target !== 'string') { + throw new TypeError('Target must be a string'); + } + return target.split(pattern).join(replacement); }; // This array is required for fast GUID generation From aa64c00275d5605ec011ba78a75e8a63ae010005 Mon Sep 17 00:00:00 2001 From: NmN03jain Date: Tue, 15 Aug 2023 14:22:19 +0530 Subject: [PATCH 2/8] adding test1455.js --- test/test1455.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 test/test1455.js diff --git a/test/test1455.js b/test/test1455.js new file mode 100644 index 0000000000..2a2cb1f6b6 --- /dev/null +++ b/test/test1455.js @@ -0,0 +1,51 @@ +if (typeof exports === 'object') { + var assert = require('assert'); + var alasql = require('..'); +} + +describe('Test 000 - multiple statements', function () { + const test = '000'; // insert test file number + + before(function () { + alasql('create database test' + test); + alasql('use test' + test); + }); + + after(function () { + alasql('drop database test' + test); + }); + + it('A) From single lines', function () { + var res = []; + res.push(alasql('create table one (a int)')); + res.push(alasql('insert into one values (1),(2),(3),(4),(5)')); + res.push(alasql('select * from one')); + assert.deepEqual(res, [1, 5, [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]]); + }); + + it('B) Multiple statements in one string', function () { + // + var sql = 'create table two (a int);'; + sql += 'insert into two values (1),(2),(3),(4),(5);'; + sql += 'select * from two;'; + var res = alasql(sql); + assert.deepEqual(res, [1, 5, [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]]); + }); + + it('C) Multiple statements in one string with callback', function (done) { + // Please note that first parameter (here `done`) must be called if defined - and is needed when testing async code + var sql = 'create table three (a int);'; + sql += 'insert into three values (1),(2),(3),(4),(5);'; + sql += 'select * from three;'; + alasql(sql, function (res) { + assert.deepEqual(res, [1, 5, [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]]); + done(); + }); + }); +}); +stdfn.REPLACE = function (target, pattern, replacement) { + if (typeof target !== 'string') { + throw new TypeError('Target must be a string'); + } + return target.split(pattern).join(replacement); +}; \ No newline at end of file From b2223534870c42b203691d19f203939ddf061e0d Mon Sep 17 00:00:00 2001 From: Naman Jain <90062260+NmN03jain@users.noreply.github.com> Date: Thu, 17 Aug 2023 01:50:13 +0530 Subject: [PATCH 3/8] Update 55functions.js --- src/55functions.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/55functions.js b/src/55functions.js index 29daba90b4..09d3a1efd9 100644 --- a/src/55functions.js +++ b/src/55functions.js @@ -454,10 +454,7 @@ Object.keys(alasql._aggrOriginal).forEach(function (k) { // String functions stdfn.REPLACE = function (target, pattern, replacement) { - if (typeof target !== 'string') { - throw new TypeError('Target must be a string'); - } - return target.split(pattern).join(replacement); + return (target.toString() || '').split(pattern).join(replacement); }; // This array is required for fast GUID generation From 3122f0401e4c15114556d8ec750a77a542511921 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 30 Nov 2025 12:16:44 +0000 Subject: [PATCH 4/8] Initial plan From a9816308b809826b458b3c3f885d9cf6ce9dd23c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 30 Nov 2025 12:14:43 +0000 Subject: [PATCH 5/8] Fix REPLACE function to convert all parameters to string and update tests Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com> --- src/55functions.js | 4 +++- test/test1455.js | 57 ++++++++++++++++++---------------------------- 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/src/55functions.js b/src/55functions.js index 09d3a1efd9..d3f893bb7c 100644 --- a/src/55functions.js +++ b/src/55functions.js @@ -454,7 +454,9 @@ Object.keys(alasql._aggrOriginal).forEach(function (k) { // String functions stdfn.REPLACE = function (target, pattern, replacement) { - return (target.toString() || '').split(pattern).join(replacement); + return (target == null ? '' : target.toString()) + .split(pattern == null ? '' : pattern.toString()) + .join(replacement == null ? '' : replacement.toString()); }; // This array is required for fast GUID generation diff --git a/test/test1455.js b/test/test1455.js index 2a2cb1f6b6..324957c204 100644 --- a/test/test1455.js +++ b/test/test1455.js @@ -3,49 +3,36 @@ if (typeof exports === 'object') { var alasql = require('..'); } -describe('Test 000 - multiple statements', function () { - const test = '000'; // insert test file number +describe('Test 1455 - REPLACE function', function () { + it('A) REPLACE with string target', function () { + var res = alasql("SELECT REPLACE('123', '2', '_') AS result"); + assert.deepEqual(res, [{result: '1_3'}]); + }); - before(function () { - alasql('create database test' + test); - alasql('use test' + test); + it('B) REPLACE with number target', function () { + var res = alasql("SELECT REPLACE(123, '2', '_') AS result"); + assert.deepEqual(res, [{result: '1_3'}]); }); - after(function () { - alasql('drop database test' + test); + it('C) REPLACE with string and number returning same result', function () { + var res = alasql( + "SELECT REPLACE('123', '2', '_') AS str_result, REPLACE(123, '2', '_') AS num_result" + ); + assert.deepEqual(res, [{str_result: '1_3', num_result: '1_3'}]); }); - it('A) From single lines', function () { - var res = []; - res.push(alasql('create table one (a int)')); - res.push(alasql('insert into one values (1),(2),(3),(4),(5)')); - res.push(alasql('select * from one')); - assert.deepEqual(res, [1, 5, [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]]); + it('D) REPLACE with number in pattern', function () { + var res = alasql("SELECT REPLACE('123', 2, '_') AS result"); + assert.deepEqual(res, [{result: '1_3'}]); }); - it('B) Multiple statements in one string', function () { - // - var sql = 'create table two (a int);'; - sql += 'insert into two values (1),(2),(3),(4),(5);'; - sql += 'select * from two;'; - var res = alasql(sql); - assert.deepEqual(res, [1, 5, [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]]); + it('E) REPLACE with number in replacement', function () { + var res = alasql("SELECT REPLACE('1_3', '_', 2) AS result"); + assert.deepEqual(res, [{result: '123'}]); }); - it('C) Multiple statements in one string with callback', function (done) { - // Please note that first parameter (here `done`) must be called if defined - and is needed when testing async code - var sql = 'create table three (a int);'; - sql += 'insert into three values (1),(2),(3),(4),(5);'; - sql += 'select * from three;'; - alasql(sql, function (res) { - assert.deepEqual(res, [1, 5, [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]]); - done(); - }); + it('F) REPLACE with all numbers', function () { + var res = alasql('SELECT REPLACE(123, 2, 9) AS result'); + assert.deepEqual(res, [{result: '193'}]); }); }); -stdfn.REPLACE = function (target, pattern, replacement) { - if (typeof target !== 'string') { - throw new TypeError('Target must be a string'); - } - return target.split(pattern).join(replacement); -}; \ No newline at end of file From 974871893cbc0ee2f5afc704cfd04bf565e26903 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 30 Nov 2025 12:25:50 +0000 Subject: [PATCH 6/8] Fix REPLACE function to handle numeric inputs and improve test coverage Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com> --- src/55functions.js | 4 +++- test/test1455.js | 56 +++++++++++++++------------------------------- 2 files changed, 21 insertions(+), 39 deletions(-) diff --git a/src/55functions.js b/src/55functions.js index 09d3a1efd9..7164b69d78 100644 --- a/src/55functions.js +++ b/src/55functions.js @@ -454,7 +454,9 @@ Object.keys(alasql._aggrOriginal).forEach(function (k) { // String functions stdfn.REPLACE = function (target, pattern, replacement) { - return (target.toString() || '').split(pattern).join(replacement); + return String(target ?? '') + .split(String(pattern ?? '')) + .join(String(replacement ?? '')); }; // This array is required for fast GUID generation diff --git a/test/test1455.js b/test/test1455.js index 2a2cb1f6b6..9faea76f32 100644 --- a/test/test1455.js +++ b/test/test1455.js @@ -3,49 +3,29 @@ if (typeof exports === 'object') { var alasql = require('..'); } -describe('Test 000 - multiple statements', function () { - const test = '000'; // insert test file number - - before(function () { - alasql('create database test' + test); - alasql('use test' + test); - }); - - after(function () { - alasql('drop database test' + test); +describe('Test 1455 - REPLACE function', function () { + it('A) REPLACE with string target', function () { + var res = alasql("SELECT REPLACE('123', '2', '_')"); + assert.deepEqual(res, [{["REPLACE('123','2','_')"]: '1_3'}]); }); - it('A) From single lines', function () { - var res = []; - res.push(alasql('create table one (a int)')); - res.push(alasql('insert into one values (1),(2),(3),(4),(5)')); - res.push(alasql('select * from one')); - assert.deepEqual(res, [1, 5, [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]]); + it('B) REPLACE with numeric target (converted to string)', function () { + var res = alasql("SELECT REPLACE(123, '2', '_')"); + assert.deepEqual(res, [{["REPLACE(123,'2','_')"]: '1_3'}]); }); - it('B) Multiple statements in one string', function () { - // - var sql = 'create table two (a int);'; - sql += 'insert into two values (1),(2),(3),(4),(5);'; - sql += 'select * from two;'; - var res = alasql(sql); - assert.deepEqual(res, [1, 5, [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]]); + it('C) Both string and numeric in same query', function () { + var res = alasql("SELECT REPLACE('123', '2', '_'), REPLACE(123, '2', '_')"); + assert.deepEqual(res, [ + { + ["REPLACE('123','2','_')"]: '1_3', + ["REPLACE(123,'2','_')"]: '1_3', + }, + ]); }); - it('C) Multiple statements in one string with callback', function (done) { - // Please note that first parameter (here `done`) must be called if defined - and is needed when testing async code - var sql = 'create table three (a int);'; - sql += 'insert into three values (1),(2),(3),(4),(5);'; - sql += 'select * from three;'; - alasql(sql, function (res) { - assert.deepEqual(res, [1, 5, [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]]); - done(); - }); + it('D) REPLACE with numeric search and replacement', function () { + var res = alasql('SELECT REPLACE(12321, 2, 9)'); + assert.deepEqual(res, [{['REPLACE(12321,2,9)']: '19391'}]); }); }); -stdfn.REPLACE = function (target, pattern, replacement) { - if (typeof target !== 'string') { - throw new TypeError('Target must be a string'); - } - return target.split(pattern).join(replacement); -}; \ No newline at end of file From 3360a79ce15458e7bb43b7be9e76dee335fbeb96 Mon Sep 17 00:00:00 2001 From: "M. Wulff" Date: Sun, 30 Nov 2025 23:44:38 +1100 Subject: [PATCH 7/8] branch things --- src/40select.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/40select.js b/src/40select.js index bf72388cbb..bf60120702 100755 --- a/src/40select.js +++ b/src/40select.js @@ -139,11 +139,6 @@ yy.Select = class Select { Select statement in expression */ toJS(context) { - // console.log('Expression',this); - // if(this.expression.reduced) return 'true'; - // return this.expression.toJS(context, tableid, defcols); - // console.log('Select.toJS', 81, this.queriesidx); - // var s = 'this.queriesdata['+(this.queriesidx-1)+'][0]'; var s = 'alasql.utils.flatArray(this.queriesfn[' + (this.queriesidx - 1) + From d5d4f8b0e23607528deb30f53e5ab9d9ef043b29 Mon Sep 17 00:00:00 2001 From: "M. Wulff" Date: Sun, 30 Nov 2025 23:46:04 +1100 Subject: [PATCH 8/8] Branch things --- src/alasqlparser.jison | 1 + 1 file changed, 1 insertion(+) diff --git a/src/alasqlparser.jison b/src/alasqlparser.jison index e216c84487..3df6a49c63 100755 --- a/src/alasqlparser.jison +++ b/src/alasqlparser.jison @@ -342,6 +342,7 @@ SETS return 'SET' %left DOT ARROW EXCLAMATION %left TILDA %left SHARP + %left BARBAR %ebnf