Skip to content
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
24 changes: 7 additions & 17 deletions web-console/src/druid-models/flatten-spec/flatten-spec.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('flatten-spec', () => {
describe('computeFlattenExprsForData', () => {
const data = [
{
context: { host: 'cla', topic: 'moon', bonus: { foo: 'bar' } },
context: { host: 'cla', topic: 'moon', bonus: { 'fo.o': 'bar' } },
tags: ['a', 'b', 'c'],
messages: [
{ metric: 'request/time', value: 122 },
Expand All @@ -32,7 +32,7 @@ describe('flatten-spec', () => {
value: 5,
},
{
context: { host: 'piv', popic: 'sun' },
context: { 'host': 'piv', '1pic': 'sun' },
tags: ['a', 'd'],
messages: [
{ metric: 'request/time', value: 44 },
Expand All @@ -41,7 +41,7 @@ describe('flatten-spec', () => {
value: 4,
},
{
context: { host: 'imp', dopik: 'fun' },
context: { 'host': 'imp', "d\\o\npi'c'": 'fun' },
tags: ['x', 'y'],
messages: [
{ metric: 'request/time', value: 4 },
Expand All @@ -53,22 +53,12 @@ describe('flatten-spec', () => {
];

it('works for path, ignore-arrays', () => {
expect(computeFlattenExprsForData(data, 'path', 'ignore-arrays')).toEqual([
'$.context.bonus.foo',
'$.context.dopik',
expect(computeFlattenExprsForData(data, 'ignore-arrays')).toEqual([
"$.context.bonus['fo.o']",
'$.context.host',
'$.context.popic',
'$.context.topic',
]);
});

it('works for jq, ignore-arrays', () => {
expect(computeFlattenExprsForData(data, 'jq', 'ignore-arrays')).toEqual([
'.context.bonus.foo',
'.context.dopik',
'.context.host',
'.context.popic',
'.context.topic',
"$.context['1pic']",
"$.context['d\\\\o\npi\\'c\\'']",
]);
});
});
Expand Down
24 changes: 11 additions & 13 deletions web-console/src/druid-models/flatten-spec/flatten-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,29 @@ export const FLATTEN_FIELD_FIELDS: Field<FlattenField>[] = [
},
];

export type ExprType = 'path' | 'jq';
export type ArrayHandling = 'ignore-arrays' | 'include-arrays';

function escapePathKey(pathKey: string): string {
return /^[a-z]\w*$/i.test(pathKey)
? `.${pathKey}`
: `['${pathKey.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}']`;
}

export function computeFlattenPathsForData(
data: Record<string, any>[],
exprType: ExprType,
arrayHandling: ArrayHandling,
): FlattenField[] {
return computeFlattenExprsForData(data, exprType, arrayHandling).map(expr => {
return computeFlattenExprsForData(data, arrayHandling).map(expr => {
return {
name: expr.replace(/^\$?\./, ''),
type: exprType,
name: expr.replace(/^\$\./, '').replace(/['\]]/g, '').replace(/\[/g, '.'),
type: 'path',
expr,
};
});
}

export function computeFlattenExprsForData(
data: Record<string, any>[],
exprType: ExprType,
arrayHandling: ArrayHandling,
includeTopLevel = false,
): string[] {
Expand All @@ -91,12 +94,7 @@ export function computeFlattenExprsForData(
for (const datumKey of datumKeys) {
const datumValue = datum[datumKey];
if (includeTopLevel || isNested(datumValue)) {
addPath(
seenPaths,
exprType === 'path' ? `$.${datumKey}` : `.${datumKey}`,
datumValue,
arrayHandling,
);
addPath(seenPaths, `$${escapePathKey(datumKey)}`, datumValue, arrayHandling);
}
}
}
Expand All @@ -114,7 +112,7 @@ function addPath(
if (!Array.isArray(value)) {
const valueKeys = Object.keys(value);
for (const valueKey of valueKeys) {
addPath(paths, `${path}.${valueKey}`, value[valueKey], arrayHandling);
addPath(paths, `${path}${escapePathKey(valueKey)}`, value[valueKey], arrayHandling);
}
} else if (arrayHandling === 'include-arrays') {
for (let i = 0; i < value.length; i++) {
Expand Down
1 change: 0 additions & 1 deletion web-console/src/views/load-data-view/load-data-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,6 @@ export class LoadDataView extends React.PureComponent<LoadDataViewProps, LoadDat
if (canFlatten && !flattenFields.length && parserQueryState.data) {
suggestedFlattenFields = computeFlattenPathsForData(
filterMap(parserQueryState.data.rows, r => r.input),
'path',
'ignore-arrays',
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function jsonValue(ex: SqlExpression, path: string): SqlExpression {
}

function getJsonPaths(jsons: Record<string, any>[]): string[] {
return ['$.'].concat(computeFlattenExprsForData(jsons, 'path', 'include-arrays', true));
return ['$.'].concat(computeFlattenExprsForData(jsons, 'include-arrays', true));
}

function isComparable(x: unknown): boolean {
Expand Down