Skip to content

Commit

Permalink
clarify type casting in CESQL spec (#1281)
Browse files Browse the repository at this point in the history
* clarify type casting in CESQL spec

Signed-off-by: Calum Murray <[email protected]>

* fix: addressed feedback from meeting

Signed-off-by: Calum Murray <[email protected]>

* address review feedback

Signed-off-by: Calum Murray <[email protected]>

* clarify the default values when an error occurs

Signed-off-by: Calum Murray <[email protected]>

* fix: may -> MAY

Signed-off-by: Calum Murray <[email protected]>

* clarify second operand in LIKE expression MUST be a string literal

Signed-off-by: Calum Murray <[email protected]>

* clarify leading zeros

Signed-off-by: Calum Murray <[email protected]>

* add table of supported and unsupported type casts

Signed-off-by: Calum Murray <[email protected]>

* add boolean <-> integer casting

Signed-off-by: Calum Murray <[email protected]>

---------

Signed-off-by: Calum Murray <[email protected]>
  • Loading branch information
Cali0707 authored May 30, 2024
1 parent e79d7b2 commit 66c067b
Showing 1 changed file with 34 additions and 1 deletion.
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

0 comments on commit 66c067b

Please sign in to comment.