From c12cab9bd98b256cd4c16a563bd953f362fff696 Mon Sep 17 00:00:00 2001 From: Ara Adkins Date: Thu, 2 Sep 2021 10:30:02 +0100 Subject: [PATCH] Add `Column.set_index` (#1982) --- RELEASES.md | 9 ++++++ .../0.2.28-SNAPSHOT/src/Data/Column.enso | 19 +++++++++++++ .../Table/0.2.28-SNAPSHOT/src/Data/Table.enso | 9 ++++-- .../java/org/enso/table/data/index/Index.java | 6 ++-- .../org/enso/table/data/table/Column.java | 13 +++++++++ .../java/org/enso/table/data/table/Table.java | 28 ++++++++++++++----- test/Table_Tests/src/Column_Spec.enso | 5 ++++ test/Table_Tests/src/Table_Spec.enso | 5 ++++ 8 files changed, 80 insertions(+), 14 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index f0d9c3b71599..3c62a045336b 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,5 +1,14 @@ # Enso Next +## Libraries + +- Added `Column.set_index` to allow you to explicitly change the index for a + column ([#1982](https://github.com/enso-org/enso/pull/1982)). Also expanded + `Table.set_index` to accept columns, rather than just names of columns in the + table. + +## Tooling + - Implement `library/preinstall` endpoint, allowing the IDE to request a library to be installed asynchronously before importing it, so that adding the import does not seem to freeze the compiler diff --git a/distribution/lib/Standard/Table/0.2.28-SNAPSHOT/src/Data/Column.enso b/distribution/lib/Standard/Table/0.2.28-SNAPSHOT/src/Data/Column.enso index 69c9e25d2eaa..dfb2088d503e 100644 --- a/distribution/lib/Standard/Table/0.2.28-SNAPSHOT/src/Data/Column.enso +++ b/distribution/lib/Standard/Table/0.2.28-SNAPSHOT/src/Data/Column.enso @@ -690,11 +690,30 @@ type Column > Example Get the index from a column. + + import Standard.Examples + + example_index = Examples.decimal_column.index index : Column ! Table.No_Index_Set_Error index = case this.java_column.getIndex.toColumn of Nothing -> Error.throw Table.No_Index_Set_Error i -> Column i + ## Sets the index of this column, using the provided column. + + Arguments: + - index: The column to use as the index to this column. + + > Example + Index the decimal column by the integer column. + + import Standard.Examples + + example_set_index = + Examples.decimal_column.set_index Examples.integer_column + set_index : Column -> Column + set_index index = Column (this.java_column.setIndex index.java_column) + ## Returns the value contained in this column at the given index. Arguments: diff --git a/distribution/lib/Standard/Table/0.2.28-SNAPSHOT/src/Data/Table.enso b/distribution/lib/Standard/Table/0.2.28-SNAPSHOT/src/Data/Table.enso index 873dfa88d056..1e5344f5d139 100644 --- a/distribution/lib/Standard/Table/0.2.28-SNAPSHOT/src/Data/Table.enso +++ b/distribution/lib/Standard/Table/0.2.28-SNAPSHOT/src/Data/Table.enso @@ -298,7 +298,8 @@ type Table ## Sets the index of this table, using the column with the provided name. Arguments: - - index: The name of the column to use as the index in this table. + - index: The name of the column to use as the index in this table, or the + column itself to use. > Example Set the index of the inventory table to be the item name. @@ -306,8 +307,10 @@ type Table import Standard.Examples example_set_index = Examples.inventory_table.set_index "item_name" - set_index : Text -> Table - set_index index = Table (this.java_table.indexFromColumn index) + set_index : Text | Column -> Table + set_index index = case index of + Text -> Table (this.java_table.indexFromColumn index) + Column.Column c -> Table (this.java_table.indexFromColumn c) ## Returns the index of this table, as a column that is indexed by itself. diff --git a/std-bits/table/src/main/java/org/enso/table/data/index/Index.java b/std-bits/table/src/main/java/org/enso/table/data/index/Index.java index fb6d76b4e001..c47a0f19ec38 100644 --- a/std-bits/table/src/main/java/org/enso/table/data/index/Index.java +++ b/std-bits/table/src/main/java/org/enso/table/data/index/Index.java @@ -1,11 +1,9 @@ package org.enso.table.data.index; -import org.enso.table.data.column.storage.Storage; -import org.enso.table.data.mask.OrderMask; -import org.enso.table.data.table.Column; - import java.util.BitSet; import java.util.List; +import org.enso.table.data.mask.OrderMask; +import org.enso.table.data.table.Column; /** A storage class for ordered multisets. */ public abstract class Index { diff --git a/std-bits/table/src/main/java/org/enso/table/data/table/Column.java b/std-bits/table/src/main/java/org/enso/table/data/table/Column.java index 00a29100bb08..629b0f972e96 100644 --- a/std-bits/table/src/main/java/org/enso/table/data/table/Column.java +++ b/std-bits/table/src/main/java/org/enso/table/data/table/Column.java @@ -9,6 +9,7 @@ import org.enso.table.data.column.storage.BoolStorage; import org.enso.table.data.column.storage.Storage; import org.enso.table.data.index.DefaultIndex; +import org.enso.table.data.index.HashIndex; import org.enso.table.data.index.Index; import org.enso.table.data.mask.OrderMask; import org.enso.table.error.UnexpectedColumnTypeException; @@ -133,6 +134,18 @@ public Column withIndex(Index ix) { return new Column(name, ix, storage); } + /** + * Sets the index of this column to the provided column. + * + * @param col the column to use as the index. + * @return a column indexed by {@code col} + */ + public Column setIndex(Column col) { + Storage storage = col.getStorage(); + Index ix = HashIndex.fromStorage(col.getName(), storage); + return this.withIndex(ix); + } + /** @return the index of this column */ public Index getIndex() { return index; diff --git a/std-bits/table/src/main/java/org/enso/table/data/table/Table.java b/std-bits/table/src/main/java/org/enso/table/data/table/Table.java index 5a833072c8ac..6cfcc411a23a 100644 --- a/std-bits/table/src/main/java/org/enso/table/data/table/Table.java +++ b/std-bits/table/src/main/java/org/enso/table/data/table/Table.java @@ -1,8 +1,12 @@ package org.enso.table.data.table; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.BitSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; import java.util.stream.Collectors; - import org.enso.table.data.column.builder.object.InferredBuilder; import org.enso.table.data.column.storage.BoolStorage; import org.enso.table.data.column.storage.Storage; @@ -157,12 +161,10 @@ public Index getIndex() { /** * Reindexes this table by using values from the column with the given name. * - * @param name the column name to use as index + * @param col the column to use as index * @return a table indexed by the proper column */ - public Table indexFromColumn(String name) { - Column col = getColumnByName(name); - if (col == null) throw new NoSuchColumnException(name); + public Table indexFromColumn(Column col) { Storage storage = col.getStorage(); Index ix = HashIndex.fromStorage(col.getName(), storage); List newColumns = new ArrayList<>(); @@ -171,13 +173,25 @@ public Table indexFromColumn(String name) { newColumns.add(indexCol.withIndex(ix)); } for (Column column : columns) { - if (!column.getName().equals(name)) { + if (!column.getName().equals(col.getName())) { newColumns.add(column.withIndex(ix)); } } return new Table(newColumns.toArray(new Column[0]), ix); } + /** + * Reindexes this table by using values from the column with the given name. + * + * @param name the column name to use as index + * @return a table indexed by the proper column + */ + public Table indexFromColumn(String name) { + Column col = getColumnByName(name); + if (col == null) throw new NoSuchColumnException(name); + return indexFromColumn(col); + } + /** * Selects a subset of columns of this table, by names. * diff --git a/test/Table_Tests/src/Column_Spec.enso b/test/Table_Tests/src/Column_Spec.enso index 196c3ade40dd..e679693231d5 100644 --- a/test/Table_Tests/src/Column_Spec.enso +++ b/test/Table_Tests/src/Column_Spec.enso @@ -1,6 +1,7 @@ from Standard.Base import all from Standard.Table import all +import Standard.Examples import Standard.Table.Data.Column import Standard.Test @@ -59,3 +60,7 @@ spec = Test.group "Columns" <| c_2 = Column.from_vector "c_2" ["foo", "bar", "foo", "baz", "bar"] c_2.duplicate_count.to_vector.should_equal [0, 0, 1, 0, 1] + Test.specify "should allow setting their index" <| + col = Examples.decimal_column.set_index Examples.integer_column + col.index.to_vector . should_equal Examples.integer_column.to_vector + diff --git a/test/Table_Tests/src/Table_Spec.enso b/test/Table_Tests/src/Table_Spec.enso index 9704b11e1a58..01b85c007ab2 100644 --- a/test/Table_Tests/src/Table_Spec.enso +++ b/test/Table_Tests/src/Table_Spec.enso @@ -496,6 +496,7 @@ spec = Test.group "Index" <| t = Table.new [['ix', [1, 2, 3]], ['c1', [4, 5, 6]]] . set_index 'ix' + c = Column.from_vector "name" ["a", "b", "c"] Test.specify "should be accessible by `at` like other columns" <| c = t.at 'ix' i = t.index @@ -504,6 +505,10 @@ spec = Test.specify "should be accessible by `select` like other columns" <| t.select ['ix'] . columns . first . to_vector . should_equal t.index.to_vector + Test.specify "should be able to be set by column" <| + with_index = t.set_index c + with_index.index.to_vector . should_equal c.to_vector + Test.group "Slicing Tables" <| Test.specify 'should allow taking first n rows' <| i_1 = ['ix', [1, 2, 3]]