I came through lately following line of code while analyzing 3rd party data script.
CREATE OR REPLACE PROCEDURE WH.SCHEMA.PROCEDURE_NAME(DATE_OF_LOAD STRING) --input which will be binded later
RETURNS STRING
LANGUAGE javascript
AS $$
var drop_table = `drop table if exists TABLE_NAME;`;
var stmt_drop_table = snowflake.createStatement( {sqlText: drop_table} );
var incremental_data =
`CREATE TABLE AS <many arguments>
WHERE P.CZAS_MODYF_SF >= :1 --this is where biding of DATE_OF_LOAD occurs)
SELECT *, HASH(*) HASH FROM (
SELECT <arguments>
FROM CTE) q; `;
var stmt_incremental_data = snowflake.createStatement( {sqlText: incremental_data,binds: [DATE_OF_LOAD ].map(function(x){return x === undefined ? null : x}) } );
try {
stmt_drop_table.execute();
stmt_incremental_data.execute();
rtr = "Success!";
return rtr;
}
catch (err) {
return "Failed: " + err;
}
$$
;
Entire challenge I have is with:
var stmt_incremental_data = snowflake.createStatement( {sqlText: incremental_data,binds: [DATE_OF_LOAD ].map(function(x){return x === undefined ? null : x}) } )
.
object
.method
part is clear. Same for binds
. Code after binds
is my issue here.
Another topic: Snowflake interpretes method's parameters as a JSON. Does it mean that bind
value can be extended by assigning JS code?
I'll be greatly thankful for help and explanation.
The reason I did not post this as an answer initially is that JavaScript is not my area of expertise. I did dabble in it for a few months.
But in order to understand what is going on with:
[DATE_OF_LOAD ].map(function(x){return x === undefined ? null : x})
You need to break it down:
x === undefined ? null : x
This is called an elvis operator and is the equivalent of :
if (x === undefined)
{
return null
} else {
return x
}
Now that we know what the function does, we need to understand the map method. But in short it creates a new array with the results of calling a provided function on every element in the calling array.
So the simple answer copied from my comment is: if DATE_OF_LOAD is undefined it will replace it with null, otherwise it will use whatever value is stored in DATE_OF_LOAD. That is because SQL does not know how to handle undefined. But here is the reasoning for my answer.