Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DATE PostgreSQL type to min and max aggregate functions #4816

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import app.cash.sqldelight.dialect.api.TypeResolver
import app.cash.sqldelight.dialect.api.encapsulatingType
import app.cash.sqldelight.dialect.api.encapsulatingTypePreferringKotlin
import app.cash.sqldelight.dialects.postgresql.PostgreSqlType.BIG_INT
import app.cash.sqldelight.dialects.postgresql.PostgreSqlType.DATE
import app.cash.sqldelight.dialects.postgresql.PostgreSqlType.SMALL_INT
import app.cash.sqldelight.dialects.postgresql.PostgreSqlType.TIMESTAMP
import app.cash.sqldelight.dialects.postgresql.PostgreSqlType.TIMESTAMP_TIMEZONE
Expand Down Expand Up @@ -119,8 +120,8 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes
"substring", "replace" -> IntermediateType(TEXT).nullableIf(resolvedType(exprList[0]).javaType.isNullable)
"starts_with" -> IntermediateType(BOOLEAN)
"coalesce", "ifnull" -> encapsulatingTypePreferringKotlin(exprList, SMALL_INT, PostgreSqlType.INTEGER, INTEGER, BIG_INT, REAL, TEXT, BLOB)
"max" -> encapsulatingTypePreferringKotlin(exprList, SMALL_INT, PostgreSqlType.INTEGER, INTEGER, BIG_INT, REAL, TEXT, BLOB, TIMESTAMP_TIMEZONE, TIMESTAMP).asNullable()
"min" -> encapsulatingTypePreferringKotlin(exprList, BLOB, TEXT, SMALL_INT, INTEGER, PostgreSqlType.INTEGER, BIG_INT, REAL, TIMESTAMP_TIMEZONE, TIMESTAMP).asNullable()
"max" -> encapsulatingTypePreferringKotlin(exprList, SMALL_INT, PostgreSqlType.INTEGER, INTEGER, BIG_INT, REAL, TEXT, BLOB, TIMESTAMP_TIMEZONE, TIMESTAMP, DATE).asNullable()
"min" -> encapsulatingTypePreferringKotlin(exprList, BLOB, TEXT, SMALL_INT, INTEGER, PostgreSqlType.INTEGER, BIG_INT, REAL, TIMESTAMP_TIMEZONE, TIMESTAMP, DATE).asNullable()
"sum" -> {
val type = resolvedType(exprList.single())
if (type.dialectType == REAL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ SELECT max(timestamp) FROM dates;

selectMin:
SELECT min(timestamp_with_timezone) FROM dates;

selectMaxDate:
SELECT max(date) FROM dates;

selectMinDate:
SELECT min(date) FROM dates;
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,30 @@ class PostgreSqlTest {
}
}

@Test fun testMinMaxDates() {
database.datesQueries.insertDate(
date = LocalDate.of(2023, 1, 1),
time = LocalTime.of(11, 30, 59, 10000),
timestamp = LocalDateTime.of(2029, 1, 1, 21, 30, 59, 10000),
timestamp_with_timezone = OffsetDateTime.of(1970, 4, 9, 20, 15, 45, 0, ZoneOffset.ofHours(0)),
).executeAsOne()

database.datesQueries.insertDate(
date = LocalDate.of(2022, 10, 17),
time = LocalTime.of(11, 30, 59, 10000),
timestamp = LocalDateTime.of(2029, 1, 1, 21, 30, 59, 10000),
timestamp_with_timezone = OffsetDateTime.of(1970, 4, 9, 20, 15, 45, 0, ZoneOffset.ofHours(0)),
).executeAsOne()

database.datesQueries.selectMaxDate().executeAsOne().let {
assertThat(it.max).isEqualTo(LocalDate.of(2023, 1, 1))
}

database.datesQueries.selectMinDate().executeAsOne().let {
assertThat(it.min).isEqualTo(LocalDate.of(2022, 10, 17))
}
}

@Test fun testSerial() {
database.run {
oneEntityQueries.transaction {
Expand Down