diff --git a/clouds/snowflake/common/test-utils.js b/clouds/snowflake/common/test-utils.js index b73a2dac4..3dd81e65b 100644 --- a/clouds/snowflake/common/test-utils.js +++ b/clouds/snowflake/common/test-utils.js @@ -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); } @@ -113,6 +120,7 @@ module.exports = { runQuery, createTable, deleteTable, + deleteView, sortByKey, sortByKeyAndRound, existsTable, diff --git a/clouds/snowflake/modules/sql/quadbin/QUADBIN_RESOLUTION.sql b/clouds/snowflake/modules/sql/quadbin/QUADBIN_RESOLUTION.sql index 2f31f02c9..61128bfec 100644 --- a/clouds/snowflake/modules/sql/quadbin/QUADBIN_RESOLUTION.sql +++ b/clouds/snowflake/modules/sql/quadbin/QUADBIN_RESOLUTION.sql @@ -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) $$; diff --git a/clouds/snowflake/modules/sql/quadbin/QUADBIN_TOPARENT.sql b/clouds/snowflake/modules/sql/quadbin/QUADBIN_TOPARENT.sql index d75a9b600..6854ce95c 100644 --- a/clouds/snowflake/modules/sql/quadbin/QUADBIN_TOPARENT.sql +++ b/clouds/snowflake/modules/sql/quadbin/QUADBIN_TOPARENT.sql @@ -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 + ) ) ) $$; diff --git a/clouds/snowflake/modules/test/quadbin/QUADBIN_TOPARENT.test.js b/clouds/snowflake/modules/test/quadbin/QUADBIN_TOPARENT.test.js index d7b173ff9..31930e4e9 100644 --- a/clouds/snowflake/modules/test/quadbin/QUADBIN_TOPARENT.test.js +++ b/clouds/snowflake/modules/test/quadbin/QUADBIN_TOPARENT.test.js @@ -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); }); \ No newline at end of file diff --git a/clouds/snowflake/modules/test/quadbin/fixtures/coords_sample.sql b/clouds/snowflake/modules/test/quadbin/fixtures/coords_sample.sql new file mode 100644 index 000000000..ccd22a555 --- /dev/null +++ b/clouds/snowflake/modules/test/quadbin/fixtures/coords_sample.sql @@ -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 \ No newline at end of file diff --git a/clouds/snowflake/modules/test/quadbin/global/setup.js b/clouds/snowflake/modules/test/quadbin/global/setup.js new file mode 100644 index 000000000..e8e15a20b --- /dev/null +++ b/clouds/snowflake/modules/test/quadbin/global/setup.js @@ -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; \ No newline at end of file diff --git a/clouds/snowflake/modules/test/quadbin/global/teardown.js b/clouds/snowflake/modules/test/quadbin/global/teardown.js new file mode 100644 index 000000000..03bb1e78a --- /dev/null +++ b/clouds/snowflake/modules/test/quadbin/global/teardown.js @@ -0,0 +1,9 @@ +const { deleteTable } = require('../../../../common/test-utils'); + +async function deleteTables () { + await Promise.all([ + deleteTable('coords_sample') + ]); +} + +module.exports = deleteTables; \ No newline at end of file