MySQL/MariaDB: support the `SOURCE` client command
Search before asking
- I searched the issues and found no similar issues.
Description
SOURCE file.sql is how a MySQL/MariaDB script includes another script. It is
the mechanism multi-file schemas and programs are assembled with, so it
appears in exactly the top-level files that drive everything else.
It's terminated by an end of line or a semicolon, so it can't easily be fixed without lexer support. It also allows spaces, and does not accept surrounding quotes, as they are interpreted as part of the filename (not by sqlfluff, but by MySQL/MariaDB).
SQLFluff currently reports the whole line as an unparsable section:
L: 1 | P: 1 | PRS | Line 1, Position 1: Found unparsable section: 'SOURCE
| schema/tables.sql'It is a client command rather than server SQL, but so is DELIMITER, which
SQLFluff already models as DelimiterStatement.
All of these are currently unparsable, on both mysql and mariadb:
SOURCE setup.sql
SOURCE schema/tables.sql;
source lowercase/path.sql
SOURCE path/with spaces/file.sql
\. schema/tables.sqlPaths may be relative or absolute, the trailing semicolon is optional, and an unquoted path may contain spaces, because the client reads to end of line.
I think it needs to lex like this to fit MySQL/MariaDB, at start of
statement (case insensitive):
(?i)[ \t]*(SOURCE|\\\.)[ ]+([^\n\r;]+?)[ ]*(\r\n|\n|;)
It is genuinely statement-positional rather than line-positional — this runs the SOURCE, so an anchor on the preceding newline would miss it:
> select 1; source 'abcd';
ERROR: Failed to open file ''abcd'', error: 2Though actually turning that into code is nontrivial. Note the lazy file capture
to avoid swallowing the later spaces. Case-insensitivity will need an inline
(?i): RegexLexer compiles with regex.DOTALL only and takes no flags
argument.
A SOURCE directive with multiple semicolons is parsed only up to the first semicolon, the second one is read by the sql client as another statement.
Multiple spaces are allowed both before and after the keyword, but there must be at least one after the keyword.
The argument is arbitrary text rather than SQL tokens, which is why this needs
the lexer. SOURCE schema/tables.sql happens to lex because / and . have
matchers, but a filename need not be made of those — and then it fails exactly
the way DELIMITER $$ does:
SOURCE a$b.sql Unable to lex characters: '$b.sql'
SOURCE C:\db\\\x.sql Unable to lex characters: '\db\\\x.sql'DELIMITER is the model: DelimiterStatement is inserted into
StatementSegment, so it is only offered where a statement can begin. That is
what keeps it unambiguous even though DELIMITER is an unreserved keyword and
SELECT delimiter FROM t parses fine. A SourceStatement in the same place
gets the same protection, which matters because SOURCE is unreserved too.
The one wrinkle is the path's end. DELIMITER sidesteps it —
match_grammar = Ref.keyword("DELIMITER") never consumes its own argument —
but SOURCE has to capture the path, and the terminator is a newline whenever
the optional semicolon is omitted. There is precedent for matching newlines
from a grammar: postgres defines
MultilineConcatenateNewline=TypedParser("newline", NewlineSegment, type="newline"),
MultilineConcatenateDelimiterGrammar=AnyNumberOf(
Ref("MultilineConcatenateNewline"), min_times=1, allow_gaps=False
),for multiline string concatenation, which is the same need.
#3599 is the same kind of problem and may want the same solution: https://github.com/sqlfluff/sqlfluff/issues/3599#issuecomment-5430153824
\. is the documented alias for SOURCE and means the same thing, so it is in
the syntax above. It needs one extra piece — the backslash currently fails with
Unable to lex characters, so it wants a fixed-string matcher. That is a
one-liner of a kind dialect_mysql.py already has six sets of
(StringLexer("walrus_operator", ":=", ...) and friends), it is stateless, and
build_lexers.py handles StringLexer so the Rust side comes along for free.
Nothing like the stateful matcher #3599 needs. Worth doing at the same time.
Are you willing to work on and submit a PR to address the issue?
Sort of. I'm trying to make MySQL/MariaDB first class in the functions/procedures realm, but I know @Mynk11 is working on a very similar problem with DELIMITER, and I think i should stay out of this while that's occurring. It also looks easier to subtly get wrong than my other PR's so I'd prefer that someone else do it. That being said, sure, maybe.
Use case
Linting a multi-file schema where scripts include SOURCE lines. Those files are
currently unparsable in their entirety, so they have to be excluded, which means
the files that define the load order are the ones that get no checking at all.
Dialect
mysql and mariadb
Are you willing to work on and submit a PR to address the issue?
- Yes I am willing to submit a PR!
Code of Conduct
- I agree to follow this project's Code of Conduct
Source: sqlfluff/sqlfluff