SQream SQL Reference Guide Ver 1-9-2
SQream SQL Reference Guide Ver 1-9-2
1. SQL Reference
This is the reference guide for the SQL statements supported by SQream. For a conceptual overview
and recommendations on best practices regarding SQream internal behaviour, please refer to SQream
User Administrator Guide.
CREATE DATABASE
CREATE DATABASE will create a new database in the current database storage set. To use the new
database you can use the utility function 'use_database' or disconnect and reconnect to the new
database.
create_database_statement ::=
A database storage set is the set of files on disk which represent a set of databases
Examples
DROP DATABASE
DROP DATABASE will delete a database and all its files. Any connections open to this database will
subsequently fail.
1
drop_database_statement ::=
Examples
1.1.2. Users
CREATE USER
create_user_statement ::=
DROP USER
drop_user_statement ::=
ALTER USER
alter_user_statement ::=
2
1.1.3. Schemas
CREATE SCHEMA
create_schema_statement ::=
Examples
DROP SCHEMA
drop_schema_statement ::=
Examples
1.1.4. Tables
CREATE TABLE
CREATE TABLE creates a new table in the current database under a specific schema.
3
create_table_statement ::=
column_constraint ::=
default ::=
DEFAULT default_value
Identity columns are only supported for columns of type TINYINT, SMALLINT, INT and BIGINT.
compression_type ::=
When compression is needed, SQream recommends using the default compression type with: check
('CS "default"')
4
partitioning ::=
timeframe ::=
5
Examples
create table t (
a int identity (1,1) check ('CS "default"'),
b int
);
create table u (
a int default 0,
b int,
c date
);
create table u (
k int not null identity,
v varchar(10)
);
create table u (
k bigint not null identity(1,1),
v varchar(10)
);
ALTER TABLE
Rename table
6
alter_table_statement_rename_table ::=
Examples
Rename column
This form of alter table allows you to rename a column in an existing table.
alter_table_statement_rename_column ::=
Examples
Drop column
This form of alter table allows you to drop a column from an existing table.
alter_table_statement_rename_table ::=
Examples
DROP TABLE
Dropping a table without explicit schema_name, will drop the table under PUBLIC
schema.
7
drop_table ::=
Examples
1.1.5. Views
CREATE VIEW
create_view_statement ::=
Examples
DROP VIEW
drop_view_statement ::=
Examples
1.1.6. DROP_PARTITION
DROP_PARTITION is used to delete all the rows in that partition. To see the list of existing partitions,
use the command: select list_partitions('table_name')
8
CREATE PARTITION is being done automatically by SQream while inserting data
into a partitioned table.
drop_partition_statement ::=
Examples
1.2.1. INSERT
insert_statement ::=
Examples
When the insert statement does not include all the columns in the table, columns
which aren’t explicitly mentioned with get their default values (string/number,
NULL or identity)
9
1.2.2. COPY FROM (bulk import)
COPY FROM is used to quickly insert CSV data into a table. It is the recommended way to insert data in
production.
copy_from_statement ::=
The filename read from must be a path available to the sqreamd process on the server machine.
The delimiter default is ',' and it must be a single character. You cannot set the record delimiter, it is
always '\n'.
PARSERS 'dt=fixed_datetime' will stop the parser from doing 'date' validations. Use this option only if
you certain the date/datetime columns on your CSV are in the above formats:
Adding the syntax PARSERS 'dt=fixed_datetime' without caution, can damage your
data.
Error verbosity:
Examples
10
copy_to_statement ::=
The TRUNCATE TABLE command deletes all the rows from a table.
truncate_statement ::=
Using RESTART IDENTITY will reset the identity columns to their starting values. CONTINUE
IDENTITY is the default.
Examples
1.3. Queries
Queries are used to retrieve data from the current database.
11
query_term ::=
SELECT
[ TOP num_rows ]
[ DISTINCT ]
select_list
[ FROM table_ref [, ... ]
[ WHERE value_expr ]
[ GROUP BY value_expr [, ... ]
[ HAVING value_expr ]
]
]
|
VALUES ( value_expr [, ... ] ) [, ... ]
select_list ::=
table_ref ::=
join_type ::=
join_hint ::=
MERGE | LOOP
order ::=
12
1.3.1. SELECT lists
TOP will be the last operation on the query execution. This means that SQream will
limit the results to the end-user after executing the entire statement.
Value expressions in select lists support aggregate and window functions as well as normal value
expressions (see below).
Examples
select * from t;
select 1 + a from t;
select a as b from t;
1.3.2. FROM
FROM is used to specify which tables to read in a query. You can also put complete subqueries in the
from clause.
13
Examples
select * from t;
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
SELECT *
FROM table1,table2
WHERE table1.column_name=table2.column_name;
Join hints can be used to override the query compiler and choose a particular join algorithm. The
available algorithms are LOOP (corresponding to non-indexed nested loop join algorithm), and
MERGE (corresponding to sort merge join algorithm).
SELECT *
FROM table1
INNER MERGE JOIN table2
ON table1.column_name=table2.column_name;
SELECT *
FROM table1
INNER LOOP JOIN table2
ON table1.column_name=table2.column_name;
1.3.3. WHERE
14
Examples
SELECT Column1
FROM table1
WHERE column2 <= 1;
1.3.4. GROUP BY
GROUP BY is used to partition a table so that aggregates can be applied separately to each partition.
Examples
1.3.5. HAVING
Examples
1.3.6. ORDER BY
Examples
1.3.7. VALUES
UNION is used to concatenate two queries together. The only UNION supported is UNION ALL, which
doesn’t remove duplicate rows.
15
Examples
select * from t
union all
select * from u;
The global query hints can be used to select algorithms for reduce which implements GROUP BY
aggregates.
Examples
type_name ::=
BOOL
| INT
| BIGINT
| FLOAT/DOUBLE
| REAL
| DATE
| DATETIME
| VARCHAR ( digits )
1.4.1. Boolean
16
Type Description Uncompressed size
Examples
1.4.3. Date/time
17
Type Description Uncompressed size
VARCHAR datatype is intended to ASCII character set. The maximum size of a VARCHAR is 2048.
Varchars are padded with spaces. SQream ignores the trailing whitespaces in functions, and compress
them in storage level.
The only supported character set/encoding for VARCHAR datatype is ASCII, and the
only collation is ASCII byte order/ byte equality.
18
value_expr ::=
string_literal
| number_literal
| typed_literal
| unary_operator value_expr
| value_expr postfix_unary_operator
| special_operator
| extract_operator
| case_expression
| conditional_expression
| ( value_expr )
| identifier
| star
| function_app
| aggregate_function_app
| window_function_app
| cast_operator
string_literal is delimited by single quotes ('), and can contain any printable character other than
single quote. To include a single quote in a string use two quotes together.
19
Examples
'string literal'
number_literal ::=
digits
Examples
1234
1234.56
12.
.34
123.56e-45
typed_literal ::=
type_name string_literal
20
<strong><em>binary_operator</em></strong> ::=
unary_operator ::=
+ | - | NOT
postfix_unary_operator ::=
special_operator ::=
Note: to use AND in the middle value_expr in a BETWEEN operator, use parentheses like this:
The extract operator can be used to extract parts of dates/times from date or datetime values.
21
extract_operator ::=
extract_field ::=
YEAR
| MONTH
| DAY
| HOUR
| MINUTE
| SECOND
| MILLISECOND
| DOW
| DOY
| QUARTER
22
case_expression ::=
searched_case | simple_case
searched_case ::=
simple_case ::=
CASE value_expr
WHEN value_expr THEN value_expr
[WHEN ...]
[ELSE value_expr]
END
• each WHEN value_expr is checked in order, the value of the CASE expression is the value of the
THEN value_expr then for the first WHEN branch which evaluates to true;
• if no WHEN branches evaluate to true, then the value is the value of the ELSE expression, or if
there is no ELSE, then the value is NULL.
CASE v0
WHEN v1 THEN r1
WHEN v2 THEN r2
...
ELSE e
END
->
CASE
WHEN v0 = v1 THEN r1
WHEN v0 = v2 THEN r2
...
ELSE e
END
23
1.5.10. Identifier
identifier is
• unquoted identifier: any letter, underscore or digit, and the first character must be a letter or
underscore;
• quoted identifier: delimited by double quotes, any printable character other than double quotes. To
include a double quotes, use two double quotes next to each other.
aggregate_function_app ::=
window_function_app ::=
This table lists the operators in decreasing order of precedence. We recommend using parentheses
rather than relying on precedences in anything other than trivial expressions.
Operator Associativity
. left
+ - (unary)
^ left
*/% left
24
Operator Associativity
+ - (binary) left
|| right
NOT
AND left
OR left
The NOT variations: NOT BETWEEN, NOT IN, NOT LIKE, NOT RLIKE have the same precedence as
their non-NOT variations.
cast_operator ::=
There is also a system of implicit casting, where the system will insert a cast automatically.
bigint implicit
real implicit
float implicit
bool explicit
datetime none
date none
varchar explicit
25
From type To Type Context
bigint n/a
real implicit
float implicit
bool explicit
datetime none
date none
varchar explicit
bigint explicit
real n/a
float implicit
bool none
datetime none
date none
varchar explicit
bigint explicit
real explicit
float n/a
bool none
datetime none
date none
varchar explicit
26
From type To Type Context
bigint explicit
real explicit
float explicit
bool n/a
datetime none
date none
varchar explicit
bigint none
real none
float none
bool none
datetime implicit
date n/a
varchar explicit
bigint none
real none
float none
bool none
datetime n/a
date implicit
varchar explicit
27
From type To Type Context
bigint explicit
real explicit
float explicit
bool explicit
datetime explicit
date explicit
varchar explicit
* there are conversion functions to convert part of a date or datetime to integer, or to convert the
whole date/datetime to unix timestamps but these are not considered casts or available using the
cast syntax
** string literals without an explicit type are considered unknown type (and not varchar) and will
implicitly cast to any type.
Assignment resolution
This is a different kind of implicit cast which applies when you are inserting one type of expression
into a column with a different type. The casting rules are essentially the same as the implicit casting in
value expressions for the equals operator.
The last system of implicit casts is used to resolve the type of a collection of expressions with different
types which should resolve to a single compatible type.
• in list values
• join keys
• union columns
1.5.15. Operators
28
Logical
AND
Logical and.
Examples
OR
Logical or.
Examples
a OR b
NOT
Logical not.
Examples
NOT TRUE
29
Comparison
< > <= >= == != (any, any) returns bool regular binary comparison
operations
between (exp any, min any, max any) is exp between min and max
returns bool inclusive
not between (exp any, min any, max any) inverse of between
returns bool
<em>binary_comparison_operator</em> (<strong><em>any</em></strong>,
<strong><em>any</em></strong> ) <strong>returns</strong> <strong>bool</strong>
Regular binary comparison operators. The two input types should be the same, but the system will
insert valid implicit casts in many cases (see the cast section).
exp between min and max is shorthand for exp >= min and exp <= max.
exp not between min and max is shorthand for not (exp between min and max).
Examples
a between b and c
30
IS NOT NULL checks if the argument isn’t null.
Testing for null using exp = NULL will not work to check if a value is null, and testing
for not null using exp <> NULL will not work to check if a value is not null. You have
to use the IS NULL and IS NOT NULL operators.
Examples
(1 + null) is null
(a * b) is not null
IN
Examples
a in (1,3,5,7,11)
SQRT
Returns Float/Double
ABS
Parameters Float/Double (All other numbers available via implicit casting) Returns Float/Double
ROUND
31
SELECT ROUND (cfloat,2) FROM table
Parameters Float/Double (All other numbers available via implicit casting) Int32T Precision (number
of places after the decimal point)
Returns Float/Double
ASIN
ASIN sin-1 (x) – Arcsine (angle in radians whose sine is the argument of the function)
Parameters Float/Double (All other numbers available via implicit casting) Returns Float/Double
ATAN
ATAN tg-1 (x)- Arctangent (angle in radians whose tangent is the argument of the function)
Returns Float/Double
ATN2 – Arctangent
ATN2 (angle in radians between positive X-axis and the ray from the origin where x and y are the first
and second arguments)
Parameters Float/Double (All other numbers available via implicit casting) Float/Double (All other
numbers available via implicit casting)
Returns Float/Double
COS
32
Parameters Float/Double (All other numbers available via implicit casting)
Returns Float/Double
COT
Returns Float/Double
CEILING
Returns Float/Double
LOG10
Returns Float/Double
LOG
Returns Float/Double
33
LOG (base-y)
LOG base=y - Base-y logarithm of the x parameter, where x,y are the arguments
Parameters Float/DoubleArgument (all other numbers available via implicit casting) Integer Base
Returns Float/Double
FLOOR
Returns Float/Double
SIN
Returns Float/Double
SQUARE
Returns Float/Double
TAN
34
SELECT tan(cfloat) FROM t
Returns Float/Double
PI
PI - mathematical constant
Parameters none
POWER
POWER - perform a power of one value over the other (x raised to the power of y)
Parameters Float/Double (All other numbers available via implicit casting) Float/Double (All other
numbers available via implicit casting)
Returns Float/Double
TO_HEX
LOWER
35
SELECT LOWER (varchar_column) FROM table
UPPER
LEN
Remarks Trailing whitespace on the right are ignored: LEN on ‘abc’ and ‘abc ‘ will both return 3.
LIKE
RLIKE
Parameters VarChar
Returns Boolean
SUBSTRING
Parameters start - the starting point of the substring, while the value 1 represent the first character.
36
length - the length of the substring
Remarks If start ⇐1, then the substring begins from the first character but the length is reduced.
substring(1,2,’abc’) == ‘ab’ substring(0,2,’abc’) == substring(1,1,’abc’) == ‘a’
REGEXP_COUNT
REGEXP_COUNT - Counts regex matches in string. For example, the pattern ‘[1-9]’ appears once in ‘01’
and twice in ‘12’.
Parameters varchar_column - the string column to match VarChar - the regex (literal only) Int - starting
location (Optional. When unset, default is 1)
Returns Int
REGEXP_INSTR
Parameters varchar_column - the string column to match VarChar - the regex (literal only) Int - starting
location (Optional. When unset, default is 1) Int - which occurence of the pattern (Optional. When
unset, default is 1) Int - 0 to return the match’s start position, 1 for its end (Optional. When unset,
default is 0)
REGEXP_SUBSTR
Parameters varchar_column - the string column to match VarChar - the regex (literal only) Int - starting
location (Optional. When unset, default is 1) Int - which occurence of the pattern (Optional. When
unset, default is 1)
37
Returns VarChar vector (string column of the matches)
ISPREFIXOF
Remarks Internal function. “isprefix(x,y)” is equivalent to “y LIKE x + ‘%’ “, but more efficient
Concatenation (||)
CHARINDEX
Parameters VarChar -the subexpression. Either a scalar or a column VarChar -the expression Int
(optional) -starts the search from this index
Returns Int - the position of the subexpression in the expression or 0 if it wasn’t found
PATINDEX
Returns Int -the position of the first match of the pattern in the expression or 0 if there’s no match
LTRIM
LTRIM - Trims trailing whitespace from the left side of the string
38
Parameters VarChar
Returns VarChar
REVERSE
Parameters VarChar
Returns VarChar
RTRIM
RTRIM - Trims trailing whitespace from the right side of the string
Parameters VarChar
Returns VarChar
Syntax Description
Syntax Description
39
Syntax Description
{2,4} Match the previous pattern between two and four times
[a-dX], [^a- Matches any character that is (or is not, if ^ is used) either a, b, c, d or X. A - character
dX] between two other characters forms a range that matches all characters from the first
character to the second. For example, [0-9] matches any decimal digit. To include a
literal ] character, it must immediately follow the opening bracket [. To include a literal
- character, it must be written first or last. Any character that does not have a defined
special meaning inside a [] pair matches only itself.
Date/time
40
Datepart Shorthand
aliases
year yyyy, yy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw
hour hh
minute n
second ss, s
millisecond ms
Geospatial
Point
Points are represented as longitude and latitude columns. Example:
Polygon
Polygons are N number of points:
...
Polyline
41
A polyline is a collection of line segments, and contains up to twenty points. We represent it as twenty
points plus a count column which indicates how many points are actually used in the given row.
...
polyline_long20 float,
polyline_lat20 float,
poly_long1 float, poly_lat1 float,
POINT_IN_POLYGON
Limitations: the point arguments cannot be literals. The polygon arguments can either be all columns
or all literals.
42
LINE_CROSSES_POLYGON
Limitations: the polyline arguments cannot be literals. The polygon arguments can either be all
columns or all literals.
any can be any type. anynumber is any numeric type: INT, BIGINT, REAL, FLOAT.
Name Type
Examples
select col_a,col_c, rank() over ( partition by col_c order by col_c) from my_table;
select sum(col_a) over ( partition by col_c order by col_c) from my_table;
43
1.6. Saved queries
Saved queries allow SQream to save the query plan for a query. Saved query will save the compiler
time on each execution, and therefor can help optimize the total query execution time.
Example
select * from t where xint > 1 AND xdate < '2015-10-03' AND xvarchar6 = 'sqream'
1.6.1. save_query
1.6.2. execute_saved_query
1.6.3. drop_saved_query
1.6.4. show_saved_query
44
SELECT show_saved_query ( saved_query_name ) ;
1.6.5. list_saved_queries
SELECT list_saved_queries ( ) ;
All SQream metadata can be seen via SQream Catalog. To access the catalog, query the internal views
sqream_catalog.<object type>
view_name Description
Example
45