Skip to content

Commit d1a84a0

Browse files
committed
SingleColumn<*> in ColumnSelectionDsl. reworked ColumnSelectionDsl tests
1 parent 6ae692d commit d1a84a0

File tree

6 files changed

+876
-806
lines changed

6 files changed

+876
-806
lines changed

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

Lines changed: 86 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
285285
* @throws [NoSuchElementException] if no column adheres to the given [condition].
286286
* @see [last]
287287
*/
288-
public fun SingleColumn<AnyRow>.first(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
288+
public fun SingleColumn<*>.first(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
289289
all().first(condition)
290290

291291
/**
@@ -388,7 +388,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
388388
* @throws [NoSuchElementException] if no column adheres to the given [condition].
389389
* @see [first]
390390
*/
391-
public fun SingleColumn<AnyRow>.last(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
391+
public fun SingleColumn<*>.last(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
392392
all().last(condition)
393393

394394
/**
@@ -491,7 +491,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
491491
* @throws [NoSuchElementException] if no column adheres to the given [condition].
492492
* @throws [IllegalArgumentException] if more than one column adheres to the given [condition].
493493
*/
494-
public fun SingleColumn<AnyRow>.single(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
494+
public fun SingleColumn<*>.single(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
495495
all().single(condition)
496496

497497
/**
@@ -1053,7 +1053,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
10531053
*/
10541054
public fun <C> ColumnGroupReference.col(property: KProperty<C>): ColumnAccessor<C> = column(property)
10551055

1056-
public fun SingleColumn<AnyRow>.col(index: Int): SingleColumn<Any?> = getChildrenAt(index).singleImpl()
1056+
public fun SingleColumn<*>.col(index: Int): SingleColumn<Any?> = getChildrenAt(index).singleImpl()
10571057

10581058
public operator fun <C> ColumnSet<C>.get(index: Int): SingleColumn<C> = getAt(index)
10591059

@@ -1946,7 +1946,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
19461946
* @param [predicate] A [ColumnFilter function][org.jetbrains.kotlinx.dataframe.ColumnFilter] that takes a [ColumnReference][org.jetbrains.kotlinx.dataframe.columns.ColumnReference] and returns a [Boolean].
19471947
* @return A [ColumnSet][org.jetbrains.kotlinx.dataframe.columns.ColumnSet] containing the columns that match the given [predicate].
19481948
*/
1949-
public fun SingleColumn<AnyRow>.cols(
1949+
public fun SingleColumn<*>.cols(
19501950
predicate: ColumnFilter<*> = { true },
19511951
): ColumnSet<*> = colsInternal(predicate)
19521952

@@ -1992,7 +1992,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
19921992
* @param [predicate] A [ColumnFilter function][org.jetbrains.kotlinx.dataframe.ColumnFilter] that takes a [ColumnReference][org.jetbrains.kotlinx.dataframe.columns.ColumnReference] and returns a [Boolean].
19931993
* @return A [ColumnSet][org.jetbrains.kotlinx.dataframe.columns.ColumnSet] containing the columns that match the given [predicate].
19941994
*/
1995-
public operator fun SingleColumn<AnyRow>.get(
1995+
public operator fun SingleColumn<*>.get(
19961996
predicate: ColumnFilter<*> = { true },
19971997
): ColumnSet<Any?> = cols(predicate)
19981998

@@ -2479,7 +2479,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
24792479
* @param [otherCols] Optional additional [ColumnReference][org.jetbrains.kotlinx.dataframe.columns.ColumnReference]s that point to columns.
24802480
* @return A [ColumnSet][org.jetbrains.kotlinx.dataframe.columns.ColumnSet] containing the columns that [firstCol] and [otherCols] point to.
24812481
*/
2482-
public fun <C> SingleColumn<AnyRow>.cols(
2482+
public fun <C> SingleColumn<*>.cols(
24832483
firstCol: ColumnReference<C>,
24842484
vararg otherCols: ColumnReference<C>,
24852485
): ColumnSet<C> = headPlusArray(firstCol, otherCols).let { refs ->
@@ -2524,7 +2524,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
25242524
* @param [otherCols] Optional additional [ColumnReference][org.jetbrains.kotlinx.dataframe.columns.ColumnReference]s that point to columns.
25252525
* @return A [ColumnSet][org.jetbrains.kotlinx.dataframe.columns.ColumnSet] containing the columns that [firstCol] and [otherCols] point to.
25262526
*/
2527-
public operator fun <C> SingleColumn<AnyRow>.get(
2527+
public operator fun <C> SingleColumn<*>.get(
25282528
firstCol: ColumnReference<C>,
25292529
vararg otherCols: ColumnReference<C>,
25302530
): ColumnSet<C> = cols(firstCol, *otherCols)
@@ -2989,7 +2989,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
29892989
* @param [otherCols] Optional additional [String]s that point to columns.
29902990
* @return A [ColumnSet][org.jetbrains.kotlinx.dataframe.columns.ColumnSet] containing the columns that [firstCol] and [otherCols] point to.
29912991
*/
2992-
public fun SingleColumn<AnyRow>.cols(
2992+
public fun SingleColumn<*>.cols(
29932993
firstCol: String,
29942994
vararg otherCols: String,
29952995
): ColumnSet<*> = headPlusArray(firstCol, otherCols).let { names ->
@@ -3030,7 +3030,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
30303030
* @param [otherCols] Optional additional [String]s that point to columns.
30313031
* @return A [ColumnSet][org.jetbrains.kotlinx.dataframe.columns.ColumnSet] containing the columns that [firstCol] and [otherCols] point to.
30323032
*/
3033-
public operator fun SingleColumn<AnyRow>.get(
3033+
public operator fun SingleColumn<*>.get(
30343034
firstCol: String,
30353035
vararg otherCols: String,
30363036
): ColumnSet<*> = cols(firstCol, *otherCols)
@@ -3471,7 +3471,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
34713471
* @param [otherCols] Optional additional [KProperty]s that point to columns.
34723472
* @return A [ColumnSet][org.jetbrains.kotlinx.dataframe.columns.ColumnSet] containing the columns that [firstCol] and [otherCols] point to.
34733473
*/
3474-
public fun <C> SingleColumn<AnyRow>.cols(
3474+
public fun <C> SingleColumn<*>.cols(
34753475
firstCol: KProperty<C>,
34763476
vararg otherCols: KProperty<C>,
34773477
): ColumnSet<C> = headPlusArray(firstCol, otherCols).let { props ->
@@ -3508,7 +3508,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
35083508
* @param [otherCols] Optional additional [KProperty]s that point to columns.
35093509
* @return A [ColumnSet][org.jetbrains.kotlinx.dataframe.columns.ColumnSet] containing the columns that [firstCol] and [otherCols] point to.
35103510
*/
3511-
public operator fun <C> SingleColumn<AnyRow>.get(
3511+
public operator fun <C> SingleColumn<*>.get(
35123512
firstCol: KProperty<C>,
35133513
vararg otherCols: KProperty<C>,
35143514
): ColumnSet<C> = cols(firstCol, *otherCols)
@@ -3806,7 +3806,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
38063806
vararg otherIndices: Int,
38073807
): ColumnSet<C> = cols(firstIndex, *otherIndices)
38083808

3809-
public fun SingleColumn<AnyRow>.cols(
3809+
public fun SingleColumn<*>.cols(
38103810
firstIndex: Int,
38113811
vararg otherIndices: Int,
38123812
): ColumnSet<*> = headPlusArray(firstIndex, otherIndices).let { indices ->
@@ -3816,7 +3816,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
38163816
/**
38173817
*
38183818
*/
3819-
public operator fun SingleColumn<AnyRow>.get(
3819+
public operator fun SingleColumn<*>.get(
38203820
firstIndex: Int,
38213821
vararg otherIndices: Int,
38223822
): ColumnSet<*> = cols(firstIndex, *otherIndices)
@@ -3866,13 +3866,13 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
38663866

38673867
public operator fun <C> ColumnSet<C>.get(range: IntRange): ColumnSet<C> = cols(range)
38683868

3869-
public fun SingleColumn<AnyRow>.cols(range: IntRange): ColumnSet<*> =
3869+
public fun SingleColumn<*>.cols(range: IntRange): ColumnSet<*> =
38703870
transform { it.flatMap { it.children().subList(range.first, range.last + 1) } }
38713871

38723872
/**
38733873
*
38743874
*/
3875-
public operator fun SingleColumn<AnyRow>.get(range: IntRange): ColumnSet<*> = cols(range)
3875+
public operator fun SingleColumn<*>.get(range: IntRange): ColumnSet<*> = cols(range)
38763876

38773877
public fun String.cols(range: IntRange): ColumnSet<*> = colGroup(this).cols(range)
38783878

@@ -3914,21 +3914,21 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
39143914

39153915
@Deprecated(
39163916
message = "dfs is deprecated, use recursively instead.",
3917-
replaceWith = ReplaceWith("this.cols(predicate).recursively()"),
3917+
replaceWith = ReplaceWith("this.cols(predicate).recursively(includeTopLevel = false)"),
39183918
level = DeprecationLevel.WARNING,
39193919
)
39203920
public fun <C> ColumnSet<C>.dfs(predicate: (ColumnWithPath<*>) -> Boolean): ColumnSet<Any?> = dfsInternal(predicate)
39213921

39223922
@Deprecated(
39233923
message = "dfs is deprecated, use recursively instead.",
3924-
replaceWith = ReplaceWith("this.cols(predicate).recursively()"),
3924+
replaceWith = ReplaceWith("this.cols(predicate).recursively(includeTopLevel = false)"),
39253925
level = DeprecationLevel.WARNING,
39263926
)
39273927
public fun String.dfs(predicate: (ColumnWithPath<*>) -> Boolean): ColumnSet<*> = toColumnAccessor().dfs(predicate)
39283928

39293929
@Deprecated(
39303930
message = "dfs is deprecated, use recursively instead.",
3931-
replaceWith = ReplaceWith("this.cols(predicate).recursively()"),
3931+
replaceWith = ReplaceWith("this.cols(predicate).recursively(includeTopLevel = false)"),
39323932
level = DeprecationLevel.WARNING,
39333933
)
39343934
public fun <C> KProperty<C>.dfs(predicate: (ColumnWithPath<*>) -> Boolean): ColumnSet<*> =
@@ -3949,32 +3949,51 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
39493949

39503950
@Deprecated(
39513951
message = "allDfs is deprecated, use recursively instead.",
3952-
replaceWith = ReplaceWith("this.all().recursively(includeGroups)"),
3952+
replaceWith = ReplaceWith("this.all().recursively(includeGroups = includeGroups, includeTopLevel = false)"),
39533953
level = DeprecationLevel.WARNING,
39543954
)
39553955
public fun ColumnSet<*>.allDfs(includeGroups: Boolean = false): ColumnSet<Any?> =
39563956
if (includeGroups) dfs { true } else dfs { !it.isColumnGroup() }
39573957

39583958
@Deprecated(
39593959
message = "allDfs is deprecated, use recursively instead.",
3960-
replaceWith = ReplaceWith("this.all().recursively(includeGroups)"),
3960+
replaceWith = ReplaceWith("this.all().recursively(includeGroups = includeGroups, includeTopLevel = false)"),
39613961
level = DeprecationLevel.WARNING,
39623962
)
39633963
public fun String.allDfs(includeGroups: Boolean = false): ColumnSet<Any?> = toColumnAccessor().allDfs(includeGroups)
39643964

39653965
@Deprecated(
39663966
message = "allDfs is deprecated, use recursively instead.",
3967-
replaceWith = ReplaceWith("this.all().recursively(includeGroups)"),
3967+
replaceWith = ReplaceWith("this.all().recursively(includeGroups = includeGroups, includeTopLevel = false)"),
39683968
level = DeprecationLevel.WARNING,
39693969
)
39703970
public fun KProperty<*>.allDfs(includeGroups: Boolean = false): ColumnSet<Any?> =
39713971
toColumnAccessor().allDfs(includeGroups)
39723972

3973-
public fun <C> ColumnSet<C>.recursively(includeGroups: Boolean = true): ColumnSet<C> = object : ColumnSet<C> {
3973+
/**
3974+
* Modifies the previous call to run not only on the current column set, but also on all of its children.
3975+
*
3976+
* For example:
3977+
*
3978+
* `df.`[select][DataFrame.select]` { `[colsOf][ColumnSet.colsOf]`<`[String][String]`>() }`
3979+
*
3980+
* returns all columns of type [String] in the top-level, as expected. However, what if you want ALL
3981+
* columns of type [String] even if they are inside a nested column group? Then you can use [recursively]:
3982+
*
3983+
* `df.`[select][DataFrame.select]` { `[colsOf][ColumnSet.colsOf]`<`[String][String]`>().`[recursively][ColumnSet.recursively]`() }`
3984+
*
3985+
* This will return all columns of type [String] in lower levels (unless [includeTopLevel]` == true`).
3986+
*
3987+
* TODO
3988+
*/
3989+
public fun <C> ColumnSet<C>.recursively(
3990+
includeGroups: Boolean = true,
3991+
includeTopLevel: Boolean = true,
3992+
): ColumnSet<C> = object : ColumnSet<C> {
39743993

39753994
private fun flatten(columnSet: ColumnSet<*>): ColumnSet<*> = columnSet.transform { list ->
39763995
list
3977-
.filter { it.isColumnGroup() } // TODO should I include this from dfs?
3996+
.filter { includeTopLevel || it.isColumnGroup() } // TODO should I include this from dfs?
39783997
.flatMap {
39793998
it.children()
39803999
.dfs()
@@ -3984,8 +4003,9 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
39844003

39854004
override fun resolve(
39864005
context: ColumnResolutionContext,
3987-
): List<ColumnWithPath<C>> = this@recursively
3988-
.resolveAfterTransform(context = context, transform = ::flatten)
4006+
): List<ColumnWithPath<C>> =
4007+
this@recursively
4008+
.resolveAfterTransform(context = context, transform = ::flatten)
39894009

39904010
override fun resolveAfterTransform(
39914011
context: ColumnResolutionContext,
@@ -3995,13 +4015,22 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
39954015
.resolveAfterTransform(context = context, transform = ::flatten)
39964016
}
39974017

3998-
public fun <C> ColumnSet<C>.rec(includeGroups: Boolean = true): ColumnSet<C> = recursively(includeGroups)
4018+
public fun <C> ColumnSet<C>.rec(
4019+
includeGroups: Boolean = true,
4020+
includeTopLevel: Boolean = true,
4021+
): ColumnSet<C> = recursively(includeTopLevel = includeTopLevel, includeGroups = includeGroups)
39994022

4000-
public fun <C> ColumnSet<C>.allRecursively(includeGroups: Boolean = true): ColumnSet<C> =
4001-
wrap().recursively(includeGroups = includeGroups)
4023+
public fun <C> ColumnSet<C>.allRecursively(
4024+
includeGroups: Boolean = true,
4025+
includeTopLevel: Boolean = true,
4026+
): ColumnSet<C> =
4027+
wrap().recursively(includeTopLevel = includeTopLevel, includeGroups = includeGroups)
40024028

4003-
public fun <C> ColumnSet<C>.allRec(includeGroups: Boolean = true): ColumnSet<C> =
4004-
allRecursively(includeGroups = includeGroups)
4029+
public fun <C> ColumnSet<C>.allRec(
4030+
includeGroups: Boolean = true,
4031+
includeTopLevel: Boolean = true,
4032+
): ColumnSet<C> =
4033+
allRecursively(includeTopLevel = includeTopLevel, includeGroups = includeGroups)
40054034

40064035

40074036
// endregion
@@ -4161,10 +4190,10 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
41614190
public operator fun <C> List<DataColumn<C>>.get(range: IntRange): ColumnSet<C> =
41624191
ColumnsList(subList(range.first, range.last + 1))
41634192

4164-
public fun SingleColumn<AnyRow>.take(n: Int): ColumnSet<*> = transformSingle { it.children().take(n) }
4165-
public fun SingleColumn<AnyRow>.takeLast(n: Int): ColumnSet<*> = transformSingle { it.children().takeLast(n) }
4166-
public fun SingleColumn<AnyRow>.drop(n: Int): ColumnSet<*> = transformSingle { it.children().drop(n) }
4167-
public fun SingleColumn<AnyRow>.dropLast(n: Int = 1): ColumnSet<*> = transformSingle { it.children().dropLast(n) }
4193+
public fun SingleColumn<*>.take(n: Int): ColumnSet<*> = transformSingle { it.children().take(n) }
4194+
public fun SingleColumn<*>.takeLast(n: Int): ColumnSet<*> = transformSingle { it.children().takeLast(n) }
4195+
public fun SingleColumn<*>.drop(n: Int): ColumnSet<*> = transformSingle { it.children().drop(n) }
4196+
public fun SingleColumn<*>.dropLast(n: Int = 1): ColumnSet<*> = transformSingle { it.children().dropLast(n) }
41684197

41694198
public fun <C> ColumnSet<C>.drop(n: Int): ColumnSet<C> = transform { it.drop(n) }
41704199
public fun <C> ColumnSet<C>.take(n: Int): ColumnSet<C> = transform { it.take(n) }
@@ -4180,13 +4209,13 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
41804209
public fun <C> ColumnSet<C>.filter(predicate: Predicate<ColumnWithPath<C>>): ColumnSet<C> =
41814210
transform { it.filter(predicate) }
41824211

4183-
public fun SingleColumn<AnyRow>.nameContains(text: CharSequence): ColumnSet<*> = cols { it.name.contains(text) }
4212+
public fun SingleColumn<*>.nameContains(text: CharSequence): ColumnSet<*> = cols { it.name.contains(text) }
41844213
public fun <C> ColumnSet<C>.nameContains(text: CharSequence): ColumnSet<C> = cols { it.name.contains(text) }
4185-
public fun SingleColumn<AnyRow>.nameContains(regex: Regex): ColumnSet<*> = cols { it.name.contains(regex) }
4214+
public fun SingleColumn<*>.nameContains(regex: Regex): ColumnSet<*> = cols { it.name.contains(regex) }
41864215
public fun <C> ColumnSet<C>.nameContains(regex: Regex): ColumnSet<C> = cols { it.name.contains(regex) }
4187-
public fun SingleColumn<AnyRow>.startsWith(prefix: CharSequence): ColumnSet<*> = cols { it.name.startsWith(prefix) }
4216+
public fun SingleColumn<*>.startsWith(prefix: CharSequence): ColumnSet<*> = cols { it.name.startsWith(prefix) }
41884217
public fun <C> ColumnSet<C>.startsWith(prefix: CharSequence): ColumnSet<C> = cols { it.name.startsWith(prefix) }
4189-
public fun SingleColumn<AnyRow>.endsWith(suffix: CharSequence): ColumnSet<*> = cols { it.name.endsWith(suffix) }
4218+
public fun SingleColumn<*>.endsWith(suffix: CharSequence): ColumnSet<*> = cols { it.name.endsWith(suffix) }
41904219
public fun <C> ColumnSet<C>.endsWith(suffix: CharSequence): ColumnSet<C> = cols { it.name.endsWith(suffix) }
41914220

41924221
public fun <C> ColumnSet<C>.except(vararg other: ColumnSet<*>): ColumnSet<*> = except(other.toColumnSet())
@@ -4277,9 +4306,25 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
42774306

42784307
public fun <C> ColumnSet<C>.distinct(): ColumnSet<C> = DistinctColumnSet(this)
42794308

4309+
@Deprecated(
4310+
message = "Use recursively() instead",
4311+
replaceWith = ReplaceWith(
4312+
"this.colsOf(type, predicate).recursively(includeTopLevel = false)",
4313+
"org.jetbrains.kotlinx.dataframe.columns.recursively",
4314+
"org.jetbrains.kotlinx.dataframe.api.colsOf",
4315+
),
4316+
)
42804317
public fun <C> String.dfsOf(type: KType, predicate: (ColumnWithPath<C>) -> Boolean = { true }): ColumnSet<*> =
42814318
toColumnAccessor().dfsOf(type, predicate)
42824319

4320+
@Deprecated(
4321+
message = "Use recursively() instead",
4322+
replaceWith = ReplaceWith(
4323+
"this.colsOf(type, predicate).recursively(includeTopLevel = false)",
4324+
"org.jetbrains.kotlinx.dataframe.columns.recursively",
4325+
"org.jetbrains.kotlinx.dataframe.api.colsOf",
4326+
),
4327+
)
42834328
public fun <C> KProperty<*>.dfsOf(type: KType, predicate: (ColumnWithPath<C>) -> Boolean = { true }): ColumnSet<*> =
42844329
toColumnAccessor().dfsOf(type, predicate)
42854330

@@ -4398,8 +4443,7 @@ internal fun ColumnSet<*>.dfsInternal(predicate: (ColumnWithPath<*>) -> Boolean)
43984443
@Deprecated(
43994444
message = "Use recursively() instead",
44004445
replaceWith = ReplaceWith(
4401-
"this.colsOf(type, predicate).recursively()",
4402-
"org.jetbrains.kotlinx.dataframe.columns.recursively",
4446+
"this.colsOf(type, predicate).recursively(includeTopLevel = false)",
44034447
"org.jetbrains.kotlinx.dataframe.columns.recursively",
44044448
"org.jetbrains.kotlinx.dataframe.api.colsOf",
44054449
),
@@ -4410,7 +4454,7 @@ public fun <C> ColumnSet<*>.dfsOf(type: KType, predicate: (ColumnWithPath<C>) ->
44104454
@Deprecated(
44114455
message = "Use recursively() instead",
44124456
replaceWith = ReplaceWith(
4413-
"this.colsOf<C>(filter).recursively()",
4457+
"this.colsOf<C>(filter).recursively(includeTopLevel = false)",
44144458
"org.jetbrains.kotlinx.dataframe.columns.recursively",
44154459
"org.jetbrains.kotlinx.dataframe.api.colsOf",
44164460
),

0 commit comments

Comments
 (0)