Skip to content

Commit 0719288

Browse files
authored
Merge pull request #318 from Kotlin/kproperties-columnsSelectionDsl
Column Selection DSL improvements
2 parents 1f77969 + e1c579f commit 0719288

File tree

15 files changed

+393
-51
lines changed

15 files changed

+393
-51
lines changed

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/ColumnsContainer.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public interface ColumnsContainer<out T> {
3737
public fun getColumnOrNull(name: String): AnyCol?
3838
public fun getColumnOrNull(index: Int): AnyCol?
3939
public fun <R> getColumnOrNull(column: ColumnReference<R>): DataColumn<R>?
40+
public fun <R> getColumnOrNull(column: KProperty<R>): DataColumn<R>?
4041
public fun getColumnOrNull(path: ColumnPath): AnyCol?
4142
public fun <R> getColumnOrNull(column: ColumnSelector<T, R>): DataColumn<R>?
4243

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/ColumnsSelectionDsl.kt

Lines changed: 125 additions & 17 deletions
Large diffs are not rendered by default.

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/columns/ColumnWithPath.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.jetbrains.kotlinx.dataframe.api.isColumnGroup
66
import org.jetbrains.kotlinx.dataframe.impl.columns.addParentPath
77
import org.jetbrains.kotlinx.dataframe.impl.columns.addPath
88
import org.jetbrains.kotlinx.dataframe.impl.columns.depth
9+
import kotlin.reflect.KProperty
910

1011
public interface ColumnWithPath<out T> : DataColumn<T> {
1112

@@ -17,6 +18,8 @@ public interface ColumnWithPath<out T> : DataColumn<T> {
1718
public fun <C> getChild(accessor: ColumnReference<C>): ColumnWithPath<C>? = asColumnGroup().getColumnOrNull(accessor)?.addPath(path + accessor.path())
1819
public fun getChild(name: String): ColumnWithPath<Any?>? = asColumnGroup().getColumnOrNull(name)?.addParentPath(path)
1920
public fun getChild(index: Int): ColumnWithPath<Any?>? = asColumnGroup().getColumnOrNull(index)?.addParentPath(path)
21+
public fun <C> getChild(accessor: KProperty<C>): ColumnWithPath<C>? = asColumnGroup().getColumnOrNull(accessor)?.addParentPath(path)
22+
2023
public fun children(): List<ColumnWithPath<Any?>> = if (isColumnGroup()) data.asColumnGroup().columns().map { it.addParentPath(path) } else emptyList()
2124

2225
override fun path(): ColumnPath = path

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/DataFrameImpl.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import org.jetbrains.kotlinx.dataframe.api.cast
1414
import org.jetbrains.kotlinx.dataframe.api.getColumn
1515
import org.jetbrains.kotlinx.dataframe.api.indices
1616
import org.jetbrains.kotlinx.dataframe.api.name
17+
import org.jetbrains.kotlinx.dataframe.api.toColumnAccessor
1718
import org.jetbrains.kotlinx.dataframe.columns.ColumnPath
1819
import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
1920
import org.jetbrains.kotlinx.dataframe.columns.UnresolvedColumnsPolicy
@@ -24,6 +25,7 @@ import org.jetbrains.kotlinx.dataframe.impl.aggregation.AggregatableInternal
2425
import org.jetbrains.kotlinx.dataframe.impl.aggregation.GroupByReceiverImpl
2526
import org.jetbrains.kotlinx.dataframe.impl.columns.resolveSingle
2627
import org.jetbrains.kotlinx.dataframe.io.renderToString
28+
import kotlin.reflect.KProperty
2729

2830
private const val unnamedColumnPrefix = "untitled"
2931

@@ -116,6 +118,7 @@ internal open class DataFrameImpl<T>(cols: List<AnyCol>, val nrow: Int) : DataFr
116118
).singleOrNull()
117119

118120
override fun <R> getColumnOrNull(column: ColumnReference<R>): DataColumn<R>? = column.resolveSingle(this, UnresolvedColumnsPolicy.Skip)?.data
121+
override fun <R> getColumnOrNull(column: KProperty<R>): DataColumn<R>? = getColumnOrNull(column.toColumnAccessor())
119122

120123
override fun getColumnOrNull(path: ColumnPath): AnyCol? =
121124
when (path.size) {

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/columns/missing/MissingColumnGroup.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath
1515
import org.jetbrains.kotlinx.dataframe.columns.UnresolvedColumnsPolicy
1616
import org.jetbrains.kotlinx.dataframe.impl.columns.DataColumnGroup
1717
import org.jetbrains.kotlinx.dataframe.impl.columns.addPath
18+
import kotlin.reflect.KProperty
1819
import kotlin.reflect.KType
1920

2021
internal class MissingColumnGroup<T>(val path: ColumnPath, val host: ColumnsContainer<*>) : MissingDataColumn<DataRow<T>>(), DataColumnGroup<T> {
@@ -49,6 +50,8 @@ internal class MissingColumnGroup<T>(val path: ColumnPath, val host: ColumnsCont
4950

5051
override fun <R> getColumnOrNull(column: ColumnReference<R>) = MissingColumnGroup<Any>(path + column.name(), host).asDataColumn().cast<R>()
5152

53+
override fun <R> getColumnOrNull(column: KProperty<R>) = MissingColumnGroup<Any>(path + column.name, host).asDataColumn().cast<R>()
54+
5255
override fun getColumnOrNull(path: ColumnPath) = MissingColumnGroup<Any?>(this.path + path, host)
5356

5457
override fun <R> getColumnOrNull(column: ColumnSelector<T, R>) = MissingColumnGroup<Any>(path + "", host).asDataColumn().cast<R>()

core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/samples/api/Access.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,41 @@ class Access : TestBase() {
706706
// SampleEnd
707707
}
708708

709+
@Test
710+
fun columnSelectors_kproperties() {
711+
// SampleStart
712+
// by column name
713+
df.select { it[Person::name] }
714+
df.select { (Person::name)() }
715+
df.select { col(Person::name) }
716+
717+
// by column path
718+
df.select { it[Person::name][Name::firstName] }
719+
df.select { Person::name[Name::firstName] }
720+
721+
// with a new name
722+
df.select { Person::name named "Full Name" }
723+
724+
// converted
725+
df.select { Person::name[Name::firstName].map { it.lowercase() } }
726+
727+
// column arithmetics
728+
df.select { 2021 - (Person::age)() }
729+
730+
// two columns
731+
df.select { Person::name and Person::age }
732+
733+
// range of columns
734+
df.select { Person::name..Person::age }
735+
736+
// all children of ColumnGroup
737+
df.select { Person::name.all() }
738+
739+
// dfs traversal of all children columns
740+
df.select { Person::name.allDfs() }
741+
// SampleEnd
742+
}
743+
709744
@Test
710745
fun columnSelectors_strings() {
711746
// SampleStart

core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/DataFrameTests.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,9 +1899,9 @@ class DataFrameTests : BaseTest() {
18991899
@Test
19001900
fun `select all after`() {
19011901
typed.select { allAfter(age) } shouldBe typed.select { city and weight }
1902-
typed.select { allSince(age) } shouldBe typed.select { age and city and weight }
1902+
typed.select { allFrom(age) } shouldBe typed.select { age and city and weight }
19031903
typed.select { allBefore(age) } shouldBe typed.select { name }
1904-
typed.select { allUntil(age) } shouldBe typed.select { name and age }
1904+
typed.select { allUpTo(age) } shouldBe typed.select { name and age }
19051905
}
19061906

19071907
@Test

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/ColumnsContainer.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public interface ColumnsContainer<out T> {
3737
public fun getColumnOrNull(name: String): AnyCol?
3838
public fun getColumnOrNull(index: Int): AnyCol?
3939
public fun <R> getColumnOrNull(column: ColumnReference<R>): DataColumn<R>?
40+
public fun <R> getColumnOrNull(column: KProperty<R>): DataColumn<R>?
4041
public fun getColumnOrNull(path: ColumnPath): AnyCol?
4142
public fun <R> getColumnOrNull(column: ColumnSelector<T, R>): DataColumn<R>?
4243

0 commit comments

Comments
 (0)