Query Expressions
Query expressions are used to retrieve and aggregate values from collections. Their semantics is essentially the same as SQL query semantics. A query expression evaluates to a collection, object, string, number, boolean value, or null.
Each query expression has the following form:
select <select-specification>
from <identifier> [as] <simple-identifier>
[where <boolean-expr>]
[group by <arithmetic-expr>[, <arithmetic-expr>]*]
[order by <arithmetic-expr> [asc | desc][, <arithmetic-expr> [asc | desc]]*]
The syntactic category select-expression is defined below in the section
Select clause. The other syntactic categories referenced above are defined elsewhere in
Expressions or in
Identifiers and Literals.
Expressions of this form may have the following clauses:
• select
• from
• where
• group by
• order by
For more information, see the following sections:
See also
Query in the CSL Language Overview section.
Select clause
The select-specification in the select clause has one of the following forms:
• all [{* | <identifier>}], where * here is not a metacharacter but rather a character that can occur literally in a query expression, as in select all * from collection x.
• distinct [<identifier>]
• <simple-identifier>(<identifier>), where the simple-identifier names one of the following query aggregate functions:
o first: returns the first element of the intermediate query results. Returns null if there are no intermediate results.
o last: returns the last element of the intermediate query results. Returns null if there are no intermediate results.
o only: returns the only element of the intermediate query results. Use this (instead of first or last) when you know that the intermediate results include only a single element. Signals an error (InvaidResultException) if there are multiple intermediate results. Returns null if there are no intermediate results.
o sum: returns the sum of the values in the intermediate query results. Returns null if there are no intermediate results.
o min: returns the smallest value in the intermediate query results. Returns null if there are no intermediate results.
o max: returns the largest value in the intermediate query results. Returns null if there are no intermediate results.
o count: returns the number of elements in the intermediate query results. Returns 0 if there are no intermediate results.
o exists: returns a boolean value indicating whether the select expression found any matches.
Queries that use the first two forms always return either a collection or
null. They typically return
null if the result set is empty (as opposed to returning an empty collection). If you need to check a query result to see if it is empty, use the predefined function
isEmpty(collection), which returns
true if its argument is either
null or an empty collection.
Note the following form (a hybrid of the second and third forms) does not provoke an error, but nevertheless should not be used:
distinct <simple-identifier>(<identifier>)
For example, select distinct min(n) from numbers n does not cause an error, but its use constitutes poor coding practice as this use of distinct has no effect.
From clause
The identifier in the from clause must evaluate to the collection to be queried. Note that tables in CSL are represented as collections whose elements are objects with one field for each table column.
The
simple-identifier in the
from clause is an alias for a single element in the collection being queried. It must match the
simple-identifier that forms all or part of the
identifier in the
Select clause (if there is one). It can occur in the following components of the
from clause:
• boolean-expression in the where clause
• arithmetic-expressions in the group by clause
• arithmetic-expressions in the order by clause
This alias can also occur in the right-hand side of formulas whose evaluation is necessitated by evaluation of expressions in the query. See, for example, the section
Example in
Machine Selection.
Examples
numManualMigWelds = _
select count(w) from welds w _
where w.type != 'Spot' and not w.isRobotic
componentMass = _
select sum(c.weight) from part.subcomponents c
finishMass = _
select sum(w.weldWeight) from childOps w _
where isWeld(w)
weldWeight = _
select sum(op.weldWeight) from childOps op
numCostableWelds = _
select count(a) from part.childArtifacts a _
where isWeld(a) and a.costable
computedClampTime = _
select first(entry.loadTime) from xtraLoadTime entry _
where entry.weight >= gcd.weight _
order by entry.loadTime // time (secs) for load part
coreboxCostVoids =
select sum(operation.hardToolingCost) from childOps operation _
where operation.artifactType.name == 'Void' _
group by _
adaptiveRound(operation.volume, _
voxelVolumeError(voidCoreArea(operation))), _
adaptiveRound(operation.boxLength, voxelLengthError), _
adaptiveRound(operation.boxWidth,voxelLengthError), _
adaptiveRound(operation.boxHeight,voxelLengthError)
Deprecated Form
A deprecated form of the query expression uses just select instead of select all. This form might occur in older code of existing cost models. Its behavior is the same as the select all form, with the following exception: if an expression using select all evaluates to a collection that has only a single element, the corresponding select expression evaluates to the element itself rather than a collection with only that element.
For example, suppose the following evaluates to a one-element collection that contains only the current part’s Blank GCD:
select all (g) from part.childArtifacts g where isBlank(g)
In this case, the following evaluates to the Blank GCD itself (rather than a collection containing it):
select g from part.childArtifacts g where isBlank(g)
Do not use the deprecated form. Use select all or, if you know that the results include only a single element, use select together with the aggregation function only(), as in the following:
select only(g) from part.childArtifacts g where isBlank(g)
This results in more robust and readable code.