This document provides an overview of the Asterix Query language.
Expression ::= ( OperatorExpr | IfThenElse | FLWOGR | QuantifiedExpression )
PrimaryExpr ::= Literal | VariableRef | ParenthesizedExpression | FunctionCallExpr | DatasetAccessExpression | ListConstructor | RecordConstructor
Literal ::= StringLiteral | <INTEGER_LITERAL> | <FLOAT_LITERAL> | <DOUBLE_LITERAL> | "null" | "true" | "false" StringLiteral ::= <STRING_LITERAL>
"a string" 42
VariableRef ::= <VARIABLE>
$id
ParenthesizedExpression ::= "(" Expression ")"
( 1 + 1 )
FunctionCallExpr ::= FunctionOrTypeName "(" ( Expression ( "," Expression )* )? ")"
string-length("a string")
DatasetAccessExpression ::= "dataset" ( ( Identifier ( "." Identifier )? ) | ( "(" Expression ")" ) ) Identifier ::= <IDENTIFIER> | StringLiteral
dataset customers dataset (string-join("customers", $country))
ListConstructor ::= ( OrderedListConstructor | UnorderedListConstructor ) OrderedListConstructor ::= "[" ( Expression ( "," Expression )* )? "]" UnorderedListConstructor ::= "{{" ( Expression ( "," Expression )* )? "}}" RecordConstructor ::= "{" ( FieldBinding ( "," FieldBinding )* )? "}" FieldBinding ::= Expression ":" Expression
[ "a", "b", "c" ] {{ 42, "forty-two", "AsterixDB!" }} { "project name" : "AsterixDB" "project members" : {{ "vinayakb", "dtabass", "chenli" }} }
ValueExpr ::= PrimaryExpr ( Field | Index )* Field ::= "." Identifier Index ::= "[" ( Expression | "?" ) "]"
{ "list" : [ "a", "b", "c"] }.list [ "a", "b", "c"][2] { "list" : [ "a", "b", "c"] }.list[2]
OperatorExpr ::= AndExpr ( "or" AndExpr )* AndExpr ::= RelExpr ( "and" RelExpr )*
$a > 3 and $a < 5
RelExpr ::= AddExpr ( ( "<" | ">" | "<=" | ">=" | "=" | "!=" | "~=" ) AddExpr )?
5 > 3
AddExpr ::= MultExpr ( ( "+" | "-" ) MultExpr )* MultExpr ::= UnaryExpr ( ( "*" | "/" | "%" | <CARET> | "idiv" ) UnaryExpr )* UnaryExpr ::= ( ( "+" | "-" ) )? ValueExpr
3 ^ 2 + 4 ^ 2
FLWOGR ::= ( ForClause | LetClause ) ( Clause )* "return" Expression Clause ::= ForClause | LetClause | WhereClause | OrderbyClause | GroupClause | LimitClause | DistinctClause ForClause ::= "for" Variable ( "at" Variable )? "in" ( Expression ) LetClause ::= "let" Variable ":=" Expression WhereClause ::= "where" Expression OrderbyClause ::= "order" "by" Expression ( ( "asc" ) | ( "desc" ) )? ( "," Expression ( ( "asc" ) | ( "desc" ) )? )* GroupClause ::= "group" "by" ( Variable ":=" )? Expression ( "," ( Variable ":=" )? Expression )* "with" VariableRef ( "," VariableRef )* LimitClause ::= "limit" Expression ( "offset" Expression )? DistinctClause ::= "distinct" "by" Expression ( "," Expression )* Variable ::= <VARIABLE>
for $user in dataset FacebookUsers where $user.id = 8 return $user
for $user in dataset FacebookUsers for $message in dataset FacebookMessages where $message.author-id = $user.id return { "uname": $user.name, "message": $message.message };
for $user in dataset FacebookUsers let $messages := for $message in dataset FacebookMessages where $message.author-id = $user.id return $message.message return { "uname": $user.name, "messages": $messages };
for $user in dataset TwitterUsers order by $user.followers_count desc, $user.lang asc return $user
for $x in dataset FacebookMessages let $messages := $x.message group by $loc := $x.sender-location with $messages return { "location" : $loc, "message" : $messages }
for $user in dataset TwitterUsers order by $user.followers_count desc limit 2 return $user
for $x in dataset FacebookMessages distinct by $x.sender-location return { "location" : $x.sender-location, "message" : $x.message }
IfThenElse ::= "if" "(" Expression ")" "then" Expression "else" Expression
if (2 < 3) then "yes" else "no"
QuantifiedExpression ::= ( ( "some" ) | ( "every" ) ) Variable "in" Expression ( "," Variable "in" Expression )* "satisfies" Expression
every $x in [ 1, 2, 3] satisfies $x < 3 some $x in [ 1, 2, 3] satisfies $x < 3
Statement ::= ( SingleStatement ( ";" )? )* <EOF> SingleStatement ::= DataverseDeclaration | FunctionDeclaration | CreateStatement | DropStatement | LoadStatement | SetStatement | InsertStatement | DeleteStatement | Query
DataverseDeclaration ::= "use" "dataverse" Identifier SetStatement ::= "set" Identifier StringLiteral FunctionDeclaration ::= "declare" "function" Identifier ParameterList "{" Expression "}" ParameterList ::= "(" ( <VARIABLE> ( "," <VARIABLE> )* )? ")"
use dataverse TinySocial;
set simfunction "jaccard"; set simthreshold "0.6f";
set simfunction "jaccard"; set simthreshold "0.6f";
declare function add($a, $b) { $a + $b };
CreateStatement ::= "create" ( DataverseSpecification | TypeSpecification | DatasetSpecification | IndexSpecification | FunctionSpecification ) QualifiedName ::= Identifier ( "." Identifier )? DoubleQualifiedName ::= Identifier "." Identifier ( "." Identifier )?
DataverseSpecification ::= "dataverse" Identifier IfNotExists ( "with format" StringLiteral )?
create dataverse TinySocial;
TypeSpecification ::= "type" FunctionOrTypeName IfNotExists "as" TypeExpr FunctionOrTypeName ::= QualifiedName IfNotExists ::= ( "if not exists" )? TypeExpr ::= RecordTypeDef | TypeReference | OrderedListTypeDef | UnorderedListTypeDef RecordTypeDef ::= ( "closed" | "open" )? "{" ( RecordField ( "," RecordField )* )? "}" RecordField ::= Identifier ":" ( TypeExpr ) ( "?" )? TypeReference ::= Identifier OrderedListTypeDef ::= "[" ( TypeExpr ) "]" UnorderedListTypeDef ::= "{{" ( TypeExpr ) "}}"
create type FacebookUserType as closed { id: int32, alias: string, name: string, user-since: datetime, friend-ids: {{ int32 }}, employment: [EmploymentType] }
DatasetSpecification ::= "internal"? "dataset" QualifiedName "(" Identifier ")" IfNotExists PrimaryKey ( "on" Identifier )? ( "hints" Properties )? | "external" "dataset" QualifiedName "(" Identifier ")" IfNotExists "using" AdapterName Configuration ( "hints" Properties )? AdapterName ::= Identifier Configuration ::= "(" ( KeyValuePair ( "," KeyValuePair )* )? ")" KeyValuePair ::= "(" StringLiteral "=" StringLiteral ")" Properties ::= ( "(" Property ( "," Property )* ")" )? Property ::= Identifier "=" ( StringLiteral | <INTEGER_LITERAL> ) ApplyFunction ::= "apply" "function" FunctionSignature FunctionSignature ::= FunctionOrTypeName "@" <INTEGER_LITERAL> PrimaryKey ::= "primary" "key" Identifier ( "," Identifier )*
create internal dataset FacebookUsers(FacebookUserType) primary key id;
create external dataset Lineitem(LineitemType) using localfs ( ("path"="127.0.0.1://SOURCE_PATH"), ("format"="delimited-text"), ("delimiter"="|"));
IndexSpecification ::= "index" Identifier IfNotExists "on" QualifiedName "(" ( Identifier ) ( "," Identifier )* ")" ( "type" IndexType )? IndexType ::= "btree" | "rtree" | "keyword" | "fuzzy keyword" | "ngram" "(" <INTEGER_LITERAL> ")" | "fuzzy ngram" "(" <INTEGER_LITERAL> ")"
create index fbAuthorIdx on FacebookMessages(author-id) type btree;
create index fbSenderLocIndex on FacebookMessages(sender-location) type rtree;
create index fbMessageIdx on FacebookMessages(message) type keyword;
FunctionSpecification ::= "function" FunctionOrTypeName IfNotExists ParameterList "{" Expression "}"
create function add($a, $b) { $a + $b };
DropStatement ::= "drop" ( "dataverse" Identifier IfExists | "type" FunctionOrTypeName IfExists | "dataset" QualifiedName IfExists | "index" DoubleQualifiedName IfExists | "function" FunctionSignature IfExists ) IfExists ::= ( "if" "exists" )?
drop dataset FacebookUsers if exists;
drop index fbSenderLocIndex;
drop type FacebookUserType;
drop dataverse TinySocial;
drop function add;
LoadStatement ::= "load" "dataset" QualifiedName "using" AdapterName Configuration ( "pre-sorted" )?
load dataset FacebookUsers using localfs (("path"="localhost:///Users/zuck/AsterixDB/load/fbu.adm"),("format"="adm"));
InsertStatement ::= "insert" "into" "dataset" QualifiedName Query DeleteStatement ::= "delete" Variable "from" "dataset" QualifiedName ( "where" Expression )?
insert into dataset UsersCopy (for $user in dataset FacebookUsers return $user)
delete $user from dataset FacebookUsers where $user.id = 8;
Query ::= Expression
for $praise in {{ "great", "brilliant", "awesome" }} return string-concat(["AsterixDB is ", $praise])