Skip to content

Commit 38d889f

Browse files
axj2613michaelmior
authored andcommitted
Partial Implementation of aliasing functionality.
1 parent 7975f74 commit 38d889f

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

src/RelExpr.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @flow
22
import React, {useRef} from 'react';
33
import {
4+
Alias,
45
UnaryRelOp,
56
Projection,
67
Rename,
@@ -97,6 +98,14 @@ const RelExpr: StatelessFunctionalComponent<Props> = (props) => {
9798
case 'relation':
9899
return <Relation name={expr.relation} />;
99100

101+
case 'alias':
102+
return (
103+
<Alias
104+
value={expr.alias.value}
105+
alias_value={expr.alias.alias_value}
106+
/>
107+
);
108+
100109
case 'order_by':
101110
return (
102111
<OrderBy

src/RelOp.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ export const Intersect: StatelessFunctionalComponent<{||}> = () => (
137137
<span>&cap;</span>
138138
);
139139

140+
export const Alias: StatelessFunctionalComponent<{
141+
value: string,
142+
alias_value: string,
143+
}> = (props) => {
144+
return (
145+
<span>
146+
&rho;<sub>{props.alias_value}</sub> {props.value}
147+
</span>
148+
);
149+
};
150+
140151
export const OrderBy: StatelessFunctionalComponent<{
141152
columns: Array<OrderByColumn>,
142153
relation: Node,
@@ -146,7 +157,7 @@ export const OrderBy: StatelessFunctionalComponent<{
146157
.join(', ');
147158
return (
148159
<span>
149-
τ<sub>{columnSort}</sub> {props.relation}
160+
&tau;<sub>{columnSort}</sub> {props.relation}
150161
</span>
151162
);
152163
};

src/modules/data.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {produce} from 'immer';
55
import department from '../resources/Department.json';
66
import doctor from '../resources/Doctor.json';
77
import patient from '../resources/Patient.json';
8+
import relation from '../Relation';
89

910
export const CHANGE_EXPR = 'CHANGE_EXPR';
1011
export const RESET_EXPR = 'RESET_EXPR';
@@ -86,6 +87,29 @@ function getCombinedColumns(left: {[string]: any}, right: {[string]: any}) {
8687
return combinedColumns;
8788
}
8889

90+
function getAliasedOutput(relation: {[string]: any}, alias: string) {
91+
const newColumns: Array<string> = [];
92+
const newData: Array<{[string]: any}> = [];
93+
for (const column of relation.columns) {
94+
const newColumnName = alias + '.' + column;
95+
newColumns.push(newColumnName);
96+
}
97+
for (const datum of relation.data) {
98+
let newDatum: {[string]: any} = {};
99+
for (const column of relation.columns) {
100+
newDatum[alias + '.' + column] = datum[column];
101+
}
102+
newData.push(newDatum);
103+
}
104+
105+
const output: Output = {
106+
name: alias,
107+
columns: newColumns,
108+
data: newData,
109+
};
110+
return output;
111+
}
112+
89113
function getCombinedData(
90114
leftName: string,
91115
leftRow: {[string]: any},
@@ -327,6 +351,12 @@ function applyExpr(
327351
// Make a copy of the data from a source table and return it
328352
return {...sourceData[expr.relation]};
329353

354+
case 'alias':
355+
return getAliasedOutput(
356+
sourceData[expr.alias.value],
357+
expr.alias.alias_value
358+
);
359+
330360
case 'order_by':
331361
let ordData = applyExpr(expr.order_by.children[0], sourceData);
332362

src/modules/relexp.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ function convertExpr(
279279
}
280280

281281
default:
282-
console.log(expr);
283282
// Produce an error if the expression is unsupported
284283
throw new Error('Invalid expression.');
285284
}
@@ -396,9 +395,16 @@ function buildRelExp(
396395
case 'TableFactor':
397396
// Store this table as one referenced by the query
398397
tables.push(sql.value.value);
399-
400-
return {relation: sql.value.value};
401-
398+
if (sql.hasAs) {
399+
return {
400+
alias: {
401+
value: sql.value.value,
402+
alias_value: sql.alias.value,
403+
},
404+
};
405+
} else {
406+
return {relation: sql.value.value};
407+
}
402408
case 'InnerCrossJoinTable':
403409
// Add the condition if it exists
404410
if (sql.condition) {

src/modules/relexp.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ it('converts a rename', () => {
4747
});
4848
});
4949

50+
/** @test {relexp} */
51+
it('converts a aliasing operation', () => {
52+
const sql = parser.parse('SELECT bar FROM foo as f');
53+
const action = exprFromSql(sql.value, {foo: ['bar']});
54+
expect(reducer({}, action)).toMatchObject({
55+
expr: {
56+
projection: {
57+
arguments: {project: ['bar']},
58+
children: [{alias: {alias_value: 'f', value: 'foo'}}],
59+
},
60+
},
61+
});
62+
});
63+
5064
/** @test {relexp} */
5165
it('converts a difference', () => {
5266
const sql = parser.parse('SELECT * FROM foo EXCEPT SELECT * FROM bar');

0 commit comments

Comments
 (0)