Is a Snowflake function able to return a query's results?
In my particular case I'm trying to save data from a table into an array within the function so I can run a series of checks.
create or replace function GeoLocator ("STRING" VARCHAR(16777216) )
RETURNS VARCHAR(16777216)
LANGUAGE JAVASCRIPT
AS $$
//This function will find the location of a transaction.
//PATTERN 1 ARRAY -- THESE WILL BE THE MOST COMMON LOCATIONS
var pattern_1_most_common = [];
//Location Data
var rs_pattern_1_sql = snowflake.execute( { sqlText:
`
SELECT LOCATION
FROM A_TABLE_THAT_HAS_ALL_LOCATIONS
WHERE "STATE" = 'California'
`} );
//Load location pattern values into Pattern 1 Array i.e. "San Francisco, CA","Walnut Creek, CA", etc...
while (rs_pattern_1_sql.next()) {
pattern_1_most_common.push(rs_pattern_1_sql.getColumnValue(1))
};
// THERE WILL BE LOGIC HERE TO LOOP THROUGH ALL THE LOCATIONS AND CHECK IF THERE IS A MATCH
// FOR NOW, FOR BREVITY LETS JUST RETURN PATTERN_1_MOST_COMMON
return pattern_1_most_common
$$
But when I run
select GEOLOCATOR('text here') I get the following error:
JavaScript execution error: Uncaught ReferenceError: snowflake is not defined in GEOLOCATOR at 'var rs_pattern_1_sql = snowflake.execute( { sqlText:' position 24 stackstrace:
------------------------- Update
Concretely, I'm trying to take the following code which is currently a procedure, because I was not able to load data from a table into a function using something like snowflake.execute.
create or replace procedure GeoLocator ("STRING_PARAM" VARCHAR(16777216) )
RETURNS VARCHAR(16777216)
LANGUAGE JAVASCRIPT
AS $$
//This function will find the location of a transaction.
//Creating STRING_PASS_THRU so that we can pass the functions parameter into a Javascript variable
var STRING_PARAM_PASS_THRU_SQL = snowflake.execute( { sqlText: ` select '${STRING_PARAM}' as letsgo ` } );
var STRING_PARAM_PASS_THRU_LIST = []
while (STRING_PARAM_PASS_THRU_SQL.next()) {
STRING_PARAM_PASS_THRU_LIST.push(STRING_PARAM_PASS_THRU_SQL.getColumnValue(1))
};
//PATTERN 1 ARRAY -- THESE WILL BE THE MOST COMMON LOCATIONS
var pattern_1_most_common = [];
//Location Data --- ** This is what I cant figure out how to do in a Snowflake function. **
var rs_pattern_1_sql = snowflake.execute( { sqlText:
`
SELECT 'San Francisco' as LOCATION
UNION
SELECT 'Oakland' as LOCATION
`} );
//Load location pattern values into Pattern 1 Array i.e. "San Francisco, CA","Walnut Creek, CA", etc...
while (rs_pattern_1_sql.next()) {
pattern_1_most_common.push(rs_pattern_1_sql.getColumnValue(1))
};
//Loop through locations see if theres a match
for (var i=0; i<pattern_1_most_common.length; i++) {
if (STRING_PARAM_PASS_THRU_LIST[0].match(pattern_1_most_common[i]) !== null){
return STRING_PARAM_PASS_THRU_LIST[0].match(pattern_1_most_common[i]);
}
else {
continue;
} ;
};
$$;
So running the above procedure
CALL GeoLocator('Snowflake - Downtown Oakland') would yield 'Oakland'
However I cannot do something like
Select
call geolocator(COLUMN_NAMEHERE) as Location
from TABLE
And this is why I'm trying to convert the geolocator to a function rather than a procedure, but it seems as though (and i might be wrong here) Snowflake functions are missing a lot of functionality...such as loading data from a table using snowflake.execute into a variable so I can loop through that said variable.