Skip to content

Commit

Permalink
Merge pull request #3340 from ingef/feature/speedup-date-statistics
Browse files Browse the repository at this point in the history
speedup DateColumnStatsCollector:
  • Loading branch information
awildturtok authored Mar 11, 2024
2 parents fe16a40 + cf04337 commit 21cc4c0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
import com.bakdata.conquery.models.common.daterange.CDateRange;
import com.bakdata.conquery.models.query.PrintSettings;
import com.bakdata.conquery.models.types.ResultType;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import lombok.Getter;
import lombok.ToString;

@Getter
public class DateColumnStatsCollector extends ColumnStatsCollector {

private final SortedMap<String, Integer> quarterCounts = new TreeMap<>();
private final SortedMap<String, Integer> monthCounts = new TreeMap<>();
private final Object2IntMap<String> quarterCounts = new Object2IntOpenHashMap<>();

private int totalCount = 0;
private int nulls = 0;
Expand Down Expand Up @@ -71,26 +72,19 @@ private void handleDay(int day) {
final LocalDate date = CDate.toLocalDate(day);
final int year = date.getYear();
final int quarter = date.get(IsoFields.QUARTER_OF_YEAR);
final int month = date.getMonthValue();

final String yearQuarter = year + "-" + quarter;
// This code is pretty hot, therefore I want to avoid String.format
final String yearMonth = year + "-" + (month < 10 ? "0" : "") + month;


quarterCounts.compute(yearQuarter, (ignored, current) -> current == null ? 1 : current + 1);
monthCounts.compute(yearMonth, (ignored, current) -> current == null ? 1 : current + 1);

}

@Override
public ResultColumnStatistics describe() {

return new ColumnDescription(getName(), getLabel(), getDescription(),
totalCount,
nulls,
quarterCounts,
monthCounts,
// we only need to sort for output, so using a fastmap for computation is sensible.
new TreeMap<>(quarterCounts),
span == null ? CDateRange.all().toSimpleRange() : span.toSimpleRange()
);
}
Expand All @@ -103,16 +97,14 @@ public static class ColumnDescription extends ResultColumnStatistics {
private final int count;
private final int nullValues;
private final SortedMap<String, Integer> quarterCounts;
private final SortedMap<String, Integer> monthCounts;

private final Range<LocalDate> span;

public ColumnDescription(String name, String label, String description, int count, int nullValues, SortedMap<String, Integer> quarterCounts, SortedMap<String, Integer> monthCounts, Range<LocalDate> span) {
public ColumnDescription(String name, String label, String description, int count, int nullValues, SortedMap<String, Integer> quarterCounts, Range<LocalDate> span) {
super(name, label, description);
this.count = count;
this.nullValues = nullValues;
this.quarterCounts = quarterCounts;
this.monthCounts = monthCounts;
this.span = span;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ public void execute(String name, TestConquery testConquery) throws Exception {
"2021-1", 5,
"2021-4", 1
)),
new TreeMap<>(Map.of(
"2021-01", 5,
"2021-10", 1
)),
Range.of(
LocalDate.of(2021, 1, 1), LocalDate.of(2021, 10, 1)
)
Expand All @@ -97,10 +93,6 @@ public void execute(String name, TestConquery testConquery) throws Exception {
"2021-1", 5,
"2021-4", 1
)),
new TreeMap<>(Map.of(
"2021-01", 5,
"2021-10", 1
)),
Range.of(
LocalDate.of(2021, 1, 1), LocalDate.of(2021, 10, 1)
)
Expand Down

0 comments on commit 21cc4c0

Please sign in to comment.