fix(singlestoredb): support % as the modulo operator - #971
Open
MaxFreedomPollard wants to merge 1 commit into
Open
fix(singlestoredb): support % as the modulo operator#971MaxFreedomPollard wants to merge 1 commit into
MaxFreedomPollard wants to merge 1 commit into
Conversation
Formatting a SingleStoreDB query that uses % throws instead of
returning SQL:
format('SELECT 25 % 7', { language: 'singlestoredb' })
Parse error: Unexpected "% 7" at line 1 column 11.
SingleStore documents three equivalent modulo forms, MOD(a, b),
a MOD b and a % b. The first two format fine, the third does not.
The tokenizer builds its OPERATOR rule from a fixed standard set
(+ - / > < = <> <= >= !=) plus the dialect's own cfg.operators list,
in src/lexer/Tokenizer.ts. % is not in the standard set, and the
operators array in src/languages/singlestoredb/singlestoredb.formatter.ts
omits it, so no rule matches the character and TokenizerEngine raises a
parse error. Every MySQL-family sibling (mysql, mariadb, tidb) lists '%'
first in the same array; the SingleStoreDB copy dropped it.
Add '%' to that array, in the same leading position the siblings use.
regexFactory.operator sorts the alternatives by length descending, so
SingleStore's ::% conversion path-operator still matches before the
bare %.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Formatting a SingleStoreDB query that uses
%throws a parse error instead of returning SQL.MOD(a, b)anda MOD bformat fine, buta % bdoes not, even though SingleStore documents all three as equivalent (MOD givesexpression1 % expression2and the exampleSELECT 25 % 0;).This is the same symptom #578 reported against MySQL, where
(released_year % 2) != 0failed to format. MySQL was fixed; SingleStoreDB still fails on the identical query.Root cause
src/lexer/Tokenizer.tsbuilds theOPERATORrule from a fixed standard set (+ - / > < = <> <= >= !=) plus whatever the dialect puts incfg.operators.%is not in the standard set, and theoperatorsarray atsrc/languages/singlestoredb/singlestoredb.formatter.ts:267omits it, so nothing matches the character andTokenizerEngineraises the parse error.Every MySQL-family sibling lists
'%'first in that same array:src/languages/mysql/mysql.formatter.ts,src/languages/mariadb/mariadb.formatter.tsandsrc/languages/tidb/tidb.formatter.ts. The SingleStoreDB list, which otherwise tracks them, just dropped it.Fix
Add
'%'tosinglestoredb.formatter.ts, in the same leading position the siblings use.regexFactory.operatorsorts the alternatives by length descending, so SingleStore's three-character::%conversion path-operator still matches before the bare one-character%. The existingformats '::%' conversion path-operator without spacestest intest/singlestoredb.test.tspins that and still passes.I checked the other 19 dialect files under
src/languages/for the same omission and found none: every dialect whose documentation gives a%operator already lists it. Standardsqlandbigqueryare correct to leave it out, since GoogleSQL hasMOD(X, Y)and no%operator.db2ialso leaves it out, and I left that as it is.Tests
'%'added to thesupportsOperators(...)list intest/singlestoredb.test.ts, which covers both default spacing (foo % bar % zap) anddenseOperators: true(foo%bar). Prettier reflowed the array onto one entry per line, matching howtest/mysql.test.tsalready writes it.Both new cases fail on master with
Parse error: Unexpected "% bar %zap"and pass with the fix.pnpm run pretty:check,pnpm run lint,pnpm run ts:check,pnpm run testandpnpm run buildare all green.