query_text
The query_text
variable can be used in SQL Pre-hook only and is the text of the query at the time we arrive at the SQL Pre-hook.
It is not necessarily what user initially submitted.
For example, if REPLACE_TABLES fired before the SQL Pre-hook that uses query_text
variable, the query_text
variable will
have text with replaced table names.
{{query_text}}
Create flow with the single SQL Pre-hook that uses query_text variable:
nori#COMPUTE_WH@(no database).(no schema)>CREATE OR REPLACE SUNDECK FLOW MY_FLOW
PRE HOOK TRACK_ELAPSED_TIME
ALWAYS
SQL {
'sqlText': 'insert
into
nori_db.public.elapsed_time(query_text, avg_time)
select
query_text,
avg(total_elapsed_time)
from
snowflake.account_usage.query_history
where
query_text = \'{{query_text}}\'
group by query_text',
'errorBehavior' : 'BYPASS'
}
;
+------------------------------------------------------------------------------+
| status |
|------------------------------------------------------------------------------|
| Flow MY_FLOW created. Use ‘DESCRIBE SUNDECK FLOW "MY_FLOW"’ for flow details |
+------------------------------------------------------------------------------+
1 Row(s) produced.
Connect to it and run simple query that was preivously executed on this system:
nori#COMPUTE_WH@(no database).(no schema)>select count(*) from snowflake_sample_data.tpch_sf1.nation;
+----------+
| COUNT(*) |
|----------|
| 25 |
+----------+
1 Row(s) produced.
nori#COMPUTE_WH@(no database).(no schema)>select * from nori_db.public.elapsed_time;
+-------------------------------------------------------------+----------+
| QUERY_TEXT | AVG_TIME |
|-------------------------------------------------------------+----------|
| select count(*) from snowflake_sample_data.tpch_sf1.nation; | 81 |
+-------------------------------------------------------------+----------+
1 Row(s) produced.
Please note, that table nori_db.public.elapsed_time
was created prior to query execution.