Skip to content

Commit 8d87e0a

Browse files
committed
fix: Improve isJsonExpression
1 parent 30dbf17 commit 8d87e0a

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

packages/common-utils/src/__tests__/utils.test.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -676,22 +676,22 @@ describe('utils', () => {
676676
});
677677
});
678678

679-
describe('findJsonAccesses', () => {
679+
describe('findJsonExpressions', () => {
680680
it('should handle empty expression', () => {
681681
const sql = '';
682682
const actual = findJsonExpressions(sql);
683683
const expected = [];
684684
expect(actual).toEqual(expected);
685685
});
686686

687-
it('should find a single JSON access', () => {
687+
it('should find a single JSON expression', () => {
688688
const sql = 'SELECT a.b.c as alias1, col2 as alias2 FROM table';
689689
const actual = findJsonExpressions(sql);
690690
const expected = [{ index: 7, expr: 'a.b.c' }];
691691
expect(actual).toEqual(expected);
692692
});
693693

694-
it('should find multiple JSON access', () => {
694+
it('should find multiple JSON expression', () => {
695695
const sql = 'SELECT a.b.c, d.e, col2 FROM table';
696696
const actual = findJsonExpressions(sql);
697697
const expected = [
@@ -701,14 +701,14 @@ describe('utils', () => {
701701
expect(actual).toEqual(expected);
702702
});
703703

704-
it('should find JSON access with type specifier', () => {
704+
it('should find JSON expression with type specifier', () => {
705705
const sql = 'SELECT a.b.:UInt64, col2 FROM table';
706706
const actual = findJsonExpressions(sql);
707707
const expected = [{ index: 7, expr: 'a.b.:UInt64' }];
708708
expect(actual).toEqual(expected);
709709
});
710710

711-
it('should find JSON access with complex type specifier', () => {
711+
it('should find JSON expression with complex type specifier', () => {
712712
const sql = 'SELECT a.b.:Array(String) , col2 FROM table';
713713
const actual = findJsonExpressions(sql);
714714
const expected = [{ index: 7, expr: 'a.b.:Array(String)' }];
@@ -850,6 +850,20 @@ describe('utils', () => {
850850
const expected = [{ index: 10, expr: 'a.b.c' }];
851851
expect(actual).toEqual(expected);
852852
});
853+
854+
it('should not find a decimal number expression', () => {
855+
const sql = 'SELECT 10.50, 2.3, 2, 1.5 - a.b FROM table';
856+
const actual = findJsonExpressions(sql);
857+
const expected = [{ index: 28, expr: 'a.b' }];
858+
expect(actual).toEqual(expected);
859+
});
860+
861+
it('should not find a . as a JSON expression', () => {
862+
const sql = 'SELECT . FROM table';
863+
const actual = findJsonExpressions(sql);
864+
const expected = [];
865+
expect(actual).toEqual(expected);
866+
});
853867
});
854868

855869
describe('replaceJsonAccesses', () => {

packages/common-utils/src/clickhouse/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,14 @@ export function chSqlToAliasMap(
722722
aliasMap[alias] = jsonReplacementsToExpressions.get(expression)!;
723723
}
724724
}
725+
return aliasMap;
725726
} catch (e) {
726-
console.error('Error parsing alias map', e, 'for query', chSql);
727+
console.error(
728+
'Error parsing alias map with JSON removed',
729+
e,
730+
'for query',
731+
chSql,
732+
);
727733
}
728734

729735
return aliasMap;

packages/common-utils/src/utils.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,22 @@ const isJsonExpression = (expr: string) => {
100100
if (c === '"' && !isInBacktick) {
101101
isInDoubleQuote = !isInDoubleQuote;
102102
current += c;
103-
continue;
104103
} else if (c === '`' && !isInDoubleQuote) {
105104
isInBacktick = !isInBacktick;
106105
current += c;
107-
continue;
108106
} else if (c === '.' && !isInDoubleQuote && !isInBacktick) {
109107
parts.push(current);
110108
current = '';
111-
continue;
109+
} else {
110+
current += c;
112111
}
113112
}
114113

115114
if (!isInDoubleQuote && !isInBacktick) {
116115
parts.push(current);
117116
}
118117

119-
return parts.length > 1;
118+
return parts.filter(p => p.trim().length > 0 && isNaN(Number(p))).length > 1;
120119
};
121120

122121
/**

0 commit comments

Comments
 (0)