Skip to content

Commit 7f3c54e

Browse files
committed
Add DataRow.getValueOrNull(columnRef) and ColumnReference.getValueOrNull(row)
1 parent 2a90628 commit 7f3c54e

File tree

11 files changed

+57
-5
lines changed

11 files changed

+57
-5
lines changed

src/main/kotlin/org/jetbrains/kotlinx/dataframe/DataRow.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public interface DataRow<out T> {
3333
public operator fun get(name: String): Any?
3434
public fun getColumnGroup(columnName: String): AnyRow = get(columnName) as AnyRow
3535
public fun getOrNull(name: String): Any?
36+
public fun <R> getValueOrNull(column: ColumnReference<R>): R?
3637
public fun containsKey(name: String): Boolean
3738

3839
// endregion

src/main/kotlin/org/jetbrains/kotlinx/dataframe/columns/ColumnPath.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public data class ColumnPath(val path: List<String>) : List<String> by path, Col
4343

4444
override fun getValue(row: AnyRow): Any? = row.owner[this][row.index()]
4545

46+
override fun getValueOrNull(row: AnyRow): Any? = row.owner.getColumnOrNull(this)?.get(row.index())
47+
4648
override fun toString(): String = path.toString()
4749

4850
override fun <C> get(column: ColumnReference<C>): ColumnAccessor<C> = ColumnAccessorImpl(this + column.path())

src/main/kotlin/org/jetbrains/kotlinx/dataframe/columns/ColumnReference.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public interface ColumnReference<out C> : SingleColumn<C> {
2929

3030
public fun path(): ColumnPath = ColumnPath(name)
3131

32-
public fun getValue(row: AnyRow): C = resolveFor(row)
32+
public fun getValue(row: AnyRow): C = resolveFor(row.df())!![row.index()]
33+
34+
public fun getValueOrNull(row: AnyRow): C? = resolveFor(row.df())?.get(row.index())
3335

3436
override fun resolveSingle(context: ColumnResolutionContext): ColumnWithPath<C>? {
3537
return context.df.getColumn<C>(path(), context.unresolvedColumnsPolicy)?.addPath(path(), context.df)
@@ -40,6 +42,4 @@ internal fun <C> ColumnReference<C>.renamedReference(newName: String): ColumnRef
4042

4143
internal fun ColumnReference<*>.shortPath() = ColumnPath(name)
4244

43-
internal fun <C> ColumnReference<C>.resolveFor(df: AnyFrame): ColumnWithPath<C> = resolveSingle(ColumnResolutionContext(df, UnresolvedColumnsPolicy.Fail))!!
44-
45-
internal fun <C> ColumnReference<C>.resolveFor(row: AnyRow): C = resolveFor(row.df())[row.index()]
45+
internal fun <C> ColumnReference<C>.resolveFor(df: AnyFrame): ColumnWithPath<C>? = resolveSingle(ColumnResolutionContext(df, UnresolvedColumnsPolicy.Skip))

src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/DataRowImpl.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ internal open class DataRowImpl<T>(private val index: Int, private val df: DataF
2424
return column.getValue(this)
2525
}
2626

27+
override fun <R> getValueOrNull(column: ColumnReference<R>): R? {
28+
ColumnAccessTracker.registerColumnAccess(column.name())
29+
return column.getValueOrNull(this)
30+
}
31+
2732
override fun index() = index
2833

2934
private val values by lazy { df.columns().map { it[index] } }

src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/columns/ColumnAccessorImpl.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ internal class ColumnAccessorImpl<T>(val path: ColumnPath) : ColumnAccessor<T> {
3636

3737
override fun <C> get(column: ColumnReference<C>) = ColumnAccessorImpl<C>(path + column.path())
3838

39-
override fun getValue(row: AnyRow) = row[path] as T
39+
override fun getValue(row: AnyRow) = path.getValue(row) as T
40+
41+
override fun getValueOrNull(row: AnyRow) = path.getValueOrNull(row) as T
4042
}

src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/columns/ColumnGroupImpl.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,7 @@ internal class ResolvingColumnGroup<T>(
9494

9595
override fun getValue(row: AnyRow) = super<DataColumnGroup>.getValue(row)
9696

97+
override fun getValueOrNull(row: AnyRow) = super<DataColumnGroup>.getValueOrNull(row)
98+
9799
override fun rename(newName: String) = ResolvingColumnGroup(source.rename(newName))
98100
}

src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/columns/ComputedColumnReference.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ internal class ComputedColumnReference<R>(
2525
override fun rename(newName: String) = ComputedColumnReference(newName, type, infer, compute)
2626

2727
override fun getValue(row: AnyRow) = compute(row, row)
28+
29+
override fun getValueOrNull(row: AnyRow) = compute(row, row)
2830
}
2931

3032
@PublishedApi

src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/columns/FrameColumnImpl.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,7 @@ internal class ResolvingFrameColumn<T>(
6060

6161
override fun getValue(row: AnyRow) = super<FrameColumn>.getValue(row)
6262

63+
override fun getValueOrNull(row: AnyRow) = super<FrameColumn>.getValueOrNull(row)
64+
6365
override fun rename(newName: String) = ResolvingFrameColumn(source.rename(newName))
6466
}

src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/columns/RenamedColumnReference.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ internal class RenamedColumnReference<C>(val source: ColumnReference<C>, val nam
1616
override fun rename(newName: String) = RenamedColumnReference(source, newName)
1717

1818
override fun getValue(row: AnyRow) = source.getValue(row)
19+
20+
override fun getValueOrNull(row: AnyRow) = source.getValueOrNull(row)
1921
}

src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/columns/ValueColumnImpl.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,7 @@ internal class ResolvingValueColumn<T>(
5959

6060
override fun getValue(row: AnyRow) = super<ValueColumn>.getValue(row)
6161

62+
override fun getValueOrNull(row: AnyRow) = super<ValueColumn>.getValueOrNull(row)
63+
6264
override fun rename(newName: String) = ResolvingValueColumn(source.rename(newName))
6365
}

0 commit comments

Comments
 (0)