MCPcopy Create free account
hub / github.com/apache/datafusion / expr_to_sql_ok

Function expr_to_sql_ok

datafusion/sql/src/unparser/expr.rs:2006–2511  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2004
2005 #[test]
2006 fn expr_to_sql_ok() -> Result<()> {
2007 let dummy_schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);
2008 #[expect(deprecated)]
2009 let dummy_logical_plan = table_scan(Some("t"), &dummy_schema, None)?
2010 .project(vec![Expr::Wildcard {
2011 qualifier: None,
2012 options: Box::new(WildcardOptions::default()),
2013 }])?
2014 .filter(col("a").eq(lit(1)))?
2015 .build()?;
2016
2017 let tests: Vec<(Expr, &str)> = vec![
2018 ((col("a") + col("b")).gt(lit(4)), r#"((a + b) > 4)"#),
2019 (
2020 Expr::Column(Column {
2021 relation: Some(TableReference::partial("a", "b")),
2022 name: "c".to_string(),
2023 spans: Spans::new(),
2024 })
2025 .gt(lit(4)),
2026 r#"(b.c > 4)"#,
2027 ),
2028 (
2029 case(col("a"))
2030 .when(lit(1), lit(true))
2031 .when(lit(0), lit(false))
2032 .otherwise(lit(ScalarValue::Null))?,
2033 r#"CASE a WHEN 1 THEN true WHEN 0 THEN false ELSE NULL END"#,
2034 ),
2035 (
2036 when(col("a").is_null(), lit(true)).otherwise(lit(false))?,
2037 r#"CASE WHEN a IS NULL THEN true ELSE false END"#,
2038 ),
2039 (
2040 when(col("a").is_not_null(), lit(true)).otherwise(lit(false))?,
2041 r#"CASE WHEN a IS NOT NULL THEN true ELSE false END"#,
2042 ),
2043 (
2044 Expr::Cast(Cast::new(Box::new(col("a")), DataType::Date64)),
2045 r#"CAST(a AS DATETIME)"#,
2046 ),
2047 (
2048 Expr::Cast(Cast::new(
2049 Box::new(col("a")),
2050 DataType::Timestamp(TimeUnit::Nanosecond, Some("+08:00".into())),
2051 )),
2052 r#"CAST(a AS TIMESTAMP WITH TIME ZONE)"#,
2053 ),
2054 (
2055 Expr::Cast(Cast::new(
2056 Box::new(col("a")),
2057 DataType::Timestamp(TimeUnit::Millisecond, None),
2058 )),
2059 r#"CAST(a AS TIMESTAMP)"#,
2060 ),
2061 (
2062 Expr::Cast(Cast::new(Box::new(col("a")), DataType::UInt32)),
2063 r#"CAST(a AS INTEGER UNSIGNED)"#,

Callers

nothing calls this directly

Calls 9

newFunction · 0.85
table_scanFunction · 0.85
expr_to_sqlFunction · 0.85
colFunction · 0.50
litFunction · 0.50
buildMethod · 0.45
filterMethod · 0.45
projectMethod · 0.45
eqMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…