Skip to content

Commit 4be93d0

Browse files
committed
Added support for RowBinaryFormatWriter
1 parent 84803ff commit 4be93d0

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

client-v2/src/main/java/com/clickhouse/client/api/data_formats/RowBinaryFormatWriter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ public void commitRow() throws IOException {
5454
List<ClickHouseColumn> columnList = tableSchema.getColumns();
5555
for (int i = 0; i < row.length; i++) {
5656
ClickHouseColumn column = columnList.get(i);
57-
57+
// here we skip if we have a default value that is MATERIALIZED or ALIAS or ...
58+
if (column.hasDefault() && column.getDefaultValue() != ClickHouseColumn.DefaultValue.DEFAULT)
59+
continue;
5860
if (RowBinaryFormatSerializer.writeValuePreamble(out, defaultSupport, column, row[i])) {
5961
SerializerUtils.serializeData(out, row[i], column);
6062
}

client-v2/src/test/java/com/clickhouse/client/insert/InsertTests.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,51 @@ public void testAdvancedWriter() throws Exception {
554554
}
555555
}
556556

557+
@Test
558+
public void testWriterWithMaterialize() throws Exception {
559+
String tableName = "table_name_with_materialize";
560+
String tableCreate = "CREATE TABLE \"" + tableName + "\" " +
561+
" (name String, " +
562+
" v1 Float32, " +
563+
" v2 Float32, " +
564+
" attrs Nullable(String), " +
565+
" corrected_time DateTime('UTC') DEFAULT now()," +
566+
" special_attr Nullable(Int8) DEFAULT -1," +
567+
" name_lower String MATERIALIZED lower(name)" +
568+
" ) Engine = MergeTree ORDER by (name)";
569+
570+
initTable(tableName, tableCreate);
571+
572+
ZonedDateTime correctedTime = Instant.now().atZone(ZoneId.of("UTC"));
573+
Object[][] rows = new Object[][] {
574+
{"foo1", 0.3f, 0.6f, "a=1,b=2,c=5", correctedTime, 10},
575+
{"foo2", 0.6f, 0.1f, "a=1,b=2,c=5", correctedTime, null},
576+
{"foo3", 0.7f, 0.4f, "a=1,b=2,c=5", null, null},
577+
{"foo4", 0.8f, 0.5f, null, null, null},
578+
};
579+
580+
TableSchema schema = client.getTableSchema(tableName);
581+
582+
ClickHouseFormat format = ClickHouseFormat.RowBinaryWithDefaults;
583+
try (InsertResponse response = client.insert(tableName, out -> {
584+
RowBinaryFormatWriter w = new RowBinaryFormatWriter(out, schema, format);
585+
for (Object[] row : rows) {
586+
for (int i = 0; i < row.length; i++) {
587+
w.setValue(i + 1, row[i]);
588+
}
589+
w.commitRow();
590+
}
591+
}, format, new InsertSettings()).get()) {
592+
System.out.println("Rows written: " + response.getWrittenRows());
593+
}
594+
595+
List<GenericRecord> records = client.queryAll("SELECT * FROM \"" + tableName + "\"" );
596+
597+
for (GenericRecord record : records) {
598+
System.out.println("> " + record.getString(1) + ", " + record.getFloat(2) + ", " + record.getFloat(3));
599+
}
600+
}
601+
557602
@Test
558603
public void testCollectionInsert() throws Exception {
559604
String tableName = "very_long_table_name_with_uuid_" + UUID.randomUUID().toString().replace('-', '_');

0 commit comments

Comments
 (0)