Repository navigation
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
The Concourse epic cinchapi/concourse#1417 adds new calculations, which include
new functions, calculations grouped by the values of keys or by ranges of
numbers or time, and several calculations in one statement. Concourse builds the
command for a calculate statement from the
CalculateSymbolthat CCL parses thestatement into, so CCL has to parse each new form before Concourse can run it.
Before this change, a calculate statement applied one function to one key, and
that function had to be
sum,avg,average,count,minormax. Thestatement could not group its records. Its
CalculateSymbolalso had noequals,hashCodeor text, so a caller could neither compare two parses of astatement nor turn a symbol back into CCL.
A calculation's key can be a function key, such as
age | avg, so the hash codeof a
CalculateSymboldepends on the hash code of a function. Before thischange, two equal functions had different hash codes, so a
FunctionKeySymbol,a
FunctionValueSymbol, or a tree that held one could fail to match an equalone in a hash set or as a hash map key.
Closes #89
Change
The calculate statement takes the new functions and an optional
group byclause, and a grouped statement can list several calculations separated by
commas. A
CalculateSymbolexposes each calculation as aCalculationSymbolthrough
calculations(), and the group clause as aGroupSymbolthroughgroup(). TheGroupSymbolholds the group keys, the range, thehavingclause as a
HavingSymbol, the order of the groups as aResultOrderSymbol,and the page. For example, a caller reads a grouped statement like this.
The order and page of a grouped statement belong to its
GroupSymbolratherthan to the tree's
OrderTreeandPageTree, because they order and pagegroups rather than records. For the same reason, a grouped statement orders its
groups by the result of a calculation, so its
order bynames a functionrather than a key.
The existing constructors and accessors of
CalculateSymbolkeep their meaning.An ungrouped statement has one calculation, so a caller that reads
function(),key(),records()andtimestamp()gets the same values as before. Astatement that holds the text
group byin an unquoted value now fails toparse, because the grammar reserves that text.
A caller can also build a symbol from its parts, and the text of a
CalculateSymbolis a statement that parses back to an equal symbol.JavaCC generates
Grammar.java,GrammarConstants.javaandGrammarTokenManager.javafromgrammar/grammar.jjt, so the grammar changeitself is in
grammar.jjt. The diff ofFunctionmoves its members into sortedorder, and its hash code is the one part whose behavior changes.
Semantics
Statements
countDistinct,median,percentile,stddev,stddevPopulation,varianceandvariancePopulation, and every functionname parses in any letter case.
percentiletakes a fraction from 0 to 1 before its key, as incalculate percentile 0.99 latency, and no other function takes a fraction.A percentile without a fraction, or with one outside that range, is a syntax
error.
group byclause can list several calculations oruse
having,order by,limitoroffset.group byclause comes after the scope (records or awherecondition)and the timestamp. An
atright after a comparison belongs to thatcomparison, as it does for
find, so a statement with a condition sets thetimestamp of its calculation with
as of, or withatafter a parenthesizedcondition.
group byis reserved in any letter case, even when a longer wordstarts with
by, as ingroup bypass. Sofind name = rock group by seaisa syntax error, and a value that holds the text must be quoted, as in
name = "rock group by sea". The wordsperandhavingare not reserved,so a key may have either name.
Groups
group byclause names one or more group keys, which may be navigationkeys. Its optional parts follow the keys in this order.
per <number>groups the values of the last key into number ranges of thatwidth, and
per "<duration>"groups them into time ranges of that period.A duration has the form of a
withinduration, such as"1 day".between <a> and <b>followsperand asks for every range between thebounds, including an empty one. The bounds are numbers for number ranges
and timestamps for time ranges.
having <function> <operator> <value>keeps the groups whose resultsatisfies the comparison, as in
having count >= 2.order by <function> [asc|desc]orders the groups by a result, ascendingby default.
limitandoffsetpage the groups.havingandorder bynames one of the statement'scalculations in any letter case, and
avgandaveragename the samefunction. A function that names none of the calculations, or more than one,
is a syntax error, and so is an
order bythat names a group key.betweenmustcome before the second. A statement that breaks either rule is a syntax
error.
atclause does, so a datewithout a time takes the current time of day. The start of a day needs an
explicit time, such as
"2026-01-01 00:00:00".Symbols
calculations()returns the calculations in the order the statement liststhem, and the
function()andkey()of aCalculateSymbolare those of thefirst.
function()returns a canonical name. It is camelCase for a new function,such as
countDistinct, because that is the name of the Concourse calculationit asks for, and it stays lower case for
sum,avg,average,count,minandmax.fraction()is the fraction of apercentileandnullforany other function.
group()isnullfor an ungrouped statement, and each part of a group thatthe statement leaves out is
null. A whole number in a range is anIntegeror a
Long, a decimal is aDouble,period()is ajava.time.Duration,and
start()andend()areTimestampSymbols.calculation()of aHavingSymbolor aResultOrderSymbolis theelement of
calculations()that its function names.CalculateSymbol,CalculationSymbol,GroupSymbol,HavingSymbolandResultOrderSymbolimplementequalsandhashCode, and numbers in a groupare equal when they have the same value, such as
10and10.0. The text ofa
CalculateSymbolis its statement without the condition, which stays achild of the
CommandTree, and that text parses to an equal symbol.constructor or a builder method throws
IllegalArgumentExceptionfor aninvalid part, and
GroupSymbol.Builder#build()throwsIllegalStateExceptionfor a range whose parts do not fit together, such asboth a width and a period.
Compiler#analyzereports the key of every calculation and every group key.FunctionKeySymbol, aFunctionValueSymbol, or a tree that holds one matches an equal one in a hashset or as a hash map key.