Arithmetic Operators

Arithmetic operators are used to perform operations on numeric values.

OperatorPurposeExample
-A unary operator that denotes a negative valueWHERE field_name = -1
+, -As binary operators, they add or subtractWHERE field_name + 1 > 2
*MultiplyWHERE field_name * 2 > 2
/DivideWHERE field_name / 2 > 1
%ModuloWHERE field_name % 2 > 1
abs(x)Absolute valueWHERE abs(field_name) = 1
ceil(x)Next whole numberWHERE ceil(field_name) > 1
floor(x)Previous whole numberWHERE floor(field_name) > 1

String Operators

Performs various operations on string scalar types:

OperatorPurposeExample
||String concatenationWHERE field_name || 'world' = 'hello world'
concat(str, …)String concatenationWHERE concat(field_name, 'world') = 'hello world'
contains(str, substr)Returns true if substr is contained in str, otherwise returns falseWHERE contains(field_name, 'hello')
starts_with(str, substr)Returns true if str starts with substr, otherwise returns falseWHERE starts_with(field_name, 'hello')
ends_with(str, substr)Returns true if str ends with substr, otherwise returns falseWHERE ends_with(field_name, 'world')
byte_length(str)Returns the length of str in bytes. Strings in Ditto are encoded in UTF-8, so a character can take up to 4 bytes.WHERE byte_length(field_name) > 4
char_length(str) character_length(str)Returns the number of characters in str.WHERE char_length(field_name) > 4

Scalar Type Operators

Indicates which scalar type to interact with:

OperatorPurposeExample
is_boolean(x)Boolean type testWHERE is_boolean(field_name)
is_number(x)Float, Int, or UInt type testWHERE is_number(field_name)
is_string(x)String type testWHERE is_string(field_name)
type(x)Returns a string name representing the typeWHERE type(field_name) = 'string'

Following is the mapping between data types and scalar types; all of which are case-sensitive:

REGISTERMAPATTACHMENT
* null
* boolean
* number
* binary
* string
* array
* object
objectobject

Conversion Operators

Converts between different value types.

OperatorPurposeExample
deserialize_json(str)Returns the JSON string deserialized into an objectWHERE deserialize_json('{"field_name": "blue"}') = 'blue'

Array Operators

The following table provides an overview of interactions with array. Arrays can be either scalar values in a REGISTER or an input via an argument.

OperatorPurposeExample
array_contains(array, value)Returns true if the array contains the value, otherwise returns falseWHERE array_contains(field_name, 'blue')``WHERE array_contains(:your_array, field_name)
array_contains_null(array)Returns true if the array contains a NULL value, otherwise returns falseWHERE array_contains_null(field_name)
array_length(array)Returns the length of the array.WHERE array_length(field_name) > 0

Object Operators

The following table provides an overview of interactions with object. Objects can be either scalar values in a REGISTER, MAP, Attachment, or an input via an argument.

OperatorPurposeExample
object_length(object)Returns the number of key-value pairs in the top-level of the objectWHERE object_length(field_name) > 0

Collection Operators

The following table provides the collection operators for comparing if a given value is equal to any of the values in a list:

OperatorPurposeExample
IN (x, y, …)Membership testWHERE department IN ('HR', 'Sales')
NOT IN (x, y, …)Non-membership testWHERE department NOT IN ('HR', 'Sales')

Array and Object Literals

Since version 4.8 of the Ditto SDK, Array and object literals are supported and can be used inline.

SQL
SELECT * FROM your_collection_name WHERE field1 = [0, 1]

If using a version prior to 4.8, use arguments to pass in your array or object instead.

SQL
SELECT * FROM your_collection_name WHERE field1 = :your_array

Comparison Operators

The comparison operators fall into one of two sub-categories:

  • Missing value comparisons
  • Regular value comparisons

DQL has two ways of representing missing information in an object:

  • The presence of the field with a NULL for its value (as in SQL)
  • The absence of the field (which JSON permits)

The following table provides operators for comparing if a given value is equal to any of the values in a list:

NULL and UNKNOWN are synonym keywords and provide the same behavior.

OperatorPurposeExample
IS NULLReturns true if the value is NULL, otherwise returns falseWHERE field_name IS NULL
IS NOT NULLReturns true if the value is not NULL, otherwise returns falseWHERE field_name IS NOT NULL
IS MISSINGReturns true if the field is missing in a given document, otherwise returns falseWHERE field_name IS MISSING
IS NOT MISSINGReturns true if the field is not missing in a given document, otherwise returns falseWHERE field_name IS NOT MISSING
IS UNKNOWNReturns true if the value is NULL, otherwise returns falseWHERE field_name IS UNKNOWN
IS NOT UNKNOWNReturns true if the value is not NULL, otherwise returns falseWHERE field_name IS NOT UNKNOWN
=Equality testWHERE field_name = 100
==Equality testWHERE field_name == 100
!=Inequality testWHERE field_name != 100
<>Inequality testWHERE field_name <> 100
<Less thanWHERE field_name < 100
>Greater thanWHERE field_name > 100
<=Less than or equal toWHERE field_name <= 100
>=Greater than or equal toWHERE field_name >= 100

Comparison Operations with NULL

In DQL, NULL represents missing or unknown data. It’s not a value in the way that 1 or 'text' are values. Any comparison operation that includes NULL will result in NULL.

EquationResult
NULL = NULLNULL
NULL <> NULLNULL
1 = NULLNULL
1 > NULLNULL

Conditional Operators

Conditional Operators allow you to more easily express conditional logic within your DQL queries.

OperatorPurposeExample
coalesce(v1, v2, …)Returns the first non-null value passed to the functionWHERE coalesce(field_name_1, field_name_2, field_name_3) = 'foo'
decode(input, comp1, res1, [comp2, res2…] [, default value])Compares the input against each compparameter, and if they match type and value, returns the correspondingres value. If no matches are found, the default value is returned (which defaults to NULL).WHERE decode(field_name, 'foo', 'I found foo', 'bar', 'I found bar', 'I found nothing') = 'foo'
nvl(input, default)Returns the input if input evaluates to NOT NULL, otherwise returns default.WHERE nvl(field_name, 0) > 10

Logical Operators

Logical operators perform logical NOTAND, and OR operations over Boolean values (TRUE and FALSE), plus NULL and MISSING.

OperatorPurposeExample
NOTReturns true if the following condition is false, otherwise returns false.WHERE NOT field_name = true
ANDReturns true if both branches are true, otherwise returns falseWHERE field_1 = true AND field_2 = false
ORReturns true if one branch is true; otherwise, returns falseWHERE field_1 = true OR field_2 = false

NOT Truth Table

ValueResult
TrueFalse
FalseTrue
NULLNULL

AND Truth Table

TrueNULLFalse
TrueTrueNULLFalse
NULLNULLNULLFalse
FalseFalseFalseFalse

OR Truth Table

TrueNULLFalse
TrueTrueTrueTrue
NULLTrueNULLNULL
FalseTrueNULLFalse