Skip to content

Commit

Permalink
Handle duplicate timeseries index (#3243)
Browse files Browse the repository at this point in the history
* Skipped duplicate time index in CSV timeseries parser
* Throws exception in IrregularTimeSeriesIndex if the times array contains duplicate or is unordered
* Replace String join with text block in unit tests

Signed-off-by: lisrte <[email protected]>
  • Loading branch information
Lisrte authored Dec 6, 2024
1 parent 83838ca commit be489a8
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.time.Instant;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Stream;

/**
Expand All @@ -32,6 +29,11 @@ public class IrregularTimeSeriesIndex extends AbstractTimeSeriesIndex {

public IrregularTimeSeriesIndex(long[] times) {
this.times = Objects.requireNonNull(times);
for (int i = 1; i < times.length; i++) {
if (times[i] <= times[i - 1]) {
throw new IllegalArgumentException("Time list should be sorted and without duplicate values");
}
}
if (times.length == 0) {
throw new IllegalArgumentException("Empty time list");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,27 +291,25 @@ void parseToken(int i, String token) {
}

void parseLine(String[] tokens) {
for (int i = fixedColumns; i < tokens.length; i++) {
String token = tokens[i] != null ? tokens[i].trim() : "";
parseToken(i, token);
long time = parseTokenTime(tokens[0]);
if (times.isEmpty() || times.get(times.size() - 1) != time) {
for (int i = fixedColumns; i < tokens.length; i++) {
String token = tokens[i] != null ? tokens[i].trim() : "";
parseToken(i, token);
}
times.add(time);
} else {
LOGGER.warn("Row with the same time have already been read, the row will be skipped");
}

parseTokenTime(tokens);
}

void parseTokenTime(String[] tokens) {
long parseTokenTime(String token) {
TimeFormat timeFormat = timeSeriesCsvConfig.timeFormat();
switch (timeFormat) {
case DATE_TIME -> times.add(ZonedDateTime.parse(tokens[0]).toInstant().toEpochMilli());
case FRACTIONS_OF_SECOND -> {
double time = Double.parseDouble(tokens[0]) * 1000;
times.add((long) time);
}
case MILLIS -> {
double millis = Double.parseDouble(tokens[0]);
times.add((long) millis);
}
}
return switch (timeFormat) {
case DATE_TIME -> ZonedDateTime.parse(token).toInstant().toEpochMilli();
case FRACTIONS_OF_SECOND -> (long) (Double.parseDouble(token) * 1000);
case MILLIS -> (long) Double.parseDouble(token);
};
}

void reInit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,9 @@ void testEquals() {
@Test
void testContructorError() {
assertThrows(IllegalArgumentException.class, IrregularTimeSeriesIndex::create);
long[] duplicates = {0L, 1L, 1L};
assertThrows(IllegalArgumentException.class, () -> new IrregularTimeSeriesIndex(duplicates));
long[] unordered = {0L, 2L, 1L};
assertThrows(IllegalArgumentException.class, () -> new IrregularTimeSeriesIndex(unordered));
}
}
Loading

0 comments on commit be489a8

Please sign in to comment.