From ed15e43b7594710718f5b445f272c80d6955f303 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Laiola=20Guimar=C3=A3es?= Date: Tue, 22 Oct 2024 11:14:11 -0300 Subject: [PATCH 1/4] Add project all to RA --- src/db/parser/grammar_ra.d.ts | 8 ++++++- src/db/parser/grammar_ra.pegjs | 16 ++++++++++++++ src/db/tests/translate_tests_ra.ts | 34 ++++++++++++++++++++++++++++++ src/db/translate/relalgFromAst.ts | 12 ++++++++++- 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/src/db/parser/grammar_ra.d.ts b/src/db/parser/grammar_ra.d.ts index e0bb5b61..b83a6f7d 100644 --- a/src/db/parser/grammar_ra.d.ts +++ b/src/db/parser/grammar_ra.d.ts @@ -79,6 +79,12 @@ declare module relalgAst { relAlias: string | null, } + interface columnAsterisk { + type: 'column', + name: '*', + relAlias: string | null + } + interface namedColumnExpr { type: 'namedColumnExpr', name: string, @@ -139,7 +145,7 @@ declare module relalgAst { child: relalgOperation, child2?: undefined, assignments?: undefined, - arg: (namedColumnExpr | columnName)[], + arg: (namedColumnExpr | columnName | columnAsterisk)[], wrappedInParentheses?: boolean, metaData?: { [key: string]: any }, diff --git a/src/db/parser/grammar_ra.pegjs b/src/db/parser/grammar_ra.pegjs index dfc30661..09727b17 100644 --- a/src/db/parser/grammar_ra.pegjs +++ b/src/db/parser/grammar_ra.pegjs @@ -254,6 +254,17 @@ columnName relAlias: relAlias }; } + +columnAsterisk += relAlias:(relationName '.')? '*' + { + return { + type: 'column', + name: '*', + relAlias: relAlias ? relAlias[0] : null + }; + } + / relAlias:(relationName '.')? '[' index:$[0-9]+ ']' { if(relAlias != null) @@ -442,6 +453,11 @@ namedColumnExpr { return a; } +/ col:columnAsterisk + { + col.alias = null; + return col; + } // list of columns (kd.id, kd.name, test) e.g. for the projection listOfNamedColumnExpressions diff --git a/src/db/tests/translate_tests_ra.ts b/src/db/tests/translate_tests_ra.ts index 1202114c..62fb2dc1 100644 --- a/src/db/tests/translate_tests_ra.ts +++ b/src/db/tests/translate_tests_ra.ts @@ -254,6 +254,24 @@ QUnit.test('test selection with xor', function (assert) { assert.deepEqual(root.getResult(), ref.getResult()); }); +QUnit.test('test projection[*](R)', function (assert) { + const relations = getTestRelations(); + const query = 'pi * (R)'; + const root = exec_ra(query, relations); + + const ref = exec_ra(`{ + R.a, R.b, R.c + + 1, 'a', 'd' + 3, 'c', 'c' + 4, 'd', 'f' + 5, 'd', 'b' + 6, 'e', 'f' + }`, relations); + + assert.deepEqual(root.getResult(), ref.getResult()); +}); + QUnit.test('test projection[a, b](R)', function (assert) { const relations = getTestRelations(); const query = 'pi a, b (R)'; @@ -301,6 +319,22 @@ QUnit.test('test projection[b, a, a, b](R)', function (assert) { } }); +QUnit.test('test (pi * (R)) inner join [R.b = S.b] (pi * (S))', function (assert) { + const relations = getTestRelations(); + const root = exec_ra('(R) inner join R.b = S.b (S)', relations); + const ref = exec_ra(`{ + R.a:number, R.b:string, R.c:string, S.b:string, S.d:number + + 1, 'a', 'd', 'a', 100 + 3, 'c', 'c', 'c', 400 + 4, 'd', 'f', 'd', 200 + 5, 'd', 'b', 'd', 200 + 6, 'e', 'f', 'e', 150 + }`, relations); + + assert.deepEqual(root.getResult(), ref.getResult()); +}); + QUnit.test('test (R) inner join [R.b = S.b] join (S)', function (assert) { const relations = getTestRelations(); const root = exec_ra('(R) inner join R.b = S.b (S)', relations); diff --git a/src/db/translate/relalgFromAst.ts b/src/db/translate/relalgFromAst.ts index 3f3119e8..8450e526 100644 --- a/src/db/translate/relalgFromAst.ts +++ b/src/db/translate/relalgFromAst.ts @@ -537,7 +537,17 @@ export function relalgFromRelalgAstNode(astNode: relalgAst.relalgOperation, rela for (let i = 0; i < n.arg.length; i++) { const el = n.arg[i]; - if (el.type === 'columnName') { + if (el.type === 'column' && + el.name === '*' && + el.relAlias === null) { + // project all columns + let cols = child.getSchema(); + for (let i = 0; i < cols.getSize(); i++) { + // normal columns + projections.push(cols.getColumn(i)); + } + } + else if (el.type === 'columnName') { const e = el as relalgAst.columnName; projections.push(new Column(e.name, e.relAlias)); } From e4f8b8a57a48cb2d2dbb4d8f3c675bc17cda034c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Laiola=20Guimar=C3=A3es?= Date: Tue, 22 Oct 2024 11:25:49 -0300 Subject: [PATCH 2/4] Update grammar_ra.pegjs --- src/db/parser/grammar_ra.pegjs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/db/parser/grammar_ra.pegjs b/src/db/parser/grammar_ra.pegjs index 09727b17..958bde13 100644 --- a/src/db/parser/grammar_ra.pegjs +++ b/src/db/parser/grammar_ra.pegjs @@ -254,17 +254,6 @@ columnName relAlias: relAlias }; } - -columnAsterisk -= relAlias:(relationName '.')? '*' - { - return { - type: 'column', - name: '*', - relAlias: relAlias ? relAlias[0] : null - }; - } - / relAlias:(relationName '.')? '[' index:$[0-9]+ ']' { if(relAlias != null) @@ -277,6 +266,16 @@ columnAsterisk }; } +columnAsterisk += relAlias:(relationName '.')? '*' + { + return { + type: 'column', + name: '*', + relAlias: relAlias ? relAlias[0] : null + }; + } + // operator names: pi = _ o:('π' { return getNodeInfo('pi'); }) _ From 59411e73ebdc1d37b843c51087767973ac42e0be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Laiola=20Guimar=C3=A3es?= Date: Tue, 22 Oct 2024 11:26:04 -0300 Subject: [PATCH 3/4] Add project all to Bags --- src/db/parser/grammar_bags.d.ts | 8 +++++++- src/db/parser/grammar_bags.pegjs | 15 ++++++++++++++ src/db/tests/translate_tests_bags.ts | 30 ++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/db/parser/grammar_bags.d.ts b/src/db/parser/grammar_bags.d.ts index 5aaa03dd..f1e29748 100644 --- a/src/db/parser/grammar_bags.d.ts +++ b/src/db/parser/grammar_bags.d.ts @@ -80,6 +80,12 @@ declare module relalgAst { relAlias: string | null, } + interface columnAsterisk { + type: 'column', + name: '*', + relAlias: string | null + } + interface namedColumnExpr { type: 'namedColumnExpr', name: string, @@ -140,7 +146,7 @@ declare module relalgAst { child: relalgOperation, child2?: undefined, assignments?: undefined, - arg: (namedColumnExpr | columnName)[], + arg: (namedColumnExpr | columnName | columnAsterisk)[], wrappedInParentheses?: boolean, metaData?: { [key: string]: any }, diff --git a/src/db/parser/grammar_bags.pegjs b/src/db/parser/grammar_bags.pegjs index b971822b..a216dc3f 100644 --- a/src/db/parser/grammar_bags.pegjs +++ b/src/db/parser/grammar_bags.pegjs @@ -266,6 +266,16 @@ columnName }; } +columnAsterisk += relAlias:(relationName '.')? '*' + { + return { + type: 'column', + name: '*', + relAlias: relAlias ? relAlias[0] : null + }; + } + // operator names: delta = _ o:('∂' { return getNodeInfo('delta'); }) _ @@ -448,6 +458,11 @@ namedColumnExpr { return a; } +/ col:columnAsterisk + { + col.alias = null; + return col; + } // list of columns (kd.id, kd.name, test) e.g. for the projection listOfNamedColumnExpressions diff --git a/src/db/tests/translate_tests_bags.ts b/src/db/tests/translate_tests_bags.ts index 01b82601..43ac208c 100644 --- a/src/db/tests/translate_tests_bags.ts +++ b/src/db/tests/translate_tests_bags.ts @@ -160,6 +160,22 @@ QUnit.test('test bag projection[a](R)', function (assert) { assert.deepEqual(root.getResult(false), ref.getResult(false)); }); +QUnit.test('test projection[*](R)', function (assert) { + const relations = getTestBags(); + const query = 'pi * (R)'; + const root = exec_ra(query, relations); + + const ref = exec_ra(`{ + R.a, R.b + + 1, 2 + 5, 6 + 1, 2 + }`, relations); + + assert.deepEqual(root.getResult(false), ref.getResult(false)); +}); + QUnit.test('test (R) bag product (S)', function (assert) { const relations = getTestBags(); const root = exec_ra('(R) x (S)', relations); @@ -272,6 +288,20 @@ QUnit.test('test bag duplicate elimination (R)', function (assert) { assert.deepEqual(root.getResult(false), ref.getResult(false)); }); +QUnit.test('test (pi * (R)) inner join [R.b = S2.b] (pi * (S2))', function (assert) { + const relations = getTestBags(); + const root = exec_ra('(pi * R) inner join R.b = S.b (pi * S2)', relations); + const ref = exec_ra(`{ + R.a:number, R.b:number, S.b:number, S.c:number + 1, 2, 2, 4 + 1, 2, 2, 5 + 1, 2, 2, 4 + 1, 2, 2, 5 + }`, relations); + + assert.deepEqual(root.getResult(false), ref.getResult(false)); +}); + QUnit.test('test bag groupBy a; sum[b] (R2)', function (assert) { const query = 'gamma a; sum(b)->sum_b (R2)'; const relations = getTestBags(); From 2be44e04e47d74cb25400d3a312b87a4c9cad5b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Laiola=20Guimar=C3=A3es?= Date: Tue, 22 Oct 2024 11:48:14 -0300 Subject: [PATCH 4/4] Update relalgFromAst.ts --- src/db/translate/relalgFromAst.ts | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/db/translate/relalgFromAst.ts b/src/db/translate/relalgFromAst.ts index 8450e526..ec1fe256 100644 --- a/src/db/translate/relalgFromAst.ts +++ b/src/db/translate/relalgFromAst.ts @@ -538,13 +538,32 @@ export function relalgFromRelalgAstNode(astNode: relalgAst.relalgOperation, rela const el = n.arg[i]; if (el.type === 'column' && - el.name === '*' && - el.relAlias === null) { + el.name === '*') { + if (el.relAlias === null) { + // project all columns + let cols; + try { + cols = child.getSchema(); + } + catch (e) { + cols = null; + } + if (cols) { + for (let i = 0; i < cols.getSize(); i++) { + // normal columns + projections.push(cols.getColumn(i)); + } + } + else // normal columns + projections.push(new Column(el.name, el.relAlias)); + } // project all columns - let cols = child.getSchema(); - for (let i = 0; i < cols.getSize(); i++) { - // normal columns - projections.push(cols.getColumn(i)); + else if (child.getMetaData('fromVariable') && + child.getMetaData('fromVariable') === el.relAlias) { + projections.push(new Column(el.name, null)); + } + else { + projections.push(new Column(el.name, el.relAlias)); } } else if (el.type === 'columnName') {