|
|
|
@ -108,16 +108,6 @@ const aggregateColumns = ctx => {
|
|
|
|
|
}, ctx.columns || {});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const aggregateColumnNames = (ctx, table) => {
|
|
|
|
|
let columns = aggregateColumns(ctx);
|
|
|
|
|
if (table) {
|
|
|
|
|
return sep(Object.keys(columns).map(
|
|
|
|
|
column_name => `${table}.${column_name}`
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
return sep(Object.keys(columns));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const aggregateExpression = (column_name, column_parameters) => {
|
|
|
|
|
const aggregate_function = column_parameters.aggregate_function || 'count';
|
|
|
|
|
const aggregate_definition = SUPPORTED_AGGREGATE_FUNCTIONS[aggregate_function];
|
|
|
|
@ -342,29 +332,41 @@ const gridInfoQuery = ctx => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Function to generate the resulting point for a cell from the aggregated data
|
|
|
|
|
// Point sample joins the query with itself to get the data from the lowest id
|
|
|
|
|
const aggregatedPoint = (ctx, aggregated) => {
|
|
|
|
|
const placement = ctx.placement || DEFAULT_PLACEMENT;
|
|
|
|
|
switch (placement) {
|
|
|
|
|
const aggregatedPointWebMercator = (ctx) => {
|
|
|
|
|
switch (ctx.placement) {
|
|
|
|
|
|
|
|
|
|
// For centroid, we return the average of the cell
|
|
|
|
|
case `centroid`:
|
|
|
|
|
return aggregated ?
|
|
|
|
|
`, ST_SetSRID(ST_MakePoint(AVG(cdb_x), AVG(cdb_y)), 3857) AS the_geom_webmercator` :
|
|
|
|
|
``;
|
|
|
|
|
return `, ST_SetSRID(ST_MakePoint(AVG(cdb_x), AVG(cdb_y)), 3857) AS the_geom_webmercator`;
|
|
|
|
|
|
|
|
|
|
// Middle point of the cell
|
|
|
|
|
case `point-grid`:
|
|
|
|
|
return aggregated ?
|
|
|
|
|
`, ST_SetSRID(ST_MakePoint(cdb_pos_grid_x, cdb_pos_grid_y), 3857) AS the_geom_webmercator`:
|
|
|
|
|
``;
|
|
|
|
|
return `, ST_SetSRID(ST_MakePoint(cdb_pos_grid_x, cdb_pos_grid_y), 3857) AS the_geom_webmercator`;
|
|
|
|
|
|
|
|
|
|
// For point-sample we'll get a single point directly from the source
|
|
|
|
|
// If it's default aggregation we'll add the extra columns to keep backwards compatibility
|
|
|
|
|
case `point-sample`:
|
|
|
|
|
return ``;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new Error(`Invalid aggregation placement "${ctx.placement}`);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Function to generate the resulting point for a cell from the a join with the source
|
|
|
|
|
const aggregatedPointJoin = (ctx) => {
|
|
|
|
|
switch (ctx.placement) {
|
|
|
|
|
|
|
|
|
|
case `centroid`:
|
|
|
|
|
return ``;
|
|
|
|
|
|
|
|
|
|
case `point-grid`:
|
|
|
|
|
return ``;
|
|
|
|
|
|
|
|
|
|
// For point-sample we'll get a single point directly from the source
|
|
|
|
|
// If it's default aggregation we'll add the extra columns to keep backwards compatibility
|
|
|
|
|
case `point-sample`:
|
|
|
|
|
return aggregated ?
|
|
|
|
|
`` :
|
|
|
|
|
`NATURAL JOIN
|
|
|
|
|
return `NATURAL JOIN
|
|
|
|
|
(
|
|
|
|
|
SELECT ${ctx.isDefaultAggregation ? `*` : `cartodb_id, the_geom_webmercator`}
|
|
|
|
|
FROM
|
|
|
|
@ -375,7 +377,7 @@ const aggregatedPoint = (ctx, aggregated) => {
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new Error(`Invalid aggregation placement "${placement}`);
|
|
|
|
|
throw new Error(`Invalid aggregation placement "${ctx.placement}`);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -384,8 +386,7 @@ const aggregatedPoint = (ctx, aggregated) => {
|
|
|
|
|
// get the coordinates of the mid point so we don't need to calculate them later
|
|
|
|
|
// which requires extra data in the group by clause
|
|
|
|
|
const aggregatedPosCoordinate = (ctx, coordinate) => {
|
|
|
|
|
const placement = ctx.placement || DEFAULT_PLACEMENT;
|
|
|
|
|
switch (placement) {
|
|
|
|
|
switch (ctx.placement) {
|
|
|
|
|
// For point-grid we return the coordinate of the middle point of the grid
|
|
|
|
|
case `point-grid`:
|
|
|
|
|
return `(FLOOR(cdb_${coordinate} / __cdb_grid_params.cdb_res) + 0.5) * __cdb_grid_params.cdb_res`;
|
|
|
|
@ -406,7 +407,7 @@ SELECT * FROM
|
|
|
|
|
(
|
|
|
|
|
SELECT
|
|
|
|
|
min(cartodb_id) as cartodb_id
|
|
|
|
|
${aggregatedPoint(ctx, true)}
|
|
|
|
|
${aggregatedPointWebMercator(ctx)}
|
|
|
|
|
${dimensionDefs(ctx)}
|
|
|
|
|
${aggregateColumnDefs(ctx)}
|
|
|
|
|
FROM
|
|
|
|
@ -433,7 +434,7 @@ SELECT * FROM
|
|
|
|
|
GROUP BY cdb_pos_grid_x, cdb_pos_grid_y ${dimensionNames(ctx)}
|
|
|
|
|
${havingClause(ctx)}
|
|
|
|
|
) __cdb_aggregation_src
|
|
|
|
|
${aggregatedPoint(ctx, false)}
|
|
|
|
|
${aggregatedPointJoin(ctx)}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const aggregationQueryTemplates = {
|
|
|
|
|