Skip to content
Open
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
6 changes: 6 additions & 0 deletions datafusion/functions/src/math/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The random seed is unique to each row."#,
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct RandomFunc {
signature: Signature,
aliases: Vec<String>,
}

impl Default for RandomFunc {
Expand All @@ -56,6 +57,7 @@ impl RandomFunc {
pub fn new() -> Self {
Self {
signature: Signature::nullary(Volatility::Volatile),
aliases: vec![String::from("rand")],
}
}
}
Expand All @@ -73,6 +75,10 @@ impl ScalarUDFImpl for RandomFunc {
Ok(Float64)
}

fn aliases(&self) -> &[String] {
&self.aliases
}

fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
assert_or_internal_err!(
args.args.is_empty(),
Expand Down
6 changes: 6 additions & 0 deletions datafusion/sqllogictest/test_files/functions.slt
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,12 @@ SELECT r FROM (SELECT r1 == r2 r, r1, r2 FROM (SELECT random()+1 r1, random()+1
----
false

# Verify that rand() is accepted as an alias for random()
query TB
SELECT arrow_typeof(rand()), rand() >= 0 AND rand() < 1
----
Float64 true

#######
# verify that random() returns a different value for each row
#######
Expand Down
9 changes: 9 additions & 0 deletions docs/source/user-guide/sql/scalar_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ dev/update_function_docs.sh file for updating surrounding text.
- [pow](#pow)
- [power](#power)
- [radians](#radians)
- [rand](#rand)
- [random](#random)
- [round](#round)
- [signum](#signum)
Expand Down Expand Up @@ -739,6 +740,10 @@ radians(numeric_expression)
+----------------+
```

### `rand`

_Alias of [random](#random)._

### `random`

Returns a random float value in the range [0, 1).
Expand All @@ -759,6 +764,10 @@ random()
+------------------+
```

#### Aliases

- rand

### `round`

Rounds a number to the nearest integer.
Expand Down
Loading