Skip to content
Open
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
6 changes: 6 additions & 0 deletions .changeset/mixed-operator-precedence.md
Original file line number Diff line number Diff line change
@@ -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`
48 changes: 48 additions & 0 deletions packages/common-utils/src/__tests__/queryParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }),
Expand Down
25 changes: 20 additions & 5 deletions packages/common-utils/src/queryParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 major — Mixed chains with negated operators are still regrouped: a AND NOT b OR c keeps the bug this PR fixes

The lucene grammar parses AND NOT/OR NOT/NOT as single operators and right-nests them the same way it does AND/OR. So a AND NOT b OR c parses as AND NOT(a, OR(b, c)). Because of the !negatingOperator exclusion, the serializer still emits a AND NOT b OR c, which ClickHouse evaluates as (a AND NOT b) OR c. That is wrong: it should be a AND NOT (b OR c). Results for this query are still silently wrong, and it now behaves differently from a AND b OR c, which the PR does group. Fix: drop the negatingOperator exclusion so the OR operand is wrapped (NOT (b OR c)), or narrow it to a case the AST actually needs. Add tests for a AND NOT b OR c, a NOT b OR c and a OR NOT b AND c alongside the new mixed operator precedence cases. The current tests only cover AND/OR, so they would pass with this regression in place.

Nothing blocks merge automatically, but a maintainer will expect this fixed if it is a real defect in code this PR changes. If it is about surrounding code, reply and say so instead of patching. Do not widen the PR. How to respond

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;
Expand Down
Loading