|
| 1 | +# Copyright (c) 2024 Snowflake Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +Tests for DECFLOAT data type support and decimal precision configuration. |
| 17 | +""" |
| 18 | + |
| 19 | +import os |
| 20 | +from decimal import getcontext |
| 21 | + |
| 22 | +import pytest |
| 23 | + |
| 24 | +pytestmark = [ |
| 25 | + pytest.mark.integration, |
| 26 | + pytest.mark.qa_only, |
| 27 | +] |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def reset_decimal_precision(): |
| 32 | + """Reset decimal precision before and after each test.""" |
| 33 | + original_prec = getcontext().prec |
| 34 | + yield |
| 35 | + getcontext().prec = original_prec |
| 36 | + |
| 37 | + |
| 38 | +def test_decfloat_values(runner, reset_decimal_precision): |
| 39 | + """Test DECFLOAT type with various value types: positive/negative, maximum/minimum, floating point.""" |
| 40 | + |
| 41 | + sql = """ |
| 42 | + SELECT |
| 43 | + CAST('123456789012345678901234567890.123456789' AS DECFLOAT) AS positive_value, |
| 44 | + CAST('-123456789012345678901234567890.123456789' AS DECFLOAT) AS negative_value, |
| 45 | + CAST('3.14159265358979323846264338327950288419' AS DECFLOAT) AS floating_point, |
| 46 | + CAST('99999999999999999999999999999999999999e16384' AS DECFLOAT) AS maximum_value, |
| 47 | + CAST('-99999999999999999999999999999999999999e16384' AS DECFLOAT) AS minimum_value, |
| 48 | + """ |
| 49 | + |
| 50 | + result = runner.invoke_with_connection_json(["sql", "-q", sql]) |
| 51 | + assert result.exit_code == 0, f"Failed to select DECFLOAT values: {result.output}" |
| 52 | + |
| 53 | + # Verify JSON response contains expected values |
| 54 | + assert len(result.json) == 1 |
| 55 | + row = result.json[0] |
| 56 | + |
| 57 | + # Assert exact values that Snowflake returns for DECFLOAT |
| 58 | + assert row["POSITIVE_VALUE"] == "1.234567890123456789012345679E+29" |
| 59 | + assert row["NEGATIVE_VALUE"] == "-1.234567890123456789012345679E+29" |
| 60 | + assert ( |
| 61 | + row["FLOATING_POINT"] == "3.141592653589793238462643383" |
| 62 | + ) # value is rounded up to 28 numbers |
| 63 | + assert row["MAXIMUM_VALUE"] == "1.000000000000000000000000000E+16422" |
| 64 | + assert row["MINIMUM_VALUE"] == "-1.000000000000000000000000000E+16422" |
| 65 | + |
| 66 | + |
| 67 | +def test_decfloat_values_precision_38(runner, reset_decimal_precision): |
| 68 | + """Test DECFLOAT type with precision=38 for higher precision decimal operations.""" |
| 69 | + |
| 70 | + sql = """ |
| 71 | + SELECT |
| 72 | + CAST('123456789012345678901234567890.123456789' AS DECFLOAT) AS positive_value, |
| 73 | + CAST('-123456789012345678901234567890.123456789' AS DECFLOAT) AS negative_value, |
| 74 | + CAST('3.14159265358979323846264338327950288419' AS DECFLOAT) AS floating_point, |
| 75 | + CAST('99999999999999999999999999999999999999e16384' AS DECFLOAT) AS maximum_value, |
| 76 | + CAST('-99999999999999999999999999999999999999e16384' AS DECFLOAT) AS minimum_value |
| 77 | + """ |
| 78 | + |
| 79 | + result = runner.invoke_with_connection_json( |
| 80 | + ["sql", "-q", sql, "--decimal-precision", "38"] |
| 81 | + ) |
| 82 | + assert ( |
| 83 | + result.exit_code == 0 |
| 84 | + ), f"Failed to select DECFLOAT values with precision=38: {result.output}" |
| 85 | + |
| 86 | + assert len(result.json) == 1 |
| 87 | + row = result.json[0] |
| 88 | + |
| 89 | + assert row["POSITIVE_VALUE"] == "123456789012345678901234567890.12345679" |
| 90 | + assert row["NEGATIVE_VALUE"] == "-123456789012345678901234567890.12345679" |
| 91 | + assert row["FLOATING_POINT"] == "3.1415926535897932384626433832795028842" |
| 92 | + assert row["MAXIMUM_VALUE"] == "9.9999999999999999999999999999999999999E+16421" |
| 93 | + assert row["MINIMUM_VALUE"] == "-9.9999999999999999999999999999999999999E+16421" |
| 94 | + |
| 95 | + |
| 96 | +def test_decimal_precision_environment_variable(runner, reset_decimal_precision): |
| 97 | + """Test decimal precision using SNOWFLAKE_DECIMAL_PRECISION environment variable.""" |
| 98 | + |
| 99 | + sql = """ |
| 100 | + SELECT |
| 101 | + CAST('1234.56789012345678901234567890' AS DECFLOAT) AS test_value, |
| 102 | + CAST('3.14159265358979323846' AS DECFLOAT) AS pi_value |
| 103 | + """ |
| 104 | + |
| 105 | + original_env = os.environ.get("SNOWFLAKE_DECIMAL_PRECISION") |
| 106 | + os.environ["SNOWFLAKE_DECIMAL_PRECISION"] = "10" |
| 107 | + |
| 108 | + try: |
| 109 | + result = runner.invoke_with_connection_json(["sql", "-q", sql]) |
| 110 | + assert ( |
| 111 | + result.exit_code == 0 |
| 112 | + ), f"Failed to execute SQL with env var precision: {result.output}" |
| 113 | + |
| 114 | + assert len(result.json) == 1 |
| 115 | + row = result.json[0] |
| 116 | + |
| 117 | + assert row["TEST_VALUE"] == "1234.567890" |
| 118 | + assert row["PI_VALUE"] == "3.141592654" |
| 119 | + |
| 120 | + finally: |
| 121 | + if original_env is not None: |
| 122 | + os.environ["SNOWFLAKE_DECIMAL_PRECISION"] = original_env |
| 123 | + else: |
| 124 | + os.environ.pop("SNOWFLAKE_DECIMAL_PRECISION", None) |
| 125 | + |
| 126 | + |
| 127 | +def test_decimal_precision_param_overrides_env(runner, reset_decimal_precision): |
| 128 | + """Test that CLI parameter takes precedence over environment variable.""" |
| 129 | + |
| 130 | + sql = """ |
| 131 | + SELECT |
| 132 | + CAST('1234.56789012345678901234567890' AS DECFLOAT) AS test_value, |
| 133 | + CAST('3.14159265358979323846' AS DECFLOAT) AS pi_value |
| 134 | + """ |
| 135 | + |
| 136 | + original_env = os.environ.get("SNOWFLAKE_DECIMAL_PRECISION") |
| 137 | + os.environ["SNOWFLAKE_DECIMAL_PRECISION"] = "25" |
| 138 | + |
| 139 | + try: |
| 140 | + result = runner.invoke_with_connection_json( |
| 141 | + ["sql", "-q", sql, "--decimal-precision", "5"] |
| 142 | + ) |
| 143 | + assert ( |
| 144 | + result.exit_code == 0 |
| 145 | + ), f"Failed to execute SQL with param override: {result.output}" |
| 146 | + |
| 147 | + assert len(result.json) == 1 |
| 148 | + row = result.json[0] |
| 149 | + |
| 150 | + assert row["TEST_VALUE"] == "1234.6" |
| 151 | + assert row["PI_VALUE"] == "3.1416" |
| 152 | + |
| 153 | + finally: |
| 154 | + if original_env is not None: |
| 155 | + os.environ["SNOWFLAKE_DECIMAL_PRECISION"] = original_env |
| 156 | + else: |
| 157 | + os.environ.pop("SNOWFLAKE_DECIMAL_PRECISION", None) |
0 commit comments