Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

refactor(api): drop the unreachable kv partition key plan

Hampusc0b8dae2f877
diff --git a/fluxer_api/src/api/database/CassandraTableDsl.ts b/fluxer_api/src/api/database/CassandraTableDsl.ts
index d2081c830..8c573ca2c 100644
--- a/fluxer_api/src/api/database/CassandraTableDsl.ts
+++ b/fluxer_api/src/api/database/CassandraTableDsl.ts
@@ -84,6 +84,9 @@ export function defineTable<Row extends object, PK extends ColumnName<Row>, Part
for (const c of columns) assertCqlIdentifier(c as string);
for (const k of pk) assertCqlIdentifier(k as string);
for (const k of partitionKey) assertCqlIdentifier(k as string);
+ if (partitionKey.some((k, index) => (pk[index] as string) !== k)) {
+ throw new Error(`Table "${def.name}" partitionKey must be a prefix of its primaryKey`);
+ }
if (def.defaultTtlSeconds !== undefined && validateTtlSeconds(def.defaultTtlSeconds) === 0) {
throw new Error(`Table "${def.name}" needs a positive default TTL`);
}
diff --git a/fluxer_api/src/api/database/PostgresKvExactnessFuzz.test.ts b/fluxer_api/src/api/database/PostgresKvExactnessFuzz.test.ts
index 4faa35eaf..acdc0dd49 100644
--- a/fluxer_api/src/api/database/PostgresKvExactnessFuzz.test.ts
+++ b/fluxer_api/src/api/database/PostgresKvExactnessFuzz.test.ts
@@ -2,7 +2,6 @@
import type {CassandraParams, KvQueryMeta, KvTableSpec, WhereExpr} from '@app/api/database/CassandraTypes';
import {buildCandidatePlan, keyFromColumns, matchesWhere} from '@app/api/database/PostgresKvQueryExecutor';
-import cassandra from 'cassandra-driver';
import {describe, expect, it} from 'vitest';
type Row = Record<string, unknown>;
@@ -70,8 +69,6 @@ function rowKeyMatchesPlan(plan: ReturnType<typeof buildCandidatePlan>, rowKey:
Buffer.compare(buf, Buffer.from(group.upperBounds[i]!, 'utf8')) < 0,
),
);
- case 'partitionKeys':
- return c.partitionKeys.includes(rowKey);
}
}
diff --git a/fluxer_api/src/api/database/PostgresKvQueryPlan.test.ts b/fluxer_api/src/api/database/PostgresKvQueryPlan.test.ts
index 4baf3c4b2..a933d7329 100644
--- a/fluxer_api/src/api/database/PostgresKvQueryPlan.test.ts
+++ b/fluxer_api/src/api/database/PostgresKvQueryPlan.test.ts
@@ -51,7 +51,7 @@ function rangeBounds(plan: CandidatePlan): Array<[string, string]> {
return [];
}
-function planAccepts(plan: CandidatePlan, rowKey: string, partitionKey: string): boolean {
+function planAccepts(plan: CandidatePlan, rowKey: string): boolean {
switch (plan.kind) {
case 'none':
return false;
@@ -62,8 +62,6 @@ function planAccepts(plan: CandidatePlan, rowKey: string, partitionKey: string):
case 'ranges':
case 'rangeGroups':
return rangeBounds(plan).some(([lower, upper]) => withinRange(rowKey, lower, upper));
- case 'partitionKeys':
- return plan.partitionKeys.includes(partitionKey);
case 'scan':
return true;
}
@@ -96,14 +94,13 @@ function planOf(query: PreparedQuery) {
function checkQuery(query: PreparedQuery, rows: Array<Row>): CandidatePlan {
const {meta, plan} = planOf(query);
const primaryKey = meta.table.primaryKey as ReadonlyArray<string>;
- const partitionKey = meta.table.partitionKey as ReadonlyArray<string>;
const accepted: Array<Row> = [];
const matched: Array<Row> = [];
for (const row of rows) {
const rowKey = keyFromColumns(primaryKey, row);
expectKeyShape(rowKey, primaryKey.length - 1);
expect(matchingRangeCount(plan.candidates, rowKey)).toBeLessThanOrEqual(1);
- if (planAccepts(plan.candidates, rowKey, keyFromColumns(partitionKey, row))) accepted.push(row);
+ if (planAccepts(plan.candidates, rowKey)) accepted.push(row);
if (matchesWhere(row, meta.where, query.params)) matched.push(row);
}
for (const row of matched) {
@@ -189,21 +186,6 @@ const wideRows: Array<Row> = ['a', 'a"b', '\u{1f600}'].flatMap((a) =>
),
);
-interface PartitionedRow {
- a: string;
- b: string;
- payload: string;
-}
-
-const PartitionedKey = defineTable<PartitionedRow, 'a' | 'b', 'b'>({
- name: 'kv_plan_partitioned',
- columns: ['a', 'b', 'payload'],
- primaryKey: ['a', 'b'],
- partitionKey: ['b'],
-});
-
-const partitionedRows: Array<Row> = TRICKY_STRINGS.flatMap((a) => TRICKY_STRINGS.map((b) => ({a, b, payload: 'p'})));
-
describe('PostgresKvQueryExecutor candidate planner', () => {
it('pins a single-column primary key by equality', () => {
for (const a of TRICKY_STRINGS) {
@@ -287,20 +269,6 @@ describe('PostgresKvQueryExecutor candidate planner', () => {
expect(checkQuery(query, wideRows).kind).toBe('range');
});
- it('uses the partition key when it is not a prefix of the primary key', () => {
- for (const b of TRICKY_STRINGS) {
- const query = PartitionedKey.select({where: PartitionedKey.where.eq('b')}).bind({b});
- expect(checkQuery(query, partitionedRows).kind).toBe('partitionKeys');
- }
- });
-
- it('uses the partition key for an IN on the partition column', () => {
- const query = PartitionedKey.select({where: PartitionedKey.where.in('b', 'bs')}).bind({
- bs: new Set(['a', 'a\u0001b', 'a\\b']),
- });
- expect(checkQuery(query, partitionedRows).kind).toBe('partitionKeys');
- });
-
it('scans a table with no predicate', () => {
expect(checkQuery(SingleKey.select().bind({}), singleRows).kind).toBe('scan');
});
@@ -310,7 +278,6 @@ describe('PostgresKvQueryExecutor plan exactness', () => {
it('is exact when the key predicate consumes every clause', () => {
expect(planOf(PairKey.select({where: PairKey.where.eq('a')}).bind({a: 'a'})).plan.exact).toBe(true);
expect(planOf(SingleKey.select().bind({})).plan.exact).toBe(true);
- expect(planOf(PartitionedKey.select({where: PartitionedKey.where.eq('b')}).bind({b: 'a'})).plan.exact).toBe(true);
});
it('is inexact when a clause is left for the JavaScript filter', () => {