-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[DO NOT MERGE] Add a test to prove how to use protobuf nullability #11712
Closed
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bc47179
Add a test to prove how to use protobuf nullability
gortiz 9d70b6a
Changes to make test use proto3
gortiz 9e6282d
Add license
gortiz 59301b0
Add more tests and recovering behavior from master
gortiz 202e5b1
Remove programmatic test, which has been proven difficult to configure
gortiz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
.../java/org/apache/pinot/plugin/inputformat/protobuf/ProtoBufRecordExtractorSimpleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package org.apache.pinot.plugin.inputformat.protobuf; | ||
|
||
import com.google.protobuf.DescriptorProtos; | ||
import com.google.protobuf.Descriptors; | ||
import com.google.protobuf.DynamicMessage; | ||
import com.google.protobuf.Message; | ||
import org.apache.pinot.spi.data.readers.GenericRow; | ||
import org.apache.pinot.spi.data.readers.RecordExtractorConfig; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeTest; | ||
import org.testng.annotations.Test; | ||
|
||
|
||
public class ProtoBufRecordExtractorSimpleTest { | ||
|
||
private Descriptors.FileDescriptor _fileDescriptor; | ||
private Descriptors.Descriptor _messageDescriptor; | ||
private Descriptors.FieldDescriptor _optionalDescriptor; | ||
private Descriptors.FieldDescriptor _nonOptionalDescriptor; | ||
|
||
private ProtoBufRecordExtractor _protoBufRecordExtractor; | ||
@BeforeTest | ||
public void init() | ||
throws Descriptors.DescriptorValidationException { | ||
DescriptorProtos.FieldDescriptorProto myOptionalInt = | ||
DescriptorProtos.FieldDescriptorProto.newBuilder() | ||
.setName("myOptionalInt") | ||
.setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED32) | ||
.setNumber(1) | ||
.setProto3Optional(true) | ||
.build(); | ||
DescriptorProtos.FieldDescriptorProto myNonOptionalInt = | ||
DescriptorProtos.FieldDescriptorProto.newBuilder() | ||
.setName("myNonOptionalInt") | ||
.setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED32) | ||
.setNumber(2) | ||
.setProto3Optional(false) | ||
.build(); | ||
DescriptorProtos.FileDescriptorProto build = DescriptorProtos.FileDescriptorProto.newBuilder() | ||
.setName("withOptional") | ||
.addMessageType(DescriptorProtos.DescriptorProto.newBuilder() | ||
.setName("myMessage") | ||
.addField(myOptionalInt) | ||
.addField(myNonOptionalInt) | ||
).build(); | ||
|
||
_fileDescriptor = Descriptors.FileDescriptor.buildFrom(build, new Descriptors.FileDescriptor[] {}); | ||
_messageDescriptor = _fileDescriptor.findMessageTypeByName("myMessage"); | ||
_optionalDescriptor = _messageDescriptor.findFieldByName("myOptionalInt"); | ||
_nonOptionalDescriptor = _messageDescriptor.findFieldByName("myNonOptionalInt"); | ||
|
||
_protoBufRecordExtractor = new ProtoBufRecordExtractor(); | ||
_protoBufRecordExtractor.init(null, new RecordExtractorConfig() { | ||
}); | ||
} | ||
|
||
@Test | ||
public void whenOptionalAndPresent() { | ||
Message message = DynamicMessage.newBuilder(_messageDescriptor) | ||
.setField(_optionalDescriptor, 0) | ||
.build(); | ||
|
||
GenericRow row = new GenericRow(); | ||
_protoBufRecordExtractor.extract(message, row); | ||
|
||
Assert.assertEquals(row.getValue(_optionalDescriptor.getName()), 0); | ||
} | ||
|
||
@Test | ||
public void whenOptionalAndAbsent() { | ||
Message message = DynamicMessage.newBuilder(_messageDescriptor) | ||
.clearField(_optionalDescriptor) | ||
.build(); | ||
|
||
GenericRow row = new GenericRow(); | ||
_protoBufRecordExtractor.extract(message, row); | ||
|
||
Assert.assertNull(row.getValue(_optionalDescriptor.getName())); | ||
} | ||
|
||
@Test | ||
public void whenNotOptionalAndPresent() { | ||
Message message = DynamicMessage.newBuilder(_messageDescriptor) | ||
.setField(_nonOptionalDescriptor, 0) | ||
.build(); | ||
|
||
GenericRow row = new GenericRow(); | ||
_protoBufRecordExtractor.extract(message, row); | ||
|
||
Assert.assertEquals(row.getValue(_nonOptionalDescriptor.getName()), 0); | ||
} | ||
|
||
@Test | ||
public void whenNotOptionalAndAbsent() { | ||
Message message = DynamicMessage.newBuilder(_messageDescriptor) | ||
.clearField(_nonOptionalDescriptor) | ||
.build(); | ||
|
||
GenericRow row = new GenericRow(); | ||
_protoBufRecordExtractor.extract(message, row); | ||
|
||
Assert.assertEquals(row.getValue(_nonOptionalDescriptor.getName()), null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is exactly what my PR changed - from hasOptionalKeyword to isOptional
#11682
API is deprecated
protocolbuffers/protobuf@d6157f7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is deprecated in latest versions of protobuf, but:
Theoretically we should is
isPresent
, but it is not honoring its javadoc. You can verify that by changing this toisPresent
and see that it returns false for the test added here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems isPresent is not bug, but I was unable to correctly configure protobuf model programmatically.