Skip to content

feat(filters): added support for set and not set in where clause #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 28, 2024
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
2 changes: 1 addition & 1 deletion meerkat-browser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devrev/meerkat-browser",
"version": "0.0.71",
"version": "0.0.72",
"dependencies": {
"@swc/helpers": "~0.5.0",
"@devrev/meerkat-core": "*",
Expand Down
2 changes: 1 addition & 1 deletion meerkat-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devrev/meerkat-core",
"version": "0.0.73",
"version": "0.0.74",
"dependencies": {
"@swc/helpers": "~0.5.0"
},
Expand Down
8 changes: 8 additions & 0 deletions meerkat-core/src/cube-filter-transformer/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import { lteTransform } from './lte/lte';
import { notInDataRangeTransform } from './not-In-date-range/not-In-date-range';
import { notContainsTransform } from './not-contains/not-contains';
import { notEqualsTransform } from './not-equals/not-equals';
import { notSetTransform } from './not-set/not-set';
import { orDuckdbCondition } from './or/or';
import { setTransform } from './set/set';

export type CubeToParseExpressionTransform = (
query: QueryOperatorsWithInfo
Expand Down Expand Up @@ -54,6 +56,12 @@ const cubeFilterOperatorsToDuckdb = (cubeFilter: QueryOperatorsWithInfo) => {
return inDataRangeTransform(cubeFilter);
case 'notInDateRange':
return notInDataRangeTransform(cubeFilter);
case 'notSet': {
return notSetTransform(cubeFilter);
}
case 'set': {
return setTransform(cubeFilter);
}
default:
throw new Error('Could not transform the filter');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { notSetTransform } from "./not-set";

describe("notSetTransform", () => {
it("should return the correct expression for a given query", () => {
const query = {
member: "table.column"
};

const expectedExpression = {
class: "OPERATOR",
type: "OPERATOR_IS_NULL",
alias: "",
children: [
{
class: "COLUMN_REF",
type: "COLUMN_REF",
alias: "",
column_names: ["table", "column"]
}
]
};

const result = notSetTransform(query);

expect(result).toEqual(expectedExpression);
});
it("should return the correct expression for a __ delimited query", () => {
const query = {
member: "table__column"
};

const expectedExpression = {
class: "OPERATOR",
type: "OPERATOR_IS_NULL",
alias: "",
children: [
{
class: "COLUMN_REF",
type: "COLUMN_REF",
alias: "",
column_names: ["table__column"]
}
]
};

const result = notSetTransform(query);

expect(result).toEqual(expectedExpression);
});
});
19 changes: 19 additions & 0 deletions meerkat-core/src/cube-filter-transformer/not-set/not-set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ExpressionClass, ExpressionType } from '../../types/duckdb-serialization-types/serialization/Expression';
import { CubeToParseExpressionTransform } from "../factory";

export const notSetTransform: CubeToParseExpressionTransform = (query) => {
const { member } = query;
return {
class: ExpressionClass.OPERATOR,
type: ExpressionType.OPERATOR_IS_NULL,
alias: "",
children: [
{
class: "COLUMN_REF",
type: "COLUMN_REF",
alias: "",
column_names: member.split('.')
}
]
}
}
51 changes: 51 additions & 0 deletions meerkat-core/src/cube-filter-transformer/set/set.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { setTransform } from './set';

describe('setTransform', () => {
it('should return the correct expression object', () => {
const query = {
member: 'table.column'
};

const expected = {
class: 'OPERATOR',
type: 'OPERATOR_IS_NOT_NULL',
alias: '',
children: [
{
class: 'COLUMN_REF',
type: 'COLUMN_REF',
alias: '',
column_names: ['table', 'column']
}
]
};

const result = setTransform(query);

expect(result).toEqual(expected);
});

it('should handle __ delimited query', () => {
const query = {
member: 'table__column'
};

const expected = {
class: 'OPERATOR',
type: 'OPERATOR_IS_NOT_NULL',
alias: '',
children: [
{
class: 'COLUMN_REF',
type: 'COLUMN_REF',
alias: '',
column_names: ['table__column']
}
]
};

const result = setTransform(query);

expect(result).toEqual(expected);
});
});
19 changes: 19 additions & 0 deletions meerkat-core/src/cube-filter-transformer/set/set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ExpressionClass, ExpressionType } from '../../types/duckdb-serialization-types/serialization/Expression';
import { CubeToParseExpressionTransform } from "../factory";

export const setTransform: CubeToParseExpressionTransform = (query) => {
const { member } = query;
return {
class: ExpressionClass.OPERATOR,
type: ExpressionType.OPERATOR_IS_NOT_NULL,
alias: "",
children: [
{
class: "COLUMN_REF",
type: "COLUMN_REF",
alias: "",
column_names: member.split('.')
}
]
}
}
1 change: 0 additions & 1 deletion meerkat-core/src/utils/key-from-measures-dimension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export const getMemberInfoFromTableSchema = (
let memberInfo: Measure | Dimension | undefined;

const memberKeyName = memberKey.split('.')[1];
console.info('memberKeyName', memberKeyName, memberKey);

/**
* Finding the table key from the measures.
Expand Down
2 changes: 1 addition & 1 deletion meerkat-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devrev/meerkat-node",
"version": "0.0.71",
"version": "0.0.72",
"dependencies": {
"@swc/helpers": "~0.5.0",
"@devrev/meerkat-core": "*",
Expand Down
36 changes: 31 additions & 5 deletions meerkat-node/src/__tests__/cube-filter-params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ describe('filter-param-tests', () => {
(4, DATE '2022-03-01', 'cancelled', 40.00),
(5, DATE '2022-01-28', 'completed', 80.75),
(6, DATE '2022-02-15', 'pending', 120.00),
(7, DATE '2022-04-01', 'completed', 210.00);`);
(7, DATE '2022-02-15', 'pending', 120.00),
(8, DATE '2022-04-01', 'initiated', null);`);
});

it('Should apply filter params to base SQL', async () => {
Expand All @@ -60,7 +61,7 @@ describe('filter-param-tests', () => {
const sql = await cubeQueryToSQL(query, [SCHEMA]);
console.info('SQL: ', sql);
const output: any = await duckdbExec(sql);
expect(output).toHaveLength(1);
expect(output).toHaveLength(2);
expect(output[0].id).toBe(6);
});

Expand Down Expand Up @@ -93,7 +94,7 @@ describe('filter-param-tests', () => {
const sql = await cubeQueryToSQL(query, [SCHEMA]);
console.info('SQL: ', sql);
const output: any = await duckdbExec(sql);
expect(output).toHaveLength(2);
expect(output).toHaveLength(3);
expect(output[0].id).toBe(6);
});

Expand All @@ -107,7 +108,7 @@ describe('filter-param-tests', () => {
const sql = await cubeQueryToSQL(query, [SCHEMA]);
console.info('SQL: ', sql);
const output: any = await duckdbExec(sql);
expect(output).toHaveLength(7);
expect(output).toHaveLength(8);
});

it('Should apply true filter if filters are present but are not matching', async () => {
Expand Down Expand Up @@ -139,6 +140,31 @@ describe('filter-param-tests', () => {
const sql = await cubeQueryToSQL(query, [SCHEMA]);
console.info('SQL: ', sql);
const output: any = await duckdbExec(sql);
expect(output).toHaveLength(4);
expect(output).toHaveLength(5);
});
it('Should apply notSet and set filters', async () => {
const query = {
measures: ['*'],
filters: [
{
and: [
{
member: 'orders.amount',
operator: 'notSet',
},
{
member: 'orders.status',
operator: 'set',
}
],
},
],
dimensions: [],
};

const sql = await cubeQueryToSQL(query, [SCHEMA]);
console.info('SQL: ', sql);
const output: any = await duckdbExec(sql);
expect(output).toHaveLength(1);
});
});
Loading