Skip to content

Commit 4f4bf58

Browse files
Automated commit of generated code
1 parent 2dcf2d9 commit 4f4bf58

4 files changed

Lines changed: 113 additions & 2 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public interface ColumnsForAggregateSelectionDsl<out T> : ColumnsSelectionDsl<T>
2020
public infix fun <C> ColumnSet<C>.into(name: String): ColumnSet<C> =
2121
ConfiguredAggregateColumn.withPath(this, pathOf(name))
2222

23-
public infix fun <C> SingleColumn<C>.into(name: String): SingleColumn<C> =
24-
ConfiguredAggregateColumn.withPath(this, pathOf(name))
23+
override infix fun <C> SingleColumn<C>.into(newName: String): SingleColumn<C> =
24+
ConfiguredAggregateColumn.withPath(this, pathOf(newName))
2525

2626
public infix fun <C> ColumnSet<C>.into(path: ColumnPath): ColumnSet<C> =
2727
ConfiguredAggregateColumn.withPath(this, path)

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.jetbrains.kotlinx.dataframe.columns.ColumnAccessor
1111
import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
1212
import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath
1313
import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
14+
import org.jetbrains.kotlinx.dataframe.columns.SingleColumn
1415
import org.jetbrains.kotlinx.dataframe.columns.renamedReference
1516
import org.jetbrains.kotlinx.dataframe.columns.toColumnSet
1617
import org.jetbrains.kotlinx.dataframe.documentation.AccessApiLink
@@ -23,6 +24,7 @@ import org.jetbrains.kotlinx.dataframe.documentation.LineBreak
2324
import org.jetbrains.kotlinx.dataframe.documentation.SelectingColumns
2425
import org.jetbrains.kotlinx.dataframe.impl.api.renameImpl
2526
import org.jetbrains.kotlinx.dataframe.impl.columnName
27+
import org.jetbrains.kotlinx.dataframe.impl.columns.renamedColumn
2628
import org.jetbrains.kotlinx.dataframe.impl.toCamelCaseByDelimiters
2729
import org.jetbrains.kotlinx.dataframe.util.DEPRECATED_ACCESS_API
2830
import kotlin.reflect.KProperty
@@ -641,6 +643,10 @@ public interface RenameColumnsSelectionDsl {
641643
*/
642644
interface KPropertyReceiver
643645

646+
/**
647+
*/
648+
interface SingleColumnReceiver
649+
644650
/**
645651
*/
646652
interface ColumnReferenceParam
@@ -922,6 +928,34 @@ public interface RenameColumnsSelectionDsl {
922928
public infix fun <C> KProperty<C>.named(nameOf: KProperty<*>): ColumnReference<C> =
923929
toColumnAccessor().named(nameOf.columnName)
924930

931+
/**
932+
* ## Rename: `named` / `into`
933+
* Renaming a column in the [ColumnsSelectionDsl][org.jetbrains.kotlinx.dataframe.api.ColumnsSelectionDsl] is done by calling the `infix` functions
934+
* [named][org.jetbrains.kotlinx.dataframe.columns.ColumnReference.named] or [into][org.jetbrains.kotlinx.dataframe.columns.ColumnReference.into]. They behave exactly the same,
935+
* so it's up to contextual preference which one to use. Any combination of [Access API][org.jetbrains.kotlinx.dataframe.documentation.AccessApi] can be
936+
* used to specify the column to rename and which name should be used instead.
937+
*
938+
* ### Check out: [Grammar][org.jetbrains.kotlinx.dataframe.api.RenameColumnsSelectionDsl.Grammar]
939+
*
940+
* #### For Example:
941+
*
942+
* `df.`[select][org.jetbrains.kotlinx.dataframe.DataFrame.select]` { name `[named][org.jetbrains.kotlinx.dataframe.columns.ColumnReference.named]` "Full Name" }`
943+
*
944+
* `df.`[select][org.jetbrains.kotlinx.dataframe.DataFrame.select]` { `[expr][org.jetbrains.kotlinx.dataframe.api.expr]` { 0 } `[into][org.jetbrains.kotlinx.dataframe.columns.ColumnReference.into]` "zeroes" }`
945+
*
946+
* `df.`[select][org.jetbrains.kotlinx.dataframe.DataFrame.select]` { "colA" `[named][kotlin.String.named]` Type::colB }`
947+
*
948+
* #### Example for this overload:
949+
*
950+
* `df.`[select][org.jetbrains.kotlinx.dataframe.DataFrame.select]` { col(0) `[named][SingleColumn.named]` "columnB" }`
951+
*
952+
* @receiver The [SingleColumn] referencing the column to rename.
953+
* @param [newName] A [String] used to specify the new name of the column.
954+
* @return A [ColumnReference][org.jetbrains.kotlinx.dataframe.columns.ColumnReference] to the renamed column.
955+
*/
956+
@Interpretable("Named1")
957+
public infix fun <C> SingleColumn<C>.named(newName: String): SingleColumn<C> = renamedColumn(newName)
958+
925959
// endregion
926960

927961
// region into
@@ -1184,6 +1218,34 @@ public interface RenameColumnsSelectionDsl {
11841218
@AccessApiOverload
11851219
public infix fun <C> KProperty<C>.into(nameOf: KProperty<*>): ColumnReference<C> = named(nameOf)
11861220

1221+
/**
1222+
* ## Rename: `named` / `into`
1223+
* Renaming a column in the [ColumnsSelectionDsl][org.jetbrains.kotlinx.dataframe.api.ColumnsSelectionDsl] is done by calling the `infix` functions
1224+
* [named][org.jetbrains.kotlinx.dataframe.columns.ColumnReference.named] or [into][org.jetbrains.kotlinx.dataframe.columns.ColumnReference.into]. They behave exactly the same,
1225+
* so it's up to contextual preference which one to use. Any combination of [Access API][org.jetbrains.kotlinx.dataframe.documentation.AccessApi] can be
1226+
* used to specify the column to rename and which name should be used instead.
1227+
*
1228+
* ### Check out: [Grammar][org.jetbrains.kotlinx.dataframe.api.RenameColumnsSelectionDsl.Grammar]
1229+
*
1230+
* #### For Example:
1231+
*
1232+
* `df.`[select][org.jetbrains.kotlinx.dataframe.DataFrame.select]` { name `[named][org.jetbrains.kotlinx.dataframe.columns.ColumnReference.named]` "Full Name" }`
1233+
*
1234+
* `df.`[select][org.jetbrains.kotlinx.dataframe.DataFrame.select]` { `[expr][org.jetbrains.kotlinx.dataframe.api.expr]` { 0 } `[into][org.jetbrains.kotlinx.dataframe.columns.ColumnReference.into]` "zeroes" }`
1235+
*
1236+
* `df.`[select][org.jetbrains.kotlinx.dataframe.DataFrame.select]` { "colA" `[named][kotlin.String.named]` Type::colB }`
1237+
*
1238+
* #### Example for this overload:
1239+
*
1240+
* `df.`[select][org.jetbrains.kotlinx.dataframe.DataFrame.select]` { col(0) `[into][SingleColumn.into]` "columnB" }`
1241+
*
1242+
* @receiver The [SingleColumn] referencing the column to rename.
1243+
* @param [newName] A [String] used to specify the new name of the column.
1244+
* @return A [ColumnReference][org.jetbrains.kotlinx.dataframe.columns.ColumnReference] to the renamed column.
1245+
*/
1246+
@Interpretable("Named1")
1247+
public infix fun <C> SingleColumn<C>.into(newName: String): SingleColumn<C> = named(newName)
1248+
11871249
// endregion
11881250
}
11891251

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.jetbrains.kotlinx.dataframe.impl.columns
2+
3+
import org.jetbrains.kotlinx.dataframe.columns.ColumnResolutionContext
4+
import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath
5+
import org.jetbrains.kotlinx.dataframe.columns.SingleColumn
6+
7+
internal class RenamedSingleColumn<C>(val source: SingleColumn<C>, val name: String) : SingleColumn<C> {
8+
9+
override fun resolveSingle(context: ColumnResolutionContext): ColumnWithPath<C>? =
10+
source.resolveSingle(context)?.let {
11+
it.data.rename(name).addPath(it.path)
12+
}
13+
}
14+
15+
internal fun <C> SingleColumn<C>.renamedColumn(newName: String): SingleColumn<C> = RenamedSingleColumn(this, newName)

core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/rename.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,40 @@ class RenameTests : ColumnsSelectionDslTests() {
156156
df.select { col("age") into col("age2") },
157157
).shouldAllBeEqual()
158158
}
159+
160+
@Test
161+
fun `col by index named`() {
162+
val df = dataFrameOf(
163+
"col0" to columnOf(1, 4),
164+
"col1" to columnOf(2, 5),
165+
"col2" to columnOf(3, 6),
166+
)
167+
168+
listOf(
169+
df.select { col(0) and (col(1) named "renamed") and col(2) },
170+
df.select { col(0) and (col(1) into "renamed") and col(2) },
171+
df.select { col(0) and (col<Int>(1) named "renamed") and col(2) },
172+
df.select { col(0) and (col<Int>(1) into "renamed") and col(2) },
173+
).shouldAllBeEqual()
174+
175+
val result = df.select { col(0) and (col(1) named "renamed") and col(2) }
176+
result.columnNames() shouldBe listOf("col0", "renamed", "col2")
177+
result["renamed"].toList() shouldBe listOf(2, 5)
178+
}
179+
180+
@Test
181+
fun `col by index named with convert`() {
182+
val df = dataFrameOf(
183+
"a" to columnOf(1, 4),
184+
"b" to columnOf(2, 5),
185+
"c" to columnOf(3, 6),
186+
)
187+
188+
val result = df.convert { col<Int>(0) named "newA" }.with { it * 10 }
189+
190+
result.columnNames() shouldBe listOf("newA", "b", "c")
191+
result["newA"].toList() shouldBe listOf(10, 40)
192+
}
159193
}
160194

161195
class RenameToCamelCaseTests {

0 commit comments

Comments
 (0)