Skip to content

Support specifying independent names for the serialization and deserialization of a field #360

@destinyFvcker

Description

@destinyFvcker

Use case

Note: Only after upgrading to 0.14.0+ will it be affected.(with RowBinaryWithNamesAndTypes open)

If we have a struct like:

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Row)]
pub struct FooBar {
    #[serde(rename(deserialize = "foobar"))]
    pub foo: i32,
    pub bar: i32,
}

ddl like:

CREATE TABLE IF NOT EXISTS dummy_table (
    foo Int32,
    bar Int32
) ENGINE = MergeTree
ORDER BY foo;

then i can get it from clickhouse by using alias clause

SELECT foo AS foobar, bar FROM dummy_table order by foo

Now, clickhouse-rs(0.14.0+) will panic directly in this situation(with RowBinaryWithNamesAndTypes format enabled).

thread 'tests::fetch_with_rename' (7401293) panicked at src/lib.rs:77:14:
called `Result::unwrap()` on an `Err` value: SchemaMismatch("While processing struct FooBar: database schema has a column foobar: Int32 that was not found in the struct definition.\n#### All struct fields:\n- foo\n- bar\n#### All schema columns:\n- foobar: Int32\n- bar: Int32")

Using the RowBinary format, this problem is not an issue.

Describe the solution you'd like

Support #[serde(rename(serialize = "ser_name"))], #[serde(rename(deserialize = "de_name"))] and #[serde(rename(serialize = "ser_name", deserialize = "de_name"))]

Describe the alternatives you've considered

nothing here

Additional context

This is useful when you want to use the same field names in the database as in the DDL, but need to use different field names in the query you write.

And in some cases, using the alias clause is necessary:

SELECT                                                                                                                                                                                                                          
    date,                                                                                                                                                                                                                       
    broker_id,                                                                                                                                                                                                                  
    broker_key,                                                                                                                                                                                                                 
    account_id,                                                                                                                                                                                                                 
    argMax(timestamp, timestamp) AS timestamp,                                                                                                                                                                                  
    argMax(vwap, timestamp) AS vwap,                                                                                                                                                                                            
    argMax(vwap_profit, timestamp) AS vwap_profit,                                                                                                                                                                              
    argMax(vwap_bp, timestamp) AS vwap_bp,                                                                                                                                                                                      
    argMax(progress_rate, timestamp) AS progress_rate                                                                                                                                                                           
FROM ufas_account_serial_hist                                                                                                                                                                                                   
FINAL                                                                                                                                                                                                                           
WHERE date = today()                                                                                                                                                                                                            
GROUP BY                                                                                                                                                                                                                        
    date,                                                                                                                                                                                                                       
    broker_id,                                                                                                                                                                                                                  
    broker_key,                                                                                                                                                                                                                 
    account_id

will lead to:

Code: 184. DB::Exception: Received from localhost:9147. DB::Exception: Aggregate function argMax(timestamp, timestamp) is found inside another aggregate function in query: While processing argMax(timestamp, timestamp) AS timestamp. (ILLEGAL_AGGREGATION) 

then you can use argMax(timestamp, timestamp) AS ts to fix that

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions