Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GETDATE, NOW as alias to parser + allow CURRENT_TIMESTAMP with parenthesis #1980

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/alasqlparser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ COLUMNS return 'COLUMN'
"CROSS" return 'CROSS'
'CUBE' return 'CUBE'
"CURRENT_TIMESTAMP" return 'CURRENT_TIMESTAMP'
"GETDATE" return 'CURRENT_TIMESTAMP'
"NOW" return 'CURRENT_TIMESTAMP'
"CURRENT_DATE" return 'CURRENT_DATE'
"CURDATE" return 'CURRENT_DATE'
"CURSOR" return 'CURSOR'
Expand Down Expand Up @@ -1394,6 +1396,8 @@ FuncValue
{ $$ = new yy.FuncValue({ funcid: 'REPLACE', args:$3 }) }
| CURRENT_DATE LPAR RPAR
{ $$ = new yy.FuncValue({ funcid: $1 }) }
| CURRENT_TIMESTAMP LPAR RPAR
{ $$ = new yy.FuncValue({ funcid: $1 }) }
| DATEADD LPAR Literal COMMA Expression COMMA Expression RPAR
{ $$ = new yy.FuncValue({ funcid: 'DATEADD', args:[new yy.StringValue({value:$3}),$5,$7]}) }
| DATEADD LPAR STRING COMMA Expression COMMA Expression RPAR
Expand Down
1,006 changes: 505 additions & 501 deletions src/alasqlparser.js

Large diffs are not rendered by default.

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

/*
Test for issue #1979
*/

var test = '1979'; // insert test file number

describe('Test ' + test + ' - date function aliases in the parser', function () {
it('1a. NOW is a reserved keyword', function () {
assert.throws(function() {
alasql('SELECT NOW() AS now');
});
assert.throws(function() {
alasql('SELECT NOW() AS NOW');
});
assert.throws(function() {
alasql('SELECT NOW() AS NOW()');
});
});

it('1b. GETDATE is a reserved keyword', function () {
assert.throws(function() {
alasql('SELECT GETDATE() AS GETDATE');
});
assert.throws(function() {
alasql('SELECT GETDATE() AS getdate');
});
assert.throws(function() {
alasql('SELECT GETDATE() AS getdate()');
});
});

it('1c. Check parser', function () {
// If the parser works ok, the date functions are parsed with a funcid instead of columnid
assert.equal("CURRENT_TIMESTAMP", alasql.parse('SELECT CURRENT_TIMESTAMP').statements[0].columns[0].funcid);
assert.equal("CURRENT_TIMESTAMP", alasql.parse('SELECT CURRENT_TIMESTAMP()').statements[0].columns[0].funcid);
assert.equal("CURRENT_TIMESTAMP", alasql.parse('SELECT NOW').statements[0].columns[0].funcid);
assert.equal("NOW", alasql.parse('SELECT NOW()').statements[0].columns[0].funcid);
assert.equal("CURRENT_TIMESTAMP", alasql.parse('SELECT GETDATE').statements[0].columns[0].funcid);
assert.equal("GETDATE", alasql.parse('SELECT GETDATE()').statements[0].columns[0].funcid);
assert.equal("CURRENT_DATE", alasql.parse('SELECT CURRENT_DATE').statements[0].columns[0].funcid);
assert.equal("CURRENT_DATE", alasql.parse('SELECT CURRENT_DATE()').statements[0].columns[0].funcid);
assert.equal("CURRENT_DATE", alasql.parse('SELECT CURDATE').statements[0].columns[0].funcid);
assert.equal("CURDATE", alasql.parse('SELECT CURDATE()').statements[0].columns[0].funcid);
});
});
8 changes: 4 additions & 4 deletions test/test845.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ var test = '845'; // insert test file number

describe('Test ' + test + ' - use NOW() function', function () {
it('1a. NOW() as String', function () {
var res = alasql('SELECT NOW() AS now');
var res = alasql('SELECT NOW() AS now_alias');
//2022-02-25 19:21:27.839
assert(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}/.test(res[0].now));
assert(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}/.test(res[0].now_alias));
});

it('1b. NOW() as Date', function () {
alasql.options.dateAsString = false;
var res = alasql('SELECT NOW() AS now');
var res = alasql('SELECT NOW() AS now_alias');
//2022-02-25 19:21:27.839
assert(res[0].now instanceof Date);
assert(res[0].now_alias instanceof Date);
});

it('2. CONVERT with NOW() as an argument', function () {
Expand Down
Loading