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

Allow non literal rhs in MV_FILTER_ONLY and MV_FILTER_NONE #16113

Merged
merged 4 commits into from
Mar 26, 2024

Conversation

sreemanamala
Copy link
Contributor

@sreemanamala sreemanamala commented Mar 13, 2024

Description

This PR allows to use the MV_FILTER_ONLY & MV_FILTER_NONE functions with a non literal argument.
Currently select mv_filter_only('mvd_dim', 'array_dim') from 'table' returns a Unhandled Query Planning Failure
This is being tackled and also considered for the cases where the array_dim having null & empty values


Key changed/added classes in this PR
  • MultiValueStringOperatorConversions
  • ApplyFunction
  • CalciteMultiValueStringQueryTest

This PR has:

  • been self-reviewed.
  • added documentation for new or modified features or behaviors.
  • a release note entry in the PR description.
  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
  • added or updated version, license, or notice information in licenses.yaml
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.
  • added integration tests.
  • been tested in a test Druid cluster.


Object[] array = arrayEval.asArray();
if (array == null) {
return ExprEval.of(null);
Copy link
Member

Choose a reason for hiding this comment

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

nit: this should probably return ExprEval.ofArray(arrayEval.asArrayType(), null) so it doesn't change the type to STRING. This is a bug in FilterFunction too..

SettableLambdaBinding lambdaBinding = new SettableLambdaBinding(arrayEval.elementType(), lambdaExpr, bindings);
Object[] filtered = filter(arrayEval.asArray(), lambdaExpr, lambdaBinding).toArray();
if (filtered.length == 0) {
return ExprEval.of(null);
Copy link
Member

Choose a reason for hiding this comment

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

same comment about ExprEval.ofArray(arrayEval.asArrayType(), null)

@@ -476,6 +476,44 @@ private <T> Stream<T> filter(T[] array, LambdaExpr expr, SettableLambdaBinding b
}
}

/**
* Extended version of {@link FilterFunction} to return a null expr if filtered result turns out to be empty
Copy link
Member

Choose a reason for hiding this comment

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

hmm, reading the docs for filter, https://github.com/apache/druid/blob/master/docs/querying/math-expr.md#apply-functions, it actually claims that it returns null if no elements match. Given that, I suppose we could just fix filter to make it match the docs instead of creating a new function. Is it actually useful to have a filter that returns empty array if no matches?

Alternatively, if we do decide we want both behaviors, we should update those docs and add new docs for this function.

return ExprEval.ofArray(arrayEval.asArrayType(), filtered);
}

private <T> Stream<T> filter(T[] array, LambdaExpr expr, SettableLambdaBinding binding)
Copy link
Member

Choose a reason for hiding this comment

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

nit: maybe should just make this method not private in FilterFunction? (assuming we keep this)


String dummyNull = null;
assertExpr("filter((x) -> array_contains([], x), ['a', 'b'])", dummyNull);
assertExpr("filter((x) -> array_contains([], x), null)", dummyNull);
Copy link
Member

Choose a reason for hiding this comment

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

nit: this variable seems more chars than just using null directly...

@kfaraz kfaraz merged commit f29c8ac into apache:master Mar 26, 2024
85 checks passed
@adarshsanjeev adarshsanjeev added this to the 30.0.0 milestone May 6, 2024
@sreemanamala sreemanamala deleted the mv-filter branch September 18, 2024 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants