Skip to content

Let count, total, sum, avg, first, last, end and other keywords be names where they cannot be the keyword #2628

Open

Description

@mathiasrw

Some of the most common field names in JSON data cannot be used as names in AlaSQL: count, total, sum. SQLite (our reference engine) accepts them. Unlike the words in #2627, adding them to the NonReserved list is not enough, because each one already starts something valid at the same place. After SELECT, count can be the start of COUNT(...), so the parser has committed to the keyword before it sees that no ( follows.

Earlier context-aware keywords: #2203 (VALUE, VALUES, LEFT, RIGHT), #2383 (ORDER), #2387 (KEY), #2382 (PATH), #2381 (SOURCE). Per-keyword series: #2212 to #2261.

Measured on develop (3c08bbf)

Each word as a name in five places: CREATE TABLE t (w INT), SELECT w FROM t, WHERE w = 1, t.w and data->w. SQLite 3.43 accepts every word below as a column name.

Word What it starts today Fails in AlaSQL Series issue
count, total, sum, avg an aggregate: COUNT( all five places sum #2217
first, last an aggregate SELECT, WHERE #2233, #2226
end the end of CASE all five #2236
desc, asc the sort direction after an expression all five none
top SELECT TOP n all five none
row, matrix, recordset SELECT ROW, SELECT MATRIX and so on all except right after SELECT, where they are read as the modifier none
new NEW Date() all five #2251
array the ARRAY() aggregate all five none
interval an INTERVAL value all five none
replace REPLACE() and REPLACE INTO all five none
any, some = ANY (subquery) all five any #2242
like the operator all five #2250

Direction

Most valuable first: count, total, sum, avg, first, last, end.

  • In the grammar, let these tokens also reduce to a name when what follows cannot continue the keyword's own construct, for example COUNT not followed by (. A single shared rule for this keeps it in one place, like the NonReserved rule does for the simple words.
  • t.count and data->count can be handled first and separately: after . and -> only a name can follow, so there is no ambiguity there at all.
  • Words used after an expression (desc, asc, like) also meet the alias without AS (SELECT a desc FROM t). Decide per word what that reads as; SQLite's answer is the reference.

The grammar warnings must stay at 282, and yarn jison must reproduce the committed parser. Measure parse speed before and after with test/performance/bench.js ("parse mid-size select").

This only accepts SQL that fails to parse today, so it is not breaking.

Tests: test/test<id>.js with one it per word, covering the five places and INSERT INTO t (w) VALUES (1), plus the keyword's own use unchanged (COUNT(*), SELECT TOP 2, CASE ... END, ORDER BY a DESC).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions