-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EXPOSED-560 Support DISTINCT ON from Postgres
- Loading branch information
1 parent
b0a7aed
commit f725471
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/DistinctOnTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.jetbrains.exposed.sql.tests.shared.dml | ||
|
||
import org.jetbrains.exposed.dao.id.IntIdTable | ||
import org.jetbrains.exposed.sql.SortOrder | ||
import org.jetbrains.exposed.sql.batchInsert | ||
import org.jetbrains.exposed.sql.selectAll | ||
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase | ||
import org.jetbrains.exposed.sql.tests.TestDB | ||
import org.jetbrains.exposed.sql.tests.shared.assertEqualLists | ||
import org.junit.Test | ||
|
||
class DistinctOnTests : DatabaseTestsBase() { | ||
@Test | ||
fun test() { | ||
val tester = object : IntIdTable("distinct_function_test") { | ||
val value1 = integer("value1") | ||
val value2 = integer("value2") | ||
} | ||
|
||
withTables(excludeSettings = TestDB.ALL - TestDB.ALL_POSTGRES - TestDB.ALL_H2, tester) { | ||
tester.batchInsert( | ||
listOf( | ||
listOf(1, 1), listOf(1, 2), listOf(1, 2), | ||
listOf(2, 1), listOf(2, 2), listOf(2, 2), | ||
listOf(4, 4), listOf(4, 4), listOf(4, 4), | ||
) | ||
) { | ||
this[tester.value1] = it[0] | ||
this[tester.value2] = it[1] | ||
} | ||
|
||
val distinctValue1 = tester.selectAll() | ||
.withDistinctOn(tester.value1) | ||
.orderBy(tester.value1 to SortOrder.ASC, tester.value2 to SortOrder.ASC) | ||
.map { it[tester.value1] to it[tester.value2] } | ||
assertEqualLists(listOf(1 to 1, 2 to 1, 4 to 4), distinctValue1) | ||
|
||
val distinctValue2 = tester.selectAll() | ||
.withDistinctOn(tester.value2) | ||
.orderBy(tester.value2 to SortOrder.ASC, tester.value1 to SortOrder.ASC) | ||
.map { it[tester.value1] to it[tester.value2] } | ||
assertEqualLists(listOf(1 to 1, 1 to 2, 4 to 4), distinctValue2) | ||
|
||
val distinctBoth = tester.selectAll() | ||
.withDistinctOn(tester.value1, tester.value2) | ||
.orderBy(tester.value1 to SortOrder.ASC, tester.value2 to SortOrder.ASC) | ||
.map { it[tester.value1] to it[tester.value2] } | ||
assertEqualLists(listOf(1 to 1, 1 to 2, 2 to 1, 2 to 2, 4 to 4), distinctBoth) | ||
|
||
val distinctSequential = tester.selectAll() | ||
.withDistinctOn(tester.value1 to SortOrder.ASC) | ||
.withDistinctOn(tester.value2 to SortOrder.ASC) | ||
.map { it[tester.value1] to it[tester.value2] } | ||
assertEqualLists(distinctBoth, distinctSequential) | ||
} | ||
} | ||
} |