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

[improve][io]: Add validation for JDBC sink not supporting primitive schema #22376

Merged
merged 2 commits into from
Mar 29, 2024
Merged
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
7 changes: 7 additions & 0 deletions pulsar-io/jdbc/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>pulsar-client-original</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.pulsar.client.api.schema.GenericRecord;
import org.apache.pulsar.client.api.schema.KeyValueSchema;
import org.apache.pulsar.common.schema.KeyValue;
import org.apache.pulsar.common.schema.SchemaType;
import org.apache.pulsar.functions.api.Record;
import org.apache.pulsar.io.jdbc.JdbcUtils.ColumnId;

Expand Down Expand Up @@ -137,6 +138,10 @@ public Mutation createMutation(Record<GenericObject> message) {
}
recordValueGetter = (k) -> data.get(k);
} else {
SchemaType schemaType = message.getSchema().getSchemaInfo().getType();
if (schemaType.isPrimitive()) {
throw new UnsupportedOperationException("Primitive schema is not supported: " + schemaType);
}
recordValueGetter = (key) -> ((GenericRecord) record).getField(key);
}
String action = message.getProperties().get(ACTION_PROPERTY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import org.apache.avro.Schema;
import org.apache.avro.SchemaBuilder;
import org.apache.avro.util.Utf8;
import org.apache.pulsar.client.api.schema.GenericObject;
import org.apache.pulsar.client.api.schema.GenericRecord;
import org.apache.pulsar.client.impl.schema.AutoConsumeSchema;
import org.apache.pulsar.functions.api.Record;
import org.testng.Assert;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -143,5 +147,26 @@ private Schema createFieldAndGetSchema(Function<SchemaBuilder.FieldAssembler<Sch
return consumer.apply(record).endRecord().getFields().get(0).schema();
}

@Test(expectedExceptions = UnsupportedOperationException.class,
expectedExceptionsMessageRegExp = "Primitive schema is not supported.*")
@SuppressWarnings("unchecked")
public void testNotSupportPrimitiveSchema() {
BaseJdbcAutoSchemaSink baseJdbcAutoSchemaSink = new BaseJdbcAutoSchemaSink() {};
AutoConsumeSchema autoConsumeSchema = new AutoConsumeSchema();
autoConsumeSchema.setSchema(org.apache.pulsar.client.api.Schema.STRING);
Record<? extends GenericObject> record = new Record<GenericRecord>() {
@Override
public org.apache.pulsar.client.api.Schema<GenericRecord> getSchema() {
return autoConsumeSchema;
}

@Override
public GenericRecord getValue() {
return null;
}
};
baseJdbcAutoSchemaSink.createMutation((Record<GenericObject>) record);
}


}
Loading