Logical Functions

This topic describes the logical functions in AnzoGraph DB.

  • AND: Evaluates two logical expressions and returns true if both expressions are true.
  • BOUND: Evaluates whether an RDF term type is bound.
  • CASE: Evaluates a series of conditions and returns the matching result.
  • COALESCE: Evaluates a number of expressions and returns the results for the first expression that is bound and does not raise an error.
  • EXISTS: Evaluates whether the specified pattern exists.
  • IF: Evaluates a condition and returns the specified result depending on the outcome of the test.
  • IN: Evaluates whether the specified RDF term is found in any of the given test values.
  • NOT: Evaluates whether the specified logical expression is not true.
  • OR: Evaluates two logical expressions and returns true if at least one of the expressions is true.
  • PARTITIONINDEX: Returns the zero-based index of the bucket in which the specified value falls.
  • SAMETERM: Evaluates whether two RDF term type values are the same.
  • UNBOUNDED: Extends the SPARQL UNDEF functionality to enable users to include an undefined value as a function argument.

AND

This function evaluates two logical expressions. If both expressions are true, the function returns true. If one or both arguments are false, the function returns false.

Syntax

AND(logical_expression1, logical_expression2)
Argument Data Type Description
logical_expression1 evaluates to boolean The first logical expression to evaluate.
logical_expression2 evaluates to boolean The second logical expression to evaluate.

Returns

Data Type Description
boolean True if both conditions are true and false if either condition is false.

BOUND

This function evaluates whether the specified RDF term has a value bound to it.

Syntax

BOUND(term)
Argument Data Type Description
term RDF term The literal, URI, or blank node value to evaluate.

Returns

Data Type Description
boolean True if the term is bound and false if it is not.

CASE

This function enables you to add IF/THEN logic to a query. A CASE expression evaluates a series of conditions and returns the matching result. You can use CASE expressions wherever expressions are valid in SPARQL queries.

Syntax

There are two variations of CASE statements: simple and generic. Use the simple form to compare the results of an expression with a series of tests and return a result when a test returns true. Use the generic form when evaluating a larger range of tests with multiple conditions.

Simple Form

CASE expression_to_compare 
WHEN expression1 THEN result1
WHEN expression2 THEN result2
[ WHEN expressionN THEN resultN ]
[ ELSE result_when_false ]
END
Argument Data Type Description
expression_to_compare evaluates to boolean The expression to evaluate and compare its results with the subsequent expressions.
expression1–N evaluates to boolean The expressions to evaluate against expression_to_compare.
result1–N any The result to return when the corresponding expression is true.
result_when_false any An optional value to be returned if none of the specified expressions are true.

Generic Form

CASE WHEN condition1 THEN result1
WHEN condition2 THEN result2
[ WHEN conditionN THEN resultN ]
[ ELSE result_when_false ]
END
Argument Data Type Description
condition1–N evaluates to boolean The conditions to test.
result1–N any The result to return when the corresponding condition passes.
result_when_false any An optional value to be returned if none of the specified conditions pass.

Returns

Data Type Description
The type of the specified result The specified results according to the evaluation of the conditions.

Example

The following example uses a CASE statement to determine and report on whether the top 10 events (with the most tickets sold) sold out.

PREFIX tickit: <http://anzograph.com/tickit/>
SELECT ?event ?venue ?seats
(( CASE WHEN (?seats <= (sum(?qty))) then "yes"
        WHEN (?seats > (sum(?qty))) then "no"
   END ) as ?sold_out)
FROM <http://anzograph.com/tickit>
WHERE {
  ?sales tickit:qtysold ?qty .
  ?sales tickit:eventid ?eventid .
  ?eventid tickit:eventname ?event .
  ?eventid tickit:venueid ?venueid .
  ?venueid tickit:venuename ?venue .
  ?venueid tickit:venueseats ?seats.
}
GROUP BY ?event ?venue ?seats ?qty
ORDER BY desc(?qty)
LIMIT 10
event             | venue                        | seats | sold_out
------------------+------------------------------+-------+----------
Simple Plan       | Hubert H. Humphrey Metrodome | 64035 | no
Black Crowes      | Yankee Stadium               | 52325 | no
Hot Tuna          | Turner Field                 | 50091 | no
Marc Anthony      | Georgia Dome                 | 71149 | no
Mark Knopfler     | Edward Jones Dome            | 66965 | no
Spoon             | Dolphin Stadium              | 74916 | no
Armando Manzanero | Texas Stadium                | 65595 | no
Missy Higgins     | Great American Ball Park     | 42059 | no
Zombies           | Lambeau Field                | 72922 | no
Hannah Montana    | Monster Park                 | 69843 | no
10 rows

COALESCE

This function evaluates a number of expressions and returns the results for the first expression that is bound and does not raise an error.

Syntax

COALESCE(expression1 [, expression2 ] [, expressionN ] )
Argument Data Type Description
expression1–N RDF term The literal, URI, or blank node expressions to evaluate.

Returns

Data Type Description
RDF term The result of the first expression that is bound and does not error.

EXISTS

This function evaluates whether the specified pattern exists in the data.

Syntax

EXISTS { graph_pattern }

Returns

Data Type Description
boolean True if the pattern exists and false if it does not.

IF

This function evaluates a condition and returns the specified result depending on the outcome of the test. If the condition evaluates to true, the first result is returned. If the condition evaluates to false, the second result is returned. And if the condition results in an error, the third result is returned.

Syntax

IF(logical_expression, true_result, false_result [, error_result ])
Argument Data Type Description
logical_expression evaluates to boolean The condition that evaluates to true or false.
true_result RDF term The value that defines the result to return if the condition evaluates to true.
false_result RDF term The value that defines the result to return if the condition evaluates to false.
error_result RDF term An optional value that defines the result to return if the condition evaluates to an error. If the condition results in an error and error_result is not specified, logical_expression(error) is returned.

Returns

Data Type Description
RDF term The result based on the evaluation of the condition.

IN

This function evaluates whether the specified RDF term type value is found in any of the given test values.

Syntax

IN(term, test_value1 [, test_value2 ] [, test_valueN])
Argument Data Type Description
term RDF term The literal, URI, or blank node value to look for in the test values.
test_value1–N RDF term The literal, URI, or blank node values to look for the specified term in.

Returns

Data Type Description
boolean True if the given term is found in the test values and false if it is not.

Example

The example below queries the sample Tickit data set to return the names of people who were born in the year 1975.

PREFIX tickit: <http://anzograph.com/tickit/>
SELECT ?birthday (concat(?fname, ?lname) AS ?name)
FROM <http://anzograph.com/tickit>
WHERE {
  ?s tickit:firstname ?fname .
  ?s tickit:lastname ?lname .
  ?s tickit:birthday ?birthday.
  FILTER ((YEAR(?birthday)) IN (1975))
}
ORDER BY ?birthday
birthday   | name
-----------+---------------------
1975-01-01 | MaryamWeeks
1975-01-01 | OliverHammond
1975-01-01 | MacKenzieBaldwin
1975-01-01 | XenosBaxter
1975-01-01 | XanderWilson
1975-01-01 | HadleyBush
1975-01-01 | RajaTodd
1975-01-02 | AlecFitzgerald
1975-01-02 | QuinnBuckley
1975-01-03 | RhiannonBooth
1975-01-03 | LanaLeonard
1975-01-03 | DeirdreWheeler
...
763 rows

NOT

This function evaluates whether the specified logical expression is not true.

Syntax

NOT(logical_expression)
Argument Data Type Description
logical_expression evaluates to boolean The condition to evaluate.

Returns

Data Type Description
boolean True if the condition is false and false if it is true.

OR

This function evaluates two logical expressions. If at least one expression is true, the function returns true. If both expressions are false, the function returns false.

Syntax

OR(logical_expression1, logical_expression2)
Argument Data Type Description
logical_expression1 evaluates to boolean The first logical expression to evaluate.
logical_expression2 evaluates to boolean The second logical expression to evaluate.

Returns

Data Type Description
boolean True if one or both conditions are true and false if both conditions are false.

PARTITIONINDEX

This function returns the zero-based index of the bucket in which the specified value falls. Buckets start at the specified start value and are sized according to the specified interval. The first bucket is [start, start+interval). That means it is closed on the low end and open on the high end. PARTITIONINDEX returns less than 0 if the value does not fall into any bucket, such as when the given value is less than start or if the comparison is indeterminate for date and time data types.

Syntax

PARTITIONINDEX(value, start, interval)
Argument Data Type Description
value literal The literal value for which to determine the zero-based index.
start literal The literal value that indicates the start of the first bucket.
interval literal The literal value that specifies the size of the bucket.

Returns

Data Type Description
long The zero-based index of the bucket in which the specified value exists.

SAMETERM

This function evaluates whether two RDF term type values are the same.

Syntax

SAMETERM(term1, term2)
Argument Data Type Description
term1 RDF term The first literal, URI, or blank node value to compare.
term2 RDF term The literal, URI, or blank node value to compare to term1.

Returns

Data Type Description
boolean True if the terms are the same and false if they are not.

UNBOUNDED

This function is like the SPARQL UNDEF keyword but extends that functionality to enable users to include an undefined value as a function argument, as UNDEF is only supported in VALUES clauses.

Syntax

UNBOUNDED()

Returns

Data Type Description
RDF term The specified result according to the evaluation of the condition.

Example

The following example statement incorporates UNBOUNDED to return null if the specified condition (?x > 5) fails:

BIND(IF(?x > 5 , "Win", UNBOUNDED()) as ?testResult) 

In this case, ?testResult is bound if ?x is greater than 5. If ?x is not greater than 5, ?testResult is not bound.