Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#21] feat(schema): add Protobuf serialization and deserialization support for schema system (#26)

Jerry Shaod1aaee223640
diff --git a/core/build.gradle.kts b/core/build.gradle.kts
index e8a996c253..8303e1dbb3 100644
--- a/core/build.gradle.kts
+++ b/core/build.gradle.kts
@@ -11,12 +11,15 @@ dependencies {
exclude("com.google.guava", "guava")
.because("Brings in Guava for Andriod, which we don't want (and breaks multimaps).")
}
- implementation(libs.substrait.java.core)
+ implementation(libs.substrait.java.core) {
+ exclude("org.slf4j")
+ }
implementation(libs.jackson.databind)
implementation(libs.jackson.annotations)
implementation(libs.jackson.datatype.jdk8)
implementation(libs.jackson.datatype.jsr310)
implementation(libs.guava)
+ implementation(libs.bundles.log4j)
compileOnly(libs.lombok)
annotationProcessor(libs.lombok)
diff --git a/core/src/main/java/com/datastrato/unified_catalog/schema/json/JsonUtils.java b/core/src/main/java/com/datastrato/unified_catalog/schema/json/JsonUtils.java
index f178c18f43..66d037de60 100644
--- a/core/src/main/java/com/datastrato/unified_catalog/schema/json/JsonUtils.java
+++ b/core/src/main/java/com/datastrato/unified_catalog/schema/json/JsonUtils.java
@@ -9,6 +9,8 @@ import io.substrait.type.Type;
import io.substrait.type.parser.ParseToPojo;
import io.substrait.type.parser.TypeStringParser;
import java.io.IOException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public class JsonUtils {
private static ObjectMapper mapper = null;
@@ -29,6 +31,8 @@ public class JsonUtils {
}
public static class TypeSerializer extends JsonSerializer<io.substrait.type.Type> {
+ private static final Logger LOG = LoggerFactory.getLogger(TypeSerializer.class);
+
private final StringTypeVisitor visitor = new StringTypeVisitor();
@Override
@@ -37,12 +41,14 @@ public class JsonUtils {
try {
gen.writeString(value.accept(visitor));
} catch (Exception e) {
+ LOG.warn("Unable to serialize type {}.", value, e);
throw new IOException("Unable to serialize type " + value, e);
}
}
}
public static class TypeDeserializer extends JsonDeserializer<io.substrait.type.Type> {
+ private static final Logger LOG = LoggerFactory.getLogger(TypeDeserializer.class);
@Override
public Type deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
@@ -50,6 +56,7 @@ public class JsonUtils {
try {
return TypeStringParser.parse(s, ParseToPojo::type);
} catch (Exception e) {
+ LOG.warn("Unable to parse string {}.", s.replace("\n", " \\n"), e);
throw new IOException("Unable to parse string " + s.replace("\n", " \\n"), e);
}
}
diff --git a/core/src/main/java/com/datastrato/unified_catalog/schema/proto/AuditInfoSerDe.java b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/AuditInfoSerDe.java
new file mode 100644
index 0000000000..f8263fa25b
--- /dev/null
+++ b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/AuditInfoSerDe.java
@@ -0,0 +1,48 @@
+package com.datastrato.unified_catalog.schema.proto;
+
+class AuditInfoSerDe
+ implements ProtoSerDe<com.datastrato.unified_catalog.schema.AuditInfo, AuditInfo> {
+ @Override
+ public AuditInfo serialize(com.datastrato.unified_catalog.schema.AuditInfo auditInfo) {
+ AuditInfo.Builder builder =
+ AuditInfo.newBuilder()
+ .setCreator(auditInfo.getCreator())
+ .setCreateTime(ProtoUtils.fromInstant(auditInfo.getCreateTime()));
+
+ if (auditInfo.getLastModifier() != null && auditInfo.getLastModifiedTime() != null) {
+ builder
+ .setLastModifier(auditInfo.getLastModifier())
+ .setLastModifiedTime(ProtoUtils.fromInstant(auditInfo.getLastModifiedTime()));
+ }
+
+ if (auditInfo.getLastAccessUser() != null && auditInfo.getLastAccessTime() != null) {
+ builder
+ .setLastAccessUser(auditInfo.getLastAccessUser())
+ .setLastAccessTime(ProtoUtils.fromInstant(auditInfo.getLastAccessTime()));
+ }
+
+ return builder.build();
+ }
+
+ @Override
+ public com.datastrato.unified_catalog.schema.AuditInfo deserialize(AuditInfo p) {
+ com.datastrato.unified_catalog.schema.AuditInfo.Builder builder =
+ new com.datastrato.unified_catalog.schema.AuditInfo.Builder()
+ .withCreator(p.getCreator())
+ .withCreateTime(ProtoUtils.toInstant(p.getCreateTime()));
+
+ if (p.hasLastModifier() && p.hasLastModifiedTime()) {
+ builder
+ .withLastModifier(p.getLastModifier())
+ .withLastModifiedTime(ProtoUtils.toInstant(p.getLastModifiedTime()));
+ }
+
+ if (p.hasLastAccessUser() && p.hasLastAccessTime()) {
+ builder
+ .withLastAccessUser(p.getLastAccessUser())
+ .withLastAccessTime(ProtoUtils.toInstant(p.getLastAccessTime()));
+ }
+
+ return builder.build();
+ }
+}
diff --git a/core/src/main/java/com/datastrato/unified_catalog/schema/proto/ColumnSerDe.java b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/ColumnSerDe.java
new file mode 100644
index 0000000000..dfb052d245
--- /dev/null
+++ b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/ColumnSerDe.java
@@ -0,0 +1,46 @@
+package com.datastrato.unified_catalog.schema.proto;
+
+import io.substrait.type.proto.FromProto;
+import io.substrait.type.proto.TypeProtoConverter;
+
+class ColumnSerDe implements ProtoSerDe<com.datastrato.unified_catalog.schema.Column, Column> {
+
+ @Override
+ public Column serialize(com.datastrato.unified_catalog.schema.Column column) {
+ Column.Builder builder = Column.newBuilder();
+ builder
+ .setId(column.getId())
+ .setName(column.getName())
+ .setEntityId(column.getEntityId())
+ .setEntitySnapshotId(column.getEntitySnapshotId())
+ .setType(column.getType().accept(TypeProtoConverter.INSTANCE))
+ .setPosition(column.getPosition())
+ .setAuditInfo(new AuditInfoSerDe().serialize(column.auditInfo()));
+
+ if (column.getComment() != null) {
+ builder.setComment(column.getComment());
+ }
+
+ return builder.build();
+ }
+
+ @Override
+ public com.datastrato.unified_catalog.schema.Column deserialize(Column p) {
+ com.datastrato.unified_catalog.schema.Column.Builder builder =
+ new com.datastrato.unified_catalog.schema.Column.Builder();
+ builder
+ .withId(p.getId())
+ .withName(p.getName())
+ .withEntityId(p.getEntityId())
+ .withEntitySnapshotId(p.getEntitySnapshotId())
+ .withType(FromProto.from(p.getType()))
+ .withPosition(p.getPosition())
+ .withAuditInfo(new AuditInfoSerDe().deserialize(p.getAuditInfo()));
+
+ if (p.hasComment()) {
+ builder.withComment(p.getComment());
+ }
+
+ return builder.build();
+ }
+}
diff --git a/core/src/main/java/com/datastrato/unified_catalog/schema/proto/LakehouseSerDe.java b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/LakehouseSerDe.java
new file mode 100644
index 0000000000..fc52efcbc1
--- /dev/null
+++ b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/LakehouseSerDe.java
@@ -0,0 +1,43 @@
+package com.datastrato.unified_catalog.schema.proto;
+
+class LakehouseSerDe
+ implements ProtoSerDe<com.datastrato.unified_catalog.schema.Lakehouse, Lakehouse> {
+ @Override
+ public Lakehouse serialize(com.datastrato.unified_catalog.schema.Lakehouse lakehouse) {
+ Lakehouse.Builder builder =
+ Lakehouse.newBuilder()
+ .setId(lakehouse.getId())
+ .setName(lakehouse.getName())
+ .setAuditInfo(new AuditInfoSerDe().serialize(lakehouse.auditInfo()));
+
+ if (lakehouse.getComment() != null) {
+ builder.setComment(lakehouse.getComment());
+ }
+
+ if (lakehouse.getProperties() != null && !lakehouse.getProperties().isEmpty()) {
+ builder.putAllProperties(lakehouse.getProperties());
+ }
+
+ return builder.build();
+ }
+
+ @Override
+ public com.datastrato.unified_catalog.schema.Lakehouse deserialize(Lakehouse p) {
+ com.datastrato.unified_catalog.schema.Lakehouse.Builder builder =
+ new com.datastrato.unified_catalog.schema.Lakehouse.Builder();
+ builder
+ .withId(p.getId())
+ .withName(p.getName())
+ .withAuditInfo(new AuditInfoSerDe().deserialize(p.getAuditInfo()));
+
+ if (p.hasComment()) {
+ builder.withComment(p.getComment());
+ }
+
+ if (p.getPropertiesCount() > 0) {
+ builder.withProperties(p.getPropertiesMap());
+ }
+
+ return builder.build();
+ }
+}
diff --git a/core/src/main/java/com/datastrato/unified_catalog/schema/proto/ProtoSerDe.java b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/ProtoSerDe.java
new file mode 100644
index 0000000000..85cb43a9e8
--- /dev/null
+++ b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/ProtoSerDe.java
@@ -0,0 +1,10 @@
+package com.datastrato.unified_catalog.schema.proto;
+
+import com.google.protobuf.MessageLite;
+
+public interface ProtoSerDe<T, M extends MessageLite> {
+
+ M serialize(T t);
+
+ T deserialize(M p);
+}
diff --git a/core/src/main/java/com/datastrato/unified_catalog/schema/proto/ProtoSerDeException.java b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/ProtoSerDeException.java
new file mode 100644
index 0000000000..60014bba26
--- /dev/null
+++ b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/ProtoSerDeException.java
@@ -0,0 +1,12 @@
+package com.datastrato.unified_catalog.schema.proto;
+
+public class ProtoSerDeException extends RuntimeException {
+
+ public ProtoSerDeException(String message) {
+ super(message);
+ }
+
+ public ProtoSerDeException(String message, Throwable cause) {
+ super(message, cause);
+ }
+}
diff --git a/core/src/main/java/com/datastrato/unified_catalog/schema/proto/ProtoUtils.java b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/ProtoUtils.java
new file mode 100644
index 0000000000..7491d1e168
--- /dev/null
+++ b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/ProtoUtils.java
@@ -0,0 +1,97 @@
+package com.datastrato.unified_catalog.schema.proto;
+
+import com.datastrato.unified_catalog.schema.Entity;
+import com.google.protobuf.MessageLite;
+import com.google.protobuf.Parser;
+import com.google.protobuf.Timestamp;
+import java.time.Instant;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+class ProtoUtils {
+ private static final Map<
+ Class<? extends Entity>, ProtoSerDe<? extends Entity, ? extends MessageLite>>
+ ENTITY_TO_SERDE;
+
+ static {
+ Map<Class<? extends Entity>, ProtoSerDe<? extends Entity, ? extends MessageLite>> tmp =
+ new HashMap<>();
+ tmp.put(com.datastrato.unified_catalog.schema.AuditInfo.class, new AuditInfoSerDe());
+ tmp.put(com.datastrato.unified_catalog.schema.Tenant.class, new TenantSerDe());
+ tmp.put(com.datastrato.unified_catalog.schema.Lakehouse.class, new LakehouseSerDe());
+ tmp.put(com.datastrato.unified_catalog.schema.Zone.class, new ZoneSerDe());
+ tmp.put(com.datastrato.unified_catalog.schema.Table.class, new TableSerDe());
+ tmp.put(com.datastrato.unified_catalog.schema.Column.class, new ColumnSerDe());
+
+ ENTITY_TO_SERDE = Collections.unmodifiableMap(tmp);
+ }
+
+ private static final Map<
+ Class<? extends MessageLite>, ProtoSerDe<? extends Entity, ? extends MessageLite>>
+ PROTO_TO_SERDE;
+
+ static {
+ Map<Class<? extends MessageLite>, ProtoSerDe<? extends Entity, ? extends MessageLite>> tmp =
+ new HashMap<>();
+ tmp.put(AuditInfo.class, new AuditInfoSerDe());
+ tmp.put(Tenant.class, new TenantSerDe());
+ tmp.put(Lakehouse.class, new LakehouseSerDe());
+ tmp.put(Zone.class, new ZoneSerDe());
+ tmp.put(Table.class, new TableSerDe());
+ tmp.put(Column.class, new ColumnSerDe());
+
+ PROTO_TO_SERDE = Collections.unmodifiableMap(tmp);
+ }
+
+ private ProtoUtils() {}
+
+ public static Timestamp fromInstant(Instant instant) {
+ return Timestamp.newBuilder()
+ .setSeconds(instant.getEpochSecond())
+ .setNanos(instant.getNano())
+ .build();
+ }
+
+ public static Instant toInstant(Timestamp timestamp) {
+ return Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
+ }
+
+ public static <T extends Entity, M extends MessageLite> M toProto(T t) {
+ if (!ENTITY_TO_SERDE.containsKey(t.getClass())) {
+ throw new ProtoSerDeException("No proto serializer for " + t.getClass().getName());
+ }
+
+ ProtoSerDe<T, M> protoSerDe = (ProtoSerDe<T, M>) ENTITY_TO_SERDE.get(t.getClass());
+ return protoSerDe.serialize(t);
+ }
+
+ public static <T extends Entity> byte[] toBytes(T t) {
+ return toProto(t).toByteArray();
+ }
+
+ public static <T extends Entity, M extends MessageLite> T fromProto(M m) {
+ if (!PROTO_TO_SERDE.containsKey(m.getClass())) {
+ throw new ProtoSerDeException("No proto deserializer for " + m.getClass().getName());
+ }
+
+ ProtoSerDe<T, M> protoSerDe = (ProtoSerDe<T, M>) PROTO_TO_SERDE.get(m.getClass());
+ return protoSerDe.deserialize(m);
+ }
+
+ public static <T extends Entity, M extends MessageLite> T fromBytes(
+ byte[] bytes, Class<T> clazz, Parser<M> parser) {
+ if (!ENTITY_TO_SERDE.containsKey(clazz)) {
+ throw new ProtoSerDeException("No proto deserializer for " + clazz.getName());
+ }
+
+ try {
+ M message = parser.parseFrom(bytes);
+ ProtoSerDe<T, M> protoSerDe = (ProtoSerDe<T, M>) ENTITY_TO_SERDE.get(clazz);
+ return protoSerDe.deserialize(message);
+
+ } catch (Exception e) {
+ throw new ProtoSerDeException("Failed in deserializing bytes to proto", e);
+ }
+ }
+}
diff --git a/core/src/main/java/com/datastrato/unified_catalog/schema/proto/TableSerDe.java b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/TableSerDe.java
new file mode 100644
index 0000000000..40c1f55fae
--- /dev/null
+++ b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/TableSerDe.java
@@ -0,0 +1,103 @@
+package com.datastrato.unified_catalog.schema.proto;
+
+class TableSerDe implements ProtoSerDe<com.datastrato.unified_catalog.schema.Table, Table> {
+
+ @Override
+ public Table serialize(com.datastrato.unified_catalog.schema.Table table) {
+ Table.Builder builder = Table.newBuilder();
+ builder
+ .setId(table.getId())
+ .setZoneId(table.getZoneId())
+ .setName(table.getName())
+ .setType(Table.TableType.valueOf(table.getType().getTypeStr()))
+ .setSnapshotId(table.getSnapshotId())
+ .setAuditInfo(new AuditInfoSerDe().serialize(table.auditInfo()));
+
+ if (table.getComment() != null) {
+ builder.setComment(table.getComment());
+ }
+
+ if (table.getProperties() != null && !table.getProperties().isEmpty()) {
+ builder.putAllProperties(table.getProperties());
+ }
+
+ setTableExtraInfo(builder, table.getType(), table.getExtraInfo());
+
+ return builder.build();
+ }
+
+ @Override
+ public com.datastrato.unified_catalog.schema.Table deserialize(Table p) {
+ com.datastrato.unified_catalog.schema.Table.Builder builder =
+ new com.datastrato.unified_catalog.schema.Table.Builder();
+ builder
+ .withId(p.getId())
+ .withZoneId(p.getZoneId())
+ .withName(p.getName())
+ .withType(com.datastrato.unified_catalog.schema.Table.TableType.valueOf(p.getType().name()))
+ .withSnapshotId(p.getSnapshotId())
+ .withAuditInfo(new AuditInfoSerDe().deserialize(p.getAuditInfo()));
+
+ if (p.hasComment()) {
+ builder.withComment(p.getComment());
+ }
+
+ if (p.getPropertiesCount() > 0) {
+ builder.withProperties(p.getPropertiesMap());
+ }
+
+ builder.withExtraInfo(
+ getTableExtraInfo(
+ com.datastrato.unified_catalog.schema.Table.TableType.valueOf(p.getType().name()), p));
+
+ return builder.build();
+ }
+
+ private void setTableExtraInfo(
+ Table.Builder builder,
+ com.datastrato.unified_catalog.schema.Table.TableType tableType,
+ com.datastrato.unified_catalog.schema.hasExtraInfo.ExtraInfo tableExtraInfo) {
+ switch (tableType) {
+ case VIRTUAL:
+ com.datastrato.unified_catalog.schema.VirtualTableInfo virtualTableInfo =
+ (com.datastrato.unified_catalog.schema.VirtualTableInfo) tableExtraInfo;
+ builder.setVirtualTable(
+ Table.VirtualTableInfo.newBuilder()
+ .setConnectionId(virtualTableInfo.getConnectionId())
+ .addAllTableIdentifier(virtualTableInfo.getIdentifier())
+ .build());
+ break;
+
+ case VIEW:
+ case EXTERNAL:
+ case MANAGED:
+ // TODO. Add support for these types.
+ throw new ProtoSerDeException("Table type " + tableType + " is not supported yet.");
+
+ default:
+ throw new ProtoSerDeException("Unknown table type " + tableType + ".");
+ }
+ }
+
+ private com.datastrato.unified_catalog.schema.hasExtraInfo.ExtraInfo getTableExtraInfo(
+ com.datastrato.unified_catalog.schema.Table.TableType tableType, Table p) {
+ switch (tableType) {
+ case VIRTUAL:
+ if (!p.hasVirtualTable()) {
+ throw new ProtoSerDeException("Virtual table info is missing.");
+ }
+
+ return new com.datastrato.unified_catalog.schema.VirtualTableInfo(
+ p.getVirtualTable().getConnectionId(), p.getVirtualTable().getTableIdentifierList());
+
+ case VIEW:
+ case EXTERNAL:
+ case MANAGED:
+ // TODO. Add support for these types.
+ throw new ProtoSerDeException("Table type " + tableType + " is not supported yet.");
+
+ default:
+ throw new ProtoSerDeException("Unknown table type " + tableType + ".");
+ }
+ }
+}
diff --git a/core/src/main/java/com/datastrato/unified_catalog/schema/proto/TenantSerDe.java b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/TenantSerDe.java
new file mode 100644
index 0000000000..07c39df9c8
--- /dev/null
+++ b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/TenantSerDe.java
@@ -0,0 +1,47 @@
+package com.datastrato.unified_catalog.schema.proto;
+
+class TenantSerDe implements ProtoSerDe<com.datastrato.unified_catalog.schema.Tenant, Tenant> {
+ @Override
+ public Tenant serialize(com.datastrato.unified_catalog.schema.Tenant tenant) {
+ Tenant.Builder builder =
+ Tenant.newBuilder()
+ .setId(tenant.getId())
+ .setName(tenant.getName())
+ .setAuditInfo(new AuditInfoSerDe().serialize(tenant.auditInfo()));
+
+ if (tenant.getComment() != null) {
+ builder.setComment(tenant.getComment());
+ }
+
+ SchemaVersion version =
+ SchemaVersion.newBuilder()
+ .setMajorNumber(tenant.getVersion().majorVersion)
+ .setMinorNumber(tenant.getVersion().minorVersion)
+ .build();
+
+ builder.setVersion(version);
+
+ return builder.build();
+ }
+
+ @Override
+ public com.datastrato.unified_catalog.schema.Tenant deserialize(Tenant p) {
+ com.datastrato.unified_catalog.schema.Tenant.Builder builder =
+ new com.datastrato.unified_catalog.schema.Tenant.Builder();
+ builder
+ .withId(p.getId())
+ .withName(p.getName())
+ .withAuditInfo(new AuditInfoSerDe().deserialize(p.getAuditInfo()));
+
+ if (p.hasComment()) {
+ builder.withComment(p.getComment());
+ }
+
+ com.datastrato.unified_catalog.schema.SchemaVersion version =
+ com.datastrato.unified_catalog.schema.SchemaVersion.forValues(
+ p.getVersion().getMajorNumber(), p.getVersion().getMinorNumber());
+ builder.withVersion(version);
+
+ return builder.build();
+ }
+}
diff --git a/core/src/main/java/com/datastrato/unified_catalog/schema/proto/ZoneSerDe.java b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/ZoneSerDe.java
new file mode 100644
index 0000000000..1d3e132560
--- /dev/null
+++ b/core/src/main/java/com/datastrato/unified_catalog/schema/proto/ZoneSerDe.java
@@ -0,0 +1,44 @@
+package com.datastrato.unified_catalog.schema.proto;
+
+class ZoneSerDe implements ProtoSerDe<com.datastrato.unified_catalog.schema.Zone, Zone> {
+ @Override
+ public Zone serialize(com.datastrato.unified_catalog.schema.Zone zone) {
+ Zone.Builder builder =
+ Zone.newBuilder()
+ .setId(zone.getId())
+ .setLakehouseId(zone.getLakehouseId())
+ .setName(zone.getName())
+ .setAuditInfo(new AuditInfoSerDe().serialize(zone.auditInfo()));
+
+ if (zone.getComment() != null) {
+ builder.setComment(zone.getComment());
+ }
+
+ if (zone.getProperties() != null && !zone.getProperties().isEmpty()) {
+ builder.putAllProperties(zone.getProperties());
+ }
+
+ return builder.build();
+ }
+
+ @Override
+ public com.datastrato.unified_catalog.schema.Zone deserialize(Zone p) {
+ com.datastrato.unified_catalog.schema.Zone.Builder builder =
+ new com.datastrato.unified_catalog.schema.Zone.Builder();
+ builder
+ .withId(p.getId())
+ .withLakehouseId(p.getLakehouseId())
+ .withName(p.getName())
+ .withAuditInfo(new AuditInfoSerDe().deserialize(p.getAuditInfo()));
+
+ if (p.hasComment()) {
+ builder.withComment(p.getComment());
+ }
+
+ if (p.getPropertiesCount() > 0) {
+ builder.withProperties(p.getPropertiesMap());
+ }
+
+ return builder.build();
+ }
+}
diff --git a/core/src/test/java/com/datastrato/unified_catalog/schema/proto/TestEntityProtoSerDe.java b/core/src/test/java/com/datastrato/unified_catalog/schema/proto/TestEntityProtoSerDe.java
new file mode 100644
index 0000000000..a017085ca0
--- /dev/null
+++ b/core/src/test/java/com/datastrato/unified_catalog/schema/proto/TestEntityProtoSerDe.java
@@ -0,0 +1,254 @@
+package com.datastrato.unified_catalog.schema.proto;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import io.substrait.type.Type;
+import io.substrait.type.TypeCreator;
+import java.time.Instant;
+import java.util.Map;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestEntityProtoSerDe {
+
+ @Test
+ public void testAuditInfoSerDe() {
+ Instant now = Instant.now();
+ String creator = "creator";
+ String modifier = "modifier";
+ String accessor = "accessor";
+
+ com.datastrato.unified_catalog.schema.AuditInfo auditInfo =
+ new com.datastrato.unified_catalog.schema.AuditInfo.Builder()
+ .withCreator(creator)
+ .withCreateTime(now)
+ .withLastModifier(modifier)
+ .withLastModifiedTime(now)
+ .withLastAccessUser(accessor)
+ .withLastAccessTime(now)
+ .build();
+
+ AuditInfo auditInfoProto = ProtoUtils.toProto(auditInfo);
+ Assertions.assertEquals(creator, auditInfoProto.getCreator());
+ Assertions.assertEquals(now, ProtoUtils.toInstant(auditInfoProto.getCreateTime()));
+ Assertions.assertEquals(modifier, auditInfoProto.getLastModifier());
+ Assertions.assertEquals(now, ProtoUtils.toInstant(auditInfoProto.getLastModifiedTime()));
+ Assertions.assertEquals(accessor, auditInfoProto.getLastAccessUser());
+ Assertions.assertEquals(now, ProtoUtils.toInstant(auditInfoProto.getLastAccessTime()));
+
+ com.datastrato.unified_catalog.schema.AuditInfo auditInfoFromProto =
+ ProtoUtils.fromProto(auditInfoProto);
+ Assertions.assertEquals(auditInfo, auditInfoFromProto);
+
+ // Test with optional fields
+ com.datastrato.unified_catalog.schema.AuditInfo auditInfo1 =
+ new com.datastrato.unified_catalog.schema.AuditInfo.Builder()
+ .withCreator(creator)
+ .withCreateTime(now)
+ .build();
+
+ AuditInfo auditInfoProto1 = ProtoUtils.toProto(auditInfo1);
+
+ Assertions.assertEquals(creator, auditInfoProto1.getCreator());
+ Assertions.assertEquals(now, ProtoUtils.toInstant(auditInfoProto1.getCreateTime()));
+
+ com.datastrato.unified_catalog.schema.AuditInfo auditInfoFromProto1 =
+ ProtoUtils.fromProto(auditInfoProto1);
+ Assertions.assertEquals(auditInfo1, auditInfoFromProto1);
+
+ // Test from/to bytes
+ byte[] bytes = ProtoUtils.toBytes(auditInfo1);
+ com.datastrato.unified_catalog.schema.AuditInfo auditInfoFromBytes =
+ ProtoUtils.fromBytes(
+ bytes, com.datastrato.unified_catalog.schema.AuditInfo.class, AuditInfo.parser());
+ Assertions.assertEquals(auditInfo1, auditInfoFromBytes);
+ }
+
+ @Test
+ public void testEntitiesSerDe() {
+ Instant now = Instant.now();
+ String creator = "creator";
+ Integer tenantId = 1;
+ String tenantName = "tenant";
+ com.datastrato.unified_catalog.schema.SchemaVersion version =
+ com.datastrato.unified_catalog.schema.SchemaVersion.V_0_1;
+ Long lakehouseId = 1L;
+ String lakehouseName = "lakehouse";
+ Map<String, String> props = ImmutableMap.of("k1", "v1", "k2", "v2");
+ Long zoneId = 1L;
+ String zoneName = "zone";
+ Type columnType = TypeCreator.NULLABLE.STRING;
+
+ com.datastrato.unified_catalog.schema.AuditInfo auditInfo =
+ new com.datastrato.unified_catalog.schema.AuditInfo.Builder()
+ .withCreator(creator)
+ .withCreateTime(now)
+ .build();
+
+ // Test Tenant
+ com.datastrato.unified_catalog.schema.Tenant tenant =
+ new com.datastrato.unified_catalog.schema.Tenant.Builder()
+ .withId(tenantId)
+ .withName(tenantName)
+ .withVersion(version)
+ .withAuditInfo(auditInfo)
+ .build();
+
+ Tenant tenantProto = ProtoUtils.toProto(tenant);
+ com.datastrato.unified_catalog.schema.Tenant tenantFromProto =
+ ProtoUtils.fromProto(tenantProto);
+ Assertions.assertEquals(tenant, tenantFromProto);
+
+ byte[] tenantBytes = ProtoUtils.toBytes(tenant);
+ com.datastrato.unified_catalog.schema.Tenant tenantFromBytes =
+ ProtoUtils.fromBytes(
+ tenantBytes, com.datastrato.unified_catalog.schema.Tenant.class, Tenant.parser());
+ Assertions.assertEquals(tenant, tenantFromBytes);
+
+ // Test Lakehouse
+ com.datastrato.unified_catalog.schema.Lakehouse lakehouse =
+ new com.datastrato.unified_catalog.schema.Lakehouse.Builder()
+ .withId(lakehouseId)
+ .withName(lakehouseName)
+ .withProperties(props)
+ .withAuditInfo(auditInfo)
+ .build();
+
+ Lakehouse lakehouseProto = ProtoUtils.toProto(lakehouse);
+ Assertions.assertEquals(props, lakehouseProto.getPropertiesMap());
+ com.datastrato.unified_catalog.schema.Lakehouse lakehouseFromProto =
+ ProtoUtils.fromProto(lakehouseProto);
+ Assertions.assertEquals(lakehouse, lakehouseFromProto);
+
+ byte[] lakehouseBytes = ProtoUtils.toBytes(lakehouse);
+ com.datastrato.unified_catalog.schema.Lakehouse lakehouseFromBytes =
+ ProtoUtils.fromBytes(
+ lakehouseBytes,
+ com.datastrato.unified_catalog.schema.Lakehouse.class,
+ Lakehouse.parser());
+ Assertions.assertEquals(lakehouse, lakehouseFromBytes);
+
+ // Test Lakehouse without props map
+ com.datastrato.unified_catalog.schema.Lakehouse lakehouse1 =
+ new com.datastrato.unified_catalog.schema.Lakehouse.Builder()
+ .withId(lakehouseId)
+ .withName(lakehouseName)
+ .withAuditInfo(auditInfo)
+ .build();
+
+ Lakehouse lakehouseProto1 = ProtoUtils.toProto(lakehouse1);
+ Assertions.assertEquals(0, lakehouseProto1.getPropertiesCount());
+ com.datastrato.unified_catalog.schema.Lakehouse lakehouseFromProto1 =
+ ProtoUtils.fromProto(lakehouseProto1);
+ Assertions.assertEquals(lakehouse1, lakehouseFromProto1);
+
+ byte[] lakehouseBytes1 = ProtoUtils.toBytes(lakehouse1);
+ com.datastrato.unified_catalog.schema.Lakehouse lakehouseFromBytes1 =
+ ProtoUtils.fromBytes(
+ lakehouseBytes1,
+ com.datastrato.unified_catalog.schema.Lakehouse.class,
+ Lakehouse.parser());
+ Assertions.assertEquals(lakehouse1, lakehouseFromBytes1);
+
+ // Test Zone
+ com.datastrato.unified_catalog.schema.Zone zone =
+ new com.datastrato.unified_catalog.schema.Zone.Builder()
+ .withId(zoneId)
+ .withLakehouseId(lakehouseId)
+ .withName(zoneName)
+ .withAuditInfo(auditInfo)
+ .build();
+
+ Zone zoneProto = ProtoUtils.toProto(zone);
+ com.datastrato.unified_catalog.schema.Zone zoneFromProto = ProtoUtils.fromProto(zoneProto);
+ Assertions.assertEquals(zone, zoneFromProto);
+
+ byte[] zoneBytes = ProtoUtils.toBytes(zone);
+ com.datastrato.unified_catalog.schema.Zone zoneFromBytes =
+ ProtoUtils.fromBytes(
+ zoneBytes, com.datastrato.unified_catalog.schema.Zone.class, Zone.parser());
+ Assertions.assertEquals(zone, zoneFromBytes);
+
+ // Test Column
+ com.datastrato.unified_catalog.schema.Column column =
+ new com.datastrato.unified_catalog.schema.Column.Builder()
+ .withId(1)
+ .withEntityId(1L)
+ .withEntitySnapshotId(1L)
+ .withName("col1")
+ .withType(columnType)
+ .withPosition(1)
+ .withAuditInfo(auditInfo)
+ .build();
+
+ Column columnProto = ProtoUtils.toProto(column);
+ com.datastrato.unified_catalog.schema.Column columnFromProto =
+ ProtoUtils.fromProto(columnProto);
+ Assertions.assertEquals(column, columnFromProto);
+ Assertions.assertEquals(columnType, columnFromProto.getType());
+
+ byte[] columnBytes = ProtoUtils.toBytes(column);
+ com.datastrato.unified_catalog.schema.Column columnFromBytes =
+ ProtoUtils.fromBytes(
+ columnBytes, com.datastrato.unified_catalog.schema.Column.class, Column.parser());
+ Assertions.assertEquals(column, columnFromBytes);
+ }
+
+ @Test
+ public void testTableSerDe() {
+ com.datastrato.unified_catalog.schema.Table.TableType tableType =
+ com.datastrato.unified_catalog.schema.Table.TableType.VIRTUAL;
+ com.datastrato.unified_catalog.schema.hasExtraInfo.ExtraInfo extraInfo =
+ new com.datastrato.unified_catalog.schema.VirtualTableInfo(
+ 1, ImmutableList.of("db", "table"));
+ com.datastrato.unified_catalog.schema.AuditInfo auditInfo =
+ new com.datastrato.unified_catalog.schema.AuditInfo.Builder()
+ .withCreator("creator")
+ .withCreateTime(Instant.now())
+ .build();
+
+ com.datastrato.unified_catalog.schema.Table table =
+ new com.datastrato.unified_catalog.schema.Table.Builder()
+ .withId(1L)
+ .withZoneId(1L)
+ .withName("table")
+ .withType(tableType)
+ .withSnapshotId(1L)
+ .withAuditInfo(auditInfo)
+ .withExtraInfo(extraInfo)
+ .build();
+
+ Table tableProto = ProtoUtils.toProto(table);
+ Assertions.assertEquals(tableType.getTypeStr(), tableProto.getType().name());
+
+ com.datastrato.unified_catalog.schema.Table tableFromProto = ProtoUtils.fromProto(tableProto);
+ Assertions.assertEquals(extraInfo, tableFromProto.getExtraInfo());
+ Assertions.assertEquals(table, tableFromProto);
+
+ byte[] tableBytes = ProtoUtils.toBytes(table);
+ com.datastrato.unified_catalog.schema.Table tableFromBytes =
+ ProtoUtils.fromBytes(
+ tableBytes, com.datastrato.unified_catalog.schema.Table.class, Table.parser());
+ Assertions.assertEquals(table, tableFromBytes);
+
+ // Test Table with unsupported type
+ com.datastrato.unified_catalog.schema.Table table1 =
+ new com.datastrato.unified_catalog.schema.Table.Builder()
+ .withId(1L)
+ .withZoneId(1L)
+ .withName("table")
+ .withType(com.datastrato.unified_catalog.schema.Table.TableType.VIEW)
+ .withSnapshotId(1L)
+ .withAuditInfo(auditInfo)
+ .withExtraInfo(extraInfo)
+ .build();
+
+ Throwable exception =
+ Assertions.assertThrows(
+ ProtoSerDeException.class,
+ () -> {
+ ProtoUtils.toProto(table1);
+ });
+ Assertions.assertEquals("Table type VIEW is not supported yet.", exception.getMessage());
+ }
+}
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml
index 4036cbf936..b9d690beae 100644
--- a/gradle/libs.versions.toml
+++ b/gradle/libs.versions.toml
@@ -5,6 +5,9 @@ substrait = "0.9.0"
jackson = "2.13.4"
guava = "29.0-jre"
lombok = "1.18.20"
+slf4j = "2.0.7"
+log4j = "2.20.0"
+
protobuf-plugin = "0.9.2"
spotless-plugin = '6.11.0'
gradle-extensions-plugin = '1.74'
@@ -23,8 +26,14 @@ lombok = { group = "org.projectlombok", name = "lombok", version.ref = "lombok"
junit-jupiter-api = { group = "org.junit.jupiter", name = "junit-jupiter-api", version.ref = "junit" }
junit-jupiter-params = { group = "org.junit.jupiter", name = "junit-jupiter-params", version.ref = "junit" }
junit-jupiter-engine = { group = "org.junit.jupiter", name = "junit-jupiter-engine"}
+slf4j-api = { group = "org.slf4j", name = "slf4j-api", version.ref = "slf4j" }
+log4j-slf4j2-impl = { group = "org.apache.logging.log4j", name = "log4j-slf4j2-impl", version.ref = "log4j" }
+log4j-api = { group = "org.apache.logging.log4j", name = "log4j-api", version.ref = "log4j" }
+log4j-core = { group = "org.apache.logging.log4j", name = "log4j-core", version.ref = "log4j" }
+log4j-12-api = { group = "org.apache.logging.log4j", name = "log4j-1.2-api", version.ref = "log4j" }
[bundles]
+log4j = ["slf4j-api", "log4j-slf4j2-impl", "log4j-api", "log4j-core", "log4j-12-api"]
[plugins]
protobuf = { id = "com.google.protobuf", version.ref = "protobuf-plugin" }
diff --git a/schema/build.gradle.kts b/schema/build.gradle.kts
index e224362c4c..950da1b4d2 100644
--- a/schema/build.gradle.kts
+++ b/schema/build.gradle.kts
@@ -10,7 +10,9 @@ plugins {
dependencies {
implementation(libs.protobuf.java)
- implementation(libs.substrait.java.core)
+ implementation(libs.substrait.java.core) {
+ exclude("org.slf4j")
+ }
}
sourceSets {
diff --git a/schema/src/main/proto/schema.proto b/schema/src/main/proto/schema.proto
index d5ccebe9cf..352290e9db 100644
--- a/schema/src/main/proto/schema.proto
+++ b/schema/src/main/proto/schema.proto
@@ -21,10 +21,10 @@ message SchemaVersion {
message AuditInfo {
string creator = 1;
google.protobuf.Timestamp create_time = 2;
- string last_modifier = 3;
- google.protobuf.Timestamp last_modified_time = 4;
- string last_access_user = 5;
- google.protobuf.Timestamp last_access_time = 6;
+ optional string last_modifier = 3;
+ optional google.protobuf.Timestamp last_modified_time = 4;
+ optional string last_access_user = 5;
+ optional google.protobuf.Timestamp last_access_time = 6;
}
/**
@@ -33,7 +33,7 @@ message AuditInfo {
message Tenant {
uint32 id = 1;
string name = 2;
- string comment = 3;
+ optional string comment = 3;
AuditInfo audit_info = 4;
SchemaVersion version = 5;
}
@@ -45,7 +45,7 @@ message Tenant {
message Lakehouse {
uint64 id = 1;
string name = 2;
- string comment = 3;
+ optional string comment = 3;
map<string, string> properties = 4;
AuditInfo audit_info = 5;
}
@@ -58,7 +58,7 @@ message Zone {
uint64 id = 1;
uint64 lakehouse_id = 2;
string name = 3;
- string comment = 4;
+ optional string comment = 4;
map<string, string> properties = 5;
AuditInfo audit_info = 6;
}
@@ -73,8 +73,9 @@ message Column {
uint64 entity_snapshot_id = 3;
string name = 4;
substrait.Type type = 5;
- string comment = 6;
+ optional string comment = 6;
uint32 position = 7;
+ AuditInfo audit_info = 8;
}
/**
@@ -114,4 +115,5 @@ message Table {
uint64 snapshot_id = 9;
map<string, string> properties = 10;
AuditInfo audit_info = 11;
+ optional string comment = 12;
}
\ No newline at end of file