diff --git a/docs/web/curation/index.mdx b/docs/web/curation/index.mdx index 59b993100..290eb19fe 100644 --- a/docs/web/curation/index.mdx +++ b/docs/web/curation/index.mdx @@ -24,6 +24,7 @@ records belong, and save the selected data or the rules used to produce it. | [Scope to what changed](/curation/changes) | How to process only the rows added since your last run. | | [Profile a dataset](/curation/aggregate) | Whether coverage, missing values and class balance suit the task. | | [Statistical Sampling](/curation/statistical-sampling) | Which random fraction to inspect or use for an experiment. | +| [Semantic Sampling](/curation/semantic-sampling) | How to draw rows from every cluster in an existing IVF vector index. | | [Selective Sampling](/curation/selective-sampling) | How to re-fetch specific rows by ID, address or offset. | | [Join datasets](/curation/join) | How to attach labels, metadata or scores stored elsewhere. | | [Rank and deduplicate](/curation/windowing) | Which records to keep when several share an explicit key. | diff --git a/docs/web/curation/semantic-sampling.mdx b/docs/web/curation/semantic-sampling.mdx new file mode 100644 index 000000000..f51f68d6b --- /dev/null +++ b/docs/web/curation/semantic-sampling.mdx @@ -0,0 +1,121 @@ +--- +title: "Semantic Sampling" +sidebarTitle: "Semantic Sampling" +description: "Sample rows from every cluster in an existing IVF vector index with SQL TABLESAMPLE IVF." +icon: circle-nodes +keywords: ["semantic sampling", "sampling", "tablesample", "ivf", "clusters", "embeddings", "repeatable"] +availability: + embedded: unavailable + enterprise: available +--- + +Semantic sampling draws rows from every partition of an existing IVF vector +index. Each partition acts as a cluster of similar embeddings. Use it to +inspect examples across your embedding space, build a dataset with broader +topic coverage, or try an expensive operation on examples from each cluster. +The clusters reflect your embeddings and index; they do not guarantee +coverage of every label or topic. + +## Sample rows from every cluster {#sample} + +Connect with an [Enterprise SQL client](/basics/sql). The examples assume a +`documents` table with `id`, `caption`, and `embedding` columns. The +`embedding` column must have an up-to-date, single-segment +[IVF vector index](/indexing/vector-index) that includes every live row. +Wait for the index build to finish before sampling. + + +```sql SQL icon="database" +SELECT id, caption +FROM documents +TABLESAMPLE IVF (embedding, 100 ROWS) +REPEATABLE (42); +``` + + +This selects up to **100 rows from each IVF partition**, without replacement. +A partition with fewer than 100 rows contributes all its rows; empty +partitions contribute none. The result contains the table's existing columns, +so you can project the fields you need without returning `embedding`. + +The count is per partition, not a limit on the whole result. For example: + +| Rows in a partition | Rows selected with `100 ROWS` | +| --- | --- | +| 5 | 5 | +| 100 | 100 | +| 1,000 | 100 | + +With 1,024 populated partitions, the query returns at most 102,400 rows. +Choose the quota with the number of partitions and your output budget in mind. + +Equal quotas give sparse clusters more weight relative to their size. +This makes semantic sampling useful for cluster coverage, but the resulting +sample does not preserve the table's overall distribution. Use +[Statistical Sampling](/curation/statistical-sampling) for uniform row +sampling or account for the different sampling probabilities when estimating +whole-table statistics. + +To group records by partition or select a subset of whole partitions, use +[`ivf_partition()`](/sql/dql/ivf-partition) instead. Its `count` argument +selects the number of partitions; `TABLESAMPLE IVF` selects rows within +every partition. + +## Repeat a sample {#repeatable} + +`REPEATABLE (42)` fixes the seed. With the same table snapshot, index, and +seed, repeated queries select the same rows. Output order is unspecified. +Omit `REPEATABLE` to draw a new sample each time. + +Changes to the source data or rebuilding the index can change the selected +rows even with the same seed. Save a sample when you need a fixed dataset +for evaluation or comparison: + + +```sql SQL icon="database" +CREATE TABLE semantic_eval AS +SELECT id, caption, embedding +FROM documents +TABLESAMPLE IVF (embedding, 100 ROWS) +REPEATABLE (42); +``` + + +Create the output once with a fresh name and keep it unchanged. Record the +source version and sampling policy alongside the result. + +## Filter sampled rows {#filters} + +Sampling happens before `WHERE`, joins, aggregation, and an outer `LIMIT`. +For example, this draws up to 100 rows per partition and then removes rows +with missing captions: + + +```sql SQL icon="database" +SELECT id, caption +FROM documents +TABLESAMPLE IVF (embedding, 100 ROWS) +REPEATABLE (42) +WHERE caption IS NOT NULL; +``` + + +The result can contain fewer than 100 rows from a partition after filtering. +If you need a quota from an eligible subset, first save that subset as a +base table and build its IVF index before sampling it. + +## Requirements and limits {#requirements} + +| Requirement | Behavior | +| --- | --- | +| Source | Sample a base table directly. Views, derived relations, WAL-backed tables, and `CHANGES` relations are unsupported. | +| Vector column | Name the indexed vector column directly, rather than an expression. | +| Index | Exactly one eligible IVF index segment must cover every current fragment and assign every live row exactly once. Missing, ambiguous, or stale indexes are rejected. | +| New or unindexed rows | Rebuild the index so all live rows are covered by a single segment. Null vectors omitted by the index also prevent complete coverage. | +| Deleted rows | Tables with deletion vectors are rejected. Compact deleted rows and rebuild the IVF index before sampling. | +| Row count and seed | Use nonnegative integer literals. A count of zero returns no rows. This mode accepts row counts, not percentages. | + +Sampling examines the membership of every partition and fetches projected +data for the selected rows. A small quota limits the result size, but does +not limit index reads to that many entries. Large partitions can still +require substantial index reads and memory. diff --git a/docs/web/curation/statistical-sampling.mdx b/docs/web/curation/statistical-sampling.mdx index fc43bff12..2d37cefe1 100644 --- a/docs/web/curation/statistical-sampling.mdx +++ b/docs/web/curation/statistical-sampling.mdx @@ -16,6 +16,8 @@ statistics without reading every row. For a random subset through the Python SDK instead -- in Embedded or Enterprise -- see [Permutation](/training/permutation). To select specific, known rows rather than a random fraction, see [Selective Sampling](/curation/selective-sampling). +To draw rows from every cluster of an existing vector index, use +[Semantic Sampling](/curation/semantic-sampling). ## Example data {#example-data} diff --git a/docs/web/docs.json b/docs/web/docs.json index 4c800d65a..b561346cd 100644 --- a/docs/web/docs.json +++ b/docs/web/docs.json @@ -450,6 +450,7 @@ "curation/create-function", "curation/chunking", "curation/statistical-sampling", + "curation/semantic-sampling", "curation/selective-sampling", "curation/aggregate", "curation/join", diff --git a/docs/web/sql/dql/sampling.mdx b/docs/web/sql/dql/sampling.mdx index b6b24f8eb..c84caccc1 100644 --- a/docs/web/sql/dql/sampling.mdx +++ b/docs/web/sql/dql/sampling.mdx @@ -22,6 +22,9 @@ SELECT * FROM documents TABLESAMPLE (500 ROWS) -- 500 rows A bare number is a **percentage**. Add `ROWS` for a count instead. +To sample rows independently from every partition of an IVF vector index, +use [`TABLESAMPLE IVF`](/curation/semantic-sampling). + ## Rows or fragments {#methods} The standard calls these two methods of one clause. They are different