|
| 1 | +"""Impala adapter using impyla.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING, Any |
| 6 | + |
| 7 | +from sqlit.domains.connections.providers.adapters.base import ( |
| 8 | + ColumnInfo, |
| 9 | + CursorBasedAdapter, |
| 10 | + IndexInfo, |
| 11 | + SequenceInfo, |
| 12 | + TableInfo, |
| 13 | + TriggerInfo, |
| 14 | +) |
| 15 | +from sqlit.domains.connections.providers.registry import get_default_port |
| 16 | + |
| 17 | +if TYPE_CHECKING: |
| 18 | + from sqlit.domains.connections.domain.config import ConnectionConfig |
| 19 | + |
| 20 | + |
| 21 | +class ImpalaAdapter(CursorBasedAdapter): |
| 22 | + """Adapter for Apache Impala using impyla.""" |
| 23 | + |
| 24 | + @property |
| 25 | + def name(self) -> str: |
| 26 | + return "Impala" |
| 27 | + |
| 28 | + @property |
| 29 | + def install_extra(self) -> str: |
| 30 | + return "impala" |
| 31 | + |
| 32 | + @property |
| 33 | + def install_package(self) -> str: |
| 34 | + return "impyla" |
| 35 | + |
| 36 | + @property |
| 37 | + def driver_import_names(self) -> tuple[str, ...]: |
| 38 | + return ("impala.dbapi",) |
| 39 | + |
| 40 | + @property |
| 41 | + def supports_multiple_databases(self) -> bool: |
| 42 | + return True |
| 43 | + |
| 44 | + @property |
| 45 | + def supports_cross_database_queries(self) -> bool: |
| 46 | + return True |
| 47 | + |
| 48 | + @property |
| 49 | + def supports_stored_procedures(self) -> bool: |
| 50 | + return False |
| 51 | + |
| 52 | + @property |
| 53 | + def supports_indexes(self) -> bool: |
| 54 | + return False # Impala uses partitions, not traditional indexes |
| 55 | + |
| 56 | + @property |
| 57 | + def supports_triggers(self) -> bool: |
| 58 | + return False |
| 59 | + |
| 60 | + @property |
| 61 | + def supports_sequences(self) -> bool: |
| 62 | + return False |
| 63 | + |
| 64 | + @property |
| 65 | + def system_databases(self) -> frozenset[str]: |
| 66 | + return frozenset({"_impala_builtins"}) |
| 67 | + |
| 68 | + @property |
| 69 | + def default_schema(self) -> str: |
| 70 | + return "" |
| 71 | + |
| 72 | + def apply_database_override(self, config: ConnectionConfig, database: str) -> ConnectionConfig: |
| 73 | + """Apply a default database for unqualified queries.""" |
| 74 | + if not database: |
| 75 | + return config |
| 76 | + return config.with_endpoint(database=database) |
| 77 | + |
| 78 | + def connect(self, config: ConnectionConfig) -> Any: |
| 79 | + impala_dbapi = self._import_driver_module( |
| 80 | + "impala.dbapi", |
| 81 | + driver_name=self.name, |
| 82 | + extra_name=self.install_extra, |
| 83 | + package_name=self.install_package, |
| 84 | + ) |
| 85 | + |
| 86 | + endpoint = config.tcp_endpoint |
| 87 | + if endpoint is None: |
| 88 | + raise ValueError("Impala connections require a TCP-style endpoint.") |
| 89 | + port = int(endpoint.port or get_default_port("impala")) |
| 90 | + |
| 91 | + auth_mechanism = str(config.get_option("auth_mechanism", "NOSASL")) |
| 92 | + use_ssl = str(config.get_option("use_ssl", "false")).lower() == "true" |
| 93 | + |
| 94 | + connect_args: dict[str, Any] = { |
| 95 | + "host": endpoint.host, |
| 96 | + "port": port, |
| 97 | + "auth_mechanism": auth_mechanism, |
| 98 | + "use_ssl": use_ssl, |
| 99 | + } |
| 100 | + |
| 101 | + if endpoint.database: |
| 102 | + connect_args["database"] = endpoint.database |
| 103 | + |
| 104 | + if endpoint.username: |
| 105 | + connect_args["user"] = endpoint.username |
| 106 | + if endpoint.password: |
| 107 | + connect_args["password"] = endpoint.password |
| 108 | + |
| 109 | + connect_args.update(config.extra_options) |
| 110 | + return impala_dbapi.connect(**connect_args) |
| 111 | + |
| 112 | + def get_databases(self, conn: Any) -> list[str]: |
| 113 | + cursor = conn.cursor() |
| 114 | + cursor.execute("SHOW DATABASES") |
| 115 | + return [row[0] for row in cursor.fetchall()] |
| 116 | + |
| 117 | + def get_tables(self, conn: Any, database: str | None = None) -> list[TableInfo]: |
| 118 | + cursor = conn.cursor() |
| 119 | + if database: |
| 120 | + cursor.execute(f"SHOW TABLES IN {self.quote_identifier(database)}") |
| 121 | + else: |
| 122 | + cursor.execute("SHOW TABLES") |
| 123 | + return [("", row[0]) for row in cursor.fetchall()] |
| 124 | + |
| 125 | + def get_views(self, conn: Any, database: str | None = None) -> list[TableInfo]: |
| 126 | + # Impala doesn't distinguish views in SHOW TABLES by default |
| 127 | + # We can query from information_schema if available |
| 128 | + cursor = conn.cursor() |
| 129 | + try: |
| 130 | + if database: |
| 131 | + cursor.execute( |
| 132 | + f"SELECT table_name FROM {self.quote_identifier(database)}.information_schema.tables " |
| 133 | + "WHERE table_type = 'VIEW' ORDER BY table_name" |
| 134 | + ) |
| 135 | + else: |
| 136 | + cursor.execute( |
| 137 | + "SELECT table_name FROM information_schema.tables " |
| 138 | + "WHERE table_type = 'VIEW' ORDER BY table_name" |
| 139 | + ) |
| 140 | + return [("", row[0]) for row in cursor.fetchall()] |
| 141 | + except Exception: |
| 142 | + # information_schema might not be available |
| 143 | + return [] |
| 144 | + |
| 145 | + def get_columns( |
| 146 | + self, conn: Any, table: str, database: str | None = None, schema: str | None = None |
| 147 | + ) -> list[ColumnInfo]: |
| 148 | + cursor = conn.cursor() |
| 149 | + if database: |
| 150 | + cursor.execute(f"DESCRIBE {self.quote_identifier(database)}.{self.quote_identifier(table)}") |
| 151 | + else: |
| 152 | + cursor.execute(f"DESCRIBE {self.quote_identifier(table)}") |
| 153 | + return [ColumnInfo(name=row[0], data_type=row[1]) for row in cursor.fetchall()] |
| 154 | + |
| 155 | + def get_procedures(self, conn: Any, database: str | None = None) -> list[str]: |
| 156 | + return [] |
| 157 | + |
| 158 | + def get_indexes(self, conn: Any, database: str | None = None) -> list[IndexInfo]: |
| 159 | + return [] |
| 160 | + |
| 161 | + def get_triggers(self, conn: Any, database: str | None = None) -> list[TriggerInfo]: |
| 162 | + return [] |
| 163 | + |
| 164 | + def get_sequences(self, conn: Any, database: str | None = None) -> list[SequenceInfo]: |
| 165 | + return [] |
| 166 | + |
| 167 | + def quote_identifier(self, name: str) -> str: |
| 168 | + escaped = name.replace("`", "``") |
| 169 | + return f"`{escaped}`" |
| 170 | + |
| 171 | + def build_select_query( |
| 172 | + self, table: str, limit: int, database: str | None = None, schema: str | None = None |
| 173 | + ) -> str: |
| 174 | + if database: |
| 175 | + return f"SELECT * FROM `{database}`.`{table}` LIMIT {limit}" |
| 176 | + return f"SELECT * FROM `{table}` LIMIT {limit}" |
0 commit comments