Skip to content

NIFI-7462: QueryRecord cast functions for Choice datatypes#4280

Closed
pcgrenier wants to merge 1 commit intoapache:masterfrom
pcgrenier:NIFI-7462
Closed

NIFI-7462: QueryRecord cast functions for Choice datatypes#4280
pcgrenier wants to merge 1 commit intoapache:masterfrom
pcgrenier:NIFI-7462

Conversation

@pcgrenier
Copy link
Contributor

QueryRecord cast functions for Choice datatypes

Thank you for submitting a contribution to Apache NiFi.

Please provide a short description of the PR here:

Description of PR

Enables X functionality; fixes bug NIFI-YYYY.

In order to streamline the review of the contribution we ask you
to ensure the following steps have been taken:

For all changes:

  • Is there a JIRA ticket associated with this PR? Is it referenced
    in the commit message?

  • Does your PR title start with NIFI-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.

  • Has your PR been rebased against the latest commit within the target branch (typically master)?

  • Is your initial contribution a single, squashed commit? Additional commits in response to PR reviewer feedback should be made on this branch and pushed to allow change tracking. Do not squash or use --force when pushing to allow for clean monitoring of changes.

For code changes:

  • Have you ensured that the full suite of tests is executed via mvn -Pcontrib-check clean install at the root nifi folder?
  • Have you written or updated unit tests to verify your changes?
  • Have you verified that the full build is successful on both JDK 8 and JDK 11?
  • If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under ASF 2.0?
  • If applicable, have you updated the LICENSE file, including the main LICENSE file under nifi-assembly?
  • If applicable, have you updated the NOTICE file, including the main NOTICE file found under nifi-assembly?
  • If adding new Properties, have you added .displayName in addition to .name (programmatic access) for each of the new properties?

For documentation related changes:

  • Have you ensured that format looks appropriate for the output in which it is rendered?

Note:

Please ensure that once the PR is submitted, you check GitHub Actions CI for build issues and submit an update to your PR as soon as possible.

…valid type for use with calcite query functions
@markap14
Copy link
Contributor

Will review

@markap14
Copy link
Contributor

Hey @pcgrenier thanks for putting this PR up. I do think that it addresses the case when all of the values coming in have the same type. But if your schema says that field 'num' can be either an int or a float, this solutions forces the user to cast all values to either int or float. It stands to reason that with such a schema, you could have a mix of both ints and floats. And depending on the query, we may want to keep these values as such.

I also would like to avoid adding our own custom "cast" functions when cast functions already exist in SQL. I definitely feel like that's a red flag that we're not doing something quite right.

So I have a proposal that I think addresses this well but does so very differently. In the FlowFileTable class, we define the data types that Calcite should use internally for each of the types. For the CHOICE data type, we just blindly return typeFactory.createJavaType(Object.class) and that's why it fails - you can't cast an Object to a float, double, int, etc. So I think the better solution is to refine what we return for CHOICE types. Specifically, if we have a choice between two types of numbers where one is wider than the other, we should return the wider type (e.g., for a double/float we should return double). For two numbers where one is not wider than the other (e.g., double/int) we should return a String, as String is considered wider than both of these. For other cases, (e.g., double/Record) we should return Object as we do now.

session.transfer(original, REL_FAILURE);
} catch (final Exception e) {
getLogger().error("Unable to query {} due to {}", new Object[] {original, e});
getLogger().error("Unable to query {} due to {}", new Object[] {original, e.getCause() == null ? e : e.getCause()});
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we want to be getting the 'cause' for the general Exception - we should log the exception itself. We get the cause for SQLException only because we know that it's generally wrapping another exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this one was a strange one. Since the calcite cast function throwing the cannot handle Object exception was getting trapped here and the message was super vague so had to dig into the app logs to see the stack trace.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah unfortunately for this processor, when an Exception is thrown it's kind of difficult to figure out without the logs. But if we were to unwrap the cause, that could make even the logs confusing, which is even worse.

@markap14
Copy link
Contributor

@pcgrenier I created a new PR based off of yours - #4282. Can you review that and make sure that it gives you what you need? This will allow the table schema to be more intelligent for CHOICE types and in cases like this it will allow you to use proper SQL CAST functions and also allows you to perform functions like SUM() without the need to explicitly cast them, which is important if your data does have mixed data types.

@pcgrenier
Copy link
Contributor Author

Closing in favor of #4282

@pcgrenier pcgrenier closed this May 19, 2020
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.

2 participants