Skip to content

Commit

Permalink
trino: Start transpiling arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
emk committed Oct 19, 2023
1 parent 65b8c64 commit 79e418b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
23 changes: 21 additions & 2 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,16 @@ impl Emit for ArrayExpression {
self.definition.emit(t, f)?;
self.delim2.with_token_str(")").emit(t, f)?;
}
Target::Trino => {
if let Some(array_token) = &self.array_token {
array_token.emit(t, f)?;
} else {
write!(f, "ARRAY")?;
}
self.delim1.with_token_str("[").emit(t, f)?;
self.definition.emit(t, f)?;
self.delim2.with_token_str("]").emit(t, f)?;
}
_ => self.emit_default(t, f)?,
}
Ok(())
Expand Down Expand Up @@ -1567,8 +1577,17 @@ impl Emit for DataType {
DataType::Timestamp(token) => {
token.with_token_str("TIMESTAMP WITH TIME ZONE").emit(t, f)
}
// TODO: Can we declare the element type?
DataType::Array { array_token, .. } => array_token.ensure_ws().emit(t, f),
DataType::Array {
array_token,
lt,
data_type,
gt,
} => {
array_token.emit(t, f)?;
lt.with_token_str("(").emit(t, f)?;
data_type.emit(t, f)?;
gt.with_token_str(")").emit(t, f)
}
// TODO: I think we can translate the column types?
DataType::Struct { struct_token, .. } => {
struct_token.with_token_str("ROW").emit(t, f)
Expand Down
31 changes: 29 additions & 2 deletions src/drivers/trino/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,34 @@ use crate::{
ast::{self, Emit, Target},
drivers::sqlite3::SQLite3String,
errors::{format_err, Context, Error, Result},
transforms::{self, Transform},
transforms::{self, Transform, Udf},
};

use super::{sqlite3::SQLite3Ident, Column, Driver, DriverImpl, Locator};

/// Our locator prefix.
pub const TRINO_LOCATOR_PREFIX: &str = "trino:";

// A `phf_map!` of BigQuery function names to native function names. Use
// this for simple renaming.
static FUNCTION_NAMES: phf::Map<&'static str, &'static str> = phf::phf_map! {
"ARRAY_LENGTH" => "CARDINALITY",
};

/// A `phf_map!` of BigQuery function names to UDFs.
///
/// TODO: I'm not even sure there's a way to define SQL UDFs in Trino.
static UDFS: phf::Map<&'static str, &'static Udf> = phf::phf_map! {};

/// Format a UDF.
///
/// TODO: I'm not even sure there's a way to define SQL UDFs in Trino.
fn format_udf(udf: &Udf) -> String {
format!(
"CREATE OR REPLACE TEMP FUNCTION {} AS $$\n{}\n$$\n",
udf.decl, udf.sql
)
}
/// A locator for a Trino database. May or may not also work for Presto.
#[derive(Debug)]
pub struct TrinoLocator {
Expand Down Expand Up @@ -142,7 +162,14 @@ impl Driver for TrinoDriver {
}

fn transforms(&self) -> Vec<Box<dyn Transform>> {
vec![Box::new(transforms::OrReplaceToDropIfExists)]
vec![
Box::new(transforms::OrReplaceToDropIfExists),
Box::new(transforms::RenameFunctions::new(
&FUNCTION_NAMES,
&UDFS,
&format_udf,
)),
]
}

#[tracing::instrument(skip(self))]
Expand Down

0 comments on commit 79e418b

Please sign in to comment.