From 3ee7657aa8f1fd8cb39ba39ea18e767143ad4c91 Mon Sep 17 00:00:00 2001 From: Tyagiquamar Date: Tue, 15 Sep 2026 15:06:16 +0530 Subject: [PATCH 1/2] fix(common-utils): preserve operator grouping for mixed AND/OR search queries --- .../src/__tests__/queryParser.test.ts | 84 +++++++++++++++++++ packages/common-utils/src/queryParser.ts | 46 +++++++++- 2 files changed, 128 insertions(+), 2 deletions(-) diff --git a/packages/common-utils/src/__tests__/queryParser.test.ts b/packages/common-utils/src/__tests__/queryParser.test.ts index c3f897b3db..a6801550d8 100644 --- a/packages/common-utils/src/__tests__/queryParser.test.ts +++ b/packages/common-utils/src/__tests__/queryParser.test.ts @@ -3293,3 +3293,87 @@ describe('CustomSchemaSQLSerializerV2 - KV items version gate', () => { expect(sql).toContain(HAS_FORM); }); }); + +describe('BinaryAST operator precedence and grouping in emitted SQL', () => { + const metadata = getMetadata( + new ClickhouseClient({ host: 'http://localhost:8123' }), + ); + metadata.getColumn = jest.fn().mockImplementation(async ({ column }) => { + return { name: column, type: 'String' }; + }); + metadata.getColumns = jest.fn().mockImplementation(async () => []); + metadata.getMaterializedColumnsLookupTable = jest + .fn() + .mockImplementation(async () => new Map()); + metadata.getSkipIndices = jest.fn().mockImplementation(async () => []); + metadata.getSetting = jest.fn().mockImplementation(async () => '0'); + metadata.getServerVersion = jest.fn().mockImplementation(async () => undefined); + metadata.isClickHouseCloud = jest.fn().mockImplementation(async () => false); + + const serializer = new CustomSchemaSQLSerializerV2({ + metadata, + databaseName: 'testDb', + tableName: 'testTable', + connectionId: 'testConn', + implicitColumnExpression: 'Body', + }); + + it('groups inner OR when mixed with AND without explicit parentheses', async () => { + const actual = await new SearchQueryBuilder( + 'a:"1" AND b:"2" OR c:"3"', + serializer, + ).build(); + expect(actual).toBe("((a = '1') AND ((b = '2') OR (c = '3')))"); + }); + + it('groups inner AND when mixed with OR without explicit parentheses', async () => { + const actual = await new SearchQueryBuilder( + 'a:"1" OR b:"2" AND c:"3"', + serializer, + ).build(); + expect(actual).toBe("((a = '1') OR ((b = '2') AND (c = '3')))"); + }); + + it('keeps chained identical AND operators flat', async () => { + const actual = await new SearchQueryBuilder( + 'a:"1" AND b:"2" AND c:"3"', + serializer, + ).build(); + expect(actual).toBe("((a = '1') AND (b = '2') AND (c = '3'))"); + }); + + it('keeps chained identical OR operators flat', async () => { + const actual = await new SearchQueryBuilder( + 'a:"1" OR b:"2" OR c:"3"', + serializer, + ).build(); + expect(actual).toBe("((a = '1') OR (b = '2') OR (c = '3'))"); + }); + + it('preserves explicitly parenthesized left-grouped expressions', async () => { + const actual = await new SearchQueryBuilder( + '(a:"1" AND b:"2") OR c:"3"', + serializer, + ).build(); + expect(actual).toBe("(((a = '1') AND (b = '2')) OR (c = '3'))"); + }); + + it('preserves explicitly parenthesized right-grouped expressions', async () => { + const actual = await new SearchQueryBuilder( + 'a:"1" AND (b:"2" OR c:"3")', + serializer, + ).build(); + expect(actual).toBe("((a = '1') AND ((b = '2') OR (c = '3')))"); + }); + + it('handles multi-level operator nesting correctly', async () => { + const actual = await new SearchQueryBuilder( + 'a:"1" AND b:"2" OR c:"3" AND d:"4"', + serializer, + ).build(); + expect(actual).toBe( + "((a = '1') AND ((b = '2') OR ((c = '3') AND (d = '4'))))", + ); + }); +}); + diff --git a/packages/common-utils/src/queryParser.ts b/packages/common-utils/src/queryParser.ts index 61326a599a..6befa5deef 100644 --- a/packages/common-utils/src/queryParser.ts +++ b/packages/common-utils/src/queryParser.ts @@ -2121,6 +2121,42 @@ function isNegatedAndParenthesized(ast: lucene.BinaryAST | lucene.LeftOnlyAST) { return ast.parenthesized && ast.field?.startsWith('-'); } +function getOperatorFamily(op: lucene.Operator): 'AND' | 'OR' { + switch (op as string) { + case '||': + case 'OR': + case 'OR NOT': + return 'OR'; + case '&&': + case '': + case 'AND': + case 'NOT': + case 'AND NOT': + default: + return 'AND'; + } +} + +async function serializeBinaryChild( + child: lucene.AST | lucene.Node, + parentOperator: lucene.Operator, + serializer: Serializer, + context: SerializerContext, +): Promise { + const result = await serialize(child, serializer, context); + if (!result) { + return ''; + } + if ( + isBinaryAST(child) && + !child.parenthesized && + getOperatorFamily(child.operator) !== getOperatorFamily(parentOperator) + ) { + return `(${result})`; + } + return result; +} + async function serialize( ast: lucene.AST | lucene.Node, serializer: Serializer, @@ -2147,11 +2183,17 @@ async function serialize( const newContext = createSerializerContext(context, binaryAST); const serialized = `${isNegatedAndParenthesized(binaryAST) ? 'NOT ' : ''}${parenthesized ? '(' : ''}${ hasStart(binaryAST) ? `${binaryAST.start} ` : '' - }${await serialize( + }${await serializeBinaryChild( binaryAST.left, + binaryAST.operator, + serializer, + newContext, + )} ${operator} ${await serializeBinaryChild( + binaryAST.right, + binaryAST.operator, serializer, newContext, - )} ${operator} ${await serialize(binaryAST.right, serializer, newContext)}${ + )}${ parenthesized ? ')' : '' }`; return serialized; From b19515c455b854d42ce5fea9fb6a6be32d4d8453 Mon Sep 17 00:00:00 2001 From: Tyagiquamar Date: Sat, 3 Oct 2026 21:31:30 +0530 Subject: [PATCH 2/2] test(common-utils): cover mixed query operator generation --- .../src/__tests__/queryParser.test.ts | 181 ++++++++++-------- packages/common-utils/src/queryParser.ts | 46 +---- 2 files changed, 99 insertions(+), 128 deletions(-) diff --git a/packages/common-utils/src/__tests__/queryParser.test.ts b/packages/common-utils/src/__tests__/queryParser.test.ts index a6801550d8..35b2484550 100644 --- a/packages/common-utils/src/__tests__/queryParser.test.ts +++ b/packages/common-utils/src/__tests__/queryParser.test.ts @@ -170,6 +170,103 @@ describe('CustomSchemaSQLSerializerV2 - json', () => { }); const testCases = [ + { + lucene: '(ServiceName:"a" AND ServiceName:"b") OR ServiceName:"c"', + sql: "(((ServiceName = 'a') AND (ServiceName = 'b')) OR (ServiceName = 'c'))", + english: + "('ServiceName' is a AND 'ServiceName' is b) OR 'ServiceName' is c", + }, + { + lucene: '-ServiceName:"a" AND ServiceName:"b" OR ServiceName:"c"', + sql: "((ServiceName != 'a') AND (ServiceName = 'b') OR (ServiceName = 'c'))", + english: + "'ServiceName' is not a AND 'ServiceName' is b OR 'ServiceName' is c", + }, + { + lucene: 'ServiceName:"a" NOT ServiceName:"b" OR ServiceName:"c"', + sql: "((ServiceName = 'a') AND NOT (ServiceName = 'b') OR (ServiceName = 'c'))", + english: + "'ServiceName' is a AND NOT 'ServiceName' is b OR 'ServiceName' is c", + }, + { + lucene: 'ServiceName:"a" OR NOT (ServiceName:"b" AND ServiceName:"c")', + sql: "((ServiceName = 'a') OR NOT ((ServiceName = 'b') AND (ServiceName = 'c')))", + english: + "'ServiceName' is a OR NOT ('ServiceName' is b AND 'ServiceName' is c)", + }, + { + lucene: 'ServiceName:"a" AND ServiceName:"b" OR ServiceName:"c"', + sql: "((ServiceName = 'a') AND (ServiceName = 'b') OR (ServiceName = 'c'))", + english: + "'ServiceName' is a AND 'ServiceName' is b OR 'ServiceName' is c", + }, + { + lucene: 'ServiceName:"a" OR ServiceName:"b" AND ServiceName:"c"', + sql: "((ServiceName = 'a') OR (ServiceName = 'b') AND (ServiceName = 'c'))", + english: + "'ServiceName' is a OR 'ServiceName' is b AND 'ServiceName' is c", + }, + { + lucene: + 'ServiceName:"a" AND ServiceName:"b" OR ServiceName:"c" AND ServiceName:"d"', + sql: "((ServiceName = 'a') AND (ServiceName = 'b') OR (ServiceName = 'c') AND (ServiceName = 'd'))", + english: + "'ServiceName' is a AND 'ServiceName' is b OR 'ServiceName' is c AND 'ServiceName' is d", + }, + { + lucene: '(ServiceName:"a" OR ServiceName:"b") AND ServiceName:"c"', + sql: "(((ServiceName = 'a') OR (ServiceName = 'b')) AND (ServiceName = 'c'))", + english: + "('ServiceName' is a OR 'ServiceName' is b) AND 'ServiceName' is c", + }, + { + lucene: 'ServiceName:"a" AND (ServiceName:"b" OR ServiceName:"c")', + sql: "((ServiceName = 'a') AND ((ServiceName = 'b') OR (ServiceName = 'c')))", + english: + "'ServiceName' is a AND ('ServiceName' is b OR 'ServiceName' is c)", + }, + { + lucene: 'ServiceName:"a" && ServiceName:"b" || ServiceName:"c"', + sql: "((ServiceName = 'a') AND (ServiceName = 'b') OR (ServiceName = 'c'))", + english: + "'ServiceName' is a AND 'ServiceName' is b OR 'ServiceName' is c", + }, + { + lucene: 'ServiceName:"a" || ServiceName:"b" && ServiceName:"c"', + sql: "((ServiceName = 'a') OR (ServiceName = 'b') AND (ServiceName = 'c'))", + english: + "'ServiceName' is a OR 'ServiceName' is b AND 'ServiceName' is c", + }, + { + lucene: 'ServiceName:"a" ServiceName:"b" OR ServiceName:"c"', + sql: "((ServiceName = 'a') AND (ServiceName = 'b') OR (ServiceName = 'c'))", + english: + "'ServiceName' is a AND 'ServiceName' is b OR 'ServiceName' is c", + }, + { + lucene: 'ServiceName:"a" AND NOT ServiceName:"b" OR ServiceName:"c"', + sql: "((ServiceName = 'a') AND NOT (ServiceName = 'b') OR (ServiceName = 'c'))", + english: + "'ServiceName' is a AND NOT 'ServiceName' is b OR 'ServiceName' is c", + }, + { + lucene: 'ServiceName:"a" OR NOT ServiceName:"b" AND ServiceName:"c"', + sql: "((ServiceName = 'a') OR NOT (ServiceName = 'b') AND (ServiceName = 'c'))", + english: + "'ServiceName' is a OR NOT 'ServiceName' is b AND 'ServiceName' is c", + }, + { + lucene: 'ServiceName:"a" AND NOT (ServiceName:"b" OR ServiceName:"c")', + sql: "((ServiceName = 'a') AND NOT ((ServiceName = 'b') OR (ServiceName = 'c')))", + english: + "'ServiceName' is a AND NOT ('ServiceName' is b OR 'ServiceName' is c)", + }, + { + lucene: 'NOT ServiceName:"a" OR ServiceName:"b" AND ServiceName:"c"', + sql: "(NOT (ServiceName = 'a') OR (ServiceName = 'b') AND (ServiceName = 'c'))", + english: + "NOT 'ServiceName' is a OR 'ServiceName' is b AND 'ServiceName' is c", + }, { lucene: '"foo bar baz"', sql: "((hasToken(lower(Body), lower('foo')) AND hasToken(lower(Body), lower('bar')) AND hasToken(lower(Body), lower('baz')) AND (lower(Body) LIKE lower('%foo bar baz%'))))", @@ -3293,87 +3390,3 @@ describe('CustomSchemaSQLSerializerV2 - KV items version gate', () => { expect(sql).toContain(HAS_FORM); }); }); - -describe('BinaryAST operator precedence and grouping in emitted SQL', () => { - const metadata = getMetadata( - new ClickhouseClient({ host: 'http://localhost:8123' }), - ); - metadata.getColumn = jest.fn().mockImplementation(async ({ column }) => { - return { name: column, type: 'String' }; - }); - metadata.getColumns = jest.fn().mockImplementation(async () => []); - metadata.getMaterializedColumnsLookupTable = jest - .fn() - .mockImplementation(async () => new Map()); - metadata.getSkipIndices = jest.fn().mockImplementation(async () => []); - metadata.getSetting = jest.fn().mockImplementation(async () => '0'); - metadata.getServerVersion = jest.fn().mockImplementation(async () => undefined); - metadata.isClickHouseCloud = jest.fn().mockImplementation(async () => false); - - const serializer = new CustomSchemaSQLSerializerV2({ - metadata, - databaseName: 'testDb', - tableName: 'testTable', - connectionId: 'testConn', - implicitColumnExpression: 'Body', - }); - - it('groups inner OR when mixed with AND without explicit parentheses', async () => { - const actual = await new SearchQueryBuilder( - 'a:"1" AND b:"2" OR c:"3"', - serializer, - ).build(); - expect(actual).toBe("((a = '1') AND ((b = '2') OR (c = '3')))"); - }); - - it('groups inner AND when mixed with OR without explicit parentheses', async () => { - const actual = await new SearchQueryBuilder( - 'a:"1" OR b:"2" AND c:"3"', - serializer, - ).build(); - expect(actual).toBe("((a = '1') OR ((b = '2') AND (c = '3')))"); - }); - - it('keeps chained identical AND operators flat', async () => { - const actual = await new SearchQueryBuilder( - 'a:"1" AND b:"2" AND c:"3"', - serializer, - ).build(); - expect(actual).toBe("((a = '1') AND (b = '2') AND (c = '3'))"); - }); - - it('keeps chained identical OR operators flat', async () => { - const actual = await new SearchQueryBuilder( - 'a:"1" OR b:"2" OR c:"3"', - serializer, - ).build(); - expect(actual).toBe("((a = '1') OR (b = '2') OR (c = '3'))"); - }); - - it('preserves explicitly parenthesized left-grouped expressions', async () => { - const actual = await new SearchQueryBuilder( - '(a:"1" AND b:"2") OR c:"3"', - serializer, - ).build(); - expect(actual).toBe("(((a = '1') AND (b = '2')) OR (c = '3'))"); - }); - - it('preserves explicitly parenthesized right-grouped expressions', async () => { - const actual = await new SearchQueryBuilder( - 'a:"1" AND (b:"2" OR c:"3")', - serializer, - ).build(); - expect(actual).toBe("((a = '1') AND ((b = '2') OR (c = '3')))"); - }); - - it('handles multi-level operator nesting correctly', async () => { - const actual = await new SearchQueryBuilder( - 'a:"1" AND b:"2" OR c:"3" AND d:"4"', - serializer, - ).build(); - expect(actual).toBe( - "((a = '1') AND ((b = '2') OR ((c = '3') AND (d = '4'))))", - ); - }); -}); - diff --git a/packages/common-utils/src/queryParser.ts b/packages/common-utils/src/queryParser.ts index 6befa5deef..61326a599a 100644 --- a/packages/common-utils/src/queryParser.ts +++ b/packages/common-utils/src/queryParser.ts @@ -2121,42 +2121,6 @@ function isNegatedAndParenthesized(ast: lucene.BinaryAST | lucene.LeftOnlyAST) { return ast.parenthesized && ast.field?.startsWith('-'); } -function getOperatorFamily(op: lucene.Operator): 'AND' | 'OR' { - switch (op as string) { - case '||': - case 'OR': - case 'OR NOT': - return 'OR'; - case '&&': - case '': - case 'AND': - case 'NOT': - case 'AND NOT': - default: - return 'AND'; - } -} - -async function serializeBinaryChild( - child: lucene.AST | lucene.Node, - parentOperator: lucene.Operator, - serializer: Serializer, - context: SerializerContext, -): Promise { - const result = await serialize(child, serializer, context); - if (!result) { - return ''; - } - if ( - isBinaryAST(child) && - !child.parenthesized && - getOperatorFamily(child.operator) !== getOperatorFamily(parentOperator) - ) { - return `(${result})`; - } - return result; -} - async function serialize( ast: lucene.AST | lucene.Node, serializer: Serializer, @@ -2183,17 +2147,11 @@ async function serialize( const newContext = createSerializerContext(context, binaryAST); const serialized = `${isNegatedAndParenthesized(binaryAST) ? 'NOT ' : ''}${parenthesized ? '(' : ''}${ hasStart(binaryAST) ? `${binaryAST.start} ` : '' - }${await serializeBinaryChild( + }${await serialize( binaryAST.left, - binaryAST.operator, - serializer, - newContext, - )} ${operator} ${await serializeBinaryChild( - binaryAST.right, - binaryAST.operator, serializer, newContext, - )}${ + )} ${operator} ${await serialize(binaryAST.right, serializer, newContext)}${ parenthesized ? ')' : '' }`; return serialized;