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 Column.set_index #1982

Merged
merged 3 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,19 @@ 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.

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.

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions std-bits/table/src/main/java/org/enso/table/data/table/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 21 additions & 7 deletions std-bits/table/src/main/java/org/enso/table/data/table/Table.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Column> newColumns = new ArrayList<>();
Expand All @@ -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.
*
Expand Down
5 changes: 5 additions & 0 deletions test/Table_Tests/src/Column_Spec.enso
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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

5 changes: 5 additions & 0 deletions test/Table_Tests/src/Table_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]]
Expand Down