|
| 1 | +from dataclasses import dataclass |
| 2 | +from enum import auto |
| 3 | +from strenum import LowercaseStrEnum |
| 4 | + |
| 5 | + |
| 6 | +class StorageFormat(LowercaseStrEnum): |
| 7 | + """Storage formats for storing query results to cloud storage.""" |
| 8 | + |
| 9 | + PARQUET = auto() |
| 10 | + CSV = auto() |
| 11 | + GEOJSON = auto() |
| 12 | + |
| 13 | + |
| 14 | +@dataclass(frozen=True) |
| 15 | +class StoreResult: |
| 16 | + """Result information when a query's results are stored to cloud storage. |
| 17 | +
|
| 18 | + Attributes: |
| 19 | + result_uri: The URI or presigned URL of the stored result. |
| 20 | + size: The size of the stored result in bytes, or None if not available. |
| 21 | + """ |
| 22 | + |
| 23 | + result_uri: str |
| 24 | + size: int | None = None |
| 25 | + |
| 26 | + |
| 27 | +@dataclass |
| 28 | +class Store: |
| 29 | + """Configuration for storing query results to cloud storage. |
| 30 | +
|
| 31 | + When passed to cursor.execute(), query results will be written to cloud |
| 32 | + storage instead of being returned directly over the WebSocket connection. |
| 33 | +
|
| 34 | + Attributes: |
| 35 | + format: The storage format (parquet, csv, or geojson). Defaults to parquet. |
| 36 | + single: If True, store as a single file. If False, store as multiple files. |
| 37 | + generate_presigned_url: If True, generate a presigned URL for the result. |
| 38 | + Requires single=True. |
| 39 | + """ |
| 40 | + |
| 41 | + format: StorageFormat | None = None |
| 42 | + single: bool = False |
| 43 | + generate_presigned_url: bool = False |
| 44 | + |
| 45 | + def __post_init__(self) -> None: |
| 46 | + if self.generate_presigned_url and not self.single: |
| 47 | + raise ValueError("Presigned URL can only be generated when single=True") |
| 48 | + |
| 49 | + @classmethod |
| 50 | + def for_download(cls, format: StorageFormat | None = None) -> "Store": |
| 51 | + """Create a configuration for downloading results via a presigned URL. |
| 52 | +
|
| 53 | + This is a convenience method that creates a configuration with |
| 54 | + single file mode and presigned URL generation enabled. |
| 55 | +
|
| 56 | + Args: |
| 57 | + format: The storage format. Defaults to parquet if not specified. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + A Store configured for single-file download with presigned URL. |
| 61 | + """ |
| 62 | + return cls(format=format, single=True, generate_presigned_url=True) |
0 commit comments