use sqlparser::ast::TableFactor;
use crate::Result;
use crate::dialect::SourceDialect;
pub fn rewrite_unnest(tf: &mut TableFactor, dialect: SourceDialect) -> Result<()> {
if let TableFactor::UNNEST {
with_offset,
with_ordinality,
..
} = tf
{
match dialect {
SourceDialect::Trino => {
}
SourceDialect::Redshift | SourceDialect::Hive => {
if *with_offset && !*with_ordinality {
*with_ordinality = true;
*with_offset = false;
}
}
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use crate::dialect::SourceDialect;
#[test]
fn trino_unnest_passthrough() {
let sql = "SELECT t.x FROM my_table CROSS JOIN UNNEST(arr) AS t(x)";
let result = crate::transpile(sql, SourceDialect::Trino).unwrap();
assert!(result.contains("UNNEST"), "Got: {result}");
}
#[test]
fn trino_unnest_with_ordinality() {
let sql = "SELECT t.x, t.n FROM my_table CROSS JOIN UNNEST(arr) WITH ORDINALITY AS t(x, n)";
let result = crate::transpile(sql, SourceDialect::Trino).unwrap();
assert!(result.contains("WITH ORDINALITY"), "Got: {result}");
}
}