Skip to content

Commit e3ca520

Browse files
committed
Mention type precedence in COALESCE
1 parent bb06872 commit e3ca520

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

content/en/docs/refguide/modeling/domain-model/oql/oql-expression-syntax.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ FROM Sales.Order
625625
| Doe | 2 | 5.0 | Regular |
626626
| Moose | 3 | 8.2 | Priority |
627627

628-
If result expressions have different numeric types, date type of the result expression in the first WHEN has priority, and the whole CASE expression has type of that result expression. This behavior matches the behavior of supported database vendors.
628+
If result expressions have different numeric types, data type of the result expression is defined based on [type coercion precedence](#type-coercion).
629629

630630
```sql
631631
SELECT
@@ -643,11 +643,15 @@ SELECT
643643
FROM Sales.Order
644644
```
645645

646-
| LastName | Number | Price | PriceOrNumber (type: Decimal) | NumberOrPrice (type: Integer) |
646+
| LastName | Number | Price | PriceOrNumber (type: Decimal) | NumberOrPrice (type: Decimal) |
647647
|:---------|-------:|------:|--------------:|--------------:|
648-
| Doe | 7 | 1.5 | 1.5 | 7 |
649-
| Doe | 2 | 5.0 | 5.0 | 2 |
650-
| Moose | 3 | 8.2 | 3.0 | 8 |
648+
| Doe | 7 | 1.5 | 1.5 | 7.0 |
649+
| Doe | 2 | 5.0 | 5.0 | 2.0 |
650+
| Moose | 3 | 8.2 | 3.0 | 8.0 |
651+
652+
{{% alert color="info" %}}
653+
In OQL v1, the expression gets the type of the first argument. If you use OQL v1, the type of `NumberOrPrice` in the example above is Integer, not Decimal.
654+
{{% /alert %}}
651655

652656
### Operator Precedence
653657

@@ -796,7 +800,7 @@ SELECT COALESCE(LastName, FirstName) AS Name FROM Sales.Customer
796800
| Doe |
797801
| Jane |
798802

799-
If arguments of `COALESCE` have different numeric types, the expression gets the type of the first argument. This behavior matches the behavior of supported database vendors.
803+
If arguments of `COALESCE` have different numeric types, the result type of the operation depends on OQL version. Consider the following query:
800804

801805
```sql
802806
SELECT
@@ -805,10 +809,16 @@ SELECT
805809
FROM Sales.Customer
806810
```
807811

808-
| AgeOrAmount (type: Integer) | AmountOrAge (type: Decimal) |
812+
If all arguments have different numeric types, data type of the result expression is defined based on [type coercion precedence](#type-coercion).
813+
814+
| AgeOrAmount (type: Decimal) | AmountOrAge (type: Decimal) |
809815
|------:|------:|
810-
| 25 | 25.0 |
811-
| 42 | 42.3 |
816+
| 25.0 | 25.0 |
817+
| 42.3 | 42.3 |
818+
819+
{{% alert color="info" %}}
820+
In OQL v1, the expression gets the type of the first argument. If you use OQL v1, the type of `AgeOrAmount` in the example above is Integer, not Decimal.
821+
{{% /alert %}}
812822

813823
### DATEDIFF {#datediff-function}
814824

content/en/docs/refguide/modeling/domain-model/oql/oql-v2.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ In OQL v2, all result expressions of a [CASE](/refguide/oql-expression-syntax/#c
4848

4949
Also, in OQL v2 it is no longer possible to have Null as the result of all result expressions of CASE. In OQL v1, that was allowed, but would lead to database-level exceptions for some database vendors.
5050

51-
Numeric types Integer, Long and Decimal are considered matching. See [examples](/refguide/oql-expression-syntax/#case-expression-examples) of `CASE` expressions for details on how different numeric types are combined.
51+
Numeric types Integer, Long and Decimal are considered matching. The type of the resulting expression is defined according to [type coercion precedence](/refguide/oql-expression-syntax/#type-coercion). In OQL v1, the type of the expression was defined based on the first result expression of `CASE`.
5252

5353
#### `COALESCE` {#coalesce-validations}
5454

@@ -58,7 +58,7 @@ In OQL v2, all arguments of a [COALESCE](/refguide/oql-expression-syntax/#coales
5858

5959
Also, in OQL v2 it is no longer possible to have a COALESCE expression where all arguments are Null literals. In OQL v1, that was allowed, but would lead to database-level exceptions for some database vendors.
6060

61-
Numeric types Integer, Long and Decimal are considered matching. See [examples](/refguide/oql-expression-syntax/#coalesce-expression-examples) of `COALESCE` for details on how different numeric types are combined.
61+
Numeric types Integer, Long and Decimal are considered matching. The type of the resulting expression is defined according to [type coercion precedence](/refguide/oql-expression-syntax/#type-coercion). In OQL v1, the type of the expression was defined based on the first argument of `COALESCE`.
6262

6363
#### `LENGTH` {#length-validations}
6464

@@ -229,15 +229,11 @@ SELECT Attribute1 + Attribute2 AS SumAttr
229229
FROM Module.Entity
230230
```
231231

232-
In OQL v1, the result of the arithmetic operation will always be of type pf the first attribute in the expression because it is handled by the database. Therefore, the result would depend on the underlying database engine.
232+
In OQL v1, the result of the arithmetic operation will always be of type of the first attribute in the expression. Therefore, the result would depend on the underlying database engine.
233233

234-
When handling numeric types in OQL v2 (Integer, Long, and Decimal), the result of the operation is always the most precise attribute type, using the following precedence:
234+
When handling numeric types in OQL v2 (Integer, Long, and Decimal), the result type is defined according to [type coercion precedence](/refguide/oql-expression-syntax/#type-coercion).
235235

236-
* Decimal (highest)
237-
* Long
238-
* Integer
239-
240-
If any side of the operation is of a non-numeric type, no casting is performed, and the result is handled by the database, as in OQL v1. See [Expression syntax](/refguide/oql-expression-syntax/#type-coercion) for more information.
236+
If any side of the operation is of a non-numeric type, no casting is performed, and the result is handled by the database, as in OQL v1.
241237

242238
### The Result Type of `ROUND` Is Now `Decimal`
243239

0 commit comments

Comments
 (0)