Skip to content

Commit

Permalink
Merge pull request #25 from rlaiola/piasterisk
Browse files Browse the repository at this point in the history
Add project all columns (π *) to relational algebra
  • Loading branch information
rlaiola authored Apr 11, 2024
2 parents bf90e55 + 6111b46 commit dd88d46
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/db/parser/grammar_ra.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ declare module relalgAst {
relAlias: string | null,
}

interface columnAsterisk {
type: 'column',
name: '*',
relAlias: string | null
}

interface namedColumnExpr {
type: 'namedColumnExpr',
name: string,
Expand Down Expand Up @@ -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 },
Expand Down
16 changes: 16 additions & 0 deletions src/db/parser/grammar_ra.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions src/db/tests/translate_tests_ra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)';
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 11 additions & 1 deletion src/db/translate/relalgFromAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,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));
}
Expand Down

0 comments on commit dd88d46

Please sign in to comment.