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

Make expand_to_rows, expand_column support Rows, Tables, Column data types #9533

Merged
merged 5 commits into from
Mar 26, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,8 @@
- [Allow `Table.replace` to take mutiple target columns.][9406]
- [Initial Snowflake Support - ability to read and query, not a completed
dialect yet.][9435]
- [Make expand_to_rows, expand_column support Rows, Tables, Column data
types][9533]

[debug-shortcuts]:
https://github.com/enso-org/enso/blob/develop/app/gui/docs/product/shortcuts.md#debug
Expand Down Expand Up @@ -928,6 +930,7 @@
[9343]: https://github.com/enso-org/enso/pull/9343
[9406]: https://github.com/enso-org/enso/pull/9406
[9435]: https://github.com/enso-org/enso/pull/9435
[9533]: https://github.com/enso-org/enso/pull/9533

#### Enso Compiler

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2391,7 +2391,9 @@ type DB_Table
- `List`
- `Range`
- `Date_Range`
- `Pair
- `Pair`
- `Table`
- `Column`

Any other values are treated as non-aggregate values, and their rows are kept
unchanged.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from Standard.Base import all
from Standard.Table import Column, Table
import project.Data.Row.Row
import Standard.Base.Data.Java_Json.Jackson_Object
import Standard.Base.Errors.Illegal_Argument.Illegal_Argument

Expand Down Expand Up @@ -30,7 +32,16 @@ Convertible_To_Columns.from (that:Map) =

## PRIVATE
Convertible_To_Columns.from (that:Pair) =
Convertible_To_Columns.Value ["0", "1"] (k-> if k == "0" then that.first else that.second)
Convertible_To_Columns.Value ["0", "1"] (k-> that.at (Integer.parse k))

## PRIVATE
Convertible_To_Columns.from (that:Column) =
fields = 0.up_to that.length . map _.to_text
Convertible_To_Columns.Value fields (k-> that.at (Integer.parse k))

## PRIVATE
Convertible_To_Columns.from (that:Row) =
Convertible_To_Columns.Value that.column_names that.get

## PRIVATE
Convertible_To_Columns.from (that:Vector) =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from Standard.Base import all
from Standard.Table import Column, Table
import Standard.Base.Data.Java_Json.Jackson_Object

import project.Data.Conversions.Convertible_To_Columns.Convertible_To_Columns
Expand Down Expand Up @@ -26,6 +27,14 @@ type Convertible_To_Rows
to_vector : Vector Any
to_vector self = 0.up_to self.length . map self.getter

## PRIVATE
Convertible_To_Rows.from that:Table =
rows = that.rows
Convertible_To_Rows.from rows

## PRIVATE
Convertible_To_Rows.from that:Column = Convertible_To_Rows.Value that.length that.get

## PRIVATE
Convertible_To_Rows.from that:Vector = Convertible_To_Rows.Value that.length that.get

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1404,7 +1404,9 @@ type Table
- `List`
- `Range`
- `Date_Range`
- `Pair
- `Pair`
- `Table`
- `Column`

Any other values are treated as non-aggregate values, and their rows are kept
unchanged.
Expand Down
35 changes: 35 additions & 0 deletions test/Table_Tests/src/In_Memory/Table_Conversion_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,27 @@ add_specs suite_builder =
table = Table.new [["aaa", [1, 2]], ["bbb", [Date.new 2020 12 1 . up_to (Date.new 2020 12 3), Date.new 2022 12 1 . up_to (Date.new 2022 12 2)]]]
expected = Table.new [["aaa", [1, 2]], ["bbb 0", [Date.new 2020 12 1, Date.new 2022 12 1]], ["bbb 1", [Date.new 2020 12 2, Nothing]]]
table.expand_column "bbb" . should_equal expected

group_builder.specify "Expands rows within a column" <|
table1 = Table.new [["aaa", [1, 2, 3]], ["bbb", [4, 5, 6]], ["ccc", [7, 8, 9]]]
table2 = Table.new [["aaa", [10]], ["bbb", [11]], ["ccc", [12]]]
table = Table.new [["tables", [table1, table2]]]
expected = Table.new [["tables aaa", [1, 2, 3, 10]], ["tables bbb", [4, 5, 6, 11]], ["tables ccc", [7, 8, 9, 12]]]
table.expand_to_rows "tables" . expand_column "tables" . should_equal expected
AdRiley marked this conversation as resolved.
Show resolved Hide resolved

group_builder.specify "Expands rows within a column for ragged tables" <|
table1 = Table.new [["aaa", [1, 2, 3]], ["bbb", [4, 5, 6]]]
table2 = Table.new [["aaa", [10]], ["bbb", [11]], ["ccc", [12]]]
table = Table.new [["tables", [table1, table2]]]
expected = Table.new [["tables aaa", [1, 2, 3, 10]], ["tables bbb", [4, 5, 6, 11]], ["tables ccc", [Nothing, Nothing, Nothing, 12]]]
table.expand_to_rows "tables" . expand_column "tables" . should_equal expected

group_builder.specify "Expands columns within a column" <|
column1 = Column.from_vector "c1" ["a", "b", "c"]
column2 = Column.from_vector "c2" ["d", "e"]
table = Table.new [["cols", [column1, column2]]]
expected = Table.new [["cols 0", ["a", "d"]], ["cols 1", ["b", "e"]], ["cols 2", ["c", Nothing]]]
table.expand_column "cols" . should_equal expected

group_builder.specify "will work even if keys are not Text" <|
table = Table.new [["a", [1, 2]], ["b", [Map.from_vector [[1, "x"], [2, "y"]], Map.from_vector [[2, "z"], [3, "w"]]]]]
Expand Down Expand Up @@ -335,6 +356,20 @@ add_specs suite_builder =
table = Table.new [["aaa", [1, 2, 3]], ["bbb", values_to_expand], ["ccc", [5, 6, 7]]]
expected = Table.new [["aaa", [1, 1, 2, 2, 2, 2, 3, 3]], ["bbb", values_expanded], ["ccc", [5, 5, 6, 6, 6, 6, 7, 7]]]
table.expand_to_rows "bbb" . should_equal expected

group_builder.specify "Can expand tables" <|
table1 = Table.new [["aaa", [1, 2, 3]], ["bbb", [4, 5, 6]], ["ccc", [7, 8, 9]]]
table2 = Table.new [["aaa", [10]], ["bbb", [11]], ["ccc", [12]]]
table = Table.new [["tables", [table1, table2]]]
expected = Table.new [["tables", [table1.rows.at 0, table1.rows.at 1, table1.rows.at 2, table2.rows.at 0]]]
table.expand_to_rows "tables" . should_equal expected

group_builder.specify "Can expand column of columns" <|
column1 = Column.from_vector "c1" ["a", "b", "c"]
column2 = Column.from_vector "c2" ["d", "e"]
table = Table.new [["cols", [column1, column2]]]
expected = Table.new [["cols", ["a", "b", "c", "d", "e"]]]
table.expand_to_rows "cols" . should_equal expected

group_builder.specify "Respects `at_least_one_row=True`" <|
values_to_expand = [[10, 11], [], [30]]
Expand Down