@@ -392,10 +392,10 @@ public void testOptimizeRewritesTable()
392392
393393 for (int i = 0 ; i < 3 ; i ++) {
394394 Set <String > initialFiles = getActiveFiles (tableName );
395- computeActual ("ALTER TABLE " + tableName + " EXECUTE OPTIMIZE" );
395+ computeActual (withSingleWriterPerTask ( getSession ()), "ALTER TABLE " + tableName + " EXECUTE OPTIMIZE" );
396396 Set <String > filesAfterOptimize = getActiveFiles (tableName );
397397 assertThat (filesAfterOptimize )
398- .hasSizeBetween ( 1 , workerCount )
398+ .hasSize ( 1 )
399399 .containsExactlyElementsOf (initialFiles );
400400 }
401401
@@ -1949,18 +1949,25 @@ public void testOptimize()
19491949 // Verify we have sufficiently many test rows with respect to worker count.
19501950 .hasSizeGreaterThan (workerCount );
19511951
1952- computeActual ("ALTER TABLE " + tableName + " EXECUTE OPTIMIZE" );
1952+ computeActual (withSingleWriterPerTask ( getSession ()), "ALTER TABLE " + tableName + " EXECUTE OPTIMIZE" );
19531953 assertThat (query ("SELECT sum(key), listagg(value, ' ') WITHIN GROUP (ORDER BY key) FROM " + tableName ))
19541954 .matches ("VALUES (BIGINT '65', VARCHAR 'eleven zwölf trzynaście quatorze пʼятнадцять')" );
19551955 Set <String > updatedFiles = getActiveFiles (tableName );
19561956 assertThat (updatedFiles )
1957- .hasSizeBetween ( 1 , workerCount )
1957+ .hasSize ( 1 )
19581958 .doesNotContainAnyElementsOf (initialFiles );
19591959 // No files should be removed (this is VACUUM's job)
19601960 assertThat (getAllDataFilesFromTableDirectory (tableName )).isEqualTo (union (initialFiles , updatedFiles ));
19611961
19621962 // optimize with low retention threshold, nothing should change
1963- computeActual ("ALTER TABLE " + tableName + " EXECUTE OPTIMIZE (file_size_threshold => '33B')" );
1963+ MaterializedResult lowThresholdResult = computeActual (withSingleWriterPerTask (getSession ()), "ALTER TABLE " + tableName + " EXECUTE OPTIMIZE (file_size_threshold => '33B')" );
1964+ Map <String , Long > lowThresholdMetrics = lowThresholdResult .getMaterializedRows ().stream ()
1965+ .collect (ImmutableMap .toImmutableMap (
1966+ row -> (String ) row .getField (0 ),
1967+ row -> (Long ) row .getField (1 )));
1968+ assertThat (lowThresholdMetrics .get ("rewritten_data_files_count" )).isEqualTo (0L );
1969+ assertThat (lowThresholdMetrics .get ("added_data_files_count" )).isEqualTo (0L );
1970+ assertThat (lowThresholdMetrics .get ("removed_delete_files_count" )).isEqualTo (0L );
19641971 assertThat (query ("SELECT sum(key), listagg(value, ' ') WITHIN GROUP (ORDER BY key) FROM " + tableName ))
19651972 .matches ("VALUES (BIGINT '65', VARCHAR 'eleven zwölf trzynaście quatorze пʼятнадцять')" );
19661973 assertThat (getActiveFiles (tableName )).isEqualTo (updatedFiles );
@@ -2005,14 +2012,22 @@ public void testOptimizeWithPartitionedTable()
20052012 Set <String > initialFiles = getActiveFiles (tableName );
20062013 assertThat (initialFiles ).hasSize (9 );
20072014
2008- computeActual ("ALTER TABLE " + tableName + " EXECUTE OPTIMIZE" );
2015+ MaterializedResult optimizeResult = computeActual (withSingleWriterPerTask (getSession ()), "ALTER TABLE " + tableName + " EXECUTE OPTIMIZE" );
2016+ // Verify optimize metrics
2017+ Map <String , Long > metrics = optimizeResult .getMaterializedRows ().stream ()
2018+ .collect (ImmutableMap .toImmutableMap (
2019+ row -> (String ) row .getField (0 ),
2020+ row -> (Long ) row .getField (1 )));
2021+ assertThat (metrics .get ("rewritten_data_files_count" )).isEqualTo (3L );
2022+ assertThat (metrics .get ("added_data_files_count" )).isEqualTo (1L );
2023+ assertThat (metrics .get ("removed_delete_files_count" )).isEqualTo (0L );
20092024
20102025 assertThat (query ("SELECT sum(key), listagg(value, ' ') WITHIN GROUP (ORDER BY value) FROM " + tableName ))
20112026 .matches ("VALUES (BIGINT '508', VARCHAR 'ONE Three four one one one tHrEe three two')" );
20122027
20132028 Set <String > updatedFiles = getActiveFiles (tableName );
20142029 assertThat (updatedFiles )
2015- .hasSizeBetween ( 7 , initialFiles . size () );
2030+ .hasSize ( 7 );
20162031 assertThat (getAllDataFilesFromTableDirectory (tableName )).isEqualTo (union (initialFiles , updatedFiles ));
20172032 }
20182033 finally {
@@ -2046,7 +2061,15 @@ public void testOptimizeWithEnforcedRepartitioning()
20462061 Set <String > initialFiles = getActiveFiles (tableName , currentSession );
20472062 assertThat (initialFiles ).hasSize (10 );
20482063
2049- computeActual (currentSession , "ALTER TABLE " + tableName + " EXECUTE OPTIMIZE" );
2064+ MaterializedResult optimizeResult = computeActual (withSingleWriterPerTask (getSession ()), "ALTER TABLE " + tableName + " EXECUTE OPTIMIZE" );
2065+
2066+ Map <String , Long > metrics = optimizeResult .getMaterializedRows ().stream ()
2067+ .collect (ImmutableMap .toImmutableMap (
2068+ row -> (String ) row .getField (0 ),
2069+ row -> (Long ) row .getField (1 )));
2070+ assertThat (metrics .get ("rewritten_data_files_count" )).isEqualTo (9L );
2071+ assertThat (metrics .get ("added_data_files_count" )).isEqualTo (2L );
2072+ assertThat (metrics .get ("removed_delete_files_count" )).isEqualTo (0L );
20502073
20512074 assertThat (query (currentSession , "SELECT sum(key), listagg(value, ' ') WITHIN GROUP (ORDER BY value) FROM " + tableName ))
20522075 .matches ("VALUES (BIGINT '55', VARCHAR 'one one one one one one one three two two')" );
@@ -2144,7 +2167,7 @@ public void testOptimizeUsingForcedPartitioning()
21442167 Set <String > initialFiles = getActiveFiles (tableName );
21452168 assertThat (initialFiles ).hasSize (10 );
21462169
2147- computeActual ("ALTER TABLE " + tableName + " EXECUTE OPTIMIZE" );
2170+ computeActual (withSingleWriterPerTask ( getSession ()), "ALTER TABLE " + tableName + " EXECUTE OPTIMIZE" );
21482171
21492172 assertThat (query ("SELECT " +
21502173 "sum(value1), " +
@@ -2878,6 +2901,13 @@ private Set<String> getActiveFiles(String tableName, Session session)
28782901 .collect (toImmutableSet ());
28792902 }
28802903
2904+ private Session withSingleWriterPerTask (Session session )
2905+ {
2906+ return Session .builder (session )
2907+ .setSystemProperty ("task_min_writer_count" , "1" )
2908+ .build ();
2909+ }
2910+
28812911 private Set <String > getAllDataFilesFromTableDirectory (String tableName )
28822912 {
28832913 return getTableFiles (tableName ).stream ()
0 commit comments