SQLishParser.spec.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import {SQLishParser} from 'sentry/views/starfish/utils/sqlish/SQLishParser';
  2. describe('SQLishParser', function () {
  3. describe('SQLishParser()', () => {
  4. const parser = new SQLishParser();
  5. it.each([
  6. 'SELECT;',
  7. 'SELECT hello;',
  8. 'SELECT *;', // Wildcards
  9. 'WHERE age = 10;', // Equality
  10. 'WHERE age != 10;', // Inequality
  11. 'total / time', // Division
  12. 'sum(age)::numeric(0, 5)', // Type casting
  13. 'WHERE age > 10 AND age < 20;', // Comparison
  14. "WHERE$1 ILIKE ' % ' || 'text'", // Conditionals
  15. 'SELECT id, name;', // Column lists
  16. 'columns AS `tags[column]`', // ClickHouse backtics
  17. 'SELECT id, nam*', // Truncation
  18. 'AND created >= :c1', // PHP-Style I
  19. 'LIMIT $2', // PHP-style II
  20. 'created >= %s', // Python-style
  21. 'created >= $1', // Rails-style
  22. '@@ to_tsquery', // Postgres full-text search
  23. 'flags & %s)', // Bitwise AND
  24. 'flags | %s)', // Bitwise OR
  25. 'flags ^ %s)', // Bitwise XOR
  26. 'flags ~ %s)', // Bitwise NOT
  27. '+ %s as count', // Arithmetic
  28. ])('Parses %s', sql => {
  29. expect(() => {
  30. parser.parse(sql);
  31. }).not.toThrow();
  32. });
  33. });
  34. describe('SQLishParser.parse', () => {
  35. const parser = new SQLishParser();
  36. it('Distinguishes between real keywords and interpolated words', () => {
  37. expect(parser.parse('SELECT country')).toEqual([
  38. {
  39. type: 'Keyword',
  40. content: 'SELECT',
  41. },
  42. {
  43. type: 'Whitespace',
  44. content: ' ',
  45. },
  46. {
  47. type: 'GenericToken',
  48. content: 'country',
  49. },
  50. ]);
  51. expect(parser.parse('SELECT discount')).toEqual([
  52. {
  53. type: 'Keyword',
  54. content: 'SELECT',
  55. },
  56. {
  57. type: 'Whitespace',
  58. content: ' ',
  59. },
  60. {
  61. type: 'GenericToken',
  62. content: 'discount',
  63. },
  64. ]);
  65. });
  66. it('Detects collapsed columns', () => {
  67. expect(parser.parse('select ..')).toEqual([
  68. {
  69. type: 'Keyword',
  70. content: 'SELECT',
  71. },
  72. {
  73. type: 'Whitespace',
  74. content: ' ',
  75. },
  76. {
  77. type: 'CollapsedColumns',
  78. content: '..',
  79. },
  80. ]);
  81. });
  82. it('Detects whitespace between generic tokens and JOIN commands', () => {
  83. expect(parser.parse('sentry_users INNER JOIN sentry_messages')).toEqual([
  84. {
  85. type: 'GenericToken',
  86. content: 'sentry_users',
  87. },
  88. {type: 'Whitespace', content: ' '},
  89. {type: 'Keyword', content: 'INNER'},
  90. {type: 'Whitespace', content: ' '},
  91. {type: 'Keyword', content: 'JOIN'},
  92. {type: 'Whitespace', content: ' '},
  93. {
  94. type: 'GenericToken',
  95. content: 'sentry_messages',
  96. },
  97. ]);
  98. });
  99. });
  100. });