Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public interface Record {

String RECORD_ERROR_SUPPORT = "talend.component.record.error.support";

String RECORD_NULLABLE_CHECK = "talend.component.record.nullable.check";

/**
* @return the schema of this record.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ public Builder withNewSchema(final Schema newSchema) {
// Entry creation can be optimized a bit but recent GC should not see it as a big deal
public static class BuilderImpl implements Builder {

private final boolean skipNullCheck =
Boolean.parseBoolean(System.getProperty(Record.RECORD_NULLABLE_CHECK, "false"));

private final Map<String, Object> values = new HashMap<>(8);

private final OrderedMap<Schema.Entry> entries;
Expand Down Expand Up @@ -327,7 +330,7 @@ private Schema.Entry validateTypeAgainstProvidedSchema(final String name, final
"Entry '" + entry.getOriginalFieldName() + "' expected to be a " + entry.getType() + ", got a "
+ type);
}
if (value == null && !entry.isNullable()) {
if (!skipNullCheck && value == null && !entry.isNullable()) {
throw new IllegalArgumentException("Entry '" + entry.getOriginalFieldName() + "' is not nullable");
}
return entry;
Expand Down Expand Up @@ -357,7 +360,7 @@ public Record build() {
.filter(it -> !it.isNullable() && !values.containsKey(it.getName()))
.map(Schema.Entry::getName)
.collect(joining(", "));
if (!missing.isEmpty()) {
if (!skipNullCheck && !missing.isEmpty()) {
throw new IllegalArgumentException("Missing entries: " + missing);
}

Expand Down Expand Up @@ -587,10 +590,15 @@ private <T> Builder append(final Schema.Entry entry, final T value) {
} else {
realEntry = entry;
}
if (value != null) {

if (skipNullCheck) {
values.put(realEntry.getName(), value);
} else if (!realEntry.isNullable()) {
throw new IllegalArgumentException(realEntry.getName() + " is not nullable but got a null value");
} else {
if (value != null) {
values.put(realEntry.getName(), value);
} else if (!realEntry.isNullable()) {
throw new IllegalArgumentException(realEntry.getName() + " is not nullable but got a null value");
}
}

if (this.entries != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,57 @@ void notNullableNullBehavior() {
.withString(new SchemaImpl.EntryImpl.BuilderImpl().withNullable(false).withName("test").build(), null));
}

@Test
void disableNullableCheck_no() {
RecordBuilderFactoryImpl recordBuilderFactory = new RecordBuilderFactoryImpl("test");
Schema.Builder schemaBuilder = recordBuilderFactory.newSchemaBuilder(Type.RECORD);
Schema schema = schemaBuilder.withEntry(
recordBuilderFactory.newEntryBuilder().withType(Type.STRING).withName("c1").withNullable(false).build())
.withEntry(recordBuilderFactory.newEntryBuilder()
.withName("c2")
.withType(Type.STRING)
.withNullable(true)
.build())
.build();
Record.Builder recordBuilder = recordBuilderFactory.newRecordBuilder(schema);
recordBuilder.withString("c1", "v1");
recordBuilder.build();

Schema.Builder schemaBuilder2 = recordBuilderFactory.newSchemaBuilder(Type.RECORD);
Schema schema2 = schemaBuilder2.withEntry(
recordBuilderFactory.newEntryBuilder().withType(Type.STRING).withName("c1").withNullable(false).build())
.withEntry(recordBuilderFactory.newEntryBuilder()
.withName("c2")
.withType(Type.STRING)
.withNullable(false)
.build())
.build();
Record.Builder recordBuilder2 = recordBuilderFactory.newRecordBuilder(schema2);
recordBuilder2.withString("c1", "v1");
assertThrows(IllegalArgumentException.class, recordBuilder2::build);
}

@Test
void disableNullableCheck_yes() {
System.setProperty(Record.RECORD_NULLABLE_CHECK, "true");
RecordBuilderFactoryImpl recordBuilderFactory = new RecordBuilderFactoryImpl("test");
Schema.Builder schemaBuilder = recordBuilderFactory.newSchemaBuilder(Type.RECORD);
Schema schema = schemaBuilder.withEntry(
recordBuilderFactory.newEntryBuilder().withType(Type.STRING).withName("c1").withNullable(false).build())
.withEntry(recordBuilderFactory.newEntryBuilder()
.withName("c2")
.withType(Type.STRING)
.withNullable(false)
.build())
.build();
Record.Builder recordBuilder = recordBuilderFactory.newRecordBuilder(schema);
recordBuilder.withString("c1", "v1");
recordBuilder.withString("c2", null);
Record recordOne = recordBuilder.build();
assertNull(recordOne.getString("c2"));
System.setProperty(Record.RECORD_NULLABLE_CHECK, "false");
}

@Test
void dateTime() {
final Schema schema = new SchemaImpl.BuilderImpl()
Expand Down
Loading