66logging .basicConfig (level = logging .DEBUG )
77logger = logging .getLogger (__name__ )
88
9+
10+ def test_sea_query_exec ():
11+ """
12+ Test executing a query using the SEA backend with result compression.
13+
14+ This function connects to a Databricks SQL endpoint using the SEA backend,
15+ executes a simple query with result compression enabled and disabled,
16+ and verifies that execution completes successfully.
17+ """
18+ server_hostname = os .environ .get ("DATABRICKS_SERVER_HOSTNAME" )
19+ http_path = os .environ .get ("DATABRICKS_HTTP_PATH" )
20+ access_token = os .environ .get ("DATABRICKS_TOKEN" )
21+ catalog = os .environ .get ("DATABRICKS_CATALOG" )
22+
23+ if not all ([server_hostname , http_path , access_token ]):
24+ logger .error ("Missing required environment variables." )
25+ logger .error (
26+ "Please set DATABRICKS_SERVER_HOSTNAME, DATABRICKS_HTTP_PATH, and DATABRICKS_TOKEN."
27+ )
28+ sys .exit (1 )
29+
30+ try :
31+ # Test with compression enabled
32+ logger .info ("Creating connection with LZ4 compression enabled" )
33+ connection = Connection (
34+ server_hostname = server_hostname ,
35+ http_path = http_path ,
36+ access_token = access_token ,
37+ catalog = catalog ,
38+ schema = "default" ,
39+ use_sea = True ,
40+ user_agent_entry = "SEA-Test-Client" ,
41+ use_cloud_fetch = True , # Enable cloud fetch to use compression
42+ enable_query_result_lz4_compression = True , # Enable LZ4 compression
43+ )
44+
45+ logger .info (
46+ f"Successfully opened SEA session with ID: { connection .get_session_id_hex ()} "
47+ )
48+ logger .info (f"backend type: { type (connection .session .backend )} " )
49+
50+ # Execute a simple query with compression enabled
51+ cursor = connection .cursor (arraysize = 0 , buffer_size_bytes = 0 )
52+ logger .info ("Executing query with LZ4 compression: SELECT 1 as test_value" )
53+ cursor .execute ("SELECT 1 as test_value" )
54+ logger .info ("Query with compression executed successfully" )
55+ cursor .close ()
56+ connection .close ()
57+ logger .info ("Successfully closed SEA session with compression enabled" )
58+
59+ # Test with compression disabled
60+ logger .info ("Creating connection with LZ4 compression disabled" )
61+ connection = Connection (
62+ server_hostname = server_hostname ,
63+ http_path = http_path ,
64+ access_token = access_token ,
65+ catalog = catalog ,
66+ schema = "default" ,
67+ use_sea = True ,
68+ user_agent_entry = "SEA-Test-Client" ,
69+ use_cloud_fetch = False , # Enable cloud fetch
70+ enable_query_result_lz4_compression = False , # Disable LZ4 compression
71+ )
72+
73+ logger .info (
74+ f"Successfully opened SEA session with ID: { connection .get_session_id_hex ()} "
75+ )
76+
77+ # Execute a simple query with compression disabled
78+ cursor = connection .cursor (arraysize = 0 , buffer_size_bytes = 0 )
79+ logger .info ("Executing query without compression: SELECT 1 as test_value" )
80+ cursor .execute ("SELECT 1 as test_value" )
81+ logger .info ("Query without compression executed successfully" )
82+ cursor .close ()
83+ connection .close ()
84+ logger .info ("Successfully closed SEA session with compression disabled" )
85+
86+ except Exception as e :
87+ logger .error (f"Error during SEA query execution test: { str (e )} " )
88+ import traceback
89+
90+ logger .error (traceback .format_exc ())
91+ sys .exit (1 )
92+
93+ logger .info ("SEA query execution test with compression completed successfully" )
94+
95+
996def test_sea_session ():
1097 """
1198 Test opening and closing a SEA session using the connector.
12-
99+
13100 This function connects to a Databricks SQL endpoint using the SEA backend,
14101 opens a session, and then closes it.
15-
102+
16103 Required environment variables:
17104 - DATABRICKS_SERVER_HOSTNAME: Databricks server hostname
18105 - DATABRICKS_HTTP_PATH: HTTP path for the SQL endpoint
19106 - DATABRICKS_TOKEN: Personal access token for authentication
20107 """
21-
22108 server_hostname = os .environ .get ("DATABRICKS_SERVER_HOSTNAME" )
23109 http_path = os .environ .get ("DATABRICKS_HTTP_PATH" )
24110 access_token = os .environ .get ("DATABRICKS_TOKEN" )
25111 catalog = os .environ .get ("DATABRICKS_CATALOG" )
26-
112+
27113 if not all ([server_hostname , http_path , access_token ]):
28114 logger .error ("Missing required environment variables." )
29- logger .error ("Please set DATABRICKS_SERVER_HOSTNAME, DATABRICKS_HTTP_PATH, and DATABRICKS_TOKEN." )
115+ logger .error (
116+ "Please set DATABRICKS_SERVER_HOSTNAME, DATABRICKS_HTTP_PATH, and DATABRICKS_TOKEN."
117+ )
30118 sys .exit (1 )
31-
119+
32120 logger .info (f"Connecting to { server_hostname } " )
33121 logger .info (f"HTTP Path: { http_path } " )
34122 if catalog :
35123 logger .info (f"Using catalog: { catalog } " )
36-
124+
37125 try :
38126 logger .info ("Creating connection with SEA backend..." )
39127 connection = Connection (
@@ -42,25 +130,33 @@ def test_sea_session():
42130 access_token = access_token ,
43131 catalog = catalog ,
44132 schema = "default" ,
45- use_sea = True ,
46- user_agent_entry = "SEA-Test-Client" # add custom user agent
133+ use_sea = True ,
134+ user_agent_entry = "SEA-Test-Client" , # add custom user agent
135+ )
136+
137+ logger .info (
138+ f"Successfully opened SEA session with ID: { connection .get_session_id_hex ()} "
47139 )
48-
49- logger .info (f"Successfully opened SEA session with ID: { connection .get_session_id_hex ()} " )
50140 logger .info (f"backend type: { type (connection .session .backend )} " )
51-
141+
52142 # Close the connection
53143 logger .info ("Closing the SEA session..." )
54144 connection .close ()
55145 logger .info ("Successfully closed SEA session" )
56-
146+
57147 except Exception as e :
58148 logger .error (f"Error testing SEA session: { str (e )} " )
59149 import traceback
150+
60151 logger .error (traceback .format_exc ())
61152 sys .exit (1 )
62-
153+
63154 logger .info ("SEA session test completed successfully" )
64155
156+
65157if __name__ == "__main__" :
158+ # Test session management
66159 test_sea_session ()
160+
161+ # Test query execution with compression
162+ test_sea_query_exec ()
0 commit comments