Skip to content

Commit b4f57b5

Browse files
authored
Snowflake custom query block
1 parent 8189132 commit b4f57b5

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Snowflake Custom Query
2+
3+
Executes a SQL query on a Snowflake database
4+
5+
## Language
6+
Python
7+
8+
## Dependencies
9+
snowflake-connector-python
10+
11+
## Source
12+
[script.py](https://github.com/visokio/omniscope-custom-blocks/blob/master/Connectors/Snowflake-custom-query/script.py)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{
2+
"@visokiotype": "CustomBlockSchema.CustomBlockManifest",
3+
"name": "Snowflake Custom Query",
4+
"scriptFilename": "script.py",
5+
"language": "PYTHON",
6+
"executableVersion": null,
7+
"minVersions": [
8+
null
9+
],
10+
"optionsVersion": 1,
11+
"apiVersion": "VERSION_0",
12+
"isResourceIntensiveScript": false,
13+
"showPartitioning": false,
14+
"icon": null,
15+
"description": "Executes a SQL query on a Snowflake database.",
16+
"category": "Inputs",
17+
"subcategory": "Databases",
18+
"tags": [
19+
"snowflake",
20+
"database",
21+
"sql"
22+
],
23+
"introductoryText": "# Snowflake Custom SQL Executor\n\nUse this block to connect and execute SQL queries on a Snowflake database. Provide the necessary connection details and SQL statement.\n\nThe data is retrieved in full (in memory). For better performance please user the core **Database Input block* (select *Snowflake* in the connection options).",
24+
"dependencies": "snowflake-connector-python",
25+
"options": [
26+
{
27+
"name": "account",
28+
"title": "Snowflake Account",
29+
"description": "The Snowflake account identifier (e.g. 'kkpgiqe-sf91461').",
30+
"groupTitle": "Connection",
31+
"width": "ONE",
32+
"@visokiotype": "CustomBlockSchema.TextCustomBlockPublicOption",
33+
"mandatory": true,
34+
"defaultValue": null
35+
},
36+
{
37+
"name": "user",
38+
"title": "Username",
39+
"description": "The username for the Snowflake connection.",
40+
"groupTitle": "Connection",
41+
"width": "ONE",
42+
"@visokiotype": "CustomBlockSchema.TextCustomBlockPublicOption",
43+
"mandatory": true,
44+
"defaultValue": null
45+
},
46+
{
47+
"name": "password",
48+
"title": "Password",
49+
"description": "The password for the Snowflake user.",
50+
"groupTitle": "Connection",
51+
"width": "ONE",
52+
"@visokiotype": "CustomBlockSchema.PasswordCustomBlockPublicOption",
53+
"mandatory": true
54+
},
55+
{
56+
"name": "warehouse",
57+
"title": "Warehouse",
58+
"description": "The Snowflake warehouse to use. E.g. COMPUTE_WH",
59+
"groupTitle": "Connection",
60+
"width": "ONE",
61+
"@visokiotype": "CustomBlockSchema.TextCustomBlockPublicOption",
62+
"mandatory": true,
63+
"defaultValue": null
64+
},
65+
{
66+
"name": "database",
67+
"title": "Database",
68+
"description": "The Snowflake database name. E.g. SNOWFLAKE_SAMPLE_DATA",
69+
"groupTitle": "Connection",
70+
"width": "ONE",
71+
"@visokiotype": "CustomBlockSchema.TextCustomBlockPublicOption",
72+
"mandatory": true,
73+
"defaultValue": null
74+
},
75+
{
76+
"name": "custom_sql",
77+
"title": "Custom SQL Query",
78+
"description": "Custom SQL statement to execute. Use schema.table syntax",
79+
"groupTitle": "Query",
80+
"width": "THREE",
81+
"@visokiotype": "CustomBlockSchema.TextCustomBlockPublicOption",
82+
"mandatory": true,
83+
"defaultValue": null
84+
}
85+
],
86+
"blockOutputs": [
87+
{
88+
"@visokiotype": "CustomBlockSchema.BlockOutputPublicOption",
89+
"id": "Output Data",
90+
"label": "Query Results",
91+
"displayName": "Query Results",
92+
"tooltip": null
93+
}
94+
],
95+
"docker": {
96+
"@visokiotype": "CustomBlockSchema.DockerCustomBlockPublicOption",
97+
"customBaseImage": null,
98+
"useCustomBaseImage": false,
99+
"customSystemLibraries": null,
100+
"installVisokioRepLibraries": false
101+
},
102+
"designLock": false,
103+
"apiMode": "BATCH"
104+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import pandas as pd
2+
from omniscope.api import OmniscopeApi
3+
import snowflake.connector
4+
5+
# Initialize Omniscope API
6+
omniscope_api = OmniscopeApi()
7+
8+
# Retrieve options from the block configuration
9+
account = omniscope_api.get_option('account')
10+
user = omniscope_api.get_option('user')
11+
password = omniscope_api.get_option('password')
12+
warehouse = omniscope_api.get_option('warehouse')
13+
database = omniscope_api.get_option('database')
14+
custom_sql = omniscope_api.get_option('custom_sql')
15+
16+
# Establish a connection to Snowflake
17+
def connect_to_snowflake():
18+
return snowflake.connector.connect(
19+
user=user,
20+
password=password,
21+
account=account,
22+
warehouse=warehouse,
23+
database=database
24+
)
25+
26+
# Fetch data using the SQL query
27+
def fetch_data_from_snowflake(sql_query):
28+
try:
29+
# Connect to Snowflake
30+
conn = connect_to_snowflake()
31+
32+
# Execute the query and fetch data into a DataFrame
33+
df = pd.read_sql(sql_query, conn)
34+
35+
# Close the connection
36+
conn.close()
37+
38+
return df
39+
except Exception as e:
40+
omniscope_api.error(f"Error executing SQL query: {str(e)}")
41+
return pd.DataFrame() # Return an empty dataframe on error
42+
43+
44+
# Fetch the data
45+
output_data = fetch_data_from_snowflake(custom_sql)
46+
47+
# Write the output records to the first output if there is data
48+
if output_data is not None and not output_data.empty:
49+
omniscope_api.write_output_records(output_data, output_number=0)
50+
51+
# Close the API after processing is complete
52+
omniscope_api.close()

0 commit comments

Comments
 (0)