Skip to content

Commit adfdebc

Browse files
mathiasrwNmN03jainCopilot
authored
Support numbers in REPLACE() to fix #1455 (#2282)
Co-authored-by: NmN03jain <njain5587@gmail.com> Co-authored-by: Naman Jain <90062260+NmN03jain@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
1 parent 6d491c2 commit adfdebc

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

src/40select.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,6 @@ yy.Select = class Select {
139139
Select statement in expression
140140
*/
141141
toJS(context) {
142-
// console.log('Expression',this);
143-
// if(this.expression.reduced) return 'true';
144-
// return this.expression.toJS(context, tableid, defcols);
145-
// console.log('Select.toJS', 81, this.queriesidx);
146-
// var s = 'this.queriesdata['+(this.queriesidx-1)+'][0]';
147142
var s =
148143
'alasql.utils.flatArray(this.queriesfn[' +
149144
(this.queriesidx - 1) +

src/55functions.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,9 @@ Object.keys(alasql._aggrOriginal).forEach(function (k) {
454454

455455
// String functions
456456
stdfn.REPLACE = function (target, pattern, replacement) {
457-
return (target || '').split(pattern).join(replacement);
457+
return String(target ?? '')
458+
.split(String(pattern ?? ''))
459+
.join(String(replacement ?? ''));
458460
};
459461

460462
// This array is required for fast GUID generation

src/alasqlparser.jison

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ SETS return 'SET'
342342
%left DOT ARROW EXCLAMATION
343343
%left TILDA
344344
%left SHARP
345+
345346
%left BARBAR
346347

347348
%ebnf

test/test1455.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
if (typeof exports === 'object') {
2+
var assert = require('assert');
3+
var alasql = require('..');
4+
}
5+
6+
describe('Test 1455 - REPLACE function', function () {
7+
it('A) REPLACE with string target', function () {
8+
var res = alasql("SELECT REPLACE('123', '2', '_')");
9+
assert.deepEqual(res, [{["REPLACE('123','2','_')"]: '1_3'}]);
10+
});
11+
12+
it('B) REPLACE with numeric target (converted to string)', function () {
13+
var res = alasql("SELECT REPLACE(123, '2', '_')");
14+
assert.deepEqual(res, [{["REPLACE(123,'2','_')"]: '1_3'}]);
15+
});
16+
17+
it('C) Both string and numeric in same query', function () {
18+
var res = alasql("SELECT REPLACE('123', '2', '_'), REPLACE(123, '2', '_')");
19+
assert.deepEqual(res, [
20+
{
21+
["REPLACE('123','2','_')"]: '1_3',
22+
["REPLACE(123,'2','_')"]: '1_3',
23+
},
24+
]);
25+
});
26+
27+
it('D) REPLACE with numeric search and replacement', function () {
28+
var res = alasql('SELECT REPLACE(12321, 2, 9)');
29+
assert.deepEqual(res, [{['REPLACE(12321,2,9)']: '19391'}]);
30+
});
31+
});

0 commit comments

Comments
 (0)