|
| 1 | +"""Helpers to translate Databend stage metadata into OpenDAL operators.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import hashlib |
| 6 | +import json |
| 7 | +import logging |
| 8 | +import threading |
| 9 | +from typing import Any, Dict, Mapping, Tuple |
| 10 | + |
| 11 | +from databend_udf import StageLocation |
| 12 | +from opendal import Operator, exceptions as opendal_exceptions |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | +_OPERATOR_CACHE: Dict[str, Operator] = {} |
| 17 | +_CACHE_LOCK = threading.Lock() |
| 18 | + |
| 19 | + |
| 20 | +class StageConfigurationError(RuntimeError): |
| 21 | + """Raised when an unsupported or invalid stage configuration is encountered.""" |
| 22 | + |
| 23 | + |
| 24 | +def _normalize_bool(value: Any) -> bool: |
| 25 | + if isinstance(value, bool): |
| 26 | + return value |
| 27 | + if value is None: |
| 28 | + return False |
| 29 | + if isinstance(value, (int, float)): |
| 30 | + return bool(value) |
| 31 | + if isinstance(value, str): |
| 32 | + normalized = value.strip().lower() |
| 33 | + if normalized in {"1", "true", "t", "yes", "y", "on"}: |
| 34 | + return True |
| 35 | + if normalized in {"0", "false", "f", "no", "n", "off"}: |
| 36 | + return False |
| 37 | + return bool(value) |
| 38 | + |
| 39 | + |
| 40 | +def _first_present(storage: Mapping[str, Any], *keys: str) -> Any: |
| 41 | + for key in keys: |
| 42 | + if key in storage: |
| 43 | + value = storage[key] |
| 44 | + if value not in (None, "", {}): |
| 45 | + return value |
| 46 | + return None |
| 47 | + |
| 48 | + |
| 49 | +def _build_s3_options(storage: Mapping[str, Any]) -> Dict[str, Any]: |
| 50 | + bucket = _first_present(storage, "bucket", "name") |
| 51 | + if not bucket: |
| 52 | + raise StageConfigurationError("S3 stage is missing bucket configuration") |
| 53 | + |
| 54 | + region = _first_present(storage, "region") |
| 55 | + endpoint = _first_present(storage, "endpoint", "endpoint_url") |
| 56 | + access_key = _first_present(storage, "access_key_id", "aws_key_id") |
| 57 | + secret_key = _first_present(storage, "secret_access_key", "aws_secret_key") |
| 58 | + security_token = _first_present(storage, "security_token", "session_token", "aws_token") |
| 59 | + master_key = _first_present(storage, "master_key") |
| 60 | + root = _first_present(storage, "root") |
| 61 | + role_arn = _first_present(storage, "role_arn", "aws_role_arn") |
| 62 | + external_id = _first_present(storage, "external_id", "aws_external_id") |
| 63 | + virtual_host_style = storage.get("enable_virtual_host_style") |
| 64 | + disable_loader = storage.get("disable_credential_loader") |
| 65 | + |
| 66 | + options: Dict[str, Any] = {"bucket": bucket} |
| 67 | + |
| 68 | + if region: |
| 69 | + options["region"] = region |
| 70 | + else: |
| 71 | + # Databend stages may skip region when working with S3 compatible endpoints. |
| 72 | + # OpenDAL requires a region, default to us-east-1 if not provided. |
| 73 | + options["region"] = "us-east-1" |
| 74 | + |
| 75 | + if endpoint: |
| 76 | + options["endpoint"] = endpoint |
| 77 | + if access_key: |
| 78 | + options["access_key_id"] = access_key |
| 79 | + if secret_key: |
| 80 | + options["secret_access_key"] = secret_key |
| 81 | + if security_token: |
| 82 | + options["security_token"] = security_token |
| 83 | + if master_key: |
| 84 | + options["master_key"] = master_key |
| 85 | + if root: |
| 86 | + options["root"] = root |
| 87 | + if role_arn: |
| 88 | + options["role_arn"] = role_arn |
| 89 | + if external_id: |
| 90 | + options["external_id"] = external_id |
| 91 | + if virtual_host_style is not None: |
| 92 | + options["enable_virtual_host_style"] = _normalize_bool(virtual_host_style) |
| 93 | + if disable_loader is not None: |
| 94 | + options["disable_credential_loader"] = _normalize_bool(disable_loader) |
| 95 | + |
| 96 | + return options |
| 97 | + |
| 98 | + |
| 99 | +def _build_memory_options(_: Mapping[str, Any]) -> Dict[str, Any]: |
| 100 | + # Useful for local testing; not a Databend production configuration. |
| 101 | + return {} |
| 102 | + |
| 103 | + |
| 104 | +_STORAGE_BUILDERS: Dict[str, Any] = {"s3": _build_s3_options, "memory": _build_memory_options} |
| 105 | + |
| 106 | + |
| 107 | +def _cache_key(stage: StageLocation) -> str: |
| 108 | + payload = { |
| 109 | + "stage_name": stage.stage_name, |
| 110 | + "stage_type": stage.stage_type, |
| 111 | + "storage": stage.storage, |
| 112 | + } |
| 113 | + encoded = json.dumps(payload, sort_keys=True, default=str) |
| 114 | + return hashlib.sha256(encoded.encode("utf-8")).hexdigest() |
| 115 | + |
| 116 | + |
| 117 | +def _build_operator(stage: StageLocation) -> Operator: |
| 118 | + storage = stage.storage or {} |
| 119 | + storage_type = str(storage.get("type", "")).lower() |
| 120 | + |
| 121 | + if storage_type not in _STORAGE_BUILDERS: |
| 122 | + raise StageConfigurationError( |
| 123 | + f"Unsupported stage storage type '{storage_type or 'unknown'}'" |
| 124 | + ) |
| 125 | + |
| 126 | + builder = _STORAGE_BUILDERS[storage_type] |
| 127 | + options = builder(storage) |
| 128 | + logger.debug( |
| 129 | + "Creating OpenDAL operator for stage '%s' with backend '%s'", |
| 130 | + stage.stage_name, |
| 131 | + storage_type, |
| 132 | + ) |
| 133 | + try: |
| 134 | + return Operator(storage_type, **options) |
| 135 | + except opendal_exceptions.Error as exc: |
| 136 | + raise StageConfigurationError( |
| 137 | + f"Failed to construct operator for stage '{stage.stage_name}': {exc}" |
| 138 | + ) from exc |
| 139 | + |
| 140 | + |
| 141 | +def get_operator(stage: StageLocation) -> Operator: |
| 142 | + """Return a cached OpenDAL operator for the given stage.""" |
| 143 | + |
| 144 | + cache_key = _cache_key(stage) |
| 145 | + with _CACHE_LOCK: |
| 146 | + operator = _OPERATOR_CACHE.get(cache_key) |
| 147 | + if operator is None: |
| 148 | + operator = _build_operator(stage) |
| 149 | + _OPERATOR_CACHE[cache_key] = operator |
| 150 | + return operator |
| 151 | + |
| 152 | + |
| 153 | +def clear_operator_cache() -> None: |
| 154 | + """Utility that clears cached operators, primarily for testing.""" |
| 155 | + |
| 156 | + with _CACHE_LOCK: |
| 157 | + _OPERATOR_CACHE.clear() |
| 158 | + |
| 159 | + |
| 160 | +def resolve_stage_subpath(stage: StageLocation, path: str | None = None) -> str: |
| 161 | + """ |
| 162 | + Combine the stage's relative path with a user-provided path. |
| 163 | +
|
| 164 | + The resulting string is relative to the OpenDAL operator's configured root. |
| 165 | + """ |
| 166 | + |
| 167 | + def _normalize(component: str | None) -> Tuple[str, ...]: |
| 168 | + if not component: |
| 169 | + return () |
| 170 | + parts = [] |
| 171 | + for part in component.split("/"): |
| 172 | + chunk = part.strip() |
| 173 | + if not chunk or chunk == ".": |
| 174 | + continue |
| 175 | + if chunk == "..": |
| 176 | + raise ValueError("Stage paths must not contain '..'") |
| 177 | + parts.append(chunk) |
| 178 | + return tuple(parts) |
| 179 | + |
| 180 | + base_parts = _normalize(stage.relative_path) |
| 181 | + extra_parts = _normalize(path) |
| 182 | + full_parts = base_parts + extra_parts |
| 183 | + if not full_parts: |
| 184 | + return "" |
| 185 | + return "/".join(full_parts) |
| 186 | + |
| 187 | + |
| 188 | +def as_directory_path(path: str) -> str: |
| 189 | + """Ensure the provided path represents a directory for list operations.""" |
| 190 | + if not path: |
| 191 | + return "" |
| 192 | + return path if path.endswith("/") else f"{path}/" |
0 commit comments