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 project all columns (π *) to relational and multiset algebra #203

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion src/db/parser/grammar_bags.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,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 @@ -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 },
Expand Down
15 changes: 15 additions & 0 deletions src/db/parser/grammar_bags.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ columnName
};
}

columnAsterisk
= relAlias:(relationName '.')? '*'
{
return {
type: 'column',
name: '*',
relAlias: relAlias ? relAlias[0] : null
};
}

// operator names:
delta
= _ o:('∂' { return getNodeInfo('delta'); }) _
Expand Down Expand Up @@ -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
Expand Down
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
15 changes: 15 additions & 0 deletions src/db/parser/grammar_ra.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ columnName
};
}

columnAsterisk
= relAlias:(relationName '.')? '*'
{
return {
type: 'column',
name: '*',
relAlias: relAlias ? relAlias[0] : null
};
}

// operator names:
pi
= _ o:('π' { return getNodeInfo('pi'); }) _
Expand Down Expand Up @@ -442,6 +452,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
30 changes: 30 additions & 0 deletions src/db/tests/translate_tests_bags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
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
31 changes: 30 additions & 1 deletion src/db/translate/relalgFromAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,36 @@ 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 === '*') {
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
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') {
const e = el as relalgAst.columnName;
projections.push(new Column(e.name, e.relAlias));
}
Expand Down