Skip to content

GroupBy aggregate fix #803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.jetbrains.kotlinx.dataframe.columns.ValueColumn
import org.jetbrains.kotlinx.dataframe.columns.shortPath
import org.jetbrains.kotlinx.dataframe.impl.aggregation.receivers.AggregateInternalDsl
import org.jetbrains.kotlinx.dataframe.impl.api.AggregatedPivot
import org.jetbrains.kotlinx.dataframe.impl.createStarProjectedType
import org.jetbrains.kotlinx.dataframe.impl.createTypeWithArgument
import org.jetbrains.kotlinx.dataframe.impl.getListType
import kotlin.reflect.KType
Expand Down Expand Up @@ -118,7 +119,19 @@ internal class GroupByReceiverImpl<T>(override val df: DataFrame<T>, override va
pivot.aggregator.values.clear()
}

is AggregateInternalDsl<*> -> yield(value.copy(value = value.value.df))
is AggregateInternalDsl<*> -> {
// Attempt to create DataFrame<Type> from AggregateInternalDsl<Type>
val dfType = value.type?.arguments?.firstOrNull()?.type
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little bit worried about this expression with 6 ? - what if something goes wrong and will be null, I could not explain myself the logic, should we handle the alternative (or common alternative)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what the ?: is for at the end. We try to create DataFrame<Type1> from AggregateInternalDsl<Type1>. However, in theory, any KType can be supplied by the user. KTypes like GroupByReceiverImpl<Type1> , DataFrame<Type1> (which would both still work), but unfortunately also KTypes like CompletelyCustomType without arguments. So in cases where this type argument cannot be fetched, we default to DataFrame<*> with the ?:.

?.let { DataFrame::class.createTypeWithArgument(it) }
?: DataFrame::class.createStarProjectedType(nullable = false)

yield(
value.copy(
value = value.value.df,
type = dfType,
),
)
}

else -> values.add(value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ internal fun <T, C> DataFrame<T>.implodeImpl(dropNA: Boolean = false, columns: C
first = false
value
} else {
null
// these rows will not be taken into account,
// but we cannot leave them empty, as `map` creates a full column
when (column.kind()) {
ColumnKind.Value -> emptyList<Any?>()
ColumnKind.Group -> DataFrame.empty()
ColumnKind.Frame -> emptyList<AnyFrame>()
}
}
}
}[0..0]
}[0..0] // takes only the first row
}.concat()
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.jetbrains.kotlinx.dataframe.DataColumn
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
import org.jetbrains.kotlinx.dataframe.impl.isArray
import org.jetbrains.kotlinx.dataframe.impl.isPrimitiveArray
import org.jetbrains.kotlinx.dataframe.kind
import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.reflect.full.isSubclassOf
Expand Down Expand Up @@ -37,7 +38,7 @@ internal abstract class DataColumnImpl<T>(
if (BuildConfig.DEBUG) {
require(values.all { it matches type }) {
val types = values.map { if (it == null) "Nothing?" else it!!::class.simpleName }.distinct()
"Values of column '$name' have types '$types' which are not compatible given with column type '$type'"
"Values of $kind '$name' have types '$types' which are not compatible given with column type '$type'"
}
}
}
Expand Down
Loading