Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clarify type casting in CESQL spec #1281

Merged
35 changes: 34 additions & 1 deletion cesql/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ Similarly, whenever the left operand of the OR operation evaluates to `true`, th
| `x LIKE pattern: String x String -> Boolean` | Returns `true` if the value x matches the `pattern` |
| `x NOT LIKE pattern: String x String -> Boolean` | Same as `NOT (x LIKE PATTERN)` |

The pattern of the `LIKE` operator can contain:
The pattern of the `LIKE` operator MUST be a string literal, and can contain:

- `%` represents zero, one, or multiple characters
- `_` represents a single character
Expand All @@ -278,6 +278,10 @@ For example, the pattern `_b*` will accept values `ab`, `abc`, `abcd1` but won't
Both `%` and `_` can be escaped with `\`, in order to be matched literally. For example, the pattern `abc\%` will match
`abc%` but won't match `abcd`.

In cases where the left operand is not a `String`, it MUST be cast to a `String` before the comparison is made.
The pattern of the `LIKE` operator (that is, the right operand of the operator) MUST be a valid string literal without casting,
otherwise the parser MUST return a parse error.

#### 3.4.4. Exists operator

| Definition | Semantics |
Expand Down Expand Up @@ -353,6 +357,35 @@ left operand of the OR operation evalues to `true`, the right operand MUST NOT b

#### 3.7. Type casting

The following table indicates which type casts a CESQL engine MUST or MUST NOT support:

| Type | Integer | String | Boolean |
| ------- | ------- | ------ | ------- |
| Integer | N/A | MUST | MUST |
| String | MUST | N/A | MUST |
| Boolean | MUST | MUST | N/A |

For all of the type casts which a CESQL engine MUST support, the semantics which the engine MUST use are defined as follows:

| Definition | Semantics |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `Integer -> String` | Returns the string representation of the integer value in base 10, without leading `0`s. If the value is less than 0, the '-' character is prepended to the result. |
| `Integer -> Boolean` | Returns `false` if the integer is `0`, and `true` otherwise. |
| `String -> Integer` | Returns the result of interpreting the string as a 32 bit base 10 integer. The string MAY begin with a leading sign '+' or '-'. If the result will overflow or the string is not a valid integer an error is returned along with a value of `0`. |
| `String -> Boolean` | Returns `true` or `false` if the lower case representation of the string is exactly "true" or "false, respectively. Otherwise returns an error along with a value of `false` |
| `Boolean -> Integer` | Returns `1` if the boolean is `true`, and `0` if the boolean is `false`. |
| `Boolean -> String` | Returns `"true"` if the boolean is `true`, and `"false"` if the boolean is `false`. |

An example of how _Boolean_ values cast to _String_ combines with the case insensitivity of CESQL keywords is that:
```
TRUE = "true" AND FALSE = "false"
```
will evaluate to `true`, while
```
TRUE = "TRUE" OR FALSE = "FALSE"
```
will evaluate to `false`.

When the argument types of an operator/function invocation don't match the signature of the operator/function being invoked, the CESQL engine MUST try to perform an implicit cast.

This section defines an **ambiguous** operator/function as an operator/function that is overloaded with another
Expand Down
Loading