Skip to content

Commit

Permalink
add support for MOD on FloatField
Browse files Browse the repository at this point in the history
and cleanup skipped tests
  • Loading branch information
ecogels committed May 10, 2023
1 parent 98611e0 commit 44205df
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
4 changes: 2 additions & 2 deletions tds_django/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ def _as_sql(self):
on_conflict_suffix_sql = self.connection.ops.on_conflict_suffix_sql(
fields,
self.query.on_conflict,
self.query.update_fields,
self.query.unique_fields,
(f.column for f in self.query.update_fields),
(f.column for f in self.query.unique_fields),
)
if (
self.returning_fields
Expand Down
13 changes: 3 additions & 10 deletions tds_django/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,36 +86,29 @@ class DatabaseFeatures(BaseDatabaseFeatures):
},
"unsupported by SQL Server": {
# subquery in agg function
'aggregation.tests.AggregateTestCase.test_aggregation_subquery_annotation_values_collision',
'aggregation.test_filter_argument.FilteredAggregateTests.test_filtered_aggregate_on_exists',
'db_functions.math.test_mod.ModTests.test_float',
# subquery in avg
'expressions_case.tests.CaseExpressionTests.test_annotate_with_in_clause',
# ORDER BY LOB in window function
'expressions_window.tests.WindowFunctionTests.test_key_transform',
'expressions.tests.BasicExpressionsTests.test_aggregate_subquery_annotation',
# NTH_RESULT: could be done
'expressions_window.tests.WindowFunctionTests.test_nth_returns_null',
'expressions_window.tests.WindowFunctionTests.test_nthvalue',
# order by constant
'ordering.tests.OrderingTests.test_order_by_constant_value',
# distinct + subquery, could work if rewrite distinct with group by
'ordering.tests.OrderingTests.test_orders_nulls_first_on_filtered_subquery',
# sql server does not allow like predicates index
# sql server does not allow LIKE predicates index
'indexes.tests.PartialIndexTests.test_multiple_conditions',
# Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
'expressions.tests.BasicExpressionsTests.test_aggregate_subquery_annotation',
# alias in order by expression
'queries.test_qs_combinators.QuerySetSetOperationTests.test_ordering_by_f_expression_and_alias',
'tests.queries.test_qs_combinators.QuerySetSetOperationTests.test_union_order_with_null_first_last',
'queries.test_qs_combinators.QuerySetSetOperationTests.test_union_order_with_null_first_last',

},
"Test is using hardcoded values that are different for sql server": {
'aggregation.tests.AggregateTestCase.test_count_star',
'cache.tests.CreateCacheTableForDBCacheTests.test_createcachetable_observes_database_router',
# assumes string starts with UPDATE when we do a SET NOCOUNT
'get_or_create.tests.UpdateOrCreateTests.test_update_only_defaults_and_pre_save_fields_when_local_fields',
# bug in test: we don't "supports_expression_indexes" but test assumes so ?
# bug in test? we don't "supports_expression_indexes" but test assumes so
'schema.tests.SchemaTests.test_remove_ignored_unique_constraint_not_create_fk_index',
},
"Avg are cast as float, with can cause issues with decimals": {
Expand Down
16 changes: 14 additions & 2 deletions tds_django/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from django.db.models.aggregates import Avg, Count, StdDev, Variance
from django.db.models.expressions import Value, OrderBy, OrderByList, Exists, RawSQL, Window, ExpressionList, Case, When, \
DurationExpression, CombinedExpression
from django.db.models.fields import DecimalField, FloatField
from django.db.models.fields.json import HasKeyLookup
from django.db.models.functions import Now, ATan2, Chr, Collate, Greatest, Least, Length, LPad, Random, \
from django.db.models.functions import Now, ATan2, Cast, Chr, Collate, Greatest, Least, Length, LPad, Random, \
Repeat, RPad, StrIndex, Substr, Log, Ln, Mod, Round, Degrees, Power, Radians, RowNumber
from django.db.models.lookups import BuiltinLookup

Expand Down Expand Up @@ -160,7 +161,18 @@ def ln(self, compiler, connection, **extra_context):
@as_sqlserver(Mod)
def sqlserver_mod(self, compiler, connection, **extra_context):
compiler.escape_if_noparams = True
return self.as_sql(compiler, connection, template='%(expressions)s', arg_joiner=' %% ', **extra_context)
# cast floatfield as decimal
output_field = DecimalField(decimal_places=12, max_digits=20)
clone = self.copy()
clone.set_source_expressions(
[
Cast(expression, output_field)
if isinstance(expression.output_field, FloatField)
else expression
for expression in self.get_source_expressions()
]
)
return clone.as_sql(compiler, connection, template='%(expressions)s', arg_joiner=' %% ', **extra_context)


@as_sqlserver(Power)
Expand Down
2 changes: 1 addition & 1 deletion tds_django/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _alter_column_default_sql(self, model, old_field, new_field, drop=False):
if drop:
with self.connection.cursor() as cursor:
defaults = self.connection.introspection.get_table_defaults(cursor, model._meta.db_table)
constraint_name = defaults.get(new_field.column, ['DEF_DOES_NOT_EXISTS'])[0]
constraint_name = defaults.get(new_field.column, ['DEF_DOES_NOT_EXIST'])[0]
return (self.sql_alter_column_no_default % {
'column': constraint_name,
}, [])
Expand Down

0 comments on commit 44205df

Please sign in to comment.