question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pypika messes up Interval dates when using MySQL queries:

from pypika import functions as fn

fruits = Tables('fruits')
q = MySQLQuery.from_(fruits) \
    .select(fruits.id, fruits.name) \
    .where(fruits.harvest_date + Interval(months=1) < fn.Now())
str(q)
SELECT `fruits`.`id`,`fruits`.`name` FROM `fruits` JOIN `consumers` ON `fruits`.`consumer_id`=`consumers`.`id` WHERE `fruits`.`date`>=NOW()-INTERVAL `1 DAY`

I believe that the problem is in line 1080 of terms.py:

return self.templates.get(dialect, 'INTERVAL \'{expr} {unit}\'')

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ricardoorfaocommented, Mar 26, 2019
from pypika import functions as fn
from pypika import Tables, MySQLQuery, Interval

fruits, consumers = Tables('fruits', 'consumers')
q = MySQLQuery.from_(fruits) \
    .join(consumers) \
    .on(fruits.consumer_id == consumers.id) \
    .select(fruits.id, fruits.name) \
    .where((fruits.harvest_date + Interval(days=1)) < fn.Now())
print(q.get_sql())

Result:

SELECT `fruits`.`id`,`fruits`.`name` FROM `fruits` JOIN `consumers` ON `fruits`.`consumer_id`=`consumers`.`id` WHERE `fruits`.`harvest_date`+INTERVAL '1 DAY'<NOW()

Expected result:

SELECT `fruits`.`id`,`fruits`.`name` FROM `fruits` JOIN `consumers` ON `fruits`.`consumer_id`=`consumers`.`id` WHERE `fruits`.`harvest_date`+INTERVAL 1 DAY<NOW()

I also noticed that the desired result is showed if str(q) is used instead of q.get_sql().

0reactions
Robbie-Palmercommented, Jun 14, 2022

I’m having the same issue with using Interval only using str(q) doesn’t solve my problem as I’m generating SQL for GCP BigQuery which requires me to remove all quote chars with q.get_sql(quote_char=None) From Interval(quarters=1) I’m looking to generate INTERVAL 1 QUARTER instead of INTERVAL '1 QUARTER'

I can’t do a simple query_str.replace("'", str()) because my query contains CAST('inf' AS FLOAT64) So my current solution is some regex to strip the ' specifically from INTERVAL statements re.sub("(INTERVAL '[0-9]+ [A-Z]\w+')", lambda x:x.group(0).replace("'", str()), query_str)

Read more comments on GitHub >

github_iconTop Results From Across the Web

An Introduction To MySQL Interval
This tutorial shows you step by step how to use the MySQL interval for date and time arithmetic with many practical examples.
Read more >
MySQL INTERVAL() function - w3resource
MySQL INTERVAL () function returns the index of the argument that is more than the first argument. ... It returns 0 if 1st...
Read more >
MySQL 8.0 Reference Manual :: 12.7 Date and Time Functions
12.7 Date and Time Functions ; DATE(), Extract the date part of a date or datetime expression ; DATE_ADD(), Add time values (intervals)...
Read more >
MySQL Interval - Javatpoint
MySQL interval is an operator, which is based on the binary search algorithm to search the items and returns the value from 0...
Read more >
Working with INTERVAL and CURDATE in MySQL
I'm building a chart and I want to receive data for each month. Here's my first request which is working: SELECT s.GSP_nom AS...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found