SQL syntax for logical data warehouses
The logical data warehouse feature breaks down the barriers between online analytical processing (OLAP) and online transaction processing (TP) systems. These systems often use different engines, instances, regions, and storage. This feature provides cross-database data query and data synchronization capabilities based on a unified SQL syntax. This topic describes the SQL syntax supported by logical data warehouses.
Background information
The query syntax and functions for logical data warehouses are mostly compatible with MySQL. Therefore, you can write query statements and functions using MySQL syntax. The syntax is also extended to support the creation and deletion of logical views.
For more information, see Core features.
Scenarios
To quickly analyze your business data, you can define a logical view over raw data, such as tables in a MySQL database, and query it directly. For further analysis, you can also define new logical views that cascade from the existing view.
Logical views
Create a logical view
The syntax for creating a logical view is as follows:
CREATE VIEW <db_name>.<view_name> AS <query_statement>;Item | Description |
| The name of the DBLink. |
| The custom name of the logical view. |
| The SELECT statement:
|
Query a logical view
All logical views are saved in a public schema named public. Therefore, you must prefix the view name with public. when you query a logical view.
For example, to query a logical view named filtered_view, use the following syntax:
SELECT * FROM public.filtered_view;Delete a logical view
The syntax for deleting a logical view is as follows:
DROP VIEW public.view_name;View all logical views
To view all logical views in the public database, use the following syntax:
SHOW VIEWS public;Other
You can view the list of virtual databases.
To view all created virtual databases, including the public database, use the following syntax:
SHOW CATALOGS;Operators
Logical operators
Function | Description | Example |
AND | Logical AND |
|
OR | Logical OR |
|
NOT | Logical NOT |
|
Comparison operators
Function | Description | Example |
> | Greater than. |
|
< | Less than. |
|
= | Equal to. |
|
>= | Greater than or equal to. |
|
<= | Less than or equal to. |
|
<> | Not equal to. |
|
!= | Not equal to. |
|
is null | Checks if a value is null. |
|
is not null | Checks if a value is not null. |
|
is distinct from | A null-safe operator that compares two values. It avoids null results and always returns true or false. |
|
is not distinct from | A null-safe operator that compares two values. It avoids null results and always returns true or false. |
|
greatest(value1, value2, .., valuen) | Returns the greatest value from a list of values. | - |
least(value1, value2, .., valuen) | Returns the maximum value. | - |
all | Compares a single value with the return values of a subquery. |
|
any/some | Compares a single value with the return values of a subquery. |
|
like | Searches for a specified pattern in a column within a WHERE clause. |
Note
|
Numeric operators
Function | Description |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
Functions
Mathematical functions
Function | Return value type | Description | Example |
mod(x) | Same as the type of x by default | Modulo operation. |
|
pow(x,y) | Same as the type of x by default | Exponentiation. |
|
sqrt(x) | Same as the type of x by default | Square root. |
|
abs(x) | Same as the type of x by default | Absolute value. |
|
cbrt(x) | double | Cube root. |
|
ceil(x) | - | Alias for the ceiling function. | - |
ceiling(x) | Same as the type of x by default | Returns the smallest integer that is greater than the specified numeric expression. |
|
degrees(x) | double | Converts radians to degrees. |
|
e(x) | double | Returns Euler's constant. | - |
exp(x) | double | Returns Euler's constant raised to the power of x. | - |
floor(x) | Same as the type of x by default | Returns the largest integer that is smaller than x. |
|
from_base(string, radix) | bigint | Converts the value of a string into a base-radix number. | - |
ln(x) | double | Returns the natural logarithm of x. | - |
log2(x) | double | Returns the base-2 logarithm of x. |
|
log10(x) | double | Returns the base-10 logarithm of x. |
|
pi() | - | Returns the constant pi. | - |
power(x, p) | double | Returns x raised to the power of p. | - |
rand() | - | Alias for the random function. | - |
random() | double | Returns a pseudo-random value in the range of | - |
secure_rand() | - | Alias for the secure_random function. | - |
secure_random() | double | Returns a secure, encrypted random value in the range of | - |
secure_random(lower, upper) | - | Returns an encrypted, secure random value in the range of | - |
round(x) | Same as the type of x by default | Returns x rounded to the nearest integer. |
|
round(x, d) | Same as the type of x by default | Returns x rounded to d decimal places. |
|
sign(x) | Same as the type of x by default | Returns the sign of x.
|
|
to_base(x, radix) | varchar | Returns the string representation of x in base-radix. | - |
truncate(x) | double | Returns x with the decimal part truncated. |
|
truncate(x, n) | double | Returns x truncated to n decimal places. n can be a negative number to truncate n digits to the left of the decimal point. |
|
acos(x) | double | Returns the arc cosine of x. | - |
asin(x) | double | Returns the arc sine of x. | - |
atan(x) | double | Returns the arc tangent of x. | - |
atan2(y, x) | double | Returns the arc tangent of y/x. | - |
cos(x) | double | Returns the cosine of x. | - |
cosh(x) | double | Returns the hyperbolic cosine of x. | - |
sin(x) | double | Returns the sine of x. | - |
tan(x) | double | Returns the tangent of x. | - |
tanh(x) | double | Returns the hyperbolic tangent of x. | - |
infinity() | double | Returns the constant that represents positive infinity. | - |
is_finite(x) | boolean | Determines whether x is a finite value. | - |
is_infinite(x) | boolean | Determines whether x is an infinite value. | - |
is_nan(x) | boolean | Determines whether x is not a number (NaN). | - |
nan() | double | Returns the constant that represents not a number (NaN). | - |
String functions
Function | Return value type | Description | Example |
replace | varchar | Replace |
|
md5 | varchar | MD5 hash |
|
|| | varchar | String concatenation |
|
trim | varchar | Removes leading and trailing characters |
|
lower | varchar | Converts to lowercase |
|
upper | varchar | Converts to uppercase |
|
char_length | bigint | String length |
|
chr(n) | varchar | Returns the Unicode character at code point n as a single-character string. | - |
codepoint(string) | integer | Returns the Unicode code point of the single character in the string. | - |
concat(string1, ..., stringN) | varchar | A string concatenation function that connects two or more strings to form a new string. |
|
hamming_distance(string1, string2) | bigint | Returns the Hamming distance between string1 and string2, which is the number of positions at which the corresponding characters are different. Note The two strings must have the same length. | - |
left(string, length) | varchar | Returns a string that consists of the leftmost 'length' characters of the string. | - |
length(string) | bigint | Returns the length of the string. | - |
levenshtein_distance(string1, string2) | bigint | Returns the Levenshtein edit distance between string1 and string2. This is the minimum number of single-character edits (insertions, deletions, or substitutions) required to change string1 into string2. | - |
lower(string) | varchar | Converts the string to lowercase. |
|
lpad(string, size, padstring) | varchar | Pads the left side of the string with padstring to make the string 'size' characters long. If 'size' is smaller than the length of the string, the result is truncated to 'size' characters. 'size' cannot be negative, and padstring must not be empty. | - |
ltrim(string) | varchar | Removes leading spaces from the string. | - |
replace(string, search) | varchar | Removes all occurrences of 'search' from the string. | - |
replace(string, search, replace) | varchar | Replaces all occurrences of 'search' in the string with 'replace'. |
|
reverse(string) | varchar | Returns the reversed string. |
|
rpad(string, size, padstring) | varchar | Pads the right side of the string with padstring to make the string 'size' characters long. If 'size' is smaller than the length of the string, the result is truncated to 'size' characters. 'size' cannot be negative, and padstring must not be empty. | - |
rtrim(string) | varchar | Removes trailing spaces from the string. | - |
split(string, delimiter) | - | Splits the string by the delimiter and returns an array. | - |
split(string, delimiter, limit) | - | Splits the string by the delimiter and returns an array with a maximum size of 'limit'. | - |
strpos(string, substring) | bigint | Returns the starting position of the first occurrence of substring in the string. Traversal starts from the first character. If not found, it returns 0. | - |
strpos(string, substring, instance) | bigint | Returns the position of the nth occurrence of substring in the string. 'instance' must be a positive number. Traversal starts from the first character. If not found, it returns 0. | - |
strpos(string, substring) | bigint | Returns the starting position of the last occurrence of substring in the string. Traversal starts from the first character. If not found, it returns 0. | - |
strpos(string, substring, instance) | bigint | Returns the position of the nth occurrence of substring from the end of the string. 'instance' must be a positive number. The position is counted from the beginning of the string. If not found, it returns 0. | - |
position(substring IN string) | bigint | Returns the starting position of the first occurrence of substring in the string. Traversal starts from the first character. If not found, it returns 0. | - |
substring_index(str, delim, count) | varchar | Returns the substring from the string 'str' before 'count' occurrences of the delimiter 'delim'. |
|
substr | - | An alias for the substring function. | - |
substring(string FROM start) | varchar | Returns the substring of the string starting from the 'start' position. |
|
substring(string, start) | varchar | Returns the substring of the string starting from the 'start' position. | - |
substring(string, start, length) | varchar | Returns a substring of 'length' characters from the string, starting at the 'start' position. | - |
trim(string) | varchar | Removes leading and trailing spaces from the string. | - |
upper(string) | varchar | Converts lowercase letters in the string to uppercase letters. | - |
to_utf8(string) | varchar | Encodes the string into a UTF-8 binary representation. | - |
from_utf8(binary) | varchar | Decodes the binary string from UTF-8 format. Invalid UTF-8 characters are replaced with the default character U+FFFD. | - |
from_utf8(binary, replace) | varchar | Decodes the binary string from UTF-8 format. Invalid UTF-8 characters are replaced with a custom string. | - |
key_sampling_percent(Varchar) | varchar | Generates a double value between 0 and 1.0 based on the hash of the given string. | - |
Conditional expressions
Standard CASE expressions have two forms.
This form iterates through each value. When a value matches the expression, the statement returns the corresponding result. Otherwise, it returns the result from the ELSE branch.
CASE expression WHEN value1 THEN result1 [WHEN value2 THEN result2] [ELSE result3] ENDThis form iterates through each condition and returns the result for the first condition that evaluates to true.
CASE WHEN condition1 THEN result1 [WHEN condition2 THEN result2] [ELSE result3] ENDFunction
Description
Example
if(condition, true_value)
If the condition is true, it returns true_value. Otherwise, it returns NULL.
if(true, 1), returns 1.if(false, 1), returns NULL.
if(condition, true_value, false_value)
If the condition is true, it returns true_value. Otherwise, it returns false_value.
if(true, 1, 2), returns 1.if(false, 1, 2), returns 2.
coalesce(value1, value2[, ...])
Returns the first non-NULL value.
coalesce(NULL, NULL, 1), returns 1.ifnull(expr1, expr2)
If expr1 is not NULL, it returns expr1. Otherwise, it returns expr2.
ifnull(1,0), returns 1.ifnull(NULL,10), returns 10.
nullif(expr1, expr2)
If expr1 is equal to expr2, it returns NULL. Otherwise, it returns expr1.
-
try(expression)
Returns NULL if the expression has a syntax error. It can identify three types of errors:
Division by zero
Failed type conversion (cast) or invalid function parameters
Numeric value out of range
coalesce(try(total_cost / packages), 0)
Other functions
Function | Return value type | Description | Example |
json_extract(json, json_path) | - | Returns the value specified by json_path from a JSON string. The return value is of the JSON type. |
|
regexp_like(string, pattern) | boolean | Evaluates the regular expression 'pattern' and determines if it is contained within the string. |
|