Utils
Utility functions for koality data quality checks.
execute_query(query, duckdb_client, database_accessor, database_provider)
Execute a query, using bigquery_query() if the accessor is a BigQuery database.
This handles the limitation where BigQuery's Storage Read API cannot read views. When a BigQuery accessor is detected, the query is wrapped in bigquery_query() which uses the Jobs API instead.
Note: bigquery_query() only works for SELECT queries. Write operations (INSERT, CREATE, UPDATE, DELETE) use standard DuckDB execution with the accessor prefix.
Source code in src/koality/utils.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
format_threshold(value)
Format threshold values for SQL insertion, handling infinity appropriately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float | None
|
The threshold value to format (can be None, finite, or infinite). |
required |
Returns:
| Type | Description |
|---|---|
str
|
String representation suitable for SQL VALUES clause: |
str
|
|
str
|
|
str
|
|
str
|
|
Example
format_threshold(None) 'NULL' format_threshold(float('inf')) "'+Infinity'" format_threshold(float('-inf')) "'-Infinity'" format_threshold(42.5) '42.5'
Source code in src/koality/utils.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
identify_database_provider(duckdb_client, database_accessor)
Identify the database provider type from a DuckDB database accessor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
duckdb_client
|
DuckDBPyConnection
|
DuckDB client connection. |
required |
database_accessor
|
str
|
The name of the attached database. |
required |
Returns:
| Type | Description |
|---|---|
DatabaseProvider
|
DatabaseProvider with type information (e.g., 'bigquery', 'postgres'). |
Raises:
| Type | Description |
|---|---|
DatabaseError
|
If the database accessor is not found in DuckDB databases. |
Source code in src/koality/utils.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
parse_date(date)
Parse a date string to an ISO format date.
Supports relative terms like "today", "yesterday", or "tomorrow", actual dates, or relative dates with offsets like "today-2", "yesterday+1".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date
|
str
|
The date string to be parsed. Supports: - "today", "yesterday", "tomorrow" - "today+N", "today-N" (e.g., "today-2" for 2 days ago) - "yesterday+N", "yesterday-N" (e.g., "yesterday-2" for 3 days ago) - "tomorrow+N", "tomorrow-N" - ISO format dates like "2023-01-15" |
required |
Source code in src/koality/utils.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
substitute_variables(text, variables)
Substitute ${VAR} placeholders in text with variable values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text containing ${VAR} placeholders to substitute. |
required |
variables
|
dict[str, str]
|
Dict of variable name -> value mappings. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text with all ${VAR} placeholders substituted. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a variable is referenced but not defined. |
Example
substitute_variables("project=${PROJECT_ID}", {"PROJECT_ID": "my-project"}) 'project=my-project'
Source code in src/koality/utils.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
to_set(value)
Convert the input value to a set.
The special case of one single string is also covered. Duplicates are also removed and for deterministic behavior, the values are sorted.
Convert input as follows: - 1 -> {1} - True -> {True} - "toys" / '"toys"' -> {"toys"} - ("toys") / '("toys")' -> {"toys"} - ("toys", "shirt") / '("toys", "shirt")' -> {"shirt", "toys"} - ["toys"] -> {"toys"} - {"toys"} -> {"toys"}
Source code in src/koality/utils.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |