Skip to content

Commit 38e07d1

Browse files
authored
Add docs about Table and List type representations. (#77)
1 parent ef7cd93 commit 38e07d1

File tree

3 files changed

+154
-208
lines changed

3 files changed

+154
-208
lines changed

docs/docs/core/basics.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ An indexing flow involves source data and transformed data (either as an interme
2222
Each piece of data has a **data type**, falling into one of the following categories:
2323

2424
* Basic type.
25-
* Composite type
26-
* Struct: a collection of **fields**, each with a name and a type.
27-
* Table: a collection of **rows**, each of which is a struct with specified schema.
25+
* Struct type: a collection of **fields**, each with a name and a type.
26+
* Collection type: a collection of **rows**, each of which is a struct with specified schema. A collection type can be a table (which has a key field) or a list (ordered but without key field).
2827

2928
An indexing flow always has a top-level struct, containing all data within and managed by the flow.
3029

docs/docs/core/data_types.mdx

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,50 @@ This is the list of all basic types supported by CocoIndex:
1717

1818
| Type | Type in Python | Original Type in Python |
1919
|------|-----------------|--------------------------|
20-
| `bytes` | `bytes` | `bytes` |
21-
| `str` | `str` | `str` |
22-
| `bool` | `bool` | `bool` |
23-
| `int64` | `int` | `int` |
24-
| `float32` | `cocoindex.typing.Float32` |`float` |
25-
| `float64` | `cocoindex.typing.Float64` |`float` |
26-
| `range` | `cocoindex.typing.Range` | `tuple[int, int]` |
27-
| `vector(*type* [, *N*])` |`Annotated[list[type], cocoindex.typing.Vector(dim=N)]` | `list[type]` |
28-
| `json` | `cocoindex.typing.Json` | Any type convertible to JSON by `json` package |
20+
| bytes | `bytes` | `bytes` |
21+
| str | `str` | `str` |
22+
| bool | `bool` | `bool` |
23+
| int64 | `int` | `int` |
24+
| float32 | `cocoindex.typing.Float32` |`float` |
25+
| float64 | `cocoindex.typing.Float64` |`float` |
26+
| range | `cocoindex.typing.Range` | `tuple[int, int]` |
27+
| vector[*type*, *N*?] |`Annotated[list[type], cocoindex.typing.Vector(dim=N)]` | `list[type]` |
28+
| json | `cocoindex.typing.Json` | Any type convertible to JSON by `json` package |
2929

3030
For some types, CocoIndex Python SDK provides annotated types with finer granularity than Python's original type, e.g. `Float32` and `Float64` for `float`, and `vector` has dimension information.
3131

3232
When defining [custom functions](/docs/core/custom_function), use the specific types as type annotations for arguments and return values.
3333
So CocoIndex will have information about the specific type.
3434

35-
### Struct
35+
### Struct Type
3636

3737
A struct has a bunch of fields, each with a name and a type.
3838

39-
### Table
39+
In Python, a struct type is represented by a [dataclass](https://docs.python.org/3/library/dataclasses.html),
40+
and all fields must be annotated with a specific type. For example:
4041

41-
A table has a collection of rows, each of which is a struct with specified schema.
42+
```python
43+
from dataclasses import dataclass
4244

43-
The first field of a table is always the primary key.
45+
@dataclass
46+
class Order:
47+
order_id: str
48+
name: str
49+
price: float
50+
```
4451

45-
:::note
52+
### Collection Types
4653

47-
CocoIndex will support functions taking struct and table types as arguments or returning composite types soon.
48-
We'll update this section with corresponding Python types by then.
54+
A collection type models a collection of rows, each of which is a struct with specific schema.
4955

50-
:::
56+
We have two specific types of collection:
57+
58+
| Type | Description |Type in Python | Original Type in Python |
59+
|------|-------------|---------------|-------------------------|
60+
| Table[*type*] | The first field is the key, and CocoIndex enforces its uniqueness | `cocoindex.typing.Table[type]` | `list[type]` |
61+
| List[*type*] | No key field; row order is preserved | `cocoindex.typing.List[type]` | `list[type]` |
62+
63+
For example, we can use `cocoindex.typing.Table[Order]` to represent a table of orders, and the first field `order_id` will be taken as the key field.
5164

5265
## Types to Create Indexes
5366

0 commit comments

Comments
 (0)