Skip to content

Partial Implementation of aliasing functionality. #1930

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

Open
wants to merge 1 commit into
base: main
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
9 changes: 9 additions & 0 deletions src/RelExpr.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow
import React, {useRef} from 'react';
import {
Alias,
UnaryRelOp,
Projection,
Rename,
Expand Down Expand Up @@ -97,6 +98,14 @@ const RelExpr: StatelessFunctionalComponent<Props> = (props) => {
case 'relation':
return <Relation name={expr.relation} />;

case 'alias':
return (
<Alias
value={expr.alias.value}
alias_value={expr.alias.alias_value}
/>
);

case 'order_by':
return (
<OrderBy
Expand Down
13 changes: 12 additions & 1 deletion src/RelOp.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ export const Intersect: StatelessFunctionalComponent<{||}> = () => (
<span>&cap;</span>
);

export const Alias: StatelessFunctionalComponent<{
value: string,
alias_value: string,
}> = (props) => {
return (
<span>
&rho;<sub>{props.alias_value}</sub> {props.value}
</span>
);
};

export const OrderBy: StatelessFunctionalComponent<{
columns: Array<OrderByColumn>,
relation: Node,
Expand All @@ -146,7 +157,7 @@ export const OrderBy: StatelessFunctionalComponent<{
.join(', ');
return (
<span>
τ<sub>{columnSort}</sub> {props.relation}
&tau;<sub>{columnSort}</sub> {props.relation}
</span>
);
};
Expand Down
30 changes: 30 additions & 0 deletions src/modules/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {produce} from 'immer';
import department from '../resources/Department.json';
import doctor from '../resources/Doctor.json';
import patient from '../resources/Patient.json';
import relation from '../Relation';

export const CHANGE_EXPR = 'CHANGE_EXPR';
export const RESET_EXPR = 'RESET_EXPR';
Expand Down Expand Up @@ -86,6 +87,29 @@ function getCombinedColumns(left: {[string]: any}, right: {[string]: any}) {
return combinedColumns;
}

function getAliasedOutput(relation: {[string]: any}, alias: string) {
const newColumns: Array<string> = [];
const newData: Array<{[string]: any}> = [];
for (const column of relation.columns) {
const newColumnName = alias + '.' + column;
newColumns.push(newColumnName);
}
for (const datum of relation.data) {
let newDatum: {[string]: any} = {};
for (const column of relation.columns) {
newDatum[alias + '.' + column] = datum[column];
}
newData.push(newDatum);
}

const output: Output = {
name: alias,
columns: newColumns,
data: newData,
};
return output;
}

function getCombinedData(
leftName: string,
leftRow: {[string]: any},
Expand Down Expand Up @@ -327,6 +351,12 @@ function applyExpr(
// Make a copy of the data from a source table and return it
return {...sourceData[expr.relation]};

case 'alias':
return getAliasedOutput(
sourceData[expr.alias.value],
expr.alias.alias_value
);

case 'order_by':
let ordData = applyExpr(expr.order_by.children[0], sourceData);

Expand Down
14 changes: 10 additions & 4 deletions src/modules/relexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ function convertExpr(
}

default:
console.log(expr);
// Produce an error if the expression is unsupported
throw new Error('Invalid expression.');
}
Expand Down Expand Up @@ -396,9 +395,16 @@ function buildRelExp(
case 'TableFactor':
// Store this table as one referenced by the query
tables.push(sql.value.value);

return {relation: sql.value.value};

if (sql.hasAs) {
return {
alias: {
value: sql.value.value,
alias_value: sql.alias.value,
},
};
} else {
return {relation: sql.value.value};
}
case 'InnerCrossJoinTable':
// Add the condition if it exists
if (sql.condition) {
Expand Down
14 changes: 14 additions & 0 deletions src/modules/relexp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ it('converts a rename', () => {
});
});

/** @test {relexp} */
it('converts a aliasing operation', () => {
const sql = parser.parse('SELECT bar FROM foo as f');
const action = exprFromSql(sql.value, {foo: ['bar']});
expect(reducer({}, action)).toMatchObject({
expr: {
projection: {
arguments: {project: ['bar']},
children: [{alias: {alias_value: 'f', value: 'foo'}}],
},
},
});
});

/** @test {relexp} */
it('converts a difference', () => {
const sql = parser.parse('SELECT * FROM foo EXCEPT SELECT * FROM bar');
Expand Down