1313from native_client import NativeClient
1414from native_client import prompt
1515
16+
1617def cleanup_test_directory (test_dir ):
1718 """Clean up test directory"""
1819 if os .path .exists (test_dir ):
1920 shutil .rmtree (test_dir )
2021
22+
2123def count_files_in_directory (test_dir , pattern = "*" ):
2224 """Count files matching pattern in directory"""
2325 if not os .path .exists (test_dir ):
2426 return 0 , []
2527 files = glob .glob (os .path .join (test_dir , "**" , pattern ), recursive = True )
2628 # Filter out directories and verification files
27- files = [f for f in files if os .path .isfile (f ) and not os .path .basename (f ).startswith ("_v_d77aa11285c22e0e1d4593a035c98c0d" )]
29+ files = [
30+ f
31+ for f in files
32+ if os .path .isfile (f )
33+ and not os .path .basename (f ).startswith ("_v_d77aa11285c22e0e1d4593a035c98c0d" )
34+ ]
2835 return len (files ), files
2936
37+
3038def test_external_temp_table_cleanup ():
3139 """Test cleanup of external location temp tables when session ends"""
32-
40+
3341 # Test directory
3442 test_dir = "/tmp/databend_test_session_cleanup"
35-
43+
3644 print ("====== Testing External Temp Table Session Cleanup ======" )
37-
45+
3846 try :
3947 # Clean up any existing test directory
4048 cleanup_test_directory (test_dir )
41-
49+
4250 # Create test directory
4351 os .makedirs (test_dir , exist_ok = True )
44-
52+
4553 # Create temp table in session and verify cleanup
4654 print ("-- Create external temp table" )
47-
55+
4856 # Session: Create temp table with external location
4957 session_db = mysql .connector .connect (
5058 host = "127.0.0.1" , user = "root" , passwd = "root" , port = "3307"
5159 )
5260 session_cursor = session_db .cursor ()
53-
61+
5462 # Create temp table with external location
55- session_cursor .execute (f"CREATE OR REPLACE TEMP TABLE temp_external (id INT, data STRING) 'fs://{ test_dir } /';" )
63+ session_cursor .execute (
64+ f"CREATE OR REPLACE TEMP TABLE temp_external (id INT, data STRING) 'fs://{ test_dir } /';"
65+ )
5666 session_cursor .fetchall ()
57-
67+
5868 # Insert some data
59- session_cursor .execute ("INSERT INTO temp_external VALUES (1, 'session'), (2, 'test');" )
69+ session_cursor .execute (
70+ "INSERT INTO temp_external VALUES (1, 'session'), (2, 'test');"
71+ )
6072 session_cursor .fetchall ()
61-
73+
6274 # Verify data exists
6375 session_cursor .execute ("SELECT COUNT(*) FROM temp_external;" )
6476 result = session_cursor .fetchone ()
6577 print (f"Records in temp_external: { result [0 ]} " )
66-
78+
6779 # Check that files were created in external location
6880 files_before_count , files_before_list = count_files_in_directory (test_dir )
6981 print (f"Files created in external location: { files_before_count } " )
70-
82+
7183 # Close session to trigger cleanup
7284 session_cursor .close ()
7385 session_db .close ()
74-
86+
7587 # Wait for potential cleanup
7688 time .sleep (2 )
77-
89+
7890 # Check if files were cleaned up
7991 files_after_count , files_after_list = count_files_in_directory (test_dir )
8092 print (f"Files after session ended: { files_after_count } " )
81-
93+
8294 # If files still exist after cleanup, print their names
8395 if files_after_count > 0 :
8496 print ("Files remaining after cleanup:" )
8597 for file_path in files_after_list :
8698 print (f" { file_path } " )
87-
99+
88100 # Test result
89101 test_passed = files_before_count > 0 and files_after_count == 0
90-
102+
91103 if test_passed :
92104 print ("✓ External temp table cleanup test PASSED" )
93105 return True
94106 else :
95107 print ("✗ External temp table cleanup test FAILED" )
96108 return False
97-
109+
98110 except Exception as e :
99111 print (f"✗ Test failed with exception: { e } " )
100112 return False
101-
113+
102114 finally :
103115 # Clean up test directory
104116 cleanup_test_directory (test_dir )
105117
118+
106119if __name__ == "__main__" :
107120 success = test_external_temp_table_cleanup ()
108- sys .exit (0 if success else 1 )
121+ sys .exit (0 if success else 1 )
0 commit comments