[id='ickle-query-syntax_{context}']
= Ickle query language syntax

The Ickle query language is subset of the link:https://en.wikipedia.org/wiki/Java_Persistence_Query_Language[JPQL] query language, with some extensions for full-text.

The parser syntax has some notable rules:

* Whitespace is not significant.
* Wildcards are not supported in field names.
* A field name or path must always be specified, as there is no default field.
* `&&` and `||` are accepted instead of `AND` or `OR` in both full-text and JPA predicates.
* `!` may be used instead of `NOT`.
* A missing boolean operator is interpreted as `OR`.
* String terms must be enclosed with either single or double quotes.
* Fuzziness and boosting are not accepted in arbitrary order; fuzziness always comes first.
* `!=` is accepted instead of `<>`.
* Boosting cannot be applied to `>`,`>=`,`<`,`\<=` operators. Ranges may be used to achieve the same result.

== Filtering operators

Ickle support many filtering operators that can be used for both indexed and non-indexed fields.

[%header%autowidth,cols="1,1,1",stripes=even]
|===
|Operator |Description |Example

|`in`
|Checks that the left operand is equal to one of the elements from the Collection of values given as argument.
|`FROM Book WHERE isbn IN ('ZZ', 'X1234')`

|`like`
|Checks that the left argument (which is expected to be a String) matches a wildcard pattern that follows the JPA rules.
|`FROM Book WHERE title LIKE '%Java%'`

|`=`
|Checks that the left argument is an exact match of the given value.
|`FROM Book WHERE name = 'Programming Java'`

|`!=`
|Checks that the left argument is different from the given value.
|`FROM Book WHERE language != 'English'`

|`>`
|Checks that the left argument is greater than the given value.
|`FROM Book WHERE price > 20`

|`>=`
|Checks that the left argument is greater than or equal to the given value.
|`FROM Book WHERE price >= 20`

|`<`
|Checks that the left argument is less than the given value.
|`FROM Book WHERE year < 2020`

|`\<=`
|Checks that the left argument is less than or equal to the given value.
|`FROM Book WHERE price  <= 50`

|`between`
|Checks that the left argument is between the given range limits.
|`FROM Book WHERE price BETWEEN 50 AND 100`
|===

== Boolean conditions

Combining multiple attribute conditions with logical conjunction (`and`) and disjunction (`or`) operators in order to
create more complex conditions is demonstrated in the following example. The well known operator precedence rule for
boolean operators applies here, so the order of the operators is irrelevant. Here `and`
operator still has higher priority than `or` even though `or` was invoked first.

[source,sql]
----
# match all books that have "Data Grid" in their title
# or have an author named "Manik" and their description contains "clustering"

FROM org.infinispan.sample.Book WHERE title LIKE '%Data Grid%' OR author.name = 'Manik' AND description like '%clustering%'
----

Boolean negation has highest precedence among logical operators and applies only to the next simple attribute condition.

[source,sql]
----
# match all books that do not have "Data Grid" in their title and are authored by "Manik"
FROM org.infinispan.sample.Book WHERE title != 'Data Grid' AND author.name = 'Manik'
----

== Nested conditions
Changing the precedence of logical operators is achieved with parenthesis:

[source,sql]
----
# match all books that have an author named "Manik" and their title contains
# "Data Grid" or their description contains "clustering"
FROM org.infinispan.sample.Book WHERE author.name = 'Manik' AND ( title like '%Data Grid%' OR description like '% clustering%')
----

== Projections with SELECT statements
In some use cases returning the whole domain object is overkill if only a small subset of the attributes are actually
used by the application, especially if the domain entity has embedded entities. The query language allows you to specify
a subset of attributes (or attribute paths) to return - the projection. If projections are used then the `QueryResult.list()`
will not return the whole domain entity but will return a `List` of `Object[]`, each slot in the array corresponding to
a projected attribute.

[source,sql]
----
# match all books that have "Data Grid" in their title or description
# and return only their title and publication year
SELECT title, publicationYear FROM org.infinispan.sample.Book WHERE title like '%Data Grid%' OR description like '%Data Grid%'
----

=== Project cache entry version
It is possible to project the cache entry version, using the `version` projection function.

[source,sql]
----
# return the title, publication year and the cache entry version
SELECT b.title, b.publicationYear, version(b) FROM org.infinispan.sample.Book b WHERE b.title like '%Data Grid%'
----

=== Project cache entry value
It is possible to project the cache entry value together with other projections.
It can be used for instance to project the cache entry value together with the cache entry version
in the same `Object[]` returned hit.

[source,sql]
----
# return the cache entry value and the cache entry version
SELECT b, version(b) FROM org.infinispan.sample.Book b WHERE b.title like '%Data Grid%'
----

[id='score_projection']
=== Project the score
If the query is indexed, it is possible to project the score obtained by each matching together with other projections.
It can be used for instance to project the cache entry value together with the score
in the same `Object[]` returned hit.

[source,sql]
----
# return the cache entry value and the the score of the matching
SELECT b, score(b) FROM org.infinispan.sample.Book b WHERE b.title like '%Data Grid%'
----

[discrete]
== Sorting
Ordering the results based on one or more attributes or attribute paths is done with the `ORDER BY` clause. If multiple sorting criteria
are specified, then the order will dictate their precedence.

[source,sql]
----
# match all books that have "Data Grid" in their title or description
# and return them sorted by the publication year and title
FROM org.infinispan.sample.Book WHERE title like '%Data Grid%' ORDER BY publicationYear DESC, title ASC
----

== Grouping and aggregation

{brandname} has the ability to group query results according to a set of grouping fields and construct aggregations of
the results from each group by applying an aggregation function to the set of values that fall into each group.
Grouping and aggregation can only be applied to projection queries (queries with one or more field in the SELECT clause).

The supported aggregations are: `avg`, `sum`, `count`, `max`, and `min`.

The set of grouping fields is specified with the `GROUP BY` clause and the order used for defining grouping fields is
not relevant. All fields selected in the projection must either be grouping fields
or else they must be aggregated using one of the grouping functions described below. A projection field can be
aggregated and used for grouping at the same time. A query that selects only grouping fields but no aggregation fields
is legal.
⁠
Example: Grouping Books by author and counting them.
[source,sql]
----
SELECT author, COUNT(title) FROM org.infinispan.sample.Book WHERE title LIKE '%engine%' GROUP BY author
----

[NOTE]
====
A projection query in which all selected fields have an aggregation function applied and no fields are used for
grouping is allowed. In this case the aggregations will be computed globally as if there was a single global group.
====

[discrete]
=== Aggregations
You can apply the following aggregation functions to a field:

.Index merge attributes
[%header,cols=2*]
|===
|Aggregation function
|Description

|`avg()`
| Computes the average of a set of numbers. Accepted values are primitive numbers and instances of `java.lang.Number`. The result is represented as `java.lang.Double`. If there are no non-null values the result is `null` instead.

|`count()`
|Counts the number of non-null rows and returns a `java.lang.Long`. If there are no non-null values the result is `0` instead.

|`max()`
|Returns the greatest value found. Accepted values must be instances of `java.lang.Comparable`. If there are no non-null values the result is `null` instead.

|`min()`
|Returns the smallest value found. Accepted values must be instances of `java.lang.Comparable`. If there are no non-null values the result is `null` instead.

|`sum()`
|Computes the sum of a set of Numbers. If there are no non-null values the result is `null` instead. The following table indicates the return type based on the specified field.

|===

.Table sum return type
|===
|Field Type |Return Type

|Integral (other than BigInteger)
|Long

|Float or Double
|Double

|BigInteger
|BigInteger

|BigDecimal
|BigDecimal
|===

[discrete]
=== Evaluation of queries with grouping and aggregation

Aggregation queries can include filtering conditions, like usual queries. Filtering can be performed in two stages: before
and after the grouping operation. All filter conditions defined before invoking the `groupBy()` method will be applied
before the grouping operation is performed, directly to the cache entries (not to the final projection). These filter
conditions can reference any fields of the queried entity type, and are meant to restrict the data set that is going to
be the input for the grouping stage. All filter conditions defined after invoking the `groupBy()` method will be applied to
the projection that results from the projection and grouping operation. These filter conditions can either reference any
of the `groupBy()` fields or aggregated fields. Referencing aggregated fields that are not specified in the select clause
is allowed; however, referencing non-aggregated and non-grouping fields is forbidden. Filtering in this phase will
reduce the amount of groups based on their properties. Sorting can also be specified similar to usual queries. The
ordering operation is performed after the grouping operation and can reference any of the `groupBy()` fields or aggregated
fields.

== DELETE statements

You can delete entities from {brandname} caches with the following syntax:

[source,sql,tile="Delete Query in Ickle"]
----
DELETE FROM <entityName> [WHERE condition]
----

* Reference only single entities with `<entityName>`. DELETE queries cannot use joins.
* WHERE conditions are optional.

DELETE queries cannot use any of the following:

* Projections with SELECT statements
* Grouping and aggregation
* ORDER BY clauses

[TIP]
====
Invoke the `Query.executeStatement()` method to execute DELETE statements.
====

[role="_additional-resources"]
.Additional resources
* link:{javadocroot}/org/infinispan/query/dsl/Query.html#executeStatement()[org.infinispan.query.dsl.Query.executeStatement()]
