Skip to content

Commit

Permalink
support for statistical functions #21
Browse files Browse the repository at this point in the history
  • Loading branch information
isuru89 committed Jun 5, 2017
1 parent d405b56 commit bc3eeed
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
22 changes: 22 additions & 0 deletions core/src/main/groovy/com/virtusa/gto/nyql/db/QFunctions.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,28 @@ trait QFunctions {
String.format('COUNT(%s)', ___resolveIn(___val(c) ?: '*', ___pm(c)))
}

/**
* --------------------------------------------------------------
* Stat functions
* --------------------------------------------------------------
*/

@CompileStatic String stat_stddevpop(cx) {
String.format('STDDEV_POP(%s)', ___resolveInP(cx))
}

@CompileStatic String stat_stddevsamp(cx) {
String.format('STDDEV_SAMP(%s)', ___resolveInP(cx))
}

@CompileStatic String stat_varpop(cx) {
String.format('VAR_POP(%s)', ___resolveInP(cx))
}

@CompileStatic String stat_varsamp(cx) {
String.format('VAR_SAMP(%s)', ___resolveInP(cx))
}

/**
* Returns the sum value of given column.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ trait FunctionTraits {
@CompileStatic FunctionColumn AVG(Object column) { fColumn(column, 'avg') }
@CompileStatic FunctionColumn SUM(Object column) { fColumn(column, 'sum') }

// stats functions
@CompileStatic FunctionColumn STDDEV_POP(Object expr) { fColumn(expr, 'stat_stddevpop') }
@CompileStatic FunctionColumn STDDEV_SAMP(Object expr) { fColumn(expr, 'stat_stddevsamp') }
@CompileStatic FunctionColumn VAR_POP(Object expr) { fColumn(expr, 'stat_varpop') }
@CompileStatic FunctionColumn VAR_SAMP(Object expr) { fColumn(expr, 'stat_varsamp') }

@CompileStatic FunctionColumn LCASE(Object column) { fColumn(column, 'lcase') }
@CompileStatic FunctionColumn UCASE(Object column) { fColumn(column, 'ucase') }
@CompileStatic FunctionColumn TRIM(Object column) { fColumn(column, 'trim') }
Expand Down
11 changes: 11 additions & 0 deletions docs/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ Album.rating + 1 AS newRating
|TAN (_value_ ) | Returns the tangent for the given value. |
|TRUNCATE(_column, d_ ) | Truncate the given decimal number to _d_ decimal places. This does not _round_ the number. Negative _d_ values will truncate before the decimal point. |


### Stats Functions

| Function | Details |
|---|---|
|STDDEV_POP (_column_ ) | Population standard deviation of values in given column. |
|STDDEV_SAMP (_column_ ) | Sample standard deviation of values in given column. |
|VAR_POP (_column_ ) | Population variance of values in given column. |
|VAR_SAMP (_column_ ) | Sample variance of values in given column. |


### Casting Functions

| Function | Details |
Expand Down
11 changes: 11 additions & 0 deletions tests/scripts/projection/func2_projection.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,15 @@
mssql: "SELECT ROUND(l.v, 2, 1) FROM `Logs` l",
pg: "SELECT TRUNC(l.v, 2) FROM `Logs` l"
],

$DSL.select {
TARGET (Stats.alias('s'))
FETCH (STDDEV_POP(s.income), STDDEV_SAMP(s.income), VAR_POP(s.income), VAR_SAMP(s.income))
GROUP_BY (s.year)
},
[
mysql: "SELECT STDDEV_POP(s.income), STDDEV_SAMP(s.income), VAR_POP(s.income), VAR_SAMP(s.income) FROM `Stats` s GROUP BY s.year",
mssql: "SELECT STDEVP(s.income), STDEV(s.income), VARP(s.income), VAR(s.income) FROM `Stats` s GROUP BY s.year",
pg: "SELECT STDDEV_POP(s.income), STDDEV_SAMP(s.income), VAR_POP(s.income), VAR_SAMP(s.income) FROM `Stats` s GROUP BY s.year"
],
]
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ abstract class MSSqlFunctions extends AbstractSQLTranslator implements QFunction
super(theOptions)
}

@Override
String stat_stddevpop(Object cx) {
return String.format('STDEVP(%s)', ___resolveInP(cx))
}

@Override
String stat_stddevsamp(Object cx) {
return String.format('STDEV(%s)', ___resolveInP(cx))
}

@Override
String stat_varpop(Object cx) {
return String.format('VARP(%s)', ___resolveInP(cx))
}

@Override
String stat_varsamp(Object cx) {
return String.format('VAR(%s)', ___resolveInP(cx))
}

@CompileStatic
@Override
String truncate(Object cx) {
Expand Down

0 comments on commit bc3eeed

Please sign in to comment.