snowmobile.core.statement

Base class for all Statement objects.

Module Contents

Classes

Time

Container for execution time info.

Statement

Base class for all Statement objects.

class snowmobile.core.statement.Time(**data)

Bases: pydantic.BaseModel

Container for execution time info.

started :int
ended :int
class snowmobile.core.statement.Statement(sn: snowmobile.core.connection.Snowmobile, statement: Union[sqlparse.sql.Statement, str], index: Optional[int] = None, attrs_raw: Optional[str] = None, e: Optional[ExceptionHandler] = None, **kwargs)

Bases: snowmobile.core.tag.Attrs, snowmobile.core.Name, snowmobile.core.Generic

Base class for all Statement objects.

Home for attributes and methods that are associated with all statement objects, generic or QA.

sn

snowmobile.connect object.

Type

snowmobile.connect

statement

A sqlparse.sql.Statement object.

Type

Union[sqlparse.sql.Statement, str]

index

The context-specific index position of a statement within a script; can be None.

Type

int

patterns

config.Pattern object for more succinct access to values specified in snowmobile.toml.

Type

config.Pattern

results

The results of the statement if executed as a pandas.DataFrame.

Type

pd.DataFrame

outcome

Numeric indicator of outcome; defaults to 0 and is modified based on the outcome of statement execution and/or QA validation for derived classes.

Type

int

outcome_txt

Plain text of outcome (‘skipped’, ‘failed’, ‘completed’, ‘passed’).

Type

str

outcome_html

HTML text for the outcome as an admonition/information banner based on the following mapping of outcome_txt to admonition argument:

  • failed ——> warning

  • completed –> info

  • passed —–> success

Type

str

start_time

Unix timestamp of the query start time if executed; 0 otherwise.

Type

int

end_time

Unix timestamp of the query end time if executed; 0 otherwise.

Type

int

execution_time

Execution time of the query in seconds if executed; 0 otherwise.

Type

int

execution_time_txt

Plain text description of execution time if executed; returned in seconds if execution time is less than 60 seconds, minutes otherwise.

Type

str

first_keyword

The first keyword within the statement as a sqlparse.sql.Token.

Type

sqlparse.sql.Token

sql

The sql associated with the statement as a raw string.

Type

str

Initialize self. See help(type(self)) for accurate signature.

sql(self, set_as: Optional[str] = None, tag: bool = False)Union[str, Statement]

Raw sql from statement, including result limit if enabled.

parse(self)Tuple[Dict, str]

Parses tag contents into a valid dictionary.

Uses the values specified in snowmobile.toml to parse a raw string of statement attributes into a valid dictionary.

Note

  • If is_multiline is True and name is not included within the arguments, an assertion error will be thrown.

  • If is_multiline is False, the raw string within the wrap will be treated as the name.

  • The wrap attribute is set once parsing is completed and name has been validated.

Returns (dict):

Parsed wrap arguments as a dictionary.

start(self)

Sets start_time attribute.

end(self)

Updates execution time attributes.

In namespace, sets:
trim(self)str

Statement as a string including only the sql and a single-line wrap name.

Note

The wrap name used here will be the user-pr wrap from the original script or a generated Name.nm if a wrap was not provided for a given statement.

property is_derived(self)

Indicates whether or not it’s a generic or derived (QA) statement.

property lines(self)List[str]

Returns each line within the statement as a list.

as_section(self, incl_sql_tag: Optional[bool] = None, result_wrap: Optional[str] = None)snowmobile.core.Section

Returns current statement as a Section object.

set_state(self, index: Optional[int] = None, ctx_id: Optional[int] = None, in_context: Optional[bool] = None, filters: dict = None)snowmobile.core.statement.Statement

Sets current state/context on a statement object.

Parameters
  • ctx_id (int) – Unix timestamp the script.filter() context manager was invoked.

  • filters (dict) – Kwargs passed to script.filter().

  • index (int) – Integer to set as the statement’s index position.

reset(self, index: bool = False, ctx_id: bool = False, in_context: bool = False, scope: bool = False)snowmobile.core.statement.Statement

Resets attributes on the statement object to reflect as if read from source.

In its current form, includes:
  • Resetting the statement/wrap’s index to their original values.

  • Resetting the is_included attribute of the statement’s wrap to True.

  • Populating error_last with errors from current context.

  • Caching current context’s timestamp and resetting back to None.

process(self)

Used by derived classes for post-processing the returned results.

run(self, as_df: bool = True, lower: bool = True, render: bool = False, on_error: Optional[str] = None, on_exception: Optional[str] = None, on_failure: Optional[str] = None, ctx_id: Optional[int] = None)snowmobile.core.statement.Statement

Run method for all statement objects.

Parameters
  • as_df (bool) – Store results of query as pandas.DataFrame or SnowflakeCursor.

  • lower (bool) – Lower case column names in results DataFrame if as_df=True.

  • render (bool) – Render the sql executed as markdown.

  • on_error (str) –

    Behavior if an execution/database error is encountered
    • None: default behavior, exception will be raised

    • c: continue with execution

  • on_exception (str) –

    Behavior if an exception is raised in the post-processing of results from a derived class of Statement ( Empty and Diff).

    • None: default behavior, exception will be raised

    • c: continue with execution

  • on_failure (str) –

    Behavior if no error is encountered in execution or post-processing but the result of the post-processing has turned the statement’s outcome attribute to False, indicating the results returned by the statement have failed validation.

    • None: default behavior, exception will be raised

    • c: continue with execution

Returns (Statement):

Statement object post-executing query.

outcome_txt(self, _id: Optional[int] = None)str

Outcome as a string.

property outcome_html(self)str

Outcome as an html admonition banner.