doc.go 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633
  1. // Copyright 2014 The ql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //MAYBE set operations
  5. //MAYBE +=, -=, ...
  6. //TODO verify there's a graceful failure for a 2G+ blob on a 32 bit machine.
  7. // Package ql implements a pure Go embedded SQL database engine.
  8. //
  9. // QL is a member of the SQL family of languages. It is less complex and less
  10. // powerful than SQL (whichever specification SQL is considered to be).
  11. //
  12. // Change list
  13. //
  14. // 2016-07-29: Release v1.0.6 enables alternatively using = instead of == for
  15. // equality oparation.
  16. //
  17. // https://github.com/cznic/ql/issues/131
  18. //
  19. // 2016-07-11: Release v1.0.5 undoes vendoring of lldb. QL now uses stable lldb
  20. // (github.com/cznic/lldb).
  21. //
  22. // https://github.com/cznic/ql/issues/128
  23. //
  24. // 2016-07-06: Release v1.0.4 fixes a panic when closing the WAL file.
  25. //
  26. // https://github.com/cznic/ql/pull/127
  27. //
  28. // 2016-04-03: Release v1.0.3 fixes a data race.
  29. //
  30. // https://github.com/cznic/ql/issues/126
  31. //
  32. // 2016-03-23: Release v1.0.2 vendors github.com/cznic/exp/lldb and
  33. // github.com/camlistore/go4/lock.
  34. //
  35. // 2016-03-17: Release v1.0.1 adjusts for latest goyacc. Parser error messages
  36. // are improved and changed, but their exact form is not considered a API
  37. // change.
  38. //
  39. // 2016-03-05: The current version has been tagged v1.0.0.
  40. //
  41. // 2015-06-15: To improve compatibility with other SQL implementations, the
  42. // count built-in aggregate function now accepts * as its argument.
  43. //
  44. // 2015-05-29: The execution planner was rewritten from scratch. It should use
  45. // indices in all places where they were used before plus in some additional
  46. // situations. It is possible to investigate the plan using the newly added
  47. // EXPLAIN statement. The QL tool is handy for such analysis. If the planner
  48. // would have used an index, but no such exists, the plan includes hints in
  49. // form of copy/paste ready CREATE INDEX statements.
  50. //
  51. // The planner is still quite simple and a lot of work on it is yet ahead. You
  52. // can help this process by filling an issue with a schema and query which
  53. // fails to use an index or indices when it should, in your opinion. Bonus
  54. // points for including output of `ql 'explain <query>'`.
  55. //
  56. // 2015-05-09: The grammar of the CREATE INDEX statement now accepts an
  57. // expression list instead of a single expression, which was further limited to
  58. // just a column name or the built-in id(). As a side effect, composite
  59. // indices are now functional. However, the values in the expression-list style
  60. // index are not yet used by other statements or the statement/query planner.
  61. // The composite index is useful while having UNIQUE clause to check for
  62. // semantically duplicate rows before they get added to the table or when such
  63. // a row is mutated using the UPDATE statement and the expression-list style
  64. // index tuple of the row is thus recomputed.
  65. //
  66. // 2015-05-02: The Schema field of table __Table now correctly reflects any
  67. // column constraints and/or defaults. Also, the (*DB).Info method now has that
  68. // information provided in new ColumInfo fields NotNull, Constraint and
  69. // Default.
  70. //
  71. // 2015-04-20: Added support for {LEFT,RIGHT,FULL} [OUTER] JOIN.
  72. //
  73. // 2015-04-18: Column definitions can now have constraints and defaults.
  74. // Details are discussed in the "Constraints and defaults" chapter below the
  75. // CREATE TABLE statement documentation.
  76. //
  77. // 2015-03-06: New built-in functions formatFloat and formatInt. Thanks
  78. // urandom! (https://github.com/urandom)
  79. //
  80. // 2015-02-16: IN predicate now accepts a SELECT statement. See the updated
  81. // "Predicates" section.
  82. //
  83. // 2015-01-17: Logical operators || and && have now alternative spellings: OR
  84. // and AND (case insensitive). AND was a keyword before, but OR is a new one.
  85. // This can possibly break existing queries. For the record, it's a good idea
  86. // to not use any name appearing in, for example, [7] in your queries as the
  87. // list of QL's keywords may expand for gaining better compatibility with
  88. // existing SQL "standards".
  89. //
  90. // 2015-01-12: ACID guarantees were tightened at the cost of performance in
  91. // some cases. The write collecting window mechanism, a formerly used
  92. // implementation detail, was removed. Inserting rows one by one in a
  93. // transaction is now slow. I mean very slow. Try to avoid inserting single
  94. // rows in a transaction. Instead, whenever possible, perform batch updates of
  95. // tens to, say thousands of rows in a single transaction. See also:
  96. // http://www.sqlite.org/faq.html#q19, the discussed synchronization principles
  97. // involved are the same as for QL, modulo minor details.
  98. //
  99. // Note: A side effect is that closing a DB before exiting an application, both
  100. // for the Go API and through database/sql driver, is no more required,
  101. // strictly speaking. Beware that exiting an application while there is an open
  102. // (uncommitted) transaction in progress means losing the transaction data.
  103. // However, the DB will not become corrupted because of not closing it. Nor
  104. // that was the case before, but formerly failing to close a DB could have
  105. // resulted in losing the data of the last transaction.
  106. //
  107. // 2014-09-21: id() now optionally accepts a single argument - a table name.
  108. //
  109. // 2014-09-01: Added the DB.Flush() method and the LIKE pattern matching
  110. // predicate.
  111. //
  112. // 2014-08-08: The built in functions max and min now accept also time values.
  113. // Thanks opennota! (https://github.com/opennota)
  114. //
  115. // 2014-06-05: RecordSet interface extended by new methods FirstRow and Rows.
  116. //
  117. // 2014-06-02: Indices on id() are now used by SELECT statements.
  118. //
  119. // 2014-05-07: Introduction of Marshal, Schema, Unmarshal.
  120. //
  121. // 2014-04-15:
  122. //
  123. // Added optional IF NOT EXISTS clause to CREATE INDEX and optional IF EXISTS
  124. // clause to DROP INDEX.
  125. //
  126. // 2014-04-12:
  127. //
  128. // The column Unique in the virtual table __Index was renamed to IsUnique
  129. // because the old name is a keyword. Unfortunately, this is a breaking change,
  130. // sorry.
  131. //
  132. // 2014-04-11: Introduction of LIMIT, OFFSET.
  133. //
  134. // 2014-04-10: Introduction of query rewriting.
  135. //
  136. // 2014-04-07: Introduction of indices.
  137. //
  138. // Building non CGO QL
  139. //
  140. // QL imports zappy[8], a block-based compressor, which speeds up its
  141. // performance by using a C version of the compression/decompression
  142. // algorithms. If a CGO-free (pure Go) version of QL, or an app using QL, is
  143. // required, please include 'purego' in the -tags option of go
  144. // {build,get,install}. For example:
  145. //
  146. // $ go get -tags purego github.com/cznic/ql
  147. //
  148. // If zappy was installed before installing QL, it might be necessary to
  149. // rebuild zappy first (or rebuild QL with all its dependencies using the -a
  150. // option):
  151. //
  152. // $ touch "$GOPATH"/src/github.com/cznic/zappy/*.go
  153. // $ go install -tags purego github.com/cznic/zappy
  154. // $ go install github.com/cznic/ql
  155. //
  156. // Notation
  157. //
  158. // The syntax is specified using Extended Backus-Naur Form (EBNF)
  159. //
  160. // Production = production_name "=" [ Expression ] "." .
  161. // Expression = Alternative { "|" Alternative } .
  162. // Alternative = Term { Term } .
  163. // Term = production_name | token [ "…" token ] | Group | Option | Repetition .
  164. // Group = "(" Expression ")" .
  165. // Option = "[" Expression "]" .
  166. // Repetition = "{" Expression "}" .
  167. // Productions are expressions constructed from terms and the following operators, in increasing precedence
  168. //
  169. // | alternation
  170. // () grouping
  171. // [] option (0 or 1 times)
  172. // {} repetition (0 to n times)
  173. //
  174. // Lower-case production names are used to identify lexical tokens.
  175. // Non-terminals are in CamelCase. Lexical tokens are enclosed in double quotes
  176. // "" or back quotes ``.
  177. //
  178. // The form a … b represents the set of characters from a through b as
  179. // alternatives. The horizontal ellipsis … is also used elsewhere in the spec
  180. // to informally denote various enumerations or code snippets that are not
  181. // further specified.
  182. //
  183. // QL source code representation
  184. //
  185. // QL source code is Unicode text encoded in UTF-8. The text is not
  186. // canonicalized, so a single accented code point is distinct from the same
  187. // character constructed from combining an accent and a letter; those are
  188. // treated as two code points. For simplicity, this document will use the
  189. // unqualified term character to refer to a Unicode code point in the source
  190. // text.
  191. //
  192. // Each code point is distinct; for instance, upper and lower case letters are
  193. // different characters.
  194. //
  195. // Implementation restriction: For compatibility with other tools, the parser
  196. // may disallow the NUL character (U+0000) in the statement.
  197. //
  198. // Implementation restriction: A byte order mark is disallowed anywhere in QL
  199. // statements.
  200. //
  201. // Characters
  202. //
  203. // The following terms are used to denote specific character classes
  204. //
  205. // newline = . // the Unicode code point U+000A
  206. // unicode_char = . // an arbitrary Unicode code point except newline
  207. // ascii_letter = "a" … "z" | "A" … "Z" .
  208. //
  209. // Letters and digits
  210. //
  211. // The underscore character _ (U+005F) is considered a letter.
  212. //
  213. // letter = ascii_letter | "_" .
  214. // decimal_digit = "0" … "9" .
  215. // octal_digit = "0" … "7" .
  216. // hex_digit = "0" … "9" | "A" … "F" | "a" … "f" .
  217. //
  218. // Lexical elements
  219. //
  220. // Lexical elements are comments, tokens, identifiers, keywords, operators and
  221. // delimiters, integer, floating-point, imaginary, rune and string literals and
  222. // QL parameters.
  223. //
  224. // Comments
  225. //
  226. // There are three forms of comments
  227. //
  228. // Line comments start with the character sequence // or -- and stop at the end
  229. // of the line. A line comment acts like a space.
  230. //
  231. // General comments start with the character sequence /* and continue through
  232. // the character sequence */. A general comment acts like a space.
  233. //
  234. // Comments do not nest.
  235. //
  236. // Tokens
  237. //
  238. // Tokens form the vocabulary of QL. There are four classes: identifiers,
  239. // keywords, operators and delimiters, and literals. White space, formed from
  240. // spaces (U+0020), horizontal tabs (U+0009), carriage returns (U+000D), and
  241. // newlines (U+000A), is ignored except as it separates tokens that would
  242. // otherwise combine into a single token.
  243. //
  244. // Semicolons
  245. //
  246. // The formal grammar uses semicolons ";" as separators of QL statements. A
  247. // single QL statement or the last QL statement in a list of statements can
  248. // have an optional semicolon terminator. (Actually a separator from the
  249. // following empty statement.)
  250. //
  251. // Identifiers
  252. //
  253. // Identifiers name entities such as tables or record set columns. An
  254. // identifier is a sequence of one or more letters and digits. The first
  255. // character in an identifier must be a letter.
  256. //
  257. // identifier = letter { letter | decimal_digit } .
  258. //
  259. // For example
  260. //
  261. // price
  262. // _tmp42
  263. // Sales
  264. //
  265. // No identifiers are predeclared, however note that no keyword can be used as
  266. // an identifier. Identifiers starting with two underscores are used for meta
  267. // data virtual tables names. For forward compatibility, users should generally
  268. // avoid using any identifiers starting with two underscores. For example
  269. //
  270. // __Column
  271. // __Column2
  272. // __Index
  273. // __Table
  274. //
  275. // Keywords
  276. //
  277. // The following keywords are reserved and may not be used as identifiers.
  278. //
  279. // ADD COLUMN false int32 ORDER uint16
  280. // ALTER complex128 float int64 OUTER uint32
  281. // AND complex64 float32 int8 RIGHT uint64
  282. // AS CREATE float64 INTO SELECT uint8
  283. // ASC DEFAULT FROM JOIN SET UNIQUE
  284. // BETWEEN DELETE GROUP LEFT string UPDATE
  285. // bigint DESC IF LIMIT TABLE VALUES
  286. // bigrat DISTINCT IN LIKE time WHERE
  287. // blob DROP INDEX NOT true
  288. // bool duration INSERT NULL OR
  289. // BY EXISTS int OFFSET TRUNCATE
  290. // byte EXPLAIN int16 ON uint
  291. //
  292. // Keywords are not case sensitive.
  293. //
  294. // Operators and Delimiters
  295. //
  296. // The following character sequences represent operators, delimiters, and other
  297. // special tokens
  298. //
  299. // + & && == != ( )
  300. // - | || < <= [ ]
  301. // * ^ > >= , ;
  302. // / << = .
  303. // % >> !
  304. // &^
  305. //
  306. // Operators consisting of more than one character are referred to by names in
  307. // the rest of the documentation
  308. //
  309. // andand = "&&" .
  310. // andnot = "&^" .
  311. // lsh = "<<" .
  312. // le = "<=" .
  313. // eq = "==" | "=" .
  314. // ge = ">=" .
  315. // neq = "!=" .
  316. // oror = "||" .
  317. // rsh = ">>" .
  318. //
  319. // Integer literals
  320. //
  321. // An integer literal is a sequence of digits representing an integer constant.
  322. // An optional prefix sets a non-decimal base: 0 for octal, 0x or 0X for
  323. // hexadecimal. In hexadecimal literals, letters a-f and A-F represent values
  324. // 10 through 15.
  325. //
  326. // int_lit = decimal_lit | octal_lit | hex_lit .
  327. // decimal_lit = ( "1" … "9" ) { decimal_digit } .
  328. // octal_lit = "0" { octal_digit } .
  329. // hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit } .
  330. //
  331. // For example
  332. //
  333. // 42
  334. // 0600
  335. // 0xBadFace
  336. // 1701411834604692
  337. //
  338. // Floating-point literals
  339. //
  340. // A floating-point literal is a decimal representation of a floating-point
  341. // constant. It has an integer part, a decimal point, a fractional part, and an
  342. // exponent part. The integer and fractional part comprise decimal digits; the
  343. // exponent part is an e or E followed by an optionally signed decimal
  344. // exponent. One of the integer part or the fractional part may be elided; one
  345. // of the decimal point or the exponent may be elided.
  346. //
  347. // float_lit = decimals "." [ decimals ] [ exponent ] |
  348. // decimals exponent |
  349. // "." decimals [ exponent ] .
  350. // decimals = decimal_digit { decimal_digit } .
  351. // exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
  352. //
  353. // For example
  354. //
  355. // 0.
  356. // 72.40
  357. // 072.40 // == 72.40
  358. // 2.71828
  359. // 1.e+0
  360. // 6.67428e-11
  361. // 1E6
  362. // .25
  363. // .12345E+5
  364. //
  365. // Imaginary literals
  366. //
  367. // An imaginary literal is a decimal representation of the imaginary part of a
  368. // complex constant. It consists of a floating-point literal or decimal integer
  369. // followed by the lower-case letter i.
  370. //
  371. // imaginary_lit = (decimals | float_lit) "i" .
  372. //
  373. // For example
  374. //
  375. // 0i
  376. // 011i // == 11i
  377. // 0.i
  378. // 2.71828i
  379. // 1.e+0i
  380. // 6.67428e-11i
  381. // 1E6i
  382. // .25i
  383. // .12345E+5i
  384. //
  385. // Rune literals
  386. //
  387. // A rune literal represents a rune constant, an integer value identifying a
  388. // Unicode code point. A rune literal is expressed as one or more characters
  389. // enclosed in single quotes. Within the quotes, any character may appear
  390. // except single quote and newline. A single quoted character represents the
  391. // Unicode value of the character itself, while multi-character sequences
  392. // beginning with a backslash encode values in various formats.
  393. //
  394. // The simplest form represents the single character within the quotes; since
  395. // QL statements are Unicode characters encoded in UTF-8, multiple
  396. // UTF-8-encoded bytes may represent a single integer value. For instance, the
  397. // literal 'a' holds a single byte representing a literal a, Unicode U+0061,
  398. // value 0x61, while 'ä' holds two bytes (0xc3 0xa4) representing a literal
  399. // a-dieresis, U+00E4, value 0xe4.
  400. //
  401. // Several backslash escapes allow arbitrary values to be encoded as ASCII
  402. // text. There are four ways to represent the integer value as a numeric
  403. // constant: \x followed by exactly two hexadecimal digits; \u followed by
  404. // exactly four hexadecimal digits; \U followed by exactly eight hexadecimal
  405. // digits, and a plain backslash \ followed by exactly three octal digits. In
  406. // each case the value of the literal is the value represented by the digits in
  407. // the corresponding base.
  408. //
  409. // Although these representations all result in an integer, they have different
  410. // valid ranges. Octal escapes must represent a value between 0 and 255
  411. // inclusive. Hexadecimal escapes satisfy this condition by construction. The
  412. // escapes \u and \U represent Unicode code points so within them some values
  413. // are illegal, in particular those above 0x10FFFF and surrogate halves.
  414. //
  415. // After a backslash, certain single-character escapes represent special
  416. // values
  417. //
  418. // \a U+0007 alert or bell
  419. // \b U+0008 backspace
  420. // \f U+000C form feed
  421. // \n U+000A line feed or newline
  422. // \r U+000D carriage return
  423. // \t U+0009 horizontal tab
  424. // \v U+000b vertical tab
  425. // \\ U+005c backslash
  426. // \' U+0027 single quote (valid escape only within rune literals)
  427. // \" U+0022 double quote (valid escape only within string literals)
  428. //
  429. // All other sequences starting with a backslash are illegal inside rune
  430. // literals.
  431. //
  432. // rune_lit = "'" ( unicode_value | byte_value ) "'" .
  433. // unicode_value = unicode_char | little_u_value | big_u_value | escaped_char .
  434. // byte_value = octal_byte_value | hex_byte_value .
  435. // octal_byte_value = `\` octal_digit octal_digit octal_digit .
  436. // hex_byte_value = `\` "x" hex_digit hex_digit .
  437. // little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit .
  438. // big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit
  439. // hex_digit hex_digit hex_digit hex_digit .
  440. // escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) .
  441. //
  442. // For example
  443. //
  444. // 'a'
  445. // 'ä'
  446. // '本'
  447. // '\t'
  448. // '\000'
  449. // '\007'
  450. // '\377'
  451. // '\x07'
  452. // '\xff'
  453. // '\u12e4'
  454. // '\U00101234'
  455. // 'aa' // illegal: too many characters
  456. // '\xa' // illegal: too few hexadecimal digits
  457. // '\0' // illegal: too few octal digits
  458. // '\uDFFF' // illegal: surrogate half
  459. // '\U00110000' // illegal: invalid Unicode code point
  460. //
  461. // String literals
  462. //
  463. // A string literal represents a string constant obtained from concatenating a
  464. // sequence of characters. There are two forms: raw string literals and
  465. // interpreted string literals.
  466. //
  467. // Raw string literals are character sequences between back quotes ``. Within
  468. // the quotes, any character is legal except back quote. The value of a raw
  469. // string literal is the string composed of the uninterpreted (implicitly
  470. // UTF-8-encoded) characters between the quotes; in particular, backslashes
  471. // have no special meaning and the string may contain newlines. Carriage
  472. // returns inside raw string literals are discarded from the raw string value.
  473. //
  474. // Interpreted string literals are character sequences between double quotes
  475. // "". The text between the quotes, which may not contain newlines, forms the
  476. // value of the literal, with backslash escapes interpreted as they are in rune
  477. // literals (except that \' is illegal and \" is legal), with the same
  478. // restrictions. The three-digit octal (\nnn) and two-digit hexadecimal (\xnn)
  479. // escapes represent individual bytes of the resulting string; all other
  480. // escapes represent the (possibly multi-byte) UTF-8 encoding of individual
  481. // characters. Thus inside a string literal \377 and \xFF represent a single
  482. // byte of value 0xFF=255, while ÿ, \u00FF, \U000000FF and \xc3\xbf represent
  483. // the two bytes 0xc3 0xbf of the UTF-8 encoding of character U+00FF.
  484. //
  485. // string_lit = raw_string_lit | interpreted_string_lit .
  486. // raw_string_lit = "`" { unicode_char | newline } "`" .
  487. // interpreted_string_lit = `"` { unicode_value | byte_value } `"` .
  488. //
  489. // For example
  490. //
  491. // `abc` // same as "abc"
  492. // `\n
  493. // \n` // same as "\\n\n\\n"
  494. // "\n"
  495. // ""
  496. // "Hello, world!\n"
  497. // "日本語"
  498. // "\u65e5本\U00008a9e"
  499. // "\xff\u00FF"
  500. // "\uD800" // illegal: surrogate half
  501. // "\U00110000" // illegal: invalid Unicode code point
  502. //
  503. // These examples all represent the same string
  504. //
  505. // "日本語" // UTF-8 input text
  506. // `日本語` // UTF-8 input text as a raw literal
  507. // "\u65e5\u672c\u8a9e" // the explicit Unicode code points
  508. // "\U000065e5\U0000672c\U00008a9e" // the explicit Unicode code points
  509. // "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // the explicit UTF-8 bytes
  510. //
  511. // If the statement source represents a character as two code points, such as a
  512. // combining form involving an accent and a letter, the result will be an error
  513. // if placed in a rune literal (it is not a single code point), and will appear
  514. // as two code points if placed in a string literal.
  515. //
  516. // QL parameters
  517. //
  518. // Literals are assigned their values from the respective text representation
  519. // at "compile" (parse) time. QL parameters provide the same functionality as
  520. // literals, but their value is assigned at execution time from an expression
  521. // list passed to DB.Run or DB.Execute. Using '?' or '$' is completely
  522. // equivalent.
  523. //
  524. // ql_parameter = ( "?" | "$" ) "1" … "9" { "0" … "9" } .
  525. //
  526. // For example
  527. //
  528. // SELECT DepartmentID
  529. // FROM department
  530. // WHERE DepartmentID == ?1
  531. // ORDER BY DepartmentName;
  532. //
  533. // SELECT employee.LastName
  534. // FROM department, employee
  535. // WHERE department.DepartmentID == $1 && employee.LastName > $2
  536. // ORDER BY DepartmentID;
  537. //
  538. // Constants
  539. //
  540. // Keywords 'false' and 'true' (not case sensitive) represent the two possible
  541. // constant values of type bool (also not case sensitive).
  542. //
  543. // Keyword 'NULL' (not case sensitive) represents an untyped constant which is
  544. // assignable to any type. NULL is distinct from any other value of any type.
  545. //
  546. // Types
  547. //
  548. // A type determines the set of values and operations specific to values of
  549. // that type. A type is specified by a type name.
  550. //
  551. // Type = "bigint" // http://golang.org/pkg/math/big/#Int
  552. // | "bigrat" // http://golang.org/pkg/math/big/#Rat
  553. // | "blob" // []byte
  554. // | "bool"
  555. // | "byte" // alias for uint8
  556. // | "complex128"
  557. // | "complex64"
  558. // | "duration" // http://golang.org/pkg/time/#Duration
  559. // | "float" // alias for float64
  560. // | "float32"
  561. // | "float64"
  562. // | "int" // alias for int64
  563. // | "int16"
  564. // | "int32"
  565. // | "int64"
  566. // | "int8"
  567. // | "rune" // alias for int32
  568. // | "string"
  569. // | "time" // http://golang.org/pkg/time/#Time
  570. // | "uint" // alias for uint64
  571. // | "uint16"
  572. // | "uint32"
  573. // | "uint64"
  574. // | "uint8" .
  575. //
  576. // Named instances of the boolean, numeric, and string types are keywords. The
  577. // names are not case sensitive.
  578. //
  579. // Note: The blob type is exchanged between the back end and the API as []byte.
  580. // On 32 bit platforms this limits the size which the implementation can handle
  581. // to 2G.
  582. //
  583. // Boolean types
  584. //
  585. // A boolean type represents the set of Boolean truth values denoted by the
  586. // predeclared constants true and false. The predeclared boolean type is bool.
  587. //
  588. // Duration type
  589. //
  590. // A duration type represents the elapsed time between two instants as an int64
  591. // nanosecond count. The representation limits the largest representable
  592. // duration to approximately 290 years.
  593. //
  594. // Numeric types
  595. //
  596. // A numeric type represents sets of integer or floating-point values. The
  597. // predeclared architecture-independent numeric types are
  598. //
  599. // uint8 the set of all unsigned 8-bit integers (0 to 255)
  600. // uint16 the set of all unsigned 16-bit integers (0 to 65535)
  601. // uint32 the set of all unsigned 32-bit integers (0 to 4294967295)
  602. // uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)
  603. //
  604. // int8 the set of all signed 8-bit integers (-128 to 127)
  605. // int16 the set of all signed 16-bit integers (-32768 to 32767)
  606. // int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
  607. // int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
  608. // duration the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
  609. // bigint the set of all integers
  610. //
  611. // bigrat the set of all rational numbers
  612. //
  613. // float32 the set of all IEEE-754 32-bit floating-point numbers
  614. // float64 the set of all IEEE-754 64-bit floating-point numbers
  615. //
  616. // complex64 the set of all complex numbers with float32 real and imaginary parts
  617. // complex128 the set of all complex numbers with float64 real and imaginary parts
  618. //
  619. // byte alias for uint8
  620. // float alias for float64
  621. // int alias for int64
  622. // rune alias for int32
  623. // uint alias for uint64
  624. //
  625. // The value of an n-bit integer is n bits wide and represented using two's
  626. // complement arithmetic.
  627. //
  628. // Conversions are required when different numeric types are mixed in an
  629. // expression or assignment.
  630. //
  631. // String types
  632. //
  633. // A string type represents the set of string values. A string value is a
  634. // (possibly empty) sequence of bytes. The case insensitive keyword for the
  635. // string type is 'string'.
  636. //
  637. // The length of a string (its size in bytes) can be discovered using the
  638. // built-in function len.
  639. //
  640. // Time types
  641. //
  642. // A time type represents an instant in time with nanosecond precision. Each
  643. // time has associated with it a location, consulted when computing the
  644. // presentation form of the time.
  645. //
  646. // Predeclared functions
  647. //
  648. // The following functions are implicitly declared
  649. //
  650. // avg complex contains count date
  651. // day formatTime formatFloat formatInt
  652. // hasPrefix hasSuffix hour hours id
  653. // imag len max min minute
  654. // minutes month nanosecond nanoseconds now
  655. // parseTime real second seconds since
  656. // sum timeIn weekday year yearDay
  657. //
  658. // Expressions
  659. //
  660. // An expression specifies the computation of a value by applying operators and
  661. // functions to operands.
  662. //
  663. // Operands
  664. //
  665. // Operands denote the elementary values in an expression. An operand may be a
  666. // literal, a (possibly qualified) identifier denoting a constant or a function
  667. // or a table/record set column, or a parenthesized expression.
  668. //
  669. // Operand = Literal | QualifiedIdent | "(" Expression ")" .
  670. // Literal = "FALSE" | "NULL" | "TRUE"
  671. // | float_lit | imaginary_lit | int_lit | rune_lit | string_lit
  672. // | ql_parameter .
  673. //
  674. // Qualified identifiers
  675. //
  676. // A qualified identifier is an identifier qualified with a table/record set
  677. // name prefix.
  678. //
  679. // QualifiedIdent = identifier [ "." identifier ] .
  680. //
  681. // For example
  682. //
  683. // invoice.Num // might denote column 'Num' from table 'invoice'
  684. //
  685. // Primary expressions
  686. //
  687. // Primary expression are the operands for unary and binary expressions.
  688. //
  689. // PrimaryExpression = Operand
  690. // | Conversion
  691. // | PrimaryExpression Index
  692. // | PrimaryExpression Slice
  693. // | PrimaryExpression Call .
  694. //
  695. // Call = "(" [ "*" | ExpressionList ] ")" . // * only in count(*).
  696. // Index = "[" Expression "]" .
  697. // Slice = "[" [ Expression ] ":" [ Expression ] "]" .
  698. //
  699. // For example
  700. //
  701. // x
  702. // 2
  703. // (s + ".txt")
  704. // f(3.1415, true)
  705. // s[i : j + 1]
  706. //
  707. // Index expressions
  708. //
  709. // A primary expression of the form
  710. //
  711. // s[x]
  712. //
  713. // denotes the element of a string indexed by x. Its type is byte. The value x
  714. // is called the index. The following rules apply
  715. //
  716. // - The index x must be of integer type except bigint or duration; it is in
  717. // range if 0 <= x < len(s), otherwise it is out of range.
  718. //
  719. // - A constant index must be non-negative and representable by a value of type
  720. // int.
  721. //
  722. // - A constant index must be in range if the string a is a literal.
  723. //
  724. // - If x is out of range at run time, a run-time error occurs.
  725. //
  726. // - s[x] is the byte at index x and the type of s[x] is byte.
  727. //
  728. // If s is NULL or x is NULL then the result is NULL.
  729. //
  730. // Otherwise s[x] is illegal.
  731. //
  732. // Slices
  733. //
  734. // For a string, the primary expression
  735. //
  736. // s[low : high]
  737. //
  738. // constructs a substring. The indices low and high select which elements
  739. // appear in the result. The result has indices starting at 0 and length equal
  740. // to high - low.
  741. //
  742. // For convenience, any of the indices may be omitted. A missing low index
  743. // defaults to zero; a missing high index defaults to the length of the sliced
  744. // operand
  745. //
  746. // s[2:] // same s[2 : len(s)]
  747. // s[:3] // same as s[0 : 3]
  748. // s[:] // same as s[0 : len(s)]
  749. //
  750. // The indices low and high are in range if 0 <= low <= high <= len(a),
  751. // otherwise they are out of range. A constant index must be non-negative and
  752. // representable by a value of type int. If both indices are constant, they
  753. // must satisfy low <= high. If the indices are out of range at run time, a
  754. // run-time error occurs.
  755. //
  756. // Integer values of type bigint or duration cannot be used as indices.
  757. //
  758. // If s is NULL the result is NULL. If low or high is not omitted and is NULL
  759. // then the result is NULL.
  760. //
  761. // Calls
  762. //
  763. // Given an identifier f denoting a predeclared function,
  764. //
  765. // f(a1, a2, … an)
  766. //
  767. // calls f with arguments a1, a2, … an. Arguments are evaluated before the
  768. // function is called. The type of the expression is the result type of f.
  769. //
  770. // complex(x, y)
  771. // len(name)
  772. //
  773. // In a function call, the function value and arguments are evaluated in the
  774. // usual order. After they are evaluated, the parameters of the call are passed
  775. // by value to the function and the called function begins execution. The
  776. // return value of the function is passed by value when the function returns.
  777. //
  778. // Calling an undefined function causes a compile-time error.
  779. //
  780. // Operators
  781. //
  782. // Operators combine operands into expressions.
  783. //
  784. // Expression = Term { ( oror | "OR" ) Term } .
  785. //
  786. // ExpressionList = Expression { "," Expression } [ "," ].
  787. // Factor = PrimaryFactor { ( ge | ">" | le | "<" | neq | eq | "LIKE" ) PrimaryFactor } [ Predicate ] .
  788. // PrimaryFactor = PrimaryTerm { ( "^" | "|" | "-" | "+" ) PrimaryTerm } .
  789. // PrimaryTerm = UnaryExpr { ( andnot | "&" | lsh | rsh | "%" | "/" | "*" ) UnaryExpr } .
  790. // Term = Factor { ( andand | "AND" ) Factor } .
  791. // UnaryExpr = [ "^" | "!" | "-" | "+" ] PrimaryExpression .
  792. //
  793. // Comparisons are discussed elsewhere. For other binary operators, the operand
  794. // types must be identical unless the operation involves shifts or untyped
  795. // constants. For operations involving constants only, see the section on
  796. // constant expressions.
  797. //
  798. // Except for shift operations, if one operand is an untyped constant and the
  799. // other operand is not, the constant is converted to the type of the other
  800. // operand.
  801. //
  802. // The right operand in a shift expression must have unsigned integer type or
  803. // be an untyped constant that can be converted to unsigned integer type. If
  804. // the left operand of a non-constant shift expression is an untyped constant,
  805. // the type of the constant is what it would be if the shift expression were
  806. // replaced by its left operand alone.
  807. //
  808. // Pattern matching
  809. //
  810. // Expressions of the form
  811. //
  812. // expr1 LIKE expr2
  813. //
  814. // yield a boolean value true if expr2, a regular expression, matches expr1
  815. // (see also [6]). Both expression must be of type string. If any one of the
  816. // expressions is NULL the result is NULL.
  817. //
  818. // Predicates
  819. //
  820. // Predicates are special form expressions having a boolean result type.
  821. //
  822. // Expressions of the form
  823. //
  824. // expr IN ( expr1, expr2, expr3, ... ) // case A
  825. //
  826. // expr NOT IN ( expr1, expr2, expr3, ... ) // case B
  827. //
  828. // are equivalent, including NULL handling, to
  829. //
  830. // expr == expr1 || expr == expr2 || expr == expr3 || ... // case A
  831. //
  832. // expr != expr1 && expr != expr2 && expr != expr3 && ... // case B
  833. //
  834. // The types of involved expressions must be comparable as defined in
  835. // "Comparison operators".
  836. //
  837. // Another form of the IN predicate creates the expression list from a result
  838. // of a SelectStmt.
  839. //
  840. // DELETE FROM t WHERE id() IN (SELECT id_t FROM u WHERE inactive_days > 365)
  841. //
  842. // The SelectStmt must select only one column. The produced expression list is
  843. // resource limited by the memory available to the process. NULL values
  844. // produced by the SelectStmt are ignored, but if all records of the SelectStmt
  845. // are NULL the predicate yields NULL. The select statement is evaluated only
  846. // once. If the type of expr is not the same as the type of the field returned
  847. // by the SelectStmt then the set operation yields false. The type of the
  848. // column returned by the SelectStmt must be one of the simple (non blob-like)
  849. // types:
  850. //
  851. // bool
  852. // byte // alias uint8
  853. // complex128
  854. // complex64
  855. // float // alias float64
  856. // float32
  857. // float64
  858. // int // alias int64
  859. // int16
  860. // int32
  861. // int64
  862. // int8
  863. // rune // alias int32
  864. // string
  865. // uint // alias uint64
  866. // uint16
  867. // uint32
  868. // uint64
  869. // uint8
  870. //
  871. // Expressions of the form
  872. //
  873. // expr BETWEEN low AND high // case A
  874. //
  875. // expr NOT BETWEEN low AND high // case B
  876. //
  877. // are equivalent, including NULL handling, to
  878. //
  879. // expr >= low && expr <= high // case A
  880. //
  881. // expr < low || expr > high // case B
  882. //
  883. // The types of involved expressions must be ordered as defined in "Comparison
  884. // operators".
  885. //
  886. // Predicate = (
  887. // [ "NOT" ] (
  888. // "IN" "(" ExpressionList ")"
  889. // | "IN" "(" SelectStmt [ ";" ] ")"
  890. // | "BETWEEN" PrimaryFactor "AND" PrimaryFactor
  891. // )
  892. // | "IS" [ "NOT" ] "NULL"
  893. // ).
  894. //
  895. // Expressions of the form
  896. //
  897. // expr IS NULL // case A
  898. //
  899. // expr IS NOT NULL // case B
  900. //
  901. // yield a boolean value true if expr does not have a specific type (case A) or
  902. // if expr has a specific type (case B). In other cases the result is a boolean
  903. // value false.
  904. //
  905. // Operator precedence
  906. //
  907. // Unary operators have the highest precedence.
  908. //
  909. // There are five precedence levels for binary operators. Multiplication
  910. // operators bind strongest, followed by addition operators, comparison
  911. // operators, && (logical AND), and finally || (logical OR)
  912. //
  913. // Precedence Operator
  914. // 5 * / % << >> & &^
  915. // 4 + - | ^
  916. // 3 == != < <= > >=
  917. // 2 &&
  918. // 1 ||
  919. //
  920. // Binary operators of the same precedence associate from left to right. For
  921. // instance, x / y * z is the same as (x / y) * z.
  922. //
  923. // +x
  924. // 23 + 3*x[i]
  925. // x <= f()
  926. // ^a >> b
  927. // f() || g()
  928. // x == y+1 && z > 0
  929. //
  930. // Note that the operator precedence is reflected explicitly by the grammar.
  931. //
  932. // Arithmetic operators
  933. //
  934. // Arithmetic operators apply to numeric values and yield a result of the same
  935. // type as the first operand. The four standard arithmetic operators (+, -, *,
  936. // /) apply to integer, rational, floating-point, and complex types; + also
  937. // applies to strings; +,- also applies to times. All other arithmetic
  938. // operators apply to integers only.
  939. //
  940. // + sum integers, rationals, floats, complex values, strings
  941. // - difference integers, rationals, floats, complex values, times
  942. // * product integers, rationals, floats, complex values
  943. // / quotient integers, rationals, floats, complex values
  944. // % remainder integers
  945. //
  946. // & bitwise AND integers
  947. // | bitwise OR integers
  948. // ^ bitwise XOR integers
  949. // &^ bit clear (AND NOT) integers
  950. //
  951. // << left shift integer << unsigned integer
  952. // >> right shift integer >> unsigned integer
  953. //
  954. // Strings can be concatenated using the + operator
  955. //
  956. // "hi" + string(c) + " and good bye"
  957. //
  958. // String addition creates a new string by concatenating the operands.
  959. //
  960. // A value of type duration can be added to or subtracted from a value of type time.
  961. //
  962. // now() + duration("1h") // time after 1 hour from now
  963. // duration("1h") + now() // time after 1 hour from now
  964. // now() - duration("1h") // time before 1 hour from now
  965. // duration("1h") - now() // illegal, negative times do not exist
  966. //
  967. // Times can subtracted from each other producing a value of type duration.
  968. //
  969. // now() - t0 // elapsed time since t0
  970. // now() + now() // illegal, operator + not defined for times
  971. //
  972. // For two integer values x and y, the integer quotient q = x / y and remainder
  973. // r = x % y satisfy the following relationships
  974. //
  975. // x = q*y + r and |r| < |y|
  976. //
  977. // with x / y truncated towards zero ("truncated division").
  978. //
  979. // x y x / y x % y
  980. // 5 3 1 2
  981. // -5 3 -1 -2
  982. // 5 -3 -1 2
  983. // -5 -3 1 -2
  984. //
  985. // As an exception to this rule, if the dividend x is the most negative value
  986. // for the int type of x, the quotient q = x / -1 is equal to x (and r = 0).
  987. //
  988. // x, q
  989. // int8 -128
  990. // int16 -32768
  991. // int32 -2147483648
  992. // int64 -9223372036854775808
  993. //
  994. // If the divisor is a constant expression, it must not be zero. If the divisor
  995. // is zero at run time, a run-time error occurs. If the dividend is
  996. // non-negative and the divisor is a constant power of 2, the division may be
  997. // replaced by a right shift, and computing the remainder may be replaced by a
  998. // bitwise AND operation
  999. //
  1000. // x x / 4 x % 4 x >> 2 x & 3
  1001. // 11 2 3 2 3
  1002. // -11 -2 -3 -3 1
  1003. //
  1004. // The shift operators shift the left operand by the shift count specified by
  1005. // the right operand. They implement arithmetic shifts if the left operand is a
  1006. // signed integer and logical shifts if it is an unsigned integer. There is no
  1007. // upper limit on the shift count. Shifts behave as if the left operand is
  1008. // shifted n times by 1 for a shift count of n. As a result, x << 1 is the same
  1009. // as x*2 and x >> 1 is the same as x/2 but truncated towards negative
  1010. // infinity.
  1011. //
  1012. // For integer operands, the unary operators +, -, and ^ are defined as follows
  1013. //
  1014. // +x is 0 + x
  1015. // -x negation is 0 - x
  1016. // ^x bitwise complement is m ^ x with m = "all bits set to 1" for unsigned x
  1017. // and m = -1 for signed x
  1018. //
  1019. // For floating-point and complex numbers, +x is the same as x, while -x is the
  1020. // negation of x. The result of a floating-point or complex division by zero is
  1021. // not specified beyond the IEEE-754 standard; whether a run-time error occurs
  1022. // is implementation-specific.
  1023. //
  1024. // Whenever any operand of any arithmetic operation, unary or binary, is NULL,
  1025. // as well as in the case of the string concatenating operation, the result is
  1026. // NULL.
  1027. //
  1028. // 42*NULL // the result is NULL
  1029. // NULL/x // the result is NULL
  1030. // "foo"+NULL // the result is NULL
  1031. //
  1032. // Integer overflow
  1033. //
  1034. // For unsigned integer values, the operations +, -, *, and << are computed
  1035. // modulo 2n, where n is the bit width of the unsigned integer's type. Loosely
  1036. // speaking, these unsigned integer operations discard high bits upon overflow,
  1037. // and expressions may rely on ``wrap around''.
  1038. //
  1039. // For signed integers with a finite bit width, the operations +, -, *, and <<
  1040. // may legally overflow and the resulting value exists and is deterministically
  1041. // defined by the signed integer representation, the operation, and its
  1042. // operands. No exception is raised as a result of overflow. An evaluator may
  1043. // not optimize an expression under the assumption that overflow does not
  1044. // occur. For instance, it may not assume that x < x + 1 is always true.
  1045. //
  1046. // Integers of type bigint and rationals do not overflow but their handling is
  1047. // limited by the memory resources available to the program.
  1048. //
  1049. // Comparison operators
  1050. //
  1051. // Comparison operators compare two operands and yield a boolean value.
  1052. //
  1053. // == equal
  1054. // != not equal
  1055. // < less
  1056. // <= less or equal
  1057. // > greater
  1058. // >= greater or equal
  1059. //
  1060. // In any comparison, the first operand must be of same type as is the second
  1061. // operand, or vice versa.
  1062. //
  1063. // The equality operators == and != apply to operands that are comparable. The
  1064. // ordering operators <, <=, >, and >= apply to operands that are ordered.
  1065. // These terms and the result of the comparisons are defined as follows
  1066. //
  1067. // - Boolean values are comparable. Two boolean values are equal if they are
  1068. // either both true or both false.
  1069. //
  1070. // - Complex values are comparable. Two complex values u and v are equal if
  1071. // both real(u) == real(v) and imag(u) == imag(v).
  1072. //
  1073. // - Integer values are comparable and ordered, in the usual way. Note that
  1074. // durations are integers.
  1075. //
  1076. // - Floating point values are comparable and ordered, as defined by the
  1077. // IEEE-754 standard.
  1078. //
  1079. // - Rational values are comparable and ordered, in the usual way.
  1080. //
  1081. // - String values are comparable and ordered, lexically byte-wise.
  1082. //
  1083. // - Time values are comparable and ordered.
  1084. //
  1085. // Whenever any operand of any comparison operation is NULL, the result is
  1086. // NULL.
  1087. //
  1088. // Note that slices are always of type string.
  1089. //
  1090. // Logical operators
  1091. //
  1092. // Logical operators apply to boolean values and yield a boolean result. The
  1093. // right operand is evaluated conditionally.
  1094. //
  1095. // && conditional AND p && q is "if p then q else false"
  1096. // || conditional OR p || q is "if p then true else q"
  1097. // ! NOT !p is "not p"
  1098. //
  1099. // The truth tables for logical operations with NULL values
  1100. //
  1101. // +-------+-------+---------+---------+
  1102. // | p | q | p || q | p && q |
  1103. // +-------+-------+---------+---------+
  1104. // | true | true | *true | true |
  1105. // | true | false | *true | false |
  1106. // | true | NULL | *true | NULL |
  1107. // | false | true | true | *false |
  1108. // | false | false | false | *false |
  1109. // | false | NULL | NULL | *false |
  1110. // | NULL | true | true | NULL |
  1111. // | NULL | false | NULL | false |
  1112. // | NULL | NULL | NULL | NULL |
  1113. // +-------+-------+---------+---------+
  1114. // * indicates q is not evaluated.
  1115. //
  1116. // +-------+-------+
  1117. // | p | !p |
  1118. // +-------+-------+
  1119. // | true | false |
  1120. // | false | true |
  1121. // | NULL | NULL |
  1122. // +-------+-------+
  1123. //
  1124. // Conversions
  1125. //
  1126. // Conversions are expressions of the form T(x) where T is a type and x is an
  1127. // expression that can be converted to type T.
  1128. //
  1129. // Conversion = Type "(" Expression ")" .
  1130. //
  1131. // A constant value x can be converted to type T in any of these cases:
  1132. //
  1133. // - x is representable by a value of type T.
  1134. //
  1135. // - x is a floating-point constant, T is a floating-point type, and x is
  1136. // representable by a value of type T after rounding using IEEE 754
  1137. // round-to-even rules. The constant T(x) is the rounded value.
  1138. //
  1139. // - x is an integer constant and T is a string type. The same rule as for
  1140. // non-constant x applies in this case.
  1141. //
  1142. // Converting a constant yields a typed constant as result.
  1143. //
  1144. // float32(2.718281828) // 2.718281828 of type float32
  1145. // complex128(1) // 1.0 + 0.0i of type complex128
  1146. // float32(0.49999999) // 0.5 of type float32
  1147. // string('x') // "x" of type string
  1148. // string(0x266c) // "♬" of type string
  1149. // "foo" + "bar" // "foobar"
  1150. // int(1.2) // illegal: 1.2 cannot be represented as an int
  1151. // string(65.0) // illegal: 65.0 is not an integer constant
  1152. //
  1153. // A non-constant value x can be converted to type T in any of these cases:
  1154. //
  1155. // - x has type T.
  1156. //
  1157. // - x's type and T are both integer or floating point types.
  1158. //
  1159. // - x's type and T are both complex types.
  1160. //
  1161. // - x is an integer, except bigint or duration, and T is a string type.
  1162. //
  1163. // Specific rules apply to (non-constant) conversions between numeric types or
  1164. // to and from a string type. These conversions may change the representation
  1165. // of x and incur a run-time cost. All other conversions only change the type
  1166. // but not the representation of x.
  1167. //
  1168. // A conversion of NULL to any type yields NULL.
  1169. //
  1170. // Conversions between numeric types
  1171. //
  1172. // For the conversion of non-constant numeric values, the following rules
  1173. // apply
  1174. //
  1175. // 1. When converting between integer types, if the value is a signed integer,
  1176. // it is sign extended to implicit infinite precision; otherwise it is zero
  1177. // extended. It is then truncated to fit in the result type's size. For
  1178. // example, if v == uint16(0x10F0), then uint32(int8(v)) == 0xFFFFFFF0. The
  1179. // conversion always yields a valid value; there is no indication of overflow.
  1180. //
  1181. // 2. When converting a floating-point number to an integer, the fraction is
  1182. // discarded (truncation towards zero).
  1183. //
  1184. // 3. When converting an integer or floating-point number to a floating-point
  1185. // type, or a complex number to another complex type, the result value is
  1186. // rounded to the precision specified by the destination type. For instance,
  1187. // the value of a variable x of type float32 may be stored using additional
  1188. // precision beyond that of an IEEE-754 32-bit number, but float32(x)
  1189. // represents the result of rounding x's value to 32-bit precision. Similarly,
  1190. // x + 0.1 may use more than 32 bits of precision, but float32(x + 0.1) does
  1191. // not.
  1192. //
  1193. // In all non-constant conversions involving floating-point or complex values,
  1194. // if the result type cannot represent the value the conversion succeeds but
  1195. // the result value is implementation-dependent.
  1196. //
  1197. // Conversions to and from a string type
  1198. //
  1199. // 1. Converting a signed or unsigned integer value to a string type yields a
  1200. // string containing the UTF-8 representation of the integer. Values outside
  1201. // the range of valid Unicode code points are converted to "\uFFFD".
  1202. //
  1203. // string('a') // "a"
  1204. // string(-1) // "\ufffd" == "\xef\xbf\xbd"
  1205. // string(0xf8) // "\u00f8" == "ø" == "\xc3\xb8"
  1206. // string(0x65e5) // "\u65e5" == "日" == "\xe6\x97\xa5"
  1207. //
  1208. // 2. Converting a blob to a string type yields a string whose successive bytes
  1209. // are the elements of the blob.
  1210. //
  1211. // string(b /* []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'} */) // "hellø"
  1212. // string(b /* []byte{} */) // ""
  1213. // string(b /* []byte(nil) */) // ""
  1214. //
  1215. // 3. Converting a value of a string type to a blob yields a blob whose
  1216. // successive elements are the bytes of the string.
  1217. //
  1218. // blob("hellø") // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
  1219. // blob("") // []byte{}
  1220. //
  1221. // 4. Converting a value of a bigint type to a string yields a string
  1222. // containing the decimal decimal representation of the integer.
  1223. //
  1224. // string(M9) // "2305843009213693951"
  1225. //
  1226. // 5. Converting a value of a string type to a bigint yields a bigint value
  1227. // containing the integer represented by the string value. A prefix of “0x” or
  1228. // “0X” selects base 16; the “0” prefix selects base 8, and a “0b” or “0B”
  1229. // prefix selects base 2. Otherwise the value is interpreted in base 10. An
  1230. // error occurs if the string value is not in any valid format.
  1231. //
  1232. // bigint("2305843009213693951") // M9
  1233. // bigint("0x1ffffffffffffffffffffff") // M10 == 2^89-1
  1234. //
  1235. // 6. Converting a value of a rational type to a string yields a string
  1236. // containing the decimal decimal representation of the rational in the form
  1237. // "a/b" (even if b == 1).
  1238. //
  1239. // string(bigrat(355)/bigrat(113)) // "355/113"
  1240. //
  1241. // 7. Converting a value of a string type to a bigrat yields a bigrat value
  1242. // containing the rational represented by the string value. The string can be
  1243. // given as a fraction "a/b" or as a floating-point number optionally followed
  1244. // by an exponent. An error occurs if the string value is not in any valid
  1245. // format.
  1246. //
  1247. // bigrat("1.2e-34")
  1248. // bigrat("355/113")
  1249. //
  1250. // 8. Converting a value of a duration type to a string returns a string
  1251. // representing the duration in the form "72h3m0.5s". Leading zero units are
  1252. // omitted. As a special case, durations less than one second format using a
  1253. // smaller unit (milli-, micro-, or nanoseconds) to ensure that the leading
  1254. // digit is non-zero. The zero duration formats as 0, with no unit.
  1255. //
  1256. // string(elapsed) // "1h", for example
  1257. //
  1258. // 9. Converting a string value to a duration yields a duration represented by
  1259. // the string. A duration string is a possibly signed sequence of decimal
  1260. // numbers, each with optional fraction and a unit suffix, such as "300ms",
  1261. // "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s",
  1262. // "m", "h".
  1263. //
  1264. // duration("1m") // http://golang.org/pkg/time/#Minute
  1265. //
  1266. // 10. Converting a time value to a string returns the time formatted using the
  1267. // format string
  1268. //
  1269. // "2006-01-02 15:04:05.999999999 -0700 MST"
  1270. //
  1271. // Order of evaluation
  1272. //
  1273. // When evaluating the operands of an expression or of function calls,
  1274. // operations are evaluated in lexical left-to-right order.
  1275. //
  1276. // For example, in the evaluation of
  1277. //
  1278. // g(h(), i()+x[j()], c)
  1279. //
  1280. // the function calls and evaluation of c happen in the order h(), i(), j(), c.
  1281. //
  1282. // Floating-point operations within a single expression are evaluated according
  1283. // to the associativity of the operators. Explicit parentheses affect the
  1284. // evaluation by overriding the default associativity. In the expression x + (y
  1285. // + z) the addition y + z is performed before adding x.
  1286. //
  1287. // Statements
  1288. //
  1289. // Statements control execution.
  1290. //
  1291. // Statement = EmptyStmt | AlterTableStmt | BeginTransactionStmt | CommitStmt
  1292. // | CreateIndexStmt | CreateTableStmt | DeleteFromStmt | DropIndexStmt
  1293. // | DropTableStmt | InsertIntoStmt | RollbackStmt | SelectStmt
  1294. // | TruncateTableStmt | UpdateStmt | ExplainStmt.
  1295. //
  1296. // StatementList = Statement { ";" Statement } .
  1297. //
  1298. // Empty statements
  1299. //
  1300. // The empty statement does nothing.
  1301. //
  1302. // EmptyStmt = .
  1303. //
  1304. // ALTER TABLE
  1305. //
  1306. // Alter table statements modify existing tables. With the ADD clause it adds
  1307. // a new column to the table. The column must not exist. With the DROP clause
  1308. // it removes an existing column from a table. The column must exist and it
  1309. // must be not the only (last) column of the table. IOW, there cannot be a
  1310. // table with no columns.
  1311. //
  1312. // AlterTableStmt = "ALTER" "TABLE" TableName ( "ADD" ColumnDef | "DROP" "COLUMN" ColumnName ) .
  1313. //
  1314. // For example
  1315. //
  1316. // BEGIN TRANSACTION;
  1317. // ALTER TABLE Stock ADD Qty int;
  1318. // ALTER TABLE Income DROP COLUMN Taxes;
  1319. // COMMIT;
  1320. //
  1321. // When adding a column to a table with existing data, the constraint clause of
  1322. // the ColumnDef cannot be used. Adding a constrained column to an empty table
  1323. // is fine.
  1324. //
  1325. // BEGIN TRANSACTION
  1326. //
  1327. // Begin transactions statements introduce a new transaction level. Every
  1328. // transaction level must be eventually balanced by exactly one of COMMIT or
  1329. // ROLLBACK statements. Note that when a transaction is roll-backed because of
  1330. // a statement failure then no explicit balancing of the respective BEGIN
  1331. // TRANSACTION is statement is required nor permitted.
  1332. //
  1333. // Failure to properly balance any opened transaction level may cause dead
  1334. // locks and/or lose of data updated in the uppermost opened but never properly
  1335. // closed transaction level.
  1336. //
  1337. // BeginTransactionStmt = "BEGIN" "TRANSACTION" .
  1338. //
  1339. // For example
  1340. //
  1341. // BEGIN TRANSACTION;
  1342. // INSERT INTO foo VALUES (42, 3.14);
  1343. // INSERT INTO foo VALUES (-1, 2.78);
  1344. // COMMIT;
  1345. //
  1346. // Mandatory transactions
  1347. //
  1348. // A database cannot be updated (mutated) outside of a transaction. Statements
  1349. // requiring a transaction
  1350. //
  1351. // ALTER TABLE
  1352. // COMMIT
  1353. // CREATE INDEX
  1354. // CREATE TABLE
  1355. // DELETE FROM
  1356. // DROP INDEX
  1357. // DROP TABLE
  1358. // INSERT INTO
  1359. // ROLLBACK
  1360. // TRUNCATE TABLE
  1361. // UPDATE
  1362. //
  1363. // A database is effectively read only outside of a transaction. Statements not
  1364. // requiring a transaction
  1365. //
  1366. // BEGIN TRANSACTION
  1367. // SELECT FROM
  1368. //
  1369. // COMMIT
  1370. //
  1371. // The commit statement closes the innermost transaction nesting level. If
  1372. // that's the outermost level then the updates to the DB made by the
  1373. // transaction are atomically made persistent.
  1374. //
  1375. // CommitStmt = "COMMIT" .
  1376. //
  1377. // For example
  1378. //
  1379. // BEGIN TRANSACTION;
  1380. // INSERT INTO AccountA (Amount) VALUES ($1);
  1381. // INSERT INTO AccountB (Amount) VALUES (-$1);
  1382. // COMMIT;
  1383. //
  1384. // CREATE INDEX
  1385. //
  1386. // Create index statements create new indices. Index is a named projection of
  1387. // ordered values of a table column to the respective records. As a special
  1388. // case the id() of the record can be indexed. Index name must not be the same
  1389. // as any of the existing tables and it also cannot be the same as of any
  1390. // column name of the table the index is on.
  1391. //
  1392. // CreateIndexStmt = "CREATE" [ "UNIQUE" ] "INDEX" [ "IF" "NOT" "EXISTS" ]
  1393. // IndexName "ON" TableName "(" ExpressionList ")" .
  1394. //
  1395. // For example
  1396. //
  1397. // BEGIN TRANSACTION;
  1398. // CREATE TABLE Orders (CustomerID int, Date time);
  1399. // CREATE INDEX OrdersID ON Orders (id());
  1400. // CREATE INDEX OrdersDate ON Orders (Date);
  1401. // CREATE TABLE Items (OrderID int, ProductID int, Qty int);
  1402. // CREATE INDEX ItemsOrderID ON Items (OrderID);
  1403. // COMMIT;
  1404. //
  1405. // Now certain SELECT statements may use the indices to speed up joins and/or
  1406. // to speed up record set filtering when the WHERE clause is used; or the
  1407. // indices might be used to improve the performance when the ORDER BY clause is
  1408. // present.
  1409. //
  1410. // The UNIQUE modifier requires the indexed values tuple to be index-wise
  1411. // unique or have all values NULL.
  1412. //
  1413. // The optional IF NOT EXISTS clause makes the statement a no operation if the
  1414. // index already exists.
  1415. //
  1416. // Simple index
  1417. //
  1418. // A simple index consists of only one expression which must be either a column
  1419. // name or the built-in id().
  1420. //
  1421. // Expression list index
  1422. //
  1423. // A more complex and more general index is one that consists of more than one
  1424. // expression or its single expression does not qualify as a simple index. In
  1425. // this case the type of all expressions in the list must be one of the non
  1426. // blob-like types.
  1427. //
  1428. // Note: Blob-like types are blob, bigint, bigrat, time and duration.
  1429. //
  1430. // CREATE TABLE
  1431. //
  1432. // Create table statements create new tables. A column definition declares the
  1433. // column name and type. Table names and column names are case sensitive.
  1434. // Neither a table or an index of the same name may exist in the DB.
  1435. //
  1436. // CreateTableStmt = "CREATE" "TABLE" [ "IF" "NOT" "EXISTS" ] TableName
  1437. // "(" ColumnDef { "," ColumnDef } [ "," ] ")" .
  1438. //
  1439. // ColumnDef = ColumnName Type [ "NOT" "NULL" | Expression ] [ "DEFAULT" Expression ] .
  1440. // ColumnName = identifier .
  1441. // TableName = identifier .
  1442. //
  1443. // For example
  1444. //
  1445. // BEGIN TRANSACTION;
  1446. // CREATE TABLE department (
  1447. // DepartmentID int,
  1448. // DepartmentName string,
  1449. // );
  1450. // CREATE TABLE employee (
  1451. // LastName string,
  1452. // DepartmentID int,
  1453. // );
  1454. // COMMIT;
  1455. //
  1456. // The optional IF NOT EXISTS clause makes the statement a no operation if the
  1457. // table already exists.
  1458. //
  1459. // The optional constraint clause has two forms. The first one is found in many
  1460. // SQL dialects.
  1461. //
  1462. // BEGIN TRANSACTION;
  1463. // CREATE TABLE department (
  1464. // DepartmentID int,
  1465. // DepartmentName string NOT NULL,
  1466. // );
  1467. // COMMIT;
  1468. //
  1469. // This form prevents the data in column DepartmentName to be NULL.
  1470. //
  1471. // The second form allows an arbitrary boolean expression to be used to
  1472. // validate the column. If the value of the expression is true then the
  1473. // validation succeeded. If the value of the expression is false or NULL then
  1474. // the validation fails. If the value of the expression is not of type bool an
  1475. // error occurs.
  1476. //
  1477. // BEGIN TRANSACTION;
  1478. // CREATE TABLE department (
  1479. // DepartmentID int,
  1480. // DepartmentName string DepartmentName IN ("HQ", "R/D", "Lab", "HR"),
  1481. // );
  1482. // COMMIT;
  1483. //
  1484. // BEGIN TRANSACTION;
  1485. // CREATE TABLE t (
  1486. // TimeStamp time TimeStamp < now() && since(TimeStamp) < duration("10s"),
  1487. // Event string Event != "" && Event like "[0-9]+:[ \t]+.*",
  1488. // );
  1489. // COMMIT;
  1490. //
  1491. // The optional DEFAULT clause is an expression which, if present, is
  1492. // substituted instead of a NULL value when the colum is assigned a value.
  1493. //
  1494. // BEGIN TRANSACTION;
  1495. // CREATE TABLE department (
  1496. // DepartmentID int,
  1497. // DepartmentName string DepartmentName IN ("HQ", "R/D", "Lab", "HR") DEFAULT "HQ",
  1498. // );
  1499. // COMMIT;
  1500. //
  1501. // Note that the constraint and/or default expressions may refer to other
  1502. // columns by name:
  1503. //
  1504. // BEGIN TRANSACTION;
  1505. // CREATE TABLE t (
  1506. // a int,
  1507. // b int b > a && b < c DEFAULT (a+c)/2,
  1508. // c int,
  1509. // );
  1510. // COMMIT;
  1511. //
  1512. //
  1513. // Constraints and defaults
  1514. //
  1515. // When a table row is inserted by the INSERT INTO statement or when a table
  1516. // row is updated by the UPDATE statement, the order of operations is as
  1517. // follows:
  1518. //
  1519. // 1. The new values of the affected columns are set and the values of all the
  1520. // row columns become the named values which can be referred to in default
  1521. // expressions evaluated in step 2.
  1522. //
  1523. // 2. If any row column value is NULL and the DEFAULT clause is present in the
  1524. // column's definition, the default expression is evaluated and its value is
  1525. // set as the respective column value.
  1526. //
  1527. // 3. The values, potentially updated, of row columns become the named values
  1528. // which can be referred to in constraint expressions evaluated during step 4.
  1529. //
  1530. // 4. All row columns which definition has the constraint clause present will
  1531. // have that constraint checked. If any constraint violation is detected, the
  1532. // overall operation fails and no changes to the table are made.
  1533. //
  1534. // DELETE FROM
  1535. //
  1536. // Delete from statements remove rows from a table, which must exist.
  1537. //
  1538. // DeleteFromStmt = "DELETE" "FROM" TableName [ WhereClause ] .
  1539. //
  1540. // For example
  1541. //
  1542. // BEGIN TRANSACTION;
  1543. // DELETE FROM DepartmentID
  1544. // WHERE DepartmentName == "Ponies";
  1545. // COMMIT;
  1546. //
  1547. // If the WHERE clause is not present then all rows are removed and the
  1548. // statement is equivalent to the TRUNCATE TABLE statement.
  1549. //
  1550. // DROP INDEX
  1551. //
  1552. // Drop index statements remove indices from the DB. The index must exist.
  1553. //
  1554. // DropIndexStmt = "DROP" "INDEX" [ "IF" "EXISTS" ] IndexName .
  1555. // IndexName = identifier .
  1556. //
  1557. // For example
  1558. //
  1559. // BEGIN TRANSACTION;
  1560. // DROP INDEX ItemsOrderID;
  1561. // COMMIT;
  1562. //
  1563. // The optional IF EXISTS clause makes the statement a no operation if the
  1564. // index does not exist.
  1565. //
  1566. // DROP TABLE
  1567. //
  1568. // Drop table statements remove tables from the DB. The table must exist.
  1569. //
  1570. // DropTableStmt = "DROP" "TABLE" [ "IF" "EXISTS" ] TableName .
  1571. //
  1572. // For example
  1573. //
  1574. // BEGIN TRANSACTION;
  1575. // DROP TABLE Inventory;
  1576. // COMMIT;
  1577. //
  1578. // The optional IF EXISTS clause makes the statement a no operation if the
  1579. // table does not exist.
  1580. //
  1581. // INSERT INTO
  1582. //
  1583. // Insert into statements insert new rows into tables. New rows come from
  1584. // literal data, if using the VALUES clause, or are a result of select
  1585. // statement. In the later case the select statement is fully evaluated before
  1586. // the insertion of any rows is performed, allowing to insert values calculated
  1587. // from the same table rows are to be inserted into. If the ColumnNameList part
  1588. // is omitted then the number of values inserted in the row must be the same as
  1589. // are columns in the table. If the ColumnNameList part is present then the
  1590. // number of values per row must be same as the same number of column names.
  1591. // All other columns of the record are set to NULL. The type of the value
  1592. // assigned to a column must be the same as is the column's type or the value
  1593. // must be NULL.
  1594. //
  1595. // InsertIntoStmt = "INSERT" "INTO" TableName [ "(" ColumnNameList ")" ] ( Values | SelectStmt ) .
  1596. //
  1597. // ColumnNameList = ColumnName { "," ColumnName } [ "," ] .
  1598. // Values = "VALUES" "(" ExpressionList ")" { "," "(" ExpressionList ")" } [ "," ] .
  1599. //
  1600. // For example
  1601. //
  1602. // BEGIN TRANSACTION;
  1603. // INSERT INTO department (DepartmentID) VALUES (42);
  1604. //
  1605. // INSERT INTO department (
  1606. // DepartmentName,
  1607. // DepartmentID,
  1608. // )
  1609. // VALUES (
  1610. // "R&D",
  1611. // 42,
  1612. // );
  1613. //
  1614. // INSERT INTO department VALUES
  1615. // (42, "R&D"),
  1616. // (17, "Sales"),
  1617. // ;
  1618. // COMMIT;
  1619. //
  1620. // BEGIN TRANSACTION;
  1621. // INSERT INTO department (DepartmentName, DepartmentID)
  1622. // SELECT DepartmentName+"/headquarters", DepartmentID+1000
  1623. // FROM department;
  1624. // COMMIT;
  1625. //
  1626. // If any of the columns of the table were defined using the optional
  1627. // constraints clause or the optional defaults clause then those are processed
  1628. // on a per row basis. The details are discussed in the "Constraints and
  1629. // defaults" chapter below the CREATE TABLE statement documentation.
  1630. //
  1631. // Explain statement
  1632. //
  1633. // Explain statement produces a recordset consisting of lines of text which
  1634. // describe the execution plan of a statement, if any.
  1635. //
  1636. // ExplainStmt = "EXPLAIN" Statement .
  1637. //
  1638. // For example, the QL tool treats the explain statement specially and outputs
  1639. // the joined lines:
  1640. //
  1641. // $ ql 'create table t(i int); create table u(j int)'
  1642. // $ ql 'explain select * from t, u where t.i > 42 && u.j < 314'
  1643. // ┌Compute Cartesian product of
  1644. // │ ┌Iterate all rows of table "t"
  1645. // │ └Output field names ["i"]
  1646. // │ ┌Iterate all rows of table "u"
  1647. // │ └Output field names ["j"]
  1648. // └Output field names ["t.i" "u.j"]
  1649. // ┌Filter on t.i > 42 && u.j < 314
  1650. // │Possibly useful indices
  1651. // │CREATE INDEX xt_i ON t(i);
  1652. // │CREATE INDEX xu_j ON u(j);
  1653. // └Output field names ["t.i" "u.j"]
  1654. // $ ql 'CREATE INDEX xt_i ON t(i); CREATE INDEX xu_j ON u(j);'
  1655. // $ ql 'explain select * from t, u where t.i > 42 && u.j < 314'
  1656. // ┌Compute Cartesian product of
  1657. // │ ┌Iterate all rows of table "t" using index "xt_i" where i > 42
  1658. // │ └Output field names ["i"]
  1659. // │ ┌Iterate all rows of table "u" using index "xu_j" where j < 314
  1660. // │ └Output field names ["j"]
  1661. // └Output field names ["t.i" "u.j"]
  1662. // $ ql 'explain select * from t where i > 12 and i between 10 and 20 and i < 42'
  1663. // ┌Iterate all rows of table "t" using index "xt_i" where i > 12 && i <= 20
  1664. // └Output field names ["i"]
  1665. // $
  1666. //
  1667. // The explanation may aid in uderstanding how a statement/query would be
  1668. // executed and if indices are used as expected - or which indices may possibly
  1669. // improve the statement performance. The create index statements above were
  1670. // directly copy/pasted in the terminal from the suggestions provided by the
  1671. // filter recordset pipeline part returned by the explain statement.
  1672. //
  1673. // If the statement has nothing special in its plan, the result is the original
  1674. // statement.
  1675. //
  1676. // $ ql 'explain delete from t where 42 < i'
  1677. // DELETE FROM t WHERE i > 42;
  1678. // $
  1679. //
  1680. // To get an explanation of the select statement of the IN predicate, use the EXPLAIN
  1681. // statement with that particular select statement.
  1682. //
  1683. // $ ql 'explain select * from t where i in (select j from u where j > 0)'
  1684. // ┌Iterate all rows of table "t"
  1685. // └Output field names ["i"]
  1686. // ┌Filter on i IN (SELECT j FROM u WHERE j > 0;)
  1687. // └Output field names ["i"]
  1688. // $ ql 'explain select j from u where j > 0'
  1689. // ┌Iterate all rows of table "u" using index "xu_j" where j > 0
  1690. // └Output field names ["j"]
  1691. // $
  1692. //
  1693. // ROLLBACK
  1694. //
  1695. // The rollback statement closes the innermost transaction nesting level
  1696. // discarding any updates to the DB made by it. If that's the outermost level
  1697. // then the effects on the DB are as if the transaction never happened.
  1698. //
  1699. // RollbackStmt = "ROLLBACK" .
  1700. //
  1701. // For example
  1702. //
  1703. // // First statement list
  1704. // BEGIN TRANSACTION
  1705. // SELECT * INTO tmp FROM foo;
  1706. // INSERT INTO tmp SELECT * from bar;
  1707. // SELECT * from tmp;
  1708. //
  1709. // The (temporary) record set from the last statement is returned and can be
  1710. // processed by the client.
  1711. //
  1712. // // Second statement list
  1713. // ROLLBACK;
  1714. //
  1715. // In this case the rollback is the same as 'DROP TABLE tmp;' but it can be a
  1716. // more complex operation.
  1717. //
  1718. // SELECT FROM
  1719. //
  1720. // Select from statements produce recordsets. The optional DISTINCT modifier
  1721. // ensures all rows in the result recordset are unique. Either all of the
  1722. // resulting fields are returned ('*') or only those named in FieldList.
  1723. //
  1724. // RecordSetList is a list of table names or parenthesized select statements,
  1725. // optionally (re)named using the AS clause.
  1726. //
  1727. // The result can be filtered using a WhereClause and orderd by the OrderBy
  1728. // clause.
  1729. //
  1730. // SelectStmt = "SELECT" [ "DISTINCT" ] ( "*" | FieldList ) "FROM" RecordSetList
  1731. // [ JoinClause ] [ WhereClause ] [ GroupByClause ] [ OrderBy ] [ Limit ] [ Offset ].
  1732. //
  1733. // JoinClause = ( "LEFT" | "RIGHT" | "FULL" ) [ "OUTER" ] "JOIN" RecordSet "ON" Expression .
  1734. //
  1735. // RecordSet = ( TableName | "(" SelectStmt [ ";" ] ")" ) [ "AS" identifier ] .
  1736. // RecordSetList = RecordSet { "," RecordSet } [ "," ] .
  1737. //
  1738. // For example
  1739. //
  1740. // SELECT * FROM Stock;
  1741. //
  1742. // SELECT DepartmentID
  1743. // FROM department
  1744. // WHERE DepartmentID == 42
  1745. // ORDER BY DepartmentName;
  1746. //
  1747. // SELECT employee.LastName
  1748. // FROM department, employee
  1749. // WHERE department.DepartmentID == employee.DepartmentID
  1750. // ORDER BY DepartmentID;
  1751. //
  1752. // If Recordset is a nested, parenthesized SelectStmt then it must be given a
  1753. // name using the AS clause if its field are to be accessible in expressions.
  1754. //
  1755. // SELECT a.b, c.d
  1756. // FROM
  1757. // x AS a,
  1758. // (
  1759. // SELECT * FROM y;
  1760. // ) AS c
  1761. // WHERE a.e > c.e;
  1762. //
  1763. // Fields naming rules
  1764. //
  1765. // A field is an named expression. Identifiers, not used as a type in
  1766. // conversion or a function name in the Call clause, denote names of (other)
  1767. // fields, values of which should be used in the expression.
  1768. //
  1769. // Field = Expression [ "AS" identifier ] .
  1770. //
  1771. // The expression can be named using the AS clause. If the AS clause is not
  1772. // present and the expression consists solely of a field name, then that field
  1773. // name is used as the name of the resulting field. Otherwise the field is
  1774. // unnamed.
  1775. //
  1776. // For example
  1777. //
  1778. // SELECT 314, 42 as AUQLUE, DepartmentID, DepartmentID+1000, LastName as Name from employee;
  1779. // // Fields are []string{"", "AUQLUE", "DepartmentID", "", "Name"}
  1780. //
  1781. // The SELECT statement can optionally enumerate the desired/resulting fields
  1782. // in a list.
  1783. //
  1784. // FieldList = Field { "," Field } [ "," ] .
  1785. //
  1786. // No two identical field names can appear in the list.
  1787. //
  1788. // SELECT DepartmentID, LastName, DepartmentID from employee;
  1789. // // duplicate field name "DepartmentID"
  1790. //
  1791. // SELECT DepartmentID, LastName, DepartmentID as ID2 from employee;
  1792. // // works
  1793. //
  1794. // When more than one record set is used in the FROM clause record set list,
  1795. // the result record set field names are rewritten to be qualified using
  1796. // the record set names.
  1797. //
  1798. // SELECT * FROM employee, department;
  1799. // // Fields are []string{"employee.LastName", "employee.DepartmentID", "department.DepartmentID", "department.DepartmentName"
  1800. //
  1801. // If a particular record set doesn't have a name, its respective fields became
  1802. // unnamed.
  1803. //
  1804. // SELECT * FROM employee as e, ( SELECT * FROM department);
  1805. // // Fields are []string{"e.LastName", "e.DepartmentID", "", ""
  1806. //
  1807. // SELECT * FROM employee AS e, ( SELECT * FROM department) AS d;
  1808. // // Fields are []string{"e.LastName", "e.DepartmentID", "d.DepartmentID", "d.DepartmentName"
  1809. //
  1810. // Outer joins
  1811. //
  1812. // The optional JOIN clause, for example
  1813. //
  1814. // SELECT *
  1815. // FROM a
  1816. // LEFT OUTER JOIN b ON expr;
  1817. //
  1818. // is mostly equal to
  1819. //
  1820. // SELECT *
  1821. // FROM a, b
  1822. // WHERE expr;
  1823. //
  1824. // except that the rows from a which, when they appear in the cross join, never
  1825. // made expr to evaluate to true, are combined with a virtual row from b,
  1826. // containing all nulls, and added to the result set. For the RIGHT JOIN
  1827. // variant the discussed rules are used for rows from b not satisfying expr ==
  1828. // true and the virtual, all-null row "comes" from a. The FULL JOIN adds the
  1829. // respective rows which would be otherwise provided by the separate executions
  1830. // of the LEFT JOIN and RIGHT JOIN variants. For more thorough OUTER JOIN
  1831. // discussion please see the Wikipedia article at [10].
  1832. //
  1833. // Recordset ordering
  1834. //
  1835. // Resultins rows of a SELECT statement can be optionally ordered by the ORDER
  1836. // BY clause. Collating proceeds by considering the expressions in the
  1837. // expression list left to right until a collating order is determined. Any
  1838. // possibly remaining expressions are not evaluated.
  1839. //
  1840. // OrderBy = "ORDER" "BY" ExpressionList [ "ASC" | "DESC" ] .
  1841. //
  1842. // All of the expression values must yield an ordered type or NULL. Ordered
  1843. // types are defined in "Comparison operators". Collating of elements having a
  1844. // NULL value is different compared to what the comparison operators yield in
  1845. // expression evaluation (NULL result instead of a boolean value).
  1846. //
  1847. // Below, T denotes a non NULL value of any QL type.
  1848. //
  1849. // NULL < T
  1850. //
  1851. // NULL collates before any non NULL value (is considered smaller than T).
  1852. //
  1853. // NULL == NULL
  1854. //
  1855. // Two NULLs have no collating order (are considered equal).
  1856. //
  1857. // Recordset filtering
  1858. //
  1859. // The WHERE clause restricts records considered by some statements, like
  1860. // SELECT FROM, DELETE FROM, or UPDATE.
  1861. //
  1862. // expression value consider the record
  1863. // ---------------- -------------------
  1864. // true yes
  1865. // false or NULL no
  1866. //
  1867. // It is an error if the expression evaluates to a non null value of non bool
  1868. // type.
  1869. //
  1870. // WhereClause = "WHERE" Expression .
  1871. //
  1872. // Recordset grouping
  1873. //
  1874. // The GROUP BY clause is used to project rows having common values into a
  1875. // smaller set of rows.
  1876. //
  1877. // For example
  1878. //
  1879. // SELECT Country, sum(Qty) FROM Sales GROUP BY Country;
  1880. //
  1881. // SELECT Country, Product FROM Sales GROUP BY Country, Product;
  1882. //
  1883. // SELECT DISTINCT Country, Product FROM Sales;
  1884. //
  1885. // Using the GROUP BY without any aggregate functions in the selected fields is
  1886. // in certain cases equal to using the DISTINCT modifier. The last two examples
  1887. // above produce the same resultsets.
  1888. //
  1889. // GroupByClause = "GROUP BY" ColumnNameList .
  1890. //
  1891. // Skipping records
  1892. //
  1893. // The optional OFFSET clause allows to ignore first N records. For example
  1894. //
  1895. // SELECT * FROM t OFFSET 10;
  1896. //
  1897. // The above will produce only rows 11, 12, ... of the record set, if they
  1898. // exist. The value of the expression must a non negative integer, but not
  1899. // bigint or duration.
  1900. //
  1901. // Offset = "OFFSET" Expression .
  1902. //
  1903. // Limiting the result set size
  1904. //
  1905. // The optional LIMIT clause allows to ignore all but first N records. For
  1906. // example
  1907. //
  1908. // SELECT * FROM t LIMIT 10;
  1909. //
  1910. // The above will return at most the first 10 records of the record set. The
  1911. // value of the expression must a non negative integer, but not bigint or
  1912. // duration.
  1913. //
  1914. // Limit = "Limit" Expression .
  1915. //
  1916. // The LIMIT and OFFSET clauses can be combined. For example
  1917. //
  1918. // SELECT * FROM t LIMIT 5 OFFSET 3;
  1919. //
  1920. // Considering table t has, say 10 records, the above will produce only records
  1921. // 4 - 8.
  1922. //
  1923. // #1: Ignore 1/3
  1924. // #2: Ignore 2/3
  1925. // #3: Ignore 3/3
  1926. // #4: Return 1/5
  1927. // #5: Return 2/5
  1928. // #6: Return 3/5
  1929. // #7: Return 4/5
  1930. // #8: Return 5/5
  1931. //
  1932. // After returning record #8, no more result rows/records are computed.
  1933. //
  1934. // Select statement evaluation order
  1935. //
  1936. // 1. The FROM clause is evaluated, producing a Cartesian product of its source
  1937. // record sets (tables or nested SELECT statements).
  1938. //
  1939. // 2. If present, the JOIN cluase is evaluated on the result set of the
  1940. // previous evaluation and the recordset specified by the JOIN clause. (...
  1941. // JOIN Recordset ON ...)
  1942. //
  1943. // 3. If present, the WHERE clause is evaluated on the result set of the
  1944. // previous evaluation.
  1945. //
  1946. // 4. If present, the GROUP BY clause is evaluated on the result set of the
  1947. // previous evaluation(s).
  1948. //
  1949. // 5. The SELECT field expressions are evaluated on the result set of the
  1950. // previous evaluation(s).
  1951. //
  1952. // 6. If present, the DISTINCT modifier is evaluated on the result set of the
  1953. // previous evaluation(s).
  1954. //
  1955. // 7. If present, the ORDER BY clause is evaluated on the result set of the
  1956. // previous evaluation(s).
  1957. //
  1958. // 8. If present, the OFFSET clause is evaluated on the result set of the
  1959. // previous evaluation(s). The offset expression is evaluated once for the
  1960. // first record produced by the previous evaluations.
  1961. //
  1962. // 9. If present, the LIMIT clause is evaluated on the result set of the
  1963. // previous evaluation(s). The limit expression is evaluated once for the first
  1964. // record produced by the previous evaluations.
  1965. //
  1966. //
  1967. // TRUNCATE TABLE
  1968. //
  1969. // Truncate table statements remove all records from a table. The table must
  1970. // exist.
  1971. //
  1972. // TruncateTableStmt = "TRUNCATE" "TABLE" TableName .
  1973. //
  1974. // For example
  1975. //
  1976. // BEGIN TRANSACTION
  1977. // TRUNCATE TABLE department;
  1978. // COMMIT;
  1979. //
  1980. // UPDATE
  1981. //
  1982. // Update statements change values of fields in rows of a table.
  1983. //
  1984. // UpdateStmt = "UPDATE" TableName [ "SET" ] AssignmentList [ WhereClause ] .
  1985. //
  1986. // AssignmentList = Assignment { "," Assignment } [ "," ] .
  1987. // Assignment = ColumnName "=" Expression .
  1988. //
  1989. // For example
  1990. //
  1991. // BEGIN TRANSACTION
  1992. // UPDATE department
  1993. // DepartmentName = DepartmentName + " dpt.",
  1994. // DepartmentID = 1000+DepartmentID,
  1995. // WHERE DepartmentID < 1000;
  1996. // COMMIT;
  1997. //
  1998. // Note: The SET clause is optional.
  1999. //
  2000. // If any of the columns of the table were defined using the optional
  2001. // constraints clause or the optional defaults clause then those are processed
  2002. // on a per row basis. The details are discussed in the "Constraints and
  2003. // defaults" chapter below the CREATE TABLE statement documentation.
  2004. //
  2005. // System Tables
  2006. //
  2007. // To allow to query for DB meta data, there exist specially named tables, some
  2008. // of them being virtual.
  2009. //
  2010. // Note: Virtual system tables may have fake table-wise unique but meaningless
  2011. // and unstable record IDs. Do not apply the built-in id() to any system table.
  2012. //
  2013. // Tables Table
  2014. //
  2015. // The table __Table lists all tables in the DB. The schema is
  2016. //
  2017. // CREATE TABLE __Table (Name string, Schema string);
  2018. //
  2019. // The Schema column returns the statement to (re)create table Name. This table
  2020. // is virtual.
  2021. //
  2022. // Columns Table
  2023. //
  2024. // The table __Colum lists all columns of all tables in the DB. The schema is
  2025. //
  2026. // CREATE TABLE __Column (TableName string, Ordinal int, Name string, Type string);
  2027. //
  2028. // The Ordinal column defines the 1-based index of the column in the record.
  2029. // This table is virtual.
  2030. //
  2031. // Columns2 Table
  2032. //
  2033. // The table __Colum2 lists all columns of all tables in the DB which have the
  2034. // constraint NOT NULL or which have a constraint expression defined or which
  2035. // have a default expression defined. The schema is
  2036. //
  2037. // CREATE TABLE __Column2 (TableName string, Name string, NotNull bool, ConstraintExpr string, DefaultExpr string)
  2038. //
  2039. // It's possible to obtain a consolidated recordset for all properties of all
  2040. // DB columns using
  2041. //
  2042. // SELECT
  2043. // __Column.TableName, __Column.Ordinal, __Column.Name, __Column.Type,
  2044. // __Column2.NotNull, __Column2.ConstraintExpr, __Column2.DefaultExpr,
  2045. // FROM __Column
  2046. // LEFT JOIN __Column2
  2047. // ON __Column.TableName == __Column2.TableName && __Column.Name == __Column2.Name
  2048. // ORDER BY __Column.TableName, __Column.Ordinal;
  2049. //
  2050. // The Name column is the column name in TableName.
  2051. //
  2052. // Indices table
  2053. //
  2054. // The table __Index lists all indices in the DB. The schema is
  2055. //
  2056. // CREATE TABLE __Index (TableName string, ColumnName string, Name string, IsUnique bool);
  2057. //
  2058. // The IsUnique columns reflects if the index was created using the optional
  2059. // UNIQUE clause. This table is virtual.
  2060. //
  2061. // Built-in functions
  2062. //
  2063. // Built-in functions are predeclared.
  2064. //
  2065. // Average
  2066. //
  2067. // The built-in aggregate function avg returns the average of values of an
  2068. // expression. Avg ignores NULL values, but returns NULL if all values of a
  2069. // column are NULL or if avg is applied to an empty record set.
  2070. //
  2071. // func avg(e numeric) typeof(e)
  2072. //
  2073. // The column values must be of a numeric type.
  2074. //
  2075. // SELECT salesperson, avg(sales) FROM salesforce GROUP BY salesperson;
  2076. //
  2077. // Contains
  2078. //
  2079. // The built-in function contains returns true if substr is within s.
  2080. //
  2081. // func contains(s, substr string) bool
  2082. //
  2083. // If any argument to contains is NULL the result is NULL.
  2084. //
  2085. // Count
  2086. //
  2087. // The built-in aggregate function count returns how many times an expression
  2088. // has a non NULL values or the number of rows in a record set. Note: count()
  2089. // returns 0 for an empty record set.
  2090. //
  2091. // func count() int // The number of rows in a record set.
  2092. // func count(*) int // Equivalent to count().
  2093. // func count(e expression) int // The number of cases where the expression value is not NULL.
  2094. //
  2095. // For example
  2096. //
  2097. // SELECT count() FROM department; // # of rows
  2098. //
  2099. // SELECT count(*) FROM department; // # of rows
  2100. //
  2101. // SELECT count(DepartmentID) FROM department; // # of records with non NULL field DepartmentID
  2102. //
  2103. // SELECT count()-count(DepartmentID) FROM department; // # of records with NULL field DepartmentID
  2104. //
  2105. // SELECT count(foo+bar*3) AS y FROM t; // # of cases where 'foo+bar*3' is non NULL
  2106. //
  2107. // Date
  2108. //
  2109. // Date returns the time corresponding to
  2110. //
  2111. // yyyy-mm-dd hh:mm:ss + nsec nanoseconds
  2112. //
  2113. // in the appropriate zone for that time in the given location.
  2114. //
  2115. // The month, day, hour, min, sec, and nsec values may be outside their usual
  2116. // ranges and will be normalized during the conversion. For example, October 32
  2117. // converts to November 1.
  2118. //
  2119. // A daylight savings time transition skips or repeats times. For example, in
  2120. // the United States, March 13, 2011 2:15am never occurred, while November 6,
  2121. // 2011 1:15am occurred twice. In such cases, the choice of time zone, and
  2122. // therefore the time, is not well-defined. Date returns a time that is correct
  2123. // in one of the two zones involved in the transition, but it does not
  2124. // guarantee which.
  2125. //
  2126. // func date(year, month, day, hour, min, sec, nsec int, loc string) time
  2127. //
  2128. // A location maps time instants to the zone in use at that time. Typically,
  2129. // the location represents the collection of time offsets in use in a
  2130. // geographical area, such as "CEST" and "CET" for central Europe. "local"
  2131. // represents the system's local time zone. "UTC" represents Universal
  2132. // Coordinated Time (UTC).
  2133. //
  2134. // The month specifies a month of the year (January = 1, ...).
  2135. //
  2136. // If any argument to date is NULL the result is NULL.
  2137. //
  2138. // Day
  2139. //
  2140. // The built-in function day returns the day of the month specified by t.
  2141. //
  2142. // func day(t time) int
  2143. //
  2144. // If the argument to day is NULL the result is NULL.
  2145. //
  2146. // Format time
  2147. //
  2148. // The built-in function formatTime returns a textual representation of the
  2149. // time value formatted according to layout, which defines the format by
  2150. // showing how the reference time,
  2151. //
  2152. // Mon Jan 2 15:04:05 -0700 MST 2006
  2153. //
  2154. // would be displayed if it were the value; it serves as an example of the
  2155. // desired output. The same display rules will then be applied to the time
  2156. // value.
  2157. //
  2158. // func formatTime(t time, layout string) string
  2159. //
  2160. // If any argument to formatTime is NULL the result is NULL.
  2161. //
  2162. // NOTE: The string value of the time zone, like "CET" or "ACDT", is dependent
  2163. // on the time zone of the machine the function is run on. For example, if the
  2164. // t value is in "CET", but the machine is in "ACDT", instead of "CET" the
  2165. // result is "+0100". This is the same what Go (time.Time).String() returns and
  2166. // in fact formatTime directly calls t.String().
  2167. //
  2168. // formatTime(date(2006, 1, 2, 15, 4, 5, 999999999, "CET"))
  2169. //
  2170. // returns
  2171. //
  2172. // 2006-01-02 15:04:05.999999999 +0100 CET
  2173. //
  2174. // on a machine in the CET time zone, but may return
  2175. //
  2176. // 2006-01-02 15:04:05.999999999 +0100 +0100
  2177. //
  2178. // on a machine in the ACDT zone. The time value is in both cases the same so
  2179. // its ordering and comparing is correct. Only the display value can differ.
  2180. //
  2181. // Format numbers
  2182. //
  2183. // The built-in functions formatFloat and formatInt format numbers
  2184. // to strings using go's number format functions in the `strconv` package. For
  2185. // all three functions, only the first argument is mandatory. The default values
  2186. // of the rest are shown in the examples. If the first argument is NULL, the
  2187. // result is NULL.
  2188. //
  2189. // formatFloat(43.2[, 'g', -1, 64]) string
  2190. //
  2191. // returns
  2192. //
  2193. // "43.2"
  2194. //
  2195. // formatInt(-42[, 10]) string
  2196. //
  2197. // returns
  2198. //
  2199. // "-42"
  2200. //
  2201. // formatInt(uint32(42)[, 10]) string
  2202. //
  2203. // returns
  2204. //
  2205. // "42"
  2206. //
  2207. // Unlike the `strconv` equivalent, the formatInt function handles all integer
  2208. // types, both signed and unsigned.
  2209. //
  2210. // HasPrefix
  2211. //
  2212. // The built-in function hasPrefix tests whether the string s begins with prefix.
  2213. //
  2214. // func hasPrefix(s, prefix string) bool
  2215. //
  2216. // If any argument to hasPrefix is NULL the result is NULL.
  2217. //
  2218. // HasSuffix
  2219. //
  2220. // The built-in function hasSuffix tests whether the string s ends with suffix.
  2221. //
  2222. // func hasSuffix(s, suffix string) bool
  2223. //
  2224. // If any argument to hasSuffix is NULL the result is NULL.
  2225. //
  2226. // Hour
  2227. //
  2228. // The built-in function hour returns the hour within the day specified by t,
  2229. // in the range [0, 23].
  2230. //
  2231. // func hour(t time) int
  2232. //
  2233. // If the argument to hour is NULL the result is NULL.
  2234. //
  2235. // Hours
  2236. //
  2237. // The built-in function hours returns the duration as a floating point number
  2238. // of hours.
  2239. //
  2240. // func hours(d duration) float
  2241. //
  2242. // If the argument to hours is NULL the result is NULL.
  2243. //
  2244. // Record id
  2245. //
  2246. // The built-in function id takes zero or one arguments. If no argument is
  2247. // provided, id() returns a table-unique automatically assigned numeric
  2248. // identifier of type int. Ids of deleted records are not reused unless the DB
  2249. // becomes completely empty (has no tables).
  2250. //
  2251. // func id() int
  2252. //
  2253. // For example
  2254. //
  2255. // SELECT id(), LastName
  2256. // FROM employee;
  2257. //
  2258. // If id() without arguments is called for a row which is not a table record
  2259. // then the result value is NULL.
  2260. //
  2261. // For example
  2262. //
  2263. // SELECT id(), e.LastName, e.DepartmentID, d.DepartmentID
  2264. // FROM
  2265. // employee AS e,
  2266. // department AS d,
  2267. // WHERE e.DepartmentID == d.DepartmentID;
  2268. // // Will always return NULL in first field.
  2269. //
  2270. // SELECT e.ID, e.LastName, e.DepartmentID, d.DepartmentID
  2271. // FROM
  2272. // (SELECT id() AS ID, LastName, DepartmentID FROM employee) AS e,
  2273. // department as d,
  2274. // WHERE e.DepartmentID == d.DepartmentID;
  2275. // // Will work.
  2276. //
  2277. // If id() has one argument it must be a table name of a table in a cross join.
  2278. //
  2279. // For example
  2280. //
  2281. // SELECT *
  2282. // FROM foo, bar
  2283. // WHERE bar.fooID == id(foo)
  2284. // ORDER BY id(foo);
  2285. //
  2286. // Length
  2287. //
  2288. // The built-in function len takes a string argument and returns the lentgh of
  2289. // the string in bytes.
  2290. //
  2291. // func len(s string) int
  2292. //
  2293. // The expression len(s) is constant if s is a string constant.
  2294. //
  2295. // If the argument to len is NULL the result is NULL.
  2296. //
  2297. // Maximum
  2298. //
  2299. // The built-in aggregate function max returns the largest value of an
  2300. // expression in a record set. Max ignores NULL values, but returns NULL if
  2301. // all values of a column are NULL or if max is applied to an empty record set.
  2302. //
  2303. // func max(e expression) typeof(e) // The largest value of the expression.
  2304. //
  2305. // The expression values must be of an ordered type.
  2306. //
  2307. // For example
  2308. //
  2309. // SELECT department, max(sales) FROM t GROUP BY department;
  2310. //
  2311. // Minimum
  2312. //
  2313. // The built-in aggregate function min returns the smallest value of an
  2314. // expression in a record set. Min ignores NULL values, but returns NULL if
  2315. // all values of a column are NULL or if min is applied to an empty record set.
  2316. //
  2317. // func min(e expression) typeof(e) // The smallest value of the expression.
  2318. //
  2319. // For example
  2320. //
  2321. // SELECT a, min(b) FROM t GROUP BY a;
  2322. //
  2323. // The column values must be of an ordered type.
  2324. //
  2325. // Minute
  2326. //
  2327. // The built-in function minute returns the minute offset within the hour
  2328. // specified by t, in the range [0, 59].
  2329. //
  2330. // func minute(t time) int
  2331. //
  2332. // If the argument to minute is NULL the result is NULL.
  2333. //
  2334. // Minutes
  2335. //
  2336. // The built-in function minutes returns the duration as a floating point
  2337. // number of minutes.
  2338. //
  2339. // func minutes(d duration) float
  2340. //
  2341. // If the argument to minutes is NULL the result is NULL.
  2342. //
  2343. // Month
  2344. //
  2345. // The built-in function month returns the month of the year specified by t
  2346. // (January = 1, ...).
  2347. //
  2348. // func month(t time) int
  2349. //
  2350. // If the argument to month is NULL the result is NULL.
  2351. //
  2352. // Nanosecond
  2353. //
  2354. // The built-in function nanosecond returns the nanosecond offset within the
  2355. // second specified by t, in the range [0, 999999999].
  2356. //
  2357. // func nanosecond(t time) int
  2358. //
  2359. // If the argument to nanosecond is NULL the result is NULL.
  2360. //
  2361. // Nanoseconds
  2362. //
  2363. // The built-in function nanoseconds returns the duration as an integer
  2364. // nanosecond count.
  2365. //
  2366. // func nanoseconds(d duration) float
  2367. //
  2368. // If the argument to nanoseconds is NULL the result is NULL.
  2369. //
  2370. // Now
  2371. //
  2372. // The built-in function now returns the current local time.
  2373. //
  2374. // func now() time
  2375. //
  2376. // Parse time
  2377. //
  2378. // The built-in function parseTime parses a formatted string and returns the
  2379. // time value it represents. The layout defines the format by showing how the
  2380. // reference time,
  2381. //
  2382. // Mon Jan 2 15:04:05 -0700 MST 2006
  2383. //
  2384. // would be interpreted if it were the value; it serves as an example of the
  2385. // input format. The same interpretation will then be made to the input string.
  2386. //
  2387. // Elements omitted from the value are assumed to be zero or, when zero is
  2388. // impossible, one, so parsing "3:04pm" returns the time corresponding to Jan
  2389. // 1, year 0, 15:04:00 UTC (note that because the year is 0, this time is
  2390. // before the zero Time). Years must be in the range 0000..9999. The day of the
  2391. // week is checked for syntax but it is otherwise ignored.
  2392. //
  2393. // In the absence of a time zone indicator, parseTime returns a time in UTC.
  2394. //
  2395. // When parsing a time with a zone offset like -0700, if the offset corresponds
  2396. // to a time zone used by the current location, then parseTime uses that
  2397. // location and zone in the returned time. Otherwise it records the time as
  2398. // being in a fabricated location with time fixed at the given zone offset.
  2399. //
  2400. // When parsing a time with a zone abbreviation like MST, if the zone
  2401. // abbreviation has a defined offset in the current location, then that offset
  2402. // is used. The zone abbreviation "UTC" is recognized as UTC regardless of
  2403. // location. If the zone abbreviation is unknown, Parse records the time as
  2404. // being in a fabricated location with the given zone abbreviation and a zero
  2405. // offset. This choice means that such a time can be parses and reformatted
  2406. // with the same layout losslessly, but the exact instant used in the
  2407. // representation will differ by the actual zone offset. To avoid such
  2408. // problems, prefer time layouts that use a numeric zone offset.
  2409. //
  2410. // func parseTime(layout, value string) time
  2411. //
  2412. // If any argument to parseTime is NULL the result is NULL.
  2413. //
  2414. // Second
  2415. //
  2416. // The built-in function second returns the second offset within the minute
  2417. // specified by t, in the range [0, 59].
  2418. //
  2419. // func second(t time) int
  2420. //
  2421. // If the argument to second is NULL the result is NULL.
  2422. //
  2423. // Seconds
  2424. //
  2425. // The built-in function seconds returns the duration as a floating point
  2426. // number of seconds.
  2427. //
  2428. // func seconds(d duration) float
  2429. //
  2430. // If the argument to seconds is NULL the result is NULL.
  2431. //
  2432. // Since
  2433. //
  2434. // The built-in function since returns the time elapsed since t. It is
  2435. // shorthand for now()-t.
  2436. //
  2437. // func since(t time) duration
  2438. //
  2439. // If the argument to since is NULL the result is NULL.
  2440. //
  2441. // Sum
  2442. //
  2443. // The built-in aggregate function sum returns the sum of values of an
  2444. // expression for all rows of a record set. Sum ignores NULL values, but
  2445. // returns NULL if all values of a column are NULL or if sum is applied to an
  2446. // empty record set.
  2447. //
  2448. // func sum(e expression) typeof(e) // The sum of the values of the expression.
  2449. //
  2450. // The column values must be of a numeric type.
  2451. //
  2452. // SELECT salesperson, sum(sales) FROM salesforce GROUP BY salesperson;
  2453. //
  2454. // Time in a specific zone
  2455. //
  2456. // The built-in function timeIn returns t with the location information set to
  2457. // loc. For discussion of the loc argument please see date().
  2458. //
  2459. // func timeIn(t time, loc string) time
  2460. //
  2461. // If any argument to timeIn is NULL the result is NULL.
  2462. //
  2463. // Weekday
  2464. //
  2465. // The built-in function weekday returns the day of the week specified by t.
  2466. // Sunday == 0, Monday == 1, ...
  2467. //
  2468. // func weekday(t time) int
  2469. //
  2470. // If the argument to weekday is NULL the result is NULL.
  2471. //
  2472. // Year
  2473. //
  2474. // The built-in function year returns the year in which t occurs.
  2475. //
  2476. // func year(t time) int
  2477. //
  2478. // If the argument to year is NULL the result is NULL.
  2479. //
  2480. // Year day
  2481. //
  2482. // The built-in function yearDay returns the day of the year specified by t, in
  2483. // the range [1,365] for non-leap years, and [1,366] in leap years.
  2484. //
  2485. // func yearDay(t time) int
  2486. //
  2487. // If the argument to yearDay is NULL the result is NULL.
  2488. //
  2489. // Manipulating complex numbers
  2490. //
  2491. // Three functions assemble and disassemble complex numbers. The built-in
  2492. // function complex constructs a complex value from a floating-point real and
  2493. // imaginary part, while real and imag extract the real and imaginary parts of
  2494. // a complex value.
  2495. //
  2496. // complex(realPart, imaginaryPart floatT) complexT
  2497. // real(complexT) floatT
  2498. // imag(complexT) floatT
  2499. //
  2500. // The type of the arguments and return value correspond. For complex, the two
  2501. // arguments must be of the same floating-point type and the return type is the
  2502. // complex type with the corresponding floating-point constituents: complex64
  2503. // for float32, complex128 for float64. The real and imag functions together
  2504. // form the inverse, so for a complex value z, z == complex(real(z), imag(z)).
  2505. //
  2506. // If the operands of these functions are all constants, the return value is a
  2507. // constant.
  2508. //
  2509. // complex(2, -2) // complex128
  2510. // complex(1.0, -1.4) // complex128
  2511. // float32(math.Cos(math.Pi/2)) // float32
  2512. // complex(5, float32(-x)) // complex64
  2513. // imag(b) // float64
  2514. // real(complex(5, float32(-x))) // float32
  2515. //
  2516. // If any argument to any of complex, real, imag functions is NULL the result
  2517. // is NULL.
  2518. //
  2519. // Size guarantees
  2520. //
  2521. // For the numeric types, the following sizes are guaranteed
  2522. //
  2523. // type size in bytes
  2524. //
  2525. // byte, uint8, int8 1
  2526. // uint16, int16 2
  2527. // uint32, int32, float32 4
  2528. // uint, uint64, int, int64, float64, complex64 8
  2529. // complex128 16
  2530. //
  2531. // License
  2532. //
  2533. // Portions of this specification page are modifications based on work[2]
  2534. // created and shared by Google[3] and used according to terms described in the
  2535. // Creative Commons 3.0 Attribution License[4].
  2536. //
  2537. // This specification is licensed under the Creative Commons Attribution 3.0
  2538. // License, and code is licensed under a BSD license[5].
  2539. //
  2540. // References
  2541. //
  2542. // Links from the above documentation
  2543. //
  2544. // [1]: http://golang.org/ref/spec#Notation
  2545. // [2]: http://golang.org/ref/spec
  2546. // [3]: http://code.google.com/policies.html
  2547. // [4]: http://creativecommons.org/licenses/by/3.0/
  2548. // [5]: http://golang.org/LICENSE
  2549. // [6]: http://golang.org/pkg/regexp/#Regexp.MatchString
  2550. // [7]: http://developer.mimer.com/validator/sql-reserved-words.tml
  2551. // [8]: http://godoc.org/github.com/cznic/zappy
  2552. // [9]: http://www.w3schools.com/sql/sql_default.asp
  2553. // [10]: http://en.wikipedia.org/wiki/Join_(SQL)#Outer_join
  2554. //
  2555. // Implementation details
  2556. //
  2557. // This section is not part of the specification.
  2558. //
  2559. // Indices
  2560. //
  2561. // WARNING: The implementation of indices is new and it surely needs more time
  2562. // to become mature.
  2563. //
  2564. // Indices are used currently used only by the WHERE clause. The following
  2565. // expression patterns of 'WHERE expression' are recognized and trigger index
  2566. // use.
  2567. //
  2568. // - WHERE c // For bool typed indexed column c
  2569. // - WHERE !c // For bool typed indexed column c
  2570. // - WHERE c relOp constExpr // For indexed column c
  2571. // - WHERE c relOp parameter // For indexed column c
  2572. // - WHERE parameter relOp c // For indexed column c
  2573. // - WHERE constExpr relOp c // For indexed column c
  2574. //
  2575. // The relOp is one of the relation operators <, <=, ==, >=, >. For the
  2576. // equality operator both operands must be of comparable types. For all other
  2577. // operators both operands must be of ordered types. The constant expression is
  2578. // a compile time constant expression. Some constant folding is still a TODO.
  2579. // Parameter is a QL parameter ($1 etc.).
  2580. //
  2581. // Query rewriting
  2582. //
  2583. // Consider tables t and u, both with an indexed field f. The WHERE expression
  2584. // doesn't comply with the above simple detected cases.
  2585. //
  2586. // SELECT * FROM t, u WHERE t.f < x && u.f < y;
  2587. //
  2588. // However, such query is now automatically rewritten to
  2589. //
  2590. // SELECT * FROM
  2591. // (SELECT * FROM t WHERE f < x),
  2592. // (SELECT * FROM u WHERE f < y);
  2593. //
  2594. // which will use both of the indices. The impact of using the indices can be
  2595. // substantial (cf. BenchmarkCrossJoin*) if the resulting rows have low
  2596. // "selectivity", ie. only few rows from both tables are selected by the
  2597. // respective WHERE filtering.
  2598. //
  2599. // Note: Existing QL DBs can be used and indices can be added to them. However,
  2600. // once any indices are present in the DB, the old QL versions cannot work with
  2601. // such DB anymore.
  2602. //
  2603. // Benchmarks
  2604. //
  2605. // Running a benchmark with -v (-test.v) outputs information about the scale
  2606. // used to report records/s and a brief description of the benchmark. For
  2607. // example
  2608. //
  2609. // $ go test -run NONE -bench 'SelectMem.*1e[23]' -v
  2610. // PASS
  2611. // BenchmarkSelectMem1kBx1e2 50000 67680 ns/op 1477537.05 MB/s
  2612. // --- BENCH: BenchmarkSelectMem1kBx1e2
  2613. // all_test.go:310:
  2614. // =============================================================
  2615. // NOTE: All benchmarks report records/s as 1000000 bytes/s.
  2616. // =============================================================
  2617. // all_test.go:321: Having a table of 100 records, each of size 1kB, measure the performance of
  2618. // SELECT * FROM t;
  2619. //
  2620. // BenchmarkSelectMem1kBx1e3 5000 634819 ns/op 1575251.01 MB/s
  2621. // --- BENCH: BenchmarkSelectMem1kBx1e3
  2622. // all_test.go:321: Having a table of 1000 records, each of size 1kB, measure the performance of
  2623. // SELECT * FROM t;
  2624. //
  2625. // ok github.com/cznic/ql 7.496s
  2626. // $
  2627. //
  2628. // Running the full suite of benchmarks takes a lot of time. Use the -timeout
  2629. // flag to avoid them being killed after the default time limit (10 minutes).
  2630. package ql