--- /dev/null
+.. change::
+ :tags: bug, postgresql, regression
+ :tickets: 9701
+
+ Fixed regression caused by the fix for :ticket:`9618` where floating point
+ values would lose precision being inserted in bulk, using either the
+ psycopg2 or psycopg drivers.
+
+
+.. change::
+ :tags: bug, mssql
+
+ Implemented the :class:`_sqltypes.Double` type for SQL Server, having it
+ resolve to ``DOUBLE PRECISION``, or :class:`_mssql.DOUBLE_PRECISION`,
+ at DDL rendering time.
+
+
+.. change::
+ :tags: bug, oracle
+
+ Fixed issue in Oracle dialects where ``Decimal`` returning types such as
+ :class:`_sqltypes.Numeric` would return floating point values, rather than
+ ``Decimal`` objects, when these columns were used in the
+ :meth:`_dml.Insert.returning` clause to return INSERTed values.
DATETIME2,
DATETIMEOFFSET,
DECIMAL,
+ DOUBLE_PRECISION,
FLOAT,
IMAGE,
INTEGER,
.. autoclass:: DATETIMEOFFSET
:members: __init__
+.. autoclass:: DOUBLE_PRECISION
+ :members: __init__
.. autoclass:: IMAGE
:members: __init__
from .base import DATETIME2
from .base import DATETIMEOFFSET
from .base import DECIMAL
+from .base import DOUBLE_PRECISION
from .base import FLOAT
from .base import IMAGE
from .base import INTEGER
class REAL(sqltypes.REAL):
- __visit_name__ = "REAL"
+ """the SQL Server REAL datatype."""
def __init__(self, **kw):
# REAL is a synonym for FLOAT(24) on SQL server.
super().__init__(**kw)
+class DOUBLE_PRECISION(sqltypes.DOUBLE_PRECISION):
+ """the SQL Server DOUBLE PRECISION datatype.
+
+ .. versionadded:: 2.0.11
+
+ """
+
+ def __init__(self, **kw):
+ # DOUBLE PRECISION is a synonym for FLOAT(53) on SQL server.
+ # it is only accepted as the word "DOUBLE PRECISION" in DDL,
+ # the numeric precision value is not allowed to be present
+ kw.setdefault("precision", 53)
+ super().__init__(**kw)
+
+
class TINYINT(sqltypes.Integer):
__visit_name__ = "TINYINT"
"varbinary": VARBINARY,
"bit": BIT,
"real": REAL,
+ "double precision": DOUBLE_PRECISION,
"image": IMAGE,
"xml": XML,
"timestamp": TIMESTAMP,
return " ".join([c for c in (spec, collation) if c is not None])
+ def visit_double(self, type_, **kw):
+ return self.visit_DOUBLE_PRECISION(type_, **kw)
+
def visit_FLOAT(self, type_, **kw):
precision = getattr(type_, "precision", None)
if precision is None:
outconverter=lambda value: value.read(),
arraysize=len_params,
)
+ elif (
+ isinstance(type_impl, _OracleNumeric)
+ and type_impl.asdecimal
+ ):
+ out_parameters[name] = self.cursor.var(
+ decimal.Decimal,
+ arraysize=len_params,
+ )
+
else:
out_parameters[name] = self.cursor.var(
dbtype, arraysize=len_params
)
+class _PsycopgFloat(_PsycopgNumeric):
+ __visit_name__ = "float"
+
+
class _PsycopgHStore(HSTORE):
def bind_processor(self, dialect):
if dialect._has_native_hstore:
PGDialect.colspecs,
{
sqltypes.Numeric: _PsycopgNumeric,
+ sqltypes.Float: _PsycopgFloat,
HSTORE: _PsycopgHStore,
sqltypes.ARRAY: _PsycopgARRAY,
INT2VECTOR: _PsycopgINT2VECTOR,
return exclusions.closed()
+ @property
+ def float_or_double_precision_behaves_generically(self):
+ return exclusions.closed()
+
@property
def precision_generic_float_type(self):
"""target backend will return native floating point numbers with at
# mypy: ignore-errors
+from decimal import Decimal
+
+from . import testing
from .. import fixtures
from ..assertions import eq_
from ..config import requirements
from ..schema import Column
from ..schema import Table
+from ... import Double
+from ... import Float
+from ... import Identity
from ... import Integer
from ... import literal
from ... import literal_column
+from ... import Numeric
from ... import select
from ... import String
eq_(rall, pks.all())
+ @testing.combinations(
+ (Double(), 8.5514716, True),
+ (
+ Double(53),
+ 8.5514716,
+ True,
+ testing.requires.float_or_double_precision_behaves_generically,
+ ),
+ (Float(), 8.5514, False),
+ (
+ Float(8),
+ 8.5514,
+ True,
+ testing.requires.float_or_double_precision_behaves_generically,
+ ),
+ (
+ Numeric(precision=15, scale=12, asdecimal=False),
+ 8.5514716,
+ True,
+ testing.requires.literal_float_coercion,
+ ),
+ (
+ Numeric(precision=15, scale=12, asdecimal=True),
+ Decimal("8.5514716"),
+ False,
+ ),
+ argnames="type_,value,do_rounding",
+ )
+ @testing.variation("sort_by_parameter_order", [True, False])
+ @testing.variation("multiple_rows", [True, False])
+ def test_insert_w_floats(
+ self,
+ connection,
+ metadata,
+ sort_by_parameter_order,
+ type_,
+ value,
+ do_rounding,
+ multiple_rows,
+ ):
+ """test #9701.
+
+ this tests insertmanyvalues as well as decimal / floating point
+ RETURNING types
+
+ """
+
+ t = Table(
+ "t",
+ metadata,
+ Column("id", Integer, Identity(), primary_key=True),
+ Column("value", type_),
+ )
+
+ t.create(connection)
+
+ result = connection.execute(
+ t.insert().returning(
+ t.c.id,
+ t.c.value,
+ sort_by_parameter_order=bool(sort_by_parameter_order),
+ ),
+ [{"value": value} for i in range(10)]
+ if multiple_rows
+ else {"value": value},
+ )
+
+ if multiple_rows:
+ i_range = range(1, 11)
+ else:
+ i_range = range(1, 2)
+
+ # we want to test only that we are getting floating points back
+ # with some degree of the original value maintained, that it is not
+ # being truncated to an integer. there's too much variation in how
+ # drivers return floats, which should not be relied upon to be
+ # exact, for us to just compare as is (works for PG drivers but not
+ # others) so we use rounding here. There's precedent for this
+ # in suite/test_types.py::NumericTest as well
+
+ if do_rounding:
+ eq_(
+ {(id_, round(val_, 5)) for id_, val_ in result},
+ {(id_, round(value, 5)) for id_ in i_range},
+ )
+
+ eq_(
+ {
+ round(val_, 5)
+ for val_ in connection.scalars(select(t.c.value))
+ },
+ {round(value, 5)},
+ )
+ else:
+ eq_(
+ set(result),
+ {(id_, value) for id_ in i_range},
+ )
+
+ eq_(
+ set(connection.scalars(select(t.c.value))),
+ {value},
+ )
+
__all__ = ("LastrowidTest", "InsertBehaviorTest", "ReturningTest")
(types.Float, [None], {}, "FLOAT"),
(types.Float, [12], {}, "FLOAT(12)"),
(mssql.MSReal, [], {}, "REAL"),
+ (types.Double, [], {}, "DOUBLE PRECISION"),
+ (types.Double, [53], {}, "DOUBLE PRECISION"),
(types.Integer, [], {}, "INTEGER"),
(types.BigInteger, [], {}, "BIGINT"),
(mssql.MSTinyInteger, [], {}, "TINYINT"),
"postgresql+pg8000", "seems to work on pg14 only, not earlier?"
)
+ @property
+ def float_or_double_precision_behaves_generically(self):
+ return skip_if(["oracle", "mysql", "mariadb"])
+
@property
def precision_generic_float_type(self):
"""target backend will return native floating point numbers with at