influxdb/influxdb — pull request #1210 by benbjohnson (2014-12-11 15:59:54+00:00)
Title: Add constant folding.

## Overview

This pull request adds constant folding which collapses constant expressions (including `now()`) into a simplified expression. This is a useful optimization for minimizing numeric binary expression operations, however, more importantly it simplifies the handling of time range expressions such as:

```
SELECT count() FROM cpu WHERE time > now() - 1h
```

This query will get folded into a simpler expression at execution time:

```
# Assuming current time is Jan 1, 2000 00:00:00
SELECT count() FROM cpu WHERE time > "2000-01-01 00:00:00"
```

See line notes for more details.

## Supported Types

The following binary expression combinations can be folded into a constant:

```
NUMBER + NUMBER (arithmetic, comparative)
BOOLEAN + BOOLEAN (equality, logical)
TIME + TIME (subtraction, comparative)
TIME + DURATION (addition, subtraction)
DURATION + DURATION (addition, subtraction, comparative)
DURATION + NUMBER (multiplication, division)
STRING + STRING (concatenation)
```

Also, the `now()` function call is evaluated to the current time at the beginning of query execution. This is to ensure that the time does not skew during the course of execution.

---

/cc @pauldix @toddboom