mirror of
https://github.com/SigNoz/signoz.git
synced 2026-11-27 10:41:04 +00:00
Compare commits
6 Commits
perf/uplot
...
fix/query-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d72141f1f2 | ||
|
|
b138f04189 | ||
|
|
fe87c075b2 | ||
|
|
a34c85a23f | ||
|
|
3237d0193f | ||
|
|
fd277c3d86 |
@@ -19,6 +19,7 @@ import {
|
||||
formatValueForExpression,
|
||||
parseLimit,
|
||||
removeKeysFromExpression,
|
||||
removeVariableFromExpression,
|
||||
sanitizeLimit,
|
||||
} from '../utils';
|
||||
|
||||
@@ -832,6 +833,18 @@ describe('convertFiltersToExpression', () => {
|
||||
expect(result.filters.items).toHaveLength(2);
|
||||
expect(result.filter.expression).toBe("service.name = 'old-service'");
|
||||
});
|
||||
|
||||
it.each([
|
||||
"path REGEXP '/api/.*' AND b = '2'",
|
||||
"path NOT REGEXP '/api/.*' AND b = '2'",
|
||||
])('keeps the clause through filters and back: %s', (expression) => {
|
||||
const items = convertExpressionToFilters(expression);
|
||||
|
||||
expect(
|
||||
convertFiltersToExpressionWithExistingQuery({ items, op: 'AND' }, expression)
|
||||
.filter.expression,
|
||||
).toBe(expression);
|
||||
});
|
||||
});
|
||||
|
||||
describe('convertAggregationToExpression', () => {
|
||||
@@ -1686,6 +1699,59 @@ describe('formatValueForExpression', () => {
|
||||
expect(pass2.filter.expression).toBe(EXPR);
|
||||
});
|
||||
});
|
||||
|
||||
describe('BETWEEN and NOT BETWEEN operators', () => {
|
||||
it('writes the bounds as low AND high', () => {
|
||||
expect(formatValueForExpression([1, 10], 'BETWEEN')).toBe('1 AND 10');
|
||||
expect(formatValueForExpression(['x', '$hi'], 'not between')).toBe(
|
||||
"'x' AND $hi",
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
"a BETWEEN 1 AND 10 AND b = '2'",
|
||||
"a NOT BETWEEN 1 AND 10 AND b = '2'",
|
||||
"k BETWEEN 'x' AND $hi",
|
||||
'a between 1 and 10',
|
||||
'a BETWEEN 1 AND b',
|
||||
])('keeps the clause through filters and back: %s', (expression) => {
|
||||
const items = convertExpressionToFilters(expression);
|
||||
|
||||
expect(
|
||||
convertFiltersToExpressionWithExistingQuery(
|
||||
{ items, op: 'AND' },
|
||||
expression,
|
||||
).filter.expression,
|
||||
).toBe(expression);
|
||||
});
|
||||
|
||||
it.each([
|
||||
[1, 'a BETWEEN 1 AND 10'],
|
||||
[[5, 50], 'a BETWEEN 5 AND 50'],
|
||||
])('applies a filter value of %j to the range', (value, expected) => {
|
||||
const items = [
|
||||
{
|
||||
id: '1',
|
||||
key: { key: 'a' } as BaseAutocompleteData,
|
||||
op: 'BETWEEN',
|
||||
value,
|
||||
},
|
||||
];
|
||||
|
||||
expect(
|
||||
convertFiltersToExpressionWithExistingQuery(
|
||||
{ items, op: 'AND' },
|
||||
'a BETWEEN 1 AND 10',
|
||||
).filter.expression,
|
||||
).toBe(expected);
|
||||
});
|
||||
|
||||
it('drops a range clause whose upper bound is the removed variable', () => {
|
||||
expect(
|
||||
removeVariableFromExpression("a BETWEEN $lo AND $hi AND b = '2'", 'hi'),
|
||||
).toBe("b = '2'");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('adjustQueryForV5', () => {
|
||||
|
||||
@@ -53,6 +53,11 @@ const isArrayOperator = (operator: string): boolean => {
|
||||
return arrayOperators.includes(operator);
|
||||
};
|
||||
|
||||
const isRangeOperator = (operator: string): boolean =>
|
||||
[OPERATORS.BETWEEN, `${OPERATORS.NOT} ${OPERATORS.BETWEEN}`].includes(
|
||||
operator.trim().toUpperCase(),
|
||||
);
|
||||
|
||||
const isVariable = (
|
||||
value: (string | number | boolean)[] | string | number | boolean,
|
||||
): boolean => {
|
||||
@@ -89,6 +94,18 @@ export const formatValueForExpression = (
|
||||
value: (string | number | boolean)[] | string | number | boolean,
|
||||
operator?: string,
|
||||
): string => {
|
||||
if (
|
||||
isRangeOperator(operator || '') &&
|
||||
Array.isArray(value) &&
|
||||
value.length === 2
|
||||
) {
|
||||
return value
|
||||
.map((bound) =>
|
||||
isVariable(bound) ? String(bound) : formatSingleValue(bound),
|
||||
)
|
||||
.join(' AND ');
|
||||
}
|
||||
|
||||
if (isVariable(value)) {
|
||||
return String(value);
|
||||
}
|
||||
@@ -443,10 +460,15 @@ export const convertFiltersToExpressionWithExistingQuery = (
|
||||
const existingPair = queryPairsMap.get(
|
||||
`${filter.key?.key}-${filter.op}`.trim().toLowerCase(),
|
||||
);
|
||||
const rewritesRange =
|
||||
Array.isArray(value) &&
|
||||
value.length === 2 &&
|
||||
!isEqual(formatValuesForFilter(existingPair?.valueList ?? []), value);
|
||||
if (
|
||||
existingPair &&
|
||||
existingPair.position?.valueStart &&
|
||||
existingPair.position?.valueEnd
|
||||
existingPair.position?.valueEnd &&
|
||||
(rewritesRange || !isRangeOperator(op))
|
||||
) {
|
||||
const formattedValue = formatValueForExpression(value, op);
|
||||
// replace the value with the new value
|
||||
|
||||
@@ -199,9 +199,9 @@ describe('carryOverListFilters', () => {
|
||||
it.each([
|
||||
"k8s.node.name BETWEEN 'node-a' AND 'node-z' AND k8s.cluster.name = 'cluster-a'",
|
||||
"k8s.node.name NOT BETWEEN 'node-a' AND 'node-z' AND k8s.cluster.name = 'cluster-a'",
|
||||
])('leaves out a range clause and keeps the rest: %s', (expression) => {
|
||||
])('carries a range clause whole: %s', (expression) => {
|
||||
expect(carryOverListFilters(expression, InfraMonitoringEntity.PODS)).toBe(
|
||||
"k8s.cluster.name = 'cluster-a'",
|
||||
expression,
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { CharStreams, CommonTokenStream } from 'antlr4';
|
||||
import { formatValueForExpression } from 'components/QueryBuilderV2/utils';
|
||||
import { OPERATORS } from 'constants/antlrQueryConstants';
|
||||
import FilterQueryLexer from 'parser/FilterQueryLexer';
|
||||
import { IQueryPair, IToken } from 'types/antlrQueryTypes';
|
||||
import { extractQueryPairs } from 'utils/queryContextUtils';
|
||||
@@ -249,7 +248,7 @@ export function resolveRelatedCategory(
|
||||
* not a plain AND of clauses (it has an OR, or a NOT over a group), so no
|
||||
* subset of its clauses means the same thing.
|
||||
*/
|
||||
function findNegatedKeyStarts(
|
||||
function findNegatedKeyStartsInAndChain(
|
||||
expression: string,
|
||||
pairs: IQueryPair[],
|
||||
): Set<number> | null {
|
||||
@@ -300,7 +299,7 @@ export function carryOverListFilters(
|
||||
}
|
||||
|
||||
const pairs = extractQueryPairs(expression);
|
||||
const negatedKeyStarts = findNegatedKeyStarts(expression, pairs);
|
||||
const negatedKeyStarts = findNegatedKeyStartsInAndChain(expression, pairs);
|
||||
|
||||
if (!negatedKeyStarts) {
|
||||
return '';
|
||||
@@ -311,9 +310,7 @@ export function carryOverListFilters(
|
||||
return pairs
|
||||
.filter(
|
||||
(pair) =>
|
||||
supported.has(pair.key) &&
|
||||
!negatedKeyStarts.has(pair.position.keyStart) &&
|
||||
pair.operator.toUpperCase() !== OPERATORS.BETWEEN,
|
||||
supported.has(pair.key) && !negatedKeyStarts.has(pair.position.keyStart),
|
||||
)
|
||||
.map((pair) => {
|
||||
const end = pair.position.valueEnd ?? pair.position.operatorEnd;
|
||||
|
||||
@@ -90,6 +90,10 @@ export function getOperatorValue(op: string): string {
|
||||
return 'contains';
|
||||
case 'NOT_CONTAINS':
|
||||
return 'not contains';
|
||||
case 'NOT_BETWEEN':
|
||||
return 'not between';
|
||||
case 'NOT_REGEXP':
|
||||
return 'not regexp';
|
||||
// Short form operators (InfraMonitoring)
|
||||
case 'NIN':
|
||||
return 'nin';
|
||||
|
||||
@@ -409,6 +409,44 @@ describe('extractQueryPairs', () => {
|
||||
expect(result[1].valuesPosition).toStrictEqual([]);
|
||||
expect(result[1].isComplete).toBe(true);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["a BETWEEN 1 AND 10 AND b = '2'", false],
|
||||
["a NOT BETWEEN 1 AND 10 AND b = '2'", true],
|
||||
])('keeps both bounds of a range in one pair: %s', (input, hasNegation) => {
|
||||
const pairs = extractQueryPairs(input);
|
||||
const low = input.indexOf('1 AND');
|
||||
const high = input.indexOf('10');
|
||||
|
||||
expect(pairs.map((pair) => pair.key)).toStrictEqual(['a', 'b']);
|
||||
expect(pairs[0]).toMatchObject({
|
||||
operator: 'BETWEEN',
|
||||
hasNegation,
|
||||
value: '1 AND 10',
|
||||
valueList: ['1', '10'],
|
||||
valuesPosition: [
|
||||
{ start: low, end: low },
|
||||
{ start: high, end: high + 1 },
|
||||
],
|
||||
isMultiValue: true,
|
||||
isComplete: true,
|
||||
position: { valueStart: low, valueEnd: high + 1 },
|
||||
});
|
||||
});
|
||||
|
||||
it('reads variable bounds as values, not as a new key', () => {
|
||||
expect(
|
||||
extractQueryPairs('a BETWEEN $lo AND $hi').map((pair) => pair.valueList),
|
||||
).toStrictEqual([['$lo', '$hi']]);
|
||||
});
|
||||
|
||||
it('keeps a range without its upper bound to the lower one', () => {
|
||||
expect(extractQueryPairs('a BETWEEN 1 AND')[0]).toMatchObject({
|
||||
value: '1',
|
||||
valueList: [],
|
||||
isMultiValue: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('createContext', () => {
|
||||
@@ -662,6 +700,21 @@ describe('getQueryContextAtCursor - trailing dot in key/value', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('getQueryContextAtCursor - range bounds', () => {
|
||||
it.each([
|
||||
[11, '1', 10, 10],
|
||||
[18, '10', 16, 17],
|
||||
])('targets the bound at cursor %i', (cursor, value, valueStart, valueEnd) => {
|
||||
const ctx = getQueryContextAtCursor('a BETWEEN 1 AND 10', cursor);
|
||||
|
||||
expect(ctx.isInValue).toBe(true);
|
||||
expect(ctx.currentPair).toMatchObject({
|
||||
value,
|
||||
position: { valueStart, valueEnd },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getQueryContextAtCursor - partial operator', () => {
|
||||
it('treats text after an incomplete key as an operator prefix', () => {
|
||||
const q = 'service.name c';
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { CharStreams, CommonTokenStream, Token } from 'antlr4';
|
||||
import { OPERATORS } from 'constants/antlrQueryConstants';
|
||||
import { analyzeQuery } from 'parser/analyzeQuery';
|
||||
import FilterQueryLexer from 'parser/FilterQueryLexer';
|
||||
import { IQueryContext, IQueryPair, IToken } from 'types/antlrQueryTypes';
|
||||
@@ -149,6 +150,37 @@ export function getCurrentValueIndexAtCursor(
|
||||
return null;
|
||||
}
|
||||
|
||||
function narrowToBetweenBoundAtCursor(
|
||||
pair: IQueryPair,
|
||||
cursorIndex: number,
|
||||
): IQueryPair {
|
||||
if (pair.operator.toUpperCase() !== OPERATORS.BETWEEN) {
|
||||
return pair;
|
||||
}
|
||||
|
||||
const bounds = pair.valuesPosition ?? [];
|
||||
const index = bounds.findIndex(
|
||||
({ start, end }) =>
|
||||
start !== undefined &&
|
||||
end !== undefined &&
|
||||
start <= cursorIndex &&
|
||||
cursorIndex <= end + 1,
|
||||
);
|
||||
if (index === -1) {
|
||||
return pair;
|
||||
}
|
||||
|
||||
return {
|
||||
...pair,
|
||||
value: pair.valueList?.[index],
|
||||
position: {
|
||||
...pair.position,
|
||||
valueStart: bounds[index].start,
|
||||
valueEnd: bounds[index].end,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Function to determine token context boundaries more precisely
|
||||
// eslint-disable-next-line sonarjs/cognitive-complexity
|
||||
function determineContextBoundaries(
|
||||
@@ -596,6 +628,9 @@ export function getQueryContextAtCursor(
|
||||
if (queryPairs.length > 0) {
|
||||
currentPair = getCurrentQueryPair(queryPairs, query, cursorIndex);
|
||||
}
|
||||
if (currentPair) {
|
||||
currentPair = narrowToBetweenBoundAtCursor(currentPair, cursorIndex);
|
||||
}
|
||||
|
||||
// Determine precise context boundaries
|
||||
const contextBoundaries = determineContextBoundaries(
|
||||
@@ -1314,6 +1349,21 @@ function indexPastArguments(tokens: IToken[], argumentStart: number): number {
|
||||
return index;
|
||||
}
|
||||
|
||||
function findUpperBoundIndex(
|
||||
tokens: IToken[],
|
||||
afterLowerBound: number,
|
||||
): number | null {
|
||||
const andIndex = nextVisibleIndex(tokens, afterLowerBound);
|
||||
if (tokens[andIndex]?.type !== FilterQueryLexer.AND) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const upperIndex = nextVisibleIndex(tokens, andIndex + 1);
|
||||
return upperIndex < tokens.length && isValueToken(tokens[upperIndex].type)
|
||||
? upperIndex
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts all key-operator-value triplets from a query string
|
||||
* This is useful for getting value suggestions based on the current key and operator
|
||||
@@ -1517,6 +1567,25 @@ export function extractQueryPairs(query: string): IQueryPair[] {
|
||||
!currentPair.value
|
||||
) {
|
||||
currentPair.value = token.text;
|
||||
let valueEnd = token.stop;
|
||||
|
||||
const upperIndex =
|
||||
currentPair.operator.toUpperCase() === OPERATORS.BETWEEN
|
||||
? findUpperBoundIndex(allTokens, iterator)
|
||||
: null;
|
||||
if (upperIndex !== null) {
|
||||
const upper = allTokens[upperIndex];
|
||||
currentPair.isMultiValue = true;
|
||||
currentPair.valueList = [token.text, upper.text];
|
||||
currentPair.valuesPosition = [
|
||||
{ start: token.start, end: token.stop },
|
||||
{ start: upper.start, end: upper.stop },
|
||||
];
|
||||
currentPair.value = query.substring(token.start, upper.stop + 1);
|
||||
valueEnd = upper.stop;
|
||||
iterator = upperIndex + 1;
|
||||
}
|
||||
|
||||
// Ensure we create a valid position object with all required fields
|
||||
currentPair.position = {
|
||||
keyStart: currentPair.position?.keyStart || 0,
|
||||
@@ -1524,7 +1593,7 @@ export function extractQueryPairs(query: string): IQueryPair[] {
|
||||
operatorStart: currentPair.position?.operatorStart || 0,
|
||||
operatorEnd: currentPair.position?.operatorEnd || 0,
|
||||
valueStart: token.start,
|
||||
valueEnd: token.stop,
|
||||
valueEnd,
|
||||
negationStart: currentPair.position?.negationStart || 0,
|
||||
negationEnd: currentPair.position?.negationEnd || 0,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user