Skip to content

Commit

Permalink
simplify data decoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
youngsofun committed Jan 7, 2025
1 parent 347b71a commit 602619f
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 619 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,25 @@ private ParseJsonDataUtils() {
* input List<List<Object>> : a list of rows parsed from QueryResponse
* output Iterable<List<Object>> : convert the input rows into DatabendType and return an immutable list
*/
public static List<List<Object>> parseRawData(List<QueryRowField> schema, List<List<Object>> data) {
public static List<List<Object>> parseRawData(List<QueryRowField> schema, List<List<String>> data) {
if (data == null || schema == null) {
return null;
}
ColumnTypeHandler[] typeHandlers = createTypeHandlers(schema);
// ensure parsed data is thread safe
ImmutableList.Builder<List<Object>> rows = ImmutableList.builderWithExpectedSize(data.size());
for (List<Object> row : data) {
for (List<String> row : data) {
if (row.size() != typeHandlers.length) {
throw new IllegalArgumentException("row / column does not match schema");
}
ArrayList<Object> newRow = new ArrayList<>(typeHandlers.length);
int column = 0;
for (Object value : row) {
for (String value : row) {
Object parsed = null;
if (value != null) {
value = typeHandlers[column].parseValue(value);
parsed = typeHandlers[column].parseValue(value);
}
newRow.add(value);
newRow.add(parsed);
column++;
}
rows.add(unmodifiableList(newRow)); // allow nulls in list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class QueryResults {
private final String sessionId;
private final DatabendSession session;
private final List<QueryRowField> schema;
private final List<List<Object>> data;
private final List<List<String>> data;
private final String state;
private final QueryErrors error;
private final QueryStats stats;
Expand All @@ -47,7 +47,7 @@ public QueryResults(
@JsonProperty("session_id") String sessionId,
@JsonProperty("session") DatabendSession session,
@JsonProperty("schema") List<QueryRowField> schema,
@JsonProperty("data") List<List<Object>> data,
@JsonProperty("data") List<List<String>> data,
@JsonProperty("state") String state,
@JsonProperty("error") QueryErrors error,
@JsonProperty("stats") QueryStats stats,
Expand All @@ -61,7 +61,7 @@ public QueryResults(
this.sessionId = sessionId;
this.session = session;
this.schema = schema;
this.data = ParseJsonDataUtils.parseRawData(schema, data);
this.data = data;
this.state = state;
this.error = error;
this.stats = stats;
Expand Down Expand Up @@ -101,6 +101,11 @@ public List<QueryRowField> getSchema() {

@JsonProperty
public List<List<Object>> getData() {
return ParseJsonDataUtils.parseRawData(schema, data);
}

@JsonProperty
public List<List<String>> getDataRaw() {
return data;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface ColumnTypeHandler {
* @param value raw input row value
* @return parsed java type value
*/
Object parseValue(Object value);
Object parseValue(String value);

void setNullable(boolean isNullable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.databend.client.data;

public abstract class ColumnTypeHandlerBase implements ColumnTypeHandler {
private boolean isNullable;

public ColumnTypeHandlerBase(boolean isNullable) {
this.isNullable = isNullable;
}
public boolean isNull(String value) {
if (value == null || value.equals("NULL")) {
if (isNullable) {
return true;
} else {
throw new IllegalArgumentException("type is not nullable");
}
}
return false;
}

@Override
public Object parseValue(String value) {
if (isNull(value)) {
return null;
}
return parseString(value);
}

@Override
public void setNullable(boolean isNullable) {
this.isNullable = isNullable;
}

abstract Object parseString(String value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,22 @@

package com.databend.client.data;

public class GeometryHandler implements ColumnTypeHandler {

private boolean isNullable;

public class GeometryHandler extends ColumnTypeHandlerBase {
public GeometryHandler(boolean isNullable) {
this.isNullable = isNullable;
super(isNullable);
}

@Override
public Object parseValue(Object value) {
if (value == null) {
if (isNullable) {
return null;
} else {
throw new IllegalArgumentException("Geometry type is not nullable");
}
}
if (value instanceof String) {
String wkbOrWkt = (String) value;
// binary wkb is converted to string during rest data transfer
if (wkbOrWkt.startsWith("00") || wkbOrWkt.startsWith("01")) {
return hexStringToByteArray(wkbOrWkt);
}
// todo We are not distinguishing between geo_json and wkt for the time being, they are now both in text format
// If we have a separate object to describe the variant type, we can consider handling geo_json
public Object parseString(String value) {
// binary wkb is converted to string during rest data transfer
if (value.startsWith("00") || value.startsWith("01")) {
return hexStringToByteArray(value);
}
// todo We are not distinguishing between geo_json and wkt for the time being, they are now both in text format
// If we have a separate object to describe the variant type, we can consider handling geo_json
return value;
}

@Override
public void setNullable(boolean isNullable) {
this.isNullable = isNullable;
}

/**
* Write the hexadecimal text back as a binary array
*/
Expand Down
Loading

0 comments on commit 602619f

Please sign in to comment.