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 boolean and Timestamp to min max #4245

Merged
merged 4 commits into from
Jun 15, 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 @@ -86,8 +86,8 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes
"concat" -> encapsulatingType(exprList, TEXT)
"substring" -> IntermediateType(TEXT).nullableIf(resolvedType(exprList[0]).javaType.isNullable)
"coalesce", "ifnull" -> encapsulatingType(exprList, SMALL_INT, PostgreSqlType.INTEGER, INTEGER, BIG_INT, REAL, TEXT, BLOB)
"max" -> encapsulatingType(exprList, SMALL_INT, PostgreSqlType.INTEGER, INTEGER, BIG_INT, REAL, TEXT, BLOB).asNullable()
"min" -> encapsulatingType(exprList, BLOB, TEXT, SMALL_INT, INTEGER, PostgreSqlType.INTEGER, BIG_INT, REAL).asNullable()
"max" -> encapsulatingType(exprList, SMALL_INT, PostgreSqlType.INTEGER, INTEGER, BIG_INT, REAL, TEXT, BLOB, TIMESTAMP_TIMEZONE, TIMESTAMP).asNullable()
"min" -> encapsulatingType(exprList, BLOB, TEXT, SMALL_INT, INTEGER, PostgreSqlType.INTEGER, BIG_INT, REAL, TIMESTAMP_TIMEZONE, TIMESTAMP).asNullable()
"date_trunc" -> encapsulatingType(exprList, TIMESTAMP_TIMEZONE, TIMESTAMP)
"date_part" -> IntermediateType(REAL)
"now" -> IntermediateType(TIMESTAMP_TIMEZONE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import app.cash.sqldelight.dialect.api.PragmaWithResults
import app.cash.sqldelight.dialect.api.PrimitiveType
import app.cash.sqldelight.dialect.api.PrimitiveType.ARGUMENT
import app.cash.sqldelight.dialect.api.PrimitiveType.BLOB
import app.cash.sqldelight.dialect.api.PrimitiveType.BOOLEAN
import app.cash.sqldelight.dialect.api.PrimitiveType.INTEGER
import app.cash.sqldelight.dialect.api.PrimitiveType.NULL
import app.cash.sqldelight.dialect.api.PrimitiveType.REAL
Expand Down Expand Up @@ -138,8 +139,8 @@ internal object AnsiSqlTypeResolver : TypeResolver {
"iif" -> exprList[1].type()
"coalesce", "ifnull" -> encapsulatingType(exprList, INTEGER, REAL, TEXT, BLOB)
"nullif" -> exprList[0].type().asNullable()
"max" -> encapsulatingType(exprList, INTEGER, REAL, TEXT, BLOB).asNullable()
"min" -> encapsulatingType(exprList, BLOB, TEXT, INTEGER, REAL).asNullable()
"max" -> encapsulatingType(exprList, INTEGER, REAL, TEXT, BLOB, BOOLEAN).asNullable()
"min" -> encapsulatingType(exprList, BLOB, TEXT, INTEGER, REAL, BOOLEAN).asNullable()
else -> null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ SELECT NOW();

selectInterval:
SELECT INTERVAL '1 day';

selectMax:
SELECT max(timestamp) FROM dates;

selectMin:
SELECT min(timestamp_with_timezone) FROM dates;
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,23 @@ class PostgreSqlTest {
)
}

@Test fun testMinMaxTimeStamps() {
database.datesQueries.insertDate(
date = LocalDate.of(2022, 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.selectMax().executeAsOne().let {
assertThat(it.max).isEqualTo(LocalDateTime.of(2029, 1, 1, 21, 30, 59, 10000))
}

database.datesQueries.selectMin().executeAsOne().let {
assertThat(it.min).isEqualTo(OffsetDateTime.of(1970, 4, 9, 20, 15, 45, 0, ZoneOffset.ofHours(0)))
}
}

@Test fun testSerial() {
database.run {
oneEntityQueries.transaction {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import kotlin.Boolean;

CREATE TABLE withBoolean (
some_boolean_field INTEGER AS Boolean NOT NULL
);

insertBoolean:
INSERT INTO withBoolean (some_boolean_field) VALUES (:someBooleanField);

selectMaxBoolean:
SELECT
max(some_boolean_field)
FROM withBoolean;

selectMinBoolean:
SELECT
min(some_boolean_field)
FROM withBoolean;
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import org.junit.Test
class IntegrationTests {
private lateinit var queryWrapper: QueryWrapper
private lateinit var personQueries: PersonQueries
private lateinit var booleansQueries: BooleansQueries

@Before fun before() {
val database = JdbcSqliteDriver(IN_MEMORY)
QueryWrapper.Schema.create(database)

queryWrapper = QueryWrapper(database)
personQueries = queryWrapper.personQueries
booleansQueries = queryWrapper.booleansQueries
}

@Test fun upsertNoConflict() {
Expand Down Expand Up @@ -44,4 +46,17 @@ class IntegrationTests {
Person(4, "Bob", "Bob"),
)
}

@Test fun aggregateBoolean() {
booleansQueries.insertBoolean(true)
booleansQueries.insertBoolean(false)

booleansQueries.selectMaxBoolean().executeAsOne().let {
assertThat(it.max).isTrue()
}

booleansQueries.selectMinBoolean().executeAsOne().let {
assertThat(it.min).isFalse()
}
}
}