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-
969def test_sea_session ():
9710"""
9811 Test opening and closing a SEA session using the connector.
99-
12+
10013 This function connects to a Databricks SQL endpoint using the SEA backend,
10114 opens a session, and then closes it.
102-
15+
10316 Required environment variables:
10417 - DATABRICKS_SERVER_HOSTNAME: Databricks server hostname
10518 - DATABRICKS_HTTP_PATH: HTTP path for the SQL endpoint
10619 - DATABRICKS_TOKEN: Personal access token for authentication
10720 """
21+
10822server_hostname = os .environ .get ("DATABRICKS_SERVER_HOSTNAME" )
10923http_path = os .environ .get ("DATABRICKS_HTTP_PATH" )
11024access_token = os .environ .get ("DATABRICKS_TOKEN" )
11125catalog = os .environ .get ("DATABRICKS_CATALOG" )
112-
26+
11327if not all ([server_hostname ,http_path ,access_token ]):
11428logger .error ("Missing required environment variables." )
115- logger .error (
116- "Please set DATABRICKS_SERVER_HOSTNAME, DATABRICKS_HTTP_PATH, and DATABRICKS_TOKEN."
117- )
29+ logger .error ("Please set DATABRICKS_SERVER_HOSTNAME, DATABRICKS_HTTP_PATH, and DATABRICKS_TOKEN." )
11830sys .exit (1 )
119-
31+
12032logger .info (f"Connecting to{ server_hostname } " )
12133logger .info (f"HTTP Path:{ http_path } " )
12234if catalog :
12335logger .info (f"Using catalog:{ catalog } " )
124-
36+
12537try :
12638logger .info ("Creating connection with SEA backend..." )
12739connection = Connection (
@@ -130,33 +42,25 @@ def test_sea_session():
13042access_token = access_token ,
13143catalog = catalog ,
13244schema = "default" ,
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 ()} "
45+ use_sea = True ,
46+ user_agent_entry = "SEA-Test-Client" # add custom user agent
13947 )
48+
49+ logger .info (f"Successfully opened SEA session with ID:{ connection .get_session_id_hex ()} " )
14050logger .info (f"backend type:{ type (connection .session .backend )} " )
141-
51+
14252# Close the connection
14353logger .info ("Closing the SEA session..." )
14454connection .close ()
14555logger .info ("Successfully closed SEA session" )
146-
56+
14757except Exception as e :
14858logger .error (f"Error testing SEA session:{ str (e )} " )
14959import traceback
150-
15160logger .error (traceback .format_exc ())
15261sys .exit (1 )
153-
62+
15463logger .info ("SEA session test completed successfully" )
15564
156-
15765if __name__ == "__main__" :
158- # Test session management
15966test_sea_session ()
160-
161- # Test query execution with compression
162- test_sea_query_exec ()