SQLishParser.spec.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 I
  28. '- %s as count', // Arithmetic II
  29. "ILIKE '\\_')", // Backslash
  30. ])('Parses %s', sql => {
  31. expect(() => {
  32. parser.parse(sql);
  33. }).not.toThrow();
  34. });
  35. });
  36. describe('SQLishParser.parse', () => {
  37. const parser = new SQLishParser();
  38. it('Distinguishes between real keywords and interpolated words', () => {
  39. expect(parser.parse('SELECT country')).toEqual([
  40. {
  41. type: 'Keyword',
  42. content: 'SELECT',
  43. },
  44. {
  45. type: 'Whitespace',
  46. content: ' ',
  47. },
  48. {
  49. type: 'GenericToken',
  50. content: 'country',
  51. },
  52. ]);
  53. expect(parser.parse('SELECT discount')).toEqual([
  54. {
  55. type: 'Keyword',
  56. content: 'SELECT',
  57. },
  58. {
  59. type: 'Whitespace',
  60. content: ' ',
  61. },
  62. {
  63. type: 'GenericToken',
  64. content: 'discount',
  65. },
  66. ]);
  67. });
  68. it('Detects collapsed columns', () => {
  69. expect(parser.parse('select ..')).toEqual([
  70. {
  71. type: 'Keyword',
  72. content: 'SELECT',
  73. },
  74. {
  75. type: 'Whitespace',
  76. content: ' ',
  77. },
  78. {
  79. type: 'CollapsedColumns',
  80. content: '..',
  81. },
  82. ]);
  83. });
  84. it('Detects whitespace between generic tokens and JOIN commands', () => {
  85. expect(parser.parse('sentry_users INNER JOIN sentry_messages')).toEqual([
  86. {
  87. type: 'GenericToken',
  88. content: 'sentry_users',
  89. },
  90. {type: 'Whitespace', content: ' '},
  91. {type: 'Keyword', content: 'INNER'},
  92. {type: 'Whitespace', content: ' '},
  93. {type: 'Keyword', content: 'JOIN'},
  94. {type: 'Whitespace', content: ' '},
  95. {
  96. type: 'GenericToken',
  97. content: 'sentry_messages',
  98. },
  99. ]);
  100. });
  101. });
  102. });