Skip to content

Commit

Permalink
fixup! Add global context
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-turbaszek committed Apr 22, 2024
1 parent b52867d commit 916238b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
* `snow snowpark build`
* `snow snowpark package lookup`
* `snow snowpark package create`
* `snow sql` command supports now templating of queries.
* `snow sql` command supports now client-side templating of queries.

## Fixes and improvements
* Adding `--image-name` option for image name argument in `spcs image-repository list-tags` for consistency with other commands.
Expand Down
23 changes: 23 additions & 0 deletions src/snowflake/cli/api/commands/flags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import tempfile
from dataclasses import dataclass
from enum import Enum
from inspect import signature
from pathlib import Path
Expand Down Expand Up @@ -532,3 +533,25 @@ def _warning_callback(ctx: click.Context, param: click.Parameter, value: Any):
return value.value

return _warning_callback


@dataclass
class Variable:
key: str
value: str

def __init__(self, key: str, value: str):
self.key = key
self.value = value


def parse_key_value_variables(variables: List[str]) -> List[Variable]:
"""Util for parsing key=value input. Useful for commands accepting multiple input options."""
result = []
for p in variables:
if "=" not in p:
raise ClickException(f"Invalid variable: '{p}'")

key, value = p.split("=", 1)
result.append(Variable(key.strip(), value.strip()))
return result
5 changes: 2 additions & 3 deletions src/snowflake/cli/plugins/sql/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import List, Optional

import typer
from snowflake.cli.api.commands.flags import parse_key_value_variables
from snowflake.cli.api.commands.snow_typer import SnowTyper
from snowflake.cli.api.output.types import CommandResult, MultipleResults, QueryResult
from snowflake.cli.plugins.sql.manager import SqlManager
Expand Down Expand Up @@ -62,9 +63,7 @@ def execute_sql(
"""
data = {}
if data_override:
for key_value_str in data_override:
key, value = _parse_key_value(key_value_str)
data[key] = value
data = {v.key: v.value for v in parse_key_value_variables(data_override)}

single_statement, cursors = SqlManager().execute(query, file, std_in, data=data)
if single_statement:
Expand Down
26 changes: 2 additions & 24 deletions src/snowflake/cli/plugins/stage/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import logging
import re
from contextlib import nullcontext
from dataclasses import dataclass
from os import path
from pathlib import Path
from typing import Dict, List, Optional, Union

from click import ClickException
from snowflake.cli.api.commands.flags import OnErrorType
from snowflake.cli.api.commands.flags import OnErrorType, parse_key_value_variables
from snowflake.cli.api.console import cli_console
from snowflake.cli.api.project.util import to_string_literal
from snowflake.cli.api.secure_path import SecurePath
Expand All @@ -27,16 +26,6 @@
EXECUTE_SUPPORTED_FILES_FORMATS = {".sql"}


@dataclass
class Variable:
key: str
value: str

def __init__(self, key: str, value: str):
self.key = key
self.value = value


class StageManager(SqlExecutionMixin):
@staticmethod
def get_standard_stage_prefix(name: str) -> str:
Expand Down Expand Up @@ -262,21 +251,10 @@ def _parse_execute_variables(variables: Optional[List[str]]) -> Optional[str]:
if not variables:
return None

parsed_variables = StageManager._parse_variables(variables)
parsed_variables = parse_key_value_variables(variables)
query_parameters = [f"{v.key}=>{v.value}" for v in parsed_variables]
return f" using ({', '.join(query_parameters)})"

@staticmethod
def _parse_variables(variables: List[str]) -> List[Variable]:
result = []
for p in variables:
if "=" not in p:
raise ClickException(f"Invalid variable: '{p}'")

key, value = p.split("=", 1)
result.append(Variable(key.strip(), value.strip()))
return result

def _call_execute_immediate(
self, file: str, variables: Optional[str], on_error: OnErrorType
) -> Dict:
Expand Down
6 changes: 4 additions & 2 deletions tests/__snapshots__/test_help_messages.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -4820,15 +4820,17 @@
Query to execute can be specified using query option, filename option (all
queries from file will be executed) or via stdin by piping output from other
command. For example `cat my.sql | snow sql -i`.
The command supports variable substitution that happens on client-side. Both
$VARIABLE or ${ VARIABLE } syntax are supported.

╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --query -q TEXT Query to execute. [default: None] │
│ --filename -f FILE File to execute. [default: None] │
│ --stdin -i Read the query from standard input. Use it when │
│ piping input to this command. │
│ --data -D TEXT String in format of key=value. If provided the SQL │
│ content will be treated and rendered using
│ provided data.
│ content will be treated as template and rendered
using provided data. │
│ [default: None] │
│ --help -h Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────╯
Expand Down
2 changes: 2 additions & 0 deletions tests/api/utils/test_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def test_that_common_logic_block_are_ignored(text):


def test_that_common_comments_are_respected():
# Make sure comment are ignored
assert snowflake_cli_jinja_render("{# note a comment &{ foo } #}") == ""
# Make sure comment's work together with templates
assert (
snowflake_cli_jinja_render("{# note a comment #}&{ foo }", data={"foo": "bar"})
== "bar"
Expand Down

0 comments on commit 916238b

Please sign in to comment.