Skip to content

Commit 980b2ff

Browse files
committed
Added compulsory engine V2 filter
1 parent 8059faf commit 980b2ff

4 files changed

Lines changed: 40 additions & 3 deletions

File tree

apps/webapp/app/v3/querySchemas.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const runsSchema: TableSchema = {
3131
projectId: "project_id",
3232
environmentId: "environment_id",
3333
},
34+
requiredFilters: [{ column: "engine", value: "V2" }],
3435
columns: {
3536
run_id: {
3637
name: "run_id",

internal-packages/tsql/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import { TSQLParser } from "./grammar/TSQLParser.js";
99
import { TSQLParseTreeConverter } from "./query/parser.js";
1010
import type { SelectQuery, SelectSetQuery, Expression } from "./query/ast.js";
1111
import { SyntaxError } from "./query/errors.js";
12-
import { createSchemaRegistry, type TableSchema, type FieldMappings } from "./query/schema.js";
12+
import {
13+
createSchemaRegistry,
14+
type TableSchema,
15+
type FieldMappings,
16+
type RequiredFilter,
17+
} from "./query/schema.js";
1318
import { createPrinterContext, type QuerySettings } from "./query/printer_context.js";
1419
import { printToClickHouse, type PrintResult } from "./query/printer.js";
1520

@@ -62,6 +67,7 @@ export {
6267
type TableSchema,
6368
type ColumnSchema,
6469
type TenantColumnConfig,
70+
type RequiredFilter,
6571
type SchemaRegistry,
6672
type ClickHouseType,
6773
type OutputColumnMetadata,

internal-packages/tsql/src/query/printer.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,15 +1147,17 @@ export class ClickHousePrinter {
11471147
// ============================================================
11481148

11491149
/**
1150-
* Create a WHERE clause expression for tenant isolation
1150+
* Create a WHERE clause expression for tenant isolation and required filters
11511151
* Note: We use just the column name without table prefix since ClickHouse
11521152
* requires the actual table name (task_runs_v2), not the TSQL alias (task_runs)
11531153
*
11541154
* Organization ID is always required. Project ID and Environment ID are optional -
11551155
* if not provided, the query will return results across all projects/environments.
1156+
*
1157+
* Required filters from the table schema are also always included.
11561158
*/
11571159
private createTenantGuard(tableSchema: TableSchema, _tableAlias: string): And | CompareOperation {
1158-
const { tenantColumns } = tableSchema;
1160+
const { tenantColumns, requiredFilters } = tableSchema;
11591161

11601162
// Organization guard is always required
11611163
const orgGuard: CompareOperation = {
@@ -1190,6 +1192,19 @@ export class ClickHousePrinter {
11901192
guards.push(envGuard);
11911193
}
11921194

1195+
// Add required filters from the table schema
1196+
if (requiredFilters && requiredFilters.length > 0) {
1197+
for (const filter of requiredFilters) {
1198+
const filterGuard: CompareOperation = {
1199+
expression_type: "compare_operation",
1200+
op: CompareOperationOp.Eq,
1201+
left: { expression_type: "field", chain: [filter.column] } as Field,
1202+
right: { expression_type: "constant", value: filter.value } as Constant,
1203+
};
1204+
guards.push(filterGuard);
1205+
}
1206+
}
1207+
11931208
// If only org guard, return it directly (no need for AND wrapper)
11941209
if (guards.length === 1) {
11951210
return orgGuard;

internal-packages/tsql/src/query/schema.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,16 @@ export interface TenantColumnConfig {
209209
environmentId: string;
210210
}
211211

212+
/**
213+
* Required filter that is always applied to queries on a table
214+
*/
215+
export interface RequiredFilter {
216+
/** The ClickHouse column name to filter on */
217+
column: string;
218+
/** The value the column must equal */
219+
value: string;
220+
}
221+
212222
/**
213223
* Schema definition for a table
214224
*/
@@ -225,6 +235,11 @@ export interface TableSchema {
225235
description?: string;
226236
/** Whether this table can be joined to other tables */
227237
joinable?: boolean;
238+
/**
239+
* Required filters that are always applied to queries on this table.
240+
* These are injected into the WHERE clause automatically, similar to tenant isolation.
241+
*/
242+
requiredFilters?: RequiredFilter[];
228243
}
229244

230245
/**

0 commit comments

Comments
 (0)