From 234daa0f99992f03c88fc7fd3b806af2283eb7f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 27 Jul 2024 17:49:13 +0200 Subject: [PATCH] Add cardinality option to Index Usage Closes #13426 --- trytond/CHANGELOG | 1 + trytond/doc/ref/models.rst | 9 +++++---- trytond/trytond/backend/postgresql/table.py | 3 ++- trytond/trytond/model/modelsql.py | 7 +++++-- trytond/trytond/tests/test_model_index.py | 13 +++++++++++++ 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/trytond/CHANGELOG b/trytond/CHANGELOG index 4f731134a3..b6d581ff62 100644 --- a/trytond/CHANGELOG +++ b/trytond/CHANGELOG @@ -1,3 +1,4 @@ +* Add cardinality option to Index Usage * Allow configuring TinyMCE license key * Add tools to apply types to SQLite select queries * Compress RPC responses diff --git a/trytond/doc/ref/models.rst b/trytond/doc/ref/models.rst index c5d17e1dc2..c268b8fe51 100644 --- a/trytond/doc/ref/models.rst +++ b/trytond/doc/ref/models.rst @@ -798,23 +798,24 @@ Available options are: Apply unaccent function if the database supports it. -.. attribute:: Index.Usage(\*\*options) +.. attribute:: Index.Usage([cardinality[, \*\*options]]) Represent a usage of a SQL expression. +Available cardinality is ``low``, ``normal`` (default) and ``high``. Available options are: * ``collation``: the name of the collation * ``order``: the sort order -.. attribute:: Index.Equality(\*\*options) +.. attribute:: Index.Equality([cardinality[, \*\*options]]) Represent an equality usage. -.. attribute:: Index.Range(\*\*options) +.. attribute:: Index.Range([cardinality[, \*\*options]]) Represent an range usage. -.. attribute:: Index.Similarity(\*\*options) +.. attribute:: Index.Similarity([cardinality[, \*\*options]]) Represent a similar usage only for text. Additional options are available: diff --git a/trytond/trytond/backend/postgresql/table.py b/trytond/trytond/backend/postgresql/table.py index e264dacc97..f172934717 100644 --- a/trytond/trytond/backend/postgresql/table.py +++ b/trytond/trytond/backend/postgresql/table.py @@ -635,7 +635,8 @@ class HashTranslator(IndexMixin, IndexTranslatorInterface): @classmethod def score(cls, index): if (len(index.expressions) > 1 - or index.expressions[0][1].__class__.__name__ != 'Equality'): + or index.expressions[0][1].__class__.__name__ != 'Equality' + or index.expressions[0][1].cardinality != 'high'): return 0 if index.options.get('include'): return 0 diff --git a/trytond/trytond/model/modelsql.py b/trytond/trytond/model/modelsql.py index c40eb6f711..15d9c49a42 100644 --- a/trytond/trytond/model/modelsql.py +++ b/trytond/trytond/model/modelsql.py @@ -232,9 +232,11 @@ class Index: return self.expression.params class Usage: - __slots__ = ('options',) + __slots__ = ('options', 'cardinality') - def __init__(self, **options): + def __init__(self, cardinality='normal', **options): + assert cardinality in {'low', 'normal', 'high'} + self.cardinality = cardinality self.options = options def __hash__(self): @@ -242,6 +244,7 @@ class Index: def __eq__(self, other): return (self.__class__ == other.__class__ + and self.cardinality == other.cardinality and self.options == other.options) class Equality(Usage): diff --git a/trytond/trytond/tests/test_model_index.py b/trytond/trytond/tests/test_model_index.py index e85e6ca40c..213b0e4edf 100644 --- a/trytond/trytond/tests/test_model_index.py +++ b/trytond/trytond/tests/test_model_index.py @@ -77,6 +77,19 @@ class ModelIndexTestCase(TestCase): self.assertNotEqual(index1, index2) + def test_index_inequality_usage_cardinality(self): + "Test Index inequality on usage cardinality" + table1 = Table('test') + index1 = Index( + table1, + (table1.name, Index.Equality(cardinality='low'))) + table2 = Table('test') + index2 = Index( + table2, + (table2.name, Index.Equality(cardinality='high'))) + + self.assertNotEqual(index1, index2) + def test_index_inequality_usage_option(self): "Test Index inequality on usage option" table1 = Table('test')