Skip to content

Commit

Permalink
Merge pull request #288 from alex268/master
Browse files Browse the repository at this point in the history
Fixed exception on PgType usage
  • Loading branch information
alex268 authored Jul 2, 2024
2 parents cf1fef2 + ba71b96 commit 30f8df0
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
68 changes: 68 additions & 0 deletions table/src/main/java/tech/ydb/table/values/PgType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package tech.ydb.table.values;

import java.util.Objects;

import tech.ydb.proto.ValueProtos;
import tech.ydb.table.values.proto.ProtoType;

/**
*
* @author Aleksandr Gorshenin
*/
public class PgType implements Type {
private final int oid;
private final int typlen;
private final int typmod;

private PgType(int oid, int typlen, int typmod) {
this.oid = oid;
this.typlen = typlen;
this.typmod = typmod;
}

public static PgType of(int oid) {
return new PgType(oid, 0, 0);
}

public static PgType of(int oid, int typlen, int typmod) {
return new PgType(oid, typlen, typmod);
}

public int getOid() {
return oid;
}

@Override
public Kind getKind() {
return Kind.PG_TYPE;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

PgType that = (PgType) o;
return oid == that.oid && typlen == that.typlen && typmod == that.typmod;
}

@Override
public int hashCode() {
return Objects.hash(oid, typlen, typmod);
}

@Override
public String toString() {
return "PgType[" + oid + "]";
}

@Override
public ValueProtos.Type toPb() {
return ProtoType.getPgType(oid, typlen, typmod);
}
}
3 changes: 2 additions & 1 deletion table/src/main/java/tech/ydb/table/values/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum Kind {
STRUCT,
DICT,
VARIANT,
VOID;
VOID,
PG_TYPE;
}
}
16 changes: 15 additions & 1 deletion table/src/main/java/tech/ydb/table/values/proto/ProtoType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
import tech.ydb.table.values.DictType;
import tech.ydb.table.values.ListType;
import tech.ydb.table.values.OptionalType;
import tech.ydb.table.values.PgType;
import tech.ydb.table.values.PrimitiveType;
import tech.ydb.table.values.StructType;
import tech.ydb.table.values.TupleType;
import tech.ydb.table.values.Type;
import tech.ydb.table.values.VariantType;
import tech.ydb.table.values.VoidType;


/**
* @author Sergey Polovko
*/
Expand Down Expand Up @@ -334,6 +334,16 @@ public static ValueProtos.Type getVoid() {
return VOID;
}

public static ValueProtos.Type getPgType(int oid, int typlen, int typmod) {
return ValueProtos.Type.newBuilder()
.setPgType(ValueProtos.PgType.newBuilder()
.setOid(typlen)
.setTyplen(typlen)
.setTypmod(typlen)
.build()
).build();
}

public static Type fromPb(ValueProtos.Type type) {
switch (type.getTypeCase()) {
case TYPE_ID:
Expand Down Expand Up @@ -407,6 +417,10 @@ public static Type fromPb(ValueProtos.Type type) {
case VOID_TYPE:
return VoidType.of();

case PG_TYPE:
ValueProtos.PgType pgType = type.getPgType();
return PgType.of(pgType.getOid(), pgType.getTyplen(), pgType.getTypmod());

default:
throw new IllegalStateException("unknown type: " + type.getTypeCase());
}
Expand Down

0 comments on commit 30f8df0

Please sign in to comment.