Skip to content

Commit 0798186

Browse files
Automated commit of generated code
1 parent 2c68cf4 commit 0798186

33 files changed

+968
-211
lines changed

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

+182-21
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import org.jetbrains.kotlinx.dataframe.api.cast
66
import org.jetbrains.kotlinx.dataframe.api.concat
77
import org.jetbrains.kotlinx.dataframe.api.filter
88
import org.jetbrains.kotlinx.dataframe.api.map
9-
import org.jetbrains.kotlinx.dataframe.api.schema
109
import org.jetbrains.kotlinx.dataframe.api.take
1110
import org.jetbrains.kotlinx.dataframe.columns.BaseColumn
1211
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
@@ -15,16 +14,28 @@ import org.jetbrains.kotlinx.dataframe.columns.ColumnPath
1514
import org.jetbrains.kotlinx.dataframe.columns.ColumnResolutionContext
1615
import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath
1716
import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
17+
import org.jetbrains.kotlinx.dataframe.columns.TypeSuggestion
1818
import org.jetbrains.kotlinx.dataframe.columns.ValueColumn
19+
import org.jetbrains.kotlinx.dataframe.impl.api.chunkedImpl
1920
import org.jetbrains.kotlinx.dataframe.impl.columns.ColumnGroupImpl
2021
import org.jetbrains.kotlinx.dataframe.impl.columns.FrameColumnImpl
2122
import org.jetbrains.kotlinx.dataframe.impl.columns.ValueColumnImpl
2223
import org.jetbrains.kotlinx.dataframe.impl.columns.addPath
23-
import org.jetbrains.kotlinx.dataframe.impl.columns.guessColumnType
24+
import org.jetbrains.kotlinx.dataframe.impl.columns.createColumnGuessingType
2425
import org.jetbrains.kotlinx.dataframe.impl.columns.toColumnKind
2526
import org.jetbrains.kotlinx.dataframe.impl.getValuesType
26-
import org.jetbrains.kotlinx.dataframe.impl.splitByIndices
2727
import org.jetbrains.kotlinx.dataframe.schema.DataFrameSchema
28+
import org.jetbrains.kotlinx.dataframe.util.CHUNKED_IMPL_IMPORT
29+
import org.jetbrains.kotlinx.dataframe.util.CREATE
30+
import org.jetbrains.kotlinx.dataframe.util.CREATE_BY_INFERENCE_IMPORT
31+
import org.jetbrains.kotlinx.dataframe.util.CREATE_BY_TYPE_IMPORT
32+
import org.jetbrains.kotlinx.dataframe.util.CREATE_FRAME_COLUMN
33+
import org.jetbrains.kotlinx.dataframe.util.CREATE_FRAME_COLUMN_REPLACE
34+
import org.jetbrains.kotlinx.dataframe.util.CREATE_INLINE_REPLACE
35+
import org.jetbrains.kotlinx.dataframe.util.CREATE_REPLACE
36+
import org.jetbrains.kotlinx.dataframe.util.CREATE_WITH_TYPE_INFERENCE
37+
import org.jetbrains.kotlinx.dataframe.util.CREATE_WITH_TYPE_INFERENCE_REPLACE
38+
import org.jetbrains.kotlinx.dataframe.util.TYPE_SUGGESTION_IMPORT
2839
import kotlin.reflect.KClass
2940
import kotlin.reflect.KProperty
3041
import kotlin.reflect.KType
@@ -45,6 +56,9 @@ public interface DataColumn<out T> : BaseColumn<T> {
4556
/**
4657
* Creates [ValueColumn] using given [name], [values] and [type].
4758
*
59+
* Be careful; values are NOT checked to adhere to [type] for efficiency,
60+
* unless you specify [infer].
61+
*
4862
* @param name name of the column
4963
* @param values list of column values
5064
* @param type type of the column
@@ -56,12 +70,20 @@ public interface DataColumn<out T> : BaseColumn<T> {
5670
type: KType,
5771
infer: Infer = Infer.None,
5872
defaultValue: T? = null,
59-
): ValueColumn<T> = ValueColumnImpl(values, name, getValuesType(values, type, infer), defaultValue)
73+
): ValueColumn<T> =
74+
ValueColumnImpl(
75+
values = values,
76+
name = name,
77+
type = getValuesType(values, type, infer),
78+
defaultValue = defaultValue,
79+
)
6080

6181
/**
6282
* Creates [ValueColumn] using given [name], [values] and reified column [type].
6383
*
64-
* Note, that column [type] will be defined at compile-time using [T] argument
84+
* The column [type] will be defined at compile-time using [T] argument.
85+
* Be careful with casting; values are NOT checked to adhere to `reified` type [T] for efficiency,
86+
* unless you specify [infer].
6587
*
6688
* @param T type of the column
6789
* @param name name of the column
@@ -74,48 +96,187 @@ public interface DataColumn<out T> : BaseColumn<T> {
7496
infer: Infer = Infer.None,
7597
): ValueColumn<T> =
7698
createValueColumn(
77-
name,
78-
values,
79-
getValuesType(
80-
values,
81-
typeOf<T>(),
82-
infer,
83-
),
99+
name = name,
100+
values = values,
101+
type = typeOf<T>(),
102+
infer = infer,
84103
)
85104

105+
/**
106+
* Creates [ColumnGroup] using the given [name] and [df] representing the group of columns.
107+
*
108+
* @param name name of the column group
109+
* @param df the collection of columns representing the column group
110+
*/
86111
public fun <T> createColumnGroup(name: String, df: DataFrame<T>): ColumnGroup<T> = ColumnGroupImpl(name, df)
87112

88-
public fun <T> createFrameColumn(name: String, df: DataFrame<T>, startIndices: Iterable<Int>): FrameColumn<T> =
89-
FrameColumnImpl(name, df.splitByIndices(startIndices.asSequence()).toList(), lazy { df.schema() })
90-
113+
/**
114+
* Creates [FrameColumn] using the given [name] and list of dataframes [groups].
115+
*
116+
* [groups] must be a non-null list of [DataFrames][DataFrame], as [FrameColumn] does
117+
* not allow `null` values.
118+
* This is NOT checked at runtime for efficiency, nor is the validity of given [schema].
119+
*
120+
* @param name name of the frame column
121+
* @param groups the dataframes to be put in the column
122+
* @param schema an optional (lazily calculated) [DataFrameSchema] representing
123+
* the intersecting schema of [groups]
124+
*/
91125
public fun <T> createFrameColumn(
92126
name: String,
93127
groups: List<DataFrame<T>>,
94128
schema: Lazy<DataFrameSchema>? = null,
95129
): FrameColumn<T> = FrameColumnImpl(name, groups, schema)
96130

97-
public fun <T> createWithTypeInference(
131+
/**
132+
* Creates either a [FrameColumn], [ColumnGroup], or [ValueColumn] by analyzing each value in
133+
* [values].
134+
*
135+
* This is safer but slower than the other functions.
136+
*
137+
* Some conversions are done automatically to attempt to unify the values.
138+
*
139+
* For instance, when there are other [DataFrames][DataFrame] present in [values], we'll convert:
140+
* - `null` -> [DataFrame.empty]`()`
141+
* - [DataRow] -> single-row [DataFrame]
142+
* - [List][List]`<`[DataRow][DataRow]`<*>>` -> multi-row [DataFrame]
143+
*
144+
* to be able to create a [FrameColumn].
145+
* There are more conversions for other types as well.
146+
*
147+
* @param name name of the column
148+
* @param values the values to represent each row in the column
149+
* @param suggestedType optional suggested type for values. Default is [TypeSuggestion.Infer].
150+
* See [TypeSuggestion] for more information.
151+
* @param nullable optionally you can specify whether [values] contains nulls, if `null` it is inferred.
152+
*/
153+
public fun <T> createByInference(
98154
name: String,
99155
values: List<T>,
156+
suggestedType: TypeSuggestion = TypeSuggestion.Infer,
100157
nullable: Boolean? = null,
101-
): DataColumn<T> = guessColumnType(name, values, nullable = nullable)
158+
): DataColumn<T> =
159+
createColumnGuessingType(
160+
name = name,
161+
values = values,
162+
suggestedType = suggestedType,
163+
nullable = nullable,
164+
)
102165

103-
public fun <T> create(
166+
/**
167+
* Calls [createColumnGroup], [createFrameColumn], or [createValueColumn] based on
168+
* [type].
169+
*
170+
* This may be unsafe but is more efficient than [createByInference].
171+
*
172+
* Be careful; Values in [values] are NOT checked to adhere to the given [type], nor
173+
* do we check whether there are unexpected nulls among the values.
174+
*
175+
* It's recommended to use [createValueColumn], [createColumnGroup], and [createFrameColumn] instead.
176+
*
177+
* @param name the name of the column
178+
* @param values the values to represent each row in the column
179+
* @param type the (unchecked) common type of [values]
180+
* @param infer in case a [ValueColumn] is created, this controls how/whether types need to be inferred
181+
*/
182+
public fun <T> createByType(
104183
name: String,
105184
values: List<T>,
106185
type: KType,
107186
infer: Infer = Infer.None,
108187
): DataColumn<T> =
109-
when (type.toColumnKind()) {
188+
when (type.toColumnKind()) { // AnyFrame -> Frame, AnyRow? -> Group, else -> Value
110189
ColumnKind.Value -> createValueColumn(name, values, type, infer)
190+
111191
ColumnKind.Group -> createColumnGroup(name, (values as List<AnyRow?>).concat()).asDataColumn().cast()
192+
112193
ColumnKind.Frame -> createFrameColumn(name, values as List<AnyFrame>).asDataColumn().cast()
113194
}
114195

115-
public inline fun <reified T> create(name: String, values: List<T>, infer: Infer = Infer.None): DataColumn<T> =
116-
create(name, values, typeOf<T>(), infer)
196+
/**
197+
* Calls [createColumnGroup], [createFrameColumn], or [createValueColumn] based on
198+
* type [T].
199+
*
200+
* This is generally safe, as [T] can be inferred by the compiler,
201+
* and more efficient than [createByInference].
202+
*
203+
* Be careful when casting occurs; Values in [values] are NOT checked to adhere to the given/inferred type [T],
204+
* nor do we check whether there are unexpected nulls among the values.
205+
*
206+
* It's recommended to use [createValueColumn], [createColumnGroup], and [createFrameColumn] instead.
207+
*
208+
* @param T the (unchecked) common type of [values]
209+
* @param name the name of the column
210+
* @param values the values to represent each row in the column
211+
* @param infer in case a [ValueColumn] is created, this controls how/whether types need to be inferred
212+
*/
213+
public inline fun <reified T> createByType(
214+
name: String,
215+
values: List<T>,
216+
infer: Infer = Infer.None,
217+
): DataColumn<T> = createByType(name, values, typeOf<T>(), infer)
117218

219+
/** Creates an empty [DataColumn] with given [name]. */
118220
public fun empty(name: String = ""): AnyCol = createValueColumn(name, emptyList<Unit>(), typeOf<Unit>())
221+
222+
// region deprecated
223+
224+
@Deprecated(
225+
message = CREATE_FRAME_COLUMN,
226+
replaceWith = ReplaceWith(CREATE_FRAME_COLUMN_REPLACE, CHUNKED_IMPL_IMPORT),
227+
level = DeprecationLevel.WARNING,
228+
)
229+
public fun <T> createFrameColumn(name: String, df: DataFrame<T>, startIndices: Iterable<Int>): FrameColumn<T> =
230+
df.chunkedImpl(startIndices = startIndices, name = name)
231+
232+
@Deprecated(
233+
message = CREATE_WITH_TYPE_INFERENCE,
234+
replaceWith = ReplaceWith(
235+
CREATE_WITH_TYPE_INFERENCE_REPLACE,
236+
CREATE_BY_INFERENCE_IMPORT,
237+
TYPE_SUGGESTION_IMPORT,
238+
),
239+
level = DeprecationLevel.WARNING,
240+
)
241+
public fun <T> createWithTypeInference(
242+
name: String,
243+
values: List<T>,
244+
nullable: Boolean? = null,
245+
): DataColumn<T> =
246+
createByInference(
247+
name = name,
248+
values = values,
249+
suggestedType = TypeSuggestion.Infer,
250+
nullable = nullable,
251+
)
252+
253+
@Deprecated(
254+
message = CREATE,
255+
replaceWith = ReplaceWith(CREATE_REPLACE, CREATE_BY_TYPE_IMPORT),
256+
level = DeprecationLevel.WARNING,
257+
)
258+
public fun <T> create(
259+
name: String,
260+
values: List<T>,
261+
type: KType,
262+
infer: Infer = Infer.None,
263+
): DataColumn<T> =
264+
createByType(
265+
name = name,
266+
values = values,
267+
type = type,
268+
infer = infer,
269+
)
270+
271+
@Deprecated(
272+
message = CREATE,
273+
replaceWith = ReplaceWith(CREATE_INLINE_REPLACE, CREATE_BY_TYPE_IMPORT),
274+
level = DeprecationLevel.WARNING,
275+
)
276+
public inline fun <reified T> create(name: String, values: List<T>, infer: Infer = Infer.None): DataColumn<T> =
277+
createByType(name = name, values = values, type = typeOf<T>(), infer = infer)
278+
279+
// endregion
119280
}
120281

121282
public fun hasNulls(): Boolean = type().isMarkedNullable

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ import org.jetbrains.kotlinx.dataframe.DataRow
66
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
77
import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
88
import org.jetbrains.kotlinx.dataframe.columns.ValueColumn
9+
import org.jetbrains.kotlinx.dataframe.impl.api.chunkedImpl
910
import org.jetbrains.kotlinx.dataframe.impl.getListType
1011
import org.jetbrains.kotlinx.dataframe.nrow
1112
import org.jetbrains.kotlinx.dataframe.type
1213

14+
/**
15+
* Creates a [FrameColumn] from [this] by splitting the dataframe into
16+
* smaller ones, with their number of rows at most [size].
17+
*/
1318
public fun <T> DataFrame<T>.chunked(size: Int, name: String = "groups"): FrameColumn<T> {
1419
val startIndices = (0 until nrow step size)
15-
return DataColumn.createFrameColumn(name, this, startIndices)
20+
return this.chunkedImpl(startIndices, name)
1621
}
1722

1823
public fun <T> DataColumn<T>.chunked(size: Int): ValueColumn<List<T>> {

0 commit comments

Comments
 (0)