数値データ型¶
このトピックでは、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桁未満であっても、有効数字の最小桁が10進数の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はこの3つをすべて64ビット浮動小数点数として扱います。
精度¶
Snowflakeは、倍精度(64ビット) IEEE 754浮動小数点数を使用します。
精度は約15桁です。たとえば、整数の場合、範囲は-9007199254740991から+9007199254740991(-253 + 1から+253 - 1)です。浮動小数点値の範囲は、およそ10:sup:-308 から10:sup:+308 です。(Snowflakeは、約10 -324 と 10-308 の間のより極端な値を、より低い精度で表すことができます。)詳細については、 倍精度数に関するウィキペディアの記事 をご参照ください。
Snowflakeは固定小数点データ型 NUMBER をサポートしているため、指数の範囲は狭くなりますが、精度が向上します。
特別な価値¶
Snowflakeは、 FLOAT に対して次の特別な値をサポートしています。
'NaN'(非数)'inf'(無限大)'-inf'(負の無限大)
記号 'NaN'、 'inf'、および '-inf' は一重引用符で囲む必要があり、大文字と小文字は区別されません。
'NaN' の比較セマンティクスは、次の点で IEEE 754標準と異なります。
条件 |
Snowflake |
IEEE 754 |
コメント |
|---|---|---|---|
|
|
|
Snowflakeでは、 |
|
|
|
Snowflakeでは、 |
丸め誤差¶
浮動小数点操作には、最下位桁に小さな丸めエラーが生じることがあります。丸めエラーは、三角関数、統計関数、地理空間関数を含む、あらゆるタイプの浮動小数点処理で発生する可能性があります。
丸めエラーに関する注意事項は以下のとおりです。
エラーは、クエリが実行されるたびに変わる可能性があります。
オペランドの精度やスケールが異なると、エラーが大きくなる可能性があります。
特に集計関数(例: SUM や AVG)が大量の行を処理する場合は、エラーが蓄積する可能性があります。集約する前に固定小数点データ型にキャストすると、これらのエラーを削減または排除できます。
丸めエラーは、 SQL を操作する場合だけではなく、Snowflake内で実行される他のコード(例: Java、 JavaScript、またはPython)を操作する場合にも発生する可能性があります(例: UDFs および ストアドプロシージャ)。
2つの浮動小数点数を比較する場合、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までの1桁以上です。e(またはE)は、科学表記法の指数を示します。指数マーカーが存在する場合は、少なくとも1桁が指数マーカーの後に続く必要があります。
次の数値は、すべてサポートされている数値定数の例です。
15
+1.34
0.2
15e-03
1.234E2
1.234E+2
-1