In general, MarkLogic supports the syntax from the SQL92 standard. This chapter describes some of the SQL syntax that are unique to MarkLogic Server.
This section describes the SQL statements and functions supported in MarkLogic. The topics are:
MarkLogic SQL does not support updates, so only the SQL statements in the following table are supported.
SQL Statement | Notes |
---|---|
EXPLAIN | Produces an execution plan, as described in Execution Plan. |
SELECT | The following SELECT options are not supported: BLOB types and correlated subqueries containing a GROUP BY. |
MarkLogic supports the SQL functions in the SQL92 standard. In addition, MarkLogic supports SQL functions that are not part of the SQL92 standard, as shown in the table below. The SQL functions are listed along with the MarkLogic builtin functions that support them. The syntax for the SQL function is the same as that of the respective builtin function.
You can also call any MarkLogic builtin function in a SQL query, as described in Calling Built-in Functions from SQL.
SQL Function | MarkLogic Builtin |
---|---|
acos |
math:acos |
ascii |
fn:string-to-codepoints |
asin |
math:asin |
atan |
math:atan |
atan2 |
math:atan2 |
bit-length |
sql:bit-length |
ceiling |
fn:ceiling |
char |
fn:codepoints-to-string |
character-length |
fn:string-length |
char-length |
fn:string-length |
concat |
fn:concat |
cos |
math:cos |
cot |
math:cot |
current-date |
fn:current-date |
current-time |
fn:current-time |
current-timestamp |
fn:current-dateTime |
current-user |
fn:get-current-user |
curdate |
fn:current-date |
curtime |
fn:current-time |
datepart |
sql:datepart |
datediff |
sql:datediff |
dateadd |
sql:dateadd |
day |
sql:day |
dayname |
sql:dayname |
dayofmonth |
sql:day |
dayofweek |
sql:weekday |
dayofyear |
sql:yearday |
degrees |
math:degrees |
exp |
math:exp |
floor |
fn:floor |
hour |
sql:hours |
initcap |
xdmp:initcap |
insert |
sql:insert |
left |
sql:left |
length |
fn:string-length |
localtime |
fn:current-time |
localtimestamp |
fn:current-dateTime |
locate |
xdmp:position |
log |
math:log |
log10 |
math:log10 |
minute |
sql:minutes |
mod |
math:fmod |
month |
sql:month |
monthname |
sql:monthname |
now |
fn:current-time |
octet-length |
sql:octet-length |
pi |
math:pi |
position |
xdmp:position |
power |
math:pow |
quarter |
sql:quarter |
radians |
math:radians |
rand |
sql:rand |
random |
sql:rand |
repeat |
sql:repeat |
right |
sql:right |
sign |
sql:sign |
sin |
math:sin |
second |
sql:seconds |
session-user |
fn:get-current-user |
space |
sql:space |
sqrt |
math:sqrt |
strpos |
xdmp:position |
substring |
fn:substring |
tan |
math:tan |
timestampadd |
sql:timestampadd |
timestampdiff |
sql:timestampdiff |
truncate |
math:trunc |
trunc |
math:trunc |
user |
xdmp:get-current-user |
week |
sql:week |
year |
sql:year |
The table below lists all of the supported SQL types in MarkLogic, along with the mapping from the SQL types to XML Schema (or MarkLogic) types. MarkLogic also supports a number of SQL type that go beyond those supported by the SQL92 standard, as well as some vendor specific types.
Limits on datatypes are not enforced. For example, if you enter DECIMAL(p,s)
, the precision and scale are ignored.
Data dictionaries consists of a series of tables that are created in the SYS schema. These system tables are listed in the table below.
To see the full contents of a system table, do a select *
. For example:
select * from sys_tables
Each view has two system columns:
Column Name | Description |
---|---|
__docid |
Identifies the fragment ID of each document that matches the view(s). |
__content |
Returns the content of document that matches the view(s). |
The __docid
and __content
system columns are preceded by two underscores.
For example: The following returns the fragment ID for each document that matches the employees
view:
select __docid from employees
The following returns the contents of each document that matches the employees
view:
select __content from employees
You can call MarkLogic built-in functions from inside a SELECT statement, as long as the parameter types match the column types. You cannot call aggregate functions from SQL.
The following are some examples of the use of MarkLogic functions in SQl statements:
Provide the version of MarkLogic Server and hardware information:
select xdmp_version(), xdmp_platform(), xdmp_architecture()
Trace the performance of a query:
select xdmp_elapsed_time, t1.this, t2.that from t1, t2 where t1.key=t2.ref group by t1.this
select math_cos(EmployeeID) from employees
select cts_distance(town.center, building.location) from town, building
Return the first five values of the FirstName column, starting with the third character:
select fn_substring(FirstName,3) from employees limit 5
MarkLogic supports the ORDER BY keyword supported in SQL standard 2003, rather than sql92.
When you include an ORDER BY in SQL queries, such as ORDER BY column_A desc nulls first
, you are also able to specify where to put the nulls, either at the beginning (NULLS FIRST
) or at the bottom (NULLS LAST
). If you don't specify the nulls ordering, the default behavior is NULLS LAST
, putting all the nulls at the bottom, which is often the most efficient option.
If you want the default behavior to place nulls as the smallest value (the default before 9.0-9) enable the trace event:
Optic Nulls Smallest On
MarkLogic supports the GROUPING SETS keyword supported in SQL99.
GROUP BY GROUPING SETS allows the calculation of multiple group bys in a single pass, by allowing you to specify multiple sets of grouping columns. For example:
SELECT A.id,B.name,COUNT(*) FROM Table_1 AS A, Table_2 as B WHERE A.number = B.number GROUP BY GROUPING SETS (A.id,(A.id,B.name));
In this example, the GROUP BY clause determines the first requirement - groups of IDs - by grouping the A.ID values from the TABLE_1 Table. It then determines the second requirement - number of IDs by ID and NAME - by grouping the A.ID values from TABLE_1 with the B.NAME values from TABLE_2.
A <grouping specification> of () (called grand total in the SQL Standard) is equivalent to grouping the entire result Table.
The CUBE keyword is a simpler way to specify particular sets of grouping columns.
Here are two examples comparing the CUBE and GROUPING SETS keywords:
select A, B, C, count(*) from Table group by cube(A, B, C)
Using the GROUPING SETS keyword:
select A, B, C, count(*) from Table group by grouping sets( (A, B, C), (A, B), (A, C), (B, C), (A), (B), (C), () )
The ROLLUP keyword is a simpler way to specify particular sets of grouping columns. Here are two examples comparing the ROLLUP and GROUPING SETS keywords:
select A, B, C, count(*) from Table group by rollup(A, B, C)
Using the GROUPING SETS keyword:
select A, B, C, count(*) from Table group by grouping sets( (A, B, C), (A, B), (A), () )
The grouping()
function is an aggregate function that accepts a single column as a parameter and returns 1
if the column is aggregated (not a grouping column), and 0
otherwise. This aggregate function can be used as a reliable way to determine which grouping set the row represents.
The MATCH operates differently on range views and template views. You can MATCH column names when using range views, but not template views. You can MATCH on tables created by both range and template views.
When the MATCH operator is used with range views, column names are bound to their corresponding index references and searchable fields are bound to their field names. When the MATCH operator is applied to individual columns, all names are unbound, as it doesn't make sense to constrain searches against one index to the values of another. These queries are executed in unfiltered mode.
The search expression following the MATCH operator must be contained inside single quotes.
Field names, like view and schema names, are treated as case-insensitive for the purposes of duplicate detection and lookup.
The following table lists the search grammar that can be used by the MATCH operator.
Type | Token |
---|---|
Wildcards* | ? % * |
Boolean Operators | AND, OR, NOT, NOT_IN, NEAR/integer |
Comparison Operators | EQ, NE, LT, LE, GT, GE |
Name Binding** | <field_name>:<value>, <column_name>:<value> |
* To use wildcards in a search expression, you must enable trailing wildcard searches
and word lexicons
(codepoint collation
) on your database.
** Searches are constrained to the named field or column values. The field or column text must have the correct case. For example, 'Position:Manager'
is not the same as 'position:Manager'
. Because you cannot specify fields in a template view, you cannot MATCH on field names.
The following queries will work on both range views and template views:
SELECT * FROM employees WHERE employees MATCH 'Manager' SELECT * FROM employees WHERE employees MATCH 'J*' SELECT employeeid, firstname, lastname, position FROM employees WHERE employees MATCH 'Steve OR John OR Goodall' SELECT employeeid, firstname, lastname, position FROM employees WHERE employees MATCH 'Steve AND Manager' SELECT * from employees WHERE firstname MATCH 'John OR Jane' AND lastname MATCH 'Lead'
The following queries will work on range views only:
SELECT * FROM employees WHERE employees MATCH 'position:Manager' SELECT firstname, lastname FROM employees WHERE employees MATCH 'employeeid LE 3' SELECT employeeid, firstname, lastname, position FROM employees WHERE firstname MATCH 'Steve OR John OR Goodall' SELECT * FROM employees WHERE employees MATCH 'firstname:J*'
The MarkLogic ODBC driver supports Postgres SET and SHOW run-time configuration parameters, as well as some parameters that are specific to MarkLogic Server. These parameters only work when accessing MarkLogic through an ODBC driver, as is the case with mlsql
. They do not work when accessing MarkLogic through xdmp:sql or the Query Console.
For details on the Postgres parameters, see:
All SET parameters are good for the duration of the SQL session in which they are set. Some parameters are read-only and can only be specified by the SHOW statement. These are described in Read-only SHOW Parameters.
All SET string values must be specified in single quotes (SET
parameter '
value'
).
Sets the timezone offset to that for the given timezone name. The standard permitted formats and keywords can be used.
For example, to set the timezone to UTC, enter:
SET timezone 'UTC'
Sets the timeout for statement execution (milliseconds).
SET statement_timeout 5000
Sets the default collation in the dynamic environment.
The form we will see from the Postgres client is:
SET lc_collate 'en_US.utf8'
This maps to the collation: http://marklogic.com/collation/en_US
You can also specify a full collation string:
SET lc_collation 'http://marklogic.com/collation/en_US/S1/MO'
Sets the number of digits displayed for floating point types.
SET extra_float_digits 2
Declares the encoding of data coming from the client.
SET client_encoding 'UTF8'
SET NAMES is the standard syntax for the same thing.
SET NAMES 'UTF8'
Set the default coordinate system for geospatial operations.
SET coordinate_system 'wgs84/double'
For more details, see The Governing Coordinate System and Controlling Coordinate System and Precision in the Search Developer's Guide.
Sets the default schema referenced by names in SQL statements.
SET search_path 'main'
Enable or disable the redundant check on normal (on full-text) query constraints on rows. Value is 1 (enable) or 0 (disable). The default is 0.
SET mls_redundant_check 1; SELECT title, year FROM songs WHERE year=1991;
The following parameters can be obtained via the SHOW statement but they are read-only and cannot be set via the SET statement.
MarkLogic SQL does not have a default/implicit limit for the rows returned. Queries that return large result sets, such as tens of thousands of rows, may perform poorly. Should you experience performance problems it is a best practice to page the results using the LIMIT
statement.