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

fix: Correctly parse CSV where multiple results include multiple tables #90

Merged
merged 1 commit into from
Feb 28, 2020
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
1. [#85](https://github.com/influxdata/influxdb-client-java/issues/85): Time field in Point supports BigInteger and BigDecimal
1. [#83](https://github.com/influxdata/influxdb-client-java/issues/83): Add reduce operator to FluxDSL

### Bugs
1. [#88](https://github.com/influxdata/influxdb-client-java/pull/88): Correctly parse CSV where multiple results include multiple tables

## 1.5.0 [2020-02-14]

### Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public void parseFluxResponse(@Nonnull final BufferedSource bufferedSource,

try (CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT)) {
int tableIndex = 0;
int tableId = -1;
boolean startNewTable = false;
FluxTable table = null;
for (CSVRecord csvRecord : parser) {
Expand Down Expand Up @@ -165,6 +166,7 @@ public void parseFluxResponse(@Nonnull final BufferedSource bufferedSource,
table = new FluxTable();
consumer.accept(tableIndex, cancellable, table);
tableIndex++;
tableId = -1;

} else if (table == null) {
String message = "Unable to parse CSV response. FluxTable definition was not found.";
Expand All @@ -189,15 +191,19 @@ public void parseFluxResponse(@Nonnull final BufferedSource bufferedSource,
continue;
}

int currentIndex = Integer.parseInt(csvRecord.get(1 + 1));
int currentId = Integer.parseInt(csvRecord.get(1 + 1));
if (tableId == -1) {
tableId = currentId;
}

if (currentIndex > (tableIndex - 1)) {
if (tableId != currentId) {
//create new table with previous column headers settings
List<FluxColumn> fluxColumns = table.getColumns();
table = new FluxTable();
table.getColumns().addAll(fluxColumns);
consumer.accept(tableIndex, cancellable, table);
tableIndex++;
tableId = currentId;
}

FluxRecord fluxRecord = parseRecord(tableIndex - 1, table, csvRecord);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,89 @@ void parsingWithoutTableDefinition() {
.hasMessage("Unable to parse CSV response. FluxTable definition was not found.");
}

@Test
void multipleQueries() throws IOException {

String data ="#datatype,string,long,string,string,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string\n"
+ "#group,false,false,true,true,true,true,false,false,true\n"
+ "#default,t1,,,,,,,,\n"
+ ",result,table,_field,_measurement,_start,_stop,_time,_value,tag\n"
+ ",,0,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:20:00Z,2,test1\n"
+ ",,0,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:21:40Z,2,test1\n"
+ ",,0,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:23:20Z,2,test1\n"
+ ",,0,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:25:00Z,2,test1\n"
+ ",,0,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:26:40Z,2,test1\n"
+ ",,0,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:28:20Z,2,test1\n"
+ ",,0,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:30:00Z,2,test1\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:20:00Z,2,test2\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:21:40Z,2,test2\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:23:20Z,2,test2\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:25:00Z,2,test2\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:26:40Z,2,test2\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:28:20Z,2,test2\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:30:00Z,2,test2\n"
+ "\n"
+ "#datatype,string,long,string,string,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string\n"
+ "#group,false,false,true,true,true,true,false,false,true\n"
+ "#default,t2,,,,,,,,\n"
+ ",result,table,_field,_measurement,_start,_stop,_time,_value,tag\n"
+ ",,0,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:20:00Z,2,test1\n"
+ ",,0,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:21:40Z,2,test1\n"
+ ",,0,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:23:20Z,2,test1\n"
+ ",,0,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:25:00Z,2,test1\n"
+ ",,0,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:26:40Z,2,test1\n"
+ ",,0,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:28:20Z,2,test1\n"
+ ",,0,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:30:00Z,2,test1\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:20:00Z,2,test2\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:21:40Z,2,test2\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:23:20Z,2,test2\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:25:00Z,2,test2\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:26:40Z,2,test2\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:28:20Z,2,test2\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:30:00Z,2,test2";

List<FluxTable> tables = parseFluxResponse(data);
Assertions.assertThat(tables).hasSize(4);
Assertions.assertThat(tables.get(0).getRecords()).hasSize(7);
Assertions.assertThat(tables.get(0).getRecords().get(0).getTable()).isEqualTo(0);
Assertions.assertThat(tables.get(1).getRecords()).hasSize(7);
Assertions.assertThat(tables.get(1).getRecords().get(0).getTable()).isEqualTo(1);
Assertions.assertThat(tables.get(2).getRecords()).hasSize(7);
Assertions.assertThat(tables.get(2).getRecords().get(0).getTable()).isEqualTo(2);
Assertions.assertThat(tables.get(3).getRecords()).hasSize(7);
Assertions.assertThat(tables.get(3).getRecords().get(0).getTable()).isEqualTo(3);
}

@Test
void tableIndexNotStartAtZero() throws IOException {

String data ="#datatype,string,long,string,string,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string\n"
+ "#group,false,false,true,true,true,true,false,false,true\n"
+ "#default,t1,,,,,,,,\n"
+ ",result,table,_field,_measurement,_start,_stop,_time,_value,tag\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:20:00Z,2,test1\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:21:40Z,2,test1\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:23:20Z,2,test1\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:25:00Z,2,test1\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:26:40Z,2,test1\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:28:20Z,2,test1\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:30:00Z,2,test1\n"
+ ",,2,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:20:00Z,2,test2\n"
+ ",,2,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:21:40Z,2,test2\n"
+ ",,2,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:23:20Z,2,test2\n"
+ ",,2,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:25:00Z,2,test2\n"
+ ",,2,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:26:40Z,2,test2\n"
+ ",,2,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:28:20Z,2,test2\n"
+ ",,2,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:30:00Z,2,test2\n";

List<FluxTable> tables = parseFluxResponse(data);
Assertions.assertThat(tables).hasSize(2);
Assertions.assertThat(tables.get(0).getRecords()).hasSize(7);
Assertions.assertThat(tables.get(0).getRecords().get(0).getTable()).isEqualTo(0);
Assertions.assertThat(tables.get(1).getRecords()).hasSize(7);
Assertions.assertThat(tables.get(1).getRecords().get(0).getTable()).isEqualTo(1);
}

@Nonnull
private List<FluxTable> parseFluxResponse(@Nonnull final String data) throws IOException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import com.influxdb.query.FluxColumn;
import com.influxdb.query.FluxRecord;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
Expand All @@ -54,9 +55,6 @@ public void testFluxColumnSerialization() throws IOException, ClassNotFoundExcep

Object copy = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())).readObject();

System.out.println("source = " + source);
System.out.println("copy = " + copy);

Assertions.assertEquals(source.hashCode(), copy.hashCode());
Assertions.assertEquals(source, copy);
}
Expand All @@ -75,9 +73,6 @@ public void testFluxRecordSerialization() throws IOException, ClassNotFoundExce

FluxRecord copy = (FluxRecord) new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())).readObject();

System.out.println("source = " + source);
System.out.println("copy = " + copy);

Assertions.assertEquals(source.getValues(), copy.getValues());
Assertions.assertEquals(source.hashCode(), copy.hashCode());
Assertions.assertEquals(source, copy);
Expand Down
4 changes: 1 addition & 3 deletions client/src/test/java/com/influxdb/client/ITTasksApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -649,13 +649,11 @@ void cancelRunNotExist() throws InterruptedException {
}

@Test
//TODO uncomment after beta
@Disabled
void cancelRunTaskNotExist() {

Assertions.assertThatThrownBy(() -> tasksApi.cancelRun("020f755c3c082000", "020f755c3c082000"))
.isInstanceOf(NotFoundException.class)
.hasMessage("failed to cancel run: run not found");
.hasMessage("failed to cancel run: task not found");
}

@Test
Expand Down