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

Add update mode to Table.Running #11045

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -2933,6 +2933,8 @@ type DB_Table
- statistic: The running statistic to calculate.
- of: The existing column to run the statistic over.
- as: The name of the new column.
- set_mode: Specifies the expected behaviour in regards to existing
column with the same name.
- group_by: Specifies the columns to group by. The running statistic is
calculated separately for each group. By default, all rows are treated as
a single group.
Expand All @@ -2959,9 +2961,9 @@ type DB_Table
@group_by Widget_Helpers.make_column_name_multi_selector
@order_by Widget_Helpers.make_order_by_selector
@of Widget_Helpers.make_column_name_selector
running : Statistic -> (Text | Integer) -> Text -> Vector (Text | Integer | Regex) | Text | Integer | Regex -> Vector (Text | Sort_Column) | Text -> Problem_Behavior -> Table
running self (statistic:Statistic=..Count) (of:(Text | Integer)=0) (as:Text='') (group_by:(Vector | Text | Integer | Regex)=[]) (order_by:(Vector | Text)=[]) (on_problems:Problem_Behavior=..Report_Warning) =
_ = [statistic, of, as, group_by, order_by, on_problems]
running : Statistic -> (Text | Integer) -> Text -> Set_Mode -> Vector (Text | Integer | Regex) | Text | Integer | Regex -> Vector (Text | Sort_Column) | Text -> Problem_Behavior -> Table
running self (statistic:Statistic=..Count) (of:(Text | Integer)=0) (as:Text='') (set_mode:Set_Mode=..Add) (group_by:(Vector | Text | Integer | Regex)=[]) (order_by:(Vector | Text)=[]) (on_problems:Problem_Behavior=..Report_Warning) =
_ = [statistic, of, as, set_mode, group_by, order_by, on_problems]
Error.throw (Unsupported_Database_Operation.Error "DB_Table.running is currently not implemented for the Database backend. You may download the table to memory using `.read` to use this feature.")


Expand Down
AdRiley marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@ add_row_number table name from step group_by order_by on_problems:Problem_Behavi
Error.throw (Illegal_Argument.Error "The row number has exceeded the 64-bit integer range. BigInteger numbering is currently not supported. Please use a smaller start/step.")

problem_builder.attach_problems_before on_problems <| Panic.catch ArithmeticException handler=handle_arithmetic_exception <| Panic.catch Unsupported_Argument_Types handler=handle_arithmetic_exception <|
no_order_no_group = grouping_columns.is_empty && ordering.is_empty
new_column = case no_order_no_group of
True -> make_range_column name from step table.row_count
False ->
ordering_columns = ordering.map c->c.column.java_column
directions = ordering.map c->c.associated_selector.direction.to_sign
grouping_java_columns = grouping_columns.map c->c.java_column
new_storage = Java_Problems.with_problem_aggregator on_problems java_problem_aggregator->
AddRowNumber.create_numbering from step grouping_java_columns ordering_columns directions java_problem_aggregator
Column.from_storage name new_storage

new_column = create_column table name from step grouping_columns ordering on_problems
renamed_table = rename_columns_if_needed table name on_problems Table.new
renamed_table.set new_column name set_mode=Set_Mode.Add

## PRIVATE
create_column table name from step grouping_columns ordering on_problems =
no_order_no_group = grouping_columns.is_empty && ordering.is_empty
case no_order_no_group of
True -> make_range_column name from step table.row_count
False ->
ordering_columns = ordering.map c->c.column.java_column
directions = ordering.map c->c.associated_selector.direction.to_sign
grouping_java_columns = grouping_columns.map c->c.java_column
new_storage = Java_Problems.with_problem_aggregator on_problems java_problem_aggregator->
AddRowNumber.create_numbering from step grouping_java_columns ordering_columns directions java_problem_aggregator
Column.from_storage name new_storage
AdRiley marked this conversation as resolved.
Show resolved Hide resolved

## PRIVATE
If the table already contains a column called `name` it will be renamed to a
unique name, so that a new column with this name can be added.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
private

from Standard.Base import all
import Standard.Base.Errors.Common.Unsupported_Argument_Types
import Standard.Base.Errors.Illegal_Argument.Illegal_Argument
Expand All @@ -19,27 +21,31 @@ polyglot java import org.enso.table.data.column.storage.numeric.LongRangeStorage
polyglot java import org.enso.table.operations.AddRunning

## PRIVATE
add_running : Table -> Statistic -> (Text|Integer) -> Text -> Vector (Text | Integer | Regex) | Text | Integer | Regex -> Vector (Text | Sort_Column) | Text -> Problem_Behavior -> Table
add_running table (statistic:Statistic=Statistic.Count) (of:Text|Integer=0) (as:Text='') (group_by:(Vector | Text | Integer | Regex)=[]) (order_by:(Vector | Text)=[]) (on_problems:Problem_Behavior=..Report_Warning) =
add_running : Table -> Statistic -> (Text|Integer) -> Text -> Set_Mode -> Vector (Text | Integer | Regex) | Text | Integer | Regex -> Vector (Text | Sort_Column) | Text -> Problem_Behavior -> Table
add_running table (statistic:Statistic=Statistic.Count) (of:Text|Integer=0) (as:Text='') (set_mode:Set_Mode=..Add) (group_by:(Vector | Text | Integer | Regex)=[]) (order_by:(Vector | Text)=[]) (on_problems:Problem_Behavior=..Report_Warning) =
check_running_support [statistic] <|
of_col = table.at of
new_name = if as.is_empty then 'Running ' + statistic.to_text + ' of ' + of_col.name else as
case statistic of
Statistic.Count ->
Add_Row_Number.add_row_number table new_name 1 1 group_by order_by on_problems
new_name = case as.is_empty of
False -> as
True -> case set_mode of
Set_Mode.Update -> of_col.name
_ -> 'Running ' + statistic.to_text + ' of ' + of_col.name

problem_builder = Problem_Builder.new error_on_missing_columns=True
grouping_columns = table.columns_helper.select_columns_helper group_by Case_Sensitivity.Default True problem_builder
ordering = Table_Helpers.resolve_order_by table.columns order_by problem_builder
source_java_column = of_col.java_column
grouping_java_columns = grouping_columns.map c->c.java_column
ordering_java_columns = ordering.map c->
c.column.java_column
directions = ordering.map c->
c.associated_selector.direction.to_sign
new_column = case statistic of
Statistic.Count ->
Add_Row_Number.create_column table new_name from=1 step=1 grouping_columns ordering on_problems
_ ->
Value_Type.expect_numeric of_col <|
problem_builder = Problem_Builder.new error_on_missing_columns=True
grouping_columns = table.columns_helper.select_columns_helper group_by Case_Sensitivity.Default True problem_builder
ordering = Table_Helpers.resolve_order_by table.columns order_by problem_builder
source_java_column = of_col.java_column
grouping_java_columns = grouping_columns.map c->c.java_column
ordering_java_columns = ordering.map c->
c.column.java_column
directions = ordering.map c->
c.associated_selector.direction.to_sign

Java_Problems.with_problem_aggregator on_problems java_problem_aggregator->
new_storage = AddRunning.create_running statistic.to_java source_java_column grouping_java_columns ordering_java_columns directions java_problem_aggregator
new_column = Column.from_storage new_name new_storage
table.set new_column new_name set_mode=Set_Mode.Add
new_storage = Java_Problems.with_problem_aggregator on_problems java_problem_aggregator->
AddRunning.create_running statistic.to_java source_java_column grouping_java_columns ordering_java_columns directions java_problem_aggregator
Column.from_storage new_name new_storage
table.set new_column new_name set_mode
8 changes: 5 additions & 3 deletions distribution/lib/Standard/Table/0.0.0-dev/src/Table.enso
Original file line number Diff line number Diff line change
Expand Up @@ -3603,6 +3603,8 @@ type Table
- statistic: The running statistic to calculate.
- of: The existing column to run the statistic over.
- as: The name of the new column.
- set_mode: Specifies the expected behaviour in regards to existing
column with the same name.
- group_by: Specifies the columns to group by. The running statistic is
calculated separately for each group. By default, all rows are treated as
a single group.
Expand All @@ -3629,9 +3631,9 @@ type Table
@group_by Widget_Helpers.make_column_name_multi_selector
@order_by Widget_Helpers.make_order_by_selector
@of Widget_Helpers.make_column_name_selector
running : Statistic -> (Text | Integer) -> Text -> Vector (Text | Integer | Regex) | Text | Integer | Regex -> Vector (Text | Sort_Column) | Text -> Problem_Behavior -> Table
running self (statistic:Statistic=..Count) (of:(Text | Integer)=0) (as:Text='') (group_by:(Vector | Text | Integer | Regex)=[]) (order_by:(Vector | Text)=[]) (on_problems:Problem_Behavior=..Report_Warning) =
Add_Running.add_running self statistic of as group_by order_by on_problems
running : Statistic -> (Text | Integer) -> Text -> Set_Mode -> Vector (Text | Integer | Regex) | Text | Integer | Regex -> Vector (Text | Sort_Column) | Text -> Problem_Behavior -> Table
running self (statistic:Statistic=..Count) (of:(Text | Integer)=0) (as:Text='') (set_mode:Set_Mode=..Add) (group_by:(Vector | Text | Integer | Regex)=[]) (order_by:(Vector | Text)=[]) (on_problems:Problem_Behavior=..Report_Warning) =
Add_Running.add_running self statistic of as set_mode group_by order_by on_problems

## PRIVATE
column_naming_helper : Column_Naming_Helper
Expand Down
Loading
Loading