/*! MySQL-specific code */ is not a comment
Hi!
According to docs http://dev.mysql.com/doc/refman/5.7/en/comments.html for example /*!40101 SET NAMES utf8 */; or /*! SET NAMES utf8 */; are an conditional-executed pieces of code. I think this should not be truncated by sqlparse.format(sql, strip_comments=True).
What do You think?
They all seem to start with /*! correct? I made a similar note (in an upstream branch) with respect to Oracle's hint comments but didn't press the issue. I think they shouldn't be stripped out.
In mysql do they have a particular location they go? in oracle they usually are next to the select keyword.
@andialbrecht any concerns in keeping them? should it be a secondary flag to strip out "special" comments?
I'm not an expert, but it seem not to be resolved in strings when I perform sql INSERT... Some example cases are in documentation that I gave a link before.
not sure i understood what you meant. The example shows CREATE and SELECT examples.
The changes I have in mind would identify the token and categorize it as a comment.code type regarless of what type of statement its in, or if its a valid instruction as long as it starts and ends with /*! */
@vmuriart, Oracle's optimizer hints all look like /*+ … */. The gory details are here (Oracle 11g).
All the examples and docs I've seen match this regex exactly, including the spaces:
re.match(r'/\*\+ .+ \*/', token.value)
So I think your solution of adding a non-skipped, non-stripped Token.Comment.Code makes a lot of sense. :+1:
Agreed!. I placed the hint tokens in an earlier commit is I'm familiar with its existance. Turns out there is a more obscure hint type; --+ ... \n is valid hint syntax.
On a side note, ISO-SQL92 defines another way to embed code (from other languages) within SQL. I've yet to find query that use it or database that allows it.
Just remembered why I haven't closed issue. I have upstream work that changes the behavior of the comment filter and handles it much earlier in the parsing process to speed-up the parsing grouping process.
I was waiting for 0.2.0 release (hint: @andialbrecht :innocent: ) before considering merging that work in.
Hi all,
Just checking in on this...I am on sqlparse version 0.2.3 (python 3.5.3) and am having issues stripping comments from Vertica SQL containing hints in the format /*+
Actually upon further investigation, this may be a separate issue with parsing comments inside comments?
stmt = "/* insert /*+ direct */ into table1 select * from table2; /" sqlparse.format(stmt, strip_comments=True, strip_whitespace=True) 'into table1 select * from table2;/'
It looks like embedding /*
Thanks!
Any workaround for this issue?
Hi everyone! Same problem w/ cutting SQL-Hints from scripts but for Vertica.
We are using this library not directly, but as a part of Airflow:
- sqlparse 0.5.1
- Python 3.10.10
- Airflow 2.7.3
Examples
Code in Airflow project looks something like this:
sqlparse.split(sqlparse.format(sql, strip_comments=True))
Code for tests:
import sqlparse
sql = """..."""
if __name__ == "__main__":
res = sqlparse.split(sqlparse.format(sql, strip_comments=True))
for i in res:
print(i)
Input:
/* multiline comment one line */
/* multiline comment
couple line */
-- single line comment standard
/* Vertica */
INSERT /*+ DIRECT */ INTO table_schema.table_name
select * from work.table1; -- select * from trst
insert /*+ DIRECT */ into schema.table_name as select * from foo;
with /*+ENABLE_WITH_CLAUSE_MATERIALIZATION*/
prt_date as (
select distinct partition_date as prt_d
from work.table_name
)
select *
from prt_date;
/* Oracle hints
https://hellodba.com/Download/OracleSQLHints.pdf
*/
select --+full(u);
SELECT /*+cluster(T)*/* FROM T_EEE T where A >:1;
SELECT /*+ hash(a) full(d) */* FROM T_AAA A, T_DDD D WHERE a.c=d.c;
SELECT /*+ ORDERED INDEX (b, jl_br_balances_n1) USE_NL (j b)
USE_NL (glcc glf) USE_MERGE (gp gsb) */
b.application_id ,
b.set_of_books_id ,
b.personnel_id,
p.vendor_id Personnel,
p.segment1 PersonnelNumber,
p.vendor_name Name
FROM jl_br_journals j, -- some comment
jl_br_balances b,
gl_code_combinations glcc,
fnd_flex_values_vl glf,
gl_periods gp,
gl_sets_of_books gsb,
po_vendors p;
/* MySQL comment */
# comment
#+ mysql hint
select * from foo;
Output w/ problem:
INSERT INTO table_schema.table_name
select * from work.table1;
insert into schema.table_name as select * from foo;
with
prt_date as (
select distinct partition_date as prt_d
from work.table_name
)
select *
from prt_date;
select
SELECT * FROM T_EEE T where A >:1;
SELECT * FROM T_AAA A, T_DDD D WHERE a.c=d.c;
SELECT
b.application_id ,
b.set_of_books_id ,
b.personnel_id,
p.vendor_id Personnel,
p.segment1 PersonnelNumber,
p.vendor_name Name
FROM jl_br_journals j,
jl_br_balances b,
gl_code_combinations glcc,
fnd_flex_values_vl glf,
gl_periods gp,
gl_sets_of_books gsb,
po_vendors p;
#+ mysql hint
select * from foo;
I made a fix for this problem in my fork https://github.com/skryzh/sqlparse and created PR, hope that will help (@rafaelreuber) https://github.com/andialbrecht/sqlparse/pull/792
Fixed output:
INSERT /*+ DIRECT */ INTO table_schema.table_name
select * from work.table1;
insert /*+ DIRECT */ into schema.table_name as select * from foo;
with /*+ENABLE_WITH_CLAUSE_MATERIALIZATION*/
prt_date as (
select distinct partition_date as prt_d
from work.table_name
)
select *
from prt_date;
select --+full(u);
SELECT /*+cluster(T)*/* FROM T_EEE T where A >:1;
SELECT /*+ hash(a) full(d) */* FROM T_AAA A, T_DDD D WHERE a.c=d.c;
SELECT /*+ ORDERED INDEX (b, jl_br_balances_n1) USE_NL (j b)
USE_NL (glcc glf) USE_MERGE (gp gsb) */
b.application_id ,
b.set_of_books_id ,
b.personnel_id,
p.vendor_id Personnel,
p.segment1 PersonnelNumber,
p.vendor_name Name
FROM jl_br_journals j,
jl_br_balances b,
gl_code_combinations glcc,
fnd_flex_values_vl glf,
gl_periods gp,
gl_sets_of_books gsb,
po_vendors p;
#+ mysql hint
select * from foo;
I open to discussions and suggestions.
Fixed in #792