Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsrnhld committed Apr 10, 2024
1 parent c3f8f46 commit 12b80e2
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,42 @@ public static TemporalAdjuster lastDayOfQuarterAdjuster() {
public static int getQuarter(LocalDate date) {
return date.get(IsoFields.QUARTER_OF_YEAR);
}

public static LocalDate jumpToQuarterStart(LocalDate date) {

Month startMonth = switch (date.getMonthValue()) {
case 1, 2, 3 -> Month.JANUARY;
case 4, 5, 6 -> Month.APRIL;
case 7, 8, 9 -> Month.JULY;
default -> Month.OCTOBER;
};

return LocalDate.of(date.getYear(), startMonth, 1);
}

public static LocalDate jumpToNextQuarterStart(LocalDate date) {

int year = date.getYear();
Month startMonth;

switch (date.getMonthValue()) {
case 1, 2, 3:
startMonth = Month.APRIL; // Start of Q2
break;
case 4, 5, 6:
startMonth = Month.JULY; // Start of Q3
break;
case 7, 8, 9:
startMonth = Month.OCTOBER; // Start of Q4
break;
default:
// For Q4, increment the year and set month to January
startMonth = Month.JANUARY;
year++;
break;
}

return LocalDate.of(year, startMonth, 1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,18 @@ public String getMessageTemplate(ErrorMessages errorMessages) {
return errorMessages.sqlError(error);
}

}

@CPSType(base = ConqueryError.class, id = "CQ_SQL_CONVERSION_ERROR")
@RequiredArgsConstructor(onConstructor_ = {@JsonCreator})
public static class SqlConversionError extends ConqueryError {

private final String message;

@Override
public String getMessageTemplate(ErrorMessages errorMessages) {
return errorMessages.sqlConversionError(message);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ public interface ErrorMessages {
@En("Something went wrong while querying the database: ${0}.")
@De("Etwas ist beim Anfragen des Servers fehlgeschlagen: ${0}.")
String sqlError(Exception error);

@En("Could not convert the query to SQL: ${0}.")
@De("Die Anfrage konnte nicht in SQL umgewandelt werden: ${0}.")
String sqlConversionError(String message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.bakdata.conquery.models.common.daterange.CDateRange;
import com.bakdata.conquery.models.datasets.Column;
import com.bakdata.conquery.models.datasets.concepts.ValidityDate;
import com.bakdata.conquery.models.error.ConqueryError;
import com.bakdata.conquery.sql.conversion.SharedAliases;
import com.bakdata.conquery.sql.conversion.model.ColumnDateRange;
import com.bakdata.conquery.sql.conversion.model.QueryStep;
Expand Down Expand Up @@ -84,10 +85,11 @@ public ColumnDateRange forCDateRange(CDateRange daterange) {
startDateExpression = daterange.getMin().toString();
}
if (daterange.hasUpperBound()) {
// end is handled as exclusive
LocalDate exclusiveMaxDate = daterange.getMax() == LocalDate.MAX
? Date.valueOf(MAX_DATE_VALUE).toLocalDate()
: daterange.getMax().plusDays(1);
// end date is expected to be handled as exclusive, but if it's already the maximum date, we can't add +1 day
if (daterange.getMax() == LocalDate.MAX) {
throw new ConqueryError.SqlConversionError("Given daterange has an upper bound of LocalDate.MAX, which is not supported by HANA.");
}
LocalDate exclusiveMaxDate = daterange.getMax().plusDays(1);
endDateExpression = exclusiveMaxDate.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.stream.Stream;

import com.bakdata.conquery.apiv1.forms.export_form.ExportForm;
import com.bakdata.conquery.models.common.QuarterUtils;
import com.bakdata.conquery.models.common.Range;
import com.bakdata.conquery.models.common.daterange.CDateRange;
import com.bakdata.conquery.models.config.Dialect;
Expand Down Expand Up @@ -178,15 +179,18 @@ protected Range<LocalDate> toGenerateSeriesBounds(Range<LocalDate> formDateRestr
return switch (resolutionAndAlignment.getResolution()) {
case COMPLETE -> formDateRestriction; // no adjustment necessary
case YEARS -> determineYearRange(formDateRestriction, resolutionAndAlignment.getAlignment());
case QUARTERS -> Range.of(jumpToQuarterStart(formDateRestriction.getMin()), jumpToNextQuarterStart(formDateRestriction.getMax()));
case QUARTERS -> Range.of(
QuarterUtils.jumpToQuarterStart(formDateRestriction.getMin()),
QuarterUtils.jumpToNextQuarterStart(formDateRestriction.getMax())
);
case DAYS -> throw new UnsupportedOperationException("DAYS resolution not supported yet");
};
}

private Range<LocalDate> determineYearRange(Range<LocalDate> bounds, Alignment alignment) {
return switch (alignment) {
case QUARTER -> {
LocalDate start = jumpToQuarterStart(bounds.getMin());
LocalDate start = QuarterUtils.jumpToQuarterStart(bounds.getMin());
LocalDate end = LocalDate.of(bounds.getMax().getYear() + 1, start.getMonth(), 1);
yield Range.of(start, end);
}
Expand All @@ -198,41 +202,4 @@ private Range<LocalDate> determineYearRange(Range<LocalDate> bounds, Alignment a
};
}

private static LocalDate jumpToQuarterStart(LocalDate date) {

Month startMonth = switch (date.getMonthValue()) {
case 1, 2, 3 -> Month.JANUARY;
case 4, 5, 6 -> Month.APRIL;
case 7, 8, 9 -> Month.JULY;
default -> Month.OCTOBER;
};

return LocalDate.of(date.getYear(), startMonth, 1);
}

private static LocalDate jumpToNextQuarterStart(LocalDate date) {

int year = date.getYear();
Month startMonth;

switch (date.getMonthValue()) {
case 1, 2, 3:
startMonth = Month.APRIL; // Start of Q2
break;
case 4, 5, 6:
startMonth = Month.JULY; // Start of Q3
break;
case 7, 8, 9:
startMonth = Month.OCTOBER; // Start of Q4
break;
default:
// For Q4, increment the year and set month to January
startMonth = Month.JANUARY;
year++;
break;
}

return LocalDate.of(year, startMonth, 1);
}

}

0 comments on commit 12b80e2

Please sign in to comment.