Skip to content

Commit

Permalink
[CALCITE-1458] Add column values to the deprecated protobuf attribute
Browse files Browse the repository at this point in the history
When we omit adding these column values to the deprecated `values`
attribute, older clients may suddenly stop seeing records which
they previously could see on query.

Closes apache#314
  • Loading branch information
joshelser committed Oct 21, 2016
1 parent ab25d36 commit 6010ce1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
10 changes: 8 additions & 2 deletions avatica/core/src/main/java/org/apache/calcite/avatica/Meta.java
Original file line number Diff line number Diff line change
Expand Up @@ -940,13 +940,19 @@ public Common.Frame toProto() {
List<?> list = (List<?>) element;
// Add each element in the list/array to the column's value
for (Object listItem : list) {
columnBuilder.addArrayValue(serializeScalar(listItem));
final Common.TypedValue scalarListItem = serializeScalar(listItem);
columnBuilder.addArrayValue(scalarListItem);
// Add the deprecated 'value' repeated attribute for backwards compat
columnBuilder.addValue(scalarListItem);
}
} else {
// The default value, but still explicit.
columnBuilder.setHasArrayValue(false);
// Only one value for this column, a scalar.
columnBuilder.setScalarValue(serializeScalar(element));
final Common.TypedValue scalarVal = serializeScalar(element);
columnBuilder.setScalarValue(scalarVal);
// Add the deprecated 'value' repeated attribute for backwards compat
columnBuilder.addValue(scalarVal);
}

// Add value to row
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -160,6 +161,51 @@ public void testMultipleRows() {
Object array = Frame.parseColumn(arrayValue);
assertEquals(Arrays.asList(1L, 1L), array);
}

@Test public void testDeprecatedValueAttributeForScalars() {
// Create a row with schema: [VARCHAR, INTEGER, DATE]
List<Object> rows = Collections.<Object>singletonList(new Object[] {"string", Integer.MAX_VALUE,
new Date().getTime()});
Meta.Frame frame = Meta.Frame.create(0, true, rows);
// Convert it to a protobuf
Common.Frame protoFrame = frame.toProto();
assertEquals(1, protoFrame.getRowsCount());
// Get that row we created
Common.Row protoRow = protoFrame.getRows(0);
// One row has many columns
List<Common.ColumnValue> protoColumns = protoRow.getValueList();
assertEquals(3, protoColumns.size());
// Verify that the scalar value is also present in the deprecated values attributes.
List<Common.TypedValue> deprecatedValues = protoColumns.get(0).getValueList();
assertEquals(1, deprecatedValues.size());
Common.TypedValue scalarValue = protoColumns.get(0).getScalarValue();
assertEquals(deprecatedValues.get(0), scalarValue);
}

@Test public void testDeprecatedValueAttributeForArrays() {
// Create a row with schema: [VARCHAR, ARRAY]
List<Object> rows = Collections.<Object>singletonList(new Object[] {"string",
Arrays.asList(1, 2, 3)});
Meta.Frame frame = Meta.Frame.create(0, true, rows);
// Convert it to a protobuf
Common.Frame protoFrame = frame.toProto();
assertEquals(1, protoFrame.getRowsCount());
// Get that row we created
Common.Row protoRow = protoFrame.getRows(0);
// One row has many columns
List<Common.ColumnValue> protoColumns = protoRow.getValueList();
// We should have two columns
assertEquals(2, protoColumns.size());
// Fetch the ARRAY column
Common.ColumnValue protoColumn = protoColumns.get(1);
// We should have the 3 ARRAY elements in the array_values attribute as well as the deprecated
// values attribute.
List<Common.TypedValue> deprecatedValues = protoColumn.getValueList();
assertEquals(3, deprecatedValues.size());
assertTrue("Column 2 should have an array_value", protoColumns.get(1).getHasArrayValue());
List<Common.TypedValue> arrayValues = protoColumns.get(1).getArrayValueList();
assertEquals(arrayValues, deprecatedValues);
}
}

// End FrameTest.java

0 comments on commit 6010ce1

Please sign in to comment.