Skip to content

Commit

Permalink
[feature] support variant type (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnehil authored Apr 10, 2024
1 parent 5f676bb commit 37c5175
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion spark-doris-connector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<project.scm.id>github</project.scm.id>
<netty.version>4.1.77.Final</netty.version>
<fasterxml.jackson.version>2.13.5</fasterxml.jackson.version>
<thrift-service.version>1.0.0</thrift-service.version>
<thrift-service.version>1.0.1</thrift-service.version>
<testcontainers.version>1.17.6</testcontainers.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ public void convertArrowToRowBatch() throws DorisException {
case "VARCHAR":
case "STRING":
case "JSONB":
case "VARIANT":
Preconditions.checkArgument(mt.equals(Types.MinorType.VARCHAR),
typeMismatchMessage(currentType, mt));
VarCharVector varCharVector = (VarCharVector) curFieldVector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ private[spark] object SchemaUtils {
case "ARRAY" => DataTypes.StringType
case "MAP" => MapType(DataTypes.StringType, DataTypes.StringType)
case "STRUCT" => DataTypes.StringType
case "VARIANT" => DataTypes.StringType
case "HLL" =>
throw new DorisException("Unsupported type " + dorisType)
case _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,4 +850,75 @@ public void testDateTime() throws IOException, DorisException {

}

@Test
public void testVariant() throws DorisException, IOException {

ImmutableList.Builder<Field> childrenBuilder = ImmutableList.builder();
childrenBuilder.add(new Field("k1", FieldType.nullable(new ArrowType.Utf8()), null));

VectorSchemaRoot root = VectorSchemaRoot.create(
new org.apache.arrow.vector.types.pojo.Schema(childrenBuilder.build(), null),
new RootAllocator(Integer.MAX_VALUE));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ArrowStreamWriter arrowStreamWriter = new ArrowStreamWriter(
root,
new DictionaryProvider.MapDictionaryProvider(),
outputStream);

arrowStreamWriter.start();
root.setRowCount(3);

FieldVector vector = root.getVector("k1");
VarCharVector datetimeVector = (VarCharVector)vector;
datetimeVector.setInitialCapacity(3);
datetimeVector.allocateNew();
datetimeVector.setIndexDefined(0);
datetimeVector.setValueLengthSafe(0, 20);
datetimeVector.setSafe(0, "{\"id\":\"a\"}".getBytes());
datetimeVector.setIndexDefined(1);
datetimeVector.setValueLengthSafe(1, 20);
datetimeVector.setSafe(1, "1000".getBytes());
datetimeVector.setIndexDefined(2);
datetimeVector.setValueLengthSafe(2, 20);
datetimeVector.setSafe(2, "123.456".getBytes());
vector.setValueCount(3);

arrowStreamWriter.writeBatch();

arrowStreamWriter.end();
arrowStreamWriter.close();

TStatus status = new TStatus();
status.setStatusCode(TStatusCode.OK);
TScanBatchResult scanBatchResult = new TScanBatchResult();
scanBatchResult.setStatus(status);
scanBatchResult.setEos(false);
scanBatchResult.setRows(outputStream.toByteArray());


String schemaStr = "{\"properties\":[" +
"{\"type\":\"VARIANT\",\"name\":\"k\",\"comment\":\"\"}" +
"], \"status\":200}";

Schema schema = RestService.parseSchema(schemaStr, logger);

RowBatch rowBatch = new RowBatch(scanBatchResult, schema);

Assert.assertTrue(rowBatch.hasNext());
List<Object> actualRow0 = rowBatch.next();
Assert.assertEquals("{\"id\":\"a\"}", actualRow0.get(0));

List<Object> actualRow1 = rowBatch.next();
Assert.assertEquals("1000", actualRow1.get(0));

List<Object> actualRow2 = rowBatch.next();
Assert.assertEquals("123.456", actualRow2.get(0));

Assert.assertFalse(rowBatch.hasNext());
thrown.expect(NoSuchElementException.class);
thrown.expectMessage(startsWith("Get row offset:"));
rowBatch.next();

}

}

0 comments on commit 37c5175

Please sign in to comment.