Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added updated_at Datetime #12

Merged
merged 1 commit into from
Nov 11, 2024
Merged

Conversation

makorne
Copy link
Contributor

@makorne makorne commented Nov 11, 2024

Error: ColumnDecode { index: ""updated_at"", source: "mismatched types; Rust type core::option::Option<std::time::SystemTime> (as SQL type Timestamp) is not compatible with SQL type DateTime" }

Error: ColumnDecode { index: "\"updated_at\"", source: "mismatched types; Rust type `core::option::Option<std::time::SystemTime>` (as SQL type `Timestamp`) is not compatible with SQL type `DateTime`" }
@MikhailNazarov
Copy link
Owner

Thank you for your contribution. I created the issue #13 and will investigate the problem.

@MikhailNazarov MikhailNazarov merged commit 93ef5f7 into MikhailNazarov:main Nov 11, 2024
1 check passed
@MikhailNazarov
Copy link
Owner

You should use CurrentUtcDateTime() instead of CurrentUtcDate(), because DateTime is not compartible with Date.
And with rust type std::SystemTime you should use database type Timestamp.

That's how it works now. We need to think about it, maybe it's worth do it other way.

@makorne
Copy link
Contributor Author

makorne commented Nov 11, 2024

You should use CurrentUtcDateTime()
The error too

Error: ColumnDecode { index: "\"updated_at\"", source: "mismatched types; Rust type core::option::Optionstd::time::SystemTime(as SQL typeTimestamp) is not compatible with SQL type DateTime" }

How to select by "updated_at > now - 1 week" ?

@MikhailNazarov
Copy link
Owner

updated_at must be also different type:

updated_at Timestamp

you could use:

CREATE TABLE test5 (id Uint64 NOT NULL, name Utf8, age UInt8 NOT NULL, description Utf8, updated_at Timestamp, PRIMARY KEY (id))

then

INSERT INTO test5 (id, name, age, description, updated_at) VALUES ( 2, "user2", 30, "", CurrentUtcDateTime() - Cast('P10D' as Interval))
select id,updated_at from test5 
where  (CurrentUtcDateTime() - updated_at) > Cast('P7D' as Interval)

@makorne
Copy link
Contributor Author

makorne commented Nov 12, 2024

Thank you for your help!
I'm thinking how to resolve problem with only 16 columns of sqlx FromRow.
My table has 40 columns with ydb official...
May be you have any ideas?

[FromRow](https://docs.rs/sqlx/latest/sqlx/trait.FromRow.html) is implemented for tuples of up to 16 elements1. Using a tuple of N elements will extract the first N columns from each row using [Decode](https://docs.rs/sqlx/latest/sqlx/trait.Decode.html). Any extra columns are ignored.

@MikhailNazarov
Copy link
Owner

Why you are trying to use tuple with 40 elements? You should use struct for this purpose:

 let pool = ydb_sqlx::database::Ydb::connect_opts(
        "grpc://localhost:2136?database=/local",
        |opts|opts.log_statements(tracing_log::log::LevelFilter::Info)
    ).await;

    #[derive(FromRow)]
    struct Test{
        col1: i32,
        col2: i32,
        col3: i32,
        col4: i32,
        col5: i32,
        col6: i32,
        col7: i32,
        col8: i32,
        col9: i32,
        col10: i32,
        col11: i32,
        col12: i32,
        col13: i32,
        col14: i32,
        col15: i32,
        col16: i32,
        col17: i32,
        col18: i32,
        col19: i32,
        col20: i32,
        col21: i32,
        col22: i32,
        col23: i32,
        col24: i32,
        col25: i32,
        col26: i32,
        col27: i32,
        col28: i32,
        col29: i32,
        col30: i32,
        col31: i32,
        col32: i32,
        col33: i32,
        col34: i32,
        col35: i32,
        col36: i32,
        col37: i32,
        col38: i32, 
        col39: i32,
        col40: i32,
    }

    assert!(pool.is_ok());
    let pool = pool.unwrap();

    sqlx::query(r#"CREATE TABLE test (
        col1 Int32,
        col2 Int32,
        col3 Int32,
        col4 Int32,
        col5 Int32,
        col6 Int32,
        col7 Int32,
        col8 Int32,
        col9 Int32,
        col10 Int32,
        col11 Int32,
        col12 Int32,
        col13 Int32,
        col14 Int32,
        col15 Int32,
        col16 Int32,
        col17 Int32,
        col18 Int32,
        col19 Int32,
        col20 Int32,
        col21 Int32,
        col22 Int32,
        col23 Int32,
        col24 Int32,
        col25 Int32,
        col26 Int32,
        col27 Int32,
        col28 Int32,
        col29 Int32,
        col30 Int32,
        col31 Int32,
        col32 Int32,
        col33 Int32,
        col34 Int32,
        col35 Int32,
        col36 Int32,
        col37 Int32,
        col38 Int32, 
        col39 Int32,
        col40 Int32,

        PRIMARY KEY (col1)
    )"#)
        .execute(pool.schema())
        .await.unwrap();

    

    sqlx::query(r#"INSERT INTO test (
            col1,
            col2,
            col3,
            col4,
            col5,
            col6,
            col7,
            col8,
            col9,
            col10,
            col11,
            col12,
            col13,
            col14,
            col15,
            col16,
            col17,
            col18,
            col19,
            col20,
            col21,
            col22,
            col23,
            col24,
            col25,
            col26,
            col27,
            col28,
            col29,
            col30,
            col31,
            col32,
            col33,
            col34,
            col35,
            col36,
            col37,
            col38, 
            col39,
            col40
    
        ) VALUES (
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12,
            13,
            14,
            15,
            16,
            17,
            18,
            19,
            20,
            21,
            22,
            23,
            24,
            25,
            26,
            27,
            28,
            29,
            30,
            31,
            32,
            33,
            34,
            35,
            36,
            37,
            38, 
            39,
            40
        )"#)
        .execute(&pool)
        .await.unwrap();

    let test: Test = sqlx::query_as("SELECT * FROM test")
        .fetch_one(&pool)
        .await
        .unwrap();
    
    assert_eq!(1,test.col1);
Снимок экрана 2024-11-13 в 15 26 34

@makorne
Copy link
Contributor Author

makorne commented Nov 15, 2024

Thank you very much for your help!
I misunderstood the documentation that will extract the first N columns from each row .
In reality, I did not intend to use tuples at all :)
What editor are you using on the screenshot?

@MikhailNazarov
Copy link
Owner

I'am using VSCode with rouge theme

@makorne
Copy link
Contributor Author

makorne commented Nov 16, 2024

Maybe you know a hint on how to search for an array of values ​​by an array in ydb?
As far as I understood the documentation :), it only has arrays via json.
But such a search will be slow.
Therefore, now I simulate arrays via bytes

let pool = ydb_sqlx::database::Ydb::connect_opts(
        "grpc://localhost:2136?database=/local",
        |opts|opts.log_statements(tracing_log::log::LevelFilter::Info)
    ).await;

    #[derive(FromRow)]
    struct Test{
        id i32,
        array: Vec<u8>
    }

    assert!(pool.is_ok());
    let pool = pool.unwrap();

    sqlx::query(r#"CREATE TABLE test (
        id Int32,
	array Bytes,

        PRIMARY KEY (id)
    )"#)
        .execute(pool.schema())
        .await.unwrap();

    let array = Value::Bytes(vec![1_u8, 2_u8, 3_u8].into());

    sqlx::query(r#"INSERT INTO test (
            id,
            array
        ) VALUES (
            1,
            $arg_1
        )"#).bind(array)
        .execute(&pool)
        .await.unwrap();

    let test: Test = sqlx::query_as("SELECT * FROM test")
        .fetch_one(&pool)
        .await
        .unwrap();
    
    assert_eq!(1,test.id);

But how do you search an array by an array?
Something like pseudocode:

 let search_values = vec![1_u8, 2_u8];

 let test: Test = sqlx::query_as("SELECT * FROM test WHERE $arg1 IN array")
 	.bind(search_values)
     .fetch_one(&pool)
     .await
     .unwrap();

@MikhailNazarov
Copy link
Owner

Sorry, I have not worked with such data in YDB

@makorne
Copy link
Contributor Author

makorne commented Nov 20, 2024

Maybe you know a hint on how to work with geospatial data, like https://postgis.net/ ?

@MikhailNazarov
Copy link
Owner

Unfortunately, I have not worked with such data. It is difficult to advise something without immersion in the subject

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants