Skip to content

Commit

Permalink
Refactor: Distinguish between FieldPredicate and SelectionFilter
Browse files Browse the repository at this point in the history
(FieldPredicate can be useful in other scenario besides filter -- e.g., for condition in #3239 or for selecting data for annotation (@starry97's project)
  • Loading branch information
kanitw committed Jan 17, 2018
1 parent b4b178a commit 567910e
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 160 deletions.
240 changes: 120 additions & 120 deletions build/vega-lite-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1641,41 +1641,6 @@
},
"type": "object"
},
"EqualFilter": {
"additionalProperties": false,
"properties": {
"equal": {
"anyOf": [
{
"type": "string"
},
{
"type": "number"
},
{
"type": "boolean"
},
{
"$ref": "#/definitions/DateTime"
}
],
"description": "The value that the field should be equal to."
},
"field": {
"description": "Field to be filtered.",
"type": "string"
},
"timeUnit": {
"$ref": "#/definitions/TimeUnit",
"description": "Time unit for the field to be filtered."
}
},
"required": [
"field",
"equal"
],
"type": "object"
},
"FacetFieldDef": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -1992,16 +1957,132 @@
],
"type": "object"
},
"FieldEqualPredicate": {
"additionalProperties": false,
"properties": {
"equal": {
"anyOf": [
{
"type": "string"
},
{
"type": "number"
},
{
"type": "boolean"
},
{
"$ref": "#/definitions/DateTime"
}
],
"description": "The value that the field should be equal to."
},
"field": {
"description": "Field to be filtered.",
"type": "string"
},
"timeUnit": {
"$ref": "#/definitions/TimeUnit",
"description": "Time unit for the field to be filtered."
}
},
"required": [
"field",
"equal"
],
"type": "object"
},
"FieldOneOfPredicate": {
"additionalProperties": false,
"properties": {
"field": {
"description": "Field to be filtered",
"type": "string"
},
"oneOf": {
"anyOf": [
{
"items": {
"type": "string"
},
"type": "array"
},
{
"items": {
"type": "number"
},
"type": "array"
},
{
"items": {
"type": "boolean"
},
"type": "array"
},
{
"items": {
"$ref": "#/definitions/DateTime"
},
"type": "array"
}
],
"description": "A set of values that the `field`'s value should be a member of,\nfor a data item included in the filtered data."
},
"timeUnit": {
"$ref": "#/definitions/TimeUnit",
"description": "time unit for the field to be filtered."
}
},
"required": [
"field",
"oneOf"
],
"type": "object"
},
"FieldRangePredicate": {
"additionalProperties": false,
"properties": {
"field": {
"description": "Field to be filtered",
"type": "string"
},
"range": {
"description": "An array of inclusive minimum and maximum values\nfor a field value of a data item to be included in the filtered data.",
"items": {
"anyOf": [
{
"type": "number"
},
{
"$ref": "#/definitions/DateTime"
}
]
},
"maxItems": 2,
"minItems": 2,
"type": "array"
},
"timeUnit": {
"$ref": "#/definitions/TimeUnit",
"description": "time unit for the field to be filtered."
}
},
"required": [
"field",
"range"
],
"type": "object"
},
"Filter": {
"anyOf": [
{
"$ref": "#/definitions/EqualFilter"
"$ref": "#/definitions/FieldEqualPredicate"
},
{
"$ref": "#/definitions/RangeFilter"
"$ref": "#/definitions/FieldRangePredicate"
},
{
"$ref": "#/definitions/OneOfFilter"
"$ref": "#/definitions/FieldOneOfPredicate"
},
{
"$ref": "#/definitions/SelectionFilter"
Expand All @@ -2016,7 +2097,7 @@
"properties": {
"filter": {
"$ref": "#/definitions/FilterOperand",
"description": "The `filter` property must be either (1) a filter object for [equal-filters](filter.html#equalfilter),\n[range-filters](filter.html#rangefilter), [one-of filters](filter.html#oneoffilter), or [selection filters](filter.html#selectionfilter);\n(2) a [Vega Expression](filter.html#expression) string,\nwhere `datum` can be used to refer to the current data object; or (3) an array of filters (either objects or expression strings) that must all be true for a datum to pass the filter and be included."
"description": "The `filter` property must be either (1) one of the field predicates: [equal predicate](filter.html#equalfilter),\n[range precidate](filter.html#rangefilter), [one-of predicate](filter.html#oneoffilter);;\n(2) a [selection filter definition](filter.html#selectionfilter)\n(3) a [Vega Expression](filter.html#expression) string,\nwhere `datum` can be used to refer to the current data object; or (3) an array of filters (either objects or expression strings) that must all be true for a datum to pass the filter and be included."
}
},
"required": [
Expand Down Expand Up @@ -3788,53 +3869,6 @@
],
"type": "string"
},
"OneOfFilter": {
"additionalProperties": false,
"properties": {
"field": {
"description": "Field to be filtered",
"type": "string"
},
"oneOf": {
"anyOf": [
{
"items": {
"type": "string"
},
"type": "array"
},
{
"items": {
"type": "number"
},
"type": "array"
},
{
"items": {
"type": "boolean"
},
"type": "array"
},
{
"items": {
"$ref": "#/definitions/DateTime"
},
"type": "array"
}
],
"description": "A set of values that the `field`'s value should be a member of,\nfor a data item included in the filtered data."
},
"timeUnit": {
"$ref": "#/definitions/TimeUnit",
"description": "time unit for the field to be filtered."
}
},
"required": [
"field",
"oneOf"
],
"type": "object"
},
"OrderFieldDef": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -4111,40 +4145,6 @@
}
]
},
"RangeFilter": {
"additionalProperties": false,
"properties": {
"field": {
"description": "Field to be filtered",
"type": "string"
},
"range": {
"description": "An array of inclusive minimum and maximum values\nfor a field value of a data item to be included in the filtered data.",
"items": {
"anyOf": [
{
"type": "number"
},
{
"$ref": "#/definitions/DateTime"
}
]
},
"maxItems": 2,
"minItems": 2,
"type": "array"
},
"timeUnit": {
"$ref": "#/definitions/TimeUnit",
"description": "time unit for the field to be filtered."
}
},
"required": [
"field",
"range"
],
"type": "object"
},
"Repeat": {
"additionalProperties": false,
"properties": {
Expand Down
11 changes: 6 additions & 5 deletions site/docs/transform/filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ Vega-Lite filter transforms must have the `filter` property.
For a [Vega Expression](https://vega.github.io/vega/docs/expressions/) string, each datum object can be referred using bound variable `datum`. For example, setting `filter` to `"datum.b2 > 60"` would make the output data includes only items that have values in the field `b2` over 60.


## Filter Object
## Field Predicate Object


For a filter object, either a `field` or [`selection` name](#selectionfilter) must be provided. The former takes one of the filter operators ([`equal`](#equalfilter), [`range`](#rangefilter), or [`oneOf`](#oneofilter)). Values of these operators can be primitive types (string, number, boolean) or a [DateTime definition object](types.html#datetime) to describe time. In addition, `timeUnit` can be provided to further transform a temporal `field`.
For a filter object, either a `field` must be provided along with one of the predicate properties: ([`equal`](#equalfilter), [`range`](#rangefilter), or [`oneOf`](#oneofilter)). Values of these operators can be primitive types (string, number, boolean) or a [DateTime definition object](types.html#datetime) to describe time. In addition, `timeUnit` can be provided to further transform a temporal `field`.

{:#equalfilter}
### Equal Filter
### Equal Predicate

{% include table.html props="field,equal,timeUnit" source="EqualFilter" %}

Expand Down Expand Up @@ -68,7 +67,9 @@ For example, to check if the `car_color` field's value is equal to `"red"`, we c
For example, `{"filter": {"field": "car_color", "oneOf":["red", "yellow"]}}` checks if the `car_color` field's value is `"red"` or `"yellow"`.

{:#selectionfilter}
### Selection Filter
## Selection Filter

For a selection filter object, a [`selection` name](#selectionfilter) must be provided.

{% include table.html props="selection" source="SelectionFilter" %}

Expand Down
4 changes: 2 additions & 2 deletions src/compile/data/formatparse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {isCountingAggregateOp} from '../../aggregate';
import {isNumberFieldDef, isTimeFieldDef} from '../../fielddef';
import {isEqualFilter, isOneOfFilter, isRangeFilter} from '../../filter';
import {isFieldEqualPredicate, isFieldOneOfPredicate, isFieldPredicate, isFieldRangePredicate} from '../../filter';
import * as log from '../../log';
import {forEachLeave} from '../../logical';
import {isCalculate, isFilter, Transform} from '../../transform';
Expand Down Expand Up @@ -54,7 +54,7 @@ export class ParseNode extends DataFlowNode {
calcFieldMap[transform.as] = true;
} else if (isFilter(transform)) {
forEachLeave(transform.filter, (filter) => {
if (isEqualFilter(filter) || isRangeFilter(filter) || isOneOfFilter(filter)) {
if (isFieldPredicate(filter)) {
if (filter.timeUnit) {
parse[filter.field] = 'date';
}
Expand Down
8 changes: 4 additions & 4 deletions src/compile/data/parse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {isNumber, isString} from 'vega-util';
import {MAIN, RAW} from '../../data';
import {DateTime, isDateTime} from '../../datetime';
import {isEqualFilter, isOneOfFilter, isRangeFilter} from '../../filter';
import {isFieldEqualPredicate, isFieldOneOfPredicate, isFieldRangePredicate} from '../../filter';
import * as log from '../../log';
import {isAggregate, isBin, isCalculate, isFilter, isLookup, isTimeUnit} from '../../transform';
import {Dict, keys} from '../../util';
Expand Down Expand Up @@ -76,11 +76,11 @@ export function parseTransformArray(model: Model) {
// For EqualFilter, just use the equal property.
// For RangeFilter and OneOfFilter, all array members should have
// the same type, so we only use the first one.
if (isEqualFilter(filter)) {
if (isFieldEqualPredicate(filter)) {
val = filter.equal;
} else if (isRangeFilter(filter)) {
} else if (isFieldRangePredicate(filter)) {
val = filter.range[0];
} else if (isOneOfFilter(filter)) {
} else if (isFieldOneOfPredicate(filter)) {
val = (filter.oneOf || filter['in'])[0];
} // else -- for filter expression, we can't infer anything

Expand Down
Loading

0 comments on commit 567910e

Please sign in to comment.