SQL - Functions

Bundled functions

Functions by category

Graph Math Collections Misc
out() eval() set() date()
in() min() map() sysdate()
both() max() list() format()
outE() sum() difference() distance()
inE() abs() first() ifnull()
bothE() intersect() coalesce()
outV() avg() distinct() uuid()
inV() count() expand() if()
traversedElement() mode() unionall()
traversedVertex() median() flatten()
traversedEdge() percentile() last()
shortestPath() variance() symmetricDifference()
dijkstra() stddev()

Functions by name

abs() avg() both() bothE()
coalesce() count() date() difference()
dijkstra() distance() distinct() eval()
expand() format() first() flatten()
if() ifnull() in() inE()
inV() intersect() list() map()
min() max() median() mode()
out() outE() outV() percentile()
set() shortestPath() stddev() sum()
symmetricDifference() sysdate() traversedElement() traversedEdge()
traversedVertex() unionall() uuid() variance()

SQL Functions are all the functions bundled with OrientDB SQL engine. You can create your own Database Functions in any language supported by JVM. Look also to SQL Methods.

SQL Functions can work in 2 ways based on the fact that they can receive 1 or more parameters:

Aggregated mode

When only one parameter is passed, the function aggregates the result in only one record. The classic example is the sum() function:

  1. SELECT SUM(salary) FROM employee

This will always return 1 record with the sum of salary field.

Inline mode

When two or more parameters are passed:

  1. SELECT SUM(salary, extra, benefits) AS total FROM employee

This will return the sum of the field “salary”, “extra” and “benefits” as “total”.

In case you need to use a function inline, when you only have one parameter, then add “null” as the second parameter:

  1. SELECT first( out('friends').name, null ) as firstFriend FROM Profiles

In the above example, the first() function doesn’t aggregate everything in only one record, but rather returns one record per Profile, where the firstFriend is the first item of the collection received as the parameter.

Function Reference

out()

Get the adjacent outgoing vertices starting from the current record as Vertex.

Syntax: out([<label-1>][,<label-n>]*)

Available since: 1.4.0

Example

Get all the outgoing vertices from all the Vehicle vertices:

  1. SELECT out() FROM V

Get all the incoming vertices connected with edges with label (class) “Eats” and “Favorited” from all the Restaurant vertices in Rome:

  1. SELECT out('Eats','Favorited') FROM Restaurant WHERE city = 'Rome'

in()

Get the adjacent incoming vertices starting from the current record as Vertex.

Syntax:

  1. in([<label-1>][,<label-n>]*)

Available since: 1.4.0

Example

Get all the incoming vertices from all the Vehicle vertices:

  1. SELECT in() FROM V

Get all the incoming vertices connected with edges with label (class) “Friend” and “Brother”:

  1. SELECT in('Friend','Brother') FROM V

both()

Get the adjacent outgoing and incoming vertices starting from the current record as Vertex.

Syntax:

  1. both([<label1>][,<label-n>]*)

Available since: 1.4.0

Example

Get all the incoming and outgoing vertices from vertex with rid #13:33:

  1. SELECT both() FROM #13:33

Get all the incoming and outgoing vertices connected with edges with label (class) “Friend” and “Brother”:

  1. SELECT both('Friend','Brother') FROM V

outE()

Get the adjacent outgoing edges starting from the current record as Vertex.

Syntax:

  1. outE([<label1>][,<label-n>]*)

Available since: 1.4.0

Example

Get all the outgoing edges from all the vertices:

  1. SELECT outE() FROM V

Get all the outgoing edges of type “Eats” from all the SocialNetworkProfile vertices:

  1. SELECT outE('Eats') FROM SocialNetworkProfile

inE()

Get the adjacent incoming edges starting from the current record as Vertex.

Syntax:

  1. inE([<label1>][,<label-n>]*)

Example

Get all the incoming edges from all the vertices:

  1. SELECT inE() FROM V

Get all the incoming edges of type “Eats” from the Restaurant ‘Bella Napoli’:

  1. SELECT inE('Eats') FROM Restaurant WHERE name = 'Bella Napoli'

bothE()

Get the adjacent outgoing and incoming edges starting from the current record as Vertex.

Syntax: bothE([<label1>][,<label-n>]*)

Available since: 1.4.0

Example

Get both incoming and outgoing edges from all the vertices:

  1. SELECT bothE() FROM V

Get all the incoming and outgoing edges of type “Friend” from the Profile with nick ‘Jay’

  1. SELECT bothE('Friend') FROM Profile WHERE nick = 'Jay'

outV()

Get outgoing vertices starting from the current record as Edge.

Syntax:

  1. outV()

Available since: 1.4.0

Example

  1. SELECT outV() FROM E

inV()

Get incoming vertices starting from the current record as Edge.

Syntax:

  1. inV()

Available since: 1.4.0

Example

  1. SELECT inV() FROM E

eval()

Syntax: eval('<expression>')

Evaluates the expression between quotes (or double quotes).

Available since: 1.4.0

Example

  1. SELECT eval('price * 120 / 100 - discount') AS finalPrice FROM Order

coalesce()

Returns the first field/value not null parameter. If no field/value is not null, returns null.

Syntax:

  1. coalesce(<field|value> [, <field-n|value-n>]*)

Available since: 1.3.0

Example

  1. SELECT coalesce(amount, amount2, amount3) FROM Account

if()

Syntax:

  1. if(<expression>, <result-if-true>, <result-if-false>)

Evaluates a condition (first parameters) and returns the second parameter if the condition is true, the third one otherwise

Example:

  1. SELECT if(eval("name = 'John'"), "My name is John", "My name is not John") FROM Person

ifnull()

Returns the passed field/value (or optional parameter return_value_if_not_null). If field/value is not null, otherwise it returns return_value_if_null.

Syntax:

  1. ifnull(&lt;field&#124;value&gt;, &lt;return_value_if_null&gt; [,&lt;return_value_if_not_null&gt;](,&lt;field&.md#124;value&gt;]*)

Available since: 1.3.0

Example

  1. SELECT ifnull(salary, 0) FROM Account

expand()

Available since: 1.4.0

This function has two meanings:

  • When used on a collection field, it unwinds the collection in the field and use it as result.
  • When used on a link (RID) field, it expands the document pointed by that link.

Syntax: expand(<field>)

Since version 2.1 the preferred operator to unwind collections is UNWIND. Expand usage for this use case will probably be deprecated in next releases

Example

on collectinos:

  1. SELECT EXPAND( addresses ) FROM Account.

on RIDs

  1. SELECT EXPAND( addresses ) FROM Account.

This replaces the flatten() now deprecated


flatten()

Deprecated, use the EXPAND() instead.

Extracts the collection in the field and use it as result.

Syntax:

  1. flatten(<field>)

Available since: 1.0rc1

Example

  1. SELECT flatten( addresses ) FROM Account

first()

Retrieves only the first item of multi-value fields (arrays, collections and maps). For non multi-value types just returns the value.

Syntax: first(<field>)

Available since: 1.2.0

Example

  1. select first( addresses ) from Account

last()

Retrieves only the last item of multi-value fields (arrays, collections and maps). For non multi-value types just returns the value.

Syntax: last(<field>)

Available since: 1.2.0

Example

  1. SELECT last( addresses ) FROM Account

count()

Counts the records that match the query condition. If * is not used as a field, then the record will be counted only if the field content is not null.

Syntax: count(<field>)

Available since: 0.9.25

Example

  1. SELECT COUNT(*) FROM Account

min()

Returns the minimum value. If invoked with more than one parameters, the function doesn’t aggregate, but returns the minimum value between all the arguments.

Syntax: min(<field> [, <field-n>]* )

Available since: 0.9.25

Example

Returns the minimum salary of all the Account records:

  1. SELECT min(salary) FROM Account

Returns the minimum value between ‘salary1’, ‘salary2’ and ‘salary3’ fields.

  1. SELECT min(salary1, salary2, salary3) FROM Account

max()

Returns the maximum value. If invoked with more than one parameters, the function doesn’t aggregate, but returns the maximum value between all the arguments.

Syntax: max(<field> [, <field-n>]* )

Available since: 0.9.25

Example

Returns the maximum salary of all the Account records:

  1. SELECT max(salary) FROM Account.

Returns the maximum value between ‘salary1’, ‘salary2’ and ‘salary3’ fields.

  1. SELECT max(salary1, salary2, salary3) FROM Account

abs()

Returns the absolute value. It works with Integer, Long, Short, Double, Float, BigInteger, BigDecimal, null.

Syntax: abs(<field>)

Available since: 2.2

Example

  1. SELECT abs(score) FROM Account
  2. SELECT abs(-2332) FROM Account
  3. SELECT abs(999) FROM Account

avg()

Returns the average value.

Syntax: avg(<field>)

Available since: 0.9.25

Example

  1. SELECT avg(salary) FROM Account

sum()

Syntax: sum(<field>)

Returns the sum of all the values returned.

Available since: 0.9.25

Example

  1. SELECT sum(salary) FROM Account

date()

Returns a date formatting a string. <date-as-string> is the date in string format, and <format> is the date format following these rules. If no format is specified, then the default database format is used. To know more about it, look at Managing Dates.

Syntax: date( <date-as-string> [<format>] [,<timezone>] )

Available since: 0.9.25

Example

  1. SELECT FROM Account WHERE created <= date('2012-07-02', 'yyyy-MM-dd')

sysdate()

Returns the current date time. To know more about it, look at Managing Dates.

Syntax: sysdate( [<format>] [,<timezone>] )

Available since: 0.9.25

Example

  1. SELECT sysdate('dd-MM-yyyy') FROM Account

format()

Formats a value using the String.format() conventions. Look here for more information.

Syntax: format( <format> [,<arg1> ](,<arg-n>]*.md)

Available since: 0.9.25

Example

  1. SELECT format("%d - Mr. %s %s (%s)", id, name, surname, address) FROM Account

dijkstra()

Returns the cheapest path between two vertices using the [http://en.wikipedia.org/wiki/Dijkstra's_algorithm Dijkstra algorithm] where the weightEdgeFieldName parameter is the field containing the weight. Direction can be OUT (default), IN or BOTH.

Syntax: dijkstra(<sourceVertex>, <destinationVertex>, <weightEdgeFieldName> [, <direction>])

Available since: 1.3.0

Example

  1. SELECT dijkstra($current, #8:10, 'weight') FROM V

shortestPath()

Returns the shortest path between two vertices. Direction can be OUT (default), IN or BOTH.

Available since: 1.3.0

Syntax: shortestPath( <sourceVertex>, <destinationVertex> [, <direction> [, <edgeClassName> [, <additionalParams>]]])

Where:

  • sourceVertex is the source vertex where to start the path
  • destinationVertex is the destination vertex where the path ends
  • direction, optional, is the direction of traversing. By default is “BOTH” (in+out). Supported values are “BOTH” (incoming and outgoing), “OUT” (outgoing) and “IN” (incoming)
  • edgeClassName, optional, is the edge class to traverse. By default all edges are crossed. Since 2.0.9 and 2.1-rc2
  • additionalParams (since v 2.1.12), optional, here you can pass a map of additional parametes (Map in Java, JSON from SQL). Currently allowed parameters are
    • ‘maxDepth’: integer, maximum depth for paths (ignore path longer that ‘maxDepth’)

Example on finding the shortest path between vertices #8:32 and #8:10

  1. SELECT shortestPath(#8:32, #8:10)

Example on finding the shortest path between vertices #8:32 and #8:10 only crossing outgoing edges

  1. SELECT shortestPath(#8:32, #8:10, 'OUT')

Example on finding the shortest path between vertices #8:32 and #8:10 only crossing incoming edges of type ‘Friend’

  1. SELECT shortestPath(#8:32, #8:10, 'IN', 'Friend')

Example on finding the shortest path between vertices #8:32 and #8:10, long at most five hops

  1. SELECT shortestPath(#8:32, #8:10, null, null, {"maxDepth": 5})

distance()

Syntax: distance( <x-field>, <y-field>, <x-value>, <y-value> )

Returns the distance between two points in the globe using the Haversine algorithm. Coordinates must be as degrees.

Available since: 0.9.25

Example

  1. SELECT FROM POI WHERE distance(x, y, 52.20472, 0.14056 ) <= 30

distinct()

Syntax: distinct(<field>)

Retrieves only unique data entries depending on the field you have specified as argument. The main difference compared to standard SQL DISTINCT is that with OrientDB, a function with parenthesis and only one field can be specified.

Available since: 1.0rc2

Example

  1. SELECT distinct(name) FROM City

unionall()

Syntax: unionall(<field> [,<field-n>]*)

Works as aggregate or inline. If only one argument is passed then aggregates, otherwise executes and returns a UNION of all the collections received as parameters. Also works also with no collection values.

Available since: 1.7

Example

  1. SELECT unionall(friends) FROM profile
  1. select unionall(inEdges, outEdges) from OGraphVertex where label = 'test'

intersect()

Syntax: intersect(<field> [,<field-n>]*)

Works as aggregate or inline. If only one argument is passed than aggregates, otherwise executes, and returns, the INTERSECTION of the collections received as parameters.

Available since: 1.0rc2

Example

  1. SELECT intersect(friends) FROM profile WHERE jobTitle = 'programmer'
  1. SELECT intersect(inEdges, outEdges) FROM OGraphVertex

difference()

Syntax: difference(<field> [,<field-n>]*)

Works as aggregate or inline. If only one argument is passed than aggregates, otherwise executes, and returns, the DIFFERENCE between the collections received as parameters.

Available since: 1.0rc2

Example

  1. SELECT difference(tags) FROM book
  1. SELECT difference(inEdges, outEdges) FROM OGraphVertex

symmetricDifference()

Syntax: symmetricDifference(<field> [,<field-n>]*)

Works as aggregate or inline. If only one argument is passed than aggregates, otherwise executes, and returns, the SYMMETRIC DIFFERENCE between the collections received as parameters.

Available since: 2.0.7

Example

  1. SELECT difference(tags) FROM book
  1. SELECT difference(inEdges, outEdges) FROM OGraphVertex

set()

Adds a value to a set. The first time the set is created. If <value> is a collection, then is merged with the set, otherwise <value> is added to the set.

Syntax: set(<field>)

Available since: 1.2.0

Example

  1. SELECT name, set(roles.name) AS roles FROM OUser

list()

Adds a value to a list. The first time the list is created. If <value> is a collection, then is merged with the list, otherwise <value> is added to the list.

Syntax: list(<field>)

Available since: 1.2.0

Example

  1. SELECT name, list(roles.name) AS roles FROM OUser

map()

Adds a value to a map. The first time the map is created. If <value> is a map, then is merged with the map, otherwise the pair <key> and <value> is added to the map as new entry.

Syntax: map(<key>, <value>)

Available since: 1.2.0

Example

  1. SELECT map(name, roles.name) FROM OUser

traversedElement()

Returns the traversed element(s) in Traverse commands.

Syntax: traversedElement(<index> [,<items>])

Where:

  • <index> is the starting item to retrieve. Value >= 0 means absolute position in the traversed stack. 0 means the first record. Negative values are counted from the end: -1 means last one, -2 means the record before last one, etc.
  • <items>, optional, by default is 1. If >1 a collection of items is returned

Available since: 1.7

Example

Returns last traversed item of TRAVERSE command:

  1. SELECT traversedElement(-1) FROM ( TRAVERSE out() FROM #34:3232 WHILE $depth <= 10 )

Returns last 3 traversed items of TRAVERSE command:

  1. SELECT traversedElement(-1, 3) FROM ( TRAVERSE out() FROM #34:3232 WHILE $depth <= 10 )

traversedEdge()

Returns the traversed edge(s) in Traverse commands.

Syntax: traversedEdge(<index> [,<items>])

Where:

  • <index> is the starting edge to retrieve. Value >= 0 means absolute position in the traversed stack. 0 means the first record. Negative values are counted from the end: -1 means last one, -2 means the edge before last one, etc.
  • <items>, optional, by default is 1. If >1 a collection of edges is returned

Available since: 1.7

Example

Returns last traversed edge(s) of TRAVERSE command:

  1. SELECT traversedEdge(-1) FROM ( TRAVERSE outE(), inV() FROM #34:3232 WHILE $depth <= 10 )

Returns last 3 traversed edge(s) of TRAVERSE command:

  1. SELECT traversedEdge(-1, 3) FROM ( TRAVERSE outE(), inV() FROM #34:3232 WHILE $depth <= 10 )

traversedVertex()

Returns the traversed vertex(es) in Traverse commands.

Syntax: traversedVertex(<index> [,<items>])

Where:

  • <index> is the starting vertex to retrieve. Value >= 0 means absolute position in the traversed stack. 0 means the first vertex. Negative values are counted from the end: -1 means last one, -2 means the vertex before last one, etc.
  • <items>, optional, by default is 1. If >1 a collection of vertices is returned

Available since: 1.7

Example

Returns last traversed vertex of TRAVERSE command:

  1. SELECT traversedVertex(-1) FROM ( TRAVERSE out() FROM #34:3232 WHILE $depth <= 10 )

Returns last 3 traversed vertices of TRAVERSE command:

  1. SELECT traversedVertex(-1, 3) FROM ( TRAVERSE out() FROM #34:3232 WHILE $depth <= 10 )

mode()

Returns the values that occur with the greatest frequency. Nulls are ignored in the calculation.

Syntax: mode(<field>)

Available since: 2.0-M1

Example

  1. SELECT mode(salary) FROM Account

median()

Returns the middle value or an interpolated value that represent the middle value after the values are sorted. Nulls are ignored in the calculation.

Syntax: median(<field>)

Available since: 2.0-M1

Example

  1. select median(salary) from Account

percentile()

Returns the nth percentiles (the values that cut off the first n percent of the field values when it is sorted in ascending order). Nulls are ignored in the calculation.

Syntax: percentile(<field> [, <quantile-n>]*)

Available since: 2.0-M1

Examples

  1. SELECT percentile(salary, 95) FROM Account
  2. SELECT percentile(salary, 25, 75) AS IQR FROM Account

variance()

Returns the middle variance: the average of the squared differences from the mean. Nulls are ignored in the calculation.

Syntax: variance(<field>)

Available since: 2.0-M1

Example

  1. SELECT variance(salary) FROM Account

stddev()

Returns the standard deviation: the measure of how spread out values are. Nulls are ignored in the calculation.

Syntax: stddev(<field>)

Available since: 2.0-M1

Example

  1. SELECT stddev(salary) FROM Account

uuid()

Generates a UUID as a 128-bits value using the Leach-Salz variant. For more information look at: http://docs.oracle.com/javase/6/docs/api/java/util/UUID.html.

Available since: 2.0-M1

Syntax: uuid()

Example

Insert a new record with an automatic generated id:

  1. INSERT INTO Account SET id = UUID()

Custom functions

The SQL engine can be extended with custom functions written with a Scripting language or via Java.

Database’s function

Look at the Functions page.

Custom functions in Java

Before to use them in your queries you need to register:

  1. // REGISTER 'BIGGER' FUNCTION WITH FIXED 2 PARAMETERS (MIN/MAX=2)
  2. OSQLEngine.getInstance().registerFunction("bigger",
  3. new OSQLFunctionAbstract("bigger", 2, 2) {
  4. public String getSyntax() {
  5. return "bigger(<first>, <second>)";
  6. }
  7. public Object execute(Object[] iParameters) {
  8. if (iParameters[0] == null || iParameters[1] == null)
  9. // CHECK BOTH EXPECTED PARAMETERS
  10. return null;
  11. if (!(iParameters[0] instanceof Number) || !(iParameters[1] instanceof Number))
  12. // EXCLUDE IT FROM THE RESULT SET
  13. return null;
  14. // USE DOUBLE TO AVOID LOSS OF PRECISION
  15. final double v1 = ((Number) iParameters[0]).doubleValue();
  16. final double v2 = ((Number) iParameters[1]).doubleValue();
  17. return Math.max(v1, v2);
  18. }
  19. public boolean aggregateResults() {
  20. return false;
  21. }
  22. });

Now you can execute it:

  1. List<ODocument> result = database.command(
  2. new OSQLSynchQuery<ODocument>("SELECT FROM Account WHERE bigger( salary, 10 ) > 10") )
  3. .execute();