Casting Functions

This topic describes the functions that are available for coercing data types in Graph Lakehouse.

  • BNODE: Creates a blank node.
  • DATATYPE: Returns the data type of the given value.
  • DATETIME (or xsd:dateTime): Returns a dateTime value from the given string, long, or dateTime.
  • DUR_TO_USECS: Casts a duration value to microseconds.
  • ENCODE_FOR_URI: Encodes the specified string as a URI.
  • FORMATDATETIME: Converts a value to a string in the specified dateTime format.
  • FORMATDURATION: Converts a value into a string in the specified duration format.
  • HEX: Converts a long value to a hexadecimal string.
  • HEX2DEC: Converts a hexadecimal string to a long value.
  • PARSEDATE: Attempts to convert the given string to a date, time, or dateTime value.
  • RADIANS: Converts to radians an angle value that is in degrees.
  • SERIALIZE: Creates a string representation of the input value.
  • STR: Casts a value to a string.
  • URI: Casts a string to a URI.
  • USECS_TO_DUR: Converts a microseconds value to a duration.
  • UUID: Generates a Universally Unique Identifier (UUID).

Typographical Conventions

The following list describes the conventions used to document function syntax:

  • CAPS: Although SPARQL is case-insensitive, SPARQL keywords in this section are written in uppercase for readability.
  • [ argument ]: Brackets indicate an optional argument or keyword.
  • |: Means OR. Indicates that you can use one or more of the specified options.

BNODE

This function creates a blank node.

Syntax

BNODE([ value ])
Argument Type Description
value string An optional string value from which to create the blank node.

Returns

Type Description
blank node The generated blank node.

DATATYPE

This function returns the data type of the given value.

Syntax

DATATYPE(value)
Argument Type Description
value any The value for which to return the data type.

Returns

Type Description
data type URI The data type.

DATETIME (or xsd:dateTime)

This function returns a dateTime value from the given long, double, date, or time value.

Syntax

DATETIME(value)
Argument Type Description
value long, double, date, time The value from which to return a dateTime.

Returns

Type Description
dateTime The dateTime value.

DUR_TO_USECS

This function calculates the time in microseconds from a duration value.

Syntax

DUR_TO_USECS(value)
Argument Type Description
value duration The duration value from which to calculate the time in microseconds.

Returns

Type Description
long The number of microseconds.

Example

SELECT (DUR_TO_USECS("PT2H11M48.376S"^^xsd:duration) as ?microseconds)
microseconds
------------------
7908376000
1 rows

ENCODE_FOR_URI

This function encodes the specified string as a URI and returns a string in URI format.

Syntax

ENCODE_FOR_URI(text)
Argument Type Description
text string The string value to encode as a URI.

Returns

Type Description
string The string as a URI.

Example

PREFIX tickit: <http://anzograph.com/tickit/>
SELECT DISTINCT (ENCODE_FOR_URI(?eventname) as ?event)
FROM <http://anzograph.com/tickit>
WHERE {
   ?s tickit:eventid ?eventid .
   ?eventid tickit:eventname ?eventname .
}
ORDER BY ?event
LIMIT 10
 event
-----------------------------------
.38%20Special
3%20Doors%20Down
70s%20Soul%20Jam
A%20Bronx%20Tale
A%20Catered%20Affair
A%20Chorus%20Line
A%20Christmas%20Carol
A%20Doll%27s%20House
A%20Man%20For%20All%20Seasons
A%20Midsummer%20Night%27s%20Dream
10 rows

HEX

This function converts a long value to a hexadecimal string.

Syntax

HEX(value)
Argument Type Description
value long The value to convert to a hexadecimal string.

Returns

Type Description
string The hex value.

HEX2DEC

This function converts a hexadecimal string to a long value.

Syntax

HEX2DEC(value)
Argument Type Description
value string The hexadecimal string to convert.

Returns

Type Description
long The converted value.

FORMATDATETIME

This function converts a value into a string with the specified dateTime format.

Syntax

FORMATDATE(value, format)
Argument Type Description
value long, double, date, time, dateTime The value to convert to a string in the specified format.
format string The format to use for the resulting dateTime string. Graph Lakehouse supports YYYY-MM-DDThh:mm:ss format.

Returns

Type Description
string The dateTime as a string.

FORMATDURATION

This function converts a value into a string with the specified duration format.

Syntax

FORMATDURATION(value, format)
Argument Type Description
value long, string, duration The value to convert to a string in the specified format.
format string The format to use for the resulting duration string. Graph Lakehouse supports PnYnMnDTnHnMnS format.

Returns

Type Description
string The duration as a string.

PARSEDATE

This function attempts to convert the given string to a date, time, or dateTime value.

Syntax

PARSEDATE(value [, output_type ])
Argument Type Description
value string The string or plain literal value to convert to a date, time, or dateTime.
output_type URI An optional URI (xsd:date, xsd:time, or xsd:dateTime) that specifies the type of value to return. If output_type is not specified, dateTime is returned.

Returns

Type Description
date, time, or dateTime The conversion of the string to the desired type.

RADIANS

This function converts to radians an angle value that is in degrees.

Syntax

RADIANS(angle)
Argument Type Description
angle double The angle value to convert to radians.

Returns

Type Description
double The angle in radians.

SERIALIZE

This function returns a string representation of the input value.

Syntax

SERIALIZE(value)
Argument Type Description
value any type except URI The value for which to generate a string representation.

Returns

Type Description
string The string representation of the input value.

STR

This function casts the specified value to a string.

Syntax

STR(value)
Argument Type Description
value any The value to convert to a string.

Returns

Type Description
string The value as a string.

URI

This function casts the specified string to a URI.

Syntax

URI(value)
Argument Type Description
value string The value to convert to a URI.

Returns

Type Description
URI The value as a URI.

USECS_TO_DUR

This function converts a number of microseconds in long, duration, or string format to a duration value.

Syntax

USECS_TO_DUR(value)
Argument Type Description
value long, duration, string The microseconds value to convert to a duration.

Returns

Type Description
duration The input value as a duration.

Example

SELECT (USECS_TO_DUR(76555373888) as ?duration)
duration
----------------------
PT21H15M55.373888S
1 rows

UUID

This function generates a Universally Unique Identifier (UUID).

Syntax

UUID()

Returns

Type Description
URI The UUID.