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

AVRO-4039 [java] fix GenericData.newArray to only return an appropriate array implementation #3307

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

mkeskells
Copy link

What is the purpose of the change

  • Fix the class cast exceptions noted in the ticket (when using logical types)
  • Fix other paths that can return PrimitiveArray when it would not be appropriate
  • Tightness the constraints for the return value, so if a GenericContainer is returned the schema must match the supplied schema

appropriate means that

  • If the suppled value could act as a container for the values that will be added, then clear its values, and reuse
  • If it is a GenericContainer and thus has a schema, then the schema is the same
    If we can't reuse the supplied value, then generate an appropriate collection, using the optimised values where we can

Updated the documentation, and added tests

Verifying this change

This change added tests and can be verified as follows:

  • Added unit tests to ensure that appropriate values are returned (as described above)

Documentation

  • Does this pull request introduce a new feature? (no)

only return an appropriate array
@github-actions github-actions bot added the Java Pull Requests for Java binding label Feb 6, 2025
@mkeskells
Copy link
Author

related issue - https://issues.apache.org/jira/browse/AVRO-4039

Mike Skells added 3 commits February 7, 2025 15:46
only return an appropriate array
fix import that spotless removed
Comment on lines 1541 to 1543
if (schema.getElementType().getLogicalType() != null) {
return new GenericData.Array<Object>(size, schema);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Please note that a logical type positiveInteger is a perfectly valid logical type for an int value. So we cannot assume an Object here (even though it's the most likely option).

Can you please use GenericData#getConversionFor(LogicalType), Conversion#getConvertedType() and Boolean#TYPE et.al. to determine the correct type?

Copy link
Author

Choose a reason for hiding this comment

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

@opwvhk that's - shows how little I know about avro
Will have a look and rework

Copy link
Author

Choose a reason for hiding this comment

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

@opwvhk reworked and added some tests

review feedback
remove schema check on returned value
Check convertors with logical types
review feedback
public static GenericData.AbstractArray<?> createOptimizedArray(int size, Schema schema, Schema.Type valueType) {

if (valueType != null)
switch (valueType) {

Check warning

Code scanning / CodeQL

Missing enum case in switch Warning

Switch statement does not have a case for
ARRAY
,
BYTES
,
ENUM
, or 6 more.
Copy link
Author

Choose a reason for hiding this comment

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

am I expected to add a default:break;

@opwvhk
Copy link
Contributor

opwvhk commented Mar 14, 2025

Hi @mkeskells,
Looks good!
Can you please push the commit with these last "removed"/"done" fixes?

review feedback
@mkeskells
Copy link
Author

Hi @mkeskells, Looks good! Can you please push the commit with these last "removed"/"done" fixes?

doh

pushed now

@mkeskells
Copy link
Author

@opwvhk What's the process from here to get this merged? Does it need other reviewers to approve etc?
I don't know the processes that apply here for this project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Java Pull Requests for Java binding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants