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

Convert in-memory Column.round to Java #7521

Merged
merged 41 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
ee11df1
wip
GregoryTravis Aug 1, 2023
134f399
Merge branch 'develop' into wip/gmt/7401-col-round
GregoryTravis Aug 7, 2023
0740b4d
some pass
GregoryTravis Aug 7, 2023
61ad15e
more tests pass
GregoryTravis Aug 7, 2023
8bc823a
throwing
GregoryTravis Aug 8, 2023
c7f3248
tests passing
GregoryTravis Aug 8, 2023
fed0636
cleanup
GregoryTravis Aug 8, 2023
09a6563
fix cos test
GregoryTravis Aug 8, 2023
66050d4
mixed col works
GregoryTravis Aug 8, 2023
f17b547
bench ints too
GregoryTravis Aug 8, 2023
10bb7d1
removed expected_result_type=target_value_type
GregoryTravis Aug 8, 2023
04ba266
instanceof syntax
GregoryTravis Aug 8, 2023
99d7607
ungeneralize
GregoryTravis Aug 8, 2023
f502c99
specialize to returning long, no conversion
GregoryTravis Aug 8, 2023
460fd7b
docs
GregoryTravis Aug 8, 2023
bb65f40
all bench
GregoryTravis Aug 8, 2023
fd8578b
bench different params
GregoryTravis Aug 8, 2023
ca1f0cf
removed old impl
GregoryTravis Aug 8, 2023
0d5b378
java fmt
GregoryTravis Aug 8, 2023
036c485
bench params
GregoryTravis Aug 8, 2023
63f85fe
Merge branch 'develop' into wip/gmt/7400-col-round
GregoryTravis Aug 9, 2023
d3a2bbc
review
GregoryTravis Aug 9, 2023
b56359e
fix typo
GregoryTravis Aug 10, 2023
c27e6cb
return type test
GregoryTravis Aug 11, 2023
54052c7
more rounding tests
GregoryTravis Aug 11, 2023
63f2982
use DoubleBuilder
GregoryTravis Aug 11, 2023
dce3080
move fast return into java
GregoryTravis Aug 11, 2023
4ae155d
cleanup
GregoryTravis Aug 11, 2023
c6be7fe
value_type tests
GregoryTravis Aug 11, 2023
5c7980e
1M
GregoryTravis Aug 11, 2023
81fd1f8
Merge branch 'develop' into wip/gmt/7400-col-round
GregoryTravis Aug 14, 2023
c39c83f
merge
GregoryTravis Aug 14, 2023
e78ee79
Merge branch 'develop' into wip/gmt/7400-col-round
GregoryTravis Aug 15, 2023
d413b11
unnecessary null checks
GregoryTravis Aug 15, 2023
641e903
standalone RS
GregoryTravis Aug 15, 2023
b895d77
runnable from COS, after moving shared
GregoryTravis Aug 15, 2023
c328a26
from Number as well
GregoryTravis Aug 15, 2023
0b05c8a
remove old
GregoryTravis Aug 15, 2023
e05c0ac
type_error tests
GregoryTravis Aug 15, 2023
d59f273
unused
GregoryTravis Aug 15, 2023
460eeb4
cleanup
GregoryTravis Aug 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import project.Function.Function
import project.Internal.Rounding_Helpers
import project.Nothing.Nothing
import project.Panic.Panic
import project.IO
Copy link
Member

Choose a reason for hiding this comment

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

Is this import used?


from project.Data.Boolean import Boolean, False, True
from project.Errors.Common import Unsupported_Argument_Types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ type Column

example_round = Examples.decimal_column.round 2
round : Integer -> Boolean -> Column | Illegal_Argument | Invalid_Value_Type
round self decimal_places=0 use_bankers=False =
round self (decimal_places:Integer = 0) (use_bankers:Boolean = False) =
Rounding_Helpers.check_decimal_places decimal_places <| Value_Type.expect_numeric self <|
if self.should_use_builtin_round decimal_places use_bankers then self.round_builtin decimal_places else
self.short_circuit_special_floating_point <|
Expand All @@ -650,7 +650,7 @@ type Column
not_bankers = use_bankers.not
# Don't use for negative decimal places, if the backend doesn't support it
negative_dp_ok = decimal_places >= 0 || self.connection.dialect.supports_negative_round_decimal_places
# Don't use for floating-point inputs if deciaml_places != 0, for Postgres
# Don't use for floating-point inputs if decimal_places != 0, for Postgres
dp_param_ok = (self.connection.dialect.rounding_decimal_places_not_allowed_for_floats && self.value_type.is_floating_point && decimal_places != 0).not
not_bankers && negative_dp_ok && dp_param_ok

Expand Down
65 changes: 32 additions & 33 deletions distribution/lib/Standard/Table/0.0.0-dev/src/Data/Column.enso
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from Standard.Base import all
import Standard.Base.Data.Array_Proxy.Array_Proxy
import Standard.Base.Data.Index_Sub_Range as Index_Sub_Range_Module
import Standard.Base.Errors.Common.Arithmetic_Error
import Standard.Base.Errors.Common.Incomparable_Values
import Standard.Base.Errors.Common.Index_Out_Of_Bounds
import Standard.Base.Errors.Common.No_Such_Method
Expand Down Expand Up @@ -766,40 +767,10 @@ type Column

Column.from_vector "foo" [1.2, 2.3, 3.6] . round == (Column.from_vector "foo" [1, 2, 4])
round : Integer -> Boolean -> Column | Illegal_Argument | Invalid_Value_Type
round self decimal_places=0 use_bankers=False =
Rounding_Helpers.check_decimal_places decimal_places <| Value_Type.expect_numeric self <|
# If it's an integer column and decimal_places >=0 then it's a no-op.
round self (decimal_places:Integer = 0) (use_bankers:Boolean = False) = Value_Type.expect_numeric self <|
Illegal_Argument.handle_java_exception <| Rounding_Helpers.check_decimal_places decimal_places <|
new_name = naming_helper.function_name "round" [self]
is_integer = self.inferred_precise_value_type.is_integer
if is_integer && decimal_places >= 0 then self.rename new_name else
should_cast_to_integer = decimal_places <= 0 || is_integer
target_value_type = if should_cast_to_integer then Value_Type.Integer else self.value_type
result = self.check_round_input col->
scale = 10 ^ decimal_places
scaled = col * scale
round_base = scaled.floor
round_midpoint = (round_base + 0.5) / scale
even_is_up = (col >= 0).iif ((scaled.truncate % 2) != 0) ((scaled.truncate % 2) == 0)
half_goes_up = if use_bankers then even_is_up else col >= 0
do_round_up = half_goes_up.iif (col >= round_midpoint) (col > round_midpoint)
result_float = do_round_up.iif ((round_base + 1.0) / scale) (round_base / scale)
cast_if_needed result_float target_value_type
result.rename new_name

## PRIVATE
Restrict allowed range of input to rounding methods.
check_round_input : (Column -> Column) -> Any ! Illegal_Argument
check_round_input self action =
min = Rounding_Helpers.round_min_long
max = Rounding_Helpers.round_max_long
within_range = if self.value_type.is_floating_point.not then self.between min max else
self.is_nan || self.is_infinite || self.between min max

any_issues = within_range.to_vector . any (== False)
if any_issues.not then action self else
warning = Illegal_Argument.Error <| "Error: `round` can only accept values between " + min.to_text + " and " + max.to_text + " (inclusive)"
result = action (within_range.iif self Nothing)
Warning.attach warning result
run_vectorized_ternary_op self Java_Storage.Maps.ROUND decimal_places use_bankers new_name=new_name

## ALIAS int

Expand Down Expand Up @@ -2114,6 +2085,34 @@ run_vectorized_binary_op column name operand new_name=Nothing fallback_fn=Nothin
Problem_Behavior.Report_Warning.attach_problems_after result <|
Java_Problems.parse_aggregated_problems problem_builder.getProblems

## PRIVATE

Executes a vectorized ternary operation over the provided column.

Arguments:
- column: The column to execute the operation over.
- name: The name of the vectorized operation.
- operand0: The first operand to apply to the function after `column`.
- operand1: The second operand to apply to the function after `column`.
- new_name: The name of the column created as the result of this operation.
- expected_result_type: The expected result type of the operation.
- skip_nulls: Specifies if nulls should be skipped. If set to `True`, a null
value results in null without passing it to the function. If set to
`False`, the null values are passed as any other value and can have custom
handling logic.
run_vectorized_ternary_op : Column -> Text -> Any -> Any -> Text|Nothing -> Value_Type -> Boolean -> Column
run_vectorized_ternary_op column name operand0 operand1 new_name=Nothing expected_result_type=Nothing skip_nulls=True =
effective_new_name = new_name.if_nothing <|
naming_helper.function_name name [column, operand0, operand1]
problem_builder = MapOperationProblemBuilder.new column.name
storage_type = resolve_storage_type expected_result_type
s1 = column.java_column.getStorage
rs = Polyglot_Helpers.handle_polyglot_dataflow_errors <|
s1.vectorizedTernaryMap name problem_builder operand0 operand1 skip_nulls storage_type
result = Column.Value (Java_Column.new effective_new_name rs)
Problem_Behavior.Report_Warning.attach_problems_after result <|
Java_Problems.parse_aggregated_problems problem_builder.getProblems

## PRIVATE
Runs a binary operation over the provided column and operand which may be
another column or a scalar value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ from project.Errors import Additional_Invalid_Rows, Additional_Warnings, Duplica
polyglot java import org.enso.table.data.column.builder.LossOfIntegerPrecision
polyglot java import org.enso.table.data.table.problems.ArithmeticError
polyglot java import org.enso.table.data.table.problems.FloatingPointGrouping
polyglot java import org.enso.table.data.table.problems.IllegalArgumentError
polyglot java import org.enso.table.data.table.problems.InvalidAggregation
polyglot java import org.enso.table.data.table.problems.UnquotedCharactersInOutput
polyglot java import org.enso.table.data.table.problems.UnquotedDelimiter
Expand All @@ -25,6 +26,8 @@ translate_problem p = case p of
Error.throw err
_ : ArithmeticError ->
Arithmetic_Error.Error p.getMessage
_ : IllegalArgumentError ->
Illegal_Argument.Error p.getMessage
_ : FloatingPointGrouping ->
Floating_Point_Equality.Error p.getLocationName
_ : LossOfIntegerPrecision ->
Expand Down
Loading