Skip to content

Commit 149e96f

Browse files
committed
docs for cols and all
1 parent d1f584d commit 149e96f

File tree

14 files changed

+530
-138
lines changed

14 files changed

+530
-138
lines changed

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

Lines changed: 438 additions & 89 deletions
Large diffs are not rendered by default.

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package org.jetbrains.kotlinx.dataframe.columns
22

33
import org.jetbrains.kotlinx.dataframe.*
4-
import org.jetbrains.kotlinx.dataframe.api.asColumnGroup
5-
import org.jetbrains.kotlinx.dataframe.api.cast
64
import org.jetbrains.kotlinx.dataframe.api.name
7-
import org.jetbrains.kotlinx.dataframe.api.toDataFrame
85
import org.jetbrains.kotlinx.dataframe.impl.columnName
96
import org.jetbrains.kotlinx.dataframe.impl.columns.RenamedColumnReference
107
import org.jetbrains.kotlinx.dataframe.impl.columns.addPath

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ public interface ColumnWithPath<out T> : DataColumn<T> {
2727
asColumnGroup().getColumnOrNull(accessor)?.addParentPath(path)
2828

2929
public fun children(): List<ColumnWithPath<Any?>> =
30-
if (isColumnGroup())
30+
if (isColumnGroup()) {
3131
data.asColumnGroup().columns().map { it.addParentPath(path) }
32-
else
32+
} else {
3333
emptyList()
34+
}
3435

3536
override fun path(): ColumnPath = path
3637

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import org.jetbrains.kotlinx.dataframe.columns.*
66
import org.jetbrains.kotlinx.dataframe.impl.columns.ColumnGroupWithParent
77
import org.jetbrains.kotlinx.dataframe.impl.columns.ColumnGroupWithPathImpl
88
import org.jetbrains.kotlinx.dataframe.impl.columns.addPath
9-
import org.jetbrains.kotlinx.dataframe.impl.columns.createColumnSet
109
import org.jetbrains.kotlinx.dataframe.impl.columns.missing.MissingColumnGroup
1110
import org.jetbrains.kotlinx.dataframe.impl.columns.missing.MissingDataColumn
1211

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ internal fun describeImpl(cols: List<AnyCol>): DataFrame<ColumnDescription> {
4141
.collectAll(true)
4242

4343
ColumnKind.Group ->
44-
if (recursively)
44+
if (recursively) {
4545
col.asColumnGroup()
4646
.columns()
4747
.map { it.addPath(col.path() + it.name) }
4848
.collectAll(true)
49-
else
49+
} else {
5050
listOf(col)
51+
}
5152

5253
ColumnKind.Value -> listOf(col)
5354
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,11 @@ internal fun <C> ColumnSet<C>.recursivelyImpl(
127127

128128
private fun flattenColumnWithPaths(list: List<ColumnWithPath<*>>, isSingleColumn: Boolean): List<ColumnWithPath<*>> {
129129
val cols =
130-
if (isSingleColumn && list.singleOrNull()?.isColumnGroup() == true)
130+
if (isSingleColumn && list.singleOrNull()?.isColumnGroup() == true) {
131131
list.single().children()
132-
else list
132+
} else {
133+
list
134+
}
133135

134136
return if (includeTopLevel) {
135137
cols.flattenRecursively()

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ internal fun <C> createColumnSet(resolver: (ColumnResolutionContext) -> List<Col
152152
context: ColumnResolutionContext,
153153
transformer: ColumnSetTransformer,
154154
): List<ColumnWithPath<C>> = throw UnsupportedOperationException("Not implemented")
155-
156155
}.wrap()
157156

158157
// region toColumnSet

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

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,11 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
857857

858858
/**
859859
* ## Cols
860-
* Creates a subset of columns ([ColumnSet]) from a parent [ColumnSet], -[ColumnGroup], or -[DataFrame].
860+
* Creates a subset of columns ([ColumnSet]) from the current [ColumnSet].
861+
*
862+
* If the current [ColumnSet] is a [SingleColumn] (and thus consists of only one column (or [column group][ColumnGroup])),
863+
* then `cols` will create a subset of its children.
864+
*
861865
* You can use either a [ColumnFilter] or any of the `vararg` overloads for all
862866
* [APIs][AccessApi] (+ [ColumnPath]).
863867
*
@@ -1204,15 +1208,11 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
12041208
private interface ColumnSetColsVarargStringDocs
12051209

12061210
/** @include [ColumnSetColsVarargStringDocs] */
1207-
@Suppress("UNCHECKED_CAST")
12081211
public fun <C> ColumnSet<C>.cols(
12091212
firstCol: String,
12101213
vararg otherCols: String,
1211-
): ColumnSet<C> = transformWithContext {
1212-
dataFrameOf(it)
1213-
.asColumnGroup()
1214-
.cols(firstCol, *otherCols)
1215-
.resolve(this) as List<ColumnWithPath<C>>
1214+
): ColumnSet<C> = headPlusArray(firstCol, otherCols).let { names ->
1215+
filter { it.name in names }
12161216
}
12171217

12181218
/**
@@ -1340,11 +1340,8 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
13401340
public fun <C> ColumnSet<C>.cols(
13411341
firstCol: KProperty<C>,
13421342
vararg otherCols: KProperty<C>,
1343-
): ColumnSet<C> = transformWithContext {
1344-
dataFrameOf(it)
1345-
.asColumnGroup()
1346-
.cols(firstCol, *otherCols)
1347-
.resolve(this)
1343+
): ColumnSet<C> = headPlusArray(firstCol, otherCols).map { it.name }.let { names ->
1344+
filter { it.name in names }
13481345
}
13491346

13501347
/** @include [ColumnSetColsVarargKPropertyDocs] */
@@ -1451,15 +1448,13 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
14511448

14521449
// region indices
14531450

1454-
@Suppress("UNCHECKED_CAST")
14551451
public fun <C> ColumnSet<C>.cols(
14561452
firstIndex: Int,
14571453
vararg otherIndices: Int,
1458-
): ColumnSet<C> = transformWithContext {
1459-
dataFrameOf(it)
1460-
.asColumnGroup()
1461-
.cols(firstIndex, *otherIndices)
1462-
.resolve(this) as List<ColumnWithPath<C>>
1454+
): ColumnSet<C> = headPlusArray(firstIndex, otherIndices).let { indices ->
1455+
transform { list ->
1456+
indices.map { list[it] }
1457+
}
14631458
}
14641459

14651460
public operator fun <C> ColumnSet<C>.get(
@@ -1516,14 +1511,8 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
15161511

15171512
// region ranges
15181513

1519-
@Suppress("UNCHECKED_CAST")
15201514
public fun <C> ColumnSet<C>.cols(range: IntRange): ColumnSet<C> =
1521-
transformWithContext {
1522-
dataFrameOf(it)
1523-
.asColumnGroup()
1524-
.cols(range)
1525-
.resolve(this) as List<ColumnWithPath<C>>
1526-
}
1515+
transform { it.subList(range.first, range.last + 1) }
15271516

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

@@ -1605,12 +1594,70 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
16051594
// endregion
16061595

16071596
// region all
1597+
1598+
/**
1599+
* ## All
1600+
* Creates a new [ColumnSet] that contains all columns from the current [ColumnSet].
1601+
*
1602+
* If the current [ColumnSet] is a [SingleColumn] (and thus consists of only one column (or [column group][ColumnGroup])),
1603+
* then `all` will create a new [ColumnSet] consisting of its children.
1604+
*
1605+
* This makes the function essentially equivalent to [cols()][ColumnSet.cols].
1606+
*
1607+
* #### For example:
1608+
* `df.`[move][DataFrame.move]` { `[all][ColumnSet.all]`().`[recursively][ColumnSet.recursively]`() }.`[under][MoveClause.under]`("info")`
1609+
*
1610+
* `df.`[select][DataFrame.select]` { myGroup.`[all][ColumnSet.all]`() }`
1611+
*
1612+
* #### Examples for this overload:
1613+
*
1614+
* {@includeArg [CommonAllDocs.Examples]}
1615+
*
1616+
* @see [cols\]
1617+
*/
1618+
private interface CommonAllDocs {
1619+
1620+
/** Example argument */
1621+
interface Examples
1622+
}
1623+
1624+
/**
1625+
* @include [CommonAllDocs]
1626+
* @arg [CommonAllDocs.Examples]
1627+
*
1628+
* `df.`[select][select]` { `[cols][cols]` { "a" in `[name][ColumnWithPath.name]` }.`[all][all]`() }`
1629+
* {@include [LineBreak]}
1630+
* NOTE: This is an identity call and can be omitted in most cases. However, it can still prove useful
1631+
* for readability or in combination with [recursively].
1632+
*/
16081633
public fun ColumnSet<*>.all(): ColumnSet<*> = wrap()
16091634

1635+
/**
1636+
* @include [CommonAllDocs]
1637+
* @arg [CommonAllDocs.Examples]
1638+
*
1639+
* `df.`[select][select]` { `[all][all]`() }`
1640+
*
1641+
* `df.`[select][select]` { myGroup.`[all][all]`() }`
1642+
*
1643+
* `df.`[select][select]` { "pathTo"["myGroup"].`[all][all]`() }`
1644+
*/
16101645
public fun SingleColumn<*>.all(): ColumnSet<*> = transformSingle { it.children() }
16111646

1647+
/**
1648+
* @include [CommonAllDocs]
1649+
* @arg [CommonAllDocs.Examples]
1650+
*
1651+
* `df.`[select][select]` { "myGroupCol".`[all][all]`() }`
1652+
*/
16121653
public fun String.all(): ColumnSet<*> = toColumnAccessor().all()
16131654

1655+
/**
1656+
* @include [CommonAllDocs]
1657+
* @arg [CommonAllDocs.Examples]
1658+
*
1659+
* `df.`[select][select]` { Type::columnGroup.`[all][all]`() }`
1660+
*/
16141661
public fun KProperty<*>.all(): ColumnSet<*> = toColumnAccessor().all()
16151662

16161663
// region allDfs
@@ -1702,7 +1749,6 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
17021749
includeTopLevel: Boolean = true,
17031750
): ColumnSet<*> = recursively(includeTopLevel = includeTopLevel, includeGroups = includeGroups)
17041751

1705-
17061752
// endregion
17071753

17081754
// region allAfter
@@ -2051,7 +2097,6 @@ public inline fun <T, reified R> ColumnsSelectionDsl<T>.expr(
20512097
internal fun <T, C> ColumnsSelector<T, C>.filter(predicate: (ColumnWithPath<C>) -> Boolean): ColumnsSelector<T, C> =
20522098
{ this@filter(it, it).filter(predicate) }
20532099

2054-
20552100
/**
20562101
* If this [ColumnSet] is a [SingleColumn], it
20572102
* returns a new [ColumnSet] containing the children of this [SingleColumn] that

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/columns/ColumnReference.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package org.jetbrains.kotlinx.dataframe.columns
22

33
import org.jetbrains.kotlinx.dataframe.*
4-
import org.jetbrains.kotlinx.dataframe.api.asColumnGroup
5-
import org.jetbrains.kotlinx.dataframe.api.cast
64
import org.jetbrains.kotlinx.dataframe.api.name
7-
import org.jetbrains.kotlinx.dataframe.api.toDataFrame
85
import org.jetbrains.kotlinx.dataframe.impl.columnName
96
import org.jetbrains.kotlinx.dataframe.impl.columns.RenamedColumnReference
107
import org.jetbrains.kotlinx.dataframe.impl.columns.addPath

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ public interface ColumnWithPath<out T> : DataColumn<T> {
2727
asColumnGroup().getColumnOrNull(accessor)?.addParentPath(path)
2828

2929
public fun children(): List<ColumnWithPath<Any?>> =
30-
if (isColumnGroup())
30+
if (isColumnGroup()) {
3131
data.asColumnGroup().columns().map { it.addParentPath(path) }
32-
else
32+
} else {
3333
emptyList()
34+
}
3435

3536
override fun path(): ColumnPath = path
3637

0 commit comments

Comments
 (0)