Skip to content

Commit ce12d6c

Browse files
authored
Add rand() alias for random() (#22147)
## Which issue does this PR close? - Closes #. ## Rationale for this change `rand()` is a common alias for `random()` in SQL engines. Supporting it improves compatibility and lets users write `rand()` as an equivalent zero-argument volatile random function. ## What changes are included in this PR? - Adds `rand` as an alias for the existing `random()` scalar function. - Adds a sqllogictest case verifying that `rand()` resolves successfully and returns a `Float64` value in the expected `[0, 1)` range. ## Are these changes tested? Yes. - `cargo fmt --all` - `cargo test --package datafusion-functions random --lib` - `cargo test --features backtrace,parquet_encryption --profile ci --package datafusion-sqllogictest --test sqllogictests -- functions.slt` ## Are there any user-facing changes? Yes. Users can now call `rand()` as an alias for `random()`.
1 parent cf57f3e commit ce12d6c

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

datafusion/functions/src/math/random.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ The random seed is unique to each row."#,
4444
#[derive(Debug, PartialEq, Eq, Hash)]
4545
pub struct RandomFunc {
4646
signature: Signature,
47+
aliases: Vec<String>,
4748
}
4849

4950
impl Default for RandomFunc {
@@ -56,6 +57,7 @@ impl RandomFunc {
5657
pub fn new() -> Self {
5758
Self {
5859
signature: Signature::nullary(Volatility::Volatile),
60+
aliases: vec![String::from("rand")],
5961
}
6062
}
6163
}
@@ -73,6 +75,10 @@ impl ScalarUDFImpl for RandomFunc {
7375
Ok(Float64)
7476
}
7577

78+
fn aliases(&self) -> &[String] {
79+
&self.aliases
80+
}
81+
7682
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
7783
assert_or_internal_err!(
7884
args.args.is_empty(),

datafusion/sqllogictest/test_files/functions.slt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,12 @@ SELECT r FROM (SELECT r1 == r2 r, r1, r2 FROM (SELECT random()+1 r1, random()+1
841841
----
842842
false
843843

844+
# Verify that rand() is accepted as an alias for random()
845+
query TB
846+
SELECT arrow_typeof(rand()), rand() >= 0 AND rand() < 1
847+
----
848+
Float64 true
849+
844850
#######
845851
# verify that random() returns a different value for each row
846852
#######

docs/source/user-guide/sql/scalar_functions.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ dev/update_function_docs.sh file for updating surrounding text.
5959
- [pow](#pow)
6060
- [power](#power)
6161
- [radians](#radians)
62+
- [rand](#rand)
6263
- [random](#random)
6364
- [round](#round)
6465
- [signum](#signum)
@@ -739,6 +740,10 @@ radians(numeric_expression)
739740
+----------------+
740741
```
741742

743+
### `rand`
744+
745+
_Alias of [random](#random)._
746+
742747
### `random`
743748

744749
Returns a random float value in the range [0, 1).
@@ -759,6 +764,10 @@ random()
759764
+------------------+
760765
```
761766

767+
#### Aliases
768+
769+
- rand
770+
762771
### `round`
763772

764773
Rounds a number to the nearest integer.

0 commit comments

Comments
 (0)