Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #19635: Snowflake allow custom account_usage schema #19678

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,18 @@ def test_connection(
engine_wrapper=engine_wrapper,
),
"GetQueries": partial(
test_query, statement=SNOWFLAKE_TEST_GET_QUERIES, engine=engine
test_query,
statement=SNOWFLAKE_TEST_GET_QUERIES.format(
account_usage=service_connection.accountUsageSchema
),
engine=engine,
),
"GetTags": partial(
test_query, statement=SNOWFLAKE_TEST_FETCH_TAG, engine=engine
test_query,
statement=SNOWFLAKE_TEST_FETCH_TAG.format(
account_usage=service_connection.accountUsageSchema
),
engine=engine,
),
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def get_stored_procedure_queries_dict(self) -> Dict[str, List[QueryByProcedure]]
start, _ = get_start_and_end(self.source_config.queryLogDuration)
query = self.stored_procedure_query.format(
start_date=start,
account_usage=self.service_connection.accountUsageSchema,
)
queries_dict = self.procedure_queries_dict(
query=query,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ def yield_tag(
SNOWFLAKE_FETCH_ALL_TAGS.format(
database_name=self.context.get().database,
schema_name=schema_name,
account_usage=self.service_connection.accountUsageSchema,
)
)

Expand All @@ -431,6 +432,7 @@ def yield_tag(
SNOWFLAKE_FETCH_ALL_TAGS.format(
database_name=f'"{self.context.get().database}"',
schema_name=f'"{self.context.get().database_schema}"',
account_usage=self.service_connection.accountUsageSchema,
)
)
except Exception as inner_exc:
Expand Down Expand Up @@ -635,6 +637,7 @@ def _get_stored_procedures_internal(
query.format(
database_name=self.context.get().database,
schema_name=self.context.get().database_schema,
account_usage=self.service_connection.accountUsageSchema,
)
).all()
for row in results:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
start_time "start_time",
end_time "end_time",
total_elapsed_time "duration"
from snowflake.account_usage.query_history
from {account_usage}.query_history
WHERE query_text NOT LIKE '/* {{"app": "OpenMetadata", %%}} */%%'
AND query_text NOT LIKE '/* {{"app": "dbt", %%}} */%%'
AND start_time between to_timestamp_ltz('{start_time}') and to_timestamp_ltz('{end_time}')
Expand All @@ -39,7 +39,7 @@
SNOWFLAKE_FETCH_ALL_TAGS = textwrap.dedent(
"""
select TAG_NAME, TAG_VALUE, OBJECT_DATABASE, OBJECT_SCHEMA, OBJECT_NAME, COLUMN_NAME
from snowflake.account_usage.tag_references
from {account_usage}.tag_references
where OBJECT_DATABASE = '{database_name}'
and OBJECT_SCHEMA = '{schema_name}'
"""
Expand Down Expand Up @@ -234,11 +234,11 @@
"""

SNOWFLAKE_TEST_FETCH_TAG = """
select TAG_NAME from snowflake.account_usage.tag_references limit 1
select TAG_NAME from {account_usage}.tag_references limit 1
"""

SNOWFLAKE_TEST_GET_QUERIES = """
SELECT query_text from snowflake.account_usage.query_history limit 1
SELECT query_text from {account_usage}.query_history limit 1
"""

SNOWFLAKE_TEST_GET_TABLES = """
Expand Down Expand Up @@ -296,10 +296,10 @@
ARGUMENT_SIGNATURE AS signature,
COMMENT as comment,
'StoredProcedure' as procedure_type
FROM SNOWFLAKE.ACCOUNT_USAGE.PROCEDURES
FROM {account_usage}.PROCEDURES
WHERE PROCEDURE_CATALOG = '{database_name}'
AND PROCEDURE_SCHEMA = '{schema_name}'
AND DELETED IS NOT NULL
AND DELETED IS NULL
"""
)

Expand All @@ -313,10 +313,10 @@
ARGUMENT_SIGNATURE AS signature,
COMMENT as comment,
'UDF' as procedure_type
FROM SNOWFLAKE.ACCOUNT_USAGE.FUNCTIONS
FROM {account_usage}.FUNCTIONS
WHERE FUNCTION_CATALOG = '{database_name}'
AND FUNCTION_SCHEMA = '{schema_name}'
AND DELETED IS NOT NULL
AND DELETED IS NULL
"""
)

Expand All @@ -336,7 +336,7 @@
SESSION_ID,
START_TIME,
END_TIME
FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY SP
FROM {account_usage}.QUERY_HISTORY SP
WHERE QUERY_TYPE = 'CALL'
AND START_TIME >= '{start_date}'
AND QUERY_TEXT <> ''
Expand All @@ -353,7 +353,7 @@
USER_NAME,
SCHEMA_NAME,
DATABASE_NAME
FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY SP
FROM {account_usage}.QUERY_HISTORY SP
WHERE QUERY_TYPE <> 'CALL'
AND QUERY_TEXT NOT LIKE '/* {{"app": "OpenMetadata", %%}} */%%'
AND QUERY_TEXT NOT LIKE '/* {{"app": "dbt", %%}} */%%'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def get_sql_statement(self, start_time: datetime, end_time: datetime) -> str:
end_time=end_time,
result_limit=self.config.sourceConfig.config.resultLimit,
filters=self.get_filters(),
account_usage=self.service_connection.accountUsageSchema,
)

def check_life_cycle_query(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ You can find more information about the `account_usage` schema [here](https://do
- **Include Temporary and Transient Tables**:
Optional configuration for ingestion of `TRANSIENT` and `TEMPORARY` tables, By default, it will skip the `TRANSIENT` and `TEMPORARY` tables.
- **Client Session Keep Alive**: Optional Configuration to keep the session active in case the ingestion job runs for longer duration.
- **Account Usage Schema Name**: Full name of account usage schema, used in case your used do not have direct access to `SNOWFLAKE.ACCOUNT_USAGE` schema. In such case you can replicate tables `QUERY_HISTORY`, `TAG_REFERENCES`, `PROCEDURES`, `FUNCTIONS` to a custom schema let's say `CUSTOM_DB.CUSTOM_SCHEMA` and provide the same name in this field.

When using this field make sure you have all these tables available within your custom schema `QUERY_HISTORY`, `TAG_REFERENCES`, `PROCEDURES`, `FUNCTIONS`.

{% partial file="/v1.6/connectors/database/advanced-configuration.md" /%}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ This is a sample config for Snowflake:

{% /codeInfo %}

{% codeInfo srNumber=40 %}

**accountUsageSchema**: Full name of account usage schema, used in case your used do not have direct access to `SNOWFLAKE.ACCOUNT_USAGE` schema. In such case you can replicate tables `QUERY_HISTORY`, `TAG_REFERENCES`, `PROCEDURES`, `FUNCTIONS` to a custom schema let's say `CUSTOM_DB.CUSTOM_SCHEMA` and provide the same name in this field.

When using this field make sure you have all these tables available within your custom schema `QUERY_HISTORY`, `TAG_REFERENCES`, `PROCEDURES`, `FUNCTIONS`.

{% /codeInfo %}

{% codeInfo srNumber=6 %}

**includeTransientTables**: Optional configuration for ingestion of TRANSIENT and TEMPORARY tables, By default, it will skip the TRANSIENT and TEMPORARY tables.
Expand Down Expand Up @@ -231,6 +239,9 @@ source:
```yaml {% srNumber=5 %}
# database: <database>
```
```yaml {% srNumber=40 %}
# accountUsageSchema: SNOWFLAKE.ACCOUNT_USAGE
```
```yaml {% srNumber=6 %}
includeTransientTables: false
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ You can find more information about the `account_usage` schema [here](https://do
- **Include Temporary and Transient Tables**:
Optional configuration for ingestion of `TRANSIENT` and `TEMPORARY` tables, By default, it will skip the `TRANSIENT` and `TEMPORARY` tables.
- **Client Session Keep Alive**: Optional Configuration to keep the session active in case the ingestion job runs for longer duration.
- **Account Usage Schema Name**: Full name of account usage schema, used in case your used do not have direct access to `SNOWFLAKE.ACCOUNT_USAGE` schema. In such case you can replicate tables `QUERY_HISTORY`, `TAG_REFERENCES`, `PROCEDURES`, `FUNCTIONS` to a custom schema let's say `CUSTOM_DB.CUSTOM_SCHEMA` and provide the same name in this field.

When using this field make sure you have all these tables available within your custom schema `QUERY_HISTORY`, `TAG_REFERENCES`, `PROCEDURES`, `FUNCTIONS`.

{% partial file="/v1.7/connectors/database/advanced-configuration.md" /%}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ This is a sample config for Snowflake:

{% /codeInfo %}

{% codeInfo srNumber=40 %}

**accountUsageSchema**: Full name of account usage schema, used in case your used do not have direct access to `SNOWFLAKE.ACCOUNT_USAGE` schema. In such case you can replicate tables `QUERY_HISTORY`, `TAG_REFERENCES`, `PROCEDURES`, `FUNCTIONS` to a custom schema let's say `CUSTOM_DB.CUSTOM_SCHEMA` and provide the same name in this field.

When using this field make sure you have all these tables available within your custom schema `QUERY_HISTORY`, `TAG_REFERENCES`, `PROCEDURES`, `FUNCTIONS`.

{% /codeInfo %}

{% codeInfo srNumber=6 %}

**includeTransientTables**: Optional configuration for ingestion of TRANSIENT and TEMPORARY tables, By default, it will skip the TRANSIENT and TEMPORARY tables.
Expand Down Expand Up @@ -231,6 +239,9 @@ source:
```yaml {% srNumber=5 %}
# database: <database>
```
```yaml {% srNumber=40 %}
# accountUsageSchema: SNOWFLAKE.ACCOUNT_USAGE
```
```yaml {% srNumber=6 %}
includeTransientTables: false
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
"description": "Session query tag used to monitor usage on snowflake. To use a query tag snowflake user should have enough privileges to alter the session.",
"type": "string"
},
"accountUsageSchema":{
"title": "Account Usage Schema Name",
"description": "Full name of the schema where the account usage data is stored.",
"type": "string",
"default": "SNOWFLAKE.ACCOUNT_USAGE"
},
"privateKey": {
"title": "Private Key",
"description": "Connection to Snowflake instance via Private Key",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export interface SnowflakeConnection {
* account is xyz1234.us-east-1.gcp
*/
account: string;
/**
* Full name of the schema where the account usage data is stored.
*/
accountUsageSchema?: string;
/**
* Optional configuration for ingestion to keep the client session active in case the
* ingestion process runs for longer durations.
Expand Down
Loading