Skip to content
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

groupBy {}.aggregate { keys } #662

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

Conversation

Jolanrensen
Copy link
Collaborator

Provides access to the keys of the groupBy {} operation in the aggregate {} step. Very useful when you're grouping by an expr {} column and want to use the value of a key column to influence how the aggregation happens.
For instance:

df.groupBy { expr { … } named "keyName" }
  .aggregate {
      keys["keyName"] into "valueName"
   }

keys will be provided as an AnyRow, since most it will just contain the key columns from df, a tiny subset. Giving it the same type as df would result in many breaking accessors on keys.

…o provide access to the keys: AnyRow used to group the df by.
@Jolanrensen Jolanrensen added the enhancement New feature or request label Apr 15, 2024
internal class GroupByReceiverImpl<T>(
override val df: DataFrame<T>,
override val hasGroupingKeys: Boolean,
private val retrieveKey: () -> AnyRow = {
Copy link
Collaborator

@koperagen koperagen Apr 16, 2024

Choose a reason for hiding this comment

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

Hm, it has to be lambda here? Can be a DataRow, not sure. And what about default parameter: can it somehow actually throw an exception?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The lambda could be replaced by by a nullable AnyRow perhaps.
And yes, it will throw an exception when you use dataFrame.aggregate { keys }, since for some reason, the same AggregateGroupedDsl is used there. There's also an option to get here via pivot, so for those cases I make it throw a helpful exception.

Copy link
Collaborator

@koperagen koperagen Apr 16, 2024

Choose a reason for hiding this comment

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

Hmm, i'd say we need to make type of lambda in Groupby.aggregate more specific then? So only for that case we provide keys as a DSL property. Also, if it makes sense, we can make aggregate an extension function and hide existing member one (but this could be a different story)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh yes, i remember now. aggregate probably should be an extension function on GroupBy

public interface GroupBy<out T, out G> : Grouped<G> {

    public val groups: FrameColumn<G>

    public val keys: DataFrame<T>

then keys can become DataRow<T>

Because now aggregate on GroupBy is resolved to this member and it simply doesn't know anything about keys

public interface Grouped<out T> : Aggregatable<T> {

    public fun <R> aggregate(body: AggregateGroupedBody<T, R>): DataFrame<T>
}

@Jolanrensen Jolanrensen mentioned this pull request Apr 23, 2024
@Jolanrensen Jolanrensen marked this pull request as draft May 16, 2024 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants