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

Added NullType support #293

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tech.ydb.table.result.impl;

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


/**
* @author Aleksandr Gorshenin
*/
final class ProtoNullValueReader extends AbstractValueReader {
static final ProtoNullValueReader INSTANCE = new ProtoNullValueReader();

private ProtoNullValueReader() { }

@Override
protected ValueProtos.Type getProtoType() {
return ProtoType.getNull();
}

@Override
protected ValueProtos.Value getProtoValue() {
return ProtoValue.nullValue();
}

@Override
protected void setProtoValue(ValueProtos.Value value) {
// skip
}

@Override
public void toString(StringBuilder sb) {
sb.append("Null");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ static AbstractValueReader forTypeImpl(ValueProtos.Type type) {
return variantReader(type);
case VOID_TYPE:
return ProtoVoidValueReader.INSTANCE;
case NULL_TYPE:
return ProtoNullValueReader.INSTANCE;
default:
throw new IllegalStateException("unsupported type: " + type);
}
Expand Down
36 changes: 36 additions & 0 deletions table/src/main/java/tech/ydb/table/values/NullType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package tech.ydb.table.values;


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


/**
* @author Aleksandr Gorshenin
*/
public final class NullType implements Type {

private static final NullType INSTANCE = new NullType();

private NullType() {
}

public static NullType of() {
return NullType.INSTANCE;
}

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

@Override
public String toString() {
return "Null";
}

@Override
public ValueProtos.Type toPb() {
return ProtoType.getNull();
}
}
35 changes: 35 additions & 0 deletions table/src/main/java/tech/ydb/table/values/NullValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tech.ydb.table.values;

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


/**
* @author Aleksandr Gorshenin
*/
public class NullValue implements Value<NullType> {

private static final NullValue INSTANCE = new NullValue();

private NullValue() {
}

public static NullValue of() {
return INSTANCE;
}

@Override
public String toString() {
return "Null";
}

@Override
public NullType getType() {
return NullType.of();
}

@Override
public ValueProtos.Value toPb() {
return ProtoValue.nullValue();
}
}
1 change: 1 addition & 0 deletions table/src/main/java/tech/ydb/table/values/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum Kind {
DICT,
VARIANT,
VOID,
NULL,
PG_TYPE;
}
}
12 changes: 12 additions & 0 deletions table/src/main/java/tech/ydb/table/values/proto/ProtoType.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tech.ydb.table.values.DecimalType;
import tech.ydb.table.values.DictType;
import tech.ydb.table.values.ListType;
import tech.ydb.table.values.NullType;
import tech.ydb.table.values.OptionalType;
import tech.ydb.table.values.PgType;
import tech.ydb.table.values.PrimitiveType;
Expand All @@ -32,6 +33,10 @@ public class ProtoType {
.setVoidType(NullValue.NULL_VALUE)
.build();

private static final ValueProtos.Type NULL = ValueProtos.Type.newBuilder()
.setNullType(NullValue.NULL_VALUE)
.build();

private static final ValueProtos.Type BOOL = primitiveType(PrimitiveTypeId.BOOL);
private static final ValueProtos.Type INT_8 = primitiveType(PrimitiveTypeId.INT8);
private static final ValueProtos.Type UINT_8 = primitiveType(PrimitiveTypeId.UINT8);
Expand Down Expand Up @@ -334,6 +339,10 @@ public static ValueProtos.Type getVoid() {
return VOID;
}

public static ValueProtos.Type getNull() {
return NULL;
}

public static ValueProtos.Type getPgType(int oid, int typlen, int typmod) {
return ValueProtos.Type.newBuilder()
.setPgType(ValueProtos.PgType.newBuilder()
Expand Down Expand Up @@ -417,6 +426,9 @@ public static Type fromPb(ValueProtos.Type type) {
case VOID_TYPE:
return VoidType.of();

case NULL_TYPE:
return NullType.of();

case PG_TYPE:
ValueProtos.PgType pgType = type.getPgType();
return PgType.of(pgType.getOid(), pgType.getTyplen(), pgType.getTypmod());
Expand Down
21 changes: 18 additions & 3 deletions table/src/main/java/tech/ydb/table/values/proto/ProtoValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import com.google.common.collect.Maps;
import com.google.protobuf.ByteString;
import com.google.protobuf.NullValue;
import com.google.protobuf.UnsafeByteOperations;

import tech.ydb.proto.ValueProtos;
Expand All @@ -25,6 +24,7 @@
import tech.ydb.table.values.DecimalValue;
import tech.ydb.table.values.DictType;
import tech.ydb.table.values.ListType;
import tech.ydb.table.values.NullValue;
import tech.ydb.table.values.OptionalType;
import tech.ydb.table.values.PrimitiveType;
import tech.ydb.table.values.PrimitiveValue;
Expand All @@ -36,6 +36,8 @@
import tech.ydb.table.values.VariantType;
import tech.ydb.table.values.VoidValue;

import static tech.ydb.table.values.Type.Kind.VOID;


/**
* @author Sergey Polovko
Expand All @@ -50,11 +52,15 @@ public class ProtoValue {
private static final ValueProtos.Value EMPTY = ValueProtos.Value.newBuilder().build();

private static final ValueProtos.Value EMPTY_OPTIONAL = ValueProtos.Value.newBuilder()
.setNullFlagValue(NullValue.NULL_VALUE)
.setNullFlagValue(com.google.protobuf.NullValue.NULL_VALUE)
.build();

private static final ValueProtos.Value VOID = ValueProtos.Value.newBuilder()
.setNullFlagValue(NullValue.NULL_VALUE)
.setNullFlagValue(com.google.protobuf.NullValue.NULL_VALUE)
.build();

private static final ValueProtos.Value NULL = ValueProtos.Value.newBuilder()
.setNullFlagValue(com.google.protobuf.NullValue.NULL_VALUE)
.build();

private ProtoValue() { }
Expand Down Expand Up @@ -650,6 +656,12 @@ public static ValueProtos.Value voidValue() {
return VOID;
}

// -- null value --

public static ValueProtos.Value nullValue() {
return NULL;
}

// -- from proto --

public static Value<?> fromPb(Type type, ValueProtos.Value value) {
Expand Down Expand Up @@ -749,6 +761,9 @@ public static Value<?> fromPb(Type type, ValueProtos.Value value) {
case VOID:
return VoidValue.of();

case NULL:
return NullValue.of();

default:
throw new IllegalStateException("unknown type kind: " + type.getKind());
}
Expand Down
57 changes: 57 additions & 0 deletions table/src/test/java/tech/ydb/table/integration/NullReadTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package tech.ydb.table.integration;

import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Test;

import tech.ydb.table.SessionRetryContext;
import tech.ydb.table.impl.SimpleTableClient;
import tech.ydb.table.query.DataQueryResult;
import tech.ydb.table.result.ResultSetReader;
import tech.ydb.table.result.ValueReader;
import tech.ydb.table.rpc.grpc.GrpcTableRpc;
import tech.ydb.table.transaction.TxControl;
import tech.ydb.table.values.NullType;
import tech.ydb.table.values.NullValue;
import tech.ydb.table.values.PrimitiveType;
import tech.ydb.test.junit4.GrpcTransportRule;

/**
*
* @author Aleksandr Gorshenin
*/
public class NullReadTest {
@ClassRule
public static final GrpcTransportRule YDB_TRANSPORT = new GrpcTransportRule();
private static final SessionRetryContext CTX = SessionRetryContext.create(SimpleTableClient.newClient(
GrpcTableRpc.useTransport(YDB_TRANSPORT)
).build()).build();

@Test
public void nullReadTest() {
DataQueryResult result = CTX.supplyResult(
s -> s.executeDataQuery("SELECT '1' AS p1, 123 AS p2, NULL AS p3", TxControl.serializableRw())
).join().getValue();

Assert.assertEquals(1, result.getResultSetCount());

ResultSetReader rs = result.getResultSet(0);
Assert.assertTrue(rs.next());

ValueReader p1 = rs.getColumn("p1");
ValueReader p2 = rs.getColumn("p2");
ValueReader p3 = rs.getColumn("p3");

Assert.assertNotNull(p1);
Assert.assertNotNull(p2);
Assert.assertNotNull(p3);

Assert.assertSame(PrimitiveType.Bytes, p1.getType());
Assert.assertSame(PrimitiveType.Int32, p2.getType());
Assert.assertSame(NullType.of(), p3.getType());

Assert.assertArrayEquals(new byte[] { '1' }, p1.getBytes());
Assert.assertEquals(123, p2.getInt32());
Assert.assertSame(NullValue.of(), p3.getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@

import javax.annotation.Nonnull;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;

import tech.ydb.core.StatusCode;
import tech.ydb.core.UnexpectedResultException;
import tech.ydb.table.SessionRetryContext;
import tech.ydb.table.description.TableDescription;
import tech.ydb.table.impl.SimpleTableClient;
Expand All @@ -21,11 +25,6 @@
import tech.ydb.table.values.StructValue;
import tech.ydb.test.junit4.GrpcTransportRule;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;


public class TableQueryTest {
@ClassRule
Expand Down Expand Up @@ -172,11 +171,11 @@ public void testReadRowsEmptyColumns() {
).join().getValue().getResultSetReader();

Assert.assertTrue(rsr.next());
Assert.assertEquals(rsr.getColumn("series_id").getUint64(), 1);
Assert.assertEquals(1, rsr.getColumn("series_id").getUint64());
Assert.assertEquals("Once I rose above the noise and confusion", rsr.getColumn("title").getText());
Assert.assertEquals("Carry on my wayward son", rsr.getColumn("series_info").getText());
Assert.assertTrue(rsr.next());
Assert.assertEquals(rsr.getColumn("series_id").getUint64(), 2);
Assert.assertEquals(2, rsr.getColumn("series_id").getUint64());
Assert.assertEquals("There'll be peace when you are done", rsr.getColumn("title").getText());
Assert.assertEquals("Lay your weary head to rest", rsr.getColumn("series_info").getText());
Assert.assertFalse(rsr.next());
Expand Down