Skip to content

Commit

Permalink
fix(queries): fix slonik validation errors (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartolkaruza committed Oct 30, 2019
1 parent a3ecef8 commit 90185ec
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 87 deletions.
2 changes: 1 addition & 1 deletion example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Server

You need to have PostgresQL install on you environment. After that, create `.env` file and fill it with the following values:
You need to have PostgresQL installed on you environment. After that, create `.env` file and fill it with the following values:

```
PGUSER=postgres
Expand Down
3 changes: 2 additions & 1 deletion example/mapbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<div id="map"></div>
<script>
mapboxgl.accessToken =
'<your-api-key-here>';
'pk.eyJ1IjoicG9yc2NoZWNoYXJnaW5nbWFwIiwiYSI6ImNqeXJiamJveDA4bmozbHF5OHRpOWsyYzYifQ.yH-K9CGaHe7HLgM7XcbbVQ';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v10',
Expand Down Expand Up @@ -73,6 +73,7 @@
id: 'unclustered-point',
type: 'circle',
source: 'points',
'source-layer': `points`,
filter: ['!', ['has', 'point_count']],
paint: {
'circle-color': '#11b4da',
Expand Down
78 changes: 27 additions & 51 deletions lib/__test__/__snapshots__/queries.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

exports[`createClusterQuery should create a clustered Query 1`] = `
"
with filtered AS
(SELECT $1.$2, 1 as theCount $3
with filtered as
(SELECT public.stations.wkb_geometry, 1 as theCount , a
FROM public.stations
WHERE ST_Intersects(TileBBox($4, $5, $6, 3857), ST_Transform($7.$8, 3857))
$9
WHERE ST_Intersects(TileBBox($1, $2, $3, 3857), ST_Transform(wkb_geometry, 3857))
AND status = status AND [1, 2] @> [2, 3]
),
clustered_$10 AS
(SELECT $11 as center,
clustered_10 AS
(SELECT wkb_geometry as center,
theCount,
ST_ClusterDBSCAN($12, $13, 1) over () as clusters $14
ST_ClusterDBSCAN(wkb_geometry, 0.009765625, 1) over () AS clusters
, a
FROM filtered),
grouped_clusters_$15 AS
(SELECT SUM(theCount) as theCount,
$16
ST_Centroid(ST_Collect(center)) as center
FROM clustered_$17
grouped_clusters_10 AS
(SELECT SUM(theCount) AS theCount,
FIRST(a) as a,
ST_Centroid(ST_Collect(center)) AS center
FROM clustered_10
GROUP BY clusters),
clustered_9 AS
(SELECT center,
Expand Down Expand Up @@ -130,50 +130,34 @@ with filtered AS
FROM clustered_1
GROUP BY clusters),
tiled as
(SELECT center,
theCount $18
FROM grouped_clusters_$19
WHERE ST_Intersects(TileBBox($20, $21, $22, 3857), ST_Transform(center, 3857))),
theCount , a
FROM grouped_clusters_1
WHERE ST_Intersects(TileBBox($4, $5, $6, 3857), ST_Transform(center, 3857))),
q as
(SELECT 1 as c1,
ST_AsMVTGeom(ST_Transform(center, 3857), TileBBox($23, $24, $25, 3857), $26, 10, false) AS geom,
jsonb_build_object('count', theCount$27) as attributes
ST_AsMVTGeom(ST_Transform(center, 3857), TileBBox($7, $8, $9, 3857), $10, 10, false) AS geom,
jsonb_build_object('count', theCount, 'a', a) as attributes
FROM tiled)
SELECT ST_AsMVT(q, '$28', $29, 'geom') as mvt
SELECT ST_AsMVT(q, $11, $12, 'geom') as mvt
from q
"
`;

exports[`createClusterQuery should create a clustered Query 2`] = `
Array [
"public.stations",
"wkb_geometry",
", a",
1,
0,
1,
"public.stations",
"wkb_geometry",
"AND status = status AND [1, 2] @> [2, 3]",
10,
"wkb_geometry",
"wkb_geometry",
0.009765625,
", a",
10,
"FIRST(a) as a,",
10,
", a",
1,
1,
0,
1,
1,
0,
1,
512,
", 'a', a",
"points",
512,
]
Expand All @@ -183,36 +167,28 @@ exports[`createClusterQuery should create a unclustered Query 1`] = `
Object {
"sql": "
WITH filtered AS
(SELECT $1.$2 $3
(SELECT public.stations.wkb_geometry , a
FROM public.stations
WHERE ST_Intersects(TileBBox($4, $5, $6, 3857), ST_Transform($7.$8, 3857))
$9
WHERE ST_Intersects(TileBBox($1, $2, $3, 3857), ST_Transform(wkb_geometry, 3857))
),
q as
(SELECT 1 as c1,
ST_AsMVTGeom(ST_Transform($10, 3857), TileBBox($11, $12, $13, 3857), $14, 10, false) AS geom,
jsonb_build_object('count', 1$15) as attributes
ST_AsMVTGeom(ST_Transform(wkb_geometry, 3857), TileBBox($4, $5, $6, 3857), $7, 10, false) AS geom,
jsonb_build_object('count', 1, 'a', a) as attributes
FROM filtered)
SELECT ST_AsMVT(q, '$16', $17, 'geom') as mvt
SELECT ST_AsMVT(q, $8, $9, 'geom') as mvt
from q
",
"type": "SLONIK_TOKEN_SQL",
"values": Array [
"public.stations",
"wkb_geometry",
", a",
15,
0,
1,
"public.stations",
"wkb_geometry",
"",
"wkb_geometry",
15,
0,
1,
512,
", 'a', a",
"points",
512,
],
Expand Down
78 changes: 44 additions & 34 deletions lib/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,25 @@ const unclusteredQuery = ({
}) =>
sql`
WITH filtered AS
(SELECT ${table}.${geometry} ${attributesToSelect(attributes)}
(SELECT ${sql.raw(table)}.${sql.raw(geometry)} ${sql.raw(
attributesToSelect(attributes)
)}
FROM ${sql.raw(table)}
WHERE ST_Intersects(TileBBox(${z}, ${x}, ${y}, 3857), ST_Transform(${table}.${geometry}, 3857))
${query.length > 0 ? `AND ${query.join(' AND ')}` : ''}
WHERE ST_Intersects(TileBBox(${z}, ${x}, ${y}, 3857), ST_Transform(${sql.raw(
geometry
)}, 3857))
${sql.raw(query.length > 0 ? `AND ${query.join(' AND ')}` : '')}
),
q as
(SELECT 1 as c1,
ST_AsMVTGeom(ST_Transform(${geometry}, 3857), TileBBox(${z}, ${x}, ${y}, 3857), ${resolution}, 10, false) AS geom,
jsonb_build_object('count', 1${attributesToArray(
attributes
ST_AsMVTGeom(ST_Transform(${sql.raw(
geometry
)}, 3857), TileBBox(${z}, ${x}, ${y}, 3857), ${resolution}, 10, false) AS geom,
jsonb_build_object('count', 1${sql.raw(
attributesToArray(attributes)
)}) as attributes
FROM filtered)
SELECT ST_AsMVT(q, '${sourceLayer}', ${resolution}, 'geom') as mvt
SELECT ST_AsMVT(q, ${sourceLayer}, ${resolution}, 'geom') as mvt
from q
`;

Expand All @@ -38,30 +44,27 @@ from q
*/
const baseClusteredQuery = ({
filterBlock,
additionalLevels,
z,
x,
y,
sourceLayer,
resolution,
attributes = [],
}) => sql`
with filtered AS
${filterBlock}
${sql.raw(additionalLevels)}
tiled as
(SELECT center,
theCount ${attributesToSelect(attributes)}
FROM grouped_clusters_${z}
theCount ${sql.raw(attributesToSelect(attributes))}
${sql.raw(`FROM grouped_clusters_${z}`)}
WHERE ST_Intersects(TileBBox(${z}, ${x}, ${y}, 3857), ST_Transform(center, 3857))),
q as
(SELECT 1 as c1,
ST_AsMVTGeom(ST_Transform(center, 3857), TileBBox(${z}, ${x}, ${y}, 3857), ${resolution}, 10, false) AS geom,
jsonb_build_object('count', theCount${attributesToArray(
attributes
jsonb_build_object('count', theCount${sql.raw(
attributesToArray(attributes)
)}) as attributes
FROM tiled)
SELECT ST_AsMVT(q, '${sourceLayer}', ${resolution}, 'geom') as mvt
SELECT ST_AsMVT(q, ${sourceLayer}, ${resolution}, 'geom') as mvt
from q
`;

Expand All @@ -77,28 +80,36 @@ const filterBlock = ({
geometry,
query,
attributes,
additionalLevels,
}) =>
sql`
(SELECT ${table}.${geometry}, 1 as theCount ${attributesToSelect(
attributes
sql`
with filtered as
(SELECT ${sql.raw(table)}.${sql.raw(geometry)}, 1 as theCount ${sql.raw(
attributesToSelect(attributes)
)}
FROM ${sql.raw(table)}
WHERE ST_Intersects(TileBBox(${z}, ${x}, ${y}, 3857), ST_Transform(${table}.${geometry}, 3857))
${query.length > 0 ? `AND ${query.join(' AND ')}` : ''}
WHERE ST_Intersects(TileBBox(${z}, ${x}, ${y}, 3857), ST_Transform(${sql.raw(
geometry
)}, 3857))
${sql.raw(query.length > 0 ? `AND ${query.join(' AND ')}` : '')}
),
clustered_${maxZoomLevel} AS
(SELECT ${geometry} as center,
${sql.raw(`clustered_${maxZoomLevel}`)} AS
(SELECT ${sql.raw(geometry)} as center,
theCount,
ST_ClusterDBSCAN(${geometry}, ${zoomToDistance(
maxZoomLevel
)}, 1) over () as clusters ${attributesToSelect(attributes)}
${sql.raw(
`ST_ClusterDBSCAN(${geometry}, ${zoomToDistance(
maxZoomLevel
)}, 1) over () AS clusters`
)}
${sql.raw(attributesToSelect(attributes))}
FROM filtered),
grouped_clusters_${maxZoomLevel} AS
(SELECT SUM(theCount) as theCount,
${attributesFirstToSelect(attributes)}
ST_Centroid(ST_Collect(center)) as center
FROM clustered_${maxZoomLevel}
${sql.raw(`grouped_clusters_${maxZoomLevel}`)} AS
(SELECT SUM(theCount) AS theCount,
${sql.raw(attributesFirstToSelect(attributes))}
ST_Centroid(ST_Collect(center)) AS center
${sql.raw(`FROM clustered_${maxZoomLevel}`)}
GROUP BY clusters),
${sql.raw(additionalLevels)}
`;

/**
Expand Down Expand Up @@ -174,7 +185,6 @@ export function createQueryForTile({
attributes,
});
}
console.log({ additionalLevels });
const ret = baseClusteredQuery({
filterBlock: filterBlock({
z,
Expand All @@ -184,17 +194,17 @@ export function createQueryForTile({
table,
geometry,
query,
additionalLevels,
attributes,
}),
z,
x,
y,
sourceLayer,
additionalLevels,
resolution,
attributes,
});
// console.log(ret);
// console.log(ret.sql);
return ret;
} else {
// Unclustered case
Expand All @@ -209,7 +219,7 @@ export function createQueryForTile({
attributes,
query,
});
// console.log(ret);
// console.log(ret.sql);
return ret;
}
}

0 comments on commit 90185ec

Please sign in to comment.