@@ -403,6 +403,59 @@ public void writeNonAlignedWithTabletWithNullValue() {
403403 }
404404 }
405405
406+ @ Test
407+ public void writeNonAlignedWithTabletWithNegativeTimestamps () {
408+ setEnv (100 , 30 );
409+ try (TsFileWriter tsFileWriter = new TsFileWriter (f )) {
410+ measurementSchemas .add (new MeasurementSchema ("s1" , TSDataType .TEXT , TSEncoding .PLAIN ));
411+ measurementSchemas .add (new MeasurementSchema ("s2" , TSDataType .STRING , TSEncoding .PLAIN ));
412+ measurementSchemas .add (new MeasurementSchema ("s3" , TSDataType .BLOB , TSEncoding .PLAIN ));
413+ measurementSchemas .add (new MeasurementSchema ("s4" , TSDataType .DATE , TSEncoding .PLAIN ));
414+
415+ // register nonAligned timeseries
416+ tsFileWriter .registerTimeseries (new Path (deviceId ), measurementSchemas );
417+
418+ Tablet tablet = new Tablet (deviceId , measurementSchemas );
419+ long [] timestamps = tablet .timestamps ;
420+ Object [] values = tablet .values ;
421+ tablet .initBitMaps ();
422+ int sensorNum = measurementSchemas .size ();
423+ long startTime = -100 ;
424+ for (long r = 0 ; r < 10000 ; r ++) {
425+ int row = tablet .rowSize ++;
426+ timestamps [row ] = startTime ++;
427+ for (int i = 0 ; i < sensorNum - 1 ; i ++) {
428+ if (i == 1 && r > 1000 ) {
429+ tablet .bitMaps [i ].mark ((int ) r % tablet .getMaxRowNumber ());
430+ continue ;
431+ }
432+ Binary [] textSensor = (Binary []) values [i ];
433+ textSensor [row ] = new Binary ("testString........." , TSFileConfig .STRING_CHARSET );
434+ }
435+ if (r > 1000 ) {
436+ tablet .bitMaps [sensorNum - 1 ].mark ((int ) r % tablet .getMaxRowNumber ());
437+ } else {
438+ LocalDate [] dateSensor = (LocalDate []) values [sensorNum - 1 ];
439+ dateSensor [row ] = LocalDate .of (2024 , 4 , 1 );
440+ }
441+ // write
442+ if (tablet .rowSize == tablet .getMaxRowNumber ()) {
443+ tsFileWriter .write (tablet );
444+ tablet .reset ();
445+ }
446+ }
447+ // write
448+ if (tablet .rowSize != 0 ) {
449+ tsFileWriter .write (tablet );
450+ tablet .reset ();
451+ }
452+
453+ } catch (Throwable e ) {
454+ e .printStackTrace ();
455+ Assert .fail ("Meet errors in test: " + e .getMessage ());
456+ }
457+ }
458+
406459 @ Test
407460 public void writeAlignedWithTabletWithNullValue () {
408461 setEnv (100 , 30 );
@@ -456,6 +509,59 @@ public void writeAlignedWithTabletWithNullValue() {
456509 }
457510 }
458511
512+ @ Test
513+ public void writeDataToTabletsWithNegativeTimestamps () {
514+ setEnv (100 , 30 );
515+ try (TsFileWriter tsFileWriter = new TsFileWriter (f )) {
516+ measurementSchemas .add (new MeasurementSchema ("s1" , TSDataType .TEXT , TSEncoding .PLAIN ));
517+ measurementSchemas .add (new MeasurementSchema ("s2" , TSDataType .STRING , TSEncoding .PLAIN ));
518+ measurementSchemas .add (new MeasurementSchema ("s3" , TSDataType .BLOB , TSEncoding .PLAIN ));
519+ measurementSchemas .add (new MeasurementSchema ("s4" , TSDataType .DATE , TSEncoding .PLAIN ));
520+
521+ // register aligned timeseries
522+ tsFileWriter .registerAlignedTimeseries (new Path (deviceId ), measurementSchemas );
523+
524+ Tablet tablet = new Tablet (deviceId , measurementSchemas );
525+ long [] timestamps = tablet .timestamps ;
526+ Object [] values = tablet .values ;
527+ tablet .initBitMaps ();
528+ int sensorNum = measurementSchemas .size ();
529+ long startTime = -1000 ;
530+ for (long r = 0 ; r < 10000 ; r ++) {
531+ int row = tablet .rowSize ++;
532+ timestamps [row ] = startTime ++;
533+ for (int i = 0 ; i < sensorNum - 1 ; i ++) {
534+ if (i == 1 && r > 1000 ) {
535+ tablet .bitMaps [i ].mark ((int ) r % tablet .getMaxRowNumber ());
536+ continue ;
537+ }
538+ Binary [] textSensor = (Binary []) values [i ];
539+ textSensor [row ] = new Binary ("testString........." , TSFileConfig .STRING_CHARSET );
540+ }
541+ if (r > 1000 ) {
542+ tablet .bitMaps [sensorNum - 1 ].mark ((int ) r % tablet .getMaxRowNumber ());
543+ } else {
544+ LocalDate [] dateSensor = (LocalDate []) values [sensorNum - 1 ];
545+ dateSensor [row ] = LocalDate .of (2024 , 4 , 1 );
546+ }
547+ // write
548+ if (tablet .rowSize == tablet .getMaxRowNumber ()) {
549+ tsFileWriter .writeAligned (tablet );
550+ tablet .reset ();
551+ }
552+ }
553+ // write
554+ if (tablet .rowSize != 0 ) {
555+ tsFileWriter .writeAligned (tablet );
556+ tablet .reset ();
557+ }
558+
559+ } catch (Throwable e ) {
560+ e .printStackTrace ();
561+ Assert .fail ("Meet errors in test: " + e .getMessage ());
562+ }
563+ }
564+
459565 /** Write an empty page and then write a nonEmpty page. */
460566 @ Test
461567 public void writeAlignedTimeseriesWithEmptyPage () throws IOException , WriteProcessException {
0 commit comments