From 120b6931083698c982fe9dcbdb51876ab2f4f2ca Mon Sep 17 00:00:00 2001 From: Tyagiquamar Date: Tue, 17 Nov 2026 15:47:38 +0530 Subject: [PATCH] fix(common-utils): preserve boolean grouping for mixed AND/OR searches Signed-off-by: Tyagiquamar --- .changeset/mixed-operator-precedence.md | 6 +++ .../src/__tests__/queryParser.test.ts | 48 +++++++++++++++++++ packages/common-utils/src/queryParser.ts | 25 ++++++++-- 3 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 .changeset/mixed-operator-precedence.md diff --git a/.changeset/mixed-operator-precedence.md b/.changeset/mixed-operator-precedence.md new file mode 100644 index 0000000000..2cadfb2bb0 --- /dev/null +++ b/.changeset/mixed-operator-precedence.md @@ -0,0 +1,6 @@ +--- +'@hyperdx/common-utils': patch +--- + +fix: preserve boolean grouping when a search mixes AND and OR without explicit +parentheses, so generated SQL no longer regroups the query as `(a AND b) OR c` diff --git a/packages/common-utils/src/__tests__/queryParser.test.ts b/packages/common-utils/src/__tests__/queryParser.test.ts index 85b7b50971..4ad9d4a9cc 100644 --- a/packages/common-utils/src/__tests__/queryParser.test.ts +++ b/packages/common-utils/src/__tests__/queryParser.test.ts @@ -2774,6 +2774,54 @@ describe('CustomSchemaSQLSerializerV2 - Array and Nested Fields', () => { ); }); +describe('CustomSchemaSQLSerializerV2 - mixed operator precedence', () => { + const metadata = getMetadata( + new ClickhouseClient({ host: 'http://localhost:8123' }), + ); + metadata.getColumn = jest.fn().mockImplementation(async ({ column }) => ({ + name: column, + type: 'String', + })); + metadata.getMaterializedColumnsLookupTable = jest + .fn() + .mockImplementation(async () => new Map()); + const serializer = new CustomSchemaSQLSerializerV2({ + metadata, + databaseName: 'testName', + tableName: 'testTable', + connectionId: 'testId', + implicitColumnExpression: 'Body', + }); + + it('groups a right-nested OR inside an AND when the source has no parentheses', async () => { + const sql = await new SearchQueryBuilder( + 'a:"1" AND b:"2" OR c:"3"', + serializer, + ).build(); + expect(sql).toBe("((a = '1') AND ((b = '2') OR (c = '3')))"); + }); + + it('emits the same SQL when the grouping is explicit in the source', async () => { + const implicit = await new SearchQueryBuilder( + 'a:"1" AND b:"2" OR c:"3"', + serializer, + ).build(); + const explicit = await new SearchQueryBuilder( + 'a:"1" AND (b:"2" OR c:"3")', + serializer, + ).build(); + expect(explicit).toBe(implicit); + }); + + it('leaves same-operator chains unchanged', async () => { + const sql = await new SearchQueryBuilder( + 'a:"1" AND b:"2" AND c:"3"', + serializer, + ).build(); + expect(sql).toBe("((a = '1') AND (b = '2') AND (c = '3'))"); + }); +}); + describe('genEnglishExplanation', () => { const metadata = getMetadata( new ClickhouseClient({ host: 'http://localhost:8123' }), diff --git a/packages/common-utils/src/queryParser.ts b/packages/common-utils/src/queryParser.ts index e6a34a8e92..8e4ae1f8b8 100644 --- a/packages/common-utils/src/queryParser.ts +++ b/packages/common-utils/src/queryParser.ts @@ -2195,13 +2195,28 @@ async function serialize( const parenthesized = binaryAST.parenthesized; const newContext = createSerializerContext(context, binaryAST); + // The parser right-nests mixed-operator chains but only marks nodes that + // were parenthesized in the source. Without explicit parens, SQL evaluates + // AND before OR, regrouping `a AND b OR c` as `(a AND b) OR c`. Only wrap a + // child whose operator differs; same-operator, start-marked and negated + // forms keep their current spelling so saved searches are unaffected. + const negatingOperator = + binaryAST.operator === 'NOT' || + binaryAST.operator === 'AND NOT' || + binaryAST.operator === 'OR NOT'; + const serializeOperand = async (child: lucene.AST | lucene.Node) => { + const text = await serialize(child, serializer, newContext); + return isBinaryAST(child) && + !child.parenthesized && + !hasStart(child) && + !negatingOperator && + child.operator !== binaryAST.operator + ? `(${text})` + : text; + }; const serialized = `${isNegatedAndParenthesized(binaryAST) ? 'NOT ' : ''}${parenthesized ? '(' : ''}${ hasStart(binaryAST) ? `${binaryAST.start} ` : '' - }${await serialize( - binaryAST.left, - serializer, - newContext, - )} ${operator} ${await serialize(binaryAST.right, serializer, newContext)}${ + }${await serializeOperand(binaryAST.left)} ${operator} ${await serializeOperand(binaryAST.right)}${ parenthesized ? ')' : '' }`; return serialized;