Skip to content

Apache DataFusion SQL Query Engine

Added Example for `Statistical Functions` in Docs (#16927)

aditya singh rathore2a90ff606d69
diff --git a/datafusion/functions-aggregate/src/regr.rs b/datafusion/functions-aggregate/src/regr.rs
index 4600f6570f..f7e0f0a104 100644
--- a/datafusion/functions-aggregate/src/regr.rs
+++ b/datafusion/functions-aggregate/src/regr.rs
@@ -144,6 +144,29 @@ static DOCUMENTATION: LazyLock<HashMap<RegrType, Documentation>> = LazyLock::new
Given input column Y and X: regr_slope(Y, X) returns the slope (k in Y = k*X + b) using minimal RSS fitting.",
"regr_slope(expression_y, expression_x)")
+ .with_sql_example(
+ r#"```sql
+create table weekly_performance(day int, user_signups int) as values (1,60), (2,65), (3, 70), (4,75), (5,80);
+select * from weekly_performance;
++-----+--------------+
+| day | user_signups |
++-----+--------------+
+| 1 | 60 |
+| 2 | 65 |
+| 3 | 70 |
+| 4 | 75 |
+| 5 | 80 |
++-----+--------------+
+
+SELECT regr_slope(user_signups, day) AS slope FROM weekly_performance;
++--------+
+| slope |
++--------+
+| 5.0 |
++--------+
+```
+"#
+ )
.with_standard_argument("expression_y", Some("Dependent variable"))
.with_standard_argument("expression_x", Some("Independent variable"))
.build()
@@ -157,6 +180,30 @@ static DOCUMENTATION: LazyLock<HashMap<RegrType, Documentation>> = LazyLock::new
this function returns b.",
"regr_intercept(expression_y, expression_x)")
+ .with_sql_example(
+ r#"```sql
+create table weekly_performance(week int, productivity_score int) as values (1,60), (2,65), (3, 70), (4,75), (5,80);
+select * from weekly_performance;
++------+---------------------+
+| week | productivity_score |
+| ---- | ------------------- |
+| 1 | 60 |
+| 2 | 65 |
+| 3 | 70 |
+| 4 | 75 |
+| 5 | 80 |
++------+---------------------+
+
+SELECT regr_intercept(productivity_score, week) AS intercept FROM weekly_performance;
++----------+
+|intercept|
+|intercept |
++----------+
+| 55 |
++----------+
+```
+"#
+ )
.with_standard_argument("expression_y", Some("Dependent variable"))
.with_standard_argument("expression_x", Some("Independent variable"))
.build()
@@ -169,6 +216,29 @@ static DOCUMENTATION: LazyLock<HashMap<RegrType, Documentation>> = LazyLock::new
"Counts the number of non-null paired data points.",
"regr_count(expression_y, expression_x)",
)
+ .with_sql_example(
+ r#"```sql
+create table daily_metrics(day int, user_signups int) as values (1,100), (2,120), (3, NULL), (4,110), (5,NULL);
+select * from daily_metrics;
++-----+---------------+
+| day | user_signups |
+| --- | ------------- |
+| 1 | 100 |
+| 2 | 120 |
+| 3 | NULL |
+| 4 | 110 |
+| 5 | NULL |
++-----+---------------+
+
+SELECT regr_count(user_signups, day) AS valid_pairs FROM daily_metrics;
++-------------+
+| valid_pairs |
++-------------+
+| 3 |
++-------------+
+```
+"#
+ )
.with_standard_argument("expression_y", Some("Dependent variable"))
.with_standard_argument("expression_x", Some("Independent variable"))
.build(),
@@ -181,6 +251,29 @@ static DOCUMENTATION: LazyLock<HashMap<RegrType, Documentation>> = LazyLock::new
"Computes the square of the correlation coefficient between the independent and dependent variables.",
"regr_r2(expression_y, expression_x)")
+ .with_sql_example(
+ r#"```sql
+create table weekly_performance(day int ,user_signups int) as values (1,60), (2,65), (3, 70), (4,75), (5,80);
+select * from weekly_performance;
++-----+--------------+
+| day | user_signups |
++-----+--------------+
+| 1 | 60 |
+| 2 | 65 |
+| 3 | 70 |
+| 4 | 75 |
+| 5 | 80 |
++-----+--------------+
+
+SELECT regr_r2(user_signups, day) AS r_squared FROM weekly_performance;
++---------+
+|r_squared|
++---------+
+| 1.0 |
++---------+
+```
+"#
+ )
.with_standard_argument("expression_y", Some("Dependent variable"))
.with_standard_argument("expression_x", Some("Independent variable"))
.build()
@@ -193,6 +286,29 @@ static DOCUMENTATION: LazyLock<HashMap<RegrType, Documentation>> = LazyLock::new
"Computes the average of the independent variable (input) expression_x for the non-null paired data points.",
"regr_avgx(expression_y, expression_x)")
+ .with_sql_example(
+ r#"```sql
+create table daily_sales(day int, total_sales int) as values (1,100), (2,150), (3,200), (4,NULL), (5,250);
+select * from daily_sales;
++-----+-------------+
+| day | total_sales |
+| --- | ----------- |
+| 1 | 100 |
+| 2 | 150 |
+| 3 | 200 |
+| 4 | NULL |
+| 5 | 250 |
++-----+-------------+
+
+SELECT regr_avgx(total_sales, day) AS avg_day FROM daily_sales;
++----------+
+| avg_day |
++----------+
+| 2.75 |
++----------+
+```
+"#
+ )
.with_standard_argument("expression_y", Some("Dependent variable"))
.with_standard_argument("expression_x", Some("Independent variable"))
.build()
@@ -205,6 +321,30 @@ static DOCUMENTATION: LazyLock<HashMap<RegrType, Documentation>> = LazyLock::new
"Computes the average of the dependent variable (output) expression_y for the non-null paired data points.",
"regr_avgy(expression_y, expression_x)")
+ .with_sql_example(
+ r#"```sql
+create table daily_temperature(day int, temperature int) as values (1,30), (2,32), (3, NULL), (4,35), (5,36);
+select * from daily_temperature;
++-----+-------------+
+| day | temperature |
+| --- | ----------- |
+| 1 | 30 |
+| 2 | 32 |
+| 3 | NULL |
+| 4 | 35 |
+| 5 | 36 |
++-----+-------------+
+
+-- temperature as Dependent Variable(Y), day as Independent Variable(X)
+SELECT regr_avgy(temperature, day) AS avg_temperature FROM daily_temperature;
++-----------------+
+| avg_temperature |
++-----------------+
+| 33.25 |
++-----------------+
+```
+"#
+ )
.with_standard_argument("expression_y", Some("Dependent variable"))
.with_standard_argument("expression_x", Some("Independent variable"))
.build()
@@ -217,6 +357,29 @@ static DOCUMENTATION: LazyLock<HashMap<RegrType, Documentation>> = LazyLock::new
"Computes the sum of squares of the independent variable.",
"regr_sxx(expression_y, expression_x)",
)
+ .with_sql_example(
+ r#"```sql
+create table study_hours(student_id int, hours int, test_score int) as values (1,2,55), (2,4,65), (3,6,75), (4,8,85), (5,10,95);
+select * from study_hours;
++------------+-------+------------+
+| student_id | hours | test_score |
++------------+-------+------------+
+| 1 | 2 | 55 |
+| 2 | 4 | 65 |
+| 3 | 6 | 75 |
+| 4 | 8 | 85 |
+| 5 | 10 | 95 |
++------------+-------+------------+
+
+SELECT regr_sxx(test_score, hours) AS sxx FROM study_hours;
++------+
+| sxx |
++------+
+| 40.0 |
++------+
+```
+"#
+ )
.with_standard_argument("expression_y", Some("Dependent variable"))
.with_standard_argument("expression_x", Some("Independent variable"))
.build(),
@@ -229,6 +392,27 @@ static DOCUMENTATION: LazyLock<HashMap<RegrType, Documentation>> = LazyLock::new
"Computes the sum of squares of the dependent variable.",
"regr_syy(expression_y, expression_x)",
)
+ .with_sql_example(
+ r#"```sql
+create table employee_productivity(week int, productivity_score int) as values (1,60), (2,65), (3,70);
+select * from employee_productivity;
++------+--------------------+
+| week | productivity_score |
++------+--------------------+
+| 1 | 60 |
+| 2 | 65 |
+| 3 | 70 |
++------+--------------------+
+
+SELECT regr_syy(productivity_score, week) AS sum_squares_y FROM employee_productivity;
++---------------+
+| sum_squares_y |
++---------------+
+| 50.0 |
++---------------+
+```
+"#
+ )
.with_standard_argument("expression_y", Some("Dependent variable"))
.with_standard_argument("expression_x", Some("Independent variable"))
.build(),
@@ -241,6 +425,27 @@ static DOCUMENTATION: LazyLock<HashMap<RegrType, Documentation>> = LazyLock::new
"Computes the sum of products of paired data points.",
"regr_sxy(expression_y, expression_x)",
)
+ .with_sql_example(
+ r#"```sql
+create table employee_productivity(week int, productivity_score int) as values(1,60), (2,65), (3,70);
+select * from employee_productivity;
++------+--------------------+
+| week | productivity_score |
++------+--------------------+
+| 1 | 60 |
+| 2 | 65 |
+| 3 | 70 |
++------+--------------------+
+
+SELECT regr_sxy(productivity_score, week) AS sum_product_deviations FROM employee_productivity;
++------------------------+
+| sum_product_deviations |
++------------------------+
+| 10.0 |
++------------------------+
+```
+"#
+ )
.with_standard_argument("expression_y", Some("Dependent variable"))
.with_standard_argument("expression_x", Some("Independent variable"))
.build(),
diff --git a/docs/source/user-guide/sql/aggregate_functions.md b/docs/source/user-guide/sql/aggregate_functions.md
index 774a4fae6b..88241770a4 100644
--- a/docs/source/user-guide/sql/aggregate_functions.md
+++ b/docs/source/user-guide/sql/aggregate_functions.md
@@ -618,6 +618,29 @@ regr_avgx(expression_y, expression_x)
- **expression_y**: Dependent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
- **expression_x**: Independent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
+#### Example
+
+```sql
+create table daily_sales(day int, total_sales int) as values (1,100), (2,150), (3,200), (4,NULL), (5,250);
+select * from daily_sales;
++-----+-------------+
+| day | total_sales |
+| --- | ----------- |
+| 1 | 100 |
+| 2 | 150 |
+| 3 | 200 |
+| 4 | NULL |
+| 5 | 250 |
++-----+-------------+
+
+SELECT regr_avgx(total_sales, day) AS avg_day FROM daily_sales;
++----------+
+| avg_day |
++----------+
+| 2.75 |
++----------+
+```
+
### `regr_avgy`
Computes the average of the dependent variable (output) expression_y for the non-null paired data points.
@@ -631,6 +654,30 @@ regr_avgy(expression_y, expression_x)
- **expression_y**: Dependent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
- **expression_x**: Independent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
+#### Example
+
+```sql
+create table daily_temperature(day int, temperature int) as values (1,30), (2,32), (3, NULL), (4,35), (5,36);
+select * from daily_temperature;
++-----+-------------+
+| day | temperature |
+| --- | ----------- |
+| 1 | 30 |
+| 2 | 32 |
+| 3 | NULL |
+| 4 | 35 |
+| 5 | 36 |
++-----+-------------+
+
+-- temperature as Dependent Variable(Y), day as Independent Variable(X)
+SELECT regr_avgy(temperature, day) AS avg_temperature FROM daily_temperature;
++-----------------+
+| avg_temperature |
++-----------------+
+| 33.25 |
++-----------------+
+```
+
### `regr_count`
Counts the number of non-null paired data points.
@@ -644,6 +691,29 @@ regr_count(expression_y, expression_x)
- **expression_y**: Dependent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
- **expression_x**: Independent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
+#### Example
+
+```sql
+create table daily_metrics(day int, user_signups int) as values (1,100), (2,120), (3, NULL), (4,110), (5,NULL);
+select * from daily_metrics;
++-----+---------------+
+| day | user_signups |
+| --- | ------------- |
+| 1 | 100 |
+| 2 | 120 |
+| 3 | NULL |
+| 4 | 110 |
+| 5 | NULL |
++-----+---------------+
+
+SELECT regr_count(user_signups, day) AS valid_pairs FROM daily_metrics;
++-------------+
+| valid_pairs |
++-------------+
+| 3 |
++-------------+
+```
+
### `regr_intercept`
Computes the y-intercept of the linear regression line. For the equation (y = kx + b), this function returns b.
@@ -657,6 +727,30 @@ regr_intercept(expression_y, expression_x)
- **expression_y**: Dependent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
- **expression_x**: Independent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
+#### Example
+
+```sql
+create table weekly_performance(week int, productivity_score int) as values (1,60), (2,65), (3, 70), (4,75), (5,80);
+select * from weekly_performance;
++------+---------------------+
+| week | productivity_score |
+| ---- | ------------------- |
+| 1 | 60 |
+| 2 | 65 |
+| 3 | 70 |
+| 4 | 75 |
+| 5 | 80 |
++------+---------------------+
+
+SELECT regr_intercept(productivity_score, week) AS intercept FROM weekly_performance;
++----------+
+|intercept|
+|intercept |
++----------+
+| 55 |
++----------+
+```
+
### `regr_r2`
Computes the square of the correlation coefficient between the independent and dependent variables.
@@ -670,6 +764,29 @@ regr_r2(expression_y, expression_x)
- **expression_y**: Dependent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
- **expression_x**: Independent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
+#### Example
+
+```sql
+create table weekly_performance(day int ,user_signups int) as values (1,60), (2,65), (3, 70), (4,75), (5,80);
+select * from weekly_performance;
++-----+--------------+
+| day | user_signups |
++-----+--------------+
+| 1 | 60 |
+| 2 | 65 |
+| 3 | 70 |
+| 4 | 75 |
+| 5 | 80 |
++-----+--------------+
+
+SELECT regr_r2(user_signups, day) AS r_squared FROM weekly_performance;
++---------+
+|r_squared|
++---------+
+| 1.0 |
++---------+
+```
+
### `regr_slope`
Returns the slope of the linear regression line for non-null pairs in aggregate columns. Given input column Y and X: regr_slope(Y, X) returns the slope (k in Y = k\*X + b) using minimal RSS fitting.
@@ -683,6 +800,29 @@ regr_slope(expression_y, expression_x)
- **expression_y**: Dependent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
- **expression_x**: Independent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
+#### Example
+
+```sql
+create table weekly_performance(day int, user_signups int) as values (1,60), (2,65), (3, 70), (4,75), (5,80);
+select * from weekly_performance;
++-----+--------------+
+| day | user_signups |
++-----+--------------+
+| 1 | 60 |
+| 2 | 65 |
+| 3 | 70 |
+| 4 | 75 |
+| 5 | 80 |
++-----+--------------+
+
+SELECT regr_slope(user_signups, day) AS slope FROM weekly_performance;
++--------+
+| slope |
++--------+
+| 5.0 |
++--------+
+```
+
### `regr_sxx`
Computes the sum of squares of the independent variable.
@@ -696,6 +836,29 @@ regr_sxx(expression_y, expression_x)
- **expression_y**: Dependent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
- **expression_x**: Independent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
+#### Example
+
+```sql
+create table study_hours(student_id int, hours int, test_score int) as values (1,2,55), (2,4,65), (3,6,75), (4,8,85), (5,10,95);
+select * from study_hours;
++------------+-------+------------+
+| student_id | hours | test_score |
++------------+-------+------------+
+| 1 | 2 | 55 |
+| 2 | 4 | 65 |
+| 3 | 6 | 75 |
+| 4 | 8 | 85 |
+| 5 | 10 | 95 |
++------------+-------+------------+
+
+SELECT regr_sxx(test_score, hours) AS sxx FROM study_hours;
++------+
+| sxx |
++------+
+| 40.0 |
++------+
+```
+
### `regr_sxy`
Computes the sum of products of paired data points.
@@ -709,6 +872,27 @@ regr_sxy(expression_y, expression_x)
- **expression_y**: Dependent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
- **expression_x**: Independent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
+#### Example
+
+```sql
+create table employee_productivity(week int, productivity_score int) as values(1,60), (2,65), (3,70);
+select * from employee_productivity;
++------+--------------------+
+| week | productivity_score |
++------+--------------------+
+| 1 | 60 |
+| 2 | 65 |
+| 3 | 70 |
++------+--------------------+
+
+SELECT regr_sxy(productivity_score, week) AS sum_product_deviations FROM employee_productivity;
++------------------------+
+| sum_product_deviations |
++------------------------+
+| 10.0 |
++------------------------+
+```
+
### `regr_syy`
Computes the sum of squares of the dependent variable.
@@ -722,6 +906,27 @@ regr_syy(expression_y, expression_x)
- **expression_y**: Dependent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
- **expression_x**: Independent variable expression to operate on. Can be a constant, column, or function, and any combination of operators.
+#### Example
+
+```sql
+create table employee_productivity(week int, productivity_score int) as values (1,60), (2,65), (3,70);
+select * from employee_productivity;
++------+--------------------+
+| week | productivity_score |
++------+--------------------+
+| 1 | 60 |
+| 2 | 65 |
+| 3 | 70 |
++------+--------------------+
+
+SELECT regr_syy(productivity_score, week) AS sum_squares_y FROM employee_productivity;
++---------------+
+| sum_squares_y |
++---------------+
+| 50.0 |
++---------------+
+```
+
### `stddev`
Returns the standard deviation of a set of numbers.