Skip to content

Commit df4c997

Browse files
committed
small linting I did during debugging to make the code more understandable
1 parent c269e08 commit df4c997

File tree

7 files changed

+61
-16
lines changed

7 files changed

+61
-16
lines changed

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/GroupByImpl.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ internal fun <T, G, R> aggregateGroupBy(
8787
val result = body(builder, builder)
8888
if (result != Unit && result !is NamedValue && result !is AggregatedPivot<*>) builder.yield(
8989
NamedValue.create(
90-
pathOf(defaultAggregateName), result, null, null, true
90+
path = pathOf(defaultAggregateName),
91+
value = result,
92+
type = null,
93+
defaultValue = null,
94+
guessType = true,
9195
)
9296
)
9397
builder.compute()

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/concat.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ internal fun <T> concatImpl(name: String, columns: List<DataColumn<T>?>, columnS
7070
val baseType = types.commonType()
7171
val tartypeOf = if (guessType || !hasList) baseType.withNullability(nulls)
7272
else getListType(baseType.withNullability(listOfNullable))
73-
return guessColumnType(name, list, tartypeOf, guessType, defaultValue).cast()
73+
return guessColumnType(
74+
name = name,
75+
values = list,
76+
suggestedType = tartypeOf,
77+
suggestedTypeIsUpperBound = guessType,
78+
defaultValue = defaultValue,
79+
).cast()
7480
}
7581
}
7682

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/pivot.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,21 @@ internal fun <T, R> aggregatePivot(
100100
val hasResult = result != null && result != Unit
101101

102102
fun NamedValue.apply(path: ColumnPath) =
103-
copy(path = path, value = this.value ?: default ?: globalDefault, default = default ?: globalDefault)
103+
copy(
104+
path = path,
105+
value = this.value ?: default ?: globalDefault,
106+
default = default ?: globalDefault,
107+
)
104108

105109
val values = builder.values
106110
when {
107111
values.size == 1 && values[0].path.isEmpty() -> aggregator.yield(values[0].apply(path))
108112
values.isEmpty() -> aggregator.yield(
109-
path,
110-
if (hasResult) result else globalDefault,
111-
null,
112-
globalDefault,
113-
true
113+
path = path,
114+
value = if (hasResult) result else globalDefault,
115+
type = null,
116+
default = globalDefault,
117+
guessType = true,
114118
)
115119

116120
else -> {

core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/pivot.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jetbrains.kotlinx.dataframe.api
22

33
import io.kotest.matchers.shouldBe
4+
import org.jetbrains.kotlinx.dataframe.impl.commonType
45
import org.junit.Test
56
import kotlin.reflect.typeOf
67

@@ -168,4 +169,20 @@ class PivotTests {
168169
}
169170
df1 shouldBe df.pivot { "category2" then "category3" }.groupBy("category1").count()
170171
}
172+
173+
@Test
174+
fun `pivot with default of other type`() {
175+
val df = dataFrameOf("firstName", "lastName", "age", "city", "weight", "isHappy")(
176+
"Alice", "Cooper", 15, "London", 54, true,
177+
"Bob", "Dylan", 45, "Dubai", 87, true,
178+
"Charlie", "Daniels", 20, "Moscow", null, false,
179+
"Charlie", "Chaplin", 40, "Milan", null, true,
180+
"Bob", "Marley", 30, "Tokyo", 68, true,
181+
"Alice", "Wolf", 20, null, 55, false,
182+
"Charlie", "Byrd", 30, "Moscow", 90, true
183+
).group("firstName", "lastName").into("name")
184+
185+
val pivoted = df.pivot("city").groupBy("name").default(0).min()
186+
pivoted["city"]["London"]["isHappy"].type() shouldBe listOf(typeOf<Int>(), typeOf<Boolean>()).commonType()
187+
}
171188
}

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/GroupByImpl.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ internal fun <T, G, R> aggregateGroupBy(
8787
val result = body(builder, builder)
8888
if (result != Unit && result !is NamedValue && result !is AggregatedPivot<*>) builder.yield(
8989
NamedValue.create(
90-
pathOf(defaultAggregateName), result, null, null, true
90+
path = pathOf(defaultAggregateName),
91+
value = result,
92+
type = null,
93+
defaultValue = null,
94+
guessType = true,
9195
)
9296
)
9397
builder.compute()

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/concat.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ internal fun <T> concatImpl(name: String, columns: List<DataColumn<T>?>, columnS
7070
val baseType = types.commonType()
7171
val tartypeOf = if (guessType || !hasList) baseType.withNullability(nulls)
7272
else getListType(baseType.withNullability(listOfNullable))
73-
return guessColumnType(name, list, tartypeOf, guessType, defaultValue).cast()
73+
return guessColumnType(
74+
name = name,
75+
values = list,
76+
suggestedType = tartypeOf,
77+
suggestedTypeIsUpperBound = guessType,
78+
defaultValue = defaultValue,
79+
).cast()
7480
}
7581
}
7682

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/pivot.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,21 @@ internal fun <T, R> aggregatePivot(
100100
val hasResult = result != null && result != Unit
101101

102102
fun NamedValue.apply(path: ColumnPath) =
103-
copy(path = path, value = this.value ?: default ?: globalDefault, default = default ?: globalDefault)
103+
copy(
104+
path = path,
105+
value = this.value ?: default ?: globalDefault,
106+
default = default ?: globalDefault,
107+
)
104108

105109
val values = builder.values
106110
when {
107111
values.size == 1 && values[0].path.isEmpty() -> aggregator.yield(values[0].apply(path))
108112
values.isEmpty() -> aggregator.yield(
109-
path,
110-
if (hasResult) result else globalDefault,
111-
null,
112-
globalDefault,
113-
true
113+
path = path,
114+
value = if (hasResult) result else globalDefault,
115+
type = null,
116+
default = globalDefault,
117+
guessType = true,
114118
)
115119

116120
else -> {

0 commit comments

Comments
 (0)