@@ -11,36 +11,27 @@ import org.jetbrains.kotlinx.dataframe.DataColumn
11
11
import org.jetbrains.kotlinx.dataframe.DataFrame
12
12
import org.jetbrains.kotlinx.dataframe.DataRow
13
13
import org.jetbrains.kotlinx.dataframe.Predicate
14
- import org.jetbrains.kotlinx.dataframe.columns.ColumnAccessor
15
- import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
16
- import org.jetbrains.kotlinx.dataframe.columns.ColumnPath
17
- import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
18
- import org.jetbrains.kotlinx.dataframe.columns.ColumnResolutionContext
19
- import org.jetbrains.kotlinx.dataframe.columns.ColumnSet
20
- import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath
21
- import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
22
- import org.jetbrains.kotlinx.dataframe.columns.SingleColumn
23
- import org.jetbrains.kotlinx.dataframe.columns.renamedReference
14
+ import org.jetbrains.kotlinx.dataframe.columns.*
15
+ import org.jetbrains.kotlinx.dataframe.documentation.AccessApi
24
16
import org.jetbrains.kotlinx.dataframe.hasNulls
25
17
import org.jetbrains.kotlinx.dataframe.impl.columnName
26
- import org.jetbrains.kotlinx.dataframe.impl.columns.ColumnsList
27
- import org.jetbrains.kotlinx.dataframe.impl.columns.DistinctColumnSet
28
- import org.jetbrains.kotlinx.dataframe.impl.columns.addPath
29
- import org.jetbrains.kotlinx.dataframe.impl.columns.allColumnsExcept
30
- import org.jetbrains.kotlinx.dataframe.impl.columns.changePath
31
- import org.jetbrains.kotlinx.dataframe.impl.columns.createColumnSet
32
- import org.jetbrains.kotlinx.dataframe.impl.columns.getAt
33
- import org.jetbrains.kotlinx.dataframe.impl.columns.getChildrenAt
34
- import org.jetbrains.kotlinx.dataframe.impl.columns.single
35
- import org.jetbrains.kotlinx.dataframe.impl.columns.toColumns
36
- import org.jetbrains.kotlinx.dataframe.impl.columns.top
37
- import org.jetbrains.kotlinx.dataframe.impl.columns.transform
38
- import org.jetbrains.kotlinx.dataframe.impl.columns.transformSingle
18
+ import org.jetbrains.kotlinx.dataframe.impl.columns.*
39
19
import org.jetbrains.kotlinx.dataframe.impl.columns.tree.dfs
40
20
import kotlin.reflect.KProperty
41
21
import kotlin.reflect.KType
42
22
import kotlin.reflect.typeOf
43
23
24
+ /* *
25
+ * Referring to a column in the selection DSL can be done in several ways corresponding to all
26
+ * [Access APIs][AccessApi]:
27
+ * TODO: [Issue #286](https://github.com/Kotlin/dataframe/issues/286)
28
+ */
29
+ private interface CommonColumnSelectionExamples
30
+
31
+ /* * [Column Selection DSL][ColumnSelectionDsl] */
32
+ internal interface ColumnSelectionDslLink
33
+
34
+ /* * TODO: [Issue #286](https://github.com/Kotlin/dataframe/issues/286) */
44
35
public interface ColumnSelectionDsl <out T > : ColumnsContainer <T > {
45
36
46
37
public operator fun <C > ColumnReference<C>.invoke (): DataColumn <C > = get(this )
@@ -56,6 +47,10 @@ public interface ColumnSelectionDsl<out T> : ColumnsContainer<T> {
56
47
public operator fun String.get (column : String ): ColumnPath = pathOf(this , column)
57
48
}
58
49
50
+ /* * [Columns Selection DSL][ColumnsSelectionDsl] */
51
+ internal interface ColumnsSelectionDslLink
52
+
53
+ /* * TODO: [Issue #286](https://github.com/Kotlin/dataframe/issues/286) */
59
54
public interface ColumnsSelectionDsl <out T > : ColumnSelectionDsl <T >, SingleColumn <DataRow <T >> {
60
55
61
56
public fun <C > ColumnSet<C>.first (condition : ColumnFilter <C >): SingleColumn <C > =
@@ -70,24 +65,26 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
70
65
71
66
public fun ColumnsContainer <* >.group (name : String ): ColumnGroupReference = name.toColumnOf()
72
67
73
- public operator fun String.rangeTo (endInclusive : String ): ColumnSet <* > = toColumnAccessor().rangeTo(endInclusive.toColumnAccessor())
74
-
75
- public operator fun AnyColumnReference.rangeTo (endInclusive : AnyColumnReference ): ColumnSet <* > = object : ColumnSet <Any ?> {
76
- override fun resolve (context : ColumnResolutionContext ): List <ColumnWithPath <Any ?>> {
77
- val startPath = this @rangeTo.resolveSingle(context)!! .path
78
- val endPath = endInclusive.resolveSingle(context)!! .path
79
- val parentPath = startPath.parent()!!
80
- require(parentPath == endPath.parent()) { " Start and end columns have different parent column paths" }
81
- val parentCol = context.df.getColumnGroup(parentPath)
82
- val startIndex = parentCol.getColumnIndex(startPath.name)
83
- val endIndex = parentCol.getColumnIndex(endPath.name)
84
- return (startIndex.. endIndex).map {
85
- parentCol.getColumn(it).let {
86
- it.addPath(parentPath + it.name)
68
+ public operator fun String.rangeTo (endInclusive : String ): ColumnSet <* > =
69
+ toColumnAccessor().rangeTo(endInclusive.toColumnAccessor())
70
+
71
+ public operator fun AnyColumnReference.rangeTo (endInclusive : AnyColumnReference ): ColumnSet <* > =
72
+ object : ColumnSet <Any ?> {
73
+ override fun resolve (context : ColumnResolutionContext ): List <ColumnWithPath <Any ?>> {
74
+ val startPath = this @rangeTo.resolveSingle(context)!! .path
75
+ val endPath = endInclusive.resolveSingle(context)!! .path
76
+ val parentPath = startPath.parent()!!
77
+ require(parentPath == endPath.parent()) { " Start and end columns have different parent column paths" }
78
+ val parentCol = context.df.getColumnGroup(parentPath)
79
+ val startIndex = parentCol.getColumnIndex(startPath.name)
80
+ val endIndex = parentCol.getColumnIndex(endPath.name)
81
+ return (startIndex.. endIndex).map {
82
+ parentCol.getColumn(it).let {
83
+ it.addPath(parentPath + it.name)
84
+ }
87
85
}
88
86
}
89
87
}
90
- }
91
88
92
89
public fun none (): ColumnSet <* > = ColumnsList <Any ?>(emptyList())
93
90
@@ -115,7 +112,8 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
115
112
116
113
public fun <C > ColumnSet<DataRow<C>>.select (vararg columns : String ): ColumnSet <* > = select { columns.toColumns() }
117
114
118
- public fun <C , R > ColumnSet<DataRow<C>>.select (vararg columns : KProperty <R >): ColumnSet <R > = select { columns.toColumns() }
115
+ public fun <C , R > ColumnSet<DataRow<C>>.select (vararg columns : KProperty <R >): ColumnSet <R > =
116
+ select { columns.toColumns() }
119
117
120
118
public fun <C , R > ColumnSet<DataRow<C>>.select (selector : ColumnsSelector <C , R >): ColumnSet <R > = createColumnSet {
121
119
this @select.resolve(it).flatMap { group ->
@@ -145,7 +143,8 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
145
143
146
144
// region allDfs
147
145
148
- public fun ColumnSet <* >.allDfs (includeGroups : Boolean = false): ColumnSet <Any ?> = if (includeGroups) dfs { true } else dfs { ! it.isColumnGroup() }
146
+ public fun ColumnSet <* >.allDfs (includeGroups : Boolean = false): ColumnSet <Any ?> =
147
+ if (includeGroups) dfs { true } else dfs { ! it.isColumnGroup() }
149
148
150
149
public fun String.allDfs (includeGroups : Boolean = false): ColumnSet <Any ?> = toColumnAccessor().allDfs(includeGroups)
151
150
@@ -242,7 +241,9 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
242
241
243
242
public fun <C > col (property : KProperty <C >): ColumnAccessor <C > = property.toColumnAccessor()
244
243
245
- public operator fun ColumnSet <* >.get (colName : String ): ColumnSet <Any ?> = transform { it.mapNotNull { it.getChild(colName) } }
244
+ public operator fun ColumnSet <* >.get (colName : String ): ColumnSet <Any ?> =
245
+ transform { it.mapNotNull { it.getChild(colName) } }
246
+
246
247
public operator fun <C > ColumnSet <* >.get (column : ColumnReference <C >): ColumnSet <C > = cols(column)
247
248
248
249
public fun SingleColumn<AnyRow>.take (n : Int ): ColumnSet <* > = transformSingle { it.children().take(n) }
@@ -288,8 +289,11 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
288
289
public infix fun <C > ColumnReference<C>.into (column : KProperty <* >): ColumnReference <C > = named(column.columnName)
289
290
290
291
public infix fun String.into (newName : String ): ColumnReference <Any ?> = toColumnAccessor().into(newName)
291
- public infix fun String.into (column : ColumnAccessor <* >): ColumnReference <Any ?> = toColumnAccessor().into(column.name())
292
- public infix fun String.into (column : KProperty <* >): ColumnReference <Any ?> = toColumnAccessor().into(column.columnName)
292
+ public infix fun String.into (column : ColumnAccessor <* >): ColumnReference <Any ?> =
293
+ toColumnAccessor().into(column.name())
294
+
295
+ public infix fun String.into (column : KProperty <* >): ColumnReference <Any ?> =
296
+ toColumnAccessor().into(column.columnName)
293
297
294
298
public infix fun <C > ColumnReference<C>.named (newName : String ): ColumnReference <C > = renamedReference(newName)
295
299
public infix fun <C > ColumnReference<C>.named (name : KProperty <* >): ColumnReference <C > = named(name.columnName)
@@ -311,6 +315,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
311
315
public infix fun <C > KProperty<C>.and (other : String ): ColumnSet <Any ?> = toColumnAccessor() and other
312
316
public infix fun <C > KProperty<C>.and (other : KProperty <C >): ColumnSet <C > =
313
317
toColumnAccessor() and other.toColumnAccessor()
318
+
314
319
public infix fun <C > KProperty<C>.and (other : ColumnsSelector <T , C >): ColumnSet <C > = toColumnAccessor() and other()
315
320
316
321
// endregion
@@ -339,7 +344,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
339
344
public inline fun <T , reified R > ColumnsSelectionDsl<T>.expr (
340
345
name : String = "",
341
346
infer : Infer = Infer .Nulls ,
342
- noinline expression : AddExpression <T , R >
347
+ noinline expression : AddExpression <T , R >,
343
348
): DataColumn <R > = mapToColumn(name, infer, expression)
344
349
345
350
internal fun <T , C > ColumnsSelector <T , C >.filter (predicate : (ColumnWithPath <C >) -> Boolean ): ColumnsSelector <T , C > =
0 commit comments