|
1 | | -import { replaceMacros } from '../macros'; |
| 1 | +import { renderFiltersToSql, replaceMacros } from '../macros'; |
| 2 | +import type { RawSqlChartConfig } from '../types'; |
| 3 | + |
| 4 | +/** Helper to create a minimal RawSqlChartConfig for testing */ |
| 5 | +function config( |
| 6 | + sqlTemplate: string, |
| 7 | + overrides?: Partial<RawSqlChartConfig>, |
| 8 | +): RawSqlChartConfig { |
| 9 | + return { |
| 10 | + configType: 'sql', |
| 11 | + sqlTemplate, |
| 12 | + connection: 'test', |
| 13 | + ...overrides, |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +describe('renderFiltersToSql', () => { |
| 18 | + it('should render sql_ast filters', () => { |
| 19 | + expect( |
| 20 | + renderFiltersToSql([ |
| 21 | + { type: 'sql_ast', operator: '=', left: 'col', right: "'val'" }, |
| 22 | + ]), |
| 23 | + ).toBe("(col = 'val')"); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should render sql filters', () => { |
| 27 | + expect( |
| 28 | + renderFiltersToSql([{ type: 'sql', condition: "name = 'test'" }]), |
| 29 | + ).toBe("(name = 'test')"); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should join multiple filters with AND', () => { |
| 33 | + expect( |
| 34 | + renderFiltersToSql([ |
| 35 | + { type: 'sql', condition: 'a = 1' }, |
| 36 | + { type: 'sql_ast', operator: '>', left: 'b', right: '2' }, |
| 37 | + ]), |
| 38 | + ).toBe('(a = 1) AND (b > 2)'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should skip empty sql conditions', () => { |
| 42 | + expect(renderFiltersToSql([{ type: 'sql', condition: '' }])).toBe( |
| 43 | + '(1=1 /** no filters applied */)', |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should skip lucene filters', () => { |
| 48 | + expect( |
| 49 | + renderFiltersToSql([{ type: 'lucene', condition: 'field:value' }]), |
| 50 | + ).toBe('(1=1 /** no filters applied */)'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return fallback for empty array', () => { |
| 54 | + expect(renderFiltersToSql([])).toBe('(1=1 /** no filters applied */)'); |
| 55 | + }); |
| 56 | +}); |
2 | 57 |
|
3 | 58 | describe('replaceMacros', () => { |
4 | 59 | it('should replace $__fromTime with seconds-precision DateTime', () => { |
5 | | - expect(replaceMacros('SELECT $__fromTime')).toBe( |
| 60 | + expect(replaceMacros(config('SELECT $__fromTime'))).toBe( |
6 | 61 | 'SELECT toDateTime(fromUnixTimestamp64Milli({startDateMilliseconds:Int64}))', |
7 | 62 | ); |
8 | 63 | }); |
9 | 64 |
|
10 | 65 | it('should replace $__toTime with seconds-precision DateTime', () => { |
11 | | - expect(replaceMacros('SELECT $__toTime')).toBe( |
| 66 | + expect(replaceMacros(config('SELECT $__toTime'))).toBe( |
12 | 67 | 'SELECT toDateTime(fromUnixTimestamp64Milli({endDateMilliseconds:Int64}))', |
13 | 68 | ); |
14 | 69 | }); |
15 | 70 |
|
16 | 71 | it('should replace $__fromTime_ms with millisecond-precision DateTime64', () => { |
17 | | - expect(replaceMacros('SELECT $__fromTime_ms')).toBe( |
| 72 | + expect(replaceMacros(config('SELECT $__fromTime_ms'))).toBe( |
18 | 73 | 'SELECT fromUnixTimestamp64Milli({startDateMilliseconds:Int64})', |
19 | 74 | ); |
20 | 75 | }); |
21 | 76 |
|
22 | 77 | it('should replace $__toTime_ms with millisecond-precision DateTime64', () => { |
23 | | - expect(replaceMacros('SELECT $__toTime_ms')).toBe( |
| 78 | + expect(replaceMacros(config('SELECT $__toTime_ms'))).toBe( |
24 | 79 | 'SELECT fromUnixTimestamp64Milli({endDateMilliseconds:Int64})', |
25 | 80 | ); |
26 | 81 | }); |
27 | 82 |
|
28 | 83 | it('should replace $__timeFilter with seconds-precision range filter', () => { |
29 | | - const result = replaceMacros('WHERE $__timeFilter(ts)'); |
| 84 | + const result = replaceMacros(config('WHERE $__timeFilter(ts)')); |
30 | 85 | expect(result).toBe( |
31 | 86 | 'WHERE ts >= toDateTime(fromUnixTimestamp64Milli({startDateMilliseconds:Int64})) AND ts <= toDateTime(fromUnixTimestamp64Milli({endDateMilliseconds:Int64}))', |
32 | 87 | ); |
33 | 88 | }); |
34 | 89 |
|
35 | 90 | it('should replace $__timeFilter_ms with millisecond-precision range filter', () => { |
36 | | - const result = replaceMacros('WHERE $__timeFilter_ms(ts)'); |
| 91 | + const result = replaceMacros(config('WHERE $__timeFilter_ms(ts)')); |
37 | 92 | expect(result).toBe( |
38 | 93 | 'WHERE ts >= fromUnixTimestamp64Milli({startDateMilliseconds:Int64}) AND ts <= fromUnixTimestamp64Milli({endDateMilliseconds:Int64})', |
39 | 94 | ); |
40 | 95 | }); |
41 | 96 |
|
42 | 97 | it('should replace $__dateFilter with date-only range filter', () => { |
43 | | - const result = replaceMacros('WHERE $__dateFilter(d)'); |
| 98 | + const result = replaceMacros(config('WHERE $__dateFilter(d)')); |
44 | 99 | expect(result).toBe( |
45 | 100 | 'WHERE d >= toDate(fromUnixTimestamp64Milli({startDateMilliseconds:Int64})) AND d <= toDate(fromUnixTimestamp64Milli({endDateMilliseconds:Int64}))', |
46 | 101 | ); |
47 | 102 | }); |
48 | 103 |
|
49 | 104 | it('should replace $__dateTimeFilter with combined date and time filter', () => { |
50 | | - const result = replaceMacros('WHERE $__dateTimeFilter(d, ts)'); |
| 105 | + const result = replaceMacros(config('WHERE $__dateTimeFilter(d, ts)')); |
51 | 106 | expect(result).toBe( |
52 | 107 | 'WHERE (d >= toDate(fromUnixTimestamp64Milli({startDateMilliseconds:Int64})) AND d <= toDate(fromUnixTimestamp64Milli({endDateMilliseconds:Int64}))) AND (ts >= toDateTime(fromUnixTimestamp64Milli({startDateMilliseconds:Int64})) AND ts <= toDateTime(fromUnixTimestamp64Milli({endDateMilliseconds:Int64})))', |
53 | 108 | ); |
54 | 109 | }); |
55 | 110 |
|
56 | 111 | it('should replace $__dt as an alias for dateTimeFilter', () => { |
57 | | - const result = replaceMacros('WHERE $__dt(d, ts)'); |
| 112 | + const result = replaceMacros(config('WHERE $__dt(d, ts)')); |
58 | 113 | expect(result).toBe( |
59 | 114 | 'WHERE (d >= toDate(fromUnixTimestamp64Milli({startDateMilliseconds:Int64})) AND d <= toDate(fromUnixTimestamp64Milli({endDateMilliseconds:Int64}))) AND (ts >= toDateTime(fromUnixTimestamp64Milli({startDateMilliseconds:Int64})) AND ts <= toDateTime(fromUnixTimestamp64Milli({endDateMilliseconds:Int64})))', |
60 | 115 | ); |
61 | 116 | }); |
62 | 117 |
|
63 | 118 | it('should replace $__timeInterval with interval bucketing expression', () => { |
64 | | - const result = replaceMacros('SELECT $__timeInterval(ts)'); |
| 119 | + const result = replaceMacros(config('SELECT $__timeInterval(ts)')); |
65 | 120 | expect(result).toBe( |
66 | 121 | 'SELECT toStartOfInterval(toDateTime(ts), INTERVAL {intervalSeconds:Int64} second)', |
67 | 122 | ); |
68 | 123 | }); |
69 | 124 |
|
70 | 125 | it('should replace $__timeInterval_ms with millisecond interval bucketing', () => { |
71 | | - const result = replaceMacros('SELECT $__timeInterval_ms(ts)'); |
| 126 | + const result = replaceMacros(config('SELECT $__timeInterval_ms(ts)')); |
72 | 127 | expect(result).toBe( |
73 | 128 | 'SELECT toStartOfInterval(toDateTime64(ts, 3), INTERVAL {intervalMilliseconds:Int64} millisecond)', |
74 | 129 | ); |
75 | 130 | }); |
76 | 131 |
|
77 | 132 | it('should replace $__interval_s with interval seconds param', () => { |
78 | | - expect(replaceMacros('INTERVAL $__interval_s second')).toBe( |
| 133 | + expect(replaceMacros(config('INTERVAL $__interval_s second'))).toBe( |
79 | 134 | 'INTERVAL {intervalSeconds:Int64} second', |
80 | 135 | ); |
81 | 136 | }); |
82 | 137 |
|
83 | 138 | it('should replace multiple macros in one query', () => { |
84 | | - const sql = |
85 | | - 'SELECT $__timeInterval(ts), count() FROM t WHERE $__timeFilter(ts) GROUP BY 1'; |
86 | | - const result = replaceMacros(sql); |
| 139 | + const result = replaceMacros( |
| 140 | + config( |
| 141 | + 'SELECT $__timeInterval(ts), count() FROM t WHERE $__timeFilter(ts) GROUP BY 1', |
| 142 | + ), |
| 143 | + ); |
87 | 144 | expect(result).toContain('toStartOfInterval'); |
88 | 145 | expect(result).toContain( |
89 | 146 | 'ts >= toDateTime(fromUnixTimestamp64Milli({startDateMilliseconds:Int64}))', |
90 | 147 | ); |
91 | 148 | }); |
92 | 149 |
|
93 | 150 | it('should throw on wrong argument count', () => { |
94 | | - expect(() => replaceMacros('$__timeFilter(a, b)')).toThrow( |
| 151 | + expect(() => replaceMacros(config('$__timeFilter(a, b)'))).toThrow( |
95 | 152 | 'expects 1 argument(s), but got 2', |
96 | 153 | ); |
97 | 154 | }); |
98 | 155 |
|
99 | 156 | it('should throw on missing close bracket', () => { |
100 | | - expect(() => replaceMacros('$__timeFilter(col')).toThrow( |
| 157 | + expect(() => replaceMacros(config('$__timeFilter(col'))).toThrow( |
101 | 158 | 'Failed to parse macro arguments', |
102 | 159 | ); |
103 | 160 | }); |
| 161 | + |
| 162 | + it('should replace $__filters with rendered filter conditions', () => { |
| 163 | + const result = replaceMacros( |
| 164 | + config('WHERE $__filters', { |
| 165 | + filters: [ |
| 166 | + { type: 'sql', condition: "col = 'val'" }, |
| 167 | + { type: 'sql_ast', operator: '>', left: 'x', right: '1' }, |
| 168 | + ], |
| 169 | + }), |
| 170 | + ); |
| 171 | + expect(result).toBe("WHERE (col = 'val') AND (x > 1)"); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should replace $__filters with fallback when no filters provided', () => { |
| 175 | + expect(replaceMacros(config('WHERE $__filters'))).toBe( |
| 176 | + 'WHERE (1=1 /** no filters applied */)', |
| 177 | + ); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should replace $__filters with fallback when filters is empty', () => { |
| 181 | + expect(replaceMacros(config('WHERE $__filters', { filters: [] }))).toBe( |
| 182 | + 'WHERE (1=1 /** no filters applied */)', |
| 183 | + ); |
| 184 | + }); |
104 | 185 | }); |
0 commit comments