Skip to content

Commit

Permalink
fix(sf|quadbin): QUADBIN_TOPARENT not working with views (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
vdelacruzb authored Feb 1, 2024
1 parent 3f399b0 commit d31d3ed
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 17 deletions.
8 changes: 8 additions & 0 deletions clouds/snowflake/common/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ async function deleteTable (tablename) {
await runQuery(query);
}

async function deleteView (tablename) {
const query = `
DROP VIEW IF EXISTS ${tablename}
`;
await runQuery(query);
}

function sortByKey (list, key) {
return list.sort((a, b) => (a[key] > b[key]) ? 1 : -1);
}
Expand Down Expand Up @@ -113,6 +120,7 @@ module.exports = {
runQuery,
createTable,
deleteTable,
deleteView,
sortByKey,
sortByKeyAndRound,
existsTable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ CREATE OR REPLACE SECURE FUNCTION @@SF_SCHEMA@@.QUADBIN_RESOLUTION
RETURNS INT
IMMUTABLE
AS $$
SELECT BITAND(BITSHIFTRIGHT(quadbin, 52), 31)
BITAND(BITSHIFTRIGHT(quadbin, 52), 31)
$$;
28 changes: 13 additions & 15 deletions clouds/snowflake/modules/sql/quadbin/QUADBIN_TOPARENT.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,20 @@ IMMUTABLE
AS $$
IFF(quadbin IS NULL OR resolution IS NULL OR resolution < 0 OR resolution > 26,
NULL,
(
SELECT bitor(
bitor(
bitand(
quadbin,
bitnot(
bitshiftleft(31, 52)
)
),
bitshiftleft(resolution, 52)
),
bitshiftright(
4503599627370495,
resolution * 2
bitor(
bitor(
bitand(
quadbin,
bitnot(
bitshiftleft(31, 52)
)
)
),
bitshiftleft(resolution, 52)
),
bitshiftright(
4503599627370495,
resolution * 2
)
)
)
$$;
18 changes: 17 additions & 1 deletion clouds/snowflake/modules/test/quadbin/QUADBIN_TOPARENT.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
const { runQuery } = require('../../../common/test-utils');
const { runQuery, deleteTable, deleteView } = require('../../../common/test-utils');

test('QUADBIN_TOPARENT should work', async () => {
const query = 'SELECT CAST(QUADBIN_TOPARENT(5209574053332910079, 3) AS STRING) AS OUTPUT';
const rows = await runQuery(query);
expect(rows.length).toEqual(1);
expect(rows[0].OUTPUT).toEqual('5205105638077628415');
});

test('QUADBIN_TOPARENT should work with nested functions when readin data from views', async () => {
const inputTable = '@@SF_SCHEMA@@.coords_sample';
const inputTableView = '@@SF_SCHEMA@@.test_quadbin_toparent_view';

query = `CREATE VIEW IF NOT EXISTS ${inputTableView} AS
SELECT * FROM ${inputTable};`;
await runQuery(query);

query = `SELECT CAST(QUADBIN_TOPARENT(QUADBIN_FROMLONGLAT(long, lat, zoom), 6) AS STRING) AS OUTPUT
FROM ${inputTableView};`;
const rows = await runQuery(query);
expect(rows.length).toEqual(120);

deleteView(inputTableView);
});
120 changes: 120 additions & 0 deletions clouds/snowflake/modules/test/quadbin/fixtures/coords_sample.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
SELECT 9 AS zoom,-180.000000 as long,36.000000 as lat
UNION ALL SELECT 8,-180.000000,54.000000
UNION ALL SELECT 9,-180.000000,54.000000
UNION ALL SELECT 7,-180.000000,18.000000
UNION ALL SELECT 8,-180.000000,18.000000
UNION ALL SELECT 8,-180.000000,72.000000
UNION ALL SELECT 9,-180.000000,72.000000
UNION ALL SELECT 7,-180.000000,54.000000
UNION ALL SELECT 7,-144.000000,18.000000
UNION ALL SELECT 8,-144.000000,18.000000
UNION ALL SELECT 8,-144.000000,36.000000
UNION ALL SELECT 8,-72.000000,54.000000
UNION ALL SELECT 9,-72.000000,54.000000
UNION ALL SELECT 7,-72.000000,72.000000
UNION ALL SELECT 7,-36.000000,18.000000
UNION ALL SELECT 9,0.000000,54.000000
UNION ALL SELECT 9,0.000000,72.000000
UNION ALL SELECT 7,-180.000000,36.000000
UNION ALL SELECT 8,-180.000000,36.000000
UNION ALL SELECT 7,-180.000000,72.000000
UNION ALL SELECT 9,-144.000000,18.000000
UNION ALL SELECT 7,-144.000000,36.000000
UNION ALL SELECT 9,-144.000000,54.000000
UNION ALL SELECT 7,-108.000000,72.000000
UNION ALL SELECT 9,-36.000000,18.000000
UNION ALL SELECT 7,144.000000,18.000000
UNION ALL SELECT 9,-180.000000,18.000000
UNION ALL SELECT 9,-144.000000,36.000000
UNION ALL SELECT 7,-144.000000,54.000000
UNION ALL SELECT 8,-144.000000,54.000000
UNION ALL SELECT 7,-108.000000,54.000000
UNION ALL SELECT 8,-108.000000,54.000000
UNION ALL SELECT 8,-108.000000,72.000000
UNION ALL SELECT 7,-72.000000,54.000000
UNION ALL SELECT 9,-72.000000,72.000000
UNION ALL SELECT 8,-36.000000,18.000000
UNION ALL SELECT 7,-36.000000,36.000000
UNION ALL SELECT 8,-36.000000,36.000000
UNION ALL SELECT 7,0.000000,54.000000
UNION ALL SELECT 8,0.000000,54.000000
UNION ALL SELECT 7,0.000000,72.000000
UNION ALL SELECT 8,36.000000,18.000000
UNION ALL SELECT 9,36.000000,18.000000
UNION ALL SELECT 7,72.000000,36.000000
UNION ALL SELECT 8,72.000000,36.000000
UNION ALL SELECT 9,0.000000,36.000000
UNION ALL SELECT 8,144.000000,18.000000
UNION ALL SELECT 9,144.000000,18.000000
UNION ALL SELECT 7,-144.000000,72.000000
UNION ALL SELECT 8,-144.000000,72.000000
UNION ALL SELECT 8,-108.000000,36.000000
UNION ALL SELECT 9,-108.000000,36.000000
UNION ALL SELECT 7,-72.000000,18.000000
UNION ALL SELECT 9,-72.000000,18.000000
UNION ALL SELECT 7,-72.000000,36.000000
UNION ALL SELECT 9,-72.000000,36.000000
UNION ALL SELECT 8,-36.000000,54.000000
UNION ALL SELECT 7,0.000000,18.000000
UNION ALL SELECT 8,0.000000,18.000000
UNION ALL SELECT 9,0.000000,18.000000
UNION ALL SELECT 8,36.000000,54.000000
UNION ALL SELECT 8,36.000000,72.000000
UNION ALL SELECT 7,72.000000,18.000000
UNION ALL SELECT 8,72.000000,18.000000
UNION ALL SELECT 7,108.000000,54.000000
UNION ALL SELECT 9,108.000000,54.000000
UNION ALL SELECT 7,144.000000,36.000000
UNION ALL SELECT 8,144.000000,36.000000
UNION ALL SELECT 9,144.000000,54.000000
UNION ALL SELECT 9,-36.000000,72.000000
UNION ALL SELECT 8,-72.000000,36.000000
UNION ALL SELECT 7,36.000000,72.000000
UNION ALL SELECT 8,108.000000,18.000000
UNION ALL SELECT 9,144.000000,72.000000
UNION ALL SELECT 7,108.000000,72.000000
UNION ALL SELECT 9,108.000000,72.000000
UNION ALL SELECT 9,-108.000000,54.000000
UNION ALL SELECT 9,-108.000000,72.000000
UNION ALL SELECT 7,0.000000,36.000000
UNION ALL SELECT 8,0.000000,36.000000
UNION ALL SELECT 8,0.000000,72.000000
UNION ALL SELECT 7,36.000000,18.000000
UNION ALL SELECT 7,36.000000,36.000000
UNION ALL SELECT 8,36.000000,36.000000
UNION ALL SELECT 9,36.000000,36.000000
UNION ALL SELECT 9,72.000000,36.000000
UNION ALL SELECT 9,-144.000000,72.000000
UNION ALL SELECT 7,-108.000000,36.000000
UNION ALL SELECT 8,-72.000000,18.000000
UNION ALL SELECT 7,-36.000000,54.000000
UNION ALL SELECT 9,-36.000000,54.000000
UNION ALL SELECT 9,36.000000,54.000000
UNION ALL SELECT 9,72.000000,18.000000
UNION ALL SELECT 7,72.000000,54.000000
UNION ALL SELECT 8,72.000000,54.000000
UNION ALL SELECT 9,72.000000,54.000000
UNION ALL SELECT 7,72.000000,72.000000
UNION ALL SELECT 9,108.000000,18.000000
UNION ALL SELECT 9,144.000000,36.000000
UNION ALL SELECT 7,144.000000,54.000000
UNION ALL SELECT 8,144.000000,54.000000
UNION ALL SELECT 8,144.000000,72.000000
UNION ALL SELECT 7,-108.000000,18.000000
UNION ALL SELECT 9,-108.000000,18.000000
UNION ALL SELECT 8,-36.000000,72.000000
UNION ALL SELECT 8,-72.000000,72.000000
UNION ALL SELECT 7,36.000000,54.000000
UNION ALL SELECT 9,72.000000,72.000000
UNION ALL SELECT 7,108.000000,18.000000
UNION ALL SELECT 8,108.000000,54.000000
UNION ALL SELECT 7,-36.000000,72.000000
UNION ALL SELECT 7,108.000000,36.000000
UNION ALL SELECT 9,108.000000,36.000000
UNION ALL SELECT 8,108.000000,72.000000
UNION ALL SELECT 9,-36.000000,36.000000
UNION ALL SELECT 9,36.000000,72.000000
UNION ALL SELECT 8,72.000000,72.000000
UNION ALL SELECT 8,-108.000000,18.000000
UNION ALL SELECT 8,108.000000,36.000000
UNION ALL SELECT 7,144.000000,72.000000
12 changes: 12 additions & 0 deletions clouds/snowflake/modules/test/quadbin/global/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { createTable, deleteTable } = require('../../../../common/test-utils');

async function initializeTables () {
await Promise.all([
createTable(
'coords_sample',
'./test/quadbin/fixtures/coords_sample.sql'
)
]);
}

module.exports = initializeTables;
9 changes: 9 additions & 0 deletions clouds/snowflake/modules/test/quadbin/global/teardown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { deleteTable } = require('../../../../common/test-utils');

async function deleteTables () {
await Promise.all([
deleteTable('coords_sample')
]);
}

module.exports = deleteTables;

0 comments on commit d31d3ed

Please sign in to comment.