본문으로 바로가기
본문으로 바로가기

sumWithOverflow

sumWithOverflow

도입 버전: v1.1

숫자 값들의 합을 계산하며, 결과에는 입력 매개변수와 동일한 데이터 타입을 사용합니다. 합이 이 데이터 타입의 최대값을 초과하는 경우, 오버플로우가 발생하도록 계산합니다.

구문

sumWithOverflow(num)

인수

반환 값

값들의 합입니다. (U)Int* 또는 Float* 또는 Decimal*

예시

UInt16에서 오버플로 발생 시 동작 시연

CREATE TABLE employees
(
    id UInt32,
    name String,
    monthly_salary UInt16 -- selected so that the sum of values produces an overflow
)
ENGINE = Memory;

INSERT INTO employees VALUES
    (1, 'John', 20000),
    (2, 'Jane', 18000),
    (3, 'Bob', 12000),
    (4, 'Alice', 10000),
    (5, 'Charlie', 8000);

-- Query for the total amount of the employee salaries using the sum and sumWithOverflow functions and show their types using the toTypeName function
-- For the sum function the resulting type is UInt64, big enough to contain the sum, whilst for sumWithOverflow the resulting type remains as UInt16.

SELECT
    sum(monthly_salary) AS no_overflow,
    sumWithOverflow(monthly_salary) AS overflow,
    toTypeName(no_overflow),
    toTypeName(overflow)
FROM employees;
┌─no_overflow─┬─overflow─┬─toTypeName(no_overflow)─┬─toTypeName(overflow)─┐
│       68000 │     2464 │ UInt64                  │ UInt16               │
└─────────────┴──────────┴─────────────────────────┴──────────────────────┘