|
| 1 | +import { appendSelectWithPrimaryAndPartitionKey } from '@/components/DBRowTable'; |
| 2 | + |
| 3 | +describe('appendSelectWithPrimaryAndPartitionKey', () => { |
| 4 | + it('should extract columns from partition key with nested function call', () => { |
| 5 | + const result = appendSelectWithPrimaryAndPartitionKey( |
| 6 | + 'col1, col2', |
| 7 | + 'id, created_at', |
| 8 | + ' toStartOfInterval(timestamp, toIntervalDay(3))', |
| 9 | + ); |
| 10 | + expect(result).toEqual({ |
| 11 | + additionalKeysLength: 3, |
| 12 | + select: 'col1,col2,timestamp,id,created_at', |
| 13 | + }); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should extract no columns from empty primary key and partition key', () => { |
| 17 | + const result = appendSelectWithPrimaryAndPartitionKey('col1, col2', '', ''); |
| 18 | + expect(result).toEqual({ |
| 19 | + additionalKeysLength: 0, |
| 20 | + select: 'col1,col2', |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should extract columns from complex primary key', () => { |
| 25 | + const result = appendSelectWithPrimaryAndPartitionKey( |
| 26 | + 'col1, col2', |
| 27 | + 'id, timestamp, toStartOfInterval(timestamp2, toIntervalDay(3))', |
| 28 | + "toStartOfInterval(timestamp, toIntervalDay(3)), date_diff('DAY', col3, col4), now(), toDate(col5 + INTERVAL 1 DAY)", |
| 29 | + ); |
| 30 | + expect(result).toEqual({ |
| 31 | + additionalKeysLength: 6, |
| 32 | + select: 'col1,col2,timestamp,col3,col4,col5,id,timestamp2', |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should extract map columns', () => { |
| 37 | + const result = appendSelectWithPrimaryAndPartitionKey( |
| 38 | + 'col1, col2', |
| 39 | + `map['key']`, |
| 40 | + `map2['key'], map1['key3 ']`, |
| 41 | + ); |
| 42 | + expect(result).toEqual({ |
| 43 | + additionalKeysLength: 3, |
| 44 | + select: `col1,col2,map2['key'],map1['key3 '],map['key']`, |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should extract map columns', () => { |
| 49 | + const result = appendSelectWithPrimaryAndPartitionKey( |
| 50 | + 'col1, col2', |
| 51 | + ``, |
| 52 | + `map2['key.2']`, |
| 53 | + ); |
| 54 | + expect(result).toEqual({ |
| 55 | + additionalKeysLength: 1, |
| 56 | + select: `col1,col2,map2['key.2']`, |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should extract array columns', () => { |
| 61 | + const result = appendSelectWithPrimaryAndPartitionKey( |
| 62 | + 'col1, col2', |
| 63 | + `array[1]`, |
| 64 | + `array[2], array[3]`, |
| 65 | + ); |
| 66 | + expect(result).toEqual({ |
| 67 | + additionalKeysLength: 3, |
| 68 | + select: `col1,col2,array[2],array[3],array[1]`, |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should extract json columns', () => { |
| 73 | + const result = appendSelectWithPrimaryAndPartitionKey( |
| 74 | + 'col1, col2', |
| 75 | + `json.b`, |
| 76 | + `json.a, json.b.c, toStartOfDay(timestamp, json_2.d)`, |
| 77 | + ); |
| 78 | + expect(result).toEqual({ |
| 79 | + additionalKeysLength: 5, |
| 80 | + select: `col1,col2,json.a,json.b.c,timestamp,json_2.d,json.b`, |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should extract json columns with type specifiers', () => { |
| 85 | + const result = appendSelectWithPrimaryAndPartitionKey( |
| 86 | + 'col1, col2', |
| 87 | + `json.b.:Int64`, |
| 88 | + `toStartOfDay(json.a.b.:DateTime)`, |
| 89 | + ); |
| 90 | + expect(result).toEqual({ |
| 91 | + additionalKeysLength: 2, |
| 92 | + select: `col1,col2,json.a.b,json.b`, |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should skip json columns with hard-to-parse type specifiers', () => { |
| 97 | + const result = appendSelectWithPrimaryAndPartitionKey( |
| 98 | + 'col1, col2', |
| 99 | + `json.b.:Array(String), col3`, |
| 100 | + ``, |
| 101 | + ); |
| 102 | + expect(result).toEqual({ |
| 103 | + additionalKeysLength: 1, |
| 104 | + select: `col1,col2,col3`, |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should skip nested map references', () => { |
| 109 | + const result = appendSelectWithPrimaryAndPartitionKey( |
| 110 | + 'col1, col2', |
| 111 | + `map['key']['key2'], col3`, |
| 112 | + ``, |
| 113 | + ); |
| 114 | + expect(result).toEqual({ |
| 115 | + additionalKeysLength: 1, |
| 116 | + select: `col1,col2,col3`, |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
0 commit comments