SQLishParser.spec.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 * FROM #temp', // Temporary tables
  18. '# Fetches', // Comments
  19. '\r\n', // Windows newlines
  20. '✌🏻', // Emoji
  21. 'ă', // Unicode
  22. 'SELECT id, nam*', // Truncation
  23. 'AND created >= :c1', // PHP-Style I
  24. 'LIMIT $2', // PHP-style II
  25. 'created >= %s', // Python-style
  26. 'created >= $1', // Rails-style
  27. '@@ to_tsquery', // Postgres full-text search
  28. 'flags & %s)', // Bitwise AND
  29. 'flags | %s)', // Bitwise OR
  30. 'flags ^ %s)', // Bitwise XOR
  31. 'flags ~ %s)', // Bitwise NOT
  32. 'FROM temp{%s}', // Relay integer stripping
  33. '+ %s as count', // Arithmetic I
  34. '- %s as count', // Arithmetic II
  35. "ILIKE '\\_')", // Backslash
  36. ])('Parses %s', sql => {
  37. expect(() => {
  38. parser.parse(sql);
  39. }).not.toThrow();
  40. });
  41. });
  42. describe('SQLishParser.parse', () => {
  43. const parser = new SQLishParser();
  44. it('Distinguishes between real keywords and interpolated words', () => {
  45. expect(parser.parse('SELECT country')).toEqual([
  46. {
  47. type: 'Keyword',
  48. content: 'SELECT',
  49. },
  50. {
  51. type: 'Whitespace',
  52. content: ' ',
  53. },
  54. {
  55. type: 'GenericToken',
  56. content: 'country',
  57. },
  58. ]);
  59. expect(parser.parse('SELECT discount')).toEqual([
  60. {
  61. type: 'Keyword',
  62. content: 'SELECT',
  63. },
  64. {
  65. type: 'Whitespace',
  66. content: ' ',
  67. },
  68. {
  69. type: 'GenericToken',
  70. content: 'discount',
  71. },
  72. ]);
  73. });
  74. it('Detects collapsed columns', () => {
  75. expect(parser.parse('select ..')).toEqual([
  76. {
  77. type: 'Keyword',
  78. content: 'SELECT',
  79. },
  80. {
  81. type: 'Whitespace',
  82. content: ' ',
  83. },
  84. {
  85. type: 'CollapsedColumns',
  86. content: '..',
  87. },
  88. ]);
  89. });
  90. it('Detects whitespace between generic tokens and JOIN commands', () => {
  91. expect(parser.parse('sentry_users INNER JOIN sentry_messages')).toEqual([
  92. {
  93. type: 'GenericToken',
  94. content: 'sentry_users',
  95. },
  96. {type: 'Whitespace', content: ' '},
  97. {type: 'Keyword', content: 'INNER'},
  98. {type: 'Whitespace', content: ' '},
  99. {type: 'Keyword', content: 'JOIN'},
  100. {type: 'Whitespace', content: ' '},
  101. {
  102. type: 'GenericToken',
  103. content: 'sentry_messages',
  104. },
  105. ]);
  106. });
  107. });
  108. });