Skip to content

Commit

Permalink
Merge pull request #202 from rlaiola/whitespacefix
Browse files Browse the repository at this point in the history
Allow whitespaces in functions of value expressions
  • Loading branch information
evazangerle authored Jun 27, 2024
2 parents 69808a3 + 0746c61 commit 4f173bb
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/db/parser/grammar_ra.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -538,14 +538,14 @@ listOfOrderByArgs


aggFunction
= func:$('sum'i / 'count'i / 'avg'i / 'min'i / 'max'i) '(' _ col:columnName _ ')'
= func:$('sum'i / 'count'i / 'avg'i / 'min'i / 'max'i) _ '(' _ col:columnName _ ')'
{
return {
aggFunction: func.toUpperCase(),
col: col
};
}
/ 'count(*)'i
/ 'count'i _ '(' _ '*' _ ')'
{
return {
aggFunction: 'COUNT_ALL',
Expand Down Expand Up @@ -1506,7 +1506,7 @@ valueExprFunctionsNary
('coalesce'i { return ['coalesce', 'null']; })
/ ('concat'i { return ['concat', 'string']; })
)
'(' _ arg0:valueExpr _ argn:(',' _ valueExpr _ )* ')'
_ '(' _ arg0:valueExpr _ argn:(',' _ valueExpr _ )* ')'
{
var args = [arg0];
for(var i = 0; i < argn.length; i++){
Expand All @@ -1533,7 +1533,7 @@ valueExprFunctionsBinary
/ ('mul'i { return ['mul', 'number']; })
/ ('div'i { return ['div', 'number']; })
)
'(' _ arg0:valueExpr _ ',' _ arg1:valueExpr _ ')'
_ '(' _ arg0:valueExpr _ ',' _ arg1:valueExpr _ ')'
{
return {
type: 'valueExpr',
Expand Down Expand Up @@ -1567,7 +1567,7 @@ valueExprFunctionsUnary
/ ('second'i { return ['second', 'number']; })
/ ('dayofmonth'i { return ['dayofmonth', 'number']; })
)
'(' _ arg0:valueExpr _ ')'
_ '(' _ arg0:valueExpr _ ')'
{
return {
type: 'valueExpr',
Expand All @@ -1593,7 +1593,7 @@ valueExprFunctionsNullary
/ ('clock_timestamp'i { return ['clock_timestamp', 'date']; })
/ ('sysdate'i { return ['clock_timestamp', 'date']; })
)
'(' _ ')'
_ '(' _ ')'
{
return {
type: 'valueExpr',
Expand Down
12 changes: 6 additions & 6 deletions src/db/parser/grammar_sql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,15 @@ columnIndex
}

aggFunction
= func:$('sum'i / 'count'i / 'avg'i / 'min'i / 'max'i) '(' _ ('ALL'i __)? col:columnName _ ')'
= func:$('sum'i / 'count'i / 'avg'i / 'min'i / 'max'i) _ '(' _ ('ALL'i __)? col:columnName _ ')'
{
return {
type: 'aggFunction',
aggFunction: func.toUpperCase(),
col: col
};
}
/ 'count(*)'i
/ 'count'i _ '(' _ '*' _ ')'
{
return {
type: 'aggFunction',
Expand Down Expand Up @@ -1339,7 +1339,7 @@ valueExprFunctionsNary
('coalesce'i { return ['coalesce', 'null']; })
/ ('concat'i { return ['concat', 'string']; })
)
'(' _ arg0:valueExpr _ argn:(',' _ valueExpr _ )* ')'
_ '(' _ arg0:valueExpr _ argn:(',' _ valueExpr _ )* ')'
{
var args = [arg0];
for(var i = 0; i < argn.length; i++){
Expand All @@ -1366,7 +1366,7 @@ valueExprFunctionsBinary
/ ('mul'i { return ['mul', 'number']; })
/ ('div'i { return ['div', 'number']; })
)
'(' _ arg0:valueExpr _ ',' _ arg1:valueExpr _ ')'
_ '(' _ arg0:valueExpr _ ',' _ arg1:valueExpr _ ')'
{
return {
type: 'valueExpr',
Expand Down Expand Up @@ -1400,7 +1400,7 @@ valueExprFunctionsUnary
/ ('second'i { return ['second', 'number']; })
/ ('dayofmonth'i { return ['dayofmonth', 'number']; })
)
'(' _ arg0:valueExpr _ ')'
_ '(' _ arg0:valueExpr _ ')'
{
return {
type: 'valueExpr',
Expand All @@ -1426,7 +1426,7 @@ valueExprFunctionsNullary
/ ('clock_timestamp'i { return ['clock_timestamp', 'date']; })
/ ('sysdate'i { return ['clock_timestamp', 'date']; })
)
'(' _ ')'
_ '(' _ ')'
{
return {
type: 'valueExpr',
Expand Down
67 changes: 67 additions & 0 deletions src/db/tests/translate_tests_ra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1234,3 +1234,70 @@ QUnit.test('groupby textgen', function (assert) {
"\t'a' , 1 \n" +
'} ) ');
});

QUnit.test('whitespace(s) between aggregate function and opening parenthesis', function (assert) {
const result = exec_ra("gamma ; sum (a)->total_a (R)", getTestRelations()).getResult();
result.eliminateDuplicateRows();

const reference = exec_ra('{total_a\n' +
'19\n' +
'}', {}).getResult();

assert.deepEqual(result, reference);
});

QUnit.test('whitespace(s) between count(*) function and opening parenthesis', function (assert) {
const result = exec_ra("gamma ; count (*)->n (R)", getTestRelations()).getResult();
result.eliminateDuplicateRows();

const reference = exec_ra('{n\n' +
'5\n' +
'}', {}).getResult();

assert.deepEqual(result, reference);
});

QUnit.test('whitespace(s) between n-ary text function and opening parenthesis', function (assert) {
const result = exec_ra("pi concat (a, b, c)->k (R)", getTestRelations()).getResult();
result.eliminateDuplicateRows();

const reference = exec_ra('{k\n' +
'1ad\n' +
'3cc\n' +
'4df\n' +
'5db\n' +
'6ef\n' +
'}', {}).getResult();

assert.deepEqual(result, reference);
});

QUnit.test('whitespace(s) between binary function and opening parenthesis', function (assert) {
const result = exec_ra("pi add (a, 5)->a_plus_5 (R)", getTestRelations()).getResult();
result.eliminateDuplicateRows();

const reference = exec_ra('{a_plus_5\n' +
'6\n' +
'8\n' +
'9\n' +
'10\n' +
'11\n' +
'}', {}).getResult();

assert.deepEqual(result, reference);
});

QUnit.test('whitespace(s) between unary function and opening parenthesis', function (assert) {
const result = exec_ra("pi a + length ( c )->x, upper ( b )->k (R)", getTestRelations()).getResult();
result.eliminateDuplicateRows();

const reference = exec_ra('{\tx:number, k:string\n' +
"\t2, 'A'\n" +
"\t4, 'C'\n" +
"\t5, 'D'\n" +
"\t6, 'D'\n" +
"\t7, 'E'\n" +
'}', {}).getResult();

assert.deepEqual(result, reference);
});
45 changes: 45 additions & 0 deletions src/db/tests/translate_tests_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -907,3 +907,48 @@ QUnit.skip('test aggregate function in value-expression', function (assert) {

assert.deepEqual(exec_sql(query).getResult(), exec_ra(queryRef).getResult());
});

QUnit.test('whitespace(s) between aggregate function and opening parenthesis', function (assert) {
const query = `
SELECT sum(a) AS total_a
FROM R`;
const queryRef = `gamma ; sum (a)->total_a (R)`;

assert.deepEqual(exec_sql(query).getResult(), exec_ra(queryRef).getResult());
});

QUnit.test('whitespace(s) between count(*) function and opening parenthesis', function (assert) {
const query = `
SELECT count (*) AS n
FROM R`;
const queryRef = `gamma ; count(*)->n (R)`;

assert.deepEqual(exec_sql(query).getResult(), exec_ra(queryRef).getResult());
});

QUnit.test('whitespace(s) between n-ary text function and opening parenthesis', function (assert) {
const query = `
SELECT concat (a, b, c) AS k
FROM R`;
const queryRef = `pi concat(a, b, c)->k (R)`;

assert.deepEqual(exec_sql(query).getResult(), exec_ra(queryRef).getResult());
});

QUnit.test('whitespace(s) between binary function and opening parenthesis', function (assert) {
const query = `
SELECT add (a, 5) AS a_plus_5
FROM R`;
const queryRef = `pi add(a, 5)->a_plus_5 (R)`;

assert.deepEqual(exec_sql(query).getResult(), exec_ra(queryRef).getResult());
});

QUnit.test('whitespace(s) between unary function and opening parenthesis', function (assert) {
const query = `
SELECT a + length ( c ) AS x, upper ( b ) AS k
FROM R`;
const queryRef = `pi a + length(c)->x, upper(b)->k (R)`;

assert.deepEqual(exec_sql(query).getResult(), exec_ra(queryRef).getResult());
});

0 comments on commit 4f173bb

Please sign in to comment.