Skip to content

Commit

Permalink
Normalize table and column alias SQL to use uppercase AS
Browse files Browse the repository at this point in the history
  • Loading branch information
jonvuri committed Jul 12, 2019
1 parent 08f7292 commit 52dcdca
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 53 deletions.
8 changes: 4 additions & 4 deletions examples/mapd-charting/src/crossfilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ export const pointmapGroup = {
},
getProjectOn() {
return [
"conv_4326_900913_x(lon) as x",
"conv_4326_900913_y(lat) as y",
"lang as color",
"followers as size"
"conv_4326_900913_x(lon) AS x",
"conv_4326_900913_y(lat) AS y",
"lang AS color",
"followers AS size"
];
}
};
Expand Down
32 changes: 24 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 21 additions & 7 deletions packages/data-layer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/data-layer/src/parser/parse-aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function aggregateField(op: string, field: string, as: string): string {
str += `${op}(${field})`;
}

return str + `${as ? " as " + as : ""}`;
return str + `${as ? " AS " + as : ""}`;
}

function parseGroupBy(
Expand All @@ -59,7 +59,7 @@ function parseGroupBy(
} else if (groupby.type === "project") {
sql.select.push(
parser.parseExpression(groupby.expr) +
(groupby.as ? " as " + groupby.as : "")
(groupby.as ? " AS " + groupby.as : "")
);
if (groupby.as) {
sql.groupby.push(groupby.as);
Expand Down
2 changes: 1 addition & 1 deletion packages/data-layer/src/parser/parse-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function parseBin(
cast((cast(${field} as float) - ${extent[0]}) * ${maxbins /
(numBins || 1) } as int)
end
as ${as}`
AS ${as}`
);
sql.where.push(
`((${field} >= ${extent[0]} AND ${field} <= ${
Expand Down
2 changes: 1 addition & 1 deletion packages/data-layer/src/parser/parse-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function parseProject(
): SQL {
sql.select.push(
parser.parseExpression(transform.expr) +
(transform.as ? " as " + transform.as : "")
(transform.as ? " AS " + transform.as : "")
);
return sql;
}
2 changes: 1 addition & 1 deletion packages/data-layer/src/parser/parse-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default function parseSource(
// $FlowFixMe
const joinStmt = left + " " + joinRelation(joinType) + " " + right;
const aliasStmt = typeof transform.as === "string"
? " as " + transform.as
? " AS " + transform.as
: "";
return stmt.concat(joinStmt + aliasStmt);
} else if (transform.type === "data" || transform.type === "root") {
Expand Down
8 changes: 4 additions & 4 deletions packages/data-layer/tests/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ tape("Integration Test", assert => {

assert.equal(
chartNode1.toSQL(),
"SELECT payment_type as key0, COUNT(*) as val FROM flights GROUP BY key0 ORDER BY val ASC LIMIT 10"
"SELECT payment_type AS key0, COUNT(*) AS val FROM flights GROUP BY key0 ORDER BY val ASC LIMIT 10"
);

assert.equal(
chartNode2.toSQL(),
"SELECT case when\n trip_distance >= 30\n then\n 29\n else\n cast((cast(trip_distance as float) - 0) * 1 as int)\n end\n as key0, COUNT(*) as val FROM flights WHERE ((trip_distance >= 0 AND trip_distance <= 30) OR (trip_distance IS NULL)) GROUP BY key0 HAVING (key0 >= 0 AND key0 < 30 OR key0 IS NULL)"
"SELECT case when\n trip_distance >= 30\n then\n 29\n else\n cast((cast(trip_distance as float) - 0) * 1 as int)\n end\n AS key0, COUNT(*) AS val FROM flights WHERE ((trip_distance >= 0 AND trip_distance <= 30) OR (trip_distance IS NULL)) GROUP BY key0 HAVING (key0 >= 0 AND key0 < 30 OR key0 IS NULL)"
);

globalFilterNode.transform(
Expand Down Expand Up @@ -157,11 +157,11 @@ tape("Integration Test", assert => {

assert.equal(
chartNode1.toSQL(),
"SELECT payment_type as key0, COUNT(*) as val FROM flights WHERE (trip_distance BETWEEN 12 AND 20) AND (dropoff_longitude BETWEEN -73.99828105055514 AND -73.7766089742046) AND (dropoff_latitude BETWEEN 40.63646686110235 AND 40.81468768513369) GROUP BY key0 ORDER BY val ASC LIMIT 10"
"SELECT payment_type AS key0, COUNT(*) AS val FROM flights WHERE (trip_distance BETWEEN 12 AND 20) AND (dropoff_longitude BETWEEN -73.99828105055514 AND -73.7766089742046) AND (dropoff_latitude BETWEEN 40.63646686110235 AND 40.81468768513369) GROUP BY key0 ORDER BY val ASC LIMIT 10"
);

assert.equal(
chartNode2.toSQL(),
"SELECT case when\n trip_distance >= 30\n then\n 29\n else\n cast((cast(trip_distance as float) - 0) * 1 as int)\n end\n as key0, COUNT(*) as val FROM flights WHERE ((trip_distance >= 0 AND trip_distance <= 30) OR (trip_distance IS NULL)) AND (payment_type = \'cash\') AND (dropoff_longitude BETWEEN -73.99828105055514 AND -73.7766089742046) AND (dropoff_latitude BETWEEN 40.63646686110235 AND 40.81468768513369) GROUP BY key0 HAVING (key0 >= 0 AND key0 < 30 OR key0 IS NULL)"
"SELECT case when\n trip_distance >= 30\n then\n 29\n else\n cast((cast(trip_distance as float) - 0) * 1 as int)\n end\n AS key0, COUNT(*) AS val FROM flights WHERE ((trip_distance >= 0 AND trip_distance <= 30) OR (trip_distance IS NULL)) AND (payment_type = \'cash\') AND (dropoff_longitude BETWEEN -73.99828105055514 AND -73.7766089742046) AND (dropoff_latitude BETWEEN 40.63646686110235 AND 40.81468768513369) GROUP BY key0 HAVING (key0 >= 0 AND key0 < 30 OR key0 IS NULL)"
);
});
16 changes: 8 additions & 8 deletions packages/data-layer/tests/parser/parse-aggregate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ tape("aggregate", assert => {
),
{
select: [
"date_trunc(day, dep_timestamp) as x",
"extract(month from dep_timestamp) as z",
"COUNT(*) as y"
"date_trunc(day, dep_timestamp) AS x",
"extract(month from dep_timestamp) AS z",
"COUNT(*) AS y"
],
from: "",
where: [],
Expand Down Expand Up @@ -87,7 +87,7 @@ tape("aggregate", assert => {
}
),
{
select: ["date_trunc(day, dep_timestamp) as x", "COUNT(*) as y"],
select: ["date_trunc(day, dep_timestamp) AS x", "COUNT(*) AS y"],
from: "",
where: [],
groupby: ["x"],
Expand Down Expand Up @@ -118,7 +118,7 @@ tape("aggregate", assert => {
}
),
{
select: ["party", "COUNT(*) as key0"],
select: ["party", "COUNT(*) AS key0"],
from: "",
where: [],
groupby: ["party"],
Expand Down Expand Up @@ -152,8 +152,8 @@ tape("aggregate", assert => {
select: [
"city",
"state",
"AVG(airtime) as val",
"COUNT(depdelay) as color"
"AVG(airtime) AS val",
"COUNT(depdelay) AS color"
],
from: "",
where: [],
Expand Down Expand Up @@ -185,7 +185,7 @@ tape("aggregate", assert => {
}
),
{
select: ["carrier", "SUM(airtime) as val", "MIN(depdelay) as color"],
select: ["carrier", "SUM(airtime) AS val", "MIN(depdelay) AS color"],
from: "",
where: [],
groupby: ["carrier"],
Expand Down
Loading

0 comments on commit 52dcdca

Please sign in to comment.