Skip to content

Commit

Permalink
Add selectDistinct / countBy to table API
Browse files Browse the repository at this point in the history
  • Loading branch information
devinrsmith committed Dec 3, 2021
1 parent 9ef33fe commit d9d8fb1
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ protected void execute(FlightSession flight) throws Exception {
final TableSpec spec = InMemoryKeyBackedInputTable.of(TableHeader.of(inputKey, groupKey, u, v, w, x, y, z),
Collections.singletonList(inputKey.name()));
builder.putMap("base", spec);
builder.putMap("countBy", spec.countBy("Count", groupKey.name()).sort(groupKey.name()));
builder.putMap("selectDistinct", spec.selectDistinct(groupKey.name()).sort(groupKey.name()));

// todo: countBy
for (AggSpec aggSpec : aggSpecs()) {
final String name = aggSpec.toString()
Expand All @@ -96,7 +99,7 @@ protected void execute(FlightSession flight) throws Exception {
.replace('}', '_')
.replace(' ', '_')
.replace('.', '_');
final TableSpec tableSpec = spec.aggAllBy(aggSpec, groupKey.name());
final TableSpec tableSpec = spec.aggAllBy(aggSpec, groupKey.name()).sort(groupKey.name());
builder.putMap(name, tableSpec);
}
final LabeledTables tables = builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import io.deephaven.proto.backplane.grpc.NotCondition;
import io.deephaven.proto.backplane.grpc.OrCondition;
import io.deephaven.proto.backplane.grpc.Reference;
import io.deephaven.proto.backplane.grpc.SelectDistinctRequest;
import io.deephaven.proto.backplane.grpc.SelectOrUpdateRequest;
import io.deephaven.proto.backplane.grpc.SnapshotTableRequest;
import io.deephaven.proto.backplane.grpc.SortDescriptor;
Expand All @@ -87,6 +88,7 @@
import io.deephaven.qst.table.AggregationTable;
import io.deephaven.qst.table.AsOfJoinTable;
import io.deephaven.qst.table.ByTableBase;
import io.deephaven.qst.table.CountByTable;
import io.deephaven.qst.table.EmptyTable;
import io.deephaven.qst.table.ExactJoinTable;
import io.deephaven.qst.table.HeadTable;
Expand All @@ -99,6 +101,7 @@
import io.deephaven.qst.table.NewTable;
import io.deephaven.qst.table.ReverseAsOfJoinTable;
import io.deephaven.qst.table.ReverseTable;
import io.deephaven.qst.table.SelectDistinctTable;
import io.deephaven.qst.table.SelectTable;
import io.deephaven.qst.table.SingleParentTable;
import io.deephaven.qst.table.SnapshotTable;
Expand Down Expand Up @@ -451,6 +454,16 @@ public void visit(InMemoryKeyBackedInputTable inMemoryKeyBacked) {
out = op(Builder::setCreateInputTable, builder);
}

@Override
public void visit(SelectDistinctTable selectDistinctTable) {
out = op(Builder::setSelectDistinct, selectDistinct(selectDistinctTable));
}

@Override
public void visit(CountByTable countByTable) {
out = op(Builder::setComboAggregate, countBy(countByTable));
}

private SelectOrUpdateRequest selectOrUpdate(SingleParentTable x,
Collection<Selectable> columns) {
SelectOrUpdateRequest.Builder builder =
Expand Down Expand Up @@ -485,6 +498,24 @@ private ComboAggregateRequest.Builder groupByColumns(ByTableBase base) {
}
return builder;
}

private SelectDistinctRequest selectDistinct(SelectDistinctTable selectDistinctTable) {
SelectDistinctRequest.Builder builder = SelectDistinctRequest.newBuilder()
.setResultId(ticket)
.setSourceId(ref(selectDistinctTable.parent()));
for (Selectable column : selectDistinctTable.groupByColumns()) {
builder.addColumnNames(Strings.of(column));
}
return builder.build();
}

private ComboAggregateRequest countBy(CountByTable countByTable) {
final Aggregate aggregate = Aggregate.newBuilder()
.setType(AggType.COUNT)
.setColumnName(countByTable.countName().name())
.build();
return groupByColumns(countByTable).addAggregates(aggregate).build();
}
}

private static class AggregationAdapter implements Aggregation.Visitor {
Expand Down
16 changes: 16 additions & 0 deletions qst/graphviz/src/main/java/io/deephaven/graphviz/LabelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.deephaven.qst.table.AggregateAllByTable;
import io.deephaven.qst.table.AggregationTable;
import io.deephaven.qst.table.AsOfJoinTable;
import io.deephaven.qst.table.CountByTable;
import io.deephaven.qst.table.EmptyTable;
import io.deephaven.qst.table.ExactJoinTable;
import io.deephaven.qst.table.HeadTable;
Expand All @@ -14,6 +15,7 @@
import io.deephaven.qst.table.JoinTable;
import io.deephaven.qst.table.NaturalJoinTable;
import io.deephaven.qst.table.ReverseAsOfJoinTable;
import io.deephaven.qst.table.SelectDistinctTable;
import io.deephaven.qst.table.SelectTable;
import io.deephaven.qst.table.SelectableTable;
import io.deephaven.qst.table.TableSpec;
Expand Down Expand Up @@ -164,6 +166,20 @@ public void visit(InMemoryKeyBackedInputTable inMemoryKeyBacked) {
});
}

@Override
public void visit(SelectDistinctTable selectDistinctTable) {
sb.append("selectDistinct(");
append(Strings::of, selectDistinctTable.groupByColumns(), sb);
sb.append(')');
}

@Override
public void visit(CountByTable countByTable) {
sb.append("countBy(").append(countByTable.countName()).append(',');
append(Strings::of, countByTable.groupByColumns(), sb);
sb.append(')');
}

private void join(String name, Join j) {
sb.append(name).append("([");
append(Strings::of, j.matches(), sb);
Expand Down
38 changes: 34 additions & 4 deletions qst/src/main/java/io/deephaven/qst/TableAdapterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.deephaven.qst.table.AggregateAllByTable;
import io.deephaven.qst.table.AggregationTable;
import io.deephaven.qst.table.AsOfJoinTable;
import io.deephaven.qst.table.CountByTable;
import io.deephaven.qst.table.EmptyTable;
import io.deephaven.qst.table.ExactJoinTable;
import io.deephaven.qst.table.HeadTable;
Expand All @@ -18,6 +19,7 @@
import io.deephaven.qst.table.ParentsVisitor;
import io.deephaven.qst.table.ReverseAsOfJoinTable;
import io.deephaven.qst.table.ReverseTable;
import io.deephaven.qst.table.SelectDistinctTable;
import io.deephaven.qst.table.SelectTable;
import io.deephaven.qst.table.SingleParentTable;
import io.deephaven.qst.table.SnapshotTable;
Expand Down Expand Up @@ -230,14 +232,22 @@ public void visit(ReverseAsOfJoinTable raj) {
@Override
public void visit(AggregateAllByTable aggAllByTable) {
final AggSpec spec = aggAllByTable.spec();
final Selectable[] groupByColumns = aggAllByTable.groupByColumns().toArray(new Selectable[0]);
addOp(aggAllByTable, parentOps(aggAllByTable).aggAllBy(spec, groupByColumns));
if (aggAllByTable.groupByColumns().isEmpty()) {
addOp(aggAllByTable, parentOps(aggAllByTable).aggAllBy(spec));
} else {
final Selectable[] groupByColumns = aggAllByTable.groupByColumns().toArray(new Selectable[0]);
addOp(aggAllByTable, parentOps(aggAllByTable).aggAllBy(spec, groupByColumns));
}
}

@Override
public void visit(AggregationTable aggregationTable) {
addOp(aggregationTable,
parentOps(aggregationTable).aggBy(aggregationTable.aggregations(), aggregationTable.groupByColumns()));
if (aggregationTable.groupByColumns().isEmpty()) {
addOp(aggregationTable, parentOps(aggregationTable).aggBy(aggregationTable.aggregations()));
} else {
addOp(aggregationTable, parentOps(aggregationTable).aggBy(aggregationTable.aggregations(),
aggregationTable.groupByColumns()));
}
}

@Override
Expand All @@ -250,6 +260,26 @@ public void visit(InputTable inputTable) {
addTable(inputTable, tableCreation.of(inputTable));
}

@Override
public void visit(SelectDistinctTable selectDistinctTable) {
if (selectDistinctTable.groupByColumns().isEmpty()) {
addOp(selectDistinctTable, parentOps(selectDistinctTable).selectDistinct());
} else {
addOp(selectDistinctTable,
parentOps(selectDistinctTable).selectDistinct(selectDistinctTable.groupByColumns()));
}
}

@Override
public void visit(CountByTable countByTable) {
if (countByTable.groupByColumns().isEmpty()) {
addOp(countByTable, parentOps(countByTable).countBy(countByTable.countName().name()));
} else {
addOp(countByTable, parentOps(countByTable).countBy(countByTable.countName().name(),
countByTable.groupByColumns().toArray(new Selectable[0])));
}
}

private final class OutputTable implements Output<TOPS, TABLE> {
private final TABLE table;

Expand Down
27 changes: 27 additions & 0 deletions qst/src/main/java/io/deephaven/qst/table/CountByTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.deephaven.qst.table;

import io.deephaven.annotations.NodeStyle;
import io.deephaven.api.ColumnName;
import org.immutables.value.Value.Immutable;

@Immutable
@NodeStyle
public abstract class CountByTable extends ByTableBase {

public static Builder builder() {
return ImmutableCountByTable.builder();
}

public abstract ColumnName countName();

@Override
public final <V extends Visitor> V walk(V visitor) {
visitor.visit(this);
return visitor;
}

public interface Builder extends ByTableBase.Builder<CountByTable, CountByTable.Builder> {

Builder countName(ColumnName countName);
}
}
10 changes: 10 additions & 0 deletions qst/src/main/java/io/deephaven/qst/table/ParentsVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@ public void visit(TableHeader header) {
});
}

@Override
public void visit(SelectDistinctTable selectDistinctTable) {
out = single(selectDistinctTable);
}

@Override
public void visit(CountByTable countByTable) {
out = single(countByTable);
}

private static class Search {

private final Predicate<TableSpec> excludePaths;
Expand Down
23 changes: 23 additions & 0 deletions qst/src/main/java/io/deephaven/qst/table/SelectDistinctTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.deephaven.qst.table;

import io.deephaven.annotations.NodeStyle;
import org.immutables.value.Value.Immutable;

@Immutable
@NodeStyle
public abstract class SelectDistinctTable extends ByTableBase {

public static Builder builder() {
return ImmutableSelectDistinctTable.builder();
}

@Override
public final <V extends Visitor> V walk(V visitor) {
visitor.visit(this);
return visitor;
}

public interface Builder extends ByTableBase.Builder<SelectDistinctTable, SelectDistinctTable.Builder> {

}
}
49 changes: 49 additions & 0 deletions qst/src/main/java/io/deephaven/qst/table/TableBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,55 @@ public final AggregationTable aggBy(Collection<? extends Aggregation> aggregatio
.addAllAggregations(aggregations).build();
}

@Override
public final SelectDistinctTable selectDistinct() {
return SelectDistinctTable.builder().parent(this).build();
}

@Override
public final SelectDistinctTable selectDistinct(String... groupByColumns) {
final SelectDistinctTable.Builder builder = SelectDistinctTable.builder().parent(this);
for (String groupByColumn : groupByColumns) {
builder.addGroupByColumns(Selectable.parse(groupByColumn));
}
return builder.build();
}

@Override
public final SelectDistinctTable selectDistinct(Selectable... groupByColumns) {
return selectDistinct(Arrays.asList(groupByColumns));
}

@Override
public final SelectDistinctTable selectDistinct(Collection<? extends Selectable> groupByColumns) {
return SelectDistinctTable.builder().parent(this).addAllGroupByColumns(groupByColumns).build();
}

@Override
public final CountByTable countBy(String countColumnName) {
return CountByTable.builder().parent(this).countName(ColumnName.of(countColumnName)).build();
}

@Override
public final CountByTable countBy(String countColumnName, String... groupByColumns) {
return countBy(countColumnName, Arrays.asList(groupByColumns));
}

@Override
public final CountByTable countBy(String countColumnName, Selectable... groupByColumns) {
return CountByTable.builder().parent(this).countName(ColumnName.of(countColumnName))
.addGroupByColumns(groupByColumns).build();
}

@Override
public final CountByTable countBy(String countColumnName, Collection<String> groupByColumns) {
CountByTable.Builder builder = CountByTable.builder().parent(this).countName(ColumnName.of(countColumnName));
for (String groupByColumn : groupByColumns) {
builder.addGroupByColumns(Selectable.parse(groupByColumn));
}
return builder.build();
}

@Override
public final <V extends TableSchema.Visitor> V walk(V visitor) {
visitor.visit(this);
Expand Down
4 changes: 4 additions & 0 deletions qst/src/main/java/io/deephaven/qst/table/TableSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,9 @@ interface Visitor {
void visit(TicketTable ticketTable);

void visit(InputTable inputTable);

void visit(SelectDistinctTable selectDistinctTable);

void visit(CountByTable countByTable);
}
}
10 changes: 10 additions & 0 deletions qst/src/main/java/io/deephaven/qst/table/TableVisitorGeneric.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,14 @@ public void visit(TicketTable ticketTable) {
public void visit(InputTable inputTable) {
accept(inputTable);
}

@Override
public void visit(SelectDistinctTable selectDistinctTable) {
accept(selectDistinctTable);
}

@Override
public void visit(CountByTable countByTable) {
accept(countByTable);
}
}
22 changes: 22 additions & 0 deletions table-api/src/main/java/io/deephaven/api/TableOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -511,4 +511,26 @@ TOPS raj(TABLE rightTable, Collection<? extends JoinMatch> columnsToMatch,
TOPS aggBy(Collection<? extends Aggregation> aggregations, String... groupByColumns);

TOPS aggBy(Collection<? extends Aggregation> aggregations, Collection<? extends Selectable> groupByColumns);

// -------------------------------------------------------------------------------------------

TOPS selectDistinct();

TOPS selectDistinct(String... groupByColumns);

TOPS selectDistinct(Selectable... groupByColumns);

TOPS selectDistinct(Collection<? extends Selectable> groupByColumns);

// -------------------------------------------------------------------------------------------

TOPS countBy(String countColumnName);

TOPS countBy(String countColumnName, String... groupByColumns);

TOPS countBy(String countColumnName, Selectable... groupByColumns);

TOPS countBy(String countColumnName, Collection<String> groupByColumns);

// -------------------------------------------------------------------------------------------
}
Loading

0 comments on commit d9d8fb1

Please sign in to comment.