Repository navigation
Using gen_random_uuid in python relational API
#263
|
I'm not sure if there's an issue here somewhere or me just misunderstanding how to do this properly, I'm generating a python relation where one of the columns is created via import duckdb
db = duckdb.connect()
my_relation = db.sql("select gen_random_uuid() as id, 'hello' as some_col")Just printing this relation out gives me a consistent UUID each time: However, if I interact with the relation in any other way (select from it, or call I assume this has to do with the python relational API's lazy evaluation? If I create a table (like with
my_relation_df = my_relation.df()
my_relation = db.sql("select * from my_relation_df")
db.sql("select * from my_relation").to_table("temp_relation_table")
my_relation = db.table("temp_relation_table")But I'm curious if I'm missing something obvious/is there a better or conventional way of dealing with this? This problem is coming up because I have multiple queries later on working with |
Replies: 1 comment
|
You're not missing anything. Relations are lazy, so every fetch or query over one runs it again and gen_random_uuid() draws new values each time. Since 2.0.0 the conventional way is rel.materialize(). It runs the relation once and keeps the rows, and every later fetch, derived relation and SQL query that names it sees the same ids. I checked this on 2.0.0 with a self join on id, which gives one row. to_table() is the right choice when the rows should live in the database, and building on relations is fine practice as long as anything non deterministic is materialized first. Note that in 2.0 printing a plain relation also runs it, so repr no longer shows a stable value. |
You're not missing anything. Relations are lazy, so every fetch or query over one runs it again and gen_random_uuid() draws new values each time. Since 2.0.0 the conventional way is rel.materialize(). It runs the relation once and keeps the rows, and every later fetch, derived relation and SQL query that names it sees the same ids. I checked this on 2.0.0 with a self join on id, which gives one row. to_table() is the right choice when the rows should live in the database, and building on relations is fine practice as long as anything non deterministic is materialized first. Note that in 2.0 printing a plain relation also runs it, so repr no longer shows a stable value.