Skip to content

Commit 9f9a57d

Browse files
authored
Use UUID to create unique table names in python binding (apache#1111)
1 parent 2454e46 commit 9f9a57d

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

python/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tokio = { version = "1.0", features = ["macros", "rt", "rt-multi-thread", "sync"
3232
rand = "0.7"
3333
pyo3 = { version = "0.14.1", features = ["extension-module", "abi3", "abi3-py36"] }
3434
datafusion = { path = "../datafusion", version = "5.1.0" }
35+
uuid = { version = "0.8", features = ["v4"] }
3536

3637
[lib]
3738
name = "datafusion"

python/src/context.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
use std::path::PathBuf;
1919
use std::{collections::HashSet, sync::Arc};
2020

21-
use rand::distributions::Alphanumeric;
22-
use rand::Rng;
21+
use uuid::Uuid;
2322

2423
use tokio::runtime::Runtime;
2524

@@ -91,10 +90,10 @@ impl ExecutionContext {
9190

9291
// generate a random (unique) name for this table
9392
// table name cannot start with numeric digit
94-
let name = std::iter::once('c')
95-
.chain(rand::thread_rng().sample_iter(&Alphanumeric))
96-
.take(10)
97-
.collect::<String>();
93+
let name = "c".to_owned()
94+
+ &Uuid::new_v4()
95+
.to_simple()
96+
.encode_lower(&mut Uuid::encode_buffer());
9897

9998
errors::wrap(self.ctx.register_table(&*name, Arc::new(table)))?;
10099
Ok(dataframe::DataFrame::new(

python/tests/test_df_sql.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,23 @@ def test_register_record_batches(ctx):
4141

4242
assert result[0].column(0) == pa.array([5, 7, 9])
4343
assert result[0].column(1) == pa.array([-3, -3, -3])
44+
45+
46+
def test_create_dataframe_registers_unique_table_name(ctx):
47+
# create a RecordBatch and register it as memtable
48+
batch = pa.RecordBatch.from_arrays(
49+
[pa.array([1, 2, 3]), pa.array([4, 5, 6])],
50+
names=["a", "b"],
51+
)
52+
53+
df = ctx.create_dataframe([[batch]])
54+
tables = list(ctx.tables())
55+
56+
assert df
57+
assert len(tables) == 1
58+
assert len(tables[0]) == 33
59+
assert tables[0].startswith("c")
60+
# ensure that the rest of the table name contains
61+
# only hexadecimal numbers
62+
for c in tables[0][1:]:
63+
assert c in "0123456789abcdef"

0 commit comments

Comments
 (0)