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
25 changes: 19 additions & 6 deletions datafusion/datasource-csv/src/file_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use arrow::error::ArrowError;
use datafusion_common::config::{ConfigField, ConfigFileType, CsvOptions};
use datafusion_common::file_options::csv_writer::CsvWriterOptions;
use datafusion_common::{
DEFAULT_CSV_EXTENSION, DataFusionError, GetExt, Result, Statistics, exec_err,
not_impl_err,
DEFAULT_CSV_EXTENSION, DataFusionError, GetExt, Result, Statistics,
exec_datafusion_err, exec_err, not_impl_err,
};
use datafusion_common_runtime::SpawnedTask;
use datafusion_datasource::TableSchema;
Expand Down Expand Up @@ -540,6 +540,21 @@ impl CsvFormat {
let mut record_number = -1;
let initial_records_to_read = records_to_read;

// Compile once rather than per chunk, and report a malformed pattern
// instead of panicking on it.
let null_regex = self
.options
.null_regex
.as_ref()
.map(|null_regex| {
Regex::new(null_regex).map_err(|e| {
exec_datafusion_err!(
"Unable to parse CSV null regex '{null_regex}': {e}"
)
})
})
.transpose()?;

pin_mut!(stream);

while let Some(chunk) = stream.next().await.transpose()? {
Expand All @@ -557,10 +572,8 @@ impl CsvFormat {
.with_quote(self.options.quote)
.with_truncated_rows(self.options.truncated_rows.unwrap_or(false));

if let Some(null_regex) = &self.options.null_regex {
let regex = Regex::new(null_regex.as_str())
.expect("Unable to parse CSV null regex.");
format = format.with_null_regex(regex);
if let Some(regex) = &null_regex {
format = format.with_null_regex(regex.clone());
}

if let Some(escape) = self.options.escape {
Expand Down
8 changes: 8 additions & 0 deletions datafusion/sqllogictest/test_files/csv_files.slt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ STORED AS CSV
LOCATION '../core/tests/data/duplicate_header.csv'
OPTIONS ('format.has_header' 'true');

# A malformed null_regex is reported as an error rather than panicking the
# query. Schema inference compiles the pattern, so CREATE is where it surfaces.
statement error Unable to parse CSV null regex
CREATE EXTERNAL TABLE csv_with_invalid_null_regex
STORED AS CSV
LOCATION '../core/tests/data/quote.csv'
OPTIONS ('format.has_header' 'true', 'format.null_regex' '(');

# create_external_table_with_quote_escape
statement ok
CREATE EXTERNAL TABLE csv_with_quote (
Expand Down