Formula language¶
Dataiku DSS includes a language to write formulas, much like a spreadsheet.
Formulas can be used:
In data preparation, to create new columns, filter rows or flag rows
More generally, to filter rows in many places of Dataiku DSS:
In the Filtering recipe, to filter rows
In Machine Learning, to define the extracts to use for train and test set
In the Python and Javascript APIs, to obtain partial extracts from the datasets
In the Public API, to obtain partial extracts from the datasets
In the grouping, window, join and stack recipes, to perform pre and post filtering
Basic usage¶
Formulas define an expression that applies row per row.
Unless stated otherwise, the behavior described below is that of the Dataiku DSS engine. In a recipe using the SQL engine, formulas are translated to SQL and use the database’s column types and functions. Only formulas that can be fully translated are supported, and results can differ from those in the preparation preview, which uses the Dataiku DSS engine. See Execution engines for details.
Assuming that you have a dataset with columns N1 (numeric), N2 (numeric) and S (string) , here are a few example formulas:
2 + 2N1 + N2min(N1, N2)# Returns the smallest of N1 and N2replace(S, 'old', 'new')# Returns the value of S with ‘old’ replaced by ‘new’if (N1 > N2, 'big', 'small')# Returns big if N1 > N2, small otherwise
Note
We also have a How-To that lists common use cases with examples.
Reading column values¶
In almost all formulas, you’ll need to read the values of the columns for the current row.
When the column has a “simple” name (i.e., starting with a letter or underscore, containing only letters, numbers and underscores), you just need to use the name of the column in the formula: N1 + 4
For other cases, you can use:
val("column with spaces"): returns the value of “column with spaces”strval("column with spaces"): returns the value of “column with spaces” as a stringnumval("column with spaces"): returns the value of “column with spaces” as a number
Warning
mycolumn evaluates to the value of the column named mycolumn
Thus, strval(mycolumn) would not work because it would try to read as a string the value of
the column whose name is the value of the mycolumn column.
Instead, use strval("mycolumn").
You may also need to use strval to avoid Dataiku DSS automatically typing a numerical variable. See Variables typing and autotyping for more details.
Accessing values from previous rows¶
The val, strval, and numval functions optionally accept an offset argument. This allows you to access values from previous rows in the dataset. See Value access functions for more details.
Note
With the Dataiku DSS engine, the offset argument is available in the Formula processor and in formula actions of the Create if, then, else statements processor in the Prepare recipe. It is not available in conditional predicates or the Filter and Flag formula processors.
Variables typing and autotyping¶
Variables in the formula language can have one of the following types: string, integer, decimal, array, object, boolean, date
Regardless of the schema of the dataset, or the meanings of the columns (in the case of working on dynamically-generated columns), the following rules are applied:
If column values are “standard” integer, they are automatically parsed to integer
Otherwise, if column values are “standard” decimal, e.g. “3.14” or “5f” or “6D”, they are automatically parsed to decimal
Else, they are kept as string
Columns containing dates, arrays, objects or booleans are not automatically converted to the matching Formula type. However:
array functions such as
arrayLen,arrayContains,arraySort,join, andforEachaccept JSON array strings. Indexing,get,slice,substring, andlengthoperate on the string itself unless you first useparseJsondate functions such as
datePart,diff,inc, andtruncautomatically attempt to convert strings to dates (using ISO-8601 for dates with a time zone,yyyy-MM-ddfor dates only, oryyyy-MM-dd HH:mm:sswith optional fractional s
In other words, if the column “begin_date” is a date (and thus contains properly-formatted ISO-8601):
type(begin_date)returns “string”inc(begin_date, 2, "days")works as expected because the inc function performed automatic conversion to date
Avoiding auto-typing¶
There are some cases where auto-typing is not a good thing. For example, a value like 012345 would be automatically converted to the number 12345 and 5f to 5.0.
To avoid autotyping to numerical, use the
strval("mycolumn")function (see above)To manually obtain an array or object, use the
parseJsonfunctionTo manually obtain a number, use the
numval("mycolumn")functionTo manually obtain a date, use the
asDatetimeTz,asDatetimeNoTzorasDateOnlyfunctionTo manually obtain a boolean, use the
asBoolfunction
Boolean values¶
Comparisons and boolean functions return boolean values. Use asBool(1) and asBool(0) to obtain boolean constants. Quoted values such as "true" and "false" are strings; unquoted true and false are treated as variable or column names in recipe formulas.
if, and, or, and not also accept the string "true" (case-insensitive) as true. Other strings and numbers are treated as false. Use asBool to convert other supported representations, such as "yes" or a non-zero number, to a boolean first.
The operators &&, ||, and !, and the Filter and Flag formula processors, additionally consider non-zero numbers as true. The filter and objectFilter control structures require an actual boolean true to keep an element.
Operators¶
The formula language supports the classical arithmetic operators:
Regular math operators:
+,-,*,/Comparison operators (evaluate to booleans):
>,>=,<,<=,==,!=Arithmetic operators:
//(division truncated toward zero when both operands are integers; regular division otherwise) and%(remainder)Boolean operators:
&&,||,!The
+operator performs string concatenation on Dataiku DSS engine but not on most SQL engines. Use theconcatfunction instead.The comparison operators (and only them) can operate on dates. For dates arithmetic, see the
diffandincfunctions. Two strings are compared lexicographically; convert date strings to the same date type for chronological comparisons.&&and||evaluate left to right and stop once the result is known. Theandandorfunctions evaluate all their arguments.The minus sign can prefix a number literal, for example
-5. To negate a column or expression, use subtraction, for example0 - N1.Negative number literals are typed as decimals. For example,
-7 // 2returns-3.5, while(0 - 7) // 2uses integer operands and returns-3.
Regular expressions¶
Functions such as match, replace, split, partition, and rpartition accept regular expression literals delimited by slashes, for example /\s+/. Add i after the closing slash for case-insensitive matching, for example /hello/i. Backslashes are preserved in regular expression literals; in quoted strings, double them to preserve them, for example "\\s+".
For replace, split, partition, and rpartition, a quoted pattern is a literal string. For match, a quoted pattern is interpreted as a regular expression.
Array and object operations¶
Formulas support array literals such as [1, 2, 3] and accessing array elements and object keys using the traditional Python/Javascript syntax:
array[0]object["key"]object.key(only valid if ‘key’ is an identifier containing letters, digits and underscores, with at least one letter or underscore)
Note: this requires that you actually have an array or object. You might need to use the parseJson function (see above paragraph about typing)
Array indexes start at 0. Negative indexes count from the end, so array[-1] returns the last element. To create an object from key/value pairs, use objectNew, for example objectNew("name", "Alice"). If you already have the object as JSON text, use parseJson, for example parseJson('{"name": "Alice"}'). You cannot write {"key": "value"} directly in a formula.
Object notations¶
Most functions can be called in either of these two equivalent ways: replace(str, "a", "b") or str.replace("a", "b").
The following operations cannot be called after a dot:
if,switch, andwithforEach,forEachIndex,forRange,filter, andobjectFilterisNull,isNotNull,isBlank,isNonBlank,isNumeric, andisError
For example, isBlank(str) is valid, but str.isBlank() is not supported.
In object notation, the first argument to the function is replaced by its ‘context’.
You can chain functions and combine both forms. For example, the two syntaxes are equivalent:
length(trim(replace(foo, 'a', 'b')))
foo.replace('a', 'b').trim().length()
Dataiku DSS variables¶
You can retrieve the value of Dataiku DSS variables via the syntax ${variable_name} or variables["variable_name"]. Both syntaxes have slightly different behaviors.
variables[“variable_name”]¶
Dataiku DSS evaluates variables["variable_name"] as JSON during the formula evaluation. If there is no variable defined with the supplied name, an empty cell is returned.
For example, given the following global variables
{
"max_height": 500,
"warning_msg": "too tall",
"msgs": {"ok":"OK", "over":"too tall"}
}
The formula if(height < variables["max_height"], "OK", variables["warning_msg"]) is evaluated as if(height < 500.0, "OK", "too tall"). Note that the max_height is evaluated as 500.0 as JSON consider all numbers to be decimal.
The formula if(height < variables["max_height"], "OK", variables["msgs"]["over"]) is evaluated as if(height < 500.0, "OK", "too tall").
${variable_name}¶
During processing Dataiku DSS replaces the ${variable_name} placeholder with its verbatim content and then evaluate the formula. If there is no variable defined with the supplied name, an error is returned.
For example, given the following global variables:
{
"max_height": 500,
"warning_msg": "too tall",
"msgs": {"ok":"OK", "over":"too tall"}
}
The formula if(height < ${max_height}, "OK", "${warning_msg}") is converted into if(height < 500, "OK", "too tall") before being evaluated. Note that quotes have been added around ${warning_msg} to output an actual string.
The formula if(height < ${max_height}, "OK", ${warning_msg}) is converted into if(height < 500, "OK", too tall) before being evaluated and will therefore issue a syntax error.
The formula if(height < ${max_height}, "OK", ${msgs}["over"]) is converted into if(height < 500, "OK", {"ok":"OK", "over":"too tall"}["over"]) before being evaluated and will therefore issue a syntax error.
The formula if(height < ${max_height}, "OK", ${msgs.over}) will throw a syntax error as there is no variable named “msgs.over”.
The formula if(height < ${max_height}, "OK", parseJson('${msgs}')["over"]) is converted into if(height < 500, "OK", parseJson('{"ok":"OK", "over":"too tall"}')["over"]) before being evaluated and will be evaluated as if(height < 500.0, "OK", "too tall").
Array functions¶
arrayContains(array a, item) boolean
Returns whether the array
acontainsitem
arrayContains([1, 2, 3], 5)returnsfalse
argsToArray(a1, a2 …) array
Returns all arguments passed to it as an array.
argsToArray(1, 2, 3)returns[1,2,3]
arrayDedup(array a) array
Returns array
awith duplicates removed. The order of the result is not guaranteed.
arrayDedup([0, 1, 0, 7])returns an array containing0,1, and7, in an unspecified order
arrayIndexOf(array a, item) int
Returns the index (starting from 0) of
itemin the arraya, or -1 if the item is not found in the array.
arrayIndexOf([1 , 0], 2)returns-1
arrayLen(array a) int
Returns the length of the array
a
arrayLen([1,2,3])returns3
arrayReverse(array a) array
Reverses array a
arrayReverse([1,2,3])returns[3,2,1]
arraySort(array a) array
Sorts array
a
arraySort(["b", "1", "a"])returns["1","a","b"]
get(array a, from index, [to index]) Output depends on arguments
Returns one element or a range of elements from an array. Indexes start at 0. When both
fromandtoare provided,fromis included andtois excluded. Whentois omitted, returns the element atfrom. Negative indexes count from the end of the array.
get([1,2,3,4,5], 1, 4)returns[2,3,4]
get([1,2,3,4,5], 1)returns2
join(array a, string sep) string
Returns the string obtained by joining the elements of array
awith the separatorsepFor example, the expression
join(date_elements, '-')on a columndate_elementsproduces:
date_elements
output
[2007, 7, 15]
2007-7-15
[2016, 1, 8]
2016-1-8
objectKeys(object o) array
Returns the keys of an object as an array
objectValues(object o) array
Returns the values of an object as an array
slice(object o, from index, [to index]) Output depends on arguments
If
ois an array, returns an array containing the items betweenfromandto(excluded). Ifois a string, returns the part of the string betweenfromandto(excluded). Omitting the optional number to will return the remainder of the string or array. Note:sliceandsubstringare identical.
slice([1,2,3,4,5], 1, 4)returns[2,3,4]
slice('hello', 1)returns'ello'
substring(object o, from index, [to index]) Output depends on arguments
If
ois a string, returns the part of the string betweenfromandto(excluded). Ifois an array, returns an array containing the items betweenfromandto(excluded). Omitting the optional number to will return the remainder of the string or array. Note:substringandsliceare identical.
substring('0123456', 2, 5)returns'234'
substring([1,2,3,4,5,6], 3)returns[4,5,6]
Boolean functions¶
asBool(o) boolean
Returns
oconverted to a boolean. A number is true if it is non-zero. Recognized strings are"true","yes","1","y","t", and"o"for true, and"false","no","0","n", and"f"for false, ignoring case. Null or an empty string returns null; an unrecognized string returns an error.
asBool(0)returnsfalse
asBool('yes')returnstrue
isFalse(boolean b) boolean
Returns whether
bis false.
isFalse('false')returnsfalse(the argument is a string, not a boolean, so this method returnsfalse)
isFalse(asBool(0))returnstrue
isTrue(boolean b) boolean
Returns whether
bis true.
isTrue('true')returnsfalse(the argument is a string, not a boolean, so this method returnsfalse)
isTrue(asBool(1))returnstrue
and(boolean a, boolean b, …) boolean
Evaluates logical AND (conjunction) on several statements. All conditions need to be fulfilled for the function to return true. All arguments are evaluated. See Boolean values for accepted values and differences from
&&.
and(1==1, 3<4)returnstrue
not(boolean b) boolean
Evaluates logical NOT (negation) of a statement, returning the opposite of
b.
not(1!=1)returnstrue
or(boolean a, boolean b, …) boolean
Evaluates logical OR (disjunction) on several statements. At least one condition needs to be fulfilled for the function to return true. All arguments are evaluated. See Boolean values for accepted values and differences from
||.
or(1==1, 3>4)returnstrue
Date functions¶
asDatetimeTz(object o, [format1, …]) datetime tz
Returns
oconverted todatetime tz(date/time with time zone), based on the format(s) provided. If your column contains more than one date format, you can give an ordered list of possible formats. If you don’t give a format, ISO-8601 is used by default. Custom formats use Joda-Time pattern syntax. Values without a time zone are interpreted in UTC. Here are some pattern symbols:
‘y’ (Year)
‘M’ (month in year)
‘w’ (week in year)
‘d’ (day in month)
‘D’ (day in year)
‘E’ (day name in week)
‘e’ (day number in week. Monday =1)
‘H’ (hour in day, 0-23)
‘m’ (minute)
‘s’ (second)
‘S’ (millisecond)
asDatetimeTz('2020-04-15', 'yyyy-MM-dd')returns2020-04-15T00:00:00.000ZUse
asDatetimeTzinstead of its deprecated aliasasDatein new formulas.Note
The format arguments are ignored when using the SQL engine.
asDatetimeNoTz(object o, [format1, …]) datetime no tz
Returns
oconverted todatetime no tz(date/time without time zone), based on the format(s) provided. If your column contains more than one date format, you can give an ordered list of possible formats. Without a format, it acceptsyyyy-MM-dd HH:mm:sswith optional fractional seconds, and ISO-8601 values. Custom formats use Joda-Time pattern syntax. Here are some pattern symbols:
‘y’ (Year)
‘M’ (month in year)
‘w’ (week in year)
‘d’ (day in month)
‘D’ (day in year)
‘E’ (day name in week)
‘e’ (day number in week. Monday =1)
‘H’ (hour in day, 0-23)
‘m’ (minute)
‘s’ (second)
‘S’ (millisecond)
asDatetimeNoTz('2020-04-15', 'yyyy-MM-dd')returns2020-04-15 00:00:00.000Note
The format arguments are ignored when using the SQL engine.
asDateOnly(object o, [format1, …]) date only
Returns
oconverted todate only(Gregorian calendar date), based on the format(s) provided. If your column contains more than one date format, you can give an ordered list of possible formats. Without a format, it accepts a date such asyyyy-MM-ddoryyyy/MM/dd, or a date/time value whose time component is discarded. Custom formats use Joda-Time pattern syntax. Here are some pattern symbols:
‘y’ (Year)
‘M’ (month in year)
‘w’ (week in year)
‘d’ (day in month)
‘D’ (day in year)
‘E’ (day name in week)
‘e’ (day number in week. Monday =1)
asDateOnly('2020-04-15', 'yyyy-MM-dd')returns2020-04-15Note
The format arguments are ignored when using the SQL engine.
datePart(date d, string part, [timezone]) Output depends on arguments
Extracts a date component from
date d(date being either datetime tz, datetime no tz or date only). Components of adatetime tzare extracted in UTC unless the optionaltimezoneis provided. Date-only and date/time-without-time-zone values keep their local components. Weekday names are in English (see a complete list of supported timezones). Component names are case-insensitive. The available components are:
‘years’ (or ‘year’)
‘isoWeekYear’ gives the ISO week-numbering year
‘months’ (or ‘month’)
‘quarters’ (or ‘quarter’ or ‘q’) gives the quarter of the year, from 1 to 4
‘weekOfYear’ gives the week of the year (as per server locale’s convention)
‘isoWeekOfYear’ gives the ISO week number of the year
‘weeks’ (or ‘weekOfMonth’ or ‘week’ or ‘w’) gives the week of the month (according to the server locale’s convention)
‘isoWeekOfMonth’ (or ‘isoWeeks’ or ‘isoWeek’) gives the week of the month using ISO week rules
‘days’ (or ‘day’ or ‘d’)
‘weekday’ (the name of the day of the week, capitalized)
‘dayofweek’ (day of the week as a number. Monday=1, Tuesday=2, … Sunday=7)
‘hours’ (or ‘hour’ or ‘h’)
‘minutes’ (or ‘minute’ or ‘min’)
‘seconds’ (or ‘second’ or ‘sec’ or ‘s’)
‘unixTime’ (number of seconds since epoch)
‘milliseconds’ (or ‘millisecond’ or ‘ms’)
‘time’ (number of milliseconds since epoch)
datePart('2020-04-15T00:00:00.000Z', 'weekday')returns"Wednesday"
datePart('2020-01-01T00:00:00.000Z', 'year', '-08:00')returns2019
diff(date d1, date d2, [string unit]) number
Returns
d1 - d2expressed in whole time units, truncating partial units toward zero. The default unit of time isdays. The available units are:
‘years’ (or ‘year’)
‘months’ (or ‘month’)
‘weeks’ (or ‘week’ or ‘w’)
‘days’ (or ‘day’ or ‘d’)
‘hours’ (or ‘hour’ or ‘h’)
‘minutes’ (or ‘minute’ or ‘min’)
‘seconds’ (or ‘sec’ or ‘s’)
diff('2019-03-15T00:00:00.000Z', '2020-04-15T00:00:00.000Z', 'month')returns-13
diff('2019-03-15 11:17:59', '2019-03-15 11:12:13', 'min')returns5
diff('2019-03-01', '2019-02-01', 'days')returns28
inc(date d, number value, string unit) datetime tz, datetime no tz or date only depending on arguments
Returns
dincremented or decremented by the number value in the specified unit of time; a decimal number value will be truncated to an integer. The available units are:
‘years’ (or ‘year’)
‘months’ (or ‘month’)
‘weeks’ (or ‘week’ or ‘w’)
‘days’ (or ‘day’ or ‘d’)
‘hours’ (or ‘hour’ or ‘h’)
‘minutes’ (or ‘minute’ or ‘min’)
‘seconds’ (or ‘sec’ or ‘s’)
inc('2020-04-15T00:00:00.000Z', -3, 'week')returns2020-03-25T00:00:00.000Z
inc('2020-04-15', 2, 'days')returns2020-04-17
now() datetime tz
Returns the current time
trunc(date d, string unit) datetime tz, datetime no tz or date only depending on arguments
Returns
dtruncated to the specified unit. Dates with a time zone are truncated in UTC. Truncating to a week returns the Monday of that week. The available units are:
‘years’ (or ‘year’)
‘quarters’ (or ‘quarter’ or ‘q’)
‘months’ (or ‘month’)
‘weeks’ (or ‘week’ or ‘w’)
‘days’ (or ‘day’ or ‘d’)
‘hours’ (or ‘hour’ or ‘h’)
‘minutes’ (or ‘minute’ or ‘min’)
‘seconds’ (or ‘sec’ or ‘s’)
trunc('2020-04-03T07:47:45.245Z', 'month')returns2020-04-01T00:00:00.000Z
trunc('2020-04-03', 'month')returns2020-04-01
Math functions¶
abs(number d) number
Returns the absolute value of a number
abs(-7)returns7.0
acos(number d) number
Returns the arc cosine of an angle, in the range 0 through PI
acos(-1)returns approximately3.141592654
asin(number d) number
Returns the arc sine of an angle in the range of -PI/2 through PI/2
asin(1)returns approximately1.570796327
atan(number d) number
Returns the arc tangent of an angle in the range of -PI/2 through PI/2
atan2(number y, number x) number theta
Returns the angle theta in radians for rectangular coordinates (x, y), in the range -PI through PI. The first argument is y and the second is x.
atan2(1, 0)returns approximately1.570796327
avg(array a | value n1, value n2, …) number
Returns the average of an array or a series of numbers, including numeric strings (e.g., “4.5”). Ignores empty elements, non-numeric values, and nested arrays. Returns
0if there are no numeric values.
avg([1, "2", 3, ""])returns2.0
avg(1, "2", 3, "")returns2.0
ceil(number n) number
Returns the ceiling of a number
ceil(4.67)returns5
ceil(-4.67)returns-4
combin(number n, number k) number
Returns the number of different groups of
kitems you can choose fromnitems. The order of items within a group does not matter.
combin(3, 2)returns3: choosing 2 items from A, B, and C gives three pairs: AB, AC, and BC. AB and BA count as the same pair.
combin(6, 2)returns15
cos(number d) number
Returns the trigonometric cosine of an angle
cos(0)returns1.0
cosh(number d) number
Returns the hyperbolic cosine of a value
cosh(0)returns1.0
dec2hex(long) string
Returns an hexadecimal representation of the input number
dec2hex(10)returnsadec2hex(256)returns100
degrees(number d) number
Converts an angle from radians to degrees.
degrees(PI())returns180.0
even(number n) number
Rounds the number up to the nearest even integer
even(3)returns4
even(2.3)returns4.0
even(-2.3)returns-2.0
even(-3)returns-2.0
exp(number n) number
Returns the exponential of a number
exp(2)returns7.38905609893065
fact(number i) number
Returns the factorial of a number i
fact(4)returns24
factn(number i, number d) number
Returns the product
i * (i-d) * (i-2*d) * .... Use a positive integer stepd. The computation stops when the remaining value is 0 or 1, and returns an error if it becomes negative.
factn(7, 3)returns28(7 * 4 * 1, subtracting 3 at each step)
floor(number d) number
Returns the floor of a number
floor(4.7)returns4
gcd(number d, number e) number
Returns the greatest common divisor of the two numbers
gcd(21, 28)returns7.0
hash(string) long
Returns a 64-bit numerical hash of the input (not crypto-secure). The result is not stable across processes or Dataiku DSS restarts. Use
sha256if you need a reproducible hash.
hex2dec(string) long
Returns a decimal representation of an hexadecimal string
hex2dec("a")returns10
lcm(number d, number e) number
Returns the least common multiple of the two numbers
lcm(20, 42)returns420.0
ln(number n) number
Returns the natural log of a number
ln(exp(1))returns1.0ln(2.72)returns1.000631880307906
log(number n) number
Returns the base 10 log of a number
log(100)returns2.0
max(a, b, …) Output depends on arguments
Returns the greatest of two or more numbers or strings, or the most recent of two or more dates. Arguments must all be numbers, all be strings, or all have the same date type. Strings are compared lexicographically; use a date conversion function for chronological comparisons. Returns null if any argument is null or an empty string.
With the Dataiku DSS engine, values returned by
now()are not supported, including after conversion withasDatetimeTz(now()). Useinc(now(), 0, "days")to convert the current time to a supporteddatetime tzvalue without changing the instant.
max(-1, 3)returns3
max('luke', 'leia')returns'luke'
max(asDatetimeTz('2020-01-01T00:00:00.000Z'), asDatetimeTz('2021-01-01T00:00:00.000Z'))returns2021-01-01T00:00:00.000Z
max(inc(now(), 0, "days"), asDatetimeTz("2020-01-01T00:00:00.000Z"))returns the later of the current time and the specified date
min(a, b, …) Output depends on arguments
Returns the smallest of two or more numbers or strings, or the oldest of two or more dates. Arguments must all be numbers, all be strings, or all have the same date type. Strings are compared lexicographically; use a date conversion function for chronological comparisons. Returns null if any argument is null or an empty string.
With the Dataiku DSS engine, values returned by
now()are not supported, including after conversion withasDatetimeTz(now()). Useinc(now(), 0, "days")to convert the current time to a supporteddatetime tzvalue without changing the instant.
min(-1, 3)returns-1.0
min('luke', 'leia')returns'leia'
min(asDatetimeTz('2020-01-01T00:00:00.000Z'), asDatetimeTz('2021-01-01T00:00:00.000Z'))returns2020-01-01T00:00:00.000Z
min(inc(now(), 0, "days"), asDatetimeTz("2020-01-01T00:00:00.000Z"))returns the earlier of the current time and the specified date
mod(number a, number b) number
Returns the remainder of a divided by b, after truncating both arguments to integers. To retain fractional values, use the
%operator instead.
mod(5, 3)returns2
mod(7.8, 3)returns1
multinomial(number d1, number d2 …) number
Returns the multinomial of a series of numbers,
(sum(d1, d2, d3,...))! / (d1! * d2! * d3! * ...)
multinomial(1, 1, 2, 1)returns60
odd(number d) number
Rounds the number up to the nearest odd integer
odd(5.3)returns7.0
odd(-5.3)returns-5.0
PI() number
Returns the value of PI
pow(number a, number b) number
Returns
ato the power ofb
pow(2, -1)returns0.5
quotient(number numerator, number denominator) number
Returns the division rounded down to the nearest integer.
quotient(7, 2)returns3
quotient(-7, 2)returns-4
radians(number d) number
Converts an angle in degrees to radians
radians(180)returns3.141592653589793
rand([long min], [long max]) double or long
Without arguments, returns a random decimal between 0 (inclusive) and 1 (exclusive). Otherwise, both
minandmaxmust be supplied as integers, with0 <= min < max. Returns a random long integer betweenmin(inclusive) andmax(exclusive).
round(number n) number
Returns the rounding of number to the nearest integer
round(3.5)returns4
round(-3.5)returns-3
sin(number d) number
Returns the trigonometric sine of an angle
sin(radians(90))returns1.0
sinh(number d) number
Returns the hyperbolic sine of an angle
sinh(0)returns0.0
sqrt(number n) number
Returns the square root of number n.
sqrt(81)returns9.0
sum(array a | value1, value2 …) number
Sums an array or a series of numbers, including numeric strings. Ignores empty elements, non-numeric values, and nested arrays. Returns
0.0if there are no numeric values.
sum([1, 2, "string", 3])returns6.0
sum([1, 2, 3])returns6.0
sum(1, "2", 3)returns6.0
sum([[10, 11], 1, 2, "string", 3])returns6.0
tan(number d) number
Returns the trigonometric tangent of an angle
tan(0)returns0.0
tanh(number d) number
Returns the hyperbolic tangent of a value
toNumber(o) number
Returns o converted to a number
toNumber("5")returns5
Object functions¶
get(object o, string field, [defaultValue]) Output depends on arguments
Returns
o[field]. If the field is missing or its value is JSON null, returnsdefaultValueif supplied, or null otherwise. An empty string is returned as-is.
get(parseJson('{"name":"joe", "age":42}'), "age")returns42
get(parseJson('{"name":"joe"}'), "age", 37)returns37
getPath(object o, string path, [defaultValue]) Output depends on arguments
Evaluates the JsonPath path on the object o and returns the result when defined or defaultValue.
getPath(parseJson('{"date":{"day":1,"month":"May"}}'), "date.day")returns1
getPath(parseJson('{"date":{"day":1,"month":"May"}}'), "date.year")fails because the path does not exist and there is no default value
getPath(parseJson('{"date":{"day":1,"month":"May"}}'), "date.year", "N/A")returns"N/A"
hasField(object o, string name) boolean
Returns whether
ohas the fieldname
hasField(parseJson('{"name": "joe", "age": 42 }'), "age")returnstrue
htmlAttr(Element e, String s) string
Selects a value from an attribute on an Html Element
htmlAttr(select(parseHtml('<div><a href="www.dataiku.com">Dataiku</a></div>'), '[href]')[0], "href")returns"www.dataiku.com"
htmlText(Element e) string
Selects the text from within an element (including all child elements)
htmlText(parseHtml('<div>Hello <b>world</b></div>'))returns"Hello world"
innerHtml(Element e) string
The innerHtml of an HTML element
jsonize(value) string
Serializes a value as JSON text. Strings are quoted and escaped; arrays and objects are serialized as JSON arrays and objects.
objectDel(object o, key, [key…]) object
Removes one or several keys from an object and returns it. The keys must not be null
objectDel(parseJson('{"firstName": "birdie", "company": "Dataiku" }'), 'firstName')returns{"company":"Dataiku"}
objectNew(k1, v1, k2, v2, …) object
Creates a new object, optionally pre-filled with key/values. Must get an even number of arguments, as successive key-value pairs. Giving 0 arguments is possible and will return an empty object (you can use objectPut to add to it)
objectNew("firstName", "birdie", "company", "Dataiku")returns{"firstName": "birdie", "company": "Dataiku" }
objectPut(object o, key, value) object
Adds or replaces a key/value pair in an object and returns it. The key must not be null. A null value removes the key.
ownText(Element e) string
Gets the text owned by this HTML element only; does not get the combined text of all children.
parseHtml(string s) HTML object
Parses a string as HTML
parseJson(string s) Output depends on input
Parses JSON text into an object, array, string, number, or boolean value.
select(Element e, String s) HTML Elements
Selects an element from an HTML element using selector syntax
type(object o) string
Returns the type of
o
type(3.126)returns"number"
String functions¶
char(number value) string
Returns the character that represents the specified unicode of
value.
char(65)returns"A"
chomp(string s, string tail) string
Removes
tailfrom the end ofsif it’s there, otherwise leaves it alone.
chomp("foobar", "bar")returns"foo"
coalesce(value1, value2, …) Output depends on arguments
Returns the first non-empty value.
coalesce("", "foo")returns"foo"
concat(object a1, [object a2, …]) string
Returns a string of concatenated values. If one of your columns contains leading 0 (zeros), wrap it in a
strval('column_name')to preserve them.
concat("Birds", " ", "fly")returns"Birds fly"
concat(1, 2, 3)returns"123"
contains(string s, string frag) boolean
Returns whether
scontainsfrag
contains("hello world ", "llo")returnstrue
endsWith(string s, string tail) boolean
Returns whether
sends withtail
endsWith("hello world", "rld")returnstrue
escape(string s, string mode) string
Escapes a
susing the escaping mode specified. Supported modes: ‘html’, ‘xml’, ‘csv’, ‘url’, ‘javascript’. Note that the escape uses the standard java URLEncoder for urls and StringEscapeUtils for others
format(string format, object… args) string
Formats a string using printf-like formatting using the Java Formatter syntax.
'%4d-%02d'.format(2004,2)returns'2004-02'
fromBase64(string s, [string charset]) string
Returns the string whose Base64 representation is given. By default, the string is read using the UTF-8 charset.
fromBase64('SA==')returns"H"
get(string s, from index, [to index]) Output depends on arguments
Returns
s.substring(from, to). Omitting the optional number to will return what is at the first index specified.
get('Oh no, kittens!', 0, 5)returns'Oh no'
get('Oh no, kittens!', 1)returns'h'
indexOf(string s, string sub) number
Returns the index of the first occurrence of
subins. Index begins at 0. Returns -1 if there is no such occurrence.
indexOf("hello world", "world")returns6
lastIndexOf(string s, string sub) number
Returns the index of the last occurrence of
subins. Index begins at 0. Returns -1 if there is no such occurrence.
lastIndexOf("hello world", "o")returns7
length(array or string o) number
Returns the length of o
length("hello world")returns11
length([4,5,6])returns3
match(string s, string or regexp pattern) array of strings
Returns an array of the matching groups found in
s. Groups are designated by () within the specified string or regular expression. The entire string must match. If there are no capturing groups, returns an array containing the whole matched string. Returns null if there is no match.
match('hello world', 'he(.*)wo(rl)d')returns["llo ","rl"]match('hello world', 'he(.*)wo(rl)')returnsnull(no match)
md5(string s) string
Returns the MD5 hash of a string
md5('hi')returns"49f68a5c8493ec2c0bf489821c21fc3b"
ord(string s) number
Returns the number representing the unicode code of a specified character in
s. Thesstring must contain only one character.
ord('A')returns65
partition(string s, string or regex frag, [boolean omitFragment]) array
Returns an array of strings
[a,frag,b]whereais the part before the first occurrence offraginsandbis the part after the occurrence. IfomitFragmentis true,fragis not returned in the array.
partition("hello", "he")returns["","he","llo"]
partition("hello", "he", asBool(1))returns["","llo"]
partition("hello", /.l/)returns["h","el","lo"]
replace(string s, string or regex f, string replacement) string
Replaces all occurrences of substring / regex
ffound in stringswith thereplacementstring.
replace('hello world', 'hel', 'a')returns"alo world"
replace("Oh my!", /\w/, 'x')returns"xx xx!"
replaceChars(string s, string f, string r) string
Returns the string obtained by replacing all character in
sthat matchfwith the character inrat that same position. The function can be used to delete characters by replacing them with nothing.
replaceChars('abcba', 'bc', 'BC')returnsaBCBareplaceChars('abcba', 'bc', 'Z')returnsaZZa
rpartition(string s, string or regex frag, [boolean omitFragment]) array
Returns an array of strings
[a,frag,b]whereais the part before the last occurrence offraginsandbis the part after the occurrence. IfomitFragmentis true,fragis not returned in the array.
rpartition("hello world", "o")returns["hello w","o","rld"]
sha1(string s) string
Returns the SHA-1 hash of a string
sha1('goo')returns3f95edc0399d06d4b84e7811dd79272c69c8ed3a
sha256(string s) string
Returns the SHA-256 hash of a string
sha512(string s) string
Returns the SHA-512 hash of a string
split(string s, string or regex sep, [boolean preserveAllTokens]) array
Returns the array of strings obtained by splitting
swith separatorsep. For a string separator,preserveAllTokenspreserves empty segments when true. For a regular expression separator, this argument is ignored and trailing empty segments are discarded.
split("hello", "he")returns["llo"]
split("hello", "he", asBool(1))returns["","llo"]
split("hello", /.l/)returns["h","lo"]
split("Hello world!", /\s+/)returns["Hello","world!"]
splitByCharType(string s) array
Returns an array of strings obtained by splitting s grouping consecutive chars by their unicode type
splitByCharType("Hello_world 101!?!")returns["H","ello","_","world"," ","101","!?!"]
splitByLengths(string s, number length1, […]) array
Returns an array of strings obtained by splitting
sinto substrings, each with its own specified length.
splitByLengths("Hello world", 1, 2, 3, 4)returns["H","el","lo ","worl"]
startsWith(string s, string sub) boolean
Returns whether
sstarts withsub
startsWith("Hello world", "He")returnstrue
strip(string s) string
Returns copy of the string, with leading and trailing whitespace omitted. This function is the same as trim.
strip(" Hello World ")returns"Hello World"
toBase64(string s, [string charset]) string
Returns the Base64 representation of a string. By default, the string is written using the UTF-8 charset.
toBase64("H")returns"SA=="
toLowercase(string s) string
Converts a string to lowercase
toLowercase("HELLO WORLD")returns"hello world"
toString(o, [string format]) string
Returns
oconverted to a string. The optionalformatapplies to date values; it is ignored for other values. Convert a date string to a date first, for exampletoString(asDatetimeTz("2020-04-15"), "yyyy-MM-dd").With the SQL engine, a string format is not supported. A numeric second argument can instead specify the maximum string length for databases that require it; it is ignored by the Dataiku DSS engine.
toString(5)returns"5"
toTitlecase(string s, [string delimiters]) string
Converts a string to titlecase. By default, words are separated by spaces, tabs, line breaks, and periods. Supply
delimitersto use a different set of word separators.
toTitlecase("hello world")returns"Hello World"
toTitlecase("hello-world", "-")returns"Hello-World"
toUppercase(string s) string
Converts a string to uppercase
toUppercase("hello world")returns"HELLO WORLD"
trim(string s) string
Returns copy of the string, with leading and trailing whitespace omitted.
trim(" Hello World ")returns"Hello World"
unescape(string s, string mode) string
Unescapes all escaped parts of the string depending on the given escaping mode. Available modes: ‘html’, ‘xml’, ‘csv’, ‘url’, ‘javascript’
unicode(string s) array of numbers
Returns the input string as an array of the unicode codepoints (numbers)
unicode("Hi!")returns[72,105,33]
unicodeType(string s) array of strings
Returns an array of strings describing each character of the input string in their full unicode notation
unicodeType("y 0H?")returns["lowercase letter","space separator","decimal digit number","uppercase letter","other punctuation"]
uuid() string
Returns a type 4 (pseudo randomly generated) UUID. The UUID is generated using a cryptographically strong pseudo random number generator.
uuid()returns7af14645-bcd9-4af2-bca8-390fc67e9a0b
Geometry functions¶
With the Dataiku DSS engine, geometry arguments accept WKT or GeoJSON strings. geoBuffer, geoEnvelope, and geoSimplify return WKT text.
geoBuffer(geometry geom, double distance, [int quadrantSegment]) string
Returns a geometry that represents all points whose distance from this geometry is less than or equal to
distance. Thedistanceparameter can be either a value or a column name from the dataset. The distance unit depends on the CRS of the given geometries (e.g., degrees for SRID=4326). A negative distance can be used with polygons, which will shrink the polygon rather than expand it. Specify an optionalquadrantSegmentvalue to set the number of segments to approximate a quarter circle (the default is 8)
geoContains(geometry geomA, geometry geomB) bool
Compute the boolean {geomA contains geomB} for two input geometries A and B.
The implementation of this function depends on the engine it is run on but its definition is standard: “Geometry A contains Geometry B if and only if no points of B lie in the exterior of A, and at least one point of the interior of B lies in the interior of A.” (https://postgis.net/docs/ST_Contains.html)
A polygon A does not contain a point B if B is exactly on its boundary.
geoContains("POLYGON((0 0,3 0,0 3,0 0))", "POINT(1 1)")returnstrue
geoContains("POLYGON((0 0,3 0,0 3,0 0))", geometry)returns the evaluation of the boolean predicate for each input geometry row of dataset column geometry
geoEnvelope(geometry geom) string
Returns the envelope of a geometry, i.e., the minimum bounding box containing this geometry.
geoMakeValid(geometry geom) string
Returns a valid representation of an invalid geometry. With the Dataiku DSS engine, it repairs polygons and multipolygons. Valid geometries remain unchanged.
Note
This function can return slightly different results whether the recipe engine is PostgreSQL or a local stream. If the method is unable to make a geometry valid, it returns an empty cell.
geoSimplify(geometry geom, double toleranceDistance) string
Returns a simpler geometry using a topology-preserving variant of the Douglas-Peucker algorithm, with respect to a non-negative
toleranceDistanceparameter. The simplification of the geometry will result in a geometry with fewer vertices. A vertex is removed only if the distance between this vertex and the edge resulting from removing this vertex is within the specifiedtoleranceDistance. ThetoleranceDistanceparameter can be either a value or a column name from the dataset. The toleranceDistance unit depends on the CRS of the given geometries (e.g., degrees for SRID=4326).
- geoDistance(geopoint geoPointA, geopoint geoPointB, string unit) number
Compute the distance between two geographical points. The available units are:
KILOMETERS,MILES.
Value access functions¶
numval(string columnName, [number offset]) number
Returns the value of a column converted to a decimal number. A missing or null value returns null. A value that cannot be parsed as a number returns an error. Specify an optional Offset argument to return the value of a previous row.
Note
Use numval when your column name contains spaces or periods to ensure Dataiku DSS parses it properly as in
numval("my.column")ornumval("my column"). Remember to always write the column name in “quotes” when using numval.
strval(string columnName, [string defaultValue], [number offset]) string
Returns the stored value of a column as a string, without numerical autotyping. This preserves leading zeros and also reads numerical column values as strings. A missing or null value returns
defaultValueif supplied, or an empty string otherwise. An empty string value is returned as-is; usecoalesce(strval("mycolumn"), "fallback")to replace it as well. Specify an optional Offset argument to return the value of a previous row.Note
Use strval when your column name contains spaces or periods to ensure Dataiku DSS parses it properly as in
strval("my.column")orstrval("my column"). Remember to always write the column name in “quotes” when using strval.
val(string columnName, [defaultValue], [number offset]) Output depends on argument
Returns the value of a column with numerical autotyping (use strval or numval to force a string or number). A missing, null, or empty string value returns
defaultValueif supplied, or null otherwise. The default can be a string, number, or another formula value. Specify an optional Offset argument to return the value of a previous row.Note
Use val when your column name contains spaces or periods to ensure Dataiku DSS parses it properly as in
val("my.column")orval("my column"). Remember to always write the column name in “quotes” when using val.
Offset argument¶
Note
With the Dataiku DSS engine, the offset argument is available in the Formula processor and in formula actions of the Create if, then, else statements processor in the Prepare recipe. It is not available in conditional predicates or the Filter and Flag formula processors.
Set offset to
1to retrieve a value from the previous rowSet offset to
2to retrieve a value from two rows before the current row…
Set offset to
0to retrieve a value from the current rowAn error is returned if offset is set to a negative value
For val and strval, the offset is the third argument. Include a default value before it, for example val("amount", 0, 1) or strval("status", "", 1). For numval, the offset is the second argument: numval("amount", 1).
Previous rows follow the order in which the recipe processes the data. Sort the input first if a specific order is required. When the previous row is unavailable, val and strval return their default and numval returns null. The default maximum offset is 1,000 rows, configurable by the Dataiku DSS administrator.
Control structures¶
Control structures allow you to perform advanced operations.
Beware: control structures cannot use object notation !
filter(array a, variable v, expression e) array
Evaluates expression a to an array. Then for each array element, binds its value to variable name v and evaluates expression e. Keeps the original element when e returns the boolean
true; strings and numbers do not keep an element.
filter(['aa', 'bb', 'cc', 'ab', 'bc'], item, item.startsWith('a'))returns["aa","ab"]Note
filter(myarray, v, v < 2)is equivalent to this Python syntax:[v for v in myarray if v < 2]and returns the array with only the elements below 2
forEach(array a, variable v, expression e) array
Evaluates expression a to an array. Then for each array element, binds its value to variable name v, evaluates expression e, and pushes the result onto the result array.
forEach(['aa', 'bb', 'cc', 'ab', 'bc'], item, item.startsWith('a'))returns[true,false,false,true,false]Note
forEach(myarray, v, v + 2)is equivalent to this Python syntax:[v+2 for v in myarray]
forEachIndex(array a, variable i, variable v, expression e) array
Evaluates expression a to an array. Then for each array element, binds its value to variable name v and its index to variable name i, evaluates expression e, and pushes the result onto the result array.
forEachIndex(['aa', 'bb', 'cc', 'ab', 'bc'], index, item, index < 2 || item.startsWith('a'))returns[true,true,false,true,false]Note
forEachIndex(myarray, i, v, v + i)is equivalent to this Javascript syntax:myarray.map(function(v, i) { return v+i ;})
forRange(from, to, step, variable v, expression e) array
Iterates, starting at
from, incrementing bystepeach time while less thanto(or while more thantoifstep < 0). At each iteration, binds the variablevto the iteration value, evaluates expressione, and pushes the result onto the result array.
stepmust be non-zero and point towardto. The default maximum number of iterations is 10,000, configurable by the Dataiku DSS administrator.
forRange(0, 21, 3, v, v)returns[0,3,6,9,12,15,18]forRange(0, 21, 3, v, v*5)returns[0,15,30,45,60,75,90]Note
forRange(0, 100, 3, v, v * 2)is equivalent to this Python syntax:[v * 2 for v in xrange(0, 100, 3)]and returns [0, 6, 12, 18 …. 198]
if(boolean, expression_true, expression_false) Output depends on expression
Evaluates to
expression_trueif the condition is true, toexpression_falseotherwise. Only the selected branch is evaluated. See Boolean values for accepted conditions.
if(3>2, "I heart #s", "my brain hurts")returns"I heart #s"
objectFilter(expression a, variable k, variable v, expression test) object
Evaluates expression a to an object. Then for each element (k, v) of this object, binds its key to variable name k, its value to variable name v, evaluates expression test which should return a boolean. If the boolean is true, pushes (k, v) onto the result object.
objectFilter('{"fName":"Joe", "lName":"Smith", "age":42 }', k, v, k.contains("Name"))returns{"fName":"Joe","lName":"Smith"}
switch(expression_to_match, match_1, return_1, match_2, return_2, …, [return_default]) Output depends on expression
Compares
expression_to_matchtomatch_1, then tomatch_2, etc. When match is found, returns the correspondingreturn_i. If no match is found, returnsreturn_default, or nothing if there is noreturn_default.
switch("Paris", "Paris", 1, "New York", 2, 0)returns1
switch("Berlin", "Paris", "1", "New York", "2", "Other")returns"Other"
switch(col1, col2+col3, "sum", "Other")returns"sum"if the value of column col1 equals the value of col2+col3,"Other"otherwise
switch("true", col1 >= 10, "good", col1 >= 5, "okay", "bad")returns"good","okay", or"bad"depending on col1’s value
with(expression o, variable v, expression e) Output depends on expression
Evaluates expression o and binds its value to variable name v. Then evaluates expression e and returns that result. The
withcontrol allows you to “split” a very big expression into more manageable chunks. It also makes reusing the result of a complex computation easier to read and faster to process.
with("european union".split(" "), a, a.length())returns2
with("european union".split(" "), a, forEach(a, v, v.length()))returns[8,5]
Tests¶
Beware: tests cannot use object notation
isBlank(expression o) boolean
Returns whether
ois null or an empty string. IsBlank() returns true when value is null or empty (“”). It does not consider a string made of blank spaces as blank; it considers an expression with an error as blank.
isBlank("")returnstrue
isBlank(" ")returnsfalse
isBlank(abs("a"))returnstrue
isError(expression o) boolean
Returns whether
ois an error
isError(abs("a"))returnstrue
isNonBlank(expression o) boolean
Returns whether
ois non-null or a not-empty string. IsNonBlank() returns false when value is null or empty (“”). It considers a string made of blank spaces as NonBlank; it considers an expression with an error as blank.
isNonBlank("")returnsfalse
isNonBlank(" ")returnstrue
isNonBlank(abs("a"))returnsfalse
isNotNull(expression o) boolean
Returns whether
ois not null. IsNotNull() returns false when value is null or empty (“”). It does not consider a string made of blank spaces as not null, and considers an expression with an error as not null.
isNotNull("")returnsfalse
isNotNull(" ")returnsfalse
isNotNull(abs("a"))returnstrue
isNull(expression o) boolean
Returns whether
ois null or an empty string. IsNull() returns true when value is null or empty (“”). It considers a string made of blank spaces as null, and does not consider an expression with an error as null.
isNull("")returnstrue
isNull(" ")returnstrue
isNull(abs("a"))returnsfalse
isNumeric(expression o) boolean
Returns true if
ois a number, a string containing only Unicode digits, or an empty string. Returns false for null. A numeric string with a sign or decimal point returns false unless it has already been converted to a number. To test whether arbitrary text can be converted, useisNonBlank(toNumber(o)).
isNumeric("123")returnstrue
isNumeric("-1.5")returnsfalse
isNumeric(-1.5)returnstrue