Kotlin / Kotlin/dataframe

Add check for "is primitive or mixed `Number` column" for statistics in compiler plugin

Open
#1,476 1 comment 0 reactions 0 assignees View on GitHub
Compiler plugin enhancement
Dominant language
Kotlin
Stars
1.1k
Forks
83
Avg merge
4d 12h
Merged PRs (30d)
30

Description

Relevant in `GroupBy` as well https://github.com/Kotlin/dataframe/issues/1090

All statistics functions accept primitive numbers and mixed primitive numbers. This is checked at runtime, but could also be checked at compile time.

For instance: `df.mean { intCol and bigNumberCol }` is fine to write; the inferred type will be `Number`, however, this will fail at runtime, because big numbers are not primitives and thus not supported. If the compiler plugin could provide an error message for such a case, it would improve runtime safety of statistics functions :)

I think we can recognize this by checking if all columns adhere to this function:

```kt
/**
* Returns `true` only if [this] represents an optionally nullable primitive number,
* (like `Double?`, or `Int`), or a "mixed Number" type: `Number?` or `Number`.
*
* We don't check for "subtype of Number" to prevent `BigInteger` etc. to be included, but since columns with
* mixed primitives are allowed in statistics, we do include `Number?` and `Number`
*/
fun ConeKotlinType.isPrimitiveOrMixedNumber(session: FirSession, errorTypesEqualToAnything: Boolean = false): Boolean =
this.isPrimitiveNumberOrNullableType ||
this.equalTypes(
otherType = session.builtinTypes.numberType.coneType,
session = session,
errorTypesEqualToAnything = errorTypesEqualToAnything,
) ||
this.equalTypes(
otherType = session.builtinTypes.numberType.coneType.withNullability(true, session.typeContext),
session = session,
errorTypesEqualToAnything = errorTypesEqualToAnything,
)
```

Of course, you could also write `columnOf(1.0, BigDecimal(1.0))`. We cannot check for this, but since it doesn't occur that often, it's probably fine.

Contributor guide

Open the contributing guide

Research direction

Start in the compiler plugin code that handles statistics functions and the relevant GroupBy path. Trace how column types are currently validated at runtime, then assess where the proposed isPrimitiveOrMixedNumber check belongs. Done means unsupported mixed Number columns such as BigDecimal combinations produce a compile-time error while primitive and mixed primitive numbers remain accepted.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.