Registering normalizes table names
Describe the bug
When registering a table with a String name in a SessionContext the table name is passed through parse_str_normalized converting it into a lower case string.
This leads to confusion when the name is used "quoted" in a SQL query.
To Reproduce
use datafusion::execution::{context::SessionContext, options::CsvReadOptions};
#[tokio::main]
async fn main() -> datafusion::error::Result<()> {
let ctx = SessionContext::new();
// Register with an Upper case name
ctx.register_csv("Example", "cars.csv", CsvReadOptions::new())
.await
.unwrap();
// unquoted query works
let df = ctx.sql("SELECT MAX(speed) FROM Example").await.unwrap();
df.show().await.unwrap();
// quoted query FAILS
let df = ctx.sql("SELECT MAX(speed) FROM \"Example\"").await.unwrap();
df.show().await.unwrap();
Ok(())
}
CSV file: https://github.com/apache/datafusion/blob/main/datafusion-examples/data/csv/cars.csv
Expected behavior
Unsure what the wider consequences and decisions are.
My expectation was that registering would preserve the case.
Additional context
Registering the table with a quoted name prevents this behavior.
use datafusion::execution::{context::SessionContext, options::CsvReadOptions};
#[tokio::main]
async fn main() -> datafusion::error::Result<()> {
let ctx = SessionContext::new();
// Register with an Upper case name
ctx.register_csv("\"Example\"", "cars.csv", CsvReadOptions::new())
.await
.unwrap();
let df = ctx.sql("SELECT MAX(speed) FROM \"Example\"").await.unwrap();
df.show().await.unwrap();
Ok(())
}
Willingness to contribute
I would like to contribute a fix for this bug, but request guidance from the DataFusion community
untake
Hi, @H4kor This is existing, documented behavior: strings passed as table references are parsed as SQL identifiers, so unquoted names such as Example are normalized to lowercase. See the TableReference documentation
To preserve the name’s case, I’d recommend using TableReference::bare:
use datafusion::common::TableReference;
ctx.register_csv(
TableReference::bare("Example"),
"cars.csv",
CsvReadOptions::new(),
)
.await?;
let df = ctx.sql(r#"SELECT MAX(speed) FROM "Example""#).await?;
df.show().await?;
The documentation recommends bare for programmatic use. It would also help to mention this behavior and add an example in the register_csv documentation, where it’s easier to discover.
take