|
| 1 | +"""AWS Athena adapter using pyathena.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING, Any |
| 6 | + |
| 7 | +from .base import CursorBasedAdapter, import_driver_module, ColumnInfo, IndexInfo, TriggerInfo, SequenceInfo, TableInfo |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from ...config import ConnectionConfig |
| 11 | + |
| 12 | + |
| 13 | +class AthenaAdapter(CursorBasedAdapter): |
| 14 | + """Adapter for AWS Athena.""" |
| 15 | + |
| 16 | + @classmethod |
| 17 | + def badge_label(cls) -> str: |
| 18 | + return "Athena" |
| 19 | + |
| 20 | + @property |
| 21 | + def name(self) -> str: |
| 22 | + return "Athena" |
| 23 | + |
| 24 | + @property |
| 25 | + def install_extra(self) -> str: |
| 26 | + return "athena" |
| 27 | + |
| 28 | + @property |
| 29 | + def install_package(self) -> str: |
| 30 | + return "pyathena" |
| 31 | + |
| 32 | + @property |
| 33 | + def driver_import_names(self) -> tuple[str, ...]: |
| 34 | + return ("pyathena",) |
| 35 | + |
| 36 | + @property |
| 37 | + def supports_multiple_databases(self) -> bool: |
| 38 | + return True |
| 39 | + |
| 40 | + @property |
| 41 | + def supports_stored_procedures(self) -> bool: |
| 42 | + return False |
| 43 | + |
| 44 | + @property |
| 45 | + def supports_triggers(self) -> bool: |
| 46 | + return False |
| 47 | + |
| 48 | + @property |
| 49 | + def supports_indexes(self) -> bool: |
| 50 | + return False |
| 51 | + |
| 52 | + @property |
| 53 | + def supports_cross_database_queries(self) -> bool: |
| 54 | + """Athena supports cross-database queries using database.table syntax.""" |
| 55 | + return True |
| 56 | + |
| 57 | + @property |
| 58 | + def system_databases(self) -> frozenset[str]: |
| 59 | + """Athena system databases to exclude from user listings.""" |
| 60 | + return frozenset({"information_schema"}) |
| 61 | + |
| 62 | + @property |
| 63 | + def default_schema(self) -> str: |
| 64 | + return "default" |
| 65 | + |
| 66 | + def connect(self, config: ConnectionConfig) -> Any: |
| 67 | + """Connect to AWS Athena.""" |
| 68 | + pyathena = import_driver_module( |
| 69 | + "pyathena", |
| 70 | + driver_name=self.name, |
| 71 | + extra_name=self.install_extra, |
| 72 | + package_name=self.install_package, |
| 73 | + ) |
| 74 | + |
| 75 | + auth_method = config.options.get("athena_auth_method", "profile") |
| 76 | + |
| 77 | + connect_args = { |
| 78 | + "region_name": config.options.get("athena_region_name", "us-east-1"), |
| 79 | + "s3_staging_dir": config.options.get("athena_s3_staging_dir"), |
| 80 | + "schema_name": config.database or "default", |
| 81 | + } |
| 82 | + |
| 83 | + if auth_method == "keys": |
| 84 | + connect_args["aws_access_key_id"] = config.username |
| 85 | + connect_args["aws_secret_access_key"] = config.password |
| 86 | + else: |
| 87 | + connect_args["profile_name"] = config.options.get("athena_profile_name", "default") |
| 88 | + |
| 89 | + # Optional WorkGroup |
| 90 | + if "athena_work_group" in config.options: |
| 91 | + connect_args["work_group"] = config.options["athena_work_group"] |
| 92 | + |
| 93 | + return pyathena.connect(**connect_args) |
| 94 | + |
| 95 | + def get_databases(self, conn: Any) -> list[str]: |
| 96 | + """Get list of databases (schemas in Athena).""" |
| 97 | + cursor = conn.cursor() |
| 98 | + cursor.execute("SHOW DATABASES") |
| 99 | + return [row[0] for row in cursor.fetchall()] |
| 100 | + |
| 101 | + def get_tables(self, conn: Any, database: str | None = None) -> list[TableInfo]: |
| 102 | + """Get list of tables.""" |
| 103 | + cursor = conn.cursor() |
| 104 | + if database: |
| 105 | + cursor.execute(f"SHOW TABLES IN {database}") |
| 106 | + else: |
| 107 | + cursor.execute("SHOW TABLES") |
| 108 | + return [(database or self.default_schema, row[0]) for row in cursor.fetchall()] |
| 109 | + |
| 110 | + def get_views(self, conn: Any, database: str | None = None) -> list[TableInfo]: |
| 111 | + """Get list of views.""" |
| 112 | + cursor = conn.cursor() |
| 113 | + if database: |
| 114 | + cursor.execute(f"SHOW VIEWS IN {database}") |
| 115 | + else: |
| 116 | + cursor.execute("SHOW VIEWS") |
| 117 | + return [(database or self.default_schema, row[0]) for row in cursor.fetchall()] |
| 118 | + |
| 119 | + def get_columns( |
| 120 | + self, conn: Any, table: str, database: str | None = None, schema: str | None = None |
| 121 | + ) -> list[ColumnInfo]: |
| 122 | + """Get columns for a table or view.""" |
| 123 | + cursor = conn.cursor() |
| 124 | + |
| 125 | + target_db = database or schema or self.default_schema |
| 126 | + full_table = f"{target_db}.{table}" |
| 127 | + cursor.execute(f"DESCRIBE {full_table}") |
| 128 | + |
| 129 | + columns = [] |
| 130 | + rows = cursor.fetchall() |
| 131 | + for row in rows: |
| 132 | + # A table row element looks like this: ('col_name \tstring \tfrom deserializer ',) |
| 133 | + # A view row element looks like this: ('col_name \tstring ',) |
| 134 | + col_name, data_type = [e.strip() for e in row[0].split("\t")[:2]] |
| 135 | + columns.append(ColumnInfo(name=col_name, data_type=data_type, is_primary_key=False)) |
| 136 | + |
| 137 | + return columns |
| 138 | + |
| 139 | + def quote_identifier(self, name: str) -> str: |
| 140 | + return name |
| 141 | + |
| 142 | + def build_select_query(self, table: str, limit: int, database: str | None = None, schema: str | None = None) -> str: |
| 143 | + """Build SELECT LIMIT query.""" |
| 144 | + target_db = database or schema or self.default_schema |
| 145 | + return f"SELECT * FROM {target_db}.{table} LIMIT {limit}" |
| 146 | + |
| 147 | + def get_procedures(self, conn: Any, database: str | None = None) -> list[str]: |
| 148 | + return [] |
| 149 | + |
| 150 | + def get_indexes(self, conn: Any, database: str | None = None) -> list[IndexInfo]: |
| 151 | + return [] |
| 152 | + |
| 153 | + def get_triggers(self, conn: Any, database: str | None = None) -> list[TriggerInfo]: |
| 154 | + return [] |
| 155 | + |
| 156 | + def get_sequences(self, conn: Any, database: str | None = None) -> list[SequenceInfo]: |
| 157 | + return [] |
0 commit comments