Skip to content
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

fix(bq|h3): fix broken reference in H3_POLYFILL_TABLE #460

Merged
merged 3 commits into from
Dec 28, 2023
Merged
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
2 changes: 1 addition & 1 deletion clouds/bigquery/modules/sql/h3/H3_POLYFILL_TABLE.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ AS """
return 'CREATE TABLE `' + output_table + '` CLUSTER BY (h3) AS\\n' +
'WITH __input AS (' + input_query + '),\\n' +
'__cells AS (SELECT h3, i.* FROM __input AS i,\\n' +
'UNNEST(`@@BQ_DATASET@@.__H3_POLYFILL_INIT`(geom,`@@BQ_DATASET@@.__H3_POLYFILL_INIT_Z`(geom,' + resolution + '))) AS parent,\\n' +
'UNNEST(`@@BQ_DATASET@@.__H3_POLYFILL_INIT_BBOX`(geom,`@@BQ_DATASET@@.__H3_POLYFILL_INIT_Z`(geom,' + resolution + '))) AS parent,\\n' +
'UNNEST(`@@BQ_DATASET@@.H3_TOCHILDREN`(parent,' + resolution + ')) AS h3)\\n' +
'SELECT * EXCEPT (geom) FROM __cells\\n' +
'WHERE ' + containmentFunction + '(geom, `' + cellFunction + '`(h3));'
Expand Down
20 changes: 19 additions & 1 deletion clouds/bigquery/modules/test/h3/H3_POLYFILL_TABLE.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ const { runQuery } = require('../../../common/test-utils');

const BQ_DATASET = process.env.BQ_DATASET;

test('H3_POLYFILL_TABLE should work', async () => {
output_table_name = 'h3_polyfill_table_geom_output'
let query = `DROP TABLE IF EXISTS \`@@BQ_DATASET@@.${output_table_name}\`;
CALL \`@@BQ_DATASET@@.H3_POLYFILL_TABLE\`(
'SELECT ST_GEOGFROMTEXT("POLYGON ((-3.71219873428345 40.413365349070865, -3.7144088745117 40.40965661286395, -3.70659828186035 40.409525904775634, -3.71219873428345 40.413365349070865))") as geom',
9, 'center',
'@@BQ_DATASET@@.${output_table_name}'
)`;
await runQuery(query);

const rows = await runQuery(`SELECT * FROM \`@@BQ_DATASET@@.${output_table_name}\``);
expect(rows.length).toEqual(1);
expect(rows[0].h3).toEqual('89390cb1b4bffff');

query = `DROP TABLE IF EXISTS \`@@BQ_DATASET@@.${output_table_name}\``;
await runQuery(query);
});

test('H3_POLYFILL_TABLE should generate the correct query', async () => {
const query = `SELECT \`@@BQ_DATASET@@.__H3_POLYFILL_QUERY\`(
'SELECT geom, name, value FROM \`<project>.<dataset>.<table>\`',
Expand All @@ -13,7 +31,7 @@ test('H3_POLYFILL_TABLE should generate the correct query', async () => {
expect(rows[0].output).toEqual(`CREATE TABLE \`<project>.<dataset>.<output_table>\` CLUSTER BY (h3) AS
WITH __input AS (SELECT geom, name, value FROM \`<project>.<dataset>.<table>\`),
__cells AS (SELECT h3, i.* FROM __input AS i,
UNNEST(\`@@BQ_DATASET@@.__H3_POLYFILL_INIT\`(geom,\`@@BQ_DATASET@@.__H3_POLYFILL_INIT_Z\`(geom,12))) AS parent,
UNNEST(\`@@BQ_DATASET@@.__H3_POLYFILL_INIT_BBOX\`(geom,\`@@BQ_DATASET@@.__H3_POLYFILL_INIT_Z\`(geom,12))) AS parent,
UNNEST(\`@@BQ_DATASET@@.H3_TOCHILDREN\`(parent,12)) AS h3)
SELECT * EXCEPT (geom) FROM __cells
WHERE ST_INTERSECTS(geom, \`@@BQ_DATASET@@.H3_CENTER\`(h3));`.replace(/@@BQ_DATASET@@/g, BQ_DATASET));
Expand Down
Loading