-
Notifications
You must be signed in to change notification settings - Fork 324
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
BigDecimalBuilder
and arithmetic operations.
#9950
Changes from 25 commits
634d12a
14b711b
291a60a
dfc854a
b1c567f
004114a
347e66e
4973d5e
deb15e4
aeb4114
a66676a
2b9a1e3
3514d31
4c0b81e
a4fd38c
f940ccd
6850751
f846903
230b8bb
a2a6019
2ff91a5
0493528
bb27fa4
fef5e4a
bdda67f
c85bb94
137f975
3f3fde8
f58ad25
5b1ef35
cc9230c
3cce4cb
8d60684
6c84519
1ad53b8
30a8f92
ca1998a
fa45de2
93f8f4b
67d2769
2a87ccb
4a8a761
c2b1521
6f45e3e
1935981
45f143f
ef4ff68
517c36c
d80d32f
345422e
e680e30
bad8fa1
c0467d9
0f521bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,11 @@ import project.Value_Type.Bits | |
import project.Value_Type.Value_Type | ||
from project.Errors import Inexact_Type_Coercion | ||
|
||
polyglot java import java.math.BigDecimal | ||
|
||
polyglot java import org.enso.table.data.column.builder.Builder as Java_Builder | ||
polyglot java import org.enso.table.data.column.storage.type.AnyObjectType | ||
polyglot java import org.enso.table.data.column.storage.type.BigDecimalType | ||
polyglot java import org.enso.table.data.column.storage.type.BigIntegerType | ||
polyglot java import org.enso.table.data.column.storage.type.Bits as Java_Bits | ||
polyglot java import org.enso.table.data.column.storage.type.BooleanType | ||
|
@@ -40,6 +43,7 @@ to_value_type storage_type = case storage_type of | |
_ : DateType -> Value_Type.Date | ||
_ : DateTimeType -> Value_Type.Date_Time with_timezone=True | ||
_ : TimeOfDayType -> Value_Type.Time | ||
_ : BigDecimalType -> Value_Type.Decimal | ||
_ : BigIntegerType -> Value_Type.Decimal scale=0 | ||
_ : AnyObjectType -> Value_Type.Mixed | ||
|
||
|
@@ -64,11 +68,18 @@ closest_storage_type value_type = case value_type of | |
Value_Type.Mixed -> AnyObjectType.INSTANCE | ||
Value_Type.Decimal _ scale -> | ||
is_integer = scale.is_nothing || scale <= 0 | ||
if is_integer then BigIntegerType.INSTANCE else | ||
Error.throw (Illegal_Argument.Error "Columns of type "+value_type.to_display_text+" are currently not supported in the in-memory backend - only Decimal of integer type (scale <= 0) is supported. You may cast the column to Float first (lossy conversion).") | ||
if is_integer then BigIntegerType.INSTANCE else BigDecimalType.INSTANCE | ||
_ -> | ||
Error.throw (Illegal_Argument.Error "Columns of type "+value_type.to_display_text+" are currently not supported in the in-memory backend.") | ||
|
||
enso_to_java x = case x of | ||
Decimal.Value big_decimal -> big_decimal | ||
_ -> x | ||
|
||
java_to_enso x = case x of | ||
_ : BigDecimal -> Decimal.Value x | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You can call the factory in Java, if you want. If you create a method: public static Object factory(Function<BigDecimal,Object> f) {
return f.apply(BigDecimal.valueOf(1));
} and call it from Enso as IO.println <| factory Decimal.Value then you should get |
||
_ -> x | ||
|
||
## PRIVATE | ||
Converts a value type to an in-memory storage type, possibly approximating it | ||
to the closest supported type. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.enso.table.data.column.builder; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import org.enso.table.data.column.storage.Storage; | ||
import org.enso.table.data.column.storage.numeric.BigDecimalStorage; | ||
import org.enso.table.data.column.storage.type.BigDecimalType; | ||
import org.enso.table.data.column.storage.type.StorageType; | ||
import org.enso.table.error.ValueTypeMismatchException; | ||
|
||
/** A builder for BigDecimal columns. */ | ||
public class BigDecimalBuilder extends TypedBuilderImpl<BigDecimal> { | ||
@Override | ||
protected BigDecimal[] newArray(int size) { | ||
return new BigDecimal[size]; | ||
} | ||
|
||
public BigDecimalBuilder(int size) { | ||
super(size); | ||
} | ||
|
||
@Override | ||
public StorageType getType() { | ||
return BigDecimalType.INSTANCE; | ||
} | ||
|
||
@Override | ||
public void appendNoGrow(Object o) { | ||
try { | ||
data[currentSize++] = (BigDecimal) o; | ||
} catch (ClassCastException e) { | ||
throw new ValueTypeMismatchException(getType(), o); | ||
} | ||
} | ||
|
||
@Override | ||
public void append(Object o) { | ||
appendNoGrow(o); | ||
} | ||
|
||
@Override | ||
public boolean accepts(Object o) { | ||
return o instanceof BigDecimal; | ||
} | ||
|
||
@Override | ||
protected Storage<BigDecimal> doSeal() { | ||
return new BigDecimalStorage(data, currentSize); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.enso.table.data.column.operation.map.numeric.arithmetic; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import org.enso.table.data.column.operation.map.MapOperationProblemAggregator; | ||
import org.enso.table.data.column.storage.Storage; | ||
|
||
public class BigDecimalDivideOp<T extends Number, I extends Storage<? super T>> | ||
extends NumericBinaryOpReturningBigDecimal<T, I> { | ||
public BigDecimalDivideOp() { | ||
super(Storage.Maps.DIV); | ||
} | ||
|
||
@Override | ||
public BigDecimal doBigDecimal( | ||
BigDecimal a, BigDecimal b, int ix, MapOperationProblemAggregator problemAggregator) { | ||
return a.divide(b); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit worried about performance here. Have you checked
Column_from_vector_*
benchmarks?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added logic to check the
value_type
and only applyingenso_to_java
if it might be necessary. There's a significant slowdown in the benchmarks in thevalue_type=Auto
cases, since it callsenso_to_java
in those cases.Before:
After:
@JaroslavTulach do you have any suggestions for making
enso_to_java
faster? Perhaps implementing it in Java?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this comment of mine be considered a suggestion? Does it have any impact on the benchmarks? It might as the number of back and forth calls is going to be different...