3838 */
3939public abstract class StatsCollectingSingleFileWriter <T , R > extends SingleFileWriter <T , R > {
4040
41+ private final RowType rowType ;
4142 private final SimpleStatsProducer statsProducer ;
42- private final SimpleColStats [] noneStats ;
43+ private final boolean isStatsDisabled ;
44+ private final boolean statsRequirePerRecord ;
4345
4446 public StatsCollectingSingleFileWriter (
4547 FileIO fileIO ,
4648 FormatWriterFactory factory ,
4749 Path path ,
4850 Function <T , InternalRow > converter ,
49- RowType writeSchema ,
51+ RowType rowType ,
5052 SimpleStatsProducer statsProducer ,
5153 String compression ,
5254 boolean asyncWrite ) {
5355 super (fileIO , factory , path , converter , compression , asyncWrite );
56+ this .rowType = rowType ;
5457 this .statsProducer = statsProducer ;
55- if (statsProducer .isStatsDisabled ()) {
56- this .noneStats =
57- IntStream .range (0 , writeSchema .getFieldCount ())
58- .mapToObj (i -> SimpleColStats .NONE )
59- .toArray (SimpleColStats []::new );
60- } else {
61- this .noneStats = null ;
62- }
58+ this .isStatsDisabled = statsProducer .isStatsDisabled ();
59+ this .statsRequirePerRecord = statsProducer .requirePerRecord ();
6360 }
6461
6562 @ Override
6663 public void write (T record ) throws IOException {
6764 InternalRow rowData = writeImpl (record );
68- if (noneStats == null ) {
65+ if (! isStatsDisabled && statsRequirePerRecord ) {
6966 statsProducer .collect (rowData );
7067 }
7168 }
7269
7370 @ Override
7471 public void writeBundle (BundleRecords bundle ) throws IOException {
75- if (statsProducer . requirePerRecord () ) {
72+ if (statsRequirePerRecord ) {
7673 throw new IllegalArgumentException (
7774 String .format (
7875 "Can't write bundle for %s, we may lose all the statistical information." ,
@@ -83,8 +80,10 @@ public void writeBundle(BundleRecords bundle) throws IOException {
8380
8481 public SimpleColStats [] fieldStats (long fileSize ) throws IOException {
8582 Preconditions .checkState (closed , "Cannot access metric unless the writer is closed." );
86- if (noneStats != null ) {
87- return noneStats ;
83+ if (isStatsDisabled ) {
84+ return IntStream .range (0 , rowType .getFieldCount ())
85+ .mapToObj (i -> SimpleColStats .NONE )
86+ .toArray (SimpleColStats []::new );
8887 }
8988
9089 return statsProducer .extract (fileIO , path , fileSize );
0 commit comments