Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions datafusion/functions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ harness = false
name = "encoding"
required-features = ["encoding_expressions"]

[[bench]]
harness = false
name = "uuid"
required-features = ["string_expressions"]

[[bench]]
harness = false
name = "regx"
Expand Down
31 changes: 31 additions & 0 deletions datafusion/functions/benches/uuid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

extern crate criterion;

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use datafusion_functions::string;

fn criterion_benchmark(c: &mut Criterion) {
let uuid = string::uuid();
c.bench_function("uuid", |b| {
b.iter(|| black_box(uuid.invoke_batch(&[], 1024)))
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
25 changes: 21 additions & 4 deletions datafusion/functions/src/string/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
use std::any::Any;
use std::sync::Arc;

use arrow::array::GenericStringArray;
use arrow::array::GenericStringBuilder;
use arrow::datatypes::DataType;
use arrow::datatypes::DataType::Utf8;
use rand::Rng;
use uuid::Uuid;

use datafusion_common::{internal_err, Result};
Expand Down Expand Up @@ -87,9 +88,25 @@ impl ScalarUDFImpl for UuidFunc {
if !args.is_empty() {
return internal_err!("{} function does not accept arguments", self.name());
}
let values = std::iter::repeat_with(|| Uuid::new_v4().to_string()).take(num_rows);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This old code will allocate a new string, create uuid, then copy that string into the output.

Removing that allocation alone is likely to make a big difference

let array = GenericStringArray::<i32>::from_iter_values(values);
Ok(ColumnarValue::Array(Arc::new(array)))

// Generate random u128 values
let mut rng = rand::thread_rng();
let mut randoms = vec![0u128; num_rows];
rng.fill(&mut randoms[..]);

let mut builder =
GenericStringBuilder::<i32>::with_capacity(num_rows, num_rows * 36);

let mut buffer = [0u8; 36];
for x in &mut randoms {
// From Uuid::new_v4(): Mask out the version and variant bits
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I double checked this is the case:
https://docs.rs/uuid/latest/src/uuid/v4.rs.html#33-39

*x = *x & 0xFFFFFFFFFFFF4FFFBFFFFFFFFFFFFFFF | 0x40008000000000000000;
let uuid = Uuid::from_u128(*x);
let fmt = uuid::fmt::Hyphenated::from_uuid(uuid);
builder.append_value(fmt.encode_lower(&mut buffer));
}

Ok(ColumnarValue::Array(Arc::new(builder.finish())))
}

fn documentation(&self) -> Option<&Documentation> {
Expand Down
8 changes: 8 additions & 0 deletions datafusion/sqllogictest/test_files/functions.slt
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,14 @@ select count(distinct u) from uuid_table;
----
2

# must be valid uuidv4 format
query B
SELECT REGEXP_LIKE(uuid(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

'^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$')
AS is_valid;
----
true

statement ok
drop table uuid_table

Expand Down
Loading