Skip to content

Commit 4054b2d

Browse files
committed
fixed single function with docs
1 parent 97d9879 commit 4054b2d

File tree

6 files changed

+246
-25
lines changed

6 files changed

+246
-25
lines changed

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

Lines changed: 123 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import org.jetbrains.kotlinx.dataframe.DataColumn
1111
import org.jetbrains.kotlinx.dataframe.DataFrame
1212
import org.jetbrains.kotlinx.dataframe.DataRow
1313
import org.jetbrains.kotlinx.dataframe.Predicate
14-
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
1514
import org.jetbrains.kotlinx.dataframe.columns.ColumnAccessor
1615
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
1716
import org.jetbrains.kotlinx.dataframe.columns.ColumnPath
@@ -38,12 +37,11 @@ import org.jetbrains.kotlinx.dataframe.impl.columns.changePath
3837
import org.jetbrains.kotlinx.dataframe.impl.columns.createColumnSet
3938
import org.jetbrains.kotlinx.dataframe.impl.columns.getAt
4039
import org.jetbrains.kotlinx.dataframe.impl.columns.getChildrenAt
41-
import org.jetbrains.kotlinx.dataframe.impl.columns.single
40+
import org.jetbrains.kotlinx.dataframe.impl.columns.singleImpl
4241
import org.jetbrains.kotlinx.dataframe.impl.columns.top
4342
import org.jetbrains.kotlinx.dataframe.impl.columns.transform
4443
import org.jetbrains.kotlinx.dataframe.impl.columns.transformSingle
4544
import org.jetbrains.kotlinx.dataframe.impl.columns.tree.dfs
46-
import org.jetbrains.kotlinx.dataframe.io.read
4745
import kotlin.reflect.KProperty
4846
import kotlin.reflect.KType
4947
import kotlin.reflect.typeOf
@@ -267,6 +265,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
267265
*
268266
* @param [condition\] The optional [ColumnFilter] condition that the column must adhere to.
269267
* @return A [SingleColumn] containing the first column that adheres to the given [condition\].
268+
* @throws [NoSuchElementException\] if no column adheres to the given [condition\].
270269
* @see [last\]
271270
*/
272271
private interface CommonFirstDocs {
@@ -287,10 +286,11 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
287286
*
288287
* @param [condition] The optional [ColumnFilter][org.jetbrains.kotlinx.dataframe.ColumnFilter] condition that the column must adhere to.
289288
* @return A [SingleColumn][org.jetbrains.kotlinx.dataframe.columns.SingleColumn] containing the first column that adheres to the given [condition].
289+
* @throws [NoSuchElementException] if no column adheres to the given [condition].
290290
* @see [last]
291291
*/
292292
public fun <C> ColumnSet<C>.first(condition: ColumnFilter<C> = { true }): SingleColumn<C> =
293-
transform { listOf(it.first(condition)) }.single()
293+
transform { listOf(it.first(condition)) }.singleImpl()
294294

295295
/**
296296
* ## First
@@ -304,6 +304,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
304304
*
305305
* @param [condition] The optional [ColumnFilter][org.jetbrains.kotlinx.dataframe.ColumnFilter] condition that the column must adhere to.
306306
* @return A [SingleColumn][org.jetbrains.kotlinx.dataframe.columns.SingleColumn] containing the first column that adheres to the given [condition].
307+
* @throws [NoSuchElementException] if no column adheres to the given [condition].
307308
* @see [last]
308309
*/
309310
public fun first(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
@@ -321,6 +322,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
321322
*
322323
* @param [condition] The optional [ColumnFilter][org.jetbrains.kotlinx.dataframe.ColumnFilter] condition that the column must adhere to.
323324
* @return A [SingleColumn][org.jetbrains.kotlinx.dataframe.columns.SingleColumn] containing the first column that adheres to the given [condition].
325+
* @throws [NoSuchElementException] if no column adheres to the given [condition].
324326
* @see [last]
325327
*/
326328
public fun ColumnGroupReference.first(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
@@ -336,6 +338,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
336338
*
337339
* @param [condition] The optional [ColumnFilter][org.jetbrains.kotlinx.dataframe.ColumnFilter] condition that the column must adhere to.
338340
* @return A [SingleColumn][org.jetbrains.kotlinx.dataframe.columns.SingleColumn] containing the first column that adheres to the given [condition].
341+
* @throws [NoSuchElementException] if no column adheres to the given [condition].
339342
* @see [last]
340343
*/
341344
public fun String.first(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
@@ -351,9 +354,10 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
351354
*
352355
* @param [condition] The optional [ColumnFilter][org.jetbrains.kotlinx.dataframe.ColumnFilter] condition that the column must adhere to.
353356
* @return A [SingleColumn][org.jetbrains.kotlinx.dataframe.columns.SingleColumn] containing the first column that adheres to the given [condition].
357+
* @throws [NoSuchElementException] if no column adheres to the given [condition].
354358
* @see [last]
355359
*/
356-
public fun KProperty<*>.first(condition: ColumnFilter<*>): SingleColumn<*> =
360+
public fun KProperty<*>.first(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
357361
getColumnGroup(this).first(condition)
358362

359363

@@ -367,6 +371,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
367371
*
368372
* @param [condition\] The optional [ColumnFilter] condition that the column must adhere to.
369373
* @return A [SingleColumn] containing the last column that adheres to the given [condition\].
374+
* @throws [NoSuchElementException\] if no column adheres to the given [condition\].
370375
* @see [first\]
371376
*/
372377
private interface CommonLastDocs {
@@ -387,10 +392,11 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
387392
*
388393
* @param [condition] The optional [ColumnFilter][org.jetbrains.kotlinx.dataframe.ColumnFilter] condition that the column must adhere to.
389394
* @return A [SingleColumn][org.jetbrains.kotlinx.dataframe.columns.SingleColumn] containing the last column that adheres to the given [condition].
395+
* @throws [NoSuchElementException] if no column adheres to the given [condition].
390396
* @see [first]
391397
*/
392398
public fun <C> ColumnSet<C>.last(condition: ColumnFilter<C> = { true }): SingleColumn<C> =
393-
transform { listOf(it.last(condition)) }.single()
399+
transform { listOf(it.last(condition)) }.singleImpl()
394400

395401
/**
396402
* ## Last
@@ -404,6 +410,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
404410
*
405411
* @param [condition] The optional [ColumnFilter][org.jetbrains.kotlinx.dataframe.ColumnFilter] condition that the column must adhere to.
406412
* @return A [SingleColumn][org.jetbrains.kotlinx.dataframe.columns.SingleColumn] containing the last column that adheres to the given [condition].
413+
* @throws [NoSuchElementException] if no column adheres to the given [condition].
407414
* @see [first]
408415
*/
409416
public fun last(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
@@ -421,6 +428,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
421428
*
422429
* @param [condition] The optional [ColumnFilter][org.jetbrains.kotlinx.dataframe.ColumnFilter] condition that the column must adhere to.
423430
* @return A [SingleColumn][org.jetbrains.kotlinx.dataframe.columns.SingleColumn] containing the last column that adheres to the given [condition].
431+
* @throws [NoSuchElementException] if no column adheres to the given [condition].
424432
* @see [first]
425433
*/
426434
public fun ColumnGroupReference.last(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
@@ -436,6 +444,7 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
436444
*
437445
* @param [condition] The optional [ColumnFilter][org.jetbrains.kotlinx.dataframe.ColumnFilter] condition that the column must adhere to.
438446
* @return A [SingleColumn][org.jetbrains.kotlinx.dataframe.columns.SingleColumn] containing the last column that adheres to the given [condition].
447+
* @throws [NoSuchElementException] if no column adheres to the given [condition].
439448
* @see [first]
440449
*/
441450
public fun String.last(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
@@ -451,15 +460,119 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
451460
*
452461
* @param [condition] The optional [ColumnFilter][org.jetbrains.kotlinx.dataframe.ColumnFilter] condition that the column must adhere to.
453462
* @return A [SingleColumn][org.jetbrains.kotlinx.dataframe.columns.SingleColumn] containing the last column that adheres to the given [condition].
463+
* @throws [NoSuchElementException] if no column adheres to the given [condition].
454464
* @see [first]
455465
*/
456-
public fun KProperty<*>.last(condition: ColumnFilter<*>): SingleColumn<*> =
466+
public fun KProperty<*>.last(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
457467
getColumnGroup(this).last(condition)
458468

459-
public fun <C> ColumnSet<C>.single(condition: ColumnFilter<C>): SingleColumn<C> =
460-
transform { listOf(it.single(condition)) }.single()
461469

462-
public fun SingleColumn<AnyRow>.col(index: Int): SingleColumn<Any?> = getChildrenAt(index).single()
470+
/**
471+
* ## Single
472+
* Returns the single column in this [ColumnSet] or [ColumnGroup] that adheres to the given [condition\].
473+
*
474+
* For example:
475+
*
476+
* {@includeArg [Examples]}
477+
*
478+
* @param [condition\] The optional [ColumnFilter] condition that the column must adhere to.
479+
* @return A [SingleColumn] containing the single column that adheres to the given [condition\].
480+
* @throws [NoSuchElementException\] if no column adheres to the given [condition\].
481+
* @throws [IllegalArgumentException\] if more than one column adheres to the given [condition\].
482+
*/
483+
private interface CommonSingleDocs {
484+
485+
/** Examples key */
486+
interface Examples
487+
}
488+
489+
/**
490+
* ## Single
491+
* Returns the single column in this [ColumnSet][org.jetbrains.kotlinx.dataframe.columns.ColumnSet] or [ColumnGroup][org.jetbrains.kotlinx.dataframe.columns.ColumnGroup] that adheres to the given [condition].
492+
*
493+
* For example:
494+
*
495+
* `df.`[select][select]` { `[colsOf][colsOf]`<`[String][String]`>().`[single][single]` { it.`[name][ColumnReference.name]`().`[startsWith][String.startsWith]`("year") } }`
496+
*
497+
* `df.`[select][select]` { `[colsOf][colsOf]`<`[Int][Int]`>().`[single][single]`() }`
498+
*
499+
* @param [condition] The optional [ColumnFilter][org.jetbrains.kotlinx.dataframe.ColumnFilter] condition that the column must adhere to.
500+
* @return A [SingleColumn][org.jetbrains.kotlinx.dataframe.columns.SingleColumn] containing the single column that adheres to the given [condition].
501+
* @throws [NoSuchElementException] if no column adheres to the given [condition].
502+
* @throws [IllegalArgumentException] if more than one column adheres to the given [condition].
503+
*/
504+
public fun <C> ColumnSet<C>.single(condition: ColumnFilter<C> = { true }): SingleColumn<C> =
505+
transform { listOf(it.single(condition)) }.singleImpl()
506+
507+
/**
508+
* ## Single
509+
* Returns the single column in this [ColumnSet][org.jetbrains.kotlinx.dataframe.columns.ColumnSet] or [ColumnGroup][org.jetbrains.kotlinx.dataframe.columns.ColumnGroup] that adheres to the given [condition].
510+
*
511+
* For example:
512+
*
513+
* `df.`[select][select]` { `[single][single]` { it.`[name][ColumnReference.name]`().`[startsWith][String.startsWith]`("year") } }`
514+
*
515+
* `df.`[select][select]` { `[single][single]`() }`
516+
*
517+
* @param [condition] The optional [ColumnFilter][org.jetbrains.kotlinx.dataframe.ColumnFilter] condition that the column must adhere to.
518+
* @return A [SingleColumn][org.jetbrains.kotlinx.dataframe.columns.SingleColumn] containing the single column that adheres to the given [condition].
519+
* @throws [NoSuchElementException] if no column adheres to the given [condition].
520+
* @throws [IllegalArgumentException] if more than one column adheres to the given [condition].
521+
*/
522+
public fun single(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
523+
all().single(condition)
524+
525+
/**
526+
* ## Single
527+
* Returns the single column in this [ColumnSet][org.jetbrains.kotlinx.dataframe.columns.ColumnSet] or [ColumnGroup][org.jetbrains.kotlinx.dataframe.columns.ColumnGroup] that adheres to the given [condition].
528+
*
529+
* For example:
530+
*
531+
* `df.`[select][select]` { myColumnGroup.`[single][single]` { it.`[name][ColumnReference.name]`().`[startsWith][String.startsWith]`("year") } }`
532+
*
533+
* `df.`[select][select]` { myColumnGroup.`[single][single]`() }`
534+
*
535+
* @param [condition] The optional [ColumnFilter][org.jetbrains.kotlinx.dataframe.ColumnFilter] condition that the column must adhere to.
536+
* @return A [SingleColumn][org.jetbrains.kotlinx.dataframe.columns.SingleColumn] containing the single column that adheres to the given [condition].
537+
* @throws [NoSuchElementException] if no column adheres to the given [condition].
538+
* @throws [IllegalArgumentException] if more than one column adheres to the given [condition].
539+
*/
540+
public fun ColumnGroupReference.single(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
541+
all().single(condition)
542+
543+
/**
544+
* ## Single
545+
* Returns the single column in this [ColumnSet][org.jetbrains.kotlinx.dataframe.columns.ColumnSet] or [ColumnGroup][org.jetbrains.kotlinx.dataframe.columns.ColumnGroup] that adheres to the given [condition].
546+
*
547+
* For example:
548+
*
549+
* `df.`[select][select]` { "myColumnGroup".`[single][single]` { it.`[name][ColumnReference.name]`().`[startsWith][String.startsWith]`("year") } }`
550+
*
551+
* @param [condition] The optional [ColumnFilter][org.jetbrains.kotlinx.dataframe.ColumnFilter] condition that the column must adhere to.
552+
* @return A [SingleColumn][org.jetbrains.kotlinx.dataframe.columns.SingleColumn] containing the single column that adheres to the given [condition].
553+
* @throws [NoSuchElementException] if no column adheres to the given [condition].
554+
* @throws [IllegalArgumentException] if more than one column adheres to the given [condition].
555+
*/
556+
public fun String.single(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
557+
getColumnGroup(this).single(condition)
558+
559+
/**
560+
* ## Single
561+
* Returns the single column in this [ColumnSet][org.jetbrains.kotlinx.dataframe.columns.ColumnSet] or [ColumnGroup][org.jetbrains.kotlinx.dataframe.columns.ColumnGroup] that adheres to the given [condition].
562+
*
563+
* For example:
564+
*
565+
* `df.`[select][select]` { Type::myColumnGroup.`[single][single]` { it.`[name][ColumnReference.name]`().`[startsWith][String.startsWith]`("year") } }`
566+
*
567+
* @param [condition] The optional [ColumnFilter][org.jetbrains.kotlinx.dataframe.ColumnFilter] condition that the column must adhere to.
568+
* @return A [SingleColumn][org.jetbrains.kotlinx.dataframe.columns.SingleColumn] containing the single column that adheres to the given [condition].
569+
* @throws [NoSuchElementException] if no column adheres to the given [condition].
570+
* @throws [IllegalArgumentException] if more than one column adheres to the given [condition].
571+
*/
572+
public fun KProperty<*>.single(condition: ColumnFilter<*> = { true }): SingleColumn<*> =
573+
getColumnGroup(this).single(condition)
574+
575+
public fun SingleColumn<AnyRow>.col(index: Int): SingleColumn<Any?> = getChildrenAt(index).singleImpl()
463576

464577
public operator fun <C> ColumnSet<C>.get(index: Int): SingleColumn<C> = getAt(index)
465578

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/columns/Utils.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ internal fun <A, B> ColumnSet<A>.transform(converter: (List<ColumnWithPath<A>>)
104104
override fun resolve(context: ColumnResolutionContext) = converter(this@transform.resolve(context))
105105
}
106106

107-
internal fun <T> ColumnSet<T>.single() = object : SingleColumn<T> {
107+
internal fun <T> ColumnSet<T>.singleImpl() = object : SingleColumn<T> {
108108
override fun resolveSingle(context: ColumnResolutionContext): ColumnWithPath<T>? {
109-
return this@single.resolve(context).singleOrNull()
109+
return this@singleImpl.resolve(context).singleOrNull()
110110
}
111111
}
112112

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

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

33
import io.kotest.matchers.shouldBe
4+
import org.jetbrains.kotlinx.dataframe.impl.columns.singleImpl
45
import org.jetbrains.kotlinx.dataframe.samples.api.TestBase
56
import org.jetbrains.kotlinx.dataframe.samples.api.age
67
import org.jetbrains.kotlinx.dataframe.samples.api.firstName
78
import org.jetbrains.kotlinx.dataframe.samples.api.isHappy
8-
import org.jetbrains.kotlinx.dataframe.samples.api.lastName
99
import org.jetbrains.kotlinx.dataframe.samples.api.name
1010
import org.junit.Test
1111

@@ -65,4 +65,26 @@ class ColumnsSelectionDslTests : TestBase() {
6565
}
6666
}
6767

68+
@Test
69+
fun single() {
70+
val singleDf = df.select { take(1) }
71+
singleDf.select { all().single() } shouldBe singleDf.select { single() }
72+
73+
singleDf.select { all().single() } shouldBe singleDf.select { name }
74+
75+
singleDf.select { single() } shouldBe singleDf.select { name }
76+
77+
df.select { single { it.name().startsWith("a") } } shouldBe df.select { age }
78+
79+
df.select {
80+
name.single {
81+
it.any { it == "Alice" }
82+
}
83+
} shouldBe df.select {
84+
name.colsOf<String>().single {
85+
it.any { it == "Alice" }
86+
}
87+
}
88+
}
89+
6890
}

0 commit comments

Comments
 (0)