from avocado.query.translators import Translator, registry
from modeltree.tree import trees


class AllowNullsTranslator(Translator):
    """For data sources that only apply to SNPs, this translator ensures only
    SNPs are filtered down and not other types of variants.
    """
    def translate(self, field, roperator, rvalue, tree, **kwargs):
        output = super(AllowNullsTranslator, self).translate(
            field, roperator, rvalue, tree, **kwargs)
        # Create a null condition for this field
        null_condition = trees[tree].query_condition(
            field.field, 'isnull', True)
        # Allow the null condition
        output['query_modifiers']['condition'] |= null_condition
        return output


registry.register(AllowNullsTranslator, 'Allow Nulls')

from avocado.query.translators import Translator, registry
from modeltree.tree import trees


class AllowNullsTranslator(Translator):
    """For data sources that only apply to SNPs, this translator ensures only
    SNPs are filtered down and not other types of variants.
    """
    def translate(self, field, roperator, rvalue, tree, **kwargs):
        output = super(AllowNullsTranslator, self).translate(
            field, roperator, rvalue, tree, **kwargs)

        # We are excluding nulls in the case of range, gt, and gte operators.
        # If we did not do this, then null values would be included all the
        # time which would be confusing, especially then they are included
        # for both lt and gt queries as it appears nulls are simultaneously
        # 0 and infinity.
        if roperator not in ('range', 'gt', 'gte'):
            # Create a null condition for this field
            null_condition = trees[tree].query_condition(
                field.field, 'isnull', True)
            # Allow the null condition
            output['query_modifiers']['condition'] |= null_condition

        return output


registry.register(AllowNullsTranslator, 'Allow Nulls')

Exclude nulls in translator when operator is range, gt, gte
Exclude nulls in translator when operator is range, gt, gte

Previously, nulls were included in all cases making it appear that null
was but 0 and infinity. Now, null is effectively treated as 0.

Signed-off-by: Don Naegely <e690a32c1e2176a2bfface09e204830e1b5491e3@gmail.com>

  from avocado.query.translators import Translator, registry
  from modeltree.tree import trees
  
  
  class AllowNullsTranslator(Translator):
      """For data sources that only apply to SNPs, this translator ensures only
      SNPs are filtered down and not other types of variants.
      """
      def translate(self, field, roperator, rvalue, tree, **kwargs):
          output = super(AllowNullsTranslator, self).translate(
              field, roperator, rvalue, tree, **kwargs)
+ 
+         # We are excluding nulls in the case of range, gt, and gte operators.
+         # If we did not do this, then null values would be included all the
+         # time which would be confusing, especially then they are included
+         # for both lt and gt queries as it appears nulls are simultaneously
+         # 0 and infinity.
+         if roperator not in ('range', 'gt', 'gte'):
-         # Create a null condition for this field
+             # Create a null condition for this field
-         null_condition = trees[tree].query_condition(
+             null_condition = trees[tree].query_condition(
-             field.field, 'isnull', True)
+                 field.field, 'isnull', True)
-         # Allow the null condition
+             # Allow the null condition
-         output['query_modifiers']['condition'] |= null_condition
+             output['query_modifiers']['condition'] |= null_condition
+ 
          return output
  
  
  registry.register(AllowNullsTranslator, 'Allow Nulls')
  
Exclude nulls in translator when operator is range, gt, gte
## Code Before:
from avocado.query.translators import Translator, registry
from modeltree.tree import trees


class AllowNullsTranslator(Translator):
    """For data sources that only apply to SNPs, this translator ensures only
    SNPs are filtered down and not other types of variants.
    """
    def translate(self, field, roperator, rvalue, tree, **kwargs):
        output = super(AllowNullsTranslator, self).translate(
            field, roperator, rvalue, tree, **kwargs)
        # Create a null condition for this field
        null_condition = trees[tree].query_condition(
            field.field, 'isnull', True)
        # Allow the null condition
        output['query_modifiers']['condition'] |= null_condition
        return output


registry.register(AllowNullsTranslator, 'Allow Nulls')

## Instruction:
Exclude nulls in translator when operator is range, gt, gte
## Code After:
from avocado.query.translators import Translator, registry
from modeltree.tree import trees


class AllowNullsTranslator(Translator):
    """For data sources that only apply to SNPs, this translator ensures only
    SNPs are filtered down and not other types of variants.
    """
    def translate(self, field, roperator, rvalue, tree, **kwargs):
        output = super(AllowNullsTranslator, self).translate(
            field, roperator, rvalue, tree, **kwargs)

        # We are excluding nulls in the case of range, gt, and gte operators.
        # If we did not do this, then null values would be included all the
        # time which would be confusing, especially then they are included
        # for both lt and gt queries as it appears nulls are simultaneously
        # 0 and infinity.
        if roperator not in ('range', 'gt', 'gte'):
            # Create a null condition for this field
            null_condition = trees[tree].query_condition(
                field.field, 'isnull', True)
            # Allow the null condition
            output['query_modifiers']['condition'] |= null_condition

        return output


registry.register(AllowNullsTranslator, 'Allow Nulls')
