Struct perspective_js::View
source · pub struct View(/* private fields */);Expand description
The View struct is Perspective’s query and serialization interface. It
represents a query on the Table’s dataset and is always created from an
existing Table instance via the Table::view method. .
const table = await perspective.table({
id: [1, 2, 3, 4],
name: ["a", "b", "c", "d"],
});
const view = await table.view({ columns: ["name"] });
const json = await view.to_json();
await view.delete();
table = perspective.Table({
"id": [1, 2, 3, 4],
"name": ["a", "b", "c", "d"]
});
view = table.view(columns=["name"])
arrow = view.to_arrow()
view.delete()
let opts = TableInitOptions::default();
let data = TableData::Update(UpdateData::Csv("x,y\n1,2\n3,4".into()));
let table = client.table(data, opts).await?;
let view = table.view(None).await?;
let arrow = view.to_arrow().await?;
view.delete().await?;Views are immutable with respect to the arguments provided to the
Table::view method; to change these parameters, you must create a new
View on the same Table. However, each View is live with respect to
the Table’s data, and will (within a conflation window) update with the
latest state as its parent Table updates, including incrementally
recalculating all aggregates, pivots, filters, etc. View query parameters
are composable, in that each parameter works independently and in conjunction
with each other, and there is no limit to the number of pivots, filters, etc.
which can be applied.
§Querying data with Table::view
To query the table, create a Table::view on the table instance with an
optional configuration object. A Table can have as many Views associated
with it as you need - Perspective conserves memory by relying on a single
Table to power multiple Views concurrently:
const view = await table.view({
columns: ["Sales"],
aggregates: { Sales: "sum" },
group_by: ["Region", "Country"],
filter: [["Category", "in", ["Furniture", "Technology"]]],
});
view = table.view(
columns=["Sales"],
aggregates={"Sales": "sum"},
group_by=["Region", "Country"],
filter=[["Category", "in", ["Furniture", "Technology"]]]
)
use crate::config::*;
let view = table
.view(Some(ViewConfigUpdate {
columns: Some(vec![Some("Sales".into())]),
aggregates: Some(HashMap::from_iter(vec![("Sales".into(), "sum".into())])),
group_by: Some(vec!["Region".into(), "Country".into()]),
filter: Some(vec![Filter::new("Category", "in", &[
"Furniture",
"Technology",
])]),
..ViewConfigUpdate::default()
}))
.await?;§Group By
A group by groups the dataset by the unique values of each column used as a
group by - a close analogue in SQL to the GROUP BY statement. The underlying
dataset is aggregated to show the values belonging to each group, and a total
row is calculated for each group, showing the currently selected aggregated
value (e.g. sum) of the column. Group by are useful for hierarchies,
categorizing data and attributing values, i.e. showing the number of units sold
based on State and City. In Perspective, group by are represented as an array of
string column names to pivot, are applied in the order provided; For example, a
group by of ["State", "City", "Postal Code"] shows the values for each Postal
Code, which are grouped by City, which are in turn grouped by State.
§Example
const view = await table.view({ group_by: ["a", "c"] });
view = table.view(group_by=["a", "c"])
let view = table.view(Some(ViewConfigUpdate {
group_by: Some(vec!["a".into(), "c".into()]),
..ViewConfigUpdate::default()
})).await?;§Split By
A split by splits the dataset by the unique values of each column used as a
split by. The underlying dataset is not aggregated, and a new column is created
for each unique value of the split by. Each newly created column contains the
parts of the dataset that correspond to the column header, i.e. a View that
has ["State"] as its split by will have a new column for each state. In
Perspective, Split By are represented as an array of string column names to
pivot:
§Example
const view = await table.view({ split_by: ["a", "c"] });
view = table.view(split_by=["a", "c"])
let view = table.view(Some(ViewConfigUpdate {
split_by: Some(vec!["a".into(), "c".into()]),
..ViewConfigUpdate::default()
})).await?;§Aggregates
Aggregates perform a calculation over an entire column, and are displayed when
one or more Group By are applied to the View. Aggregates can be
specified by the user, or Perspective will use the following sensible default
aggregates based on column type:
- “sum” for
integerandfloatcolumns - “count” for all other columns
Perspective provides a selection of aggregate functions that can be applied to
columns in the View constructor using a dictionary of column name to aggregate
function name.
§Example
const view = await table.view({
aggregates: {
a: "avg",
b: "distinct count",
},
});
view = table.view(
aggregates={
"a": "avg",
"b": "distinct count"
}
)
§Columns
The columns property specifies which columns should be included in the
View’s output. This allows users to show or hide a specific subset of columns,
as well as control the order in which columns appear to the user. This is
represented in Perspective as an array of string column names:
§Example
const view = await table.view({
columns: ["a"],
});
view = table.view(columns=["a"])
§Sort
The sort property specifies columns on which the query should be sorted,
analogous to ORDER BY in SQL. A column can be sorted regardless of its data
type, and sorts can be applied in ascending or descending order. Perspective
represents sort as an array of arrays, with the values of each inner array
being a string column name and a string sort direction. When column-pivots are
applied, the additional sort directions "col asc" and "col desc" will
determine the order of pivot columns groups.
§Example
const view = await table.view({
sort: [["a", "asc"]],
});
view = table.view(sort=[["a", "asc"]])
§Filter
The filter property specifies columns on which the query can be filtered,
returning rows that pass the specified filter condition. This is analogous to
the WHERE clause in SQL. There is no limit on the number of columns where
filter is applied, but the resulting dataset is one that passes all the filter
conditions, i.e. the filters are joined with an AND condition.
Perspective represents filter as an array of arrays, with the values of each
inner array being a string column name, a string filter operator, and a filter
operand in the type of the column:
§Example
const view = await table.view({
filter: [["a", "<", 100]],
});
view = table.view(filter=[["a", "<", 100]])
§Expressions
The expressions property specifies new columns in Perspective that are
created using existing column values or arbitary scalar values defined within
the expression. In <perspective-viewer>, expressions are added using the “New
Column” button in the side panel.
A custom name can be added to an expression by making the first line a comment:
§Example
const view = await table.view({
expressions: { '"a" + "b"': '"a" + "b"' },
});
view = table.view(expressions=['"a" + "b"'])
§Flattening a Table::view into a Table
In Javascript, a Table can be constructed on a Table::view instance,
which will return a new Table based on the Table::view’s dataset, and
all future updates that affect the Table::view will be forwarded to the new
Table. This is particularly useful for implementing a
Client/Server Replicated design, by
serializing the View to an arrow and setting up an on_update callback.
§Example
const worker1 = perspective.worker();
const table = await worker.table(data);
const view = await table.view({ filter: [["State", "==", "Texas"]] });
const table2 = await worker.table(view);
table.update([{ State: "Texas", City: "Austin" }]);
table = perspective.Table(data);
view = table.view(filter=[["State", "==", "Texas"]])
table2 = perspective.Table(view.to_arrow());
def updater(port, delta):
table2.update(delta)
view.on_update(updater, mode="Row")
table.update([{"State": "Texas", "City": "Austin"}])
let opts = TableInitOptions::default();
let data = TableData::Update(UpdateData::Csv("x,y\n1,2\n3,4".into()));
let table = client.table(data, opts).await?;
let view = table.view(None).await?;
let table2 = client.table(TableData::View(view)).await?;
table.update(data).await?;Implementations§
source§impl View
impl View
sourcepub async fn column_paths(&self) -> ApiResult<JsValue>
pub async fn column_paths(&self) -> ApiResult<JsValue>
Returns an array of strings containing the column paths of the View without any of the source columns.
A column path shows the columns that a given cell belongs to after pivots are applied.
sourcepub async fn delete(self) -> ApiResult<()>
pub async fn delete(self) -> ApiResult<()>
Delete this View and clean up all resources associated with it. View objects do not stop consuming resources or processing updates when they are garbage collected - you must call this method to reclaim these.
sourcepub async fn dimensions(&self) -> ApiResult<JsValue>
pub async fn dimensions(&self) -> ApiResult<JsValue>
Returns this View’s dimensions, row and column count, as well as those of
the crate::Table from which it was derived.
num_table_rows- The number of rows in the underlyingcrate::Table.num_table_columns- The number of columns in the underlyingcrate::Table(including theindexcolumn if thiscrate::Tablewas constructed with one).num_view_rows- The number of rows in thisView. If thisViewhas agroup_byclause,num_view_rowswill also include aggregated rows.num_view_columns- The number of columns in thisView. If thisViewhas asplit_byclause,num_view_columnswill include all column paths, e.g. the number ofcolumnsclause times the number ofsplit_bygroups.
sourcepub async fn expression_schema(&self) -> ApiResult<JsValue>
pub async fn expression_schema(&self) -> ApiResult<JsValue>
The expression schema of this View, which contains only the expressions
created on this View. See View::schema for details.
sourcepub async fn get_config(&self) -> ApiResult<JsValue>
pub async fn get_config(&self) -> ApiResult<JsValue>
A copy of the config object passed to the Table::view method which created
this View.
sourcepub async fn get_min_max(&self, name: String) -> ApiResult<Array>
pub async fn get_min_max(&self, name: String) -> ApiResult<Array>
Calculates the [min, max] of the leaf nodes of a column column_name.
§Returns
A tuple of [min, max], whose types are column and aggregate dependent.
sourcepub async fn schema(&self) -> ApiResult<JsValue>
pub async fn schema(&self) -> ApiResult<JsValue>
The schema of this View.
The View schema differs from the schema returned by Table::schema; it
may have different column names due to expressions or columns configs, or it
maye have different column types due to the application og group_by and
aggregates config. You can think of Table::schema as the input schema
and View::schema as the output schema of a Perspective pipeline.
§JavaScript Examples
const [`View`] = await table.view({ columns: ["a", "b"] });
const schema = await view.schema(); // {a: "float", b: "string"}
sourcepub async fn to_arrow(
&self,
window: Option<JsViewWindow>,
) -> ApiResult<ArrayBuffer>
pub async fn to_arrow( &self, window: Option<JsViewWindow>, ) -> ApiResult<ArrayBuffer>
Serializes a View to the Apache Arrow data format.
sourcepub async fn to_columns_string(
&self,
window: Option<JsViewWindow>,
) -> ApiResult<String>
pub async fn to_columns_string( &self, window: Option<JsViewWindow>, ) -> ApiResult<String>
Serializes this View to a string of JSON data. Useful if you want to save
additional round trip serialize/deserialize cycles.
sourcepub async fn to_columns(
&self,
window: Option<JsViewWindow>,
) -> ApiResult<Object>
pub async fn to_columns( &self, window: Option<JsViewWindow>, ) -> ApiResult<Object>
Serializes this View to JSON data in a column-oriented format.
sourcepub async fn to_json_string(
&self,
window: Option<JsViewWindow>,
) -> ApiResult<String>
pub async fn to_json_string( &self, window: Option<JsViewWindow>, ) -> ApiResult<String>
Render this View as a JSON string.
sourcepub async fn to_json(&self, window: Option<JsViewWindow>) -> ApiResult<Array>
pub async fn to_json(&self, window: Option<JsViewWindow>) -> ApiResult<Array>
Serializes this View to JSON data in a row-oriented format.
sourcepub async fn to_csv(&self, window: Option<JsViewWindow>) -> ApiResult<String>
pub async fn to_csv(&self, window: Option<JsViewWindow>) -> ApiResult<String>
Serializes this View to CSV data in a standard format.
sourcepub async fn on_update(
&self,
on_update_js: Function,
options: Option<JsOnUpdateOptions>,
) -> ApiResult<u32>
pub async fn on_update( &self, on_update_js: Function, options: Option<JsOnUpdateOptions>, ) -> ApiResult<u32>
Register a callback with this View. Whenever the view’s underlying table
emits an update, this callback will be invoked with an object containing
port_id, indicating which port the update fired on, and optionally delta,
which is the new data that was updated for each cell or each row.
§Arguments
on_update- A callback function invoked on update, which receives an object with two keys:port_id, indicating which port the update was triggered on, anddelta, whose value is dependent on the mode parameter.options- If this is provided asOnUpdateOptions { mode: Some(OnUpdateMode::Row) }, thendeltais an Arrow of the updated rows. Otherwisedeltawill beOption::None.
§Examples
// Attach an `on_update` callback
view.on_update((updated) => console.log(updated.port_id));
// `on_update` with row deltas
view.on_update((updated) => console.log(updated.delta), { mode: "row" });
sourcepub async fn remove_update(&self, callback_id: u32) -> ApiResult<()>
pub async fn remove_update(&self, callback_id: u32) -> ApiResult<()>
Unregister a previously registered update callback with this View.
§Arguments
id- A callbackidas returned by a recipricol call toView::on_update.
§JavaScript Examples
const callback = () => console.log("Updated!");
const id = await view.on_update(callback);
await view.remove_update(id);
§Python Examples
callback = lambda x: print(x)
cid = await view.on_update(callback)
await view.remove_update(cid)
§Examples
let callback = |_| async { print!("Updated!") };
let cid = view.on_update(callback, OnUpdateOptions::default()).await?;
view.remove_update(cid).await?;sourcepub async fn num_columns(&self) -> ApiResult<u32>
pub async fn num_columns(&self) -> ApiResult<u32>
sourcepub fn remove_delete(&self, callback_id: u32) -> ApiFuture<()>
pub fn remove_delete(&self, callback_id: u32) -> ApiFuture<()>
sourcepub async fn collapse(&self, row_index: u32) -> ApiResult<u32>
pub async fn collapse(&self, row_index: u32) -> ApiResult<u32>
Collapses the row at index.
This is used during a pivot.
Trait Implementations§
source§impl FromWasmAbi for View
impl FromWasmAbi for View
source§impl IntoWasmAbi for View
impl IntoWasmAbi for View
source§impl LongRefFromWasmAbi for View
impl LongRefFromWasmAbi for View
source§impl OptionFromWasmAbi for View
impl OptionFromWasmAbi for View
source§impl OptionIntoWasmAbi for View
impl OptionIntoWasmAbi for View
source§impl RefFromWasmAbi for View
impl RefFromWasmAbi for View
source§impl RefMutFromWasmAbi for View
impl RefMutFromWasmAbi for View
source§impl TryFromJsValue for View
impl TryFromJsValue for View
source§impl VectorFromWasmAbi for View
impl VectorFromWasmAbi for View
source§impl VectorIntoWasmAbi for View
impl VectorIntoWasmAbi for View
Auto Trait Implementations§
impl Freeze for View
impl !RefUnwindSafe for View
impl Send for View
impl Sync for View
impl Unpin for View
impl !UnwindSafe for View
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moresource§impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
§type Abi = <T as IntoWasmAbi>::Abi
type Abi = <T as IntoWasmAbi>::Abi
IntoWasmAbi::Abisource§fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
IntoWasmAbi::into_abi, except that it may throw and never
return in the case of Err.