histogram
Calculates an adaptive histogram. It does not guarantee precise results.values — Expression resulting in input values.
Parameters
number_of_bins — Upper limit for the number of bins in the histogram. The function automatically calculates the number of bins. It tries to reach the specified number of bins, but if it fails, it uses fewer bins.
Returned values
-
Array of Tuples of the following format:
lower— Lower bound of the bin.upper— Upper bound of the bin.height— Calculated height of the bin.
sequenceMatch
Checks whether the sequence contains an event chain that matches the pattern. SyntaxEvents that occur at the same second may lay in the sequence in an undefined order affecting the result.
-
timestamp— Column considered to contain time data. Typical data types areDateandDateTime. You can also use any of the supported UInt data types. -
cond1,cond2— Conditions that describe the chain of events. Data type:UInt8. You can pass up to 32 condition arguments. The function takes only the events described in these conditions into account. If the sequence contains data that isn’t described in a condition, the function skips them.
pattern— Pattern string. See Pattern syntax.
- 1, if the pattern is matched.
- 0, if the pattern isn’t matched.
UInt8.
Pattern syntax
-
(?N)— Matches the condition argument at positionN. Conditions are numbered in the[1, 32]range. For example,(?1)matches the argument passed to thecond1parameter. -
.*— Matches any number of events. You do not need conditional arguments to match this element of the pattern. -
(?t operator value)— Sets the time in seconds that should separate two events. For example, pattern(?1)(?t>1800)(?2)matches events that occur more than 1800 seconds from each other. An arbitrary number of any events can lay between these events. You can use the>=,>,<,<=,==operators.
t table:
sequenceCount
Counts the number of event chains that matched the pattern. The function searches event chains that do not overlap. It starts to search for the next chain after the current chain is matched.Events that occur at the same second may lay in the sequence in an undefined order affecting the result.
-
timestamp— Column considered to contain time data. Typical data types areDateandDateTime. You can also use any of the supported UInt data types. -
cond1,cond2— Conditions that describe the chain of events. Data type:UInt8. You can pass up to 32 condition arguments. The function takes only the events described in these conditions into account. If the sequence contains data that isn’t described in a condition, the function skips them.
pattern— Pattern string. See Pattern syntax.
- Number of non-overlapping event chains that are matched.
UInt64.
Example
Consider data in the t table:
sequenceMatchEvents
Return event timestamps of longest event chains that matched the pattern.Events that occur at the same second may lay in the sequence in an undefined order affecting the result.
-
timestamp— Column considered to contain time data. Typical data types areDateandDateTime. You can also use any of the supported UInt data types. -
cond1,cond2— Conditions that describe the chain of events. Data type:UInt8. You can pass up to 32 condition arguments. The function takes only the events described in these conditions into account. If the sequence contains data that isn’t described in a condition, the function skips them.
pattern— Pattern string. See Pattern syntax.
- Array of timestamps for matched condition arguments (?N) from event chain. Position in array match position of condition argument in pattern
t table:
windowFunnel
Searches for event chains in a sliding time window and calculates the maximum number of events that occurred from the chain. The function works according to the algorithm:- The function searches for data that triggers the first condition in the chain and sets the event counter to 1. This is the moment when the sliding window starts.
- If events from the chain occur sequentially within the window, the counter is incremented. If the sequence of events is disrupted, the counter isn’t incremented.
- If the data has multiple event chains at varying points of completion, the function will only output the size of the longest chain.
timestamp— Name of the column containing the timestamp. Data types supported: Date, DateTime and other unsigned integer types (note that even though timestamp supports theUInt64type, it’s value can’t exceed the Int64 maximum, which is 2^63 - 1).cond— Conditions or data describing the chain of events. UInt8.
window— Length of the sliding window, it is the time interval between the first and the last condition. The unit ofwindowdepends on thetimestampitself and varies. Determined using the expressiontimestamp of cond1 <= timestamp of cond2 <= ... <= timestamp of condN <= timestamp of cond1 + window.mode— It is an optional argument. One or more modes can be set.'strict_deduplication'— If the same condition holds for the sequence of events, then such repeating event interrupts further processing. Note: it may work unexpectedly if several conditions hold for the same event.'strict_order'— Don’t allow interventions of other events. E.g. in the case ofA->B->D->C, it stops findingA->B->Cat theDand the max event level is 2.'strict_increase'— Apply conditions only to events with strictly increasing timestamps.'strict_once'— Count each event only once in the chain even if it meets the condition several times.'allow_reentry'— Ignore events that violate the strict order. E.g. in the case of A->A->B->C, it finds A->B->C by ignoring the redundant A and the max event level is 3.
Integer.
Example
Determine if a set period of time is enough for the user to select a phone and purchase it twice in the online store.
Set the following chain of events:
- The user logged in to their account on the store (
eventID = 1003). - The user searches for a phone (
eventID = 1007, product = 'phone'). - The user placed an order (
eventID = 1009). - The user made the order again (
eventID = 1010).
user_id could get through the chain in a period in January-February of 2019.
Query
Response
allow_reentry mode works with user reentry patterns:
retention
The function takes as arguments a set of conditions from 1 to 32 arguments of typeUInt8 that indicate whether a certain condition was met for the event.
Any condition can be specified as an argument (as in WHERE).
The conditions, except the first, apply in pairs: the result of the second will be true if the first and second are true, of the third if the first and third are true, etc.
Syntax
cond— An expression that returns aUInt8result (1 or 0).
- 1 — Condition was met for the event.
- 0 — Condition wasn’t met for the event.
UInt8.
Example
Let’s consider an example of calculating the retention function to determine site traffic.
1. Create a table to illustrate an example.
Query
Query
Response
uid using the retention function.
Query
Response
Query
Response
r1- the number of unique visitors who visited the site during 2020-01-01 (thecond1condition).r2- the number of unique visitors who visited the site during a specific time period between 2020-01-01 and 2020-01-02 (cond1andcond2conditions).r3- the number of unique visitors who visited the site during a specific time period on 2020-01-01 and 2020-01-03 (cond1andcond3conditions).
uniqUpTo(N)(x)
Calculates the number of different values of the argument up to a specified limit,N. If the number of different argument values is greater than N, this function returns N + 1, otherwise it calculates the exact value.
Recommended for use with small Ns, up to 10. The maximum value of N is 100.
For the state of an aggregate function, this function uses the amount of memory equal to 1 + N * the size of one value of bytes.
When dealing with strings, this function stores a non-cryptographic hash of 8 bytes; the calculation is approximated for strings.
For example, if you had a table that logs every search query made by users on your website. Each row in the table represents a single search query, with columns for the user ID, the search query, and the timestamp of the query. You can use uniqUpTo to generate a report that shows only the keywords that produced at least 5 unique users.
uniqUpTo(4)(UserID) calculates the number of unique UserID values for each SearchPhrase, but it only counts up to 4 unique values. If there are more than 4 unique UserID values for a SearchPhrase, the function returns 5 (4 + 1). The HAVING clause then filters out the SearchPhrase values for which the number of unique UserID values is less than 5. This will give you a list of search keywords that were used by at least 5 unique users.
sumMapFiltered
This function behaves the same as sumMap except that it also accepts an array of keys to filter with as a parameter. This can be especially useful when working with a high cardinality of keys. SyntaxsumMapFiltered(keys_to_keep)(keys, values)
Parameters
Returned Value
- Returns a tuple of two arrays: keys in sorted order, and values summed for the corresponding keys.
Query
Query
Response
sumMapFilteredWithOverflow
This function behaves the same as sumMap except that it also accepts an array of keys to filter with as a parameter. This can be especially useful when working with a high cardinality of keys. It differs from the sumMapFiltered function in that it does summation with overflow - i.e. returns the same data type for the summation as the argument data type. SyntaxsumMapFilteredWithOverflow(keys_to_keep)(keys, values)
Parameters
Returned Value
- Returns a tuple of two arrays: keys in sorted order, and values summed for the corresponding keys.
sum_map, insert some data into it and then use both sumMapFilteredWithOverflow and sumMapFiltered and the toTypeName function for comparison of the result. Where requests was of type UInt8 in the created table, sumMapFiltered has promoted the type of the summed values to UInt64 to avoid overflow whereas sumMapFilteredWithOverflow has kept the type as UInt8 which is not large enough to store the result - i.e. overflow has occurred.
Query
Query
Query
Response
Response
sequenceNextNode
Returns a value of the next event that matched an event chain. Experimental function,SET allow_experimental_funnel_functions = 1 to enable it.
Syntax
-
direction— Used to navigate to directions.- forward — Moving forward.
- backward — Moving backward.
-
base— Used to set the base point.- head — Set the base point to the first event.
- tail — Set the base point to the last event.
- first_match — Set the base point to the first matched
event1. - last_match — Set the base point to the last matched
event1.
timestamp— Name of the column containing the timestamp. Data types supported: Date, DateTime and other unsigned integer types.event_column— Name of the column containing the value of the next event to be returned. Data types supported: String and Nullable(String).base_condition— Condition that the base point must fulfill.event1,event2, … — Conditions describing the chain of events. UInt8.
event_column[next_index]— If the pattern is matched and next value exists.NULL- If the pattern isn’t matched or next value doesn’t exist.
Query
Response
forward and head
backward and tail
forward and first_match
backward and last_match
base_condition