Operator Precedence
The following table lists all SQL operators from highest to lowest precedence, alongside with their associativity. The precedence and associativity of the operators is hard-coded into the parser. Add parentheses if you want an expression with multiple operators to be interpreted in some other way than what the precedence rules imply.:: Type cast
Use can use the:: operator instead of the CAST function to convert one data type to another.
Syntax
Example
Arithmetic with numbers
In arithmetic operations like +, -, *, and / , the result’s data type aligns with the most encompassing type of the operands indicated as “Corresponding type” in the table above. For clarity:
- When both operands are of the same data type (e.g., two INTEGERs or two NUMERICs), the result will also be of that same data type.
INTEGER <op> INTEGER = INTEGERINTEGER <op> BIGINT = BIGINT
- For operations involving two different numeric data types, the result will typically be of the more precise or larger data type.
INTEGER <op> REAL = DOUBLE PRECISION
- Overflow checks and floating point errors are applied according to the result data type only.
Floating point precision means that the representation of a number is accurate up to a certain number of digits. In Firebolt,
When performing arithmetic, the number of leading digits in the output is the product of the leading digits in both inputs. This means that if either or both of the input numbers are larger than 6, those numbers are the first truncated, and then the arithmetic is performed.
REAL data types have 6-digit precision and DOUBLE PRECISION have 16-digit precision. This means that calculations have a precision of 6 or 16 respectively, and numbers are truncated to that precision. For example, if a number is stored as 1.234567, it is automatically truncated to 1.23456 for REAL.When performing arithmetic, the number of leading digits in the output is the product of the leading digits in both inputs. This means that if either or both of the input numbers are larger than 6, those numbers are the first truncated, and then the arithmetic is performed.
Arithmetic with date/time and intervals
Aninterval represents a duration.
In Firebolt, values of type interval can be used to add or subtract a duration to/from a date or timestamp.
Interval cannot be used as the data type of a column.
The
+ and * operators shown above come in commutative pairs (e.g., both DATE + interval and interval + DATE are accepted).
Literal string interpretation
Interval literals can be specified in two formats.
Format examples
Cast from text literalThe following examples demonstrate different ways to cast a text literal to an interval in SQL. Each method converts a string representing a time interval into the
INTERVAL data type using various casting syntaxes.
Using interval keyword:
CAST function:
direction can be either ago or left empty. Using ago negates all specified quantities. The quantity represents a signed or unsigned integer, and unit refers to one of the following time units, matched case-insensitively:
Each
unit can appear only once in an interval literal.
The value of the interval is determined by adding the quantities of the specified units with the appropriate signs.
Unit outside of text literalThe following example demonstrates how to cast a numeric value to an interval by placing the unit outside of the text literal.
N represents a signed or unsigned integer, and unit specifies the time unit, matched case-insensitively, from the following options:
Arithmetic between interval and TIMESTAMPTZ
Interval arithmetic withTIMESTAMPTZ values works as follows:
- Convert the
TIMESTAMPTZvalue from Unix time to local time according to the rules of the time zone specified by the session’stime_zonesetting. - Add the
millennium,century,decade,year,month,weekanddaycomponents of the interval to the local time. - Convert the local time back to Unix time according to the rules of the time zone specified by the session’s
time_zonesetting. - Add the
hour,minute,second,millisecond, andmicrosecondcomponents of the interval to the Unix time.
SELECT TIMESTAMPTZ '2022-10-30 Europe/Berlin' + interval '1 day' returns 2022-10-31 00:00:00+01 but SELECT TIMESTAMPTZ '2022-10-30 Europe/Berlin' + interval '24 hours' returns 2022-10-30 23:00:00+01 (assuming the value of the session’s time_zone setting is 'Europe/Berlin').
Still, the dependence on the session’s time_zone setting should be kept in mind when doing arithmetic between interval and TIMESTAMPTZ.
Multiplying an interval by a scalar
You can use the expressiondate_time + INTERVAL * d where date_time is a constant or column reference of type DATE, TIMESTAMP, or TIMESTAMPTZ, and d is a constant or column reference of type DOUBLE PRECISION.
The effect is that the INTERVAL is scaled by d, and the resulting INTERVAL is added to date_time.
E.g., INTERVAL '1 day' * 3 -> INTERVAL '3 days'.
Examples
Rows: 1Execution time: 5ms
Rows: 1Execution time: 5ms
Rows: 1Execution time: 5ms
Rows: 1Execution time: 5ms
'Europe/Berlin':
Logical
Logical operators return the result of a boolean operation using three valued logicComparison
Example of using comparison operator in
WHERE clause
CASE
Conditional expression similar to if-then-else statements. If the result of the condition is true, then the value of the CASE expression is the result that follows the condition. If the result is false, any subsequent WHEN clauses (conditions) are searched in the same manner. If no WHEN condition is true, then the value of the case expression is the result specified in the ELSE clause. If the ELSE clause is omitted and no condition matches, the result is NULL.Syntax
Parameters
Return type
Same data type as<result>
Example
This example references a tableplayer_level with the following columns and values:
The following example categorizes each entry by length. If the movie is longer than zero minutes and less than 50 minutes it is categorized as SHORT. When the length is 50-120 minutes, it’s categorized as Medium, and when even longer, it’s categorized as Long.
String
To concatenate strings, you can use theCONCAT function.
Rows: 1Execution time: 5ms
|| operator.
Rows: 1Execution time: 5ms