From b9b0487d15c39291e768434e4643e1955bf88783 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 3 Dec 2024 14:04:34 +0000 Subject: [PATCH] Automated commit of generated code --- .../ColumnTypeMismatchesColumnValuesException.kt | 16 ++++++++++++++++ .../kotlinx/dataframe/impl/api/convert.kt | 12 +++++++++++- .../jetbrains/kotlinx/dataframe/api/convert.kt | 13 +++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/exceptions/ColumnTypeMismatchesColumnValuesException.kt diff --git a/core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/exceptions/ColumnTypeMismatchesColumnValuesException.kt b/core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/exceptions/ColumnTypeMismatchesColumnValuesException.kt new file mode 100644 index 0000000000..b985aba692 --- /dev/null +++ b/core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/exceptions/ColumnTypeMismatchesColumnValuesException.kt @@ -0,0 +1,16 @@ +package org.jetbrains.kotlinx.dataframe.exceptions + +import org.jetbrains.kotlinx.dataframe.AnyCol +import org.jetbrains.kotlinx.dataframe.DataColumn +import org.jetbrains.kotlinx.dataframe.columns.values +import org.jetbrains.kotlinx.dataframe.type + +/** + * Extension properties are generated according to [DataColumn.type] property + * [DataColumn.type] must match types of [DataColumn.values], but it can fail to do so. + * This causes [ClassCastException] or [NullPointerException] when you use extension property and actual value is of different type or is null. + * If generated extension property causes this exception, this is a bug in the library + * You can work around this problem by referring to [column] using String API + */ +public class ColumnTypeMismatchesColumnValuesException(public val column: AnyCol, cause: Throwable) : + RuntimeException("Failed to convert column '${column.name()}'", cause) diff --git a/core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/convert.kt b/core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/convert.kt index 0d7fb7e55e..f7cdacd630 100644 --- a/core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/convert.kt +++ b/core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/convert.kt @@ -33,6 +33,7 @@ import org.jetbrains.kotlinx.dataframe.columns.values import org.jetbrains.kotlinx.dataframe.dataTypes.IFRAME import org.jetbrains.kotlinx.dataframe.dataTypes.IMG import org.jetbrains.kotlinx.dataframe.exceptions.CellConversionException +import org.jetbrains.kotlinx.dataframe.exceptions.ColumnTypeMismatchesColumnValuesException import org.jetbrains.kotlinx.dataframe.exceptions.TypeConversionException import org.jetbrains.kotlinx.dataframe.exceptions.TypeConverterNotFoundException import org.jetbrains.kotlinx.dataframe.impl.columns.DataColumnInternal @@ -68,7 +69,16 @@ internal fun Convert.withRowCellImpl( type: KType, infer: Infer, rowConverter: RowValueExpression, -): DataFrame = to { col -> df.newColumn(type, col.name, infer) { rowConverter(it, it[col]) } } +): DataFrame = + to { col -> + try { + df.newColumn(type, col.name, infer) { rowConverter(it, it[col]) } + } catch (e: ClassCastException) { + throw ColumnTypeMismatchesColumnValuesException(col, e) + } catch (e: NullPointerException) { + throw ColumnTypeMismatchesColumnValuesException(col, e) + } + } @PublishedApi internal fun Convert.convertRowColumnImpl( diff --git a/core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/convert.kt b/core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/convert.kt index 188d13a17f..47c49736db 100644 --- a/core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/convert.kt +++ b/core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/convert.kt @@ -6,10 +6,12 @@ import io.kotest.matchers.shouldBe import kotlinx.datetime.Clock import kotlinx.datetime.Instant import kotlinx.datetime.LocalTime +import org.jetbrains.kotlinx.dataframe.ColumnsContainer import org.jetbrains.kotlinx.dataframe.DataColumn import org.jetbrains.kotlinx.dataframe.DataFrame import org.jetbrains.kotlinx.dataframe.annotations.DataSchema import org.jetbrains.kotlinx.dataframe.exceptions.CellConversionException +import org.jetbrains.kotlinx.dataframe.exceptions.ColumnTypeMismatchesColumnValuesException import org.jetbrains.kotlinx.dataframe.exceptions.TypeConversionException import org.jetbrains.kotlinx.dataframe.exceptions.TypeConverterNotFoundException import org.jetbrains.kotlinx.dataframe.hasNulls @@ -315,4 +317,15 @@ class ConvertTests { } } } + + private interface Marker + + private val ColumnsContainer.a get() = this["a"] as DataColumn + + @Test + fun `convert with buggy extension property`() { + shouldThrow { + dataFrameOf("a")(1, 2, 3).cast().convert { a }.with { it } + } + } }