Opened 3 years ago

Closed 2 years ago

#33229 closed Bug (fixed)

BaseDatabaseOperations.adapt_datetimefield_value and adapt_timefield_value() crash on expressions

Reported by: Tim Graham Owned by: SwastikTripathi
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

While building a backend for Snowflake, I saw these test failures:

======================================================================
ERROR: test_in_lookup_allows_F_expressions_and_expressions_for_datetimes (expressions.tests.IterableLookupInnerExpressionsTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tim/code/django/django/db/backends/utils.py", line 87, in _execute
    return self.cursor.execute(sql, params)
  File "/home/tim/.virtualenvs/django39/lib/python3.9/site-packages/snowflake/connector/cursor.py", line 777, in execute
    Error.errorhandler_wrapper(
  File "/home/tim/.virtualenvs/django39/lib/python3.9/site-packages/snowflake/connector/errors.py", line 255, in errorhandler_wrapper
    handed_over = Error.hand_to_other_handler(
  File "/home/tim/.virtualenvs/django39/lib/python3.9/site-packages/snowflake/connector/errors.py", line 310, in hand_to_other_handler
    cursor.errorhandler(connection, cursor, error_class, error_value)
  File "/home/tim/.virtualenvs/django39/lib/python3.9/site-packages/snowflake/connector/errors.py", line 189, in default_errorhandler
    raise error_class(
snowflake.connector.errors.ProgrammingError: Timestamp 'Col(expressions_ExPeRiMeNt, expressions.Experiment.start)' is not recognized

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/tim/code/django/tests/expressions/tests.py", line 959, in test_in_lookup_allows_F_expressions_and_expressions_for_datetimes
    self.assertSequenceEqual(queryset, [r1])
  File "/opt/python3.9.5/lib/python3.9/unittest/case.py", line 949, in assertSequenceEqual
    len1 = len(seq1)
  File "/home/tim/code/django/django/db/models/query.py", line 262, in __len__
    self._fetch_all()
  File "/home/tim/code/django/django/db/models/query.py", line 1324, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/tim/code/django/django/db/models/query.py", line 51, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/tim/code/django/django/db/models/sql/compiler.py", line 1175, in execute_sql
    cursor.execute(sql, params)
  File "/home/tim/code/django/django/db/backends/utils.py", line 101, in execute
    return super().execute(sql, params)
  File "/home/tim/code/django/django/db/backends/utils.py", line 69, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/tim/code/django/django/db/backends/utils.py", line 78, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/tim/code/django/django/db/backends/utils.py", line 87, in _execute
    return self.cursor.execute(sql, params)
  File "/home/tim/code/django/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/tim/code/django/django/db/backends/utils.py", line 87, in _execute
    return self.cursor.execute(sql, params)
  File "/home/tim/.virtualenvs/django39/lib/python3.9/site-packages/snowflake/connector/cursor.py", line 777, in execute
    Error.errorhandler_wrapper(
  File "/home/tim/.virtualenvs/django39/lib/python3.9/site-packages/snowflake/connector/errors.py", line 255, in errorhandler_wrapper
    handed_over = Error.hand_to_other_handler(
  File "/home/tim/.virtualenvs/django39/lib/python3.9/site-packages/snowflake/connector/errors.py", line 310, in hand_to_other_handler
    cursor.errorhandler(connection, cursor, error_class, error_value)
  File "/home/tim/.virtualenvs/django39/lib/python3.9/site-packages/snowflake/connector/errors.py", line 189, in default_errorhandler
    raise error_class(
django.db.utils.ProgrammingError: Timestamp 'Col(expressions_ExPeRiMeNt, expressions.Experiment.start)' is not recognized

======================================================================
ERROR: test_expressions_in_lookups_join_choice (expressions.tests.IterableLookupInnerExpressionsTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tim/code/django/tests/expressions/tests.py", line 872, in test_expressions_in_lookups_join_choice
    self.assertSequenceEqual(queryset, [s1])
  File "/opt/python3.9.5/lib/python3.9/unittest/case.py", line 949, in assertSequenceEqual
    len1 = len(seq1)
  File "/home/tim/code/django/django/db/models/query.py", line 262, in __len__
    self._fetch_all()
  File "/home/tim/code/django/django/db/models/query.py", line 1324, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/tim/code/django/django/db/models/query.py", line 51, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/tim/code/django/django/db/models/sql/compiler.py", line 1162, in execute_sql
    sql, params = self.as_sql()
  File "/home/tim/code/django/django/db/models/sql/compiler.py", line 528, in as_sql
    where, w_params = self.compile(self.where) if self.where is not None else ("", [])
  File "/home/tim/code/django/django/db/models/sql/compiler.py", line 445, in compile
    sql, params = node.as_sql(self, self.connection)
  File "/home/tim/code/django/django/db/models/sql/where.py", line 81, in as_sql
    sql, params = compiler.compile(child)
  File "/home/tim/code/django/django/db/models/sql/compiler.py", line 445, in compile
    sql, params = node.as_sql(self, self.connection)
  File "/home/tim/code/django/django/db/models/lookups.py", line 194, in as_sql
    rhs_sql, rhs_params = self.process_rhs(compiler, connection)
  File "/home/tim/code/django/django/db/models/lookups.py", line 248, in process_rhs
    return self.batch_process_rhs(compiler, connection)
  File "/home/tim/code/django/django/db/models/lookups.py", line 261, in batch_process_rhs
    pre_processed = super().batch_process_rhs(compiler, connection, rhs)
  File "/home/tim/code/django/django/db/models/lookups.py", line 58, in batch_process_rhs
    _, params = self.get_db_prep_lookup(rhs, connection)
  File "/home/tim/code/django/django/db/models/lookups.py", line 217, in get_db_prep_lookup
    [get_db_prep_value(v, connection, prepared=True) for v in value]
  File "/home/tim/code/django/django/db/models/lookups.py", line 217, in <listcomp>
    [get_db_prep_value(v, connection, prepared=True) for v in value]
  File "/home/tim/code/django/django/db/models/fields/__init__.py", line 2297, in get_db_prep_value
    return connection.ops.adapt_timefield_value(value)
  File "/home/tim/code/django/django/db/backends/base/operations.py", line 511, in adapt_timefield_value
    if timezone.is_aware(value):
  File "/home/tim/code/django/django/utils/timezone.py", line 220, in is_aware
    return value.utcoffset() is not None
AttributeError: 'Col' object has no attribute 'utcoffset'

The if hasattr(value, 'resolve_expression'): guard added in 4f138fe5a496a81115c4fba6615a517fc62c3b17 wasn't added to BaseDatabaseOperations.adapt_datetimefield_value (for the first test) and adapt_timefield_value() (for the second test).

Change History (7)

comment:1 by Vishal Pandey, 3 years ago

Owner: changed from nobody to Vishal Pandey
Status: newassigned

comment:2 by Mariusz Felisiak, 3 years ago

Triage Stage: UnreviewedAccepted

Thanks for the report. We should probably add the same to the BaseDatabaseOperations.adapt_datefield_value().

comment:3 by Mariusz Felisiak, 2 years ago

Owner: Vishal Pandey removed
Status: assignednew

comment:4 by SwastikTripathi, 2 years ago

Has patch: set
Owner: set to SwastikTripathi
Status: newassigned

I'm adding if hasattr(value, 'resolve_expression'): to adapt_datetimefield_value() and adapt_timefield_value() in django/db/backends/postgresql/operations.py and django/db/backends/base/operations.py

Do you know why it wasn't added there?

Last edited 2 years ago by SwastikTripathi (previous) (diff)

comment:5 by Mariusz Felisiak, 2 years ago

Patch needs improvement: set

comment:6 by Mariusz Felisiak, 2 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 2 years ago

Resolution: fixed
Status: assignedclosed

In a7e7043c:

Fixed #33229 -- Fixed BaseDatabaseOperations.adapt_datetimefield_value()/adapt_timefield_value() crash with expressions.

Note: See TracTickets for help on using tickets.
Back to Top