Skip to content

Commit d42c883

Browse files
committed
added initial generated files
1 parent a4fecde commit d42c883

File tree

271 files changed

+21098
-0
lines changed

Some content is hidden

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

271 files changed

+21098
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.jetbrains.kotlinx.dataframe
2+
3+
import org.jetbrains.kotlinx.dataframe.api.ColumnSelectionDsl
4+
import org.jetbrains.kotlinx.dataframe.api.asColumnGroup
5+
import org.jetbrains.kotlinx.dataframe.api.cast
6+
import org.jetbrains.kotlinx.dataframe.api.castFrameColumn
7+
import org.jetbrains.kotlinx.dataframe.api.getColumn
8+
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
9+
import org.jetbrains.kotlinx.dataframe.columns.ColumnPath
10+
import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
11+
import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
12+
import org.jetbrains.kotlinx.dataframe.impl.columnName
13+
import org.jetbrains.kotlinx.dataframe.impl.columns.asAnyFrameColumn
14+
import kotlin.reflect.KProperty
15+
16+
/**
17+
* Provides access to [columns][DataColumn].
18+
*
19+
* Base interface for [DataFrame] and [ColumnSelectionDsl]
20+
*
21+
* @param T Schema marker. Used to generate extension properties for typed column access.
22+
*/
23+
public interface ColumnsContainer<out T> {
24+
25+
// region columns
26+
27+
public fun columns(): List<AnyCol>
28+
public fun columnsCount(): Int
29+
public fun containsColumn(name: String): Boolean
30+
public fun containsColumn(path: ColumnPath): Boolean
31+
public fun getColumnIndex(name: String): Int
32+
33+
// endregion
34+
35+
// region getColumnOrNull
36+
37+
public fun getColumnOrNull(name: String): AnyCol?
38+
public fun getColumnOrNull(index: Int): AnyCol?
39+
public fun <R> getColumnOrNull(column: ColumnReference<R>): DataColumn<R>?
40+
public fun getColumnOrNull(path: ColumnPath): AnyCol?
41+
public fun <R> getColumnOrNull(column: ColumnSelector<T, R>): DataColumn<R>?
42+
43+
// endregion
44+
45+
// region get
46+
47+
public operator fun get(columnName: String): AnyCol = getColumn(columnName)
48+
public operator fun get(columnPath: ColumnPath): AnyCol = getColumn(columnPath)
49+
50+
public operator fun <R> get(column: DataColumn<R>): DataColumn<R> = getColumn(column.name()).cast()
51+
public operator fun <R> get(column: DataColumn<DataRow<R>>): ColumnGroup<R> = getColumn(column)
52+
public operator fun <R> get(column: DataColumn<DataFrame<R>>): FrameColumn<R> = getColumn(column)
53+
54+
public operator fun <R> get(column: ColumnReference<R>): DataColumn<R> = getColumn(column)
55+
public operator fun <R> get(column: ColumnReference<DataRow<R>>): ColumnGroup<R> = getColumn(column)
56+
public operator fun <R> get(column: ColumnReference<DataFrame<R>>): FrameColumn<R> = getColumn(column)
57+
58+
public operator fun <R> get(column: KProperty<R>): DataColumn<R> = get(column.columnName).cast()
59+
public operator fun <R> get(column: KProperty<DataRow<R>>): ColumnGroup<R> = get(column.columnName).asColumnGroup().cast()
60+
public operator fun <R> get(column: KProperty<DataFrame<R>>): FrameColumn<R> = get(column.columnName).asAnyFrameColumn().castFrameColumn()
61+
62+
public operator fun <C> get(columns: ColumnsSelector<T, C>): List<DataColumn<C>>
63+
public operator fun <C> get(column: ColumnSelector<T, C>): DataColumn<C> = get(column as ColumnsSelector<T, C>).single()
64+
65+
// endregion
66+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package org.jetbrains.kotlinx.dataframe
2+
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.schema
9+
import org.jetbrains.kotlinx.dataframe.api.take
10+
import org.jetbrains.kotlinx.dataframe.columns.BaseColumn
11+
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
12+
import org.jetbrains.kotlinx.dataframe.columns.ColumnKind
13+
import org.jetbrains.kotlinx.dataframe.columns.ColumnPath
14+
import org.jetbrains.kotlinx.dataframe.columns.ColumnResolutionContext
15+
import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath
16+
import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
17+
import org.jetbrains.kotlinx.dataframe.columns.ValueColumn
18+
import org.jetbrains.kotlinx.dataframe.impl.columns.ColumnGroupImpl
19+
import org.jetbrains.kotlinx.dataframe.impl.columns.FrameColumnImpl
20+
import org.jetbrains.kotlinx.dataframe.impl.columns.ValueColumnImpl
21+
import org.jetbrains.kotlinx.dataframe.impl.columns.addPath
22+
import org.jetbrains.kotlinx.dataframe.impl.columns.guessColumnType
23+
import org.jetbrains.kotlinx.dataframe.impl.columns.toColumnKind
24+
import org.jetbrains.kotlinx.dataframe.impl.getValuesType
25+
import org.jetbrains.kotlinx.dataframe.impl.splitByIndices
26+
import org.jetbrains.kotlinx.dataframe.schema.DataFrameSchema
27+
import kotlin.reflect.KClass
28+
import kotlin.reflect.KProperty
29+
import kotlin.reflect.KType
30+
import kotlin.reflect.typeOf
31+
32+
/**
33+
* Column with [name] and [values] of specific [type].
34+
*
35+
* Base interface for [ValueColumn] and [FrameColumn], but not for [ColumnGroup]. However, implementations for all three [column kinds][ColumnKind] derive from DataColumn and can cast to it safely.
36+
* Column operations that have signature clash with [DataFrame] API ([filter], [take], [map] etc.) are defined for [DataColumn] and not for [BaseColumn].
37+
*
38+
* @param T type of values in the column.
39+
*/
40+
public interface DataColumn<out T> : BaseColumn<T> {
41+
42+
public companion object {
43+
44+
/**
45+
* Creates [ValueColumn] using given [name], [values] and [type].
46+
*
47+
* @param name name of the column
48+
* @param values list of column values
49+
* @param type type of the column
50+
* @param infer column type inference mode
51+
*/
52+
public fun <T> createValueColumn(
53+
name: String,
54+
values: List<T>,
55+
type: KType,
56+
infer: Infer = Infer.None,
57+
defaultValue: T? = null
58+
): ValueColumn<T> = ValueColumnImpl(values, name, getValuesType(values, type, infer), defaultValue)
59+
60+
/**
61+
* Creates [ValueColumn] using given [name], [values] and reified column [type].
62+
*
63+
* Note, that column [type] will be defined at compile-time using [T] argument
64+
*
65+
* @param T type of the column
66+
* @param name name of the column
67+
* @param values list of column values
68+
* @param infer column type inference mode
69+
*/
70+
public inline fun <reified T> createValueColumn(name: String, values: List<T>, infer: Infer = Infer.None): ValueColumn<T> = createValueColumn(
71+
name, values,
72+
getValuesType(
73+
values,
74+
typeOf<T>(),
75+
infer
76+
)
77+
)
78+
79+
public fun <T> createColumnGroup(name: String, df: DataFrame<T>): ColumnGroup<T> = ColumnGroupImpl(name, df)
80+
81+
public fun <T> createFrameColumn(
82+
name: String,
83+
df: DataFrame<T>,
84+
startIndices: Iterable<Int>
85+
): FrameColumn<T> =
86+
FrameColumnImpl(name, df.splitByIndices(startIndices.asSequence()).toList(), lazy { df.schema() })
87+
88+
public fun <T> createFrameColumn(
89+
name: String,
90+
groups: List<DataFrame<T>>,
91+
schema: Lazy<DataFrameSchema>? = null
92+
): FrameColumn<T> = FrameColumnImpl(name, groups, schema)
93+
94+
public fun <T> createWithTypeInference(name: String, values: List<T>, nullable: Boolean? = null): DataColumn<T> = guessColumnType(name, values, nullable = nullable)
95+
96+
public fun <T> create(name: String, values: List<T>, type: KType, infer: Infer = Infer.None): DataColumn<T> {
97+
return when (type.toColumnKind()) {
98+
ColumnKind.Value -> createValueColumn(name, values, type, infer)
99+
ColumnKind.Group -> createColumnGroup(name, (values as List<AnyRow?>).concat()).asDataColumn().cast()
100+
ColumnKind.Frame -> createFrameColumn(name, values as List<AnyFrame>).asDataColumn().cast()
101+
}
102+
}
103+
104+
public inline fun <reified T> create(name: String, values: List<T>, infer: Infer = Infer.None): DataColumn<T> = create(name, values, typeOf<T>(), infer)
105+
106+
public fun empty(name: String = ""): AnyCol = createValueColumn(name, emptyList<Unit>(), typeOf<Unit>())
107+
}
108+
109+
public fun hasNulls(): Boolean = type().isMarkedNullable
110+
111+
override fun distinct(): DataColumn<T>
112+
113+
override fun get(indices: Iterable<Int>): DataColumn<T>
114+
115+
override fun rename(newName: String): DataColumn<T>
116+
117+
override fun resolveSingle(context: ColumnResolutionContext): ColumnWithPath<T>? = this.addPath()
118+
119+
override operator fun getValue(thisRef: Any?, property: KProperty<*>): DataColumn<T> = super.getValue(thisRef, property) as DataColumn<T>
120+
121+
public operator fun iterator(): Iterator<T> = values().iterator()
122+
123+
public override operator fun get(range: IntRange): DataColumn<T>
124+
}
125+
126+
public val AnyCol.name: String get() = name()
127+
public val AnyCol.path: ColumnPath get() = path()
128+
129+
public val <T> DataColumn<T>.values: Iterable<T> get() = values()
130+
public val AnyCol.hasNulls: Boolean get() = hasNulls()
131+
public val AnyCol.size: Int get() = size()
132+
public val AnyCol.indices: IntRange get() = indices()
133+
134+
public val AnyCol.type: KType get() = type()
135+
public val AnyCol.kind: ColumnKind get() = kind()
136+
public val AnyCol.typeClass: KClass<*> get() = type.classifier as? KClass<*> ?: error("Cannot cast ${type.classifier?.javaClass} to a ${KClass::class}. Column $name: $type")
137+
138+
public fun AnyBaseCol.indices(): IntRange = 0 until size()
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.jetbrains.kotlinx.dataframe
2+
3+
import org.jetbrains.kotlinx.dataframe.aggregation.Aggregatable
4+
import org.jetbrains.kotlinx.dataframe.aggregation.AggregateGroupedBody
5+
import org.jetbrains.kotlinx.dataframe.api.add
6+
import org.jetbrains.kotlinx.dataframe.api.cast
7+
import org.jetbrains.kotlinx.dataframe.api.getRows
8+
import org.jetbrains.kotlinx.dataframe.api.indices
9+
import org.jetbrains.kotlinx.dataframe.api.rows
10+
import org.jetbrains.kotlinx.dataframe.api.select
11+
import org.jetbrains.kotlinx.dataframe.api.toDataFrame
12+
import org.jetbrains.kotlinx.dataframe.columns.UnresolvedColumnsPolicy
13+
import org.jetbrains.kotlinx.dataframe.impl.DataFrameImpl
14+
import org.jetbrains.kotlinx.dataframe.impl.DataFrameSize
15+
import org.jetbrains.kotlinx.dataframe.impl.getColumnsImpl
16+
import org.jetbrains.kotlinx.dataframe.impl.headPlusArray
17+
import org.jetbrains.kotlinx.dataframe.impl.headPlusIterable
18+
import org.jetbrains.kotlinx.dataframe.impl.schema.createEmptyDataFrameOf
19+
import kotlin.reflect.KType
20+
21+
/**
22+
* Readonly interface for an ordered list of [columns][DataColumn].
23+
*
24+
* Columns in `DataFrame` have distinct non-empty [names][DataColumn.name] and equal [sizes][DataColumn.size].
25+
*
26+
* @param T Schema marker. It identifies column schema and is used to generate schema-specific extension properties for typed data access. It is covariant, so `DataFrame<A>` is assignable to variable of type `DataFrame<B>` if `A` is a subtype of `B`.
27+
*/
28+
public interface DataFrame<out T> : Aggregatable<T>, ColumnsContainer<T> {
29+
30+
public companion object {
31+
public val Empty: AnyFrame = DataFrameImpl<Unit>(emptyList(), 0)
32+
public fun empty(nrow: Int = 0): AnyFrame = if (nrow == 0) Empty else DataFrameImpl<Unit>(emptyList(), nrow)
33+
34+
public inline fun <reified T> emptyOf(): DataFrame<T> = createEmptyDataFrameOf(T::class).cast()
35+
}
36+
37+
// region columns
38+
39+
public fun columnNames(): List<String>
40+
41+
public fun columnTypes(): List<KType>
42+
43+
// endregion
44+
45+
// region rows
46+
47+
public fun rowsCount(): Int
48+
49+
public operator fun iterator(): Iterator<DataRow<T>> = rows().iterator()
50+
51+
// endregion
52+
53+
public fun <R> aggregate(body: AggregateGroupedBody<T, R>): DataRow<T>
54+
55+
// region get columns
56+
57+
override operator fun <C> get(columns: ColumnsSelector<T, C>): List<DataColumn<C>> =
58+
getColumnsImpl(UnresolvedColumnsPolicy.Fail, columns)
59+
60+
public operator fun get(first: AnyColumnReference, vararg other: AnyColumnReference): DataFrame<T> = select(listOf(first) + other)
61+
public operator fun get(first: String, vararg other: String): DataFrame<T> = select(listOf(first) + other)
62+
public operator fun get(columnRange: ClosedRange<String>): DataFrame<T> =
63+
select { columnRange.start..columnRange.endInclusive }
64+
65+
// endregion
66+
67+
// region get rows
68+
69+
public operator fun get(index: Int): DataRow<T>
70+
public operator fun get(indices: Iterable<Int>): DataFrame<T> = getRows(indices)
71+
public operator fun get(range: IntRange): DataFrame<T> = getRows(range)
72+
public operator fun get(first: IntRange, vararg ranges: IntRange): DataFrame<T> =
73+
getRows(headPlusArray(first, ranges).asSequence().flatMap { it.asSequence() }.asIterable())
74+
75+
public operator fun get(firstIndex: Int, vararg otherIndices: Int): DataFrame<T> =
76+
get(headPlusIterable(firstIndex, otherIndices.asIterable()))
77+
78+
// endregion
79+
80+
// region plus columns
81+
82+
public operator fun plus(col: AnyBaseCol): DataFrame<T> = add(col)
83+
public operator fun plus(cols: Iterable<AnyBaseCol>): DataFrame<T> = (columns() + cols).toDataFrame().cast()
84+
85+
// endregion
86+
}
87+
88+
internal val ColumnsContainer<*>.ncol get() = columnsCount()
89+
internal val AnyFrame.nrow get() = rowsCount()
90+
internal val AnyFrame.indices get() = indices()
91+
internal val AnyFrame.size: DataFrameSize get() = size()
92+
93+
public fun AnyFrame.size(): DataFrameSize = DataFrameSize(ncol, nrow)

0 commit comments

Comments
 (0)