implement OR logic to filters, segments, cohorts

This commit is contained in:
Francis Cao committed 2026-02-21 20:00:43 -08:00
1 parent 79c06787cd
commit cc1e4438d0
64 files changed
+343 -54

No files matched your search

+37 -22
View File
@@ -99,32 +99,44 @@ function mapFilter(
}
function getFilterQuery(filters: Record<string, any>, options: QueryOptions = {}) {
const query = filtersObjectToArray(filters, options).reduce(
(arr, { name, column, operator, paramName }) => {
const isCohort = options?.isCohort;
const { isCohort, cohortMatch, cohortActionName } = options;
const isOr = isCohort ? cohortMatch === 'any' : filters.match === 'any';
const orClauses: string[] = [];
const andClauses: string[] = [];
if (isCohort) {
column = FILTER_COLUMNS[name.slice('cohort_'.length)];
filtersObjectToArray(filters, options).forEach(({ name, column, operator, paramName }) => {
if (isCohort) {
column = FILTER_COLUMNS[name.slice('cohort_'.length)];
}
if (column) {
const isAlwaysAnd = name === 'eventType' || (isCohort && name === cohortActionName);
if (isAlwaysAnd) {
andClauses.push(
`and ${mapFilter(column, operator, name, name === 'eventType' ? 'UInt32' : 'String', paramName)}`,
);
} else if (isOr) {
orClauses.push(mapFilter(column, operator, name, 'String', paramName));
} else {
andClauses.push(`and ${mapFilter(column, operator, name, 'String', paramName)}`);
}
if (column) {
if (name === 'eventType') {
arr.push(`and ${mapFilter(column, operator, name, 'UInt32', paramName)}`);
} else {
arr.push(`and ${mapFilter(column, operator, name, 'String', paramName)}`);
}
if (name === 'referrer') {
arr.push(`and referrer_domain != hostname`);
}
if (name === 'referrer') {
andClauses.push(`and referrer_domain != hostname`);
}
}
});
return arr;
},
[],
);
const parts: string[] = [];
return query.join('\n');
if (orClauses.length > 0) {
parts.push(`and (\n ${orClauses.join('\n or ')}\n)`);
}
parts.push(...andClauses);
return parts.join('\n');
}
function getCohortQuery(filters: Record<string, any>) {
@@ -132,7 +144,10 @@ function getCohortQuery(filters: Record<string, any>) {
return '';
}
const filterQuery = getFilterQuery(filters, { isCohort: true });
const cohortMatch = filters.cohort_match;
const cohortActionName = filters.cohort_actionName;
const filterQuery = getFilterQuery(filters, { isCohort: true, cohortMatch, cohortActionName });
return `join (
select distinct session_id
@@ -267,7 +282,7 @@ async function rawQuery<T = unknown>(
output_format_json_quote_64bit_integers: 0,
},
});
console.log(query, params);
return (await resultSet.json()) as T;
}
+30 -10
View File
@@ -106,30 +106,47 @@ function mapFilter(
}
function getFilterQuery(filters: Record<string, any>, options: QueryOptions = {}): string {
const query = filtersObjectToArray(filters, options).reduce(
(arr, { name, column, operator, prefix = '', paramName }) => {
const isCohort = options?.isCohort;
const { isCohort, cohortMatch, cohortActionName } = options;
const isOr = isCohort ? cohortMatch === 'any' : filters.match === 'any';
const orClauses: string[] = [];
const andClauses: string[] = [];
filtersObjectToArray(filters, options).forEach(
({ name, column, operator, prefix = '', paramName }) => {
if (isCohort) {
column = FILTER_COLUMNS[name.slice('cohort_'.length)];
}
if (column) {
arr.push(`and ${mapFilter(`${prefix}${column}`, operator, name, '', paramName)}`);
const clause = mapFilter(`${prefix}${column}`, operator, name, '', paramName);
const isAlwaysAnd = name === 'eventType' || (isCohort && name === cohortActionName);
if (isAlwaysAnd) {
andClauses.push(`and ${clause}`);
} else if (isOr) {
orClauses.push(clause);
} else {
andClauses.push(`and ${clause}`);
}
if (name === 'referrer') {
arr.push(
andClauses.push(
`and (website_event.referrer_domain != regexp_replace(website_event.hostname, '^www.', '') or website_event.referrer_domain is null)`,
);
}
}
return arr;
},
[],
);
return query.join('\n');
const parts: string[] = [];
if (orClauses.length > 0) {
parts.push(`and (\n ${orClauses.join('\n or ')}\n)`);
}
parts.push(...andClauses);
return parts.join('\n');
}
function getCohortQuery(filters: QueryFilters = {}) {
@@ -137,7 +154,10 @@ function getCohortQuery(filters: QueryFilters = {}) {
return '';
}
const filterQuery = getFilterQuery(filters, { isCohort: true });
const cohortMatch = (filters as any).cohort_match;
const cohortActionName = (filters as any).cohort_actionName;
const filterQuery = getFilterQuery(filters, { isCohort: true, cohortMatch, cohortActionName });
return `join
(select distinct website_event.session_id
+11
View File
@@ -115,6 +115,8 @@ export async function getQueryFilters(
const dateRange = getRequestDateRange(params);
const filters = getRequestFilters(params);
let match = params?.match;
if (websiteId) {
await setWebsiteDate(websiteId, dateRange);
@@ -123,6 +125,10 @@ export async function getQueryFilters(
?.parameters as Record<string, any>;
Object.assign(filters, filtersArrayToObject(segmentParams.filters));
if (segmentParams.match) {
match = segmentParams.match;
}
}
if (params.cohort) {
@@ -146,6 +152,10 @@ export async function getQueryFilters(
...filtersArrayToObject(cohortFilters),
cohort_startDate: startDate,
cohort_endDate: endDate,
...(cohortParams.match && {
cohort_match: cohortParams.match,
cohort_actionName: `cohort_${cohortParams.action.type}`,
}),
});
}
@@ -157,6 +167,7 @@ export async function getQueryFilters(
return {
...dateRange,
...filters,
match,
page: params?.page,
pageSize: params?.pageSize ? params?.pageSize || DEFAULT_PAGE_SIZE : undefined,
orderBy: params?.orderBy,
+2
View File
@@ -67,6 +67,7 @@ export const filterParams = {
cohort: z.uuid().optional(),
eventType: z.coerce.number().int().positive().optional(),
excludeBounce: z.string().optional(),
match: z.enum(['all', 'any']).optional(),
};
export const searchParams = {
@@ -295,6 +296,7 @@ export const segmentParamSchema = z.object({
}),
)
.optional(),
match: z.enum(['all', 'any']).optional(),
dateRange: z.string().optional(),
action: z
.object({
+3
View File
@@ -53,6 +53,8 @@ export interface QueryOptions {
limit?: number;
prefix?: string;
isCohort?: boolean;
cohortMatch?: string;
cohortActionName?: string;
}
export interface QueryFilters
@@ -93,6 +95,7 @@ export interface FilterParams {
cohort?: string;
compare?: string;
excludeBounce?: boolean;
match?: 'all' | 'any';
}
export interface SortParams {