Skip to content
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
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
0.5.0
-----
* Preserve empty (non-null) tuple and UDT field values on read (CASSANALYTICS-209)
* CDC Avro schemas and records fail for quoted CQL names that are invalid in Avro or mixed case (CASSANALYTICS-216)
* Enable TLS for sidecar connections when Truststore is configured (CASSANALYTICS-220)
* Non-frozen UDTs with unset fields or partial updates fail to deserialize in bulk reader and CDC (CASSANALYTICS-217)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,59 @@ public void testTuples()
}));
}

@Test
public void testEmptyTupleFieldsAreNotNull()
{
runTest((partitioner, directory, bridge) -> {
CqlField.CqlTuple tuple = bridge.tuple(bridge.text(), bridge.blob(), bridge.text(), bridge.text());
Object[] actual = tuple.deserializeTuple(emptyAndNullCompositeFields(false), false);

assertThat(actual[0]).isEqualTo("");
assertThat(((ByteBuffer) actual[1]).remaining()).isZero();
assertThat(actual[2]).isNull();
assertThat(actual[3]).isEqualTo("x");
});
}

@Test
public void testEmptyUdtFieldsAreNotNull()
{
runTest((partitioner, directory, bridge) -> {
CqlField.CqlUdt udt = bridge.udt("keyspace", "empty_fields")
.withField("text_value", bridge.text())
.withField("blob_value", bridge.blob())
.withField("null_value", bridge.text())
.withField("tail_value", bridge.text())
.build();

// TODO: Add non-frozen option once CASSANALYTICS-217 is fixed.
for (boolean isFrozen : new boolean[]{true})
{
Map<String, Object> actual = udt.deserializeUdt(getSparkSql(bridge),
emptyAndNullCompositeFields(!isFrozen), isFrozen);
assertThat(actual.get("text_value")).isEqualTo(UTF8String.fromString(""));
assertThat((byte[]) actual.get("blob_value")).isEmpty();
assertThat(actual.get("null_value")).isNull();
assertThat(actual.get("tail_value")).isEqualTo(UTF8String.fromString("x"));
}
});
}

private static ByteBuffer emptyAndNullCompositeFields(boolean includeFieldCount)
{
ByteBuffer buffer = ByteBuffer.allocate((includeFieldCount ? Integer.BYTES : 0) + 5 * Integer.BYTES + 1);
if (includeFieldCount)
{
buffer.putInt(5);
}
buffer.putInt(0); // empty text
buffer.putInt(0); // empty blob
buffer.putInt(-1); // NULL text
buffer.putInt(1).put((byte) 'x'); // non-empty trailing text
buffer.flip();
return buffer;
}

// test utilities

static Object toInt(CassandraBridge bridge, Object value)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cassandra.analytics;

import java.util.List;

import org.junit.jupiter.api.Test;

import org.apache.cassandra.distributed.api.ConsistencyLevel;
import org.apache.cassandra.distributed.api.IInstance;
import org.apache.cassandra.sidecar.testing.QualifiedName;
import org.apache.spark.sql.Row;

import static org.apache.cassandra.testing.TestUtils.DC1_RF1;
import static org.apache.cassandra.testing.TestUtils.TEST_KEYSPACE;
import static org.apache.cassandra.testing.TestUtils.uniqueTestTableFullName;
import static org.assertj.core.api.Assertions.assertThat;

public class BulkReaderEmptyCompositeValuesTest extends SharedClusterSparkIntegrationTestBase
{
private static final String UDT_NAME = "test_empty_fields";

private static final String EMPTY_FROZEN_UDT = "{text_value: '', blob_value: 0x, null_value: null, tail_value: 'tail'}";
// TODO: Set null_value to NULL once CASSANALYTICS-217 is fixed.
private static final String EMPTY_NON_FROZEN_UDT = "{text_value: '', blob_value: 0x, null_value: 'present', tail_value: 'tail'}";

private final QualifiedName tupleTable = uniqueTestTableFullName(TEST_KEYSPACE);
private final QualifiedName frozenUdtTable = uniqueTestTableFullName(TEST_KEYSPACE);
private final QualifiedName nonFrozenUdtTable = uniqueTestTableFullName(TEST_KEYSPACE);

@Test
public void testEmptyTupleComponents()
{
Row tuple = readCompositeValue(tupleTable);

assertThat(tuple.getString(0)).isEmpty();
assertThat((byte[]) tuple.get(1)).isEmpty();
assertThat(tuple.isNullAt(2)).isTrue();
assertThat(tuple.getString(3)).isEqualTo("tail");
}

@Test
public void testEmptyFrozenUdtFields()
{
assertEmptyUdtFields(readCompositeValue(frozenUdtTable), true);
}

@Test
public void testEmptyNonFrozenUdtFields()
{
assertEmptyUdtFields(readCompositeValue(nonFrozenUdtTable), false);
}

private Row readCompositeValue(QualifiedName table)
{
List<Row> rows = bulkReaderDataFrame(table).load().collectAsList();
assertThat(rows).hasSize(1);

Row value = rows.get(0).getStruct(rows.get(0).fieldIndex("data"));
assertThat(value).isNotNull();

return value;
}

private static void assertEmptyUdtFields(Row value, boolean frozen)
{
assertThat((String) value.getAs("text_value")).isEmpty();
assertThat((byte[]) value.getAs("blob_value")).isEmpty();
if (frozen)
{
assertThat(value.isNullAt(value.fieldIndex("null_value"))).isTrue();
}
else
{
// TODO: Remove once CASSANALYTICS-217 is fixed.
assertThat((String) value.getAs("null_value")).isEqualTo("present");
}
assertThat((String) value.getAs("tail_value")).isEqualTo("tail");
}

@Override
protected void initializeSchemaForTest()
{
createTestKeyspace(TEST_KEYSPACE, DC1_RF1);

cluster.schemaChangeIgnoringStoppedInstances(String.format("CREATE TYPE %s.%s (" +
"text_value text, " +
"blob_value blob, " +
"null_value text, " +
"tail_value text)",
TEST_KEYSPACE, UDT_NAME));

createTestTable(tupleTable, "CREATE TABLE %s (" +
"id int PRIMARY KEY, " +
"data frozen<tuple<text, blob, text, text>>)");

createTestTable(frozenUdtTable, "CREATE TABLE %s (" +
"id int PRIMARY KEY, " +
"data frozen<" + UDT_NAME + ">)");

createTestTable(nonFrozenUdtTable, "CREATE TABLE %s (" +
"id int PRIMARY KEY, " +
"data " + UDT_NAME + ")");

IInstance node = cluster.getFirstRunningInstance();

node.coordinator().execute(String.format("INSERT INTO %s (id, data) VALUES (1, ('', 0x, null, 'tail'))",
tupleTable),
ConsistencyLevel.ALL);

node.coordinator().execute(String.format("INSERT INTO %s (id, data) VALUES (1, %s)",
frozenUdtTable, EMPTY_FROZEN_UDT),
ConsistencyLevel.ALL);

node.coordinator().execute(String.format("INSERT INTO %s (id, data) VALUES (1, %s)",
nonFrozenUdtTable, EMPTY_NON_FROZEN_UDT),
ConsistencyLevel.ALL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public Object[] deserializeTuple(ByteBuffer buffer, boolean isFrozen)
break;
}
int length = buffer.getInt();
result[position++] = length > 0 ? type.deserializeToJavaType(ByteBufferUtils.readBytes(buffer, length), isFrozen) : null;
result[position++] = length >= 0 ? type.deserializeToJavaType(ByteBufferUtils.readBytes(buffer, length), isFrozen) : null;
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public Map<String, Object> deserializeUdt(TypeConverter typeConverter, ByteBuffe
break;
}
int length = buffer.getInt();
result.put(field.name(), length > 0 ? field.deserializeToType(typeConverter, ByteBufferUtils.readBytes(buffer, length), isFrozen) : null);
result.put(field.name(), length >= 0 ? field.deserializeToType(typeConverter, ByteBufferUtils.readBytes(buffer, length), isFrozen) : null);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public Map<String, Object> deserializeUdt(TypeConverter typeConverter, ByteBuffe
break;
}
int length = buffer.getInt();
result.put(field.name(), length > 0 ? field.deserializeToType(typeConverter, ByteBufferUtils.readBytes(buffer, length), isFrozen) : null);
result.put(field.name(), length >= 0 ? field.deserializeToType(typeConverter, ByteBufferUtils.readBytes(buffer, length), isFrozen) : null);
}

return result;
Expand Down
Loading