Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wildcards in numeric range queries #75

Merged
merged 1 commit into from
Jun 5, 2023

Conversation

pgj
Copy link
Contributor

@pgj pgj commented May 21, 2023

When the * (wildcard) symbol is used in ranges, the parsing dies with a NullPointerException. That is because input strings with the null value are not handled -- they should be treated as infinity with the respective sign (depending on the side of range where the wildcard is used).

When the `*` symbol is used in ranges, the parsing dies with a
`NullPointerException`.  That is because input strings with the
`null` value are not handled -- they should be treated as infinity
with the respective sign (depending on the side of range where the
wildcard is used).
Copy link
Contributor

@jaydoane jaydoane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit tests pass. Also tested with make mango-test and make elixir-suite.

if (isNumber(lower) && isNumber(upper)) {
NumericRangeQuery.newDoubleRange(field, 8, lower.toDouble,
upper.toDouble, startInclusive, endInclusive)
val lb = Option(lower).getOrElse("-Infinity")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this appears to expand the syntax to mean any input that the parser yielded null for will be a valid open range, which boxes us in for future changes and I think is unintentional?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to see the NPE handled. If it occurs in;

private def isNumber(str: String): Boolean = {
    fpRegex.matcher(str).matches()
  }

then we should just change isNumber to return false if str is null.

Copy link
Contributor Author

@pgj pgj Jun 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The null values in the second and third parameters of QueryParser/getRangeQuery() have a special meaning. It denotes the start and end of open ranges as suggested by the sources of Lucene 4.6.1:

          boolean startOpen=false;
          boolean endOpen=false;
          if (goop1.kind == RANGE_QUOTED) {
            goop1.image = goop1.image.substring(1, goop1.image.length()-1);
          } else if ("*".equals(goop1.image)) {
            startOpen=true;
          }
          if (goop2.kind == RANGE_QUOTED) {
            goop2.image = goop2.image.substring(1, goop2.image.length()-1);
          } else if ("*".equals(goop2.image)) {
            endOpen=true;
          }
          q = getRangeQuery(field, startOpen ? null : discardEscapeChar(goop1.image), endOpen ? null : discardEscapeChar(goop2.image), startInc, endInc);

You can also see in the toString() method of TermRangeQuery that nulls are uniquely translated back to stars:

      buffer.append(includeLower ? '[' : '{');
      buffer.append(lowerTerm != null ? ("*".equals(Term.toString(lowerTerm)) ? "\\*" : Term.toString(lowerTerm))  : "*");
      buffer.append(" TO ");
      buffer.append(upperTerm != null ? ("*".equals(Term.toString(upperTerm)) ? "\\*" : Term.toString(upperTerm)) : "*");
      buffer.append(includeUpper ? ']' : '}');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aha, great, thanks for digging in.

@pgj pgj merged commit ccd89cd into cloudant-labs:master Jun 5, 2023
@pgj pgj deleted the fix-wildcard-in-numeric-ranges branch June 5, 2023 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants