Skip to content

Commit

Permalink
Add DATE PostgreSQL type to min and max aggregate functions (#4816)
Browse files Browse the repository at this point in the history
  • Loading branch information
anddani committed Nov 16, 2023
1 parent 9891ca5 commit ac96b24
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
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

0 comments on commit ac96b24

Please sign in to comment.