snowflake-cli-labs 3.0.0rc4__py3-none-any.whl → 3.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- README.md +21 -0
- {snowflake_cli_labs-3.0.0rc4.dist-info → snowflake_cli_labs-3.0.1.dist-info}/METADATA +6 -96
- snowflake_cli_labs-3.0.1.dist-info/RECORD +5 -0
- snowflake/cli/__about__.py +0 -17
- snowflake/cli/__init__.py +0 -13
- snowflake/cli/_app/__init__.py +0 -22
- snowflake/cli/_app/__main__.py +0 -31
- snowflake/cli/_app/api_impl/__init__.py +0 -13
- snowflake/cli/_app/api_impl/plugin/__init__.py +0 -13
- snowflake/cli/_app/api_impl/plugin/plugin_config_provider_impl.py +0 -66
- snowflake/cli/_app/cli_app.py +0 -252
- snowflake/cli/_app/commands_registration/__init__.py +0 -33
- snowflake/cli/_app/commands_registration/builtin_plugins.py +0 -50
- snowflake/cli/_app/commands_registration/command_plugins_loader.py +0 -169
- snowflake/cli/_app/commands_registration/commands_registration_with_callbacks.py +0 -105
- snowflake/cli/_app/commands_registration/exception_logging.py +0 -26
- snowflake/cli/_app/commands_registration/threadsafe.py +0 -48
- snowflake/cli/_app/commands_registration/typer_registration.py +0 -153
- snowflake/cli/_app/constants.py +0 -19
- snowflake/cli/_app/dev/__init__.py +0 -13
- snowflake/cli/_app/dev/commands_structure.py +0 -48
- snowflake/cli/_app/dev/docs/__init__.py +0 -13
- snowflake/cli/_app/dev/docs/commands_docs_generator.py +0 -118
- snowflake/cli/_app/dev/docs/generator.py +0 -35
- snowflake/cli/_app/dev/docs/project_definition_docs_generator.py +0 -58
- snowflake/cli/_app/dev/docs/project_definition_generate_json_schema.py +0 -227
- snowflake/cli/_app/dev/docs/template_utils.py +0 -23
- snowflake/cli/_app/dev/docs/templates/definition_description.rst.jinja2 +0 -38
- snowflake/cli/_app/dev/docs/templates/overview.rst.jinja2 +0 -9
- snowflake/cli/_app/dev/docs/templates/usage.rst.jinja2 +0 -67
- snowflake/cli/_app/dev/pycharm_remote_debug.py +0 -46
- snowflake/cli/_app/loggers.py +0 -199
- snowflake/cli/_app/main_typer.py +0 -62
- snowflake/cli/_app/printing.py +0 -181
- snowflake/cli/_app/secret.py +0 -9
- snowflake/cli/_app/snow_connector.py +0 -309
- snowflake/cli/_app/telemetry.py +0 -220
- snowflake/cli/_app/version_check.py +0 -74
- snowflake/cli/_plugins/__init__.py +0 -13
- snowflake/cli/_plugins/connection/__init__.py +0 -13
- snowflake/cli/_plugins/connection/commands.py +0 -353
- snowflake/cli/_plugins/connection/plugin_spec.py +0 -30
- snowflake/cli/_plugins/connection/util.py +0 -195
- snowflake/cli/_plugins/cortex/__init__.py +0 -13
- snowflake/cli/_plugins/cortex/commands.py +0 -332
- snowflake/cli/_plugins/cortex/constants.py +0 -17
- snowflake/cli/_plugins/cortex/manager.py +0 -189
- snowflake/cli/_plugins/cortex/plugin_spec.py +0 -30
- snowflake/cli/_plugins/cortex/types.py +0 -22
- snowflake/cli/_plugins/git/__init__.py +0 -13
- snowflake/cli/_plugins/git/commands.py +0 -358
- snowflake/cli/_plugins/git/manager.py +0 -151
- snowflake/cli/_plugins/git/plugin_spec.py +0 -30
- snowflake/cli/_plugins/helpers/__init__.py +0 -13
- snowflake/cli/_plugins/helpers/commands.py +0 -61
- snowflake/cli/_plugins/helpers/plugin_spec.py +0 -30
- snowflake/cli/_plugins/init/__init__.py +0 -13
- snowflake/cli/_plugins/init/commands.py +0 -248
- snowflake/cli/_plugins/init/plugin_spec.py +0 -30
- snowflake/cli/_plugins/nativeapp/__init__.py +0 -13
- snowflake/cli/_plugins/nativeapp/artifacts.py +0 -757
- snowflake/cli/_plugins/nativeapp/bundle_context.py +0 -31
- snowflake/cli/_plugins/nativeapp/codegen/__init__.py +0 -13
- snowflake/cli/_plugins/nativeapp/codegen/artifact_processor.py +0 -91
- snowflake/cli/_plugins/nativeapp/codegen/compiler.py +0 -149
- snowflake/cli/_plugins/nativeapp/codegen/sandbox.py +0 -306
- snowflake/cli/_plugins/nativeapp/codegen/setup/native_app_setup_processor.py +0 -249
- snowflake/cli/_plugins/nativeapp/codegen/setup/setup_driver.py.source +0 -59
- snowflake/cli/_plugins/nativeapp/codegen/snowpark/callback_source.py.jinja +0 -181
- snowflake/cli/_plugins/nativeapp/codegen/snowpark/extension_function_utils.py +0 -217
- snowflake/cli/_plugins/nativeapp/codegen/snowpark/models.py +0 -61
- snowflake/cli/_plugins/nativeapp/codegen/snowpark/python_processor.py +0 -523
- snowflake/cli/_plugins/nativeapp/codegen/templates/templates_processor.py +0 -114
- snowflake/cli/_plugins/nativeapp/commands.py +0 -559
- snowflake/cli/_plugins/nativeapp/common_flags.py +0 -44
- snowflake/cli/_plugins/nativeapp/constants.py +0 -27
- snowflake/cli/_plugins/nativeapp/entities/__init__.py +0 -0
- snowflake/cli/_plugins/nativeapp/entities/application.py +0 -878
- snowflake/cli/_plugins/nativeapp/entities/application_package.py +0 -1392
- snowflake/cli/_plugins/nativeapp/exceptions.py +0 -113
- snowflake/cli/_plugins/nativeapp/feature_flags.py +0 -24
- snowflake/cli/_plugins/nativeapp/manager.py +0 -415
- snowflake/cli/_plugins/nativeapp/plugin_spec.py +0 -30
- snowflake/cli/_plugins/nativeapp/policy.py +0 -53
- snowflake/cli/_plugins/nativeapp/project_model.py +0 -211
- snowflake/cli/_plugins/nativeapp/run_processor.py +0 -184
- snowflake/cli/_plugins/nativeapp/same_account_install_method.py +0 -70
- snowflake/cli/_plugins/nativeapp/teardown_processor.py +0 -70
- snowflake/cli/_plugins/nativeapp/utils.py +0 -98
- snowflake/cli/_plugins/nativeapp/v2_conversions/v2_to_v1_decorator.py +0 -262
- snowflake/cli/_plugins/nativeapp/version/__init__.py +0 -13
- snowflake/cli/_plugins/nativeapp/version/commands.py +0 -141
- snowflake/cli/_plugins/nativeapp/version/version_processor.py +0 -98
- snowflake/cli/_plugins/notebook/__init__.py +0 -13
- snowflake/cli/_plugins/notebook/commands.py +0 -86
- snowflake/cli/_plugins/notebook/exceptions.py +0 -20
- snowflake/cli/_plugins/notebook/manager.py +0 -71
- snowflake/cli/_plugins/notebook/plugin_spec.py +0 -30
- snowflake/cli/_plugins/notebook/types.py +0 -15
- snowflake/cli/_plugins/object/__init__.py +0 -13
- snowflake/cli/_plugins/object/command_aliases.py +0 -95
- snowflake/cli/_plugins/object/commands.py +0 -180
- snowflake/cli/_plugins/object/common.py +0 -85
- snowflake/cli/_plugins/object/manager.py +0 -118
- snowflake/cli/_plugins/object/plugin_spec.py +0 -30
- snowflake/cli/_plugins/snowpark/__init__.py +0 -13
- snowflake/cli/_plugins/snowpark/commands.py +0 -450
- snowflake/cli/_plugins/snowpark/common.py +0 -268
- snowflake/cli/_plugins/snowpark/models.py +0 -150
- snowflake/cli/_plugins/snowpark/package/__init__.py +0 -13
- snowflake/cli/_plugins/snowpark/package/anaconda_packages.py +0 -199
- snowflake/cli/_plugins/snowpark/package/commands.py +0 -195
- snowflake/cli/_plugins/snowpark/package/manager.py +0 -44
- snowflake/cli/_plugins/snowpark/package/utils.py +0 -26
- snowflake/cli/_plugins/snowpark/package_utils.py +0 -354
- snowflake/cli/_plugins/snowpark/plugin_spec.py +0 -30
- snowflake/cli/_plugins/snowpark/snowpark_entity.py +0 -29
- snowflake/cli/_plugins/snowpark/snowpark_entity_model.py +0 -173
- snowflake/cli/_plugins/snowpark/snowpark_project_paths.py +0 -109
- snowflake/cli/_plugins/snowpark/snowpark_shared.py +0 -59
- snowflake/cli/_plugins/snowpark/zipper.py +0 -89
- snowflake/cli/_plugins/spcs/__init__.py +0 -33
- snowflake/cli/_plugins/spcs/common.py +0 -99
- snowflake/cli/_plugins/spcs/compute_pool/__init__.py +0 -13
- snowflake/cli/_plugins/spcs/compute_pool/commands.py +0 -241
- snowflake/cli/_plugins/spcs/compute_pool/manager.py +0 -121
- snowflake/cli/_plugins/spcs/image_registry/__init__.py +0 -13
- snowflake/cli/_plugins/spcs/image_registry/commands.py +0 -65
- snowflake/cli/_plugins/spcs/image_registry/manager.py +0 -105
- snowflake/cli/_plugins/spcs/image_repository/__init__.py +0 -13
- snowflake/cli/_plugins/spcs/image_repository/commands.py +0 -202
- snowflake/cli/_plugins/spcs/image_repository/manager.py +0 -84
- snowflake/cli/_plugins/spcs/plugin_spec.py +0 -30
- snowflake/cli/_plugins/spcs/services/__init__.py +0 -13
- snowflake/cli/_plugins/spcs/services/commands.py +0 -345
- snowflake/cli/_plugins/spcs/services/manager.py +0 -208
- snowflake/cli/_plugins/sql/__init__.py +0 -13
- snowflake/cli/_plugins/sql/commands.py +0 -86
- snowflake/cli/_plugins/sql/manager.py +0 -92
- snowflake/cli/_plugins/sql/plugin_spec.py +0 -30
- snowflake/cli/_plugins/sql/snowsql_templating.py +0 -28
- snowflake/cli/_plugins/stage/__init__.py +0 -13
- snowflake/cli/_plugins/stage/commands.py +0 -264
- snowflake/cli/_plugins/stage/diff.py +0 -280
- snowflake/cli/_plugins/stage/manager.py +0 -582
- snowflake/cli/_plugins/stage/md5.py +0 -160
- snowflake/cli/_plugins/stage/plugin_spec.py +0 -30
- snowflake/cli/_plugins/stage/utils.py +0 -54
- snowflake/cli/_plugins/streamlit/__init__.py +0 -13
- snowflake/cli/_plugins/streamlit/commands.py +0 -195
- snowflake/cli/_plugins/streamlit/manager.py +0 -220
- snowflake/cli/_plugins/streamlit/plugin_spec.py +0 -30
- snowflake/cli/_plugins/streamlit/streamlit_entity.py +0 -12
- snowflake/cli/_plugins/streamlit/streamlit_entity_model.py +0 -66
- snowflake/cli/_plugins/workspace/__init__.py +0 -13
- snowflake/cli/_plugins/workspace/action_context.py +0 -18
- snowflake/cli/_plugins/workspace/commands.py +0 -306
- snowflake/cli/_plugins/workspace/manager.py +0 -74
- snowflake/cli/_plugins/workspace/plugin_spec.py +0 -30
- snowflake/cli/api/__init__.py +0 -48
- snowflake/cli/api/cli_global_context.py +0 -247
- snowflake/cli/api/commands/__init__.py +0 -13
- snowflake/cli/api/commands/alias.py +0 -23
- snowflake/cli/api/commands/common.py +0 -25
- snowflake/cli/api/commands/decorators.py +0 -369
- snowflake/cli/api/commands/execution_metadata.py +0 -40
- snowflake/cli/api/commands/experimental_behaviour.py +0 -18
- snowflake/cli/api/commands/flags.py +0 -561
- snowflake/cli/api/commands/overrideable_parameter.py +0 -143
- snowflake/cli/api/commands/snow_typer.py +0 -247
- snowflake/cli/api/commands/utils.py +0 -18
- snowflake/cli/api/config.py +0 -380
- snowflake/cli/api/connections.py +0 -216
- snowflake/cli/api/console/__init__.py +0 -17
- snowflake/cli/api/console/abc.py +0 -94
- snowflake/cli/api/console/console.py +0 -134
- snowflake/cli/api/console/enum.py +0 -17
- snowflake/cli/api/constants.py +0 -90
- snowflake/cli/api/entities/common.py +0 -56
- snowflake/cli/api/entities/utils.py +0 -370
- snowflake/cli/api/errno.py +0 -28
- snowflake/cli/api/exceptions.py +0 -190
- snowflake/cli/api/feature_flags.py +0 -54
- snowflake/cli/api/identifiers.py +0 -190
- snowflake/cli/api/metrics.py +0 -92
- snowflake/cli/api/output/__init__.py +0 -13
- snowflake/cli/api/output/formats.py +0 -20
- snowflake/cli/api/output/types.py +0 -118
- snowflake/cli/api/plugins/__init__.py +0 -13
- snowflake/cli/api/plugins/command/__init__.py +0 -72
- snowflake/cli/api/plugins/command/plugin_hook_specs.py +0 -21
- snowflake/cli/api/plugins/plugin_config.py +0 -32
- snowflake/cli/api/project/__init__.py +0 -13
- snowflake/cli/api/project/definition.py +0 -126
- snowflake/cli/api/project/definition_conversion.py +0 -395
- snowflake/cli/api/project/definition_manager.py +0 -145
- snowflake/cli/api/project/errors.py +0 -56
- snowflake/cli/api/project/project_verification.py +0 -23
- snowflake/cli/api/project/schemas/__init__.py +0 -13
- snowflake/cli/api/project/schemas/entities/__init__.py +0 -13
- snowflake/cli/api/project/schemas/entities/common.py +0 -153
- snowflake/cli/api/project/schemas/entities/entities.py +0 -61
- snowflake/cli/api/project/schemas/project_definition.py +0 -330
- snowflake/cli/api/project/schemas/template.py +0 -77
- snowflake/cli/api/project/schemas/updatable_model.py +0 -202
- snowflake/cli/api/project/schemas/v1/__init__.py +0 -0
- snowflake/cli/api/project/schemas/v1/identifier_model.py +0 -51
- snowflake/cli/api/project/schemas/v1/native_app/__init__.py +0 -0
- snowflake/cli/api/project/schemas/v1/native_app/application.py +0 -61
- snowflake/cli/api/project/schemas/v1/native_app/native_app.py +0 -93
- snowflake/cli/api/project/schemas/v1/native_app/package.py +0 -84
- snowflake/cli/api/project/schemas/v1/native_app/path_mapping.py +0 -65
- snowflake/cli/api/project/schemas/v1/snowpark/__init__.py +0 -0
- snowflake/cli/api/project/schemas/v1/snowpark/argument.py +0 -28
- snowflake/cli/api/project/schemas/v1/snowpark/callable.py +0 -69
- snowflake/cli/api/project/schemas/v1/snowpark/snowpark.py +0 -36
- snowflake/cli/api/project/schemas/v1/streamlit/__init__.py +0 -0
- snowflake/cli/api/project/schemas/v1/streamlit/streamlit.py +0 -47
- snowflake/cli/api/project/util.py +0 -278
- snowflake/cli/api/rendering/__init__.py +0 -13
- snowflake/cli/api/rendering/jinja.py +0 -118
- snowflake/cli/api/rendering/project_definition_templates.py +0 -43
- snowflake/cli/api/rendering/project_templates.py +0 -98
- snowflake/cli/api/rendering/sql_templates.py +0 -105
- snowflake/cli/api/rest_api.py +0 -178
- snowflake/cli/api/sanitizers.py +0 -43
- snowflake/cli/api/secure_path.py +0 -360
- snowflake/cli/api/secure_utils.py +0 -118
- snowflake/cli/api/sql_execution.py +0 -280
- snowflake/cli/api/utils/__init__.py +0 -13
- snowflake/cli/api/utils/cursor.py +0 -34
- snowflake/cli/api/utils/definition_rendering.py +0 -415
- snowflake/cli/api/utils/dict_utils.py +0 -73
- snowflake/cli/api/utils/error_handling.py +0 -23
- snowflake/cli/api/utils/graph.py +0 -97
- snowflake/cli/api/utils/models.py +0 -63
- snowflake/cli/api/utils/naming_utils.py +0 -13
- snowflake/cli/api/utils/path_utils.py +0 -36
- snowflake/cli/api/utils/templating_functions.py +0 -144
- snowflake/cli/api/utils/types.py +0 -35
- snowflake_cli_labs-3.0.0rc4.dist-info/RECORD +0 -242
- snowflake_cli_labs-3.0.0rc4.dist-info/entry_points.txt +0 -2
- {snowflake_cli_labs-3.0.0rc4.dist-info → snowflake_cli_labs-3.0.1.dist-info}/WHEEL +0 -0
- {snowflake_cli_labs-3.0.0rc4.dist-info → snowflake_cli_labs-3.0.1.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,280 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2024 Snowflake Inc.
|
|
2
|
-
#
|
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
# you may not use this file except in compliance with the License.
|
|
5
|
-
# You may obtain a copy of the License at
|
|
6
|
-
#
|
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
#
|
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
# See the License for the specific language governing permissions and
|
|
13
|
-
# limitations under the License.
|
|
14
|
-
|
|
15
|
-
from __future__ import annotations
|
|
16
|
-
|
|
17
|
-
import logging
|
|
18
|
-
from contextlib import contextmanager
|
|
19
|
-
from functools import cached_property
|
|
20
|
-
from io import StringIO
|
|
21
|
-
from textwrap import dedent
|
|
22
|
-
from typing import Iterable, Optional, Tuple
|
|
23
|
-
|
|
24
|
-
from snowflake.cli.api.cli_global_context import get_cli_context
|
|
25
|
-
from snowflake.cli.api.console import cli_console
|
|
26
|
-
from snowflake.cli.api.constants import ObjectType
|
|
27
|
-
from snowflake.cli.api.exceptions import (
|
|
28
|
-
DatabaseNotProvidedError,
|
|
29
|
-
SchemaNotProvidedError,
|
|
30
|
-
SnowflakeSQLExecutionError,
|
|
31
|
-
)
|
|
32
|
-
from snowflake.cli.api.identifiers import FQN
|
|
33
|
-
from snowflake.cli.api.project.util import (
|
|
34
|
-
identifier_to_show_like_pattern,
|
|
35
|
-
unquote_identifier,
|
|
36
|
-
)
|
|
37
|
-
from snowflake.cli.api.utils.cursor import find_first_row
|
|
38
|
-
from snowflake.connector import SnowflakeConnection
|
|
39
|
-
from snowflake.connector.cursor import DictCursor, SnowflakeCursor
|
|
40
|
-
from snowflake.connector.errors import ProgrammingError
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
class SqlExecutor:
|
|
44
|
-
def __init__(self, connection: SnowflakeConnection | None = None):
|
|
45
|
-
self._snowpark_session = None
|
|
46
|
-
self._connection = connection
|
|
47
|
-
|
|
48
|
-
@property
|
|
49
|
-
def _conn(self) -> SnowflakeConnection:
|
|
50
|
-
if self._connection:
|
|
51
|
-
return self._connection
|
|
52
|
-
return get_cli_context().connection
|
|
53
|
-
|
|
54
|
-
@cached_property
|
|
55
|
-
def _log(self):
|
|
56
|
-
return logging.getLogger(__name__)
|
|
57
|
-
|
|
58
|
-
def _execute_string(
|
|
59
|
-
self,
|
|
60
|
-
sql_text: str,
|
|
61
|
-
remove_comments: bool = False,
|
|
62
|
-
return_cursors: bool = True,
|
|
63
|
-
cursor_class: SnowflakeCursor = SnowflakeCursor,
|
|
64
|
-
**kwargs,
|
|
65
|
-
) -> Iterable[SnowflakeCursor]:
|
|
66
|
-
"""
|
|
67
|
-
This is a custom implementation of SnowflakeConnection.execute_string that returns generator
|
|
68
|
-
instead of list. In case of executing multiple queries are executed one by one. This mean we can
|
|
69
|
-
access result of previous queries while evaluating next one. For example, we can print the results.
|
|
70
|
-
"""
|
|
71
|
-
self._log.debug("Executing %s", sql_text)
|
|
72
|
-
stream = StringIO(sql_text)
|
|
73
|
-
stream_generator = self._conn.execute_stream(
|
|
74
|
-
stream, remove_comments=remove_comments, cursor_class=cursor_class, **kwargs
|
|
75
|
-
)
|
|
76
|
-
return stream_generator if return_cursors else list()
|
|
77
|
-
|
|
78
|
-
def _execute_query(self, query: str, **kwargs):
|
|
79
|
-
*_, last_result = self._execute_queries(query, **kwargs)
|
|
80
|
-
return last_result
|
|
81
|
-
|
|
82
|
-
def _execute_queries(self, queries: str, **kwargs):
|
|
83
|
-
return list(self._execute_string(dedent(queries), **kwargs))
|
|
84
|
-
|
|
85
|
-
def execute_query(self, query: str, **kwargs):
|
|
86
|
-
return self._execute_query(query, **kwargs)
|
|
87
|
-
|
|
88
|
-
def execute_queries(self, queries: str, **kwargs):
|
|
89
|
-
return self._execute_queries(queries, **kwargs)
|
|
90
|
-
|
|
91
|
-
def use(self, object_type: ObjectType, name: str):
|
|
92
|
-
try:
|
|
93
|
-
self._execute_query(f"use {object_type.value.sf_name} {name}")
|
|
94
|
-
except ProgrammingError:
|
|
95
|
-
# Rewrite the error to make the message more useful.
|
|
96
|
-
raise ProgrammingError(
|
|
97
|
-
f"Could not use {object_type} {name}. Object does not exist, or operation cannot be performed."
|
|
98
|
-
)
|
|
99
|
-
|
|
100
|
-
def current_role(self) -> str:
|
|
101
|
-
return self._execute_query(f"select current_role()").fetchone()[0]
|
|
102
|
-
|
|
103
|
-
@contextmanager
|
|
104
|
-
def use_role(self, new_role: str):
|
|
105
|
-
"""
|
|
106
|
-
Switches to a different role for a while, then switches back.
|
|
107
|
-
This is a no-op if the requested role is already active.
|
|
108
|
-
"""
|
|
109
|
-
prev_role = self.current_role()
|
|
110
|
-
is_different_role = new_role.lower() != prev_role.lower()
|
|
111
|
-
if is_different_role:
|
|
112
|
-
self._log.debug("Assuming different role: %s", new_role)
|
|
113
|
-
self._execute_query(f"use role {new_role}")
|
|
114
|
-
try:
|
|
115
|
-
yield
|
|
116
|
-
finally:
|
|
117
|
-
if is_different_role:
|
|
118
|
-
self._execute_query(f"use role {prev_role}")
|
|
119
|
-
|
|
120
|
-
def session_has_warehouse(self) -> bool:
|
|
121
|
-
result = self._execute_query(
|
|
122
|
-
"select current_warehouse() is not null"
|
|
123
|
-
).fetchone()
|
|
124
|
-
return bool(result[0])
|
|
125
|
-
|
|
126
|
-
@contextmanager
|
|
127
|
-
def use_warehouse(self, new_wh: str):
|
|
128
|
-
"""
|
|
129
|
-
Switches to a different warehouse for a while, then switches back.
|
|
130
|
-
This is a no-op if the requested warehouse is already active.
|
|
131
|
-
If there is no default warehouse in the account, it will throw an error.
|
|
132
|
-
"""
|
|
133
|
-
|
|
134
|
-
wh_result = self._execute_query(f"select current_warehouse()").fetchone()
|
|
135
|
-
# If user has an assigned default warehouse, prev_wh will contain a value even if the warehouse is suspended.
|
|
136
|
-
try:
|
|
137
|
-
prev_wh = wh_result[0]
|
|
138
|
-
except:
|
|
139
|
-
prev_wh = None
|
|
140
|
-
|
|
141
|
-
# new_wh is not None, and should already be a valid identifier, no additional check is performed here.
|
|
142
|
-
is_different_wh = new_wh != prev_wh
|
|
143
|
-
try:
|
|
144
|
-
if is_different_wh:
|
|
145
|
-
self._log.debug("Using warehouse: %s", new_wh)
|
|
146
|
-
self.use(object_type=ObjectType.WAREHOUSE, name=new_wh)
|
|
147
|
-
yield
|
|
148
|
-
finally:
|
|
149
|
-
if prev_wh and is_different_wh:
|
|
150
|
-
self._log.debug("Switching back to warehouse: %s", prev_wh)
|
|
151
|
-
self.use(object_type=ObjectType.WAREHOUSE, name=prev_wh)
|
|
152
|
-
|
|
153
|
-
def create_password_secret(
|
|
154
|
-
self, name: FQN, username: str, password: str
|
|
155
|
-
) -> SnowflakeCursor:
|
|
156
|
-
return self._execute_query(
|
|
157
|
-
f"""
|
|
158
|
-
create secret {name.sql_identifier}
|
|
159
|
-
type = password
|
|
160
|
-
username = '{username}'
|
|
161
|
-
password = '{password}'
|
|
162
|
-
"""
|
|
163
|
-
)
|
|
164
|
-
|
|
165
|
-
def create_api_integration(
|
|
166
|
-
self, name: FQN, api_provider: str, allowed_prefix: str, secret: Optional[str]
|
|
167
|
-
) -> SnowflakeCursor:
|
|
168
|
-
return self._execute_query(
|
|
169
|
-
f"""
|
|
170
|
-
create api integration {name.sql_identifier}
|
|
171
|
-
api_provider = {api_provider}
|
|
172
|
-
api_allowed_prefixes = ('{allowed_prefix}')
|
|
173
|
-
allowed_authentication_secrets = ({secret if secret else ''})
|
|
174
|
-
enabled = true
|
|
175
|
-
"""
|
|
176
|
-
)
|
|
177
|
-
|
|
178
|
-
def _execute_schema_query(self, query: str, name: Optional[str] = None, **kwargs):
|
|
179
|
-
"""
|
|
180
|
-
Check that a database and schema are provided before executing the query. Useful for operating on schema level objects.
|
|
181
|
-
"""
|
|
182
|
-
self.check_database_and_schema_provided(name)
|
|
183
|
-
return self._execute_query(query, **kwargs)
|
|
184
|
-
|
|
185
|
-
def check_database_and_schema_provided(self, name: Optional[str] = None) -> None:
|
|
186
|
-
"""
|
|
187
|
-
Checks if a database and schema are provided, either through the connection context or a qualified name.
|
|
188
|
-
"""
|
|
189
|
-
fqn = FQN.from_string(name).using_connection(self._conn)
|
|
190
|
-
if not fqn.database:
|
|
191
|
-
raise DatabaseNotProvidedError()
|
|
192
|
-
if not fqn.schema:
|
|
193
|
-
raise SchemaNotProvidedError()
|
|
194
|
-
|
|
195
|
-
@staticmethod
|
|
196
|
-
def _qualified_name_to_in_clause(identifier: FQN) -> Tuple[str, Optional[str]]:
|
|
197
|
-
if identifier.database:
|
|
198
|
-
schema = identifier.schema or "PUBLIC"
|
|
199
|
-
in_clause = f"in schema {identifier.database}.{schema}"
|
|
200
|
-
elif identifier.schema:
|
|
201
|
-
in_clause = f"in schema {identifier.schema}"
|
|
202
|
-
else:
|
|
203
|
-
in_clause = None
|
|
204
|
-
return identifier.name, in_clause
|
|
205
|
-
|
|
206
|
-
class InClauseWithQualifiedNameError(ValueError):
|
|
207
|
-
def __init__(self):
|
|
208
|
-
super().__init__("non-empty 'in_clause' passed with qualified 'name'")
|
|
209
|
-
|
|
210
|
-
def show_specific_object(
|
|
211
|
-
self,
|
|
212
|
-
object_type_plural: str,
|
|
213
|
-
name: str,
|
|
214
|
-
name_col: str = "name",
|
|
215
|
-
in_clause: str = "",
|
|
216
|
-
check_schema: bool = False,
|
|
217
|
-
) -> Optional[dict]:
|
|
218
|
-
"""
|
|
219
|
-
Executes a "show <objects> like" query for a particular entity with a
|
|
220
|
-
given (optionally qualified) name. This command is useful when the corresponding
|
|
221
|
-
"describe <object>" query does not provide the information you seek.
|
|
222
|
-
|
|
223
|
-
Note that this command is analogous to describe and should only return a single row.
|
|
224
|
-
If the target object type is a schema level object, then check_schema should be set to True
|
|
225
|
-
so that the function will verify that a database and schema are provided, either through
|
|
226
|
-
the connection or a qualified name, before executing the query.
|
|
227
|
-
"""
|
|
228
|
-
|
|
229
|
-
unqualified_name, name_in_clause = self._qualified_name_to_in_clause(
|
|
230
|
-
FQN.from_string(name)
|
|
231
|
-
)
|
|
232
|
-
if in_clause and name_in_clause:
|
|
233
|
-
raise self.InClauseWithQualifiedNameError()
|
|
234
|
-
elif name_in_clause:
|
|
235
|
-
in_clause = name_in_clause
|
|
236
|
-
show_obj_query = f"show {object_type_plural} like {identifier_to_show_like_pattern(unqualified_name)} {in_clause}".strip()
|
|
237
|
-
|
|
238
|
-
if check_schema:
|
|
239
|
-
show_obj_cursor = self._execute_schema_query( # type: ignore
|
|
240
|
-
show_obj_query, name=name, cursor_class=DictCursor
|
|
241
|
-
)
|
|
242
|
-
else:
|
|
243
|
-
show_obj_cursor = self._execute_query( # type: ignore
|
|
244
|
-
show_obj_query, cursor_class=DictCursor
|
|
245
|
-
)
|
|
246
|
-
|
|
247
|
-
if show_obj_cursor.rowcount is None:
|
|
248
|
-
raise SnowflakeSQLExecutionError(show_obj_query)
|
|
249
|
-
elif show_obj_cursor.rowcount > 1:
|
|
250
|
-
raise ProgrammingError(
|
|
251
|
-
f"Received multiple rows from result of SQL statement: {show_obj_query}. Usage of 'show_specific_object' may not be properly scoped."
|
|
252
|
-
)
|
|
253
|
-
|
|
254
|
-
show_obj_row = find_first_row(
|
|
255
|
-
show_obj_cursor,
|
|
256
|
-
lambda row: row[name_col] == unquote_identifier(unqualified_name),
|
|
257
|
-
)
|
|
258
|
-
return show_obj_row
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
class SqlExecutionMixin(SqlExecutor):
|
|
262
|
-
def __init__(self, *args, **kwargs):
|
|
263
|
-
super().__init__(*args, **kwargs)
|
|
264
|
-
self._snowpark_session = None
|
|
265
|
-
|
|
266
|
-
@property
|
|
267
|
-
def snowpark_session(self):
|
|
268
|
-
if not self._snowpark_session:
|
|
269
|
-
from snowflake.snowpark.session import Session
|
|
270
|
-
|
|
271
|
-
self._snowpark_session = Session.builder.configs(
|
|
272
|
-
{"connection": self._conn}
|
|
273
|
-
).create()
|
|
274
|
-
return self._snowpark_session
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
class VerboseCursor(SnowflakeCursor):
|
|
278
|
-
def execute(self, command: str, *args, **kwargs):
|
|
279
|
-
cli_console.message(command)
|
|
280
|
-
super().execute(command, *args, **kwargs)
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2024 Snowflake Inc.
|
|
2
|
-
#
|
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
# you may not use this file except in compliance with the License.
|
|
5
|
-
# You may obtain a copy of the License at
|
|
6
|
-
#
|
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
#
|
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
# See the License for the specific language governing permissions and
|
|
13
|
-
# limitations under the License.
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2024 Snowflake Inc.
|
|
2
|
-
#
|
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
# you may not use this file except in compliance with the License.
|
|
5
|
-
# You may obtain a copy of the License at
|
|
6
|
-
#
|
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
#
|
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
# See the License for the specific language governing permissions and
|
|
13
|
-
# limitations under the License.
|
|
14
|
-
|
|
15
|
-
from __future__ import annotations
|
|
16
|
-
|
|
17
|
-
from typing import Callable, List, Optional
|
|
18
|
-
|
|
19
|
-
from snowflake.connector.cursor import DictCursor
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
def _rows_generator(cursor: DictCursor, predicate: Callable[[dict], bool]):
|
|
23
|
-
return (row for row in cursor.fetchall() if predicate(row))
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
def find_all_rows(cursor: DictCursor, predicate: Callable[[dict], bool]) -> List[dict]:
|
|
27
|
-
return list(_rows_generator(cursor, predicate))
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
def find_first_row(
|
|
31
|
-
cursor: DictCursor, predicate: Callable[[dict], bool]
|
|
32
|
-
) -> Optional[dict]:
|
|
33
|
-
"""Returns the first row that matches the predicate, or None."""
|
|
34
|
-
return next(_rows_generator(cursor, predicate), None)
|