|
| 1 | +""" |
| 2 | +Test for SEA asynchronous query execution functionality. |
| 3 | +""" |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import logging |
| 7 | +import time |
| 8 | +from databricks.sql.client import Connection |
| 9 | +from databricks.sql.backend.types import CommandState |
| 10 | + |
| 11 | +logging.basicConfig(level=logging.INFO) |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +def test_sea_async_query_with_cloud_fetch(): |
| 16 | + """ |
| 17 | + Test executing a query asynchronously using the SEA backend with cloud fetch enabled. |
| 18 | +
|
| 19 | + This function connects to a Databricks SQL endpoint using the SEA backend, |
| 20 | + executes a simple query asynchronously with cloud fetch enabled, and verifies that execution completes successfully. |
| 21 | + """ |
| 22 | + server_hostname = os.environ.get("DATABRICKS_SERVER_HOSTNAME") |
| 23 | + http_path = os.environ.get("DATABRICKS_HTTP_PATH") |
| 24 | + access_token = os.environ.get("DATABRICKS_TOKEN") |
| 25 | + catalog = os.environ.get("DATABRICKS_CATALOG") |
| 26 | + |
| 27 | + if not all([server_hostname, http_path, access_token]): |
| 28 | + logger.error("Missing required environment variables.") |
| 29 | + logger.error( |
| 30 | + "Please set DATABRICKS_SERVER_HOSTNAME, DATABRICKS_HTTP_PATH, and DATABRICKS_TOKEN." |
| 31 | + ) |
| 32 | + return False |
| 33 | + |
| 34 | + try: |
| 35 | + # Create connection with cloud fetch enabled |
| 36 | + logger.info( |
| 37 | + "Creating connection for asynchronous query execution with cloud fetch enabled" |
| 38 | + ) |
| 39 | + connection = Connection( |
| 40 | + server_hostname=server_hostname, |
| 41 | + http_path=http_path, |
| 42 | + access_token=access_token, |
| 43 | + catalog=catalog, |
| 44 | + schema="default", |
| 45 | + use_sea=True, |
| 46 | + user_agent_entry="SEA-Test-Client", |
| 47 | + use_cloud_fetch=True, |
| 48 | + ) |
| 49 | + |
| 50 | + logger.info( |
| 51 | + f"Successfully opened SEA session with ID: {connection.get_session_id_hex()}" |
| 52 | + ) |
| 53 | + |
| 54 | + # Execute a simple query asynchronously |
| 55 | + cursor = connection.cursor() |
| 56 | + logger.info( |
| 57 | + "Executing asynchronous query with cloud fetch: SELECT 1 as test_value" |
| 58 | + ) |
| 59 | + cursor.execute_async("SELECT 1 as test_value") |
| 60 | + logger.info( |
| 61 | + "Asynchronous query submitted successfully with cloud fetch enabled" |
| 62 | + ) |
| 63 | + |
| 64 | + # Check query state |
| 65 | + logger.info("Checking query state...") |
| 66 | + while cursor.is_query_pending(): |
| 67 | + logger.info("Query is still pending, waiting...") |
| 68 | + time.sleep(1) |
| 69 | + |
| 70 | + logger.info("Query is no longer pending, getting results...") |
| 71 | + cursor.get_async_execution_result() |
| 72 | + logger.info( |
| 73 | + "Successfully retrieved asynchronous query results with cloud fetch enabled" |
| 74 | + ) |
| 75 | + |
| 76 | + # Close resources |
| 77 | + cursor.close() |
| 78 | + connection.close() |
| 79 | + logger.info("Successfully closed SEA session") |
| 80 | + |
| 81 | + return True |
| 82 | + |
| 83 | + except Exception as e: |
| 84 | + logger.error( |
| 85 | + f"Error during SEA asynchronous query execution test with cloud fetch: {str(e)}" |
| 86 | + ) |
| 87 | + import traceback |
| 88 | + |
| 89 | + logger.error(traceback.format_exc()) |
| 90 | + return False |
| 91 | + |
| 92 | + |
| 93 | +def test_sea_async_query_without_cloud_fetch(): |
| 94 | + """ |
| 95 | + Test executing a query asynchronously using the SEA backend with cloud fetch disabled. |
| 96 | +
|
| 97 | + This function connects to a Databricks SQL endpoint using the SEA backend, |
| 98 | + executes a simple query asynchronously with cloud fetch disabled, and verifies that execution completes successfully. |
| 99 | + """ |
| 100 | + server_hostname = os.environ.get("DATABRICKS_SERVER_HOSTNAME") |
| 101 | + http_path = os.environ.get("DATABRICKS_HTTP_PATH") |
| 102 | + access_token = os.environ.get("DATABRICKS_TOKEN") |
| 103 | + catalog = os.environ.get("DATABRICKS_CATALOG") |
| 104 | + |
| 105 | + if not all([server_hostname, http_path, access_token]): |
| 106 | + logger.error("Missing required environment variables.") |
| 107 | + logger.error( |
| 108 | + "Please set DATABRICKS_SERVER_HOSTNAME, DATABRICKS_HTTP_PATH, and DATABRICKS_TOKEN." |
| 109 | + ) |
| 110 | + return False |
| 111 | + |
| 112 | + try: |
| 113 | + # Create connection with cloud fetch disabled |
| 114 | + logger.info( |
| 115 | + "Creating connection for asynchronous query execution with cloud fetch disabled" |
| 116 | + ) |
| 117 | + connection = Connection( |
| 118 | + server_hostname=server_hostname, |
| 119 | + http_path=http_path, |
| 120 | + access_token=access_token, |
| 121 | + catalog=catalog, |
| 122 | + schema="default", |
| 123 | + use_sea=True, |
| 124 | + user_agent_entry="SEA-Test-Client", |
| 125 | + use_cloud_fetch=False, |
| 126 | + enable_query_result_lz4_compression=False, |
| 127 | + ) |
| 128 | + |
| 129 | + logger.info( |
| 130 | + f"Successfully opened SEA session with ID: {connection.get_session_id_hex()}" |
| 131 | + ) |
| 132 | + |
| 133 | + # Execute a simple query asynchronously |
| 134 | + cursor = connection.cursor() |
| 135 | + logger.info( |
| 136 | + "Executing asynchronous query without cloud fetch: SELECT 1 as test_value" |
| 137 | + ) |
| 138 | + cursor.execute_async("SELECT 1 as test_value") |
| 139 | + logger.info( |
| 140 | + "Asynchronous query submitted successfully with cloud fetch disabled" |
| 141 | + ) |
| 142 | + |
| 143 | + # Check query state |
| 144 | + logger.info("Checking query state...") |
| 145 | + while cursor.is_query_pending(): |
| 146 | + logger.info("Query is still pending, waiting...") |
| 147 | + time.sleep(1) |
| 148 | + |
| 149 | + logger.info("Query is no longer pending, getting results...") |
| 150 | + cursor.get_async_execution_result() |
| 151 | + logger.info( |
| 152 | + "Successfully retrieved asynchronous query results with cloud fetch disabled" |
| 153 | + ) |
| 154 | + |
| 155 | + # Close resources |
| 156 | + cursor.close() |
| 157 | + connection.close() |
| 158 | + logger.info("Successfully closed SEA session") |
| 159 | + |
| 160 | + return True |
| 161 | + |
| 162 | + except Exception as e: |
| 163 | + logger.error( |
| 164 | + f"Error during SEA asynchronous query execution test without cloud fetch: {str(e)}" |
| 165 | + ) |
| 166 | + import traceback |
| 167 | + |
| 168 | + logger.error(traceback.format_exc()) |
| 169 | + return False |
| 170 | + |
| 171 | + |
| 172 | +def test_sea_async_query_exec(): |
| 173 | + """ |
| 174 | + Run both asynchronous query tests and return overall success. |
| 175 | + """ |
| 176 | + with_cloud_fetch_success = test_sea_async_query_with_cloud_fetch() |
| 177 | + logger.info( |
| 178 | + f"Asynchronous query with cloud fetch: {'✅ PASSED' if with_cloud_fetch_success else '❌ FAILED'}" |
| 179 | + ) |
| 180 | + |
| 181 | + without_cloud_fetch_success = test_sea_async_query_without_cloud_fetch() |
| 182 | + logger.info( |
| 183 | + f"Asynchronous query without cloud fetch: {'✅ PASSED' if without_cloud_fetch_success else '❌ FAILED'}" |
| 184 | + ) |
| 185 | + |
| 186 | + return with_cloud_fetch_success and without_cloud_fetch_success |
| 187 | + |
| 188 | + |
| 189 | +if __name__ == "__main__": |
| 190 | + success = test_sea_async_query_exec() |
| 191 | + sys.exit(0 if success else 1) |
0 commit comments