Skip to content

Instantly share code, notes, and snippets.

View orellabac's full-sized avatar

Orellabac orellabac

View GitHub Profile
@orellabac
orellabac / connected_component.sql
Created July 16, 2024 20:22
snowpark python connected_component with networkx
create or replace function connected_component(id1 varchar, id2 varchar)
returns table(identifier varchar, gr_members variant)
language python
runtime_version = '3.11'
packages = ('networkx')
handler = 'graph'
as
$$
import networkx as nx
@orellabac
orellabac / EXECUTE_SCHEDULED.sql
Created July 15, 2024 17:01
Allows the execute of simple sql statements or tasks X minutes from now
CREATE OR REPLACE PROCEDURE EXECUTE_SCHEDULED(COMMAND VARCHAR, IN_NEXT_MINUTES INTEGER) RETURNS STRING
LANGUAGE SQL
AS
$$
BEGIN
LET TASK_NAME VARCHAR := 'SCHEDULED_COMMAND_' || UUID_STRING();
LET CREATE_TASK_COMMAND VARCHAR := 'CREATE TASK "' || :TASK_NAME ||
'" SCHEDULE = \'' || :IN_NEXT_MINUTES || ' MINUTES\' USER_TASK_MANAGED_INITIAL_WAREHOUSE_SIZE = ''XSMALL'' AS ' ||
' BEGIN \n' ||
COMMAND || '; DROP TASK "' || :TASK_NAME || '"; \n' ||
@orellabac
orellabac / sma-assistant-prompts.yaml
Created July 15, 2024 03:54
Example configuration of custom prompts for the SMA Assistant
prompts:
- name: "DataFrameReader.load"
prompt: |
Explain this tag:
------ TAG
@@firstline
and how it affects the following line of code:
------
**CODE**
@orellabac
orellabac / snow_tasks.py
Created July 11, 2024 01:15
A script to build tasks declaratively from a yaml in Snowflake
from snowflake.core import Root
from snowflake.core._common import CreateMode
from snowflake.core.task import Cron
from snowflake.core.task.dagv1 import DAG, DAGTask, DAGOperation, DAGTaskBranch
from snowflake.snowpark import Session
from snowflake.core import Root
from snowflake.snowpark import Session
import yaml
@orellabac
orellabac / execute_immediate_py.ipynb
Created July 10, 2024 21:00
Example of a function to run arbitrary scripts in snowpark from a notebook
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@orellabac
orellabac / proc_logged_to_stage.sql
Created July 8, 2024 13:31
Example Snowpark Procedure where we trap errors and save them into a file in an stage
create or replace procedure proc_logged_to_stage(arg1 string, arg2 string)
returns string
language python
runtime_version = 3.11
packages =('snowflake-snowpark-python')
handler = 'logged_main'
as
$$
from snowflake.snowpark import Session
from snowflake.snowpark.functions import col
@orellabac
orellabac / JSONGenie.py
Last active July 1, 2024 15:02
Streamlit Tool to help quickly build queries to flatten JSON into tables a.k.a JSONGenie
# Import python packages sqlparse snowflake-snowpark-python streamlit
# For this streamlit to work, I am assuming you have
# a database called JSON_GENIE_DB, and stage called JSON_GENIE_DB.PUBLIC.SAMPLES
# and a json file format called JSON_GENIE_DB.PUBLIC.JSON_GENIE_FILE_FORMAT.
# CREATE OR REPLACE FILE FORMAT JSON_GENIE_FILE_FORMAT
# TYPE = JSON
# NULL_IF = ()
#;
# The only thing that you need is to upload some sample files into the JSON_GENIE_DB.PUBLIC.SAMPLES stage.
@orellabac
orellabac / CUSTOMER_RAW.sql
Created June 30, 2024 02:45
SNOWPIPE WITH AUTOINGEST
CREATE STORAGE INTEGRATION s3_json_ingestion_int
TYPE = EXTERNAL_STAGE
STORAGE_PROVIDER = 'S3'
ENABLED = TRUE
STORAGE_AWS_ROLE_ARN = 'arn:aws:iam::00000000000:role/s3-json-ingestion-role'
STORAGE_ALLOWED_LOCATIONS = ('s3://s3-json-ingestion/customers');
CREATE STAGE CUSTOMERS
URL = 's3://s3-json-ingestion/customers'
STORAGE_INTEGRATION = S3_JSON_INGESTION_INT
@orellabac
orellabac / customers1.json
Created June 30, 2024 00:06
Example of nested JSON
[
{
"customer_id": 3,
"name": "Alice Johnson",
"contact": {
"email": "alice@example.com",
"phone": "555-1234"
},
"address": {
"street": "123 Elm Street",
@orellabac
orellabac / initialize_snowpark_python.ipynb
Last active June 26, 2024 03:23
Sample Notebook that can be used to use Snowpark In a Databricks Notebook
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.