Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field names containing periods such as f.c cannot work #3682

Closed
hengfeiyang opened this issue Oct 3, 2022 · 6 comments · Fixed by #3700
Closed

Field names containing periods such as f.c cannot work #3682

hengfeiyang opened this issue Oct 3, 2022 · 6 comments · Fixed by #3700
Labels
bug Something isn't working

Comments

@hengfeiyang
Copy link
Contributor

hengfeiyang commented Oct 3, 2022

Describe the bug
Field names containing periods such as f.c cannot work.

To Reproduce
Steps to reproduce the behavior:

use std::sync::Arc;

use datafusion::arrow::array::Int32Array;
use datafusion::arrow::datatypes::{DataType, Field, Schema};
use datafusion::arrow::record_batch::RecordBatch;
use datafusion::datasource::MemTable;
use datafusion::error::Result;
use datafusion::from_slice::FromSlice;
use datafusion::prelude::{col, lit, SessionContext, Expr};
use datafusion_common::Column;

/// This example demonstrates how to use the DataFrame API against in-memory data.
#[tokio::main]
async fn main() -> Result<()> {
    // define a schema.
    let schema = Arc::new(Schema::new(vec![Field::new("f.c", DataType::Int32, false)]));

    // define data.
    let batch = RecordBatch::try_new(
        schema.clone(),
        vec![Arc::new(Int32Array::from_slice(&[1, 10, 10, 100]))],
    )?;

    // declare a new context. In spark API, this corresponds to a new spark SQLsession
    let ctx = SessionContext::new();

    // declare a table in memory. In spark API, this corresponds to createDataFrame(...).
    let provider = MemTable::try_new(schema.clone(), vec![vec![batch]])?;
    ctx.register_table("t", Arc::new(provider))?;
    let df = ctx.table("t")?;
    let df = df.with_column("f2", lit("hello"))?;

    // construct an expression corresponding to "SELECT * FROM t WHERE f.c = 10" in SQL
    let filter = Expr::Column(Column {
        relation: None,
        name: "f.c".into()
      }).eq(lit(10));

    let df = df.filter(filter)?;

    // print the results
    df.show().await?;

    Ok(())
}

Run the example will got error:

Error: SchemaError(FieldNotFound { qualifier: Some("f"), name: "c", valid_fields: Some(["t.f.c"]) })

Expected behavior
expect it can work.

Additional context
DataFusion 12.0

@hengfeiyang hengfeiyang added the bug Something isn't working label Oct 3, 2022
@alamb
Copy link
Contributor

alamb commented Oct 3, 2022

Does this work if you run a query (note the double quotes " around f.c)?

SELECT * FROM t WHERE "f.c" = 10

@hengfeiyang
Copy link
Contributor Author

@alamb In SQL layer it can work, but in DataFrame it can't work.

@alamb
Copy link
Contributor

alamb commented Oct 3, 2022

@hengfeiyang I think you may have to create an explicit column reference -- for example instead of col("f.c") perhaps you could do something like

Expr::Column(Column {
  relation: None,
  name: "f.c".into()
})

@hengfeiyang
Copy link
Contributor Author

hengfeiyang commented Oct 3, 2022

@alamb Thanks, the filter can work now. But my real work is with_column, I updated my example code above, added a line:

    let df = df.with_column("f2", lit("hello"))?;

You can have a try, this line will cause the error too, Do you have any idea? thank you.

@alamb
Copy link
Contributor

alamb commented Oct 4, 2022

You can have a try, this line will cause the error too, Do you have any idea? thank you.

I think it is a bug in with_column (basically it is using col as well). I will create a PR to fix it. Thank you for the report @hengfeiyang

@alamb
Copy link
Contributor

alamb commented Oct 4, 2022

#3700

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants