Skip to content

Commit 5f5b5d3

Browse files
authored
Merge pull request #372 from Kotlin/continued-columnsselectiondsl
ColumnsSelection DSL overhaul
2 parents d3b278f + 31c71c3 commit 5f5b5d3

File tree

324 files changed

+76373
-9413
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

324 files changed

+76373
-9413
lines changed

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ allprojects {
7171
"filename",
7272
"comment-spacing",
7373
"curly-spacing",
74-
"experimental:annotation-spacing"
74+
"experimental:annotation-spacing",
75+
"no-unused-imports", // broken
7576
)
7677
}
7778
} catch (_: UnknownDomainObjectException) {

core/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ val processKDocsMain by creatingProcessDocTask(
205205
ARG_DOC_PROCESSOR,
206206
COMMENT_DOC_PROCESSOR,
207207
SAMPLE_DOC_PROCESSOR,
208+
// REMOVE_ESCAPE_CHARS_PROCESSOR, TODO enable when doc preprocessor hits 0.3.0
208209
)
209210

210211
arguments += ARG_DOC_PROCESSOR_LOG_NOT_FOUND to false

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public interface ColumnsContainer<out T> {
6161
public operator fun <R> get(column: KProperty<DataFrame<R>>): FrameColumn<R> = get(column.columnName).asAnyFrameColumn().castFrameColumn()
6262

6363
public fun <C> get(columns: ColumnsSelector<T, C>): List<DataColumn<C>>
64-
public operator fun <C> get(column: ColumnSelector<T, C>): DataColumn<C> = get(column as ColumnsSelector<T, C>).single()
64+
public fun <C> get(column: ColumnSelector<T, C>): DataColumn<C> = get(column as ColumnsSelector<T, C>).single()
6565

6666
// endregion
6767
}

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
package org.jetbrains.kotlinx.dataframe
22

3-
import org.jetbrains.kotlinx.dataframe.api.*
4-
import org.jetbrains.kotlinx.dataframe.columns.*
5-
import org.jetbrains.kotlinx.dataframe.impl.columns.*
3+
import org.jetbrains.kotlinx.dataframe.api.Infer
4+
import org.jetbrains.kotlinx.dataframe.api.asDataColumn
5+
import org.jetbrains.kotlinx.dataframe.api.cast
6+
import org.jetbrains.kotlinx.dataframe.api.concat
7+
import org.jetbrains.kotlinx.dataframe.api.filter
8+
import org.jetbrains.kotlinx.dataframe.api.map
9+
import org.jetbrains.kotlinx.dataframe.api.schema
10+
import org.jetbrains.kotlinx.dataframe.api.take
11+
import org.jetbrains.kotlinx.dataframe.api.type
12+
import org.jetbrains.kotlinx.dataframe.columns.BaseColumn
13+
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
14+
import org.jetbrains.kotlinx.dataframe.columns.ColumnKind
15+
import org.jetbrains.kotlinx.dataframe.columns.ColumnPath
16+
import org.jetbrains.kotlinx.dataframe.columns.ColumnResolutionContext
17+
import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath
18+
import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
19+
import org.jetbrains.kotlinx.dataframe.columns.ValueColumn
20+
import org.jetbrains.kotlinx.dataframe.impl.columns.ColumnGroupImpl
21+
import org.jetbrains.kotlinx.dataframe.impl.columns.FrameColumnImpl
22+
import org.jetbrains.kotlinx.dataframe.impl.columns.ValueColumnImpl
23+
import org.jetbrains.kotlinx.dataframe.impl.columns.addPath
24+
import org.jetbrains.kotlinx.dataframe.impl.columns.guessColumnType
25+
import org.jetbrains.kotlinx.dataframe.impl.columns.toColumnKind
626
import org.jetbrains.kotlinx.dataframe.impl.getValuesType
727
import org.jetbrains.kotlinx.dataframe.impl.splitByIndices
828
import org.jetbrains.kotlinx.dataframe.schema.DataFrameSchema

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.jetbrains.kotlinx.dataframe
22

33
import org.jetbrains.kotlinx.dataframe.aggregation.Aggregatable
44
import org.jetbrains.kotlinx.dataframe.aggregation.AggregateGroupedBody
5+
import org.jetbrains.kotlinx.dataframe.api.ColumnsSelectionDsl
56
import org.jetbrains.kotlinx.dataframe.api.add
67
import org.jetbrains.kotlinx.dataframe.api.cast
78
import org.jetbrains.kotlinx.dataframe.api.getRows
@@ -68,18 +69,14 @@ public interface DataFrame<out T> : Aggregatable<T>, ColumnsContainer<T> {
6869

6970
// region get columns
7071

71-
override operator fun <C> get(columns: ColumnsSelector<T, C>): List<DataColumn<C>> =
72+
/**
73+
* Returns a list of columns selected by [columns], a [ColumnsSelectionDsl].
74+
*
75+
* NOTE: This doesn't work in [ColumnsSelectionDsl], use [ColumnsSelectionDsl.cols] to select columns by predicate.
76+
*/
77+
override fun <C> get(columns: ColumnsSelector<T, C>): List<DataColumn<C>> =
7278
getColumnsImpl(UnresolvedColumnsPolicy.Fail, columns)
7379

74-
public operator fun get(first: AnyColumnReference, vararg other: AnyColumnReference): DataFrame<T> =
75-
select { (listOf(first) + other).toColumnSet() }
76-
77-
public operator fun get(first: String, vararg other: String): DataFrame<T> =
78-
select { (listOf(first) + other).toColumnSet() }
79-
80-
public operator fun get(columnRange: ClosedRange<String>): DataFrame<T> =
81-
select { columnRange.start..columnRange.endInclusive }
82-
8380
// endregion
8481

8582
// region get rows
@@ -103,6 +100,25 @@ public interface DataFrame<out T> : Aggregatable<T>, ColumnsContainer<T> {
103100
// endregion
104101
}
105102

103+
// region get columns
104+
105+
/**
106+
* Returns a list of columns selected by [columns], a [ColumnsSelectionDsl].
107+
*/
108+
public operator fun <T, C> DataFrame<T>.get(columns: ColumnsSelector<T, C>): List<DataColumn<C>> =
109+
this.get(columns)
110+
111+
public operator fun <T> DataFrame<T>.get(first: AnyColumnReference, vararg other: AnyColumnReference): DataFrame<T> =
112+
select { (listOf(first) + other).toColumnSet() }
113+
114+
public operator fun <T> DataFrame<T>.get(first: String, vararg other: String): DataFrame<T> =
115+
select { (listOf(first) + other).toColumnSet() }
116+
117+
public operator fun <T> DataFrame<T>.get(columnRange: ClosedRange<String>): DataFrame<T> =
118+
select { columnRange.start..columnRange.endInclusive }
119+
120+
// endregion
121+
106122
internal val ColumnsContainer<*>.ncol get() = columnsCount()
107123
internal val AnyFrame.nrow get() = rowsCount()
108124
internal val AnyFrame.indices get() = indices()

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@ import org.jetbrains.kotlinx.dataframe.api.ColumnsSelectionDsl
44
import org.jetbrains.kotlinx.dataframe.api.pathOf
55
import org.jetbrains.kotlinx.dataframe.columns.ColumnPath
66
import org.jetbrains.kotlinx.dataframe.columns.ColumnSet
7+
import org.jetbrains.kotlinx.dataframe.columns.SingleColumn
78
import org.jetbrains.kotlinx.dataframe.impl.aggregation.ConfiguredAggregateColumn
89

910
public interface ColumnsForAggregateSelectionDsl<out T> : ColumnsSelectionDsl<T> {
1011

1112
public infix fun <C> ColumnSet<C>.default(defaultValue: C): ColumnSet<C> =
1213
ConfiguredAggregateColumn.withDefault(this, defaultValue)
1314

15+
public infix fun <C> SingleColumn<C>.default(defaultValue: C): SingleColumn<C> =
16+
ConfiguredAggregateColumn.withDefault(this, defaultValue).single()
17+
1418
public fun path(vararg names: String): ColumnPath = ColumnPath(names.asList())
1519

1620
public infix fun <C> ColumnSet<C>.into(name: String): ColumnSet<C> = ConfiguredAggregateColumn.withPath(this, pathOf(name))
1721

22+
public infix fun <C> SingleColumn<C>.into(name: String): SingleColumn<C> =
23+
ConfiguredAggregateColumn.withPath(this, pathOf(name)).single()
24+
1825
public infix fun <C> ColumnSet<C>.into(path: ColumnPath): ColumnSet<C> = ConfiguredAggregateColumn.withPath(this, path)
26+
27+
public infix fun <C> SingleColumn<C>.into(path: ColumnPath): SingleColumn<C> =
28+
ConfiguredAggregateColumn.withPath(this, path).single()
1929
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package org.jetbrains.kotlinx.dataframe.aggregation
22

33
import org.jetbrains.kotlinx.dataframe.Selector
4-
import org.jetbrains.kotlinx.dataframe.columns.ColumnSet
4+
import org.jetbrains.kotlinx.dataframe.columns.ColumnsResolver
55

66
public typealias AggregateBody<T, R> = Selector<AggregateDsl<T>, R>
77

88
public typealias AggregateGroupedBody<G, R> = Selector<AggregateGroupedDsl<G>, R>
99

10-
public typealias ColumnsForAggregateSelector<T, C> = Selector<ColumnsForAggregateSelectionDsl<T>, ColumnSet<C>>
10+
public typealias ColumnsForAggregateSelector<T, C> = Selector<ColumnsForAggregateSelectionDsl<T>, ColumnsResolver<C>>

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

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import org.jetbrains.kotlinx.dataframe.columns.ColumnAccessor
66
import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
77
import org.jetbrains.kotlinx.dataframe.columns.ColumnSet
88
import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath
9+
import org.jetbrains.kotlinx.dataframe.columns.ColumnsResolver
910
import org.jetbrains.kotlinx.dataframe.columns.SingleColumn
1011

1112
/**
12-
* [Predicate] is used to reach a [Boolean] result using the given instance of `T` as `it`.
13+
* ## Predicate
14+
*
15+
* [Predicate] is a lambda function expecting a [Boolean] result given an instance of `T` as `it`.
1316
*
1417
* Shorthand for:
1518
* ```kotlin
@@ -19,7 +22,9 @@ import org.jetbrains.kotlinx.dataframe.columns.SingleColumn
1922
public typealias Predicate<T> = (it: T) -> Boolean
2023

2124
/**
22-
* [Selector] is used to express or select any instance of `R` using the given instance of `T` as `this` and `it`.
25+
* ## Selector
26+
*
27+
* [Selector] is a lambda function expecting an `R` result given an instance of `T` as context (`this` and `it`).
2328
*
2429
* Shorthand for:
2530
* ```kotlin
@@ -31,8 +36,11 @@ public typealias Selector<T, R> = T.(it: T) -> R
3136
// region selectors
3237

3338
/**
34-
* [DataFrameExpression] is used to express or select any instance of `R` using the given instance of [DataFrame]`<T>`
35-
* as `this` and `it`.
39+
* ## DataFrame Expression
40+
*
41+
* [DataFrameExpression] is a lambda function expecting an `R` result given an instance of [DataFrame]`<T>` as context
42+
* (`this` and `it`).
43+
* `R` can be selected or expressed.
3644
*
3745
* Shorthand for:
3846
* ```kotlin
@@ -42,8 +50,10 @@ public typealias Selector<T, R> = T.(it: T) -> R
4250
public typealias DataFrameExpression<T, R> = Selector<DataFrame<T>, R>
4351

4452
/**
45-
* [RowExpression] is used to express or select any instance of `R` using the given instance of [DataRow]`<T>` as
46-
* `this` and `it`.
53+
* ## Row Expression
54+
*
55+
* [RowExpression] is a lambda function expecting an `R` result given an instance of [DataRow]`<T>` as context
56+
* (`this` and `it`). `R` can be selected or expressed.
4757
*
4858
* Shorthand for:
4959
* ```kotlin
@@ -53,8 +63,10 @@ public typealias DataFrameExpression<T, R> = Selector<DataFrame<T>, R>
5363
public typealias RowExpression<T, R> = Selector<DataRow<T>, R>
5464

5565
/**
56-
* [RowValueExpression] is used to express or select any instance of `R` using the given value `it: C` and the given
57-
* instance of [DataRow]`<T>` as `this`.
66+
* ## Row Value Expression
67+
*
68+
* [RowValueExpression] is a lambda function expecting an `R` result given the value `it: C` and an
69+
* instance of [DataRow]`<T>` as context (`this`). `R` can be selected or expressed.
5870
*
5971
* Shorthand for:
6072
* ```kotlin
@@ -64,8 +76,10 @@ public typealias RowExpression<T, R> = Selector<DataRow<T>, R>
6476
public typealias RowValueExpression<T, C, R> = DataRow<T>.(it: C) -> R
6577

6678
/**
67-
* [RowColumnExpression] is used to express or select any instance of `R` using the given instances of
68-
* [DataRow]`<T>` as `row` and [DataColumn]`<C>` as `col`.
79+
* ## Row Column Expression
80+
*
81+
* [RowColumnExpression] is a lambda function expecting an `R` result given an instance of [DataRow]`<T>` as
82+
* `row` and [DataColumn]`<C>` as `col`. `R` can be selected or expressed.
6983
*
7084
* Shorthand for:
7185
* ```kotlin
@@ -75,8 +89,10 @@ public typealias RowValueExpression<T, C, R> = DataRow<T>.(it: C) -> R
7589
public typealias RowColumnExpression<T, C, R> = (row: DataRow<T>, col: DataColumn<C>) -> R
7690

7791
/**
78-
* [ColumnExpression] is used to express or select any instance of `R` using the given instance of [DataColumn]`<C>` as
79-
* `this` and `it`.
92+
* ## Column Expression
93+
*
94+
* [ColumnExpression] is a lambda function expecting an `R` result given an instance of [DataColumn]`<C>` as context
95+
* (`this` and `it`). `R` can be selected or expressed.
8096
*
8197
* Shorthand for:
8298
* ```kotlin
@@ -86,8 +102,12 @@ public typealias RowColumnExpression<T, C, R> = (row: DataRow<T>, col: DataColum
86102
public typealias ColumnExpression<C, R> = Selector<DataColumn<C>, R>
87103

88104
/**
89-
* [ColumnSelector] is used to express or select a single column, represented by [SingleColumn]`<C>`, using the
90-
* context of [ColumnsSelectionDsl]`<T>` as `this` and `it`.
105+
* ## Column Selector
106+
*
107+
* [ColumnSelector] is a lambda function expecting a [SingleColumn]<`C`> result given an instance of [ColumnsSelectionDsl]`<T>`
108+
* as context (`this` and `it`). [SingleColumn]`<C>` can be selected or expressed.
109+
*
110+
* See [Columns Selection DSL][ColumnsSelectionDsl] for more information.
91111
*
92112
* Shorthand for:
93113
* ```kotlin
@@ -97,22 +117,31 @@ public typealias ColumnExpression<C, R> = Selector<DataColumn<C>, R>
97117
public typealias ColumnSelector<T, C> = Selector<ColumnsSelectionDsl<T>, SingleColumn<C>>
98118

99119
/**
100-
* [ColumnsSelector] is used to express or select multiple columns, represented by [ColumnSet]`<C>`, using the
101-
* context of [ColumnsSelectionDsl]`<T>` as `this` and `it`.
120+
* ## Columns Selector
121+
*
122+
* [ColumnsSelector] is a lambda function expecting a [ColumnsResolver]<`C`> ([SingleColumn]<`C`> or [ColumnSet]<`C`>)
123+
* result given an instance of [ColumnsSelectionDsl]`<T>` as context (`this` and `it`).
124+
* [ColumnsResolver]<`C`> can be selected or expressed.
125+
*
126+
* See [Columns Selection DSL][ColumnsSelectionDsl] for more information.
102127
*
103128
* Shorthand for:
104129
* ```kotlin
105-
* ColumnsSelectionDsl<T>.(it: ColumnsSelectionDsl<T>) -> ColumnSet<C>
130+
* ColumnsSelectionDsl<T>.(it: ColumnsSelectionDsl<T>) -> ColumnsResolver<C>
106131
* ```
107132
*/
108-
public typealias ColumnsSelector<T, C> = Selector<ColumnsSelectionDsl<T>, ColumnSet<C>>
133+
public typealias ColumnsSelector<T, C> = Selector<ColumnsSelectionDsl<T>, ColumnsResolver<C>>
109134

110135
// endregion
111136

112137
// region filters
113138

114139
/**
115-
* [RowFilter] is used to filter or find rows using the given instance of [DataRow]`<T>` as `this` and `it`.
140+
* ## Row Filter
141+
*
142+
* [RowFilter] is a lambda function expecting a [Boolean] result given an instance of [DataRow]`<T>` as context
143+
* (`this` and `it`).
144+
*
116145
* Return `true` if the row should be included in the result.
117146
*
118147
* Shorthand for:
@@ -123,7 +152,11 @@ public typealias ColumnsSelector<T, C> = Selector<ColumnsSelectionDsl<T>, Column
123152
public typealias RowFilter<T> = RowExpression<T, Boolean>
124153

125154
/**
126-
* [ColumnFilter] is used to filter or find columns using the given instance of [ColumnWithPath]`<T>` as `it`.
155+
* ## Column Filter
156+
*
157+
* [ColumnFilter] is a lambda function expecting a [Boolean] result given an instance of [DataColumn]`<C>` as context
158+
* (`this` and `it`).
159+
*
127160
* Return `true` if the column should be included in the result.
128161
*
129162
* Shorthand for:
@@ -134,8 +167,11 @@ public typealias RowFilter<T> = RowExpression<T, Boolean>
134167
public typealias ColumnFilter<T> = Predicate<ColumnWithPath<T>>
135168

136169
/**
137-
* [RowValueFilter] is used to filter or find rows using the given value of `it: C` and the given instance of
138-
* [DataRow]`<T>` as `this`.
170+
* ## Row Value Filter
171+
*
172+
* [RowValueFilter] is a lambda function expecting a [Boolean] result given the value `it: C` and an instance
173+
* of [DataRow]`<T>` as context (`this`).
174+
*
139175
* Return `true` if the row should be included in the result.
140176
*
141177
* Shorthand for:

0 commit comments

Comments
 (0)