nooberfsh / prusto

A presto/trino client library written in rust.
MIT License
37 stars 23 forks source link

How to define a generic return structure? #44

Closed groobyming closed 4 months ago

groobyming commented 4 months ago

The get_all method requires that the generic type T must implement the Presto trait. This can be limiting; for example, when Trino queries table data in Hive, there might be tens of thousands of tables. Do I need to define tens of thousands of structs similar to the following first?

use prusto::{ClientBuilder, Presto};

#[derive(Presto, Debug)]
struct Foo {
    a: i64,
    b: f64,
    c: String,
}
groobyming commented 4 months ago

@nooberfsh Hi, nooberfsh It seems that we can use Row instead, but there are two issues:

  1. It will fail when using "select *".

Code:

#[tokio::test]
    async fn main() {
        let cli = ClientBuilder::new("admin", "localhost")
            .port(8080)
            .catalog("iceberg")
            .build()
            .unwrap();
        let sql = "SELECT * FROM iceberg.test.test_table LIMIT 1";
        let result = cli.get_all::<Row>(sql.to_string()).await.unwrap().into_vec();
        println!("result size:{}", result.len());

        println!("result is {:#?}", result)
    }

Error:

image
  1. It will lose the column names.
nooberfsh commented 4 months ago

prusto does not support some trino types yet, e.g. timestamp with timezone, when prusto see these unsupported types it just return EmptyData. The right way I think it should return some error instead. You can cast the unsupported column into supported column as a workaround.

It will lose the column names

You can use split to retain column names and types.

groobyming commented 4 months ago

prusto does not support some trino types yet, e.g. timestamp with timezone, when prusto see these unsupported types it just return EmptyData. The right way I think it should return some error instead. You can cast the unsupported column into supported column as a workaround.

It will lose the column names

You can use split to retain column names and types.

Thanks 😀