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

[DO NOT MERGE] Add a test to prove how to use protobuf nullability #11712

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions pinot-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -213,22 +213,10 @@
<dependency>
<groupId>org.apache.calcite</groupId>
<artifactId>calcite-core</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.calcite</groupId>
<artifactId>calcite-babel</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.testng</groupId>
Expand Down Expand Up @@ -393,12 +381,6 @@
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java7</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java7</artifactId>
Expand Down
14 changes: 0 additions & 14 deletions pinot-plugins/pinot-file-system/pinot-gcs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.j2objc</groupId>
<artifactId>j2objc-annotations</artifactId>
Expand Down Expand Up @@ -87,22 +83,12 @@
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-common-protos</artifactId>
<version>1.18.1</version>
<exclusions>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-iam-v1</artifactId>
<version>1.0.0</version>
<exclusions>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-common-protos</artifactId>
Expand Down
6 changes: 0 additions & 6 deletions pinot-plugins/pinot-input-format/pinot-protobuf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@
<groupId>com.github.os72</groupId>
<artifactId>protobuf-dynamic</artifactId>
<version>1.0.1</version>
<exclusions>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private Object getFieldValue(Descriptors.FieldDescriptor fieldDescriptor, Messag
// Note w.r.t proto3 - If a field is not declared with optional keyword, there's no way to distinguish
// if its explicitly set to a proto default or not been set at all i.e hasField() returns false
// and we would use null.
if (fieldDescriptor.isRepeated() || !fieldDescriptor.isOptional() || message.hasField(fieldDescriptor)) {
if (fieldDescriptor.isRepeated() || !fieldDescriptor.hasOptionalKeyword() || message.hasField(fieldDescriptor)) {
Copy link
Contributor

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

Copy link
Contributor Author

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:

  • It is not deprecated in the version we use
  • There is no alternative to this method in the latest protobuf version.

Theoretically we should is isPresent, but it is not honoring its javadoc. You can verify that by changing this to isPresent and see that it returns false for the test added here.

Copy link
Contributor Author

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.

return message.getField(fieldDescriptor);
} else {
return null;
Expand Down
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);
}
}
6 changes: 0 additions & 6 deletions pinot-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,6 @@
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
Expand Down
24 changes: 1 addition & 23 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
<joda-time.version>2.12.5</joda-time.version>
<!-- Sets the VM argument line used when unit tests are run. -->
<argLine>-Xms4g -Xmx4g</argLine>
<protobuf.version>3.22.0</protobuf.version>
<protobuf.version>3.24.3</protobuf.version>
<grpc.version>1.53.0</grpc.version>
<confluent.version>5.5.3</confluent.version>

Expand Down Expand Up @@ -712,10 +712,6 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.yetus</groupId>
<artifactId>audience-annotations</artifactId>
Expand Down Expand Up @@ -804,10 +800,6 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand Down Expand Up @@ -888,12 +880,6 @@
<groupId>org.apache.orc</groupId>
<artifactId>orc-core</artifactId>
<version>1.5.9</version>
<exclusions>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.avro</groupId>
Expand Down Expand Up @@ -945,18 +931,10 @@
<artifactId>calcite-core</artifactId>
<version>${calcite.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.calcite.avatica</groupId>
<artifactId>avatica-metrics</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down
Loading