Skip to content

Commit 7765eec

Browse files
committed
Reject negative schemaInferMaxRecords
1 parent 9885fc2 commit 7765eec

4 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎core/src/main/java/org/apache/datafusion/CsvReadOptions.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public CsvReadOptions newlinesInValues(boolean v) {
8181
}
8282

8383
public CsvReadOptions schemaInferMaxRecords(long n) {
84+
if (n < 0) {
85+
throw new IllegalArgumentException("schemaInferMaxRecords must be non-negative, got " + n);
86+
}
8487
this.schemaInferMaxRecords = n;
8588
return this;
8689
}

‎core/src/main/java/org/apache/datafusion/NdJsonReadOptions.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public NdJsonReadOptions fileCompressionType(FileCompressionType t) {
4949
}
5050

5151
public NdJsonReadOptions schemaInferMaxRecords(long n) {
52+
if (n < 0) {
53+
throw new IllegalArgumentException("schemaInferMaxRecords must be non-negative, got " + n);
54+
}
5255
this.schemaInferMaxRecords = n;
5356
return this;
5457
}

‎core/src/test/java/org/apache/datafusion/CsvReadOptionsTest.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.jupiter.api.Assertions.assertEquals;
2323
import static org.junit.jupiter.api.Assertions.assertFalse;
2424
import static org.junit.jupiter.api.Assertions.assertSame;
25+
import static org.junit.jupiter.api.Assertions.assertThrows;
2526
import static org.junit.jupiter.api.Assertions.assertTrue;
2627

2728
import java.util.List;
@@ -107,4 +108,14 @@ void allCompressionTypesMapThroughProto() throws InvalidProtocolBufferException
107108
"mismatch for " + t);
108109
}
109110
}
111+
112+
@Test
113+
void schemaInferMaxRecordsRejectsNegative() {
114+
// The proto wire field is uint64, so a negative long would be reinterpreted
115+
// as a huge unsigned value on the Rust side and silently expand schema
116+
// inference across the full dataset. Reject at the Java setter instead.
117+
CsvReadOptions opts = new CsvReadOptions();
118+
assertThrows(IllegalArgumentException.class, () -> opts.schemaInferMaxRecords(-1L));
119+
assertThrows(IllegalArgumentException.class, () -> opts.schemaInferMaxRecords(Long.MIN_VALUE));
120+
}
110121
}

‎core/src/test/java/org/apache/datafusion/NdJsonReadOptionsTest.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.jupiter.api.Assertions.assertEquals;
2323
import static org.junit.jupiter.api.Assertions.assertFalse;
2424
import static org.junit.jupiter.api.Assertions.assertSame;
25+
import static org.junit.jupiter.api.Assertions.assertThrows;
2526

2627
import java.util.List;
2728

@@ -85,4 +86,14 @@ void allCompressionTypesMapThroughProto() throws InvalidProtocolBufferException
8586
"mismatch for " + t);
8687
}
8788
}
89+
90+
@Test
91+
void schemaInferMaxRecordsRejectsNegative() {
92+
// The proto wire field is uint64, so a negative long would be reinterpreted
93+
// as a huge unsigned value on the Rust side and silently expand schema
94+
// inference across the full dataset. Reject at the Java setter instead.
95+
NdJsonReadOptions opts = new NdJsonReadOptions();
96+
assertThrows(IllegalArgumentException.class, () -> opts.schemaInferMaxRecords(-1L));
97+
assertThrows(IllegalArgumentException.class, () -> opts.schemaInferMaxRecords(Long.MIN_VALUE));
98+
}
8899
}

0 commit comments

Comments
 (0)