local-bigquery 0.3.0__tar.gz
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.
- local_bigquery-0.3.0/.gitignore +6 -0
- local_bigquery-0.3.0/LICENSE +21 -0
- local_bigquery-0.3.0/PKG-INFO +168 -0
- local_bigquery-0.3.0/README.md +145 -0
- local_bigquery-0.3.0/pyproject.toml +60 -0
- local_bigquery-0.3.0/src/local_bigquery/__init__.py +3 -0
- local_bigquery-0.3.0/src/local_bigquery/api/__init__.py +30 -0
- local_bigquery-0.3.0/src/local_bigquery/api/datasets.py +59 -0
- local_bigquery-0.3.0/src/local_bigquery/api/iam.py +29 -0
- local_bigquery-0.3.0/src/local_bigquery/api/jobs.py +176 -0
- local_bigquery-0.3.0/src/local_bigquery/api/models.py +40 -0
- local_bigquery-0.3.0/src/local_bigquery/api/projects.py +35 -0
- local_bigquery-0.3.0/src/local_bigquery/api/routines.py +81 -0
- local_bigquery-0.3.0/src/local_bigquery/api/row_access_policies.py +73 -0
- local_bigquery-0.3.0/src/local_bigquery/api/tables.py +115 -0
- local_bigquery-0.3.0/src/local_bigquery/api/uploads.py +80 -0
- local_bigquery-0.3.0/src/local_bigquery/app.py +145 -0
- local_bigquery-0.3.0/src/local_bigquery/catalog/__init__.py +0 -0
- local_bigquery-0.3.0/src/local_bigquery/catalog/datasets.py +119 -0
- local_bigquery-0.3.0/src/local_bigquery/catalog/ddl.py +215 -0
- local_bigquery-0.3.0/src/local_bigquery/catalog/iam.py +68 -0
- local_bigquery-0.3.0/src/local_bigquery/catalog/indexes.py +59 -0
- local_bigquery-0.3.0/src/local_bigquery/catalog/metadata.py +112 -0
- local_bigquery-0.3.0/src/local_bigquery/catalog/models.py +102 -0
- local_bigquery-0.3.0/src/local_bigquery/catalog/names.py +83 -0
- local_bigquery-0.3.0/src/local_bigquery/catalog/options.py +102 -0
- local_bigquery-0.3.0/src/local_bigquery/catalog/routines.py +144 -0
- local_bigquery-0.3.0/src/local_bigquery/catalog/row_access.py +174 -0
- local_bigquery-0.3.0/src/local_bigquery/catalog/tabledata.py +203 -0
- local_bigquery-0.3.0/src/local_bigquery/catalog/tables.py +430 -0
- local_bigquery-0.3.0/src/local_bigquery/cli.py +42 -0
- local_bigquery-0.3.0/src/local_bigquery/discovery.json +11957 -0
- local_bigquery-0.3.0/src/local_bigquery/engine/__init__.py +0 -0
- local_bigquery-0.3.0/src/local_bigquery/engine/database.py +159 -0
- local_bigquery-0.3.0/src/local_bigquery/engine/results.py +102 -0
- local_bigquery-0.3.0/src/local_bigquery/engine/sessions.py +40 -0
- local_bigquery-0.3.0/src/local_bigquery/engine/types.py +222 -0
- local_bigquery-0.3.0/src/local_bigquery/errors.py +287 -0
- local_bigquery-0.3.0/src/local_bigquery/grpc/__init__.py +0 -0
- local_bigquery-0.3.0/src/local_bigquery/grpc/read.py +258 -0
- local_bigquery-0.3.0/src/local_bigquery/grpc/server.py +121 -0
- local_bigquery-0.3.0/src/local_bigquery/grpc/write.py +301 -0
- local_bigquery-0.3.0/src/local_bigquery/jobs/__init__.py +0 -0
- local_bigquery-0.3.0/src/local_bigquery/jobs/copy.py +46 -0
- local_bigquery-0.3.0/src/local_bigquery/jobs/extract.py +95 -0
- local_bigquery-0.3.0/src/local_bigquery/jobs/load.py +281 -0
- local_bigquery-0.3.0/src/local_bigquery/jobs/merge.py +139 -0
- local_bigquery-0.3.0/src/local_bigquery/jobs/query.py +428 -0
- local_bigquery-0.3.0/src/local_bigquery/jobs/runner.py +243 -0
- local_bigquery-0.3.0/src/local_bigquery/jobs/storage.py +81 -0
- local_bigquery-0.3.0/src/local_bigquery/jobs/store.py +61 -0
- local_bigquery-0.3.0/src/local_bigquery/models.py +2104 -0
- local_bigquery-0.3.0/src/local_bigquery/pytest_plugin.py +59 -0
- local_bigquery-0.3.0/src/local_bigquery/repl.py +125 -0
- local_bigquery-0.3.0/src/local_bigquery/resource.py +41 -0
- local_bigquery-0.3.0/src/local_bigquery/settings.py +23 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/__init__.py +0 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/dialect.py +241 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/functions/aggregates.sql +24 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/functions/arrays.sql +23 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/functions/conditional.sql +1 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/functions/datetime.sql +68 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/functions/geography.sql +126 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/functions/json.sql +145 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/functions/math.sql +34 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/functions/net.sql +59 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/functions/ranges.sql +49 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/functions/strings.sql +81 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/js.py +160 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/native.py +345 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/params.py +118 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/__init__.py +49 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/arrays.py +122 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/columns.py +24 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/datetime.py +419 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/ddl.py +161 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/dml.py +42 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/external.py +46 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/information_schema.py +604 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/json.py +54 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/literals.py +51 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/math.py +91 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/parameters.py +70 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/partitions.py +83 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/query.py +140 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/ranges.py +114 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/row_access.py +71 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/safe.py +42 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/strings.py +148 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/tables.py +221 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/typing.py +319 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/rules/udfs.py +72 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/script.py +672 -0
- local_bigquery-0.3.0/src/local_bigquery/sql/translate.py +62 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 The Authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: local-bigquery
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: A BigQuery emulator for local development and tests
|
|
5
|
+
Project-URL: Repository, https://github.com/novucs/local-bigquery
|
|
6
|
+
Project-URL: Issues, https://github.com/novucs/local-bigquery/issues
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Python: <3.14,>=3.13
|
|
10
|
+
Requires-Dist: duckdb>=1.5.5
|
|
11
|
+
Requires-Dist: fastapi>=0.139
|
|
12
|
+
Requires-Dist: fastavro>=1.12.2
|
|
13
|
+
Requires-Dist: google-cloud-bigquery-storage>=2.41.0
|
|
14
|
+
Requires-Dist: grpcio>=1.83.0
|
|
15
|
+
Requires-Dist: mini-racer>=0.12.4
|
|
16
|
+
Requires-Dist: prompt-toolkit>=3.0.50
|
|
17
|
+
Requires-Dist: pyarrow>=25.0.1
|
|
18
|
+
Requires-Dist: pydantic-settings>=2.8.1
|
|
19
|
+
Requires-Dist: pygments>=2.19.1
|
|
20
|
+
Requires-Dist: sqlglot>=30.19
|
|
21
|
+
Requires-Dist: uvicorn>=0.35
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# Local BigQuery
|
|
25
|
+
|
|
26
|
+
A BigQuery emulator for development and tests. It serves the BigQuery REST and Storage
|
|
27
|
+
APIs and runs GoogleSQL on DuckDB, so the official clients work with no project or
|
|
28
|
+
credentials.
|
|
29
|
+
|
|
30
|
+
## Quick start
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
docker run -p 9050:9050 -p 9060:9060 -v bigquery:/data ghcr.io/novucs/local-bigquery:0.3.0
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Then point a client at `http://localhost:9050`, without credentials. Any project ID
|
|
37
|
+
works, and data persists in `/data`.
|
|
38
|
+
|
|
39
|
+
## Connecting
|
|
40
|
+
|
|
41
|
+
### Python
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from google.auth.credentials import AnonymousCredentials
|
|
45
|
+
from google.cloud import bigquery
|
|
46
|
+
|
|
47
|
+
client = bigquery.Client(
|
|
48
|
+
project="local",
|
|
49
|
+
credentials=AnonymousCredentials(),
|
|
50
|
+
client_options={"api_endpoint": "http://localhost:9050"},
|
|
51
|
+
)
|
|
52
|
+
rows = client.query_and_wait("SELECT 1 AS x")
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
For faster `to_dataframe()`, add the Storage Read API, served as plain gRPC on port `9060`:
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
import grpc
|
|
59
|
+
from google.cloud import bigquery_storage_v1
|
|
60
|
+
from google.cloud.bigquery_storage_v1.services.big_query_read.transports import (
|
|
61
|
+
BigQueryReadGrpcTransport,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
transport = BigQueryReadGrpcTransport(channel=grpc.insecure_channel("localhost:9060"))
|
|
65
|
+
bqstorage = bigquery_storage_v1.BigQueryReadClient(transport=transport)
|
|
66
|
+
frame = client.query_and_wait("SELECT 1 AS x").to_dataframe(bqstorage_client=bqstorage)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
For SQLAlchemy, pass the client in:
|
|
70
|
+
`create_engine("bigquery://local", connect_args={"client": client})`.
|
|
71
|
+
|
|
72
|
+
### Go
|
|
73
|
+
|
|
74
|
+
```go
|
|
75
|
+
client, err := bigquery.NewClient(ctx, "local",
|
|
76
|
+
option.WithEndpoint("http://localhost:9050/bigquery/v2/"),
|
|
77
|
+
option.WithoutAuthentication(),
|
|
78
|
+
)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Node.js
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
import { BigQuery } from "@google-cloud/bigquery";
|
|
85
|
+
|
|
86
|
+
const bigquery = new BigQuery({ projectId: "local", apiEndpoint: "http://localhost:9050" });
|
|
87
|
+
const [rows] = await bigquery.query("SELECT 1 AS x");
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Testing with pytest
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
pip install local-bigquery
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
This adds pytest fixtures that run the emulator inside the test process. There's no
|
|
97
|
+
Docker, and it starts in under a second:
|
|
98
|
+
|
|
99
|
+
- `bigquery_client`: a client connected to an emulator shared by the whole test run.
|
|
100
|
+
- `bigquery_dataset`: a new dataset for each test, deleted afterwards.
|
|
101
|
+
- `bigquery_emulator`: the emulator's `rest_url`, `grpc_address` and `project_id`.
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
def test_totals(bigquery_client, bigquery_dataset):
|
|
105
|
+
table = f"{bigquery_dataset.dataset_id}.orders"
|
|
106
|
+
bigquery_client.query_and_wait(f"CREATE TABLE {table} (customer STRING, amount INT64)")
|
|
107
|
+
bigquery_client.insert_rows_json(table, [{"customer": "a", "amount": 2}] * 3)
|
|
108
|
+
rows = bigquery_client.query_and_wait(f"SELECT SUM(amount) AS total FROM {table}")
|
|
109
|
+
assert [row.total for row in rows] == [6]
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Features
|
|
113
|
+
|
|
114
|
+
- **SQL**: GoogleSQL functions, DML including `MERGE`, DDL, scripting, procedures,
|
|
115
|
+
SQL/JavaScript UDFs, table functions, wildcard tables, `INFORMATION_SCHEMA`.
|
|
116
|
+
- **Tables**: partitioning, clustering, constraints, views, materialized views,
|
|
117
|
+
snapshots, clones, time travel, search and vector indexes.
|
|
118
|
+
- **Jobs**: query, load, copy and extract, with dry runs, cancellation and sessions.
|
|
119
|
+
- **Data**: CSV, JSON, Parquet, Avro and ORC loads, and CSV, JSON, Parquet and Avro exports.
|
|
120
|
+
Also `insert_rows_json` and the Storage Read and Write APIs.
|
|
121
|
+
- **`gs://`**: maps to `$DATA_DIR/gcs`, or to a GCS emulator set by `STORAGE_EMULATOR_HOST`.
|
|
122
|
+
- **Row access policies**: the caller is the service account that signed the request.
|
|
123
|
+
Signatures aren't checked, so any key works:
|
|
124
|
+
`service_account.Credentials.from_service_account_file("key.json", always_use_jwt_access=True)`.
|
|
125
|
+
Unsigned requests run as `CALLER`.
|
|
126
|
+
- **`EXTERNAL_QUERY`** against Postgres at `POSTGRES_URI`.
|
|
127
|
+
- **REPL**: `local-bigquery repl`. Delete all data with `local-bigquery reset`.
|
|
128
|
+
|
|
129
|
+
## Limitations
|
|
130
|
+
|
|
131
|
+
- For test-sized data on one machine. There's no authentication, and IAM policies
|
|
132
|
+
aren't enforced.
|
|
133
|
+
- Legacy SQL and BigQuery ML functions (`ML.PREDICT` and so on) aren't supported.
|
|
134
|
+
- A failed statement aborts the whole transaction.
|
|
135
|
+
- `STRING(n)` and `BYTES(n)` lengths aren't enforced.
|
|
136
|
+
- Other API methods return `501`.
|
|
137
|
+
|
|
138
|
+
Found a difference from BigQuery? [Open an issue](https://github.com/novucs/local-bigquery/issues).
|
|
139
|
+
|
|
140
|
+
## Configuration
|
|
141
|
+
|
|
142
|
+
Set these as environment variables, or pass the matching flag to `local-bigquery`.
|
|
143
|
+
|
|
144
|
+
| Variable | Flag | Default |
|
|
145
|
+
|---|---|---|
|
|
146
|
+
| `BIGQUERY_PORT` | `--port` | `9050` |
|
|
147
|
+
| `GRPC_PORT` | `--grpc-port` | `9060` |
|
|
148
|
+
| `DATA_DIR` | `--data-dir` | `/data` |
|
|
149
|
+
| `DEFAULT_PROJECT_ID` | `--project` | `local` |
|
|
150
|
+
| `DEFAULT_DATASET_ID` | `--dataset` | `local` |
|
|
151
|
+
| `CALLER` | | `user:local-bigquery@localhost` |
|
|
152
|
+
| `GROUPS` | | `{}`, e.g. `{"user:alice@example.com": ["group:team@example.com"]}` |
|
|
153
|
+
| `GCS_LOCAL_ROOT` | | `$DATA_DIR/gcs` |
|
|
154
|
+
| `STORAGE_EMULATOR_HOST` | | |
|
|
155
|
+
| `POSTGRES_CONNECTION_ID` | | `us.default` |
|
|
156
|
+
| `POSTGRES_URI` | | `postgresql://postgres:example@db:5432/postgres` |
|
|
157
|
+
|
|
158
|
+
## Development
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
uv run local-bigquery --data-dir /tmp/local-bigquery # run from source
|
|
162
|
+
uv run pytest # run the tests
|
|
163
|
+
uv run pytest --endpoint google --project <project> # run them against real BigQuery
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## License
|
|
167
|
+
|
|
168
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# Local BigQuery
|
|
2
|
+
|
|
3
|
+
A BigQuery emulator for development and tests. It serves the BigQuery REST and Storage
|
|
4
|
+
APIs and runs GoogleSQL on DuckDB, so the official clients work with no project or
|
|
5
|
+
credentials.
|
|
6
|
+
|
|
7
|
+
## Quick start
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
docker run -p 9050:9050 -p 9060:9060 -v bigquery:/data ghcr.io/novucs/local-bigquery:0.3.0
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Then point a client at `http://localhost:9050`, without credentials. Any project ID
|
|
14
|
+
works, and data persists in `/data`.
|
|
15
|
+
|
|
16
|
+
## Connecting
|
|
17
|
+
|
|
18
|
+
### Python
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
from google.auth.credentials import AnonymousCredentials
|
|
22
|
+
from google.cloud import bigquery
|
|
23
|
+
|
|
24
|
+
client = bigquery.Client(
|
|
25
|
+
project="local",
|
|
26
|
+
credentials=AnonymousCredentials(),
|
|
27
|
+
client_options={"api_endpoint": "http://localhost:9050"},
|
|
28
|
+
)
|
|
29
|
+
rows = client.query_and_wait("SELECT 1 AS x")
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
For faster `to_dataframe()`, add the Storage Read API, served as plain gRPC on port `9060`:
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
import grpc
|
|
36
|
+
from google.cloud import bigquery_storage_v1
|
|
37
|
+
from google.cloud.bigquery_storage_v1.services.big_query_read.transports import (
|
|
38
|
+
BigQueryReadGrpcTransport,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
transport = BigQueryReadGrpcTransport(channel=grpc.insecure_channel("localhost:9060"))
|
|
42
|
+
bqstorage = bigquery_storage_v1.BigQueryReadClient(transport=transport)
|
|
43
|
+
frame = client.query_and_wait("SELECT 1 AS x").to_dataframe(bqstorage_client=bqstorage)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
For SQLAlchemy, pass the client in:
|
|
47
|
+
`create_engine("bigquery://local", connect_args={"client": client})`.
|
|
48
|
+
|
|
49
|
+
### Go
|
|
50
|
+
|
|
51
|
+
```go
|
|
52
|
+
client, err := bigquery.NewClient(ctx, "local",
|
|
53
|
+
option.WithEndpoint("http://localhost:9050/bigquery/v2/"),
|
|
54
|
+
option.WithoutAuthentication(),
|
|
55
|
+
)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Node.js
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
import { BigQuery } from "@google-cloud/bigquery";
|
|
62
|
+
|
|
63
|
+
const bigquery = new BigQuery({ projectId: "local", apiEndpoint: "http://localhost:9050" });
|
|
64
|
+
const [rows] = await bigquery.query("SELECT 1 AS x");
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Testing with pytest
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
pip install local-bigquery
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
This adds pytest fixtures that run the emulator inside the test process. There's no
|
|
74
|
+
Docker, and it starts in under a second:
|
|
75
|
+
|
|
76
|
+
- `bigquery_client`: a client connected to an emulator shared by the whole test run.
|
|
77
|
+
- `bigquery_dataset`: a new dataset for each test, deleted afterwards.
|
|
78
|
+
- `bigquery_emulator`: the emulator's `rest_url`, `grpc_address` and `project_id`.
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
def test_totals(bigquery_client, bigquery_dataset):
|
|
82
|
+
table = f"{bigquery_dataset.dataset_id}.orders"
|
|
83
|
+
bigquery_client.query_and_wait(f"CREATE TABLE {table} (customer STRING, amount INT64)")
|
|
84
|
+
bigquery_client.insert_rows_json(table, [{"customer": "a", "amount": 2}] * 3)
|
|
85
|
+
rows = bigquery_client.query_and_wait(f"SELECT SUM(amount) AS total FROM {table}")
|
|
86
|
+
assert [row.total for row in rows] == [6]
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Features
|
|
90
|
+
|
|
91
|
+
- **SQL**: GoogleSQL functions, DML including `MERGE`, DDL, scripting, procedures,
|
|
92
|
+
SQL/JavaScript UDFs, table functions, wildcard tables, `INFORMATION_SCHEMA`.
|
|
93
|
+
- **Tables**: partitioning, clustering, constraints, views, materialized views,
|
|
94
|
+
snapshots, clones, time travel, search and vector indexes.
|
|
95
|
+
- **Jobs**: query, load, copy and extract, with dry runs, cancellation and sessions.
|
|
96
|
+
- **Data**: CSV, JSON, Parquet, Avro and ORC loads, and CSV, JSON, Parquet and Avro exports.
|
|
97
|
+
Also `insert_rows_json` and the Storage Read and Write APIs.
|
|
98
|
+
- **`gs://`**: maps to `$DATA_DIR/gcs`, or to a GCS emulator set by `STORAGE_EMULATOR_HOST`.
|
|
99
|
+
- **Row access policies**: the caller is the service account that signed the request.
|
|
100
|
+
Signatures aren't checked, so any key works:
|
|
101
|
+
`service_account.Credentials.from_service_account_file("key.json", always_use_jwt_access=True)`.
|
|
102
|
+
Unsigned requests run as `CALLER`.
|
|
103
|
+
- **`EXTERNAL_QUERY`** against Postgres at `POSTGRES_URI`.
|
|
104
|
+
- **REPL**: `local-bigquery repl`. Delete all data with `local-bigquery reset`.
|
|
105
|
+
|
|
106
|
+
## Limitations
|
|
107
|
+
|
|
108
|
+
- For test-sized data on one machine. There's no authentication, and IAM policies
|
|
109
|
+
aren't enforced.
|
|
110
|
+
- Legacy SQL and BigQuery ML functions (`ML.PREDICT` and so on) aren't supported.
|
|
111
|
+
- A failed statement aborts the whole transaction.
|
|
112
|
+
- `STRING(n)` and `BYTES(n)` lengths aren't enforced.
|
|
113
|
+
- Other API methods return `501`.
|
|
114
|
+
|
|
115
|
+
Found a difference from BigQuery? [Open an issue](https://github.com/novucs/local-bigquery/issues).
|
|
116
|
+
|
|
117
|
+
## Configuration
|
|
118
|
+
|
|
119
|
+
Set these as environment variables, or pass the matching flag to `local-bigquery`.
|
|
120
|
+
|
|
121
|
+
| Variable | Flag | Default |
|
|
122
|
+
|---|---|---|
|
|
123
|
+
| `BIGQUERY_PORT` | `--port` | `9050` |
|
|
124
|
+
| `GRPC_PORT` | `--grpc-port` | `9060` |
|
|
125
|
+
| `DATA_DIR` | `--data-dir` | `/data` |
|
|
126
|
+
| `DEFAULT_PROJECT_ID` | `--project` | `local` |
|
|
127
|
+
| `DEFAULT_DATASET_ID` | `--dataset` | `local` |
|
|
128
|
+
| `CALLER` | | `user:local-bigquery@localhost` |
|
|
129
|
+
| `GROUPS` | | `{}`, e.g. `{"user:alice@example.com": ["group:team@example.com"]}` |
|
|
130
|
+
| `GCS_LOCAL_ROOT` | | `$DATA_DIR/gcs` |
|
|
131
|
+
| `STORAGE_EMULATOR_HOST` | | |
|
|
132
|
+
| `POSTGRES_CONNECTION_ID` | | `us.default` |
|
|
133
|
+
| `POSTGRES_URI` | | `postgresql://postgres:example@db:5432/postgres` |
|
|
134
|
+
|
|
135
|
+
## Development
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
uv run local-bigquery --data-dir /tmp/local-bigquery # run from source
|
|
139
|
+
uv run pytest # run the tests
|
|
140
|
+
uv run pytest --endpoint google --project <project> # run them against real BigQuery
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
|
|
145
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "local-bigquery"
|
|
3
|
+
version = "0.3.0"
|
|
4
|
+
description = "A BigQuery emulator for local development and tests"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.13,<3.14"
|
|
9
|
+
dependencies = [
|
|
10
|
+
"duckdb>=1.5.5",
|
|
11
|
+
"fastapi>=0.139",
|
|
12
|
+
"fastavro>=1.12.2",
|
|
13
|
+
"google-cloud-bigquery-storage>=2.41.0",
|
|
14
|
+
"grpcio>=1.83.0",
|
|
15
|
+
"mini-racer>=0.12.4",
|
|
16
|
+
"prompt-toolkit>=3.0.50",
|
|
17
|
+
"pyarrow>=25.0.1",
|
|
18
|
+
"pydantic-settings>=2.8.1",
|
|
19
|
+
"pygments>=2.19.1",
|
|
20
|
+
"sqlglot>=30.19",
|
|
21
|
+
"uvicorn>=0.35",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Repository = "https://github.com/novucs/local-bigquery"
|
|
26
|
+
Issues = "https://github.com/novucs/local-bigquery/issues"
|
|
27
|
+
|
|
28
|
+
[project.scripts]
|
|
29
|
+
local-bigquery = "local_bigquery.cli:main"
|
|
30
|
+
|
|
31
|
+
[project.entry-points.pytest11]
|
|
32
|
+
local_bigquery = "local_bigquery.pytest_plugin"
|
|
33
|
+
|
|
34
|
+
[build-system]
|
|
35
|
+
requires = ["hatchling"]
|
|
36
|
+
build-backend = "hatchling.build"
|
|
37
|
+
|
|
38
|
+
[tool.hatch.build.targets.sdist]
|
|
39
|
+
include = ["src"]
|
|
40
|
+
|
|
41
|
+
[dependency-groups]
|
|
42
|
+
dev = [
|
|
43
|
+
"cryptography>=49.0.0",
|
|
44
|
+
"datamodel-code-generator>=0.82.0",
|
|
45
|
+
"db-dtypes>=1.7.1",
|
|
46
|
+
"google-cloud-bigquery>=3.31.0",
|
|
47
|
+
"hypothesis>=6.168.1",
|
|
48
|
+
"pandas>=3.0.6",
|
|
49
|
+
"psycopg2-binary>=2.9.13",
|
|
50
|
+
"pytest>=8.3.5",
|
|
51
|
+
"pytest-timeout>=2.4.0",
|
|
52
|
+
"pytest-xdist>=3.8.0",
|
|
53
|
+
"sqlalchemy-bigquery>=1.13.0",
|
|
54
|
+
"testcontainers>=4.10.0",
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
[tool.pytest.ini_options]
|
|
58
|
+
xfail_strict = true
|
|
59
|
+
addopts = "-p no:warnings -n auto --dist loadgroup"
|
|
60
|
+
timeout = 30
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
from fastapi import APIRouter, Response
|
|
4
|
+
|
|
5
|
+
from local_bigquery.resource import Resource
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Router(APIRouter):
|
|
9
|
+
def add_api_route(self, path, endpoint, **kwargs):
|
|
10
|
+
super().add_api_route(
|
|
11
|
+
path, endpoint, **kwargs | {"response_model_exclude_unset": True}
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def paginate(
|
|
16
|
+
items: list, max_results: int | None, page_token: str | None
|
|
17
|
+
) -> tuple[list, str | None]:
|
|
18
|
+
start = int(page_token or 0)
|
|
19
|
+
end = len(items) if max_results is None else start + max_results
|
|
20
|
+
return items[start:end], str(end) if end < len(items) else None
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def with_rows(payload: dict, rows: list[str]) -> Response:
|
|
24
|
+
body = json.dumps(
|
|
25
|
+
{key: value for key, value in payload.items() if value is not None},
|
|
26
|
+
default=Resource.dump,
|
|
27
|
+
)
|
|
28
|
+
if rows:
|
|
29
|
+
body = f'{body[:-1]}, "rows": [{",".join(rows)}]}}'
|
|
30
|
+
return Response(body, media_type="application/json")
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
from fastapi import Header
|
|
2
|
+
|
|
3
|
+
from local_bigquery.api import Router, paginate
|
|
4
|
+
from local_bigquery.catalog import datasets
|
|
5
|
+
from local_bigquery.models import Dataset, DatasetList, DatasetListDatasetsItem
|
|
6
|
+
|
|
7
|
+
router = Router(tags=["datasets"])
|
|
8
|
+
SUMMARY_FIELDS = set(DatasetListDatasetsItem.model_fields)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@router.get("/projects/{project_id}/datasets")
|
|
12
|
+
def list_datasets(
|
|
13
|
+
project_id: str,
|
|
14
|
+
maxResults: int | None = None,
|
|
15
|
+
pageToken: str | None = None,
|
|
16
|
+
all: bool = False,
|
|
17
|
+
filter: str | None = None,
|
|
18
|
+
) -> DatasetList:
|
|
19
|
+
summaries = [
|
|
20
|
+
dataset.model_dump(include=SUMMARY_FIELDS, exclude_none=True)
|
|
21
|
+
for dataset in datasets.list_(project_id, filter, all)
|
|
22
|
+
]
|
|
23
|
+
page, token = paginate(summaries, maxResults, pageToken)
|
|
24
|
+
return DatasetList(kind="bigquery#datasetList", datasets=page, nextPageToken=token)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@router.post("/projects/{project_id}/datasets")
|
|
28
|
+
def insert_dataset(project_id: str, body: Dataset) -> Dataset:
|
|
29
|
+
return datasets.create(project_id, body)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@router.get("/projects/{project_id}/datasets/{dataset_id}")
|
|
33
|
+
def get_dataset(project_id: str, dataset_id: str) -> Dataset:
|
|
34
|
+
return datasets.get(project_id, dataset_id)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@router.patch("/projects/{project_id}/datasets/{dataset_id}")
|
|
38
|
+
def patch_dataset(
|
|
39
|
+
project_id: str,
|
|
40
|
+
dataset_id: str,
|
|
41
|
+
body: Dataset,
|
|
42
|
+
if_match: str | None = Header(None),
|
|
43
|
+
) -> Dataset:
|
|
44
|
+
return datasets.update(project_id, dataset_id, body, if_match, replace=False)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@router.put("/projects/{project_id}/datasets/{dataset_id}")
|
|
48
|
+
def update_dataset(
|
|
49
|
+
project_id: str,
|
|
50
|
+
dataset_id: str,
|
|
51
|
+
body: Dataset,
|
|
52
|
+
if_match: str | None = Header(None),
|
|
53
|
+
) -> Dataset:
|
|
54
|
+
return datasets.update(project_id, dataset_id, body, if_match, replace=True)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@router.delete("/projects/{project_id}/datasets/{dataset_id}", status_code=204)
|
|
58
|
+
def delete_dataset(project_id: str, dataset_id: str, deleteContents: bool = False):
|
|
59
|
+
datasets.delete(project_id, dataset_id, deleteContents)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from local_bigquery.api import Router
|
|
2
|
+
from local_bigquery.catalog import iam
|
|
3
|
+
from local_bigquery.models import (
|
|
4
|
+
Policy,
|
|
5
|
+
SetIamPolicyRequest,
|
|
6
|
+
TestIamPermissionsRequest,
|
|
7
|
+
TestIamPermissionsResponse,
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
router = Router(tags=["iam"])
|
|
11
|
+
RESOURCE = "/{resource:path}"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@router.post(f"{RESOURCE}:getIamPolicy")
|
|
15
|
+
def get_policy(resource: str) -> Policy:
|
|
16
|
+
return iam.get(resource)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@router.post(f"{RESOURCE}:setIamPolicy")
|
|
20
|
+
def set_policy(resource: str, body: SetIamPolicyRequest) -> Policy:
|
|
21
|
+
return iam.set_(resource, body.policy or Policy())
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@router.post(f"{RESOURCE}:testIamPermissions")
|
|
25
|
+
def test_permissions(
|
|
26
|
+
resource: str, body: TestIamPermissionsRequest
|
|
27
|
+
) -> TestIamPermissionsResponse:
|
|
28
|
+
iam.get(resource)
|
|
29
|
+
return TestIamPermissionsResponse(permissions=body.permissions or [])
|