@@ -72,18 +72,53 @@ def test_sea_result_set_json_array_inline():
7272 logger .info ("Executing query for Arrow testing: SELECT * FROM range(1, 5) AS id, range(101, 105) AS value" )
7373 cursor .execute ("SELECT * FROM range(1, 5) AS id, range(101, 105) AS value" )
7474
75- # Test fetchmany_arrow
76- logger .info ("Testing fetchmany_arrow(2)..." )
77- arrow_batch = cursor .fetchmany_arrow (2 )
78- logger .info (f"Arrow batch num rows: { arrow_batch .num_rows } " )
79- logger .info (f"Arrow batch columns: { arrow_batch .column_names } " )
80- logger .info (f"Arrow batch data: { arrow_batch .to_pydict ()} " )
75+ try :
76+ # Test fetchmany_arrow
77+ logger .info ("Testing fetchmany_arrow(2)..." )
78+ arrow_batch = cursor .fetchmany_arrow (2 )
79+ logger .info (f"Arrow batch num rows: { arrow_batch .num_rows } " )
80+ logger .info (f"Arrow batch columns: { arrow_batch .column_names } " )
81+ logger .info (f"Arrow batch data: { arrow_batch .to_pydict ()} " )
82+
83+ # Test fetchall_arrow
84+ logger .info ("Testing fetchall_arrow..." )
85+ remaining_arrow_batch = cursor .fetchall_arrow ()
86+ logger .info (f"Remaining arrow batch num rows: { remaining_arrow_batch .num_rows } " )
87+ logger .info (f"Remaining arrow batch data: { remaining_arrow_batch .to_pydict ()} " )
88+ except ImportError :
89+ logger .warning ("PyArrow not installed, skipping Arrow tests" )
8190
82- # Test fetchall_arrow
83- logger .info ("Testing fetchall_arrow..." )
84- remaining_arrow_batch = cursor .fetchall_arrow ()
85- logger .info (f"Remaining arrow batch num rows: { remaining_arrow_batch .num_rows } " )
86- logger .info (f"Remaining arrow batch data: { remaining_arrow_batch .to_pydict ()} " )
91+ # Test metadata commands
92+ logger .info ("Testing metadata commands..." )
93+
94+ # Get catalogs
95+ logger .info ("Getting catalogs..." )
96+ cursor .catalogs ()
97+ catalogs = cursor .fetchall ()
98+ logger .info (f"Available catalogs: { [c .catalog for c in catalogs ]} " )
99+
100+ # Get schemas
101+ if catalog :
102+ logger .info (f"Getting schemas for catalog '{ catalog } '..." )
103+ cursor .schemas (catalog_name = catalog )
104+ schemas = cursor .fetchall ()
105+ logger .info (f"Available schemas in { catalog } : { [s .databaseName for s in schemas ]} " )
106+
107+ # Get tables for a schema
108+ if schemas :
109+ schema = schemas [0 ].databaseName
110+ logger .info (f"Getting tables for schema '{ schema } '..." )
111+ cursor .tables (catalog_name = catalog , schema_name = schema )
112+ tables = cursor .fetchall ()
113+ logger .info (f"Available tables in { schema } : { [t .tableName for t in tables ]} " )
114+
115+ # Get columns for a table
116+ if tables :
117+ table = tables [0 ].tableName
118+ logger .info (f"Getting columns for table '{ table } '..." )
119+ cursor .columns (catalog_name = catalog , schema_name = schema , table_name = table )
120+ columns = cursor .fetchall ()
121+ logger .info (f"Columns in { table } : { [c .column_name for c in columns ]} " )
87122
88123 # Close cursor and connection
89124 cursor .close ()
@@ -99,6 +134,70 @@ def test_sea_result_set_json_array_inline():
99134 logger .info ("SEA result set test with JSON_ARRAY format and INLINE disposition completed successfully" )
100135
101136
137+ def test_sea_session ():
138+ """
139+ Test opening and closing a SEA session using the connector.
140+
141+ This function connects to a Databricks SQL endpoint using the SEA backend,
142+ opens a session, and then closes it.
143+
144+ Required environment variables:
145+ - DATABRICKS_SERVER_HOSTNAME: Databricks server hostname
146+ - DATABRICKS_HTTP_PATH: HTTP path for the SQL endpoint
147+ - DATABRICKS_TOKEN: Personal access token for authentication
148+ """
149+ server_hostname = os .environ .get ("DATABRICKS_SERVER_HOSTNAME" )
150+ http_path = os .environ .get ("DATABRICKS_HTTP_PATH" )
151+ access_token = os .environ .get ("DATABRICKS_TOKEN" )
152+ catalog = os .environ .get ("DATABRICKS_CATALOG" )
153+
154+ if not all ([server_hostname , http_path , access_token ]):
155+ logger .error ("Missing required environment variables." )
156+ logger .error (
157+ "Please set DATABRICKS_SERVER_HOSTNAME, DATABRICKS_HTTP_PATH, and DATABRICKS_TOKEN."
158+ )
159+ sys .exit (1 )
160+
161+ logger .info (f"Connecting to { server_hostname } " )
162+ logger .info (f"HTTP Path: { http_path } " )
163+ if catalog :
164+ logger .info (f"Using catalog: { catalog } " )
165+
166+ try :
167+ logger .info ("Creating connection with SEA backend..." )
168+ connection = Connection (
169+ server_hostname = server_hostname ,
170+ http_path = http_path ,
171+ access_token = access_token ,
172+ catalog = catalog ,
173+ schema = "default" ,
174+ use_sea = True ,
175+ user_agent_entry = "SEA-Test-Client" , # add custom user agent
176+ )
177+
178+ logger .info (
179+ f"Successfully opened SEA session with ID: { connection .get_session_id_hex ()} "
180+ )
181+ logger .info (f"backend type: { type (connection .session .backend )} " )
182+
183+ # Close the connection
184+ logger .info ("Closing the SEA session..." )
185+ connection .close ()
186+ logger .info ("Successfully closed SEA session" )
187+
188+ except Exception as e :
189+ logger .error (f"Error testing SEA session: { str (e )} " )
190+ import traceback
191+
192+ logger .error (traceback .format_exc ())
193+ sys .exit (1 )
194+
195+ logger .info ("SEA session test completed successfully" )
196+
197+
102198if __name__ == "__main__" :
103- # Test result set implementation
199+ # Test session management
200+ test_sea_session ()
201+
202+ # Test result set implementation with metadata commands
104203 test_sea_result_set_json_array_inline ()
0 commit comments