Skip to content

Commit

Permalink
Implement basic slicing
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Mar 16, 2021
1 parent 2e818d1 commit e0d3713
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 12 deletions.
7 changes: 7 additions & 0 deletions distribution/std-lib/Standard/src/Table/Data/Column.enso
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ type Column
new_col = this.java_column.applyMask mask
Column new_col

## UNSTABLE

Returns a column truncated to at most `max_rows` rows.
take_start : Integer -> Column
take_start max_rows =
Column (this.java_column.slice 0 max_rows)

## Creates a new column given a name and a vector of elements.
from_vector : Text -> Vector -> Column
from_vector name items = Column (Java_Column.fromItems name items.to_array)
Expand Down
7 changes: 7 additions & 0 deletions distribution/std-lib/Standard/src/Table/Data/Table.enso
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ type Table
concat other =
Table (this.java_table.concat other.java_table)

## UNSTABLE

Returns a table truncated to at most `max_rows` rows.
take_start : Integer -> Table
take_start max_rows =
Table (this.java_table.slice 0 max_rows)

## PRIVATE
comparator_to_java cmp x y = cmp x y . to_sign

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ import Standard.Table.Data.Column as Dataframe_Column
import Standard.Database.Data.Table as Database_Table
import Standard.Database.Data.Column as Database_Column

max_rows = 1000

# TODO add an initial offset to fully support lazy visualizations
## PRIVATE
prepare_visualization x = case x of
prepare_visualization x max_rows = case x of
Dataframe_Table.Table _ -> here.make_json x
Dataframe_Column.Column _ -> here.make_json x.to_table
Database_Table.Table _ _ _ _ ->
here.make_json <| x.to_dataframe here.max_rows
here.make_json <| x.to_dataframe max_rows
Database_Column.Column _ _ _ _ _ ->
here.make_json <| x.to_table.to_dataframe here.max_rows
here.make_json <| x.to_table.to_dataframe max_rows
_ -> x . to_json . to_text

## PRIVATE
make_json dataframe =
header = ["header", x.columns.map .name]
data = ["data", x.columns.map .to_vector . map (x -> x.take_start 2000) ]
make_json dataframe all_rows_count =
header = ["header", dataframe.columns.map .name]
data = ["data", dataframe.columns.map .to_vector . map (x -> x.take_start 2000) ]
pairs = [header,data]
Json.from_pairs pairs . to_text
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.BitSet;
import java.util.Comparator;

import org.enso.table.data.column.operation.map.MapOpStorage;
import org.enso.table.data.column.operation.map.MapOperation;
import org.enso.table.data.column.operation.map.UnaryMapOperation;
Expand Down Expand Up @@ -307,4 +306,14 @@ public static BitSet toMask(BoolStorage storage) {
public Comparator getDefaultComparator() {
return Comparator.naturalOrder();
}

@Override
public BoolStorage slice(int offset, int limit) {
int newSize = Math.min(size - offset, limit);
return new BoolStorage(
values.get(offset, offset + limit),
isMissing.get(offset, offset + limit),
newSize,
negated);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,13 @@ public Storage run(DoubleStorage storage) {
});
return ops;
}

@Override
public DoubleStorage slice(int offset, int limit) {
int newSize = Math.min(data.length - offset, limit);
long[] newData = new long[newSize];
System.arraycopy(data, offset, newData, 0, newSize);
BitSet newMask = isMissing.get(offset, offset + limit);
return new DoubleStorage(newData, newSize, newMask);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,13 @@ public Storage run(LongStorage storage) {
});
return ops;
}

@Override
public LongStorage slice(int offset, int limit) {
int newSize = Math.min(data.length - offset, limit);
long[] newData = new long[newSize];
System.arraycopy(data, offset, newData, 0, newSize);
BitSet newMask = isMissing.get(offset, offset + limit);
return new LongStorage(newData, newSize, newMask);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.enso.table.data.column.storage;

import java.util.stream.DoubleStream;
import org.enso.table.data.column.operation.aggregate.Aggregator;
import org.enso.table.data.column.operation.aggregate.numeric.NumericAggregator;

import java.util.stream.DoubleStream;

/** A storage containing items representable as a {@code double}. */
public abstract class NumericStorage extends Storage {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.enso.table.data.column.storage;

import java.util.Arrays;
import java.util.BitSet;
import java.util.Comparator;

Expand Down Expand Up @@ -145,4 +146,12 @@ protected Storage run(ObjectStorage storage) {
});
return ops;
}

@Override
public ObjectStorage slice(int offset, int limit) {
int newSize = Math.min(data.length - offset, limit);
Object[] newData = new Object[newSize];
System.arraycopy(data, offset, newData, 0, newSize);
return new ObjectStorage(newData, newSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ protected final Storage fillMissingHelper(Object arg, Builder builder) {
*/
public abstract Comparator<Object> getDefaultComparator();

/** @return a copy of the storage containing a slice of the original data */
public abstract Storage slice(int offset, int limit);

public List<Object> toList() {
return new StorageListView(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,10 @@ protected boolean doString(String a, String b) {
});
return t;
}

@Override
public StringStorage slice(int offset, int limit) {
ObjectStorage storage = super.slice(offset, limit);
return new StringStorage(storage.getData(), storage.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,9 @@ public int size() {
public Index applyMask(OrderMask mask) {
return this;
}

@Override
public DefaultIndex slice(int offset, int limit) {
return new DefaultIndex(Math.min(size, limit));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,10 @@ public Index unique() {
public int size() {
return items.size();
}

@Override
public HashIndex slice(int offset, int limit) {
var newStorage = items.slice(offset, limit);
return new HashIndex(name, newStorage, newStorage.size());
}
}
3 changes: 3 additions & 0 deletions std-bits/src/main/java/org/enso/table/data/index/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,7 @@ public abstract class Index {

/** @return the number of elements in this index. */
public abstract int size();

/** @return a copy of the index containing a slice of the original data */
public abstract Index slice(int offset, int limit);
}
6 changes: 5 additions & 1 deletion std-bits/src/main/java/org/enso/table/data/table/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;
import java.util.function.Function;
import java.util.stream.IntStream;

import org.enso.table.data.column.builder.object.InferredBuilder;
import org.enso.table.data.column.operation.aggregate.Aggregator;
import org.enso.table.data.column.storage.BoolStorage;
Expand Down Expand Up @@ -167,4 +166,9 @@ public Column applyMask(OrderMask mask) {
Storage newStorage = storage.applyMask(mask);
return new Column(name, newIndex, newStorage);
}

/** @return a copy of the Column containing a slice of the original data */
public Column slice(int offset, int limit) {
return new Column(name, index.slice(offset, limit), storage.slice(offset, limit));
}
}
9 changes: 9 additions & 0 deletions std-bits/src/main/java/org/enso/table/data/table/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,13 @@ public AggregateTable group(String by) {
Table t = by == null ? this : indexFromColumn(by);
return new AggregateTable(t);
}

/** @return a copy of the Column containing a slice of the original data */
public Table slice(int offset, int limit) {
Column[] newColumns = new Column[columns.length];
for (int i = 0; i < columns.length; i++) {
newColumns[i] = columns[i].slice(offset, limit);
}
return new Table(newColumns, index.slice(offset, limit));
}
}
18 changes: 18 additions & 0 deletions test/Table_Tests/src/Table_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,21 @@ spec =
r = t_1.concat t_2

r.index.to_vector.should_equal [1, 2, 3, False, True]

Test.group "Slicing Tables" <|
Test.specify 'should allow to take first N elements' <|
i_1 = ['ix', [1, 2, 3]]
c_1 = ['col', [5, 6, 7]]
c_2 = ['col2', ["a", Nothing, "c"]]
c_3 = ['col3', [False, True, Nothing]]
t_1 = Table.new [i_1, c_1, c_2, c_3] . set_index 'ix'

t_1.take_start 10 . at 'col' . to_vector . should_equal (t_1.at 'col' . to_vector)

t_2 = t_1.take_start 2
t_2.index.to_vector . should_equal (t_1.index.to_vector . take_start 2)
t_2.at 'col' . to_vector . should_equal (t_1.at 'col' . to_vector . take_start 2)
t_2.at 'col2' . to_vector . should_equal (t_1.at 'col2' . to_vector . take_start 2)
t_2.at 'col3' . to_vector . should_equal (t_1.at 'col3' . to_vector . take_start 2)

t_1.at 'col' . take_start 2 . to_vector . should_equal (t_1.at 'col' . to_vector . take_start 2)

0 comments on commit e0d3713

Please sign in to comment.