Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slick-ghosts-shine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tanstack/db": patch
---

New distinct operator for queries.
2 changes: 1 addition & 1 deletion packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A reactive client store for building super fast apps on sync",
"version": "0.0.21",
"dependencies": {
"@electric-sql/d2mini": "^0.1.6",
"@electric-sql/d2mini": "^0.1.7",
"@standard-schema/spec": "^1.0.0"
},
"devDependencies": {
Expand Down
21 changes: 21 additions & 0 deletions packages/db/src/query/builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,27 @@ export class BaseQueryBuilder<TContext extends Context = Context> {
}) as any
}

/**
* Specify that the query should return distinct rows.
* Deduplicates rows based on the selected columns.
* @returns A QueryBuilder with distinct enabled
*
* @example
* ```ts
* // Get countries our users are from
* query
* .from({ users: usersCollection })
* .select(({users}) => users.country)
* .distinct()
* ```
*/
distinct(): QueryBuilder<TContext> {
return new BaseQueryBuilder({
...this.query,
distinct: true,
}) as any
}

// Helper methods
private _getCurrentAliases(): Array<string> {
const aliases: Array<string> = []
Expand Down
13 changes: 11 additions & 2 deletions packages/db/src/query/compiler/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filter, map } from "@electric-sql/d2mini"
import { distinct, filter, map } from "@electric-sql/d2mini"
import { compileExpression } from "./evaluators.js"
import { processJoins } from "./joins.js"
import { processGroupBy } from "./group-by.js"
Expand Down Expand Up @@ -98,8 +98,12 @@ export function compileQuery(
}
}

if (query.distinct && !query.fnSelect && !query.select) {
throw new Error(`DISTINCT requires a SELECT clause.`)
}

// Process the SELECT clause early - always create __select_results
// This eliminates duplication and allows for future DISTINCT implementation
// This eliminates duplication and allows for DISTINCT implementation
if (query.fnSelect) {
// Handle functional select - apply the function to transform the row
pipeline = pipeline.pipe(
Expand Down Expand Up @@ -190,6 +194,11 @@ export function compileQuery(
}
}

// Process the DISTINCT clause if it exists
if (query.distinct) {
pipeline = pipeline.pipe(distinct(([_key, row]) => row.__select_results))
}

// Process orderBy parameter if it exists
if (query.orderBy && query.orderBy.length > 0) {
const orderedPipeline = processOrderBy(
Expand Down
1 change: 1 addition & 0 deletions packages/db/src/query/ir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface QueryIR {
orderBy?: OrderBy
limit?: Limit
offset?: Offset
distinct?: true

// Functional variants
fnSelect?: (row: NamespacedRow) => any
Expand Down
Loading