At the last Snowflake Summit , the Dynamic Table was one of the big announcements. This table type adds another option to the list of Streams and Materialized Views.
We’d like you to use our startup function to hit this challenge at speed! The function creates a table if doesn’t already exist and fills it with data if it does exist! Perfect for our challenge (and a decent function to get ‘inspired by’)
Startup Function
CREATE OR REPLACE PROCEDURE check_and_generate_data(tableName STRING)
RETURNS STRING
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS
$$
try {
var tableName = TABLENAME;
var command1 = `SELECT count(*) as count FROM information_schema.tables WHERE table_schema = CURRENT_SCHEMA() AND table_name = '${tableName.toUpperCase()}'`;
var statement1 = snowflake.createStatement({sqlText: command1});
var result_set1 = statement1.execute();
result_set1.next();
var count = result_set1.getColumnValue('COUNT');
if (count == 0) {
var command2 = `CREATE TABLE ${tableName} (payload VARIANT, ingested_at TIMESTAMP_NTZ default CURRENT_TIMESTAMP())`;
var statement2 = snowflake.createStatement({sqlText: command2});
statement2.execute();
return `Table ${tableName} has been created.`;
} else {
for(var i=0; i<40; i++) {
var jsonObject = {
"id": i,
"name": "Name_" + i,
"address": "Address_" + i,
"email": "email_" + i + "@example.com",
"transactionValue": Math.floor(Math.random() * 10000) + 1
};
var jsonString = JSON.stringify(jsonObject);
var command3 = `INSERT INTO ${tableName} (payload) SELECT PARSE_JSON(column1) FROM VALUES ('${jsonString}')`;
var statement3 = snowflake.createStatement({sqlText: command3});
statement3.execute();
}
return `40 records have been inserted into the ${tableName} table.`;
}
} catch (err) {
return "Failed: " + err;
}
$$;
The challenge for this week is to use a Dynamic Table to create the following results:
3 responses to “Week 52 – Intermediate”
-
I’m really excited to start using this new functionality in real-world scenarios with customers!
- Solution URL – https://github.com/ChrisHastieIW/Frosty-Friday
-
Super feature!
- Solution URL – https://github.com/canonicalized/FrostyFriday/blob/main/WEEK52.sql
-
Looking forward to dbt supporting this new materialization (should be around the corner!), meanwhile, a quick and dirty one.
main bits here (used as a pre-hook in the model) https://github.com/dsmdavid/frostyfridays-sf/blob/main/macros/ch_52_create_dynamic_table.sql
Leave a Reply
You must be logged in to post a comment.