6

When writing a regular expression, it is possible to write the expression across multiple lines and including annotation, then compile the expression using the re.VERBOSE option before passing the compiled version. I'd like to do something similar with a pandas.read_sql_query.

For example, instead of:

result = pd.read_sql_query('select a.gvkey, a.tic, a.datadate as fyearend, year(a.datadate) as year, month(a.datadate) as fyrc, b.datadate, month(b.datadate) as month, b.trt1m from COMPM.FUNDA a join COMPM.SECM b on a.gvkey = b.gvkey and year(a.datadate) = year(b.datadate) where a.TIC = "IBM" and a.datafmt = "STD" and a.consol="C" and a.indfmt = "INDL" and year(a.datadate)>1980', engine)

I would like to write something like:

q = """select a.gvkey, 
    a.tic,                      #COMMENTS
    a.datadate as fyearend,     #COMMENTS
    year(a.datadate) as year,   #COMMENTS
    month(a.datadate) as fyrc, b.datadate, 
    month(b.datadate) as month, 
    b.trt1m 
    from COMPM.FUNDA a join COMPM.SECM b on a.gvkey = b.gvkey and year(a.datadate) = year(b.datadate) 
    where a.TIC = "IBM" 
        and a.datafmt = "STD" 
        and a.consol="C" 
        and a.indfmt = "INDL" 
        and year(a.datadate)>1980
"""

result = p.read_sql_query(q ,engine)

My question is related to this question about splitting python commands across multiple lines, but I'd like to add comments inside the query.

As I mentioned, what I'd like to do in the pandas/SQL case is similar to what can be done in the regular expression case with re.VERBOSE. Here is an example with regex:

pattern = r'''\s(shares?| #COMMENTS
            warrants?|       #COMMENTS
            stock|           #AND SO ON...
            (non)?vest(ed)?
            )\b             
            '''
crit = re.compile(pattern_nopt, re.VERBOSE)
match=re.search(crit, string)

This would make the query more readable and I find it important to annotate queries exhaustively when sharing code with coauthors.

Arthur Morris
  • 1,253
  • 1
  • 15
  • 21

1 Answers1

12

Yes it will works but you have to use the right comment delimiter for SQLite :
-- for an inline comment
/* foo.. */ (as in C) for a multi-lines comment

So it will looks like :

q = """select a.gvkey, 
    a.tic,                      -- COMMENTS
    a.datadate as fyearend,     -- COMMENTS
    year(a.datadate) as year,   /* Another very long
    and multi-lines comment... */
    month(a.datadate) as fyrc, b.datadate, 
    month(b.datadate) as month, 
    b.trt1m from COMPM.FUNDA a join COMPM.SECM b on a.gvkey = b.gvkey and year(a.datadate) = year(b.datadate) 
    where a.TIC = "IBM" 
        and a.datafmt = "STD" 
        and a.consol="C" 
        and a.indfmt = "INDL" 
        and year(a.datadate)>1980
"""

result = p.read_sql_query(q, conn)
mgc
  • 5,223
  • 1
  • 24
  • 37
  • mgc, Thanks! This works when I use multi-line comment(`/* foo */`) , but not when I use the inline comment (`--`). Do you have any thoughts on why that might be? – Arthur Morris Feb 01 '16 at 21:28
  • 1
    @ArthurMorris you're welcome! I don't really know, I tested (successfully) with the inline delimiter fisrt. Are you sure that the comment started by `--` ends by a end-of-line ? Or do you have any error message ? – mgc Feb 01 '16 at 21:46
  • The comment seems to end with a new line, but I maybe wrong. The error I get is: `Syntax error, expecting one of the following: ;, !, !!, &, (, *, **, +, -, '.', /, <, <=, <>, =, >, >=, AND, EQ, EQT, EXCEPT, GE, GET, GROUP, GT, GTT, HAVING, INTERSECT, LE, LET, LT, LTT -2147295222 ERROR 22-322` – Arthur Morris Feb 01 '16 at 22:30
  • thanks again for the answer! I'll debug further myself. – Arthur Morris Feb 01 '16 at 23:15
  • It turns out that my problem is due to the fact that the database I'm querying uses SAS SQL so the `--` comments are not recognized as comments. I guess the lesson is that the comment format within the SQL query is determined by the system that is processing the query and not by Python. – Arthur Morris Feb 03 '16 at 16:54
  • I guess you're right, I tested my answer with pandas, using a `connection` object from `sqlite3` module (that's also why I have put a link to SQLite doc as you mentioned it), but other db manager may have different behaviors (anyway i think that `--` is the reference symbol for comments in SQL specifications) – mgc Feb 03 '16 at 17:49
  • Once I have enough reputation I'll give you an upvote! – Arthur Morris Feb 11 '16 at 03:20