Skip to content

Commit 126462e

Browse files
authored
Refactor errors and utils (chitralverma#137)
* propagate errors to JVM for series and row * propagate errors to JVM for frame * propagate errors to JVM for lazyframe * propagate errors to JVM for lazyframe * update dependencies * propagate errors to JVM for exprs * propagate errors to JVM for writers * Update Examples
1 parent b9b7423 commit 126462e

File tree

33 files changed

+1220
-1333
lines changed

33 files changed

+1220
-1333
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ coverage.xml
1313
target/
1414
.classpath
1515
.project
16-
.settings/
16+
.settings/
17+
.metals
18+
.bloop

core/src/main/scala/org/polars/scala/polars/api/DataFrame.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ object DataFrame {
222222
* [[org.polars.scala.polars.api.Series]]
223223
*/
224224
def fromSeries(series: Series, more: Iterable[Series]): DataFrame =
225-
fromSeries(series, more.toArray)
225+
DataFrame.withPtr(data_frame.fromSeries(more.toSeq.+:(series).map(_.ptr).toArray))
226226

227227
/** Initialize new [[org.polars.scala.polars.api.DataFrame]] from one or more
228228
* [[org.polars.scala.polars.api.Series]]. The name of a series is used as column name and its

core/src/main/scala/org/polars/scala/polars/functions.scala

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.polars.scala.polars
22

3-
import java.sql.{Date, Timestamp}
43
import java.time.format.DateTimeFormatter
4+
import java.time.{LocalDate, LocalTime, ZonedDateTime}
55

66
import org.polars.scala.polars.api.expressions.{Column, Expression}
77
import org.polars.scala.polars.internal.jni.expressions.{column_expr, literal_expr}
@@ -19,12 +19,14 @@ object functions {
1919
case v: Long => literal_expr.fromLong(v)
2020
case v: Float => literal_expr.fromFloat(v)
2121
case v: Double => literal_expr.fromDouble(v)
22-
case v: Date =>
23-
val dateStr = DateTimeFormatter.ISO_DATE.format(v.toLocalDate)
24-
literal_expr.fromDate(dateStr)
25-
case v: Timestamp =>
26-
val timestampStr = DateTimeFormatter.ISO_DATE_TIME.format(v.toLocalDateTime)
27-
literal_expr.fromTimestamp(timestampStr)
22+
case v: LocalDate =>
23+
literal_expr.fromDate(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE.format(v))
24+
case v: LocalTime =>
25+
literal_expr.fromTime(java.time.format.DateTimeFormatter.ISO_LOCAL_TIME.format(v))
26+
case v: ZonedDateTime =>
27+
literal_expr.fromDateTime(
28+
java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(v)
29+
)
2830
case v: String => literal_expr.fromString(v)
2931
case _ =>
3032
throw new IllegalArgumentException(

core/src/main/scala/org/polars/scala/polars/internal/jni/expressions/literal_expr.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ private[polars] object literal_expr extends Natively {
2020

2121
@native def fromDate(value: String): Long
2222

23-
@native def fromTimestamp(value: String): Long
23+
@native def fromTime(value: String): Long
24+
25+
@native def fromDateTime(value: String): Long
2426

2527
}

examples/src/main/java/examples/java/InstantiateDataFrame.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import org.polars.scala.polars.api.Series;
66

77
public class InstantiateDataFrame {
8-
public static void main(String[] args) {
98

9+
public static void main(String[] args) {
1010
DataFrame.fromSeries(Series.ofBoolean("bool_col", new boolean[] {true, false, true})).show();
1111

1212
DataFrame.fromSeries(
@@ -15,7 +15,11 @@ public static void main(String[] args) {
1515
Series.ofBoolean("bool_col", new boolean[] {true, false, true}),
1616
Series.ofList(
1717
"nested_str_col",
18-
new String[][] {{"a", "b", "c"}, {"a", "b", "c"}, {"a", "b", "c"}}))
18+
new String[][] {
19+
{"a", "b", "c"},
20+
{"a", "b", "c"},
21+
{"a", "b", "c"},
22+
}))
1923
.show();
2024

2125
/* Values as Java array(s) */
@@ -27,7 +31,11 @@ public static void main(String[] args) {
2731
Series.ofBoolean("bool_col", new boolean[] {true, false, true}),
2832
Series.ofList(
2933
"nested_str_col",
30-
new String[][] {{"a", "b", "c"}, {"a", "b", "c"}, {"a", "b", "c"}})
34+
new String[][] {
35+
{"a", "b", "c"},
36+
{"a", "b", "c"},
37+
{"a", "b", "c"},
38+
}),
3139
})
3240
.show();
3341

@@ -36,7 +44,7 @@ public static void main(String[] args) {
3644
new Series[] {
3745
Series.ofLong("i64_col", new Long[] {1L, 2L, 3L}),
3846
Series.ofBoolean("bool_col", new Boolean[] {true, false, true}),
39-
Series.ofFloat("f32_col", new Float[] {1F, 2F, 3F})
47+
Series.ofFloat("f32_col", new Float[] {1F, 2F, 3F}),
4048
})
4149
.show();
4250

@@ -47,7 +55,7 @@ public static void main(String[] args) {
4755
new Series[] {
4856
Series.ofLong("i64_col", Arrays.asList(1L, 2L, 3L)),
4957
Series.ofBoolean("bool_col", Arrays.asList(true, false, true)),
50-
Series.ofFloat("f32_col", Arrays.asList(1F, 2F, 3F))
58+
Series.ofFloat("f32_col", Arrays.asList(1F, 2F, 3F)),
5159
})
5260
.show();
5361

@@ -58,7 +66,22 @@ public static void main(String[] args) {
5866
new Series[] {
5967
Series.ofLong("i64_col", new Long[] {1L, 2L, 3L}),
6068
Series.ofBoolean("bool_col", new Boolean[] {true, false, true}),
61-
Series.ofFloat("f32_col", Arrays.asList(1F, 2F, 3F))
69+
Series.ofFloat("f32_col", Arrays.asList(1F, 2F, 3F)),
70+
})
71+
.show();
72+
73+
DataFrame.fromSeries(
74+
Series.ofInt("i32_col", Arrays.asList(1, 2, 3)),
75+
new Series[] {
76+
Series.ofLong("i64_col", new Long[] {1L, 2L, 3L}),
77+
Series.ofBoolean("bool_col", new Boolean[] {true, false, true}),
78+
Series.ofSeries(
79+
"struct_col",
80+
new Series[] {
81+
Series.ofLong("i64_col", new Long[] {1L, 2L, 3L}),
82+
Series.ofBoolean("bool_col", new Boolean[] {true, false, true}),
83+
Series.ofFloat("f32_col", Arrays.asList(1F, 2F, 3F)),
84+
}),
6285
})
6386
.show();
6487
}

examples/src/main/java/examples/java/expressions/ApplyingSimpleExpressions.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
import static org.polars.scala.polars.functions.*;
44

55
import examples.scala.utils.CommonUtils;
6-
import java.sql.Timestamp;
7-
import java.time.Instant;
86
import java.util.Collections;
97
import java.util.Random;
108
import org.polars.scala.polars.Polars;
119
import org.polars.scala.polars.api.DataFrame;
1210
import org.polars.scala.polars.api.LazyFrame;
1311

1412
public class ApplyingSimpleExpressions {
15-
public static void main(String[] args) {
1613

14+
public static void main(String[] args) {
1715
/* Read a dataset as a DataFrame lazily or eagerly */
1816
String path = CommonUtils.getResource("/files/web-ds/data.json");
1917
LazyFrame input = Polars.ndJson().scan(path);
@@ -26,13 +24,15 @@ public static void main(String[] args) {
2624
.with_column("lower_than_four", col("id").lessThanEqualTo(4))
2725
.filter(col("lower_than_four"))
2826
.with_column("long_value", lit(new Random().nextLong()))
29-
.with_column("current_ts", lit(Timestamp.from(Instant.now())))
27+
.with_column("date", lit(java.time.LocalDate.now()))
28+
.with_column("time", lit(java.time.LocalTime.now()))
29+
.with_column("current_ts", lit(java.time.ZonedDateTime.now()))
3030
.sort(asc("name"), true, false)
3131
.set_sorted(Collections.singletonMap("name", false))
3232
.top_k(2, "id", true, true, false)
3333
.limit(2) // .head(2)
3434
.tail(2)
35-
.drop("current_ts", "long_value")
35+
.drop("long_value")
3636
.rename("lower_than_four", "less_than_four")
3737
.drop_nulls();
3838

examples/src/main/scala/examples/scala/InstantiateDataFrame.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ object InstantiateDataFrame {
5959
)
6060
)
6161
.show()
62-
}
6362

63+
DataFrame
64+
.fromSeries(
65+
Series.ofInt("i32_col", Array[Int](1, 2, 3)),
66+
Series.ofLong("i64_col", Array[Long](1L, 2L, 3L)),
67+
Series.ofBoolean("bool_col", Array[Boolean](true, false, true)),
68+
Series.ofSeries(
69+
"struct_col",
70+
Array[Series](
71+
Series.ofLong("i64_col", Array[Long](1L, 2L, 3L)),
72+
Series.ofBoolean("bool_col", Array[Boolean](true, false, true)),
73+
Series.ofFloat("f32_col", Seq(1f, 2f, 3f))
74+
)
75+
)
76+
)
77+
.show()
78+
}
6479
}

examples/src/main/scala/examples/scala/expressions/ApplyingSimpleExpressions.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package examples.scala.expressions
22

3-
import java.sql.Timestamp
4-
import java.time.Instant
5-
63
import scala.util.Random
74

85
import org.polars.scala.polars.Polars
@@ -23,13 +20,15 @@ object ApplyingSimpleExpressions {
2320
.with_column("lower_than_four", col("id") <= 4)
2421
.filter(col("lower_than_four"))
2522
.with_column("long_value", lit(Random.nextLong()))
26-
.with_column("current_ts", lit(Timestamp.from(Instant.now())))
23+
.with_column("date", lit(java.time.LocalDate.now()))
24+
.with_column("time", lit(java.time.LocalTime.now()))
25+
.with_column("current_ts", lit(java.time.ZonedDateTime.now()))
2726
.sort(asc("name"), nullLast = true, maintainOrder = false)
2827
.set_sorted(Map("name" -> false))
2928
.top_k(2, "id", descending = true, nullLast = true, maintainOrder = false)
3029
.limit(2) // .head(2)
3130
.tail(2)
32-
.drop("current_ts", "long_value")
31+
.drop("long_value")
3332
.rename("lower_than_four", "less_than_four")
3433
.drop_nulls()
3534

0 commit comments

Comments
 (0)