Other Functions¶
Other functions:
COALESCE¶
Description
The COALESCE
function returns the first non-null argument.
COALESCE
is equivalent to the expression:
CASE WHEN arg1 IS NOT NULL THEN arg1
WHEN arg2 IS NOT NULL THEN arg2
…
END
Syntax
COALESCE( <field name:identifier>, <field name:identifier> [, <field name:identifier> ]*)
field name
. Text or field name which can be NULL.
Examples
Consider the view called V
:
a |
b |
c |
---|---|---|
10.10 |
I am some text |
21-ene-2005 0h 0m 0s |
-80.10 |
Text is $% needed always |
12-mar-2005 12h 30m 0s |
20.50 |
Text for a living |
01-feb-2006 16h 45m 0s |
40.05 |
NULL |
NULL |
Example 1
SELECT COALESCE(b, 'hello')
FROM V
coalesce |
---|
I am some text |
Text is $% needed always |
Text for a living |
hello |
Example 2
SELECT coalesce('hello', 'bye')
FROM v
coalesce |
---|
hello |
hello |
hello |
hello |
Example 3
SELECT COALESCE(b, a)
FROM V
coalesce |
---|
I am some text |
Text is $% needed always |
Text for a living |
40.05 |
CONTEXTUALSUMMARY¶
Description
The CONTEXTUALSUMMARY
function returns relevant text fragments of a
text, containing the word or sentence specified.
Syntax
CONTEXTUALSUMMARY( <content:text>, <keyword:text>, [ <begin delim:text>,
<end delim:text>, <fragment separator:text>, <fragment length:int>
[, <max fragments number:int> [, <analyzer:text> ] ] ] )
content
: Required. Text that the most relevant fragments are to be extracted from.keyword
: Required. Keyword used to extract the text fragments. The content of this argument can be a single word or a sentence.begin delim
: Optional. Text to add as prefix of the keyword whenever it appears in the text. Default value is “”.end delim
: Optional. Text to add as suffix of the keyword whenever it appears in the text. Default value is “”.fragment separator
: Optional. Text to separate each text fragment of the result. Default value is “…”.fragment length
: Optional. Approximate number of characters that will appear before and after the keyword occurrences inside of the text. Default value is 5.max fragment number
: Optional. Maximum number of fragments to retrieve.analyzer
: Optional. Analyzer used to search for keywords. By default, the Standard Analyzer (std
) is used. This analyzer does not consider lemmatization or stopwords. Virtual DataPort also includes analyzers for English (en
) and Spanish (es
).
Examples
Example 1
SELECT CONTEXTUALSUMMARY(content, 'Denodo', '<b>', '</b>', ' … ', 5, 1)
FROM demo_arn_view;
This query will return fragments of text content
where the “Denodo”
word appears.
Example 2
Consider the following view text_summary_sample
:
text_sample |
---|
A web service (also webservice) is defined by the W3C as a software system designed to support interoperable machine-to-machine interaction over a network. It has an interface described in a machine-processable format (specifically Web Services Description Language WSDL). Other systems interact with the web service in a manner |
prescribed by its description using SOAP messages, typically conveyed using HTTP with an XML serialization in conjunction with other web-related standards. Web services are frequently just Internet Application Programming Interfaces (API) that can be accessed over a network, such as the Internet, and executed on a remote system hosting the requested services. Other approaches with nearly the same functionality |
as web services are Object Management Group’s (OMG) Common Object Request |
Broker Architecture (CORBA), Microsoft’s Distributed Component Object Model |
(DCOM) or Sun Microsystems’s Java/Remote Method Invocation (RMI). |
SELECT contextualsummary(text_sample, 'system')
FROM text_summary_sample
contextualsummary |
---|
system |
system |
Example 3
SELECT contextualsummary(text_sample, 'service')
FROM text_summary_sample
contextualsummary |
---|
service … service |
Example 4
SELECT contextualsummary(text_sample, 'web', '\', '/', '--', 25)
FROM text_summary_sample
contextualsummary |
---|
A web/ service (also– (specifically Web/ Services– with the web/ service |
with other web/-related |
as web/ services |
GETVAR¶
The GETVAR
function obtains the value of a variable from the context of the query that uses this function. If the variable does not exist, it returns a default value.
The page Adding Variables to Selection Conditions (GETVAR and SETVAR) explains how to use this function.
Syntax
GETVAR('<name of the variable:literal>', '<type of the variable:literal>', '<default value:literal>')
GETSESSION¶
Description
The GETSESSION
function provides information about the session
established with a Virtual DataPort server.
Syntax
GETSESSION( <parameter : literal> )
parameter
can be one of the following (the value returned by the function depends on this parameter):user
: the function returns the user name of the current user.useragent
: the function returns the user agent associated to the current user’s connection.database
: the function returns the name of the Virtual DataPort database that the client is connected to.i18n
: the function returns the name of the i18N (internationalization) configuration of the database that the client is connected to.roles
: the function returns an array with the names of the effective roles assigned to the current user. The effective roles are the roles assigned directly and indirectly to the user. For example, if “user1” has the role “A” assigned and the role “A” has the role “B” assigned, this function returns “A” and “B”.
Example
SELECT GETSESSION('user') || '@' || GETSESSION('database') as user_name
FROM Dual();
user_name |
---|
user1@denodo_samples_db |
HASH¶
Description
The HASH
function calculates the digest of the input value, with the
algorithm MD5 and returns it encoded in base64 (not in base 16).
For the same input, this function always returns the same value.
It returns null when the input parameter is null.
Syntax
HASH( <value:text> ):text
value
: Required. The name of a field or a literal.
Example
SELECT hash('text value') as hash_value, hash(null) as null_value
FROM Dual()
hash_value |
null_value |
---|---|
eNV9lLZhJ1lex1ztvwnajg== |
NULL |
MAP¶
Description
The MAP
function returns the value associated with a key. The pair
key-value can be obtained from a view or from a map (see section Defining a Map). When the key does not exist, the function returns
NULL
.
There are two possible signatures:
Syntax 1
MAP ( <key:text>, <view name:text>, <key field:text>,
<value field:text> )
It obtains the value associated with a key. MAP searches the value of a key in the columns of a view.
key
. Required. The value to search in the view.view name
. Required. The name of the view that contains the key and its value.key field
. Required. The column of the view that contains the keys.value field
. Required. The column of the view that contains the values.
Syntax 2
MAP ( <key:text>, <map name:text> [, <i18n:text> ] )
It obtains the value associated with a key from a Map.
key
. Required. The value to search in the map.map name
. Required. The name of the map that contains the key and its value.i18n
. Optional. Internationalization configuration of the contents.
Note
In both cases, key
is a case-insensitive parameter.
Examples
Example 1
Consider the map food
:
CREATE MAP SIMPLE food (
'breakfast' = 'milk'
'dinner' = 'lettuce'
'lunch' = 'meat'
)
SELECT MAP('breakfast', 'food') AS breakfast
, MAP('lunch', 'food') AS lunch
, MAP('dinner', 'food') AS dinner
, MAP('none', 'food') AS none
FROM Dual()
breakfast |
lunch |
dinner |
none |
---|---|---|---|
milk |
meat |
lettuce |
NULL |
Example 2
Consider the view FOREIGN_SALES
that contains the revenue of a
company in each country, in the country’s currency.
country |
month |
revenue |
currency |
---|---|---|---|
Mexico |
JAN |
7536.00 |
MXN |
Spain |
JAN |
20000.00 |
EUR |
United Kingdom |
JAN |
26816.00 |
GBP |
Canada |
FEB |
-25616.00 |
CAD |
Japan |
FEB |
100024.00 |
JPY |
And the CURRENCY_RATES_TO_USD
map that contains the exchange rate of
each currency to dollar.
CREATE MAP SIMPLE currency_rates_to_usd (
'CAD' = '0.957121'
'EUR' = '1.4971'
'GBP' = '1.67'
'JPY' = '0.011166'
'MXN' = '0.076989'
'USD' = '1.0'
);
SELECT month
, country
, CAST('float', MAP(CURRENCY, 'currency_rates_to_usd')) * revenue AS revenue_usd
FROM foreign_sales
month |
country |
revenue_usd |
---|---|---|
JAN |
Mexico |
580.19 |
JAN |
Spain |
29942.00 |
JAN |
United Kingdom |
44782.72 |
FEB |
Canada |
-24517.61 |
FEB |
Japan |
1116.87 |
NULLIF¶
Description
The NULLIF
function compares two values or expressions and returns
NULL
if they are equal. Otherwise it returns the first value.
This function is equivalent to the statement:
CASE WHEN <expression> = <expression>
THEN NULL
ELSE <expression>
END
NULLIF
performs implicit type conversion. That is, if the two
parameters have different type, it will try to cast one of them in order
to make the comparison.
I.e.: if the first parameter is 1
(text
) and the second is
1
(integer
), it will convert the text
parameter to an
integer and they will be considered equal even if their type is
different.
Syntax
NULLIF(<expression>, <expression>)
Examples
Consider the view internet_inc
:
id |
summary |
ttime |
taxid |
---|---|---|---|
1 |
Error in ADSL router |
2005-06-29 19:19:41.0 |
B78596011 |
2 |
Incident in ADSL router |
2005-06-29 19:19:41.0 |
B78596012 |
3 |
Install additional line |
2005-06-29 19:19:41.0 |
B78596013 |
4 |
Bandwidth increase |
2005-06-29 19:19:41.0 |
B78596014 |
Example 1
SELECT NULLIF(id, 1) AS display
FROM internet_inc
display |
---|
NULL |
2 |
3 |
4 |
Example 2
SELECT *
FROM internet_inc
WHERE NULLIF(ID, 1) <> NULL
id |
summary |
ttime |
taxid |
---|---|---|---|
2 |
Incident in ADSL router |
2005-06-29 19:19:41.0 |
B78596012 |
3 |
Install additional line |
2005-06-29 19:19:41.0 |
B78596013 |
4 |
Bandwidth increase |
2005-06-29 19:19:41.0 |
B78596014 |
The first row of the view does not match the condition.
Example 3
SELECT COALESCE(NULLIF(ID, '1'), summary) AS display
FROM internet_inc
display |
---|
Error in ADSL router |
2 |
3 |
4 |
Note
NULLIF
has automatically converted the second parameter to
an integer to compare it with the values of the column ID which are also
integers.
PIVOTREGISTER¶
Description
The PIVOTREGISTER
function transforms the provided array values to columns
with the provided names.
There are two possible signatures:
Syntax 1
PIVOTREGISTER ( <values:array>, <column names:array>)
It transforms the provided array values to columns. The result columns always be of type text.
values
. Required. The values to transform to columns.column names
. Required. The names of the new columns.
Syntax 2
PIVOTREGISTER ( <values:array>, <column names:text>)
It transforms the provided array values to columns.
values
. Required. The values to transform to columns.column names
. Required. The names of the new columns. It is a text containing the column names and, optionally, the types separated by commas. The syntax must be: field1:type1 [,field2:type2, …]. Type should be one of the following: string, double, float, integer, long, boolean, date, time, timestamp and timestamptz.
Note
If you only specify the column names as a comma-separated text without types, the column types will be text by default.
Example
Consider the table phone_array_table
:
name |
phone_numbers |
---|---|
Smith |
[Array] … |
Willson |
[Array] … |
Stan |
[Array] … |
Jonhson |
[Array] … |
McNamara |
[Array] … |
Richardson |
[Array] … |
Array Example:
value |
---|
666-987-8549 |
555-125-7841 |
We want to transform the two phone_numbers
rows into two columns:
home_phone_number
and office_phone_number
. To do this, we apply the PIVOTREGISTER
function over the phone_numbers
column, so it returns a record with the properties
home_phone_number
and office_phone_number
and the values of the phone_numbers
column:
CREATE OR REPLACE VIEW phone_table_aux AS
SELECT name,
PIVOTREGISTER(phone_numbers, { ROW ('home_phone_number'), ROW ('office_phone_number') })
FROM phone_array_table;
or
CREATE OR REPLACE VIEW phone_table_aux AS
SELECT name,
pivotregister(phone_numbers, 'home_phone_number:string,office_phone_number:string')
FROM phone_array_table;
name |
pivotregister |
---|---|
Smith |
[Register] … |
Willson |
[Register] … |
Stan |
[Register] … |
Jonhson |
[Register] … |
McNamara |
[Register] … |
Richardson |
[Register] … |
RESULT -> pivotregister |
|
---|---|
home_phone_number |
office_phone_number |
666-987-8549 |
555-125-7841 |
Finally, we can created a derived view over phone_table_aux
and project the fields of
the PIVOTREGISTER
record of the view, and we will get the pivot result:
name |
home_phone_number |
office_phone_number |
---|---|---|
Smith |
666-987-8549 |
555-125-7841 |
Willson |
666-907-8009 |
555-105-7001 |
Stan |
666-957-8509 |
555-155-7501 |
Jonhson |
666-947-8409 |
555-145-7401 |
McNamara |
666-927-8209 |
555-125-7302 |
Richardson |
666-937-8309 |
555-135-7301 |
This function can be used to transform JSONs data sources, created for example from Google Sheets or Google Analytics sources, in tables. In this case, it may be possible that the first element of the JSON was the header of the table. If we want to specify the column types, the first tuple might be null, since the header has text values.
Example:
- We want to transform this JSON in a table with six columns:
Date of type timestamptz.
C1 of type boolean.
C2 of type double.
C3 of type time.
C4 of type integer.
C5 of type integer.
{
"range": "'Sheet 1'!A1:E36",
"majorDimension": "ROWS",
"values": [
[
"Date",
"c1",
"c2",
"c3",
"c4",
"c5"
],
[
"2020-10-10T01:59:59+01:00",
"true",
"43.58",
"21:15:45",
"421",
"23"
],
[
"2020-10-11T01:59:59+01:00",
"true",
"43.79",
"21:15:45"
],
[
"2020-10-12T01:59:59+01:00",
"false",
"44.74",
"21:15:45"
],
[
"2020-10-13T01:59:59+01:00",
"true",
"45.10",
"21:15:45"
],
[
"2020-10-14T01:59:59+01:00",
"false",
"46.01",
"21:15:45",
"486",
"23"
]
]
}
After creating the data source using this JSON we create the base view
bv_pivot_json
. Then, we need to create the derived view dv_pivot_json
to
flatten the values
array.
In order to transform the array values in columns with the wanted types, we are
going to apply the PIVOTREGISTER
function:
CREATE OR REPLACE view pivot_json_aux AS
SELECT pivotregister(values, 'Date:timestampz, C1:boolean, C2:double, C3:time,
C4:long, C5:integer')
FROM dv_pivot_json
To avoid having a null row, we can number the rows of the table. To do this, we have
to add a new column, rownum
, to the view in which the record subfields are projected.
This new column is a call to the rownum()
function:
rownum |
Date |
C1 |
C2 |
C3 |
C4 |
C5 |
---|---|---|---|---|---|---|
1 |
<null> |
<null> |
<null> |
<null> |
<null> |
<null> |
2 |
2020-10-09 17:59:-07:00 |
true |
43.58 |
21:15:45 |
421 |
23 |
3 |
2020-10-10 17:59:-07:00 |
true |
43.79 |
21:15:45 |
<null> |
<null> |
4 |
2020-10-11 17:59:-07:00 |
false |
44.74 |
21:15:45 |
<null> |
<null> |
5 |
2020-10-12 17:59:-07:00 |
true |
45.1 |
21:15:45 |
<null> |
<null> |
6 |
2020-10-13 17:59:-07:00 |
false |
46.01 |
21:15:45 |
486 |
23 |
Now, we just have to create a derived view which removes the first row. To do this, we need to add the following where condition:
rownum <> 1
Remember to remove the rownum
field in the Output tab of the derived view and we
will have the result view:
Date |
C1 |
C2 |
C3 |
C4 |
C5 |
---|---|---|---|---|---|
2020-10-09 17:59:-07:00 |
true |
43.58 |
21:15:45 |
421 |
23 |
2020-10-10 17:59:-07:00 |
true |
43.79 |
21:15:45 |
<null> |
<null> |
2020-10-11 17:59:-07:00 |
false |
44.74 |
21:15:45 |
<null> |
<null> |
2020-10-12 17:59:-07:00 |
true |
45.1 |
21:15:45 |
<null> |
<null> |
2020-10-13 17:59:-07:00 |
false |
46.01 |
21:15:45 |
486 |
23 |
ROWNUM¶
Description
The ROWNUM
function returns a unique number for each row of the
result of a query.
This number is unique for each query even if the data were retrieved from different sources.
The ORDER BY
clause is processed after ROWNUM
assigns a value to
each row.
For consistent results between several executions of a query, use
ROWNUM()
over a view with the ORDER BY
clause.
Syntax
ROWNUM( [ <offset:long > ] ):long
offset
. Optional. If present, the first value returned byROWNUM
is (offset + 1
), instead of1
.
Examples
Consider the view internet_inc
:
id |
summary |
ttime |
taxid |
---|---|---|---|
1 |
Error in ADSL router |
2005-06-29 19:19:41.0 |
B78596011 |
2 |
Incident in ADSL router |
2005-06-29 19:19:41.0 |
B78596012 |
3 |
Install additional line |
2005-06-29 19:19:41.0 |
B78596013 |
4 |
Bandwidth increase |
2005-06-29 19:19:41.0 |
B78596014 |
Example 1
SELECT rownum(10), summary, taxid
FROM internet_inc
rownum |
summary |
taxid |
---|---|---|
11 |
Error in ADSL router |
B78596011 |
12 |
Incident in ADSL router |
B78596012 |
13 |
Install additional line |
B78596013 |
14 |
Bandwidth increase |
B78596014 |
Example 2
SELECT ROWNUM(), summary, taxid
FROM internet_inc
ORDER BY summary
rownum |
summary |
taxid |
---|---|---|
4 |
Bandwidth increase |
B78596014 |
1 |
Error in ADSL router |
B78596011 |
2 |
Incident in ADSL router |
B78596012 |
3 |
Install additional line |
B78596013 |
Note that the query of “Example 2” sorts the results by the field
summary
. As the ORDER BY
clause is processed after the
ROWNUM
function assigns a value to each row, the first value of
ROWNUM
is not 1
.