Skip to content
30 changes: 30 additions & 0 deletions java/cds-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,36 @@ The framework isn't responsible for closing the stream when writing to the datab

These types are used for the values of CDS elements with primitive type. In the [Model Reflection API](./reflection-api), they're represented by the enum [CdsBaseType](https://javadoc.io/doc/com.sap.cds/cds4j-api/latest/com/sap/cds/reflect/CdsBaseType.html).

### Numeric Type Determination { #type-determination }

To have a consistent behavior across different databases, the CAP Java runtime applies numeric type determination in arithmetic expressions and numeric standard functions according to the following rules.
Comment on lines +74 to +76
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a brief introductory sentence to provide context before diving into the rules. For example:

### Numeric Type Determination { #type-determination }

The CAP Java runtime ensures consistent behavior across different databases by applying numeric type determination rules in arithmetic expressions and numeric standard functions.


::: tip
Use `type(CdsBaseType)` to explicitly set the result type if needed.
:::

#### Arithmetic Expressions { #numeric-promotion }

Arithmetic expressions promote numeric types according to the following precedence:

**Type Precedence (highest to lowest):**
`cds.Double`, `hana.REAL`, `cds.Decimal`, `cds.Int64`, `cds.Int32`, `cds.Int16`, `cds.UInt8`

- For addition, subtraction, and multiplication, the result type is the one with highest precedence among the operands.
- For division:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a colon after "For division" to maintain consistency with the formatting of "For addition, subtraction, and multiplication" above:

- For division:

- If any operand is an approximate numeric type (`cds.Double`, `hana.REAL`), the result type is `cds.Double`.
- Otherwise, the result type is `DECIMAL`, which provides higher accuracy for decimal fractions.

#### Numeric Standard Functions { #type-determination-functions }

Numeric aggregation and standard functions determine their result type based on the argument types:

- **ceiling(x)**, **floor(x)**, **round(x)**: Return the same type as the input `x`.
- **min(x)**, **max(x)**, **sum(x)**: Aggregate functions return the same type as the argument `x`.
- **average(x)**: Returns `DECIMAL` for exact numeric types and `DOUBLE` for approximate numeric types.
- **count(x)**, **countdistinct(x)**: return `INT64`.


## Structured Data

In CDS, structured data is used as payload of *Insert*, *Update*, and *Upsert* statements. Also the query result of *Select* may be structured.
Expand Down
Loading