influxdata / influxdb-java

Java client for InfluxDB
MIT License
1.19k stars 478 forks source link

InfluxDBMapper.toPOJO() bug? #834

Open trdmm opened 2 years ago

trdmm commented 2 years ago

When call InfluxDBMapper.toPOJO() , if the time field type is String and the millisecond value is an integer multiple of ten, it miss the last zero. Such as: If the timestamp is 1653354743560, the result is 2022-05-24T01:12:23.56Z, it should be 2022-05-24T01:12:23.560Z?

majst01 commented 2 years ago

But this parses to the same timestamp, right

trdmm commented 2 years ago

But this parses to the same timestamp, right

I use TimeUtil.fromInfluxDBTimeFormat(2022-05-24T01:12:23.56Z), the result is 1653354743056 :cry:

majst01 commented 2 years ago

Can you write a Unit test which demonstrates this BUG please

trdmm commented 2 years ago

Can you write a Unit test which demonstrates this BUG please

OutGzjLocation:

@Data
public class OutGzjLocation {
    @TimeColumn
    @Column(name = "time")
    private String time;
    /** imgID */
    @Column(name = "imgId")
    private int imgId = -1;
}

OutGzjLocationAnother:

    @TimeColumn
    @Column(name = "time")
    private Instant time;
    /** imgID */
    @Column(name = "imgId")
    private int imgId = -1;
SelectQueryImpl query = select().from(ConstantInfluxdb.targetDatabase, "\"10602ef9\"");

QueryResult queryResult = targetInfluxDB.query(query);

// time type is String
List<OutGzjLocation> outGzjLocations = targetInfluxDBMapper.toPOJO(queryResult, OutGzjLocation.class, "10602ef9");
// time type is Instant
List<OutGzjLocationAnother> outGzjLocationAnothers = targetInfluxDBMapper.toPOJO(queryResult, OutGzjLocationAnother.class, "10602ef9");

log.info("======time type is String======");
outGzjLocations.forEach(outGzjLocation -> {
    int imgId = outGzjLocation.getImgId();
    String timeStr = outGzjLocation.getTime();
    if (imgId == 14 || imgId == 15){
        long timestamp = TimeUtil.fromInfluxDBTimeFormat(timeStr);
        log.info("Time str: {}, timestamp: {}, imgId: {}.", timeStr, timestamp,imgId);
    }
});

log.info("======time type is Instant======");
outGzjLocationAnothers.forEach(outGzjLocationAnother -> {
    Instant instant = outGzjLocationAnother.getTime();
    int imgId = outGzjLocationAnother.getImgId();
    if (imgId == 14 || imgId == 15){
        log.info("Time str: {}, timestamp: {}, imgId: {}.", instant.toString(), instant.toEpochMilli(),imgId);
    }
});

The result is:

======time type is String======
Time str: 2022-05-23T12:08:20.56Z, timestamp: 1653307700056, imgId: 14.
Time str: 2022-05-23T12:08:22.56Z, timestamp: 1653307702056, imgId: 15.
======time type is Instant======
Time str: 2022-05-23T12:08:20.560Z, timestamp: 1653307700560, imgId: 14.
Time str: 2022-05-23T12:08:22.560Z, timestamp: 1653307702560, imgId: 15.
majst01 commented 2 years ago

Hi, nice, i mean as a Pull Request referring this Issue