숫자 데이터 타입¶
이 항목에서는 숫자 상수/리터럴에 대해 지원되는 형식과 함께, Snowflake에서 지원되는 숫자 데이터 타입에 대해 설명합니다.
고정 소수점 숫자의 데이터 타입¶
Snowflake는 고정 소수점 숫자에 대해 다음 데이터 타입을 지원합니다.
NUMBER¶
최대 38자리 숫자로, 다음과 같은 선택적 전체 자릿수와 소수 자릿수를 갖습니다.
- 전체 자릿수:
허용되는 총 자릿수입니다.
- 소수 자릿수:
소수점 오른쪽에 허용되는 자릿수입니다.
기본적으로 정밀도는 38이고 배율은 0입니다(즉, NUMBER(38, 0)). 정밀도는 주어진 유형의 열에 삽입하거나 형 변환할 수 있는 값의 범위를 제한합니다. 예를 들어, 999 값은 NUMBER(38,0)에는 적합하지만, NUMBER(2,0)에는 적합하지 않습니다.
전체 자릿수는 허용되는 총 자릿수이므로 소수점 왼쪽의 자릿수가 열의 전체 자릿수에서 배율을 뺀 값을 초과하는 경우 NUMBER 열에 값을 로딩할 수 없습니다. 예를 들어 NUMBER(20, 2)는 소수점 왼쪽에 18자리, 소수점 오른쪽에 2자리, 총 20자리까지 입력할 수 있습니다.
최대 소수 자릿수(소수점 오른쪽의 자릿수)는 37입니다. 유효 자릿수가 38자리보다 작지만 소수점 이하 자릿수가 37번째 자릿수를 지나가는 숫자(예: 0.0000000000000000000000000000000000000012(1.2e-39))는 몇 자리의 정밀도를 잃지 않고는 표현할 수 없습니다.
참고
데이터를 정밀도가 낮은 다른 데이터 타입으로 변환했다가 다시 정밀도가 높은 데이터 타입으로 변환하면 데이터의 정밀도가 떨어질 수 있습니다. 예를 들어, NUMBER(38,37) 값을 DOUBLE 값(소수점 이하 자릿수 정밀도 약 15)으로 변환한 다음 다시 NUMBER 로 변환하면 정밀도가 손실됩니다.
Snowflake는 FLOAT 데이터 타입도 지원하므로, 전체 자릿수는 줄어도 더 넓은 범위의 값을 허용합니다.
DECIMAL , DEC , NUMERIC¶
NUMBER와 동의어입니다.
INT , INTEGER , BIGINT , SMALLINT , TINYINT , BYTEINT¶
NUMBER 와 동일하지만 정밀도와 배율을 지정할 수 없다는 점을 예외로 합니다(즉, 항상 NUMBER(38, 0)으로 기본값이 지정됨). 따라서 모든 INTEGER 데이터 타입의 경우 값 범위는 -99999999999999999999999999999999999999에서 +99999999999999999999999999999999999999(포함)까지의 모든 정수 값입니다.
다양한 이름(TINYINT, BYTEINT 등)은 다른 시스템에서 포팅을 단순화하고 지정된 유형의 열에 대해 예상되는 값 범위를 제시하기 위한 것입니다.
전체 자릿수와 소수 자릿수가 저장소 크기에 미치는 영향¶
정밀도(총 자릿수)는 저장소에 영향을 미치지 않습니다. NUMBER(2,0) 및 NUMBER(38,0) 같이 정밀도가 다른 열에서 동일한 숫자에 대한 저장소 요구 사항은 동일합니다. 각 마이크로 파티션에 대해, Snowflake는 주어진 열의 최소값과 최대값을 결정하고 해당 정보를 사용하여 파티션에 있는 해당 열의 모든 값에 적합한 저장소 크기를 결정합니다. 예:
열에
-128에서+127사이의 값만 있는 경우에는 각각의 값이 1바이트(압축되지 않음)를 사용합니다.열에서 가장 큰 값이
10000000이면 각각의 값이 4바이트(압축되지 않음)를 사용합니다.
그러나 소수 자릿수(소수점 이하 자릿수)은 저장소에 영향을 미칩니다. 예를 들어, NUMBER(10,5) 유형의 열에 저장된 동일한 값은 NUMBER(5,0)보다 더 많은 공간을 소비합니다. 또한 규모가 큰 값을 처리할수록 속도가 약간 느려지고 메모리를 더 많이 사용할 수 있습니다.
공간을 절약하기 위해, Snowflake는 값을 압축한 후에 저장소에 기록합니다. 압축량은 데이터 값과 기타 요인에 따라 다릅니다.
테이블에 있는 고정 소수점 데이터 타입의 예¶
다음 문은 다양한 고정 소수점 데이터 타입의 열이 있는 테이블을 생성합니다.
CREATE OR REPLACE TABLE test_fixed(
num0 NUMBER,
num10 NUMBER(10,1),
dec20 DECIMAL(20,2),
numeric30 NUMERIC(30,3),
int1 INT,
int2 INTEGER);
DESC TABLE test_fixed;
+-----------+--------------+--------+-------+---------+-------------+------------+-------+------------+---------+-------------+----------------+
| name | type | kind | null? | default | primary key | unique key | check | expression | comment | policy name | privacy domain |
|-----------+--------------+--------+-------+---------+-------------+------------+-------+------------+---------+-------------+----------------|
| NUM0 | NUMBER(38,0) | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | NULL | NULL |
| NUM10 | NUMBER(10,1) | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | NULL | NULL |
| DEC20 | NUMBER(20,2) | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | NULL | NULL |
| NUMERIC30 | NUMBER(30,3) | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | NULL | NULL |
| INT1 | NUMBER(38,0) | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | NULL | NULL |
| INT2 | NUMBER(38,0) | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | NULL | NULL |
+-----------+--------------+--------+-------+---------+-------------+------------+-------+------------+---------+-------------+----------------+
부동 소수점 숫자의 데이터 타입¶
Snowflake는 부동 소수점 숫자에 대해 다음 데이터 타입을 지원합니다.
FLOAT , FLOAT4 , FLOAT8¶
FLOAT, FLOAT4, FLOAT8 은 다른 시스템과의 호환성을 위한 이름입니다. Snowflake는 이 세 가지를 모두 64비트 부동소수점 숫자로 취급합니다.
전체 자릿수¶
Snowflake는 배정밀도(64비트) IEEE 754 부동 소수점 숫자를 사용합니다.
전체 자릿수는 약 15자리입니다. 예를 들어, 정수의 범위는 -9007199254740991에서 -9007199254740991 ~ +9007199254740991(-253 + 1 to +253 - 1)입니다. 부동 소수점 값의 범위는 10-308 ~ 10+308 입니다. (Snowflake는 약 10:sup: -324 와 10-308 사이의 더 극단적인 값을 더 정밀하게 나타낼 수 있습니다.) 자세한 내용은 배정밀도 숫자에 대한 Wikipedia 문서 를 참조하십시오.
Snowflake는 고정 소수점 데이터 타입 NUMBER 를 지원하므로, 지수 범위가 더 작아도 더 큰 전체 자릿수를 허용합니다.
특수한 값¶
Snowflake는 FLOAT에 대해 다음과 같은 특수한 값을 지원합니다.
'NaN'(숫자가 아님).'inf'(무한대).'-inf'(음의 무한대).
'NaN', 'inf', '-inf' 기호는 작은따옴표로 묶어야 하며 대/소문자를 구분하지 않습니다.
'NaN' 에 대한 비교 의미 체계는 다음과 같은 식으로 IEEE 754 표준과는 다릅니다.
조건 |
Snowflake |
IEEE 754 |
설명 |
|---|---|---|---|
|
|
|
Snowflake에서는 |
|
|
|
Snowflake에서 |
반올림 오류¶
부동 소수점 연산자는 최하위 자릿수에서 작은 반올림 오류가 발생할 수 있습니다. 반올림 오류는 삼각함수, 통계 및 지리공간 함수를 포함한 모든 유형의 부동 소수점 처리에서 발생할 수 있습니다.
다음은 반올림 오류에 대한 고려 사항입니다.
오류는 쿼리가 실행될 때마다 다를 수 있습니다.
피연산자의 정밀도 또는 소수 자릿수가 다른 경우 더 큰 오류가 발생할 수 있습니다.
특히 집계 함수(예: SUM 또는 AVG)가 많은 수의 행을 처리하는 경우 오류가 누적될 수 있습니다. 집계하기 전에 고정 소수점 데이터 타입으로 캐스팅하면 이러한 오차를 줄이거나 없앨 수 있습니다.
반올림 오류는 SQL 을 사용할 때는 물론이고, Snowflake 내부(예: UDFs 및 저장 프로시저)에서 실행되는 다른 코드(예: Java, JavaScript 또는 Python)를 사용할 때도 발생할 수 있습니다.
두 개의 부동 소수점 숫자를 비교할 때 Snowflake는 정확히 같음이 아닌 대략적으로 같음을 사용하여 비교하는 것을 권장합니다.
It might be possible to avoid these types of approximation errors by using the exact DECFLOAT data type.
DOUBLE , DOUBLE PRECISION , REAL¶
FLOAT와 동의어입니다.
테이블에 있는 부동 소수점 데이터 타입의 예¶
다음 문은 다양한 부동 소수점 데이터 타입의 열이 있는 테이블을 생성합니다.
CREATE OR REPLACE TABLE test_float(
double1 DOUBLE,
float1 FLOAT,
dp1 DOUBLE PRECISION,
real1 REAL);
DESC TABLE test_float;
+---------+-------+--------+-------+---------+-------------+------------+-------+------------+---------+-------------+----------------+
| name | type | kind | null? | default | primary key | unique key | check | expression | comment | policy name | privacy domain |
|---------+-------+--------+-------+---------+-------------+------------+-------+------------+---------+-------------+----------------|
| DOUBLE1 | FLOAT | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | NULL | NULL |
| FLOAT1 | FLOAT | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | NULL | NULL |
| DP1 | FLOAT | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | NULL | NULL |
| REAL1 | FLOAT | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | NULL | NULL |
+---------+-------+--------+-------+---------+-------------+------------+-------+------------+---------+-------------+----------------+
참고
DESC TABLE 명령의 type 열은 FLOAT 뿐만 아니라 FLOAT 의 동의어(예: DOUBLE, DOUBLE PRECISION, REAL)에 대해서도 데이터 타입 FLOAT 를 표시합니다.
DECFLOAT¶
The decimal float (DECFLOAT) data type stores numbers exactly, with up to 38 significant digits of precision, and uses a dynamic base-10 exponent to represent very large or small values. The exponent range is from -16383 to 16384, allowing values roughly between -10^(16384) and 10^(16384). The DECFLOAT data type supports a variable scale so that the scale varies depending on the specific value being stored. In contrast to the FLOAT data type, which represents values as approximations, the DECFLOAT data type represents exact values in the specified precision.
The DECFLOAT data type doesn’t support the following special values
that are supported by the FLOAT data type: 'NaN' (not a number), 'inf' (infinity),
and '-inf' (negative infinity).
Use cases for the DECFLOAT data type¶
Use the DECFLOAT data type when you need exact decimal results and a wide, variable scale in the same column.
The DECFLOAT data type is appropriate for the following general use cases:
You are ingesting data, and the scale of incoming numeric values is unknown or highly variable.
You require exact numeric values; for example, ledgers, taxes, or compliance.
You are migrating from systems that rely on the IEEE 754-decimal representation or 128-bit decimals. These migrations might be blocked by the precision or range limitations of other Snowflake data types.
You want to avoid
Number out of representable rangeerrors when you sum, multiply, or divide high-precision numeric values.
For example, you can use the DECFLOAT data type for the following specific use cases:
You are ingesting heterogeneously-scaled data from Oracle DECIMAL or DB2 DECFLOAT columns.
You are performing financial modeling involving computations with scales of results that are hard to predict.
You are running scientific measurements that swing from nano units to astronomical units.
You can continue to use the NUMBER data type for fixed-scale numeric columns or the FLOAT data type for high-throughput analytics where imprecise results are acceptable.
Usage notes for the DECFLOAT data type¶
If an operation produces a result with more than 38 digits, the DECFLOAT value is rounded to 38-digit precision, with the least-significant digits rounded off according to the current rounding mode. Snowflake uses the half up rounding mode for DECFLOAT values.
When specifying a DECFLOAT value or casting to a DECFLOAT value, avoid using numeric literals in SQL. If you do this, the values are interpreted as NUMBER or FLOAT values before being cast to a DECFLOAT value, which can result in range errors or loss of exactness. Use either string literals — such as
SELECT '<value>'::DECFLOAT— or the DECFLOAT literal — such asSELECT DECFLOAT '<value>'.When operations mix DECFLOAT values and values of other numeric types, coercion prefers the DECFLOAT values. For example, when adding a value of NUMBER type and DECFLOAT type, the result is a DECFLOAT value.
Use of the DECFLOAT type might cause storage consumption to increase.
Drivers and driver versions that support the DECFLOAT data type¶
The following Snowflake drivers and driver versions support the DECFLOAT data type. You might need to update your drivers to the versions that support DECFLOAT:
Driver |
Minimum supported version |
Notes |
|---|---|---|
Snowflake Connector for Python |
3.14.1 |
pandas DataFrames don’t support the DECFLOAT type. |
ODBC |
3.12.0 |
|
JDBC |
3.27.0 |
|
Go Snowflake Driver |
1.17.0 |
Unsupported drivers treat DECFLOAT values as TEXT values. For some drivers, a driver parameter must be set to map the DECFLOAT type to a language-native type. For more information, see 드라이버.
Limitations for the DECFLOAT data type¶
The following limitations apply to the DECFLOAT type:
DECFLOAT values can’t be stored in VARIANT, OBJECT, or ARRAY values. To cast a DECFLOAT value to a VARIANT value, you can first cast it to a VARCHAR value and then to a VARIANT value.
DECFLOAT values aren’t supported in the following types of tables:
Tables in external formats, such as Iceberg
Hybrid tables
The DECFLOAT data type isn’t supported in Snowflake Scripting stored procedures. However, it is supported in Snowflake Scripting user-defined functions (UDFs).
The DECFLOAT data type isn’t supported in stored procedures or UDFs written in a language other than SQL (such as Python or Java).
The DECFLOAT data type isn’t supported in Snowpark.
Snowsight has limited support for the DECFLOAT data type.
The following features don’t support the DECFLOAT data type:
The NUMBER and FLOAT types might provide better performance than the DECFLOAT type.
Examples for the DECFLOAT data type¶
The following examples use the DECFLOAT data type:
Show the differences between DECFLOAT and FLOAT¶
Create a table with a DECFLOAT column and a FLOAT column, and insert the same values for both types into the table:
CREATE OR REPLACE TABLE decfloat_sample (
id INT,
decfloat_val DECFLOAT,
float_val FLOAT);
INSERT INTO decfloat_sample VALUES
(
1,
DECFLOAT '123e7000',
FLOAT '123e7000'
),
(
2,
12345678901234567890123456789::DECFLOAT,
12345678901234567890123456789::FLOAT
),
(
3,
'-4.2e-5432'::DECFLOAT,
'-4.2e-5432'::FLOAT
),
(
4,
'1.00000000000000000000000000000000000014'::DECFLOAT,
'1.00000000000000000000000000000000000014'::FLOAT
),
(
5,
'1.00000000000000000000000000000000000015'::DECFLOAT,
'1.00000000000000000000000000000000000015'::FLOAT
);
The statement inserts DECFLOAT values in the following ways:
The first value is inserted by using the DECFLOAT literal.
The second value is inserted by casting an INTEGER value to a DECFLOAT value.
The last three values are inserted by casting a VARCHAR value to a DECFLOAT value.
Describe the table to show the types. The precision wasn’t specified in the table definition for either column, but the output shows that the DECFLOAT data type supports up to 38 significant digits of precision:
DESC TABLE decfloat_sample;
+--------------+--------------+--------+-------+---------+-------------+------------+-------+------------+---------+-------------+----------------+
| name | type | kind | null? | default | primary key | unique key | check | expression | comment | policy name | privacy domain |
|--------------+--------------+--------+-------+---------+-------------+------------+-------+------------+---------+-------------+----------------|
| ID | NUMBER(38,0) | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | NULL | NULL |
| DECFLOAT_VAL | DECFLOAT(38) | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | NULL | NULL |
| FLOAT_VAL | FLOAT | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | NULL | NULL |
+--------------+--------------+--------+-------+---------+-------------+------------+-------+------------+---------+-------------+----------------+
Query the table to show the differences in the values:
SELECT * FROM decfloat_sample;
+----+-----------------------------------------+------------------------+
| ID | DECFLOAT_VAL | FLOAT_VAL |
|----+-----------------------------------------+------------------------|
| 1 | 1.23e7002 | inf |
| 2 | 12345678901234567890123456789 | 1.23456789012346e+28 |
| 3 | -4.2e-5432 | -0 |
| 4 | 1.0000000000000000000000000000000000001 | 1 |
| 5 | 1.0000000000000000000000000000000000002 | 1 |
+----+-----------------------------------------+------------------------+
The output shows the following differences:
The first row shows that the DECFLOAT type supports a wider range of values than the FLOAT type. The DECFLOAT value is very large (
1.23e7002). The FLOAT value isinf, which means that the value is larger than any value that the FLOAT type can represent.The second row shows that the DECFLOAT type retains the specified value exactly. The FLOAT value is an approximation that is stored in scientific notation.
The third row shows that the DECFLOAT type supports very small values (
-4.2e-5432). The FLOAT value is approximated to-0.The fourth and fifth rows show that the DECFLOAT type supports up to 38 digits of precision and uses rounding rules for values beyond the limit. The FLOAT value is approximated to
1in both rows.
Use DECFLOAT values with aggregate functions¶
The following example uses DECFLOAT values with aggregate functions. First, create a table and insert DECFLOAT values:
CREATE OR REPLACE TABLE decfloat_agg_sample (decfloat_val DECFLOAT);
INSERT INTO decfloat_agg_sample VALUES
(DECFLOAT '1e1000'),
(DECFLOAT '-2.47e999'),
(DECFLOAT '22e-75');
Query the table by using some aggregate functions:
SELECT SUM(decfloat_val),
AVG(decfloat_val),
MAX(decfloat_val),
MIN(decfloat_val)
FROM decfloat_agg_sample;
+-------------------+-------------------+-------------------+-------------------+
| SUM(DECFLOAT_VAL) | AVG(DECFLOAT_VAL) | MAX(DECFLOAT_VAL) | MIN(DECFLOAT_VAL) |
|-------------------+-------------------+-------------------+-------------------|
| 7.53e999 | 2.51e999 | 1e1000 | -2.47e999 |
+-------------------+-------------------+-------------------+-------------------+
숫자 상수¶
상수 (리터럴)라는 용어는 수정된 데이터 값을 나타냅니다. 숫자 상수에 대해 다음 형식이 지원됩니다.
[+-][digits][.digits][e[+-]digits]
여기서:
+또는-는 양수 또는 음수 값을 나타냅니다. 기본값은 양수입니다.digits는 0에서 9까지의 숫자 중 하나 이상의 숫자입니다.e(또는E)는 과학적 표기법의 지수를 나타냅니다. 지수 표시가 있는 경우 하나 이상의 숫자가 지수 표시 뒤에 와야 합니다.
다음 숫자는 지원되는 숫자 상수의 모든 예입니다.
15
+1.34
0.2
15e-03
1.234E2
1.234E+2
-1