etc.go 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772
  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. package ql
  5. import (
  6. "bytes"
  7. "fmt"
  8. "io"
  9. "math"
  10. "math/big"
  11. "time"
  12. )
  13. // QL types.
  14. const (
  15. qBool = 0x62 // 'b'
  16. qComplex64 = 0x63 // 'c'
  17. qComplex128 = 0x64 // 'd'
  18. qFloat32 = 0x66 // 'f'
  19. qFloat64 = 0x67 // 'g', alias float
  20. qInt8 = 0x69 // 'i'
  21. qInt16 = 0x6a // 'j'
  22. qInt32 = 0x6b // 'k'
  23. qInt64 = 0x6c // 'l', alias int
  24. qString = 0x73 // 's'
  25. qUint8 = 0x75 // 'u', alias byte
  26. qUint16 = 0x76 // 'v'
  27. qUint32 = 0x77 // 'w'
  28. qUint64 = 0x78 // 'x', alias uint
  29. qBigInt = 0x49 // 'I'
  30. qBigRat = 0x52 // 'R'
  31. qBlob = 0x42 // 'B'
  32. qDuration = 0x44 // 'D'
  33. qTime = 0x54 // 'T'
  34. )
  35. var (
  36. type2Str = map[int]string{
  37. qBigInt: "bigint",
  38. qBigRat: "bigrat",
  39. qBlob: "blob",
  40. qBool: "bool",
  41. qComplex128: "complex128",
  42. qComplex64: "complex64",
  43. qDuration: "duration",
  44. qFloat32: "float32",
  45. qFloat64: "float64",
  46. qInt16: "int16",
  47. qInt32: "int32",
  48. qInt64: "int64",
  49. qInt8: "int8",
  50. qString: "string",
  51. qTime: "time",
  52. qUint16: "uint16",
  53. qUint32: "uint32",
  54. qUint64: "uint64",
  55. qUint8: "uint8",
  56. }
  57. )
  58. func typeStr(typ int) (r string) {
  59. return type2Str[typ]
  60. }
  61. func noEOF(err error) error {
  62. if err == io.EOF {
  63. err = nil
  64. }
  65. return err
  66. }
  67. func runErr(err error) error { return fmt.Errorf("run time error: %s", err) }
  68. func invXOp(s, x interface{}) error {
  69. return fmt.Errorf("invalid operation: %v[%v] (index of type %T)", s, x, x)
  70. }
  71. func invSOp(s interface{}) error {
  72. return fmt.Errorf("cannot slice %s (type %T)", s, s)
  73. }
  74. func invNegX(x interface{}) error {
  75. return fmt.Errorf("invalid string index %v (index must be non-negative)", x)
  76. }
  77. func invNegLO(x interface{}) error {
  78. return fmt.Errorf("invalid LIMIT or OFFSET value %v (must be non-negative)", x)
  79. }
  80. func invSliceNegX(x interface{}) error {
  81. return fmt.Errorf("invalid slice index %v (index must be non-negative)", x)
  82. }
  83. func invBoundX(s string, x uint64) error {
  84. return fmt.Errorf("invalid string index %d (out of bounds for %d-byte string)", x, len(s))
  85. }
  86. func invSliceBoundX(s string, x uint64) error {
  87. return fmt.Errorf("invalid slice index %d (out of bounds for %d-byte string)", x, len(s))
  88. }
  89. func intExpr(x interface{}) (i int64, err error) {
  90. switch x := x.(type) {
  91. case idealInt:
  92. if x < 0 {
  93. return 0, invNegLO(x)
  94. }
  95. return int64(x), nil
  96. case idealRune:
  97. if x < 0 {
  98. return 0, invNegLO(x)
  99. }
  100. return int64(x), nil
  101. case idealUint:
  102. if x < 0 {
  103. return 0, invNegLO(x)
  104. }
  105. return int64(x), nil
  106. case int8:
  107. if x < 0 {
  108. return 0, invNegLO(x)
  109. }
  110. return int64(x), nil
  111. case int16:
  112. if x < 0 {
  113. return 0, invNegLO(x)
  114. }
  115. return int64(x), nil
  116. case int32:
  117. if x < 0 {
  118. return 0, invNegLO(x)
  119. }
  120. return int64(x), nil
  121. case int64:
  122. if x < 0 {
  123. return 0, invNegLO(x)
  124. }
  125. return int64(x), nil
  126. case uint8:
  127. return int64(x), nil
  128. case uint16:
  129. return int64(x), nil
  130. case uint32:
  131. return int64(x), nil
  132. case uint64:
  133. return int64(x), nil
  134. default:
  135. return 0, fmt.Errorf("non-integer expression: %v (value of type %T)", x, x)
  136. }
  137. }
  138. func limOffExpr(x interface{}) (i uint64, err error) {
  139. switch x := x.(type) {
  140. case idealInt:
  141. if x < 0 {
  142. return 0, invNegLO(x)
  143. }
  144. return uint64(x), nil
  145. case idealRune:
  146. if x < 0 {
  147. return 0, invNegLO(x)
  148. }
  149. return uint64(x), nil
  150. case idealUint:
  151. if x < 0 {
  152. return 0, invNegLO(x)
  153. }
  154. return uint64(x), nil
  155. case int8:
  156. if x < 0 {
  157. return 0, invNegLO(x)
  158. }
  159. return uint64(x), nil
  160. case int16:
  161. if x < 0 {
  162. return 0, invNegLO(x)
  163. }
  164. return uint64(x), nil
  165. case int32:
  166. if x < 0 {
  167. return 0, invNegLO(x)
  168. }
  169. return uint64(x), nil
  170. case int64:
  171. if x < 0 {
  172. return 0, invNegLO(x)
  173. }
  174. return uint64(x), nil
  175. case uint8:
  176. return uint64(x), nil
  177. case uint16:
  178. return uint64(x), nil
  179. case uint32:
  180. return uint64(x), nil
  181. case uint64:
  182. return uint64(x), nil
  183. default:
  184. return 0, fmt.Errorf("non-integer used in LIMIT or OFFSET: %v (value of type %T)", x, x)
  185. }
  186. }
  187. func indexExpr(s *string, x interface{}) (i uint64, err error) {
  188. switch x := x.(type) {
  189. case idealFloat:
  190. if x < 0 {
  191. return 0, invNegX(x)
  192. }
  193. if s != nil && int(x) >= len(*s) {
  194. return 0, invBoundX(*s, uint64(x))
  195. }
  196. return uint64(x), nil
  197. case idealInt:
  198. if x < 0 {
  199. return 0, invNegX(x)
  200. }
  201. if s != nil && int64(x) >= int64(len(*s)) {
  202. return 0, invBoundX(*s, uint64(x))
  203. }
  204. return uint64(x), nil
  205. case idealRune:
  206. if x < 0 {
  207. return 0, invNegX(x)
  208. }
  209. if s != nil && int32(x) >= int32(len(*s)) {
  210. return 0, invBoundX(*s, uint64(x))
  211. }
  212. return uint64(x), nil
  213. case idealUint:
  214. if x < 0 {
  215. return 0, invNegX(x)
  216. }
  217. if s != nil && uint64(x) >= uint64(len(*s)) {
  218. return 0, invBoundX(*s, uint64(x))
  219. }
  220. return uint64(x), nil
  221. case int8:
  222. if x < 0 {
  223. return 0, invNegX(x)
  224. }
  225. if s != nil && int(x) >= len(*s) {
  226. return 0, invBoundX(*s, uint64(x))
  227. }
  228. return uint64(x), nil
  229. case int16:
  230. if x < 0 {
  231. return 0, invNegX(x)
  232. }
  233. if s != nil && int(x) >= len(*s) {
  234. return 0, invBoundX(*s, uint64(x))
  235. }
  236. return uint64(x), nil
  237. case int32:
  238. if x < 0 {
  239. return 0, invNegX(x)
  240. }
  241. if s != nil && int(x) >= len(*s) {
  242. return 0, invBoundX(*s, uint64(x))
  243. }
  244. return uint64(x), nil
  245. case int64:
  246. if x < 0 {
  247. return 0, invNegX(x)
  248. }
  249. if s != nil && x >= int64(len(*s)) {
  250. return 0, invBoundX(*s, uint64(x))
  251. }
  252. return uint64(x), nil
  253. case uint8:
  254. if s != nil && int(x) >= len(*s) {
  255. return 0, invBoundX(*s, uint64(x))
  256. }
  257. return uint64(x), nil
  258. case uint16:
  259. if s != nil && int(x) >= len(*s) {
  260. return 0, invBoundX(*s, uint64(x))
  261. }
  262. return uint64(x), nil
  263. case uint32:
  264. if s != nil && x >= uint32(len(*s)) {
  265. return 0, invBoundX(*s, uint64(x))
  266. }
  267. return uint64(x), nil
  268. case uint64:
  269. if s != nil && x >= uint64(len(*s)) {
  270. return 0, invBoundX(*s, uint64(x))
  271. }
  272. return uint64(x), nil
  273. default:
  274. return 0, fmt.Errorf("non-integer string index %v (value of type %T)", x, x)
  275. }
  276. }
  277. func sliceExpr(s *string, x interface{}, mod int) (i uint64, err error) {
  278. switch x := x.(type) {
  279. case idealFloat:
  280. if x < 0 {
  281. return 0, invSliceNegX(x)
  282. }
  283. if s != nil && int(x) >= len(*s)+mod {
  284. return 0, invSliceBoundX(*s, uint64(x))
  285. }
  286. return uint64(x), nil
  287. case idealInt:
  288. if x < 0 {
  289. return 0, invSliceNegX(x)
  290. }
  291. if s != nil && int64(x) >= int64(len(*s)+mod) {
  292. return 0, invSliceBoundX(*s, uint64(x))
  293. }
  294. return uint64(x), nil
  295. case idealRune:
  296. if x < 0 {
  297. return 0, invSliceNegX(x)
  298. }
  299. if s != nil && int32(x) >= int32(len(*s)+mod) {
  300. return 0, invSliceBoundX(*s, uint64(x))
  301. }
  302. return uint64(x), nil
  303. case idealUint:
  304. if x < 0 {
  305. return 0, invSliceNegX(x)
  306. }
  307. if s != nil && uint64(x) >= uint64(len(*s)+mod) {
  308. return 0, invSliceBoundX(*s, uint64(x))
  309. }
  310. return uint64(x), nil
  311. case int8:
  312. if x < 0 {
  313. return 0, invSliceNegX(x)
  314. }
  315. if s != nil && int(x) >= len(*s)+mod {
  316. return 0, invSliceBoundX(*s, uint64(x))
  317. }
  318. return uint64(x), nil
  319. case int16:
  320. if x < 0 {
  321. return 0, invSliceNegX(x)
  322. }
  323. if s != nil && int(x) >= len(*s)+mod {
  324. return 0, invSliceBoundX(*s, uint64(x))
  325. }
  326. return uint64(x), nil
  327. case int32:
  328. if x < 0 {
  329. return 0, invSliceNegX(x)
  330. }
  331. if s != nil && int(x) >= len(*s)+mod {
  332. return 0, invSliceBoundX(*s, uint64(x))
  333. }
  334. return uint64(x), nil
  335. case int64:
  336. if x < 0 {
  337. return 0, invSliceNegX(x)
  338. }
  339. if s != nil && x >= int64(len(*s)+mod) {
  340. return 0, invSliceBoundX(*s, uint64(x))
  341. }
  342. return uint64(x), nil
  343. case uint8:
  344. if s != nil && int(x) >= len(*s)+mod {
  345. return 0, invSliceBoundX(*s, uint64(x))
  346. }
  347. return uint64(x), nil
  348. case uint16:
  349. if s != nil && int(x) >= len(*s)+mod {
  350. return 0, invSliceBoundX(*s, uint64(x))
  351. }
  352. return uint64(x), nil
  353. case uint32:
  354. if s != nil && x >= uint32(len(*s)+mod) {
  355. return 0, invSliceBoundX(*s, uint64(x))
  356. }
  357. return uint64(x), nil
  358. case uint64:
  359. if s != nil && x >= uint64(len(*s)+mod) {
  360. return 0, invSliceBoundX(*s, uint64(x))
  361. }
  362. return uint64(x), nil
  363. default:
  364. return 0, fmt.Errorf("invalid slice index %s (type %T)", x, x)
  365. }
  366. }
  367. type iop int
  368. func (o iop) String() string {
  369. switch i := int(o); i {
  370. case andand:
  371. return "&&"
  372. case andnot:
  373. return "&^"
  374. case lsh:
  375. return "<<"
  376. case le:
  377. return "<="
  378. case eq:
  379. return "=="
  380. case ge:
  381. return ">="
  382. case neq:
  383. return "!="
  384. case oror:
  385. return "||"
  386. case rsh:
  387. return ">>"
  388. default:
  389. return string(i)
  390. }
  391. }
  392. func ideal(v interface{}) interface{} {
  393. switch x := v.(type) {
  394. case idealComplex:
  395. return complex128(x)
  396. case idealFloat:
  397. return float64(x)
  398. case idealInt:
  399. return int64(x)
  400. case idealRune:
  401. return int64(x)
  402. case idealUint:
  403. return uint64(x)
  404. default:
  405. return v
  406. }
  407. }
  408. func eval(v expression, execCtx *execCtx, ctx map[interface{}]interface{}) (y interface{}) {
  409. y, err := expand1(v.eval(execCtx, ctx))
  410. if err != nil {
  411. panic(err) // panic ok here
  412. }
  413. return
  414. }
  415. func eval2(a, b expression, execCtx *execCtx, ctx map[interface{}]interface{}) (x, y interface{}) {
  416. return eval(a, execCtx, ctx), eval(b, execCtx, ctx)
  417. }
  418. func invOp2(x, y interface{}, o int) (interface{}, error) {
  419. return nil, fmt.Errorf("invalid operation: %v %v %v (mismatched types %T and %T)", x, iop(o), y, ideal(x), ideal(y))
  420. }
  421. func undOp(x interface{}, o int) (interface{}, error) {
  422. return nil, fmt.Errorf("invalid operation: %v%v (operator %v not defined on %T)", iop(o), x, iop(o), x)
  423. }
  424. func undOp2(x, y interface{}, o int) (interface{}, error) {
  425. return nil, fmt.Errorf("invalid operation: %v %v %v (operator %v not defined on %T)", x, iop(o), y, iop(o), x)
  426. }
  427. func invConv(val interface{}, typ int) (interface{}, error) {
  428. return nil, fmt.Errorf("cannot convert %v (type %T) to type %s", val, val, typeStr(typ))
  429. }
  430. func truncConv(val interface{}) (interface{}, error) {
  431. return nil, fmt.Errorf("constant %v truncated to integer", val)
  432. }
  433. func convert(val interface{}, typ int) (v interface{}, err error) { //NTYPE
  434. if val == nil {
  435. return nil, nil
  436. }
  437. switch typ {
  438. case qBool:
  439. switch x := val.(type) {
  440. //case nil:
  441. //case idealComplex:
  442. //case idealFloat:
  443. //case idealInt:
  444. //case idealRune:
  445. //case idealUint:
  446. case bool:
  447. return bool(x), nil
  448. //case complex64:
  449. //case complex128:
  450. //case float32:
  451. //case float64:
  452. //case int8:
  453. //case int16:
  454. //case int32:
  455. //case int64:
  456. //case string:
  457. //case uint8:
  458. //case uint16:
  459. //case uint32:
  460. //case uint64:
  461. default:
  462. return invConv(val, typ)
  463. }
  464. case qComplex64:
  465. switch x := val.(type) {
  466. //case nil:
  467. case idealComplex:
  468. return complex64(x), nil
  469. case idealFloat:
  470. return complex(float32(x), 0), nil
  471. case idealInt:
  472. return complex(float32(x), 0), nil
  473. case idealRune:
  474. return complex(float32(x), 0), nil
  475. case idealUint:
  476. return complex(float32(x), 0), nil
  477. //case bool:
  478. case complex64:
  479. return complex64(x), nil
  480. case complex128:
  481. return complex64(x), nil
  482. //case float32:
  483. //case float64:
  484. //case int8:
  485. //case int16:
  486. //case int32:
  487. //case int64:
  488. //case string:
  489. //case uint8:
  490. //case uint16:
  491. //case uint32:
  492. //case uint64:
  493. default:
  494. return invConv(val, typ)
  495. }
  496. case qComplex128:
  497. switch x := val.(type) {
  498. //case nil:
  499. case idealComplex:
  500. return complex128(x), nil
  501. case idealFloat:
  502. return complex(float64(x), 0), nil
  503. case idealInt:
  504. return complex(float64(x), 0), nil
  505. case idealRune:
  506. return complex(float64(x), 0), nil
  507. case idealUint:
  508. return complex(float64(x), 0), nil
  509. //case bool:
  510. case complex64:
  511. return complex128(x), nil
  512. case complex128:
  513. return complex128(x), nil
  514. //case float32:
  515. //case float64:
  516. //case int8:
  517. //case int16:
  518. //case int32:
  519. //case int64:
  520. //case string:
  521. //case uint8:
  522. //case uint16:
  523. //case uint32:
  524. //case uint64:
  525. default:
  526. return invConv(val, typ)
  527. }
  528. case qFloat32:
  529. switch x := val.(type) {
  530. //case nil:
  531. //case idealComplex:
  532. case idealFloat:
  533. return float32(x), nil
  534. case idealInt:
  535. return float32(x), nil
  536. case idealRune:
  537. return float32(x), nil
  538. case idealUint:
  539. return float32(x), nil
  540. //case bool:
  541. //case complex64:
  542. //case complex128:
  543. case float32:
  544. return float32(x), nil
  545. case float64:
  546. return float32(x), nil
  547. case int8:
  548. return float32(x), nil
  549. case int16:
  550. return float32(x), nil
  551. case int32:
  552. return float32(x), nil
  553. case int64:
  554. return float32(x), nil
  555. //case string:
  556. case uint8:
  557. return float32(x), nil
  558. case uint16:
  559. return float32(x), nil
  560. case uint32:
  561. return float32(x), nil
  562. case uint64:
  563. return float32(x), nil
  564. case *big.Int:
  565. v, _ := big.NewRat(1, 1).SetInt(x).Float64()
  566. return float32(v), nil
  567. case *big.Rat:
  568. v, _ := x.Float64()
  569. return float32(v), nil
  570. case time.Duration:
  571. return float32(x), nil
  572. default:
  573. return invConv(val, typ)
  574. }
  575. case qFloat64:
  576. switch x := val.(type) {
  577. //case nil:
  578. //case idealComplex:
  579. case idealFloat:
  580. return float64(x), nil
  581. case idealInt:
  582. return float64(x), nil
  583. case idealRune:
  584. return float64(x), nil
  585. case idealUint:
  586. return float64(x), nil
  587. //case bool:
  588. //case complex64:
  589. //case complex128:
  590. case float32:
  591. return float64(x), nil
  592. case float64:
  593. return float64(x), nil
  594. case int8:
  595. return float64(x), nil
  596. case int16:
  597. return float64(x), nil
  598. case int32:
  599. return float64(x), nil
  600. case int64:
  601. return float64(x), nil
  602. //case string:
  603. case uint8:
  604. return float64(x), nil
  605. case uint16:
  606. return float64(x), nil
  607. case uint32:
  608. return float64(x), nil
  609. case uint64:
  610. return float64(x), nil
  611. case *big.Int:
  612. v, _ := big.NewRat(1, 1).SetInt(x).Float64()
  613. return v, nil
  614. case *big.Rat:
  615. v, _ := x.Float64()
  616. return v, nil
  617. case time.Duration:
  618. return float64(x), nil
  619. default:
  620. return invConv(val, typ)
  621. }
  622. case qInt8:
  623. switch x := val.(type) {
  624. //case nil:
  625. //case idealComplex:
  626. case idealFloat:
  627. if _, frac := math.Modf(float64(x)); frac != 0 {
  628. return truncConv(x)
  629. }
  630. return int8(x), nil
  631. case idealInt:
  632. return int8(x), nil
  633. case idealRune:
  634. return int8(x), nil
  635. case idealUint:
  636. return int8(x), nil
  637. //case bool:
  638. //case complex64:
  639. //case complex128:
  640. case float32:
  641. return int8(x), nil
  642. case float64:
  643. return int8(x), nil
  644. case int8:
  645. return int8(x), nil
  646. case int16:
  647. return int8(x), nil
  648. case int32:
  649. return int8(x), nil
  650. case int64:
  651. return int8(x), nil
  652. //case string:
  653. case uint8:
  654. return int8(x), nil
  655. case uint16:
  656. return int8(x), nil
  657. case uint32:
  658. return int8(x), nil
  659. case uint64:
  660. return int8(x), nil
  661. case *big.Int:
  662. return int8(x.Int64()), nil
  663. case time.Duration:
  664. return int8(x), nil
  665. default:
  666. return invConv(val, typ)
  667. }
  668. case qInt16:
  669. switch x := val.(type) {
  670. //case nil:
  671. //case idealComplex:
  672. case idealFloat:
  673. if _, frac := math.Modf(float64(x)); frac != 0 {
  674. return truncConv(x)
  675. }
  676. return int16(x), nil
  677. case idealInt:
  678. return int16(x), nil
  679. case idealRune:
  680. return int16(x), nil
  681. case idealUint:
  682. return int16(x), nil
  683. //case bool:
  684. //case complex64:
  685. //case complex128:
  686. case float32:
  687. return int16(x), nil
  688. case float64:
  689. return int16(x), nil
  690. case int8:
  691. return int16(x), nil
  692. case int16:
  693. return int16(x), nil
  694. case int32:
  695. return int16(x), nil
  696. case int64:
  697. return int16(x), nil
  698. //case string:
  699. case uint8:
  700. return int16(x), nil
  701. case uint16:
  702. return int16(x), nil
  703. case uint32:
  704. return int16(x), nil
  705. case uint64:
  706. return int16(x), nil
  707. case *big.Int:
  708. return int16(x.Int64()), nil
  709. case time.Duration:
  710. return int16(x), nil
  711. default:
  712. return invConv(val, typ)
  713. }
  714. case qInt32:
  715. switch x := val.(type) {
  716. //case nil:
  717. //case idealComplex:
  718. case idealFloat:
  719. if _, frac := math.Modf(float64(x)); frac != 0 {
  720. return truncConv(x)
  721. }
  722. return int32(x), nil
  723. case idealInt:
  724. return int32(x), nil
  725. case idealRune:
  726. return int32(x), nil
  727. case idealUint:
  728. return int32(x), nil
  729. //case bool:
  730. //case complex64:
  731. //case complex128:
  732. case float32:
  733. return int32(x), nil
  734. case float64:
  735. return int32(x), nil
  736. case int8:
  737. return int32(x), nil
  738. case int16:
  739. return int32(x), nil
  740. case int32:
  741. return int32(x), nil
  742. case int64:
  743. return int32(x), nil
  744. //case string:
  745. case uint8:
  746. return int32(x), nil
  747. case uint16:
  748. return int32(x), nil
  749. case uint32:
  750. return int32(x), nil
  751. case uint64:
  752. return int32(x), nil
  753. case *big.Int:
  754. return int32(x.Int64()), nil
  755. case time.Duration:
  756. return int32(x), nil
  757. default:
  758. return invConv(val, typ)
  759. }
  760. case qInt64:
  761. switch x := val.(type) {
  762. //case nil:
  763. //case idealComplex:
  764. case idealFloat:
  765. if _, frac := math.Modf(float64(x)); frac != 0 {
  766. return truncConv(x)
  767. }
  768. return int64(x), nil
  769. case idealInt:
  770. return int64(x), nil
  771. case idealRune:
  772. return int64(x), nil
  773. case idealUint:
  774. return int64(x), nil
  775. //case bool:
  776. //case complex64:
  777. //case complex128:
  778. case float32:
  779. return int64(x), nil
  780. case float64:
  781. return int64(x), nil
  782. case int8:
  783. return int64(x), nil
  784. case int16:
  785. return int64(x), nil
  786. case int32:
  787. return int64(x), nil
  788. case int64:
  789. return int64(x), nil
  790. //case string:
  791. case uint8:
  792. return int64(x), nil
  793. case uint16:
  794. return int64(x), nil
  795. case uint32:
  796. return int64(x), nil
  797. case uint64:
  798. return int64(x), nil
  799. case *big.Int:
  800. return x.Int64(), nil
  801. case time.Duration:
  802. return int64(x), nil
  803. default:
  804. return invConv(val, typ)
  805. }
  806. case qString:
  807. switch x := val.(type) {
  808. //case nil:
  809. //case idealComplex:
  810. //case idealFloat:
  811. case idealInt:
  812. return string(x), nil
  813. case idealRune:
  814. return string(x), nil
  815. case idealUint:
  816. return string(x), nil
  817. //case bool:
  818. //case complex64:
  819. //case complex128:
  820. //case float32:
  821. //case float64:
  822. case int8:
  823. return string(x), nil
  824. case int16:
  825. return string(x), nil
  826. case int32:
  827. return string(x), nil
  828. case int64:
  829. return string(x), nil
  830. case string:
  831. return string(x), nil
  832. case uint8:
  833. return string(x), nil
  834. case uint16:
  835. return string(x), nil
  836. case uint32:
  837. return string(x), nil
  838. case uint64:
  839. return string(x), nil
  840. case []byte:
  841. return string(x), nil
  842. case *big.Int:
  843. return x.String(), nil
  844. case time.Time:
  845. return x.String(), nil
  846. case time.Duration:
  847. return x.String(), nil
  848. default:
  849. return invConv(val, typ)
  850. }
  851. case qUint8:
  852. switch x := val.(type) {
  853. //case nil:
  854. //case idealComplex:
  855. case idealFloat:
  856. if _, frac := math.Modf(float64(x)); frac != 0 {
  857. return truncConv(x)
  858. }
  859. return uint8(x), nil
  860. case idealInt:
  861. return uint8(x), nil
  862. case idealRune:
  863. return uint8(x), nil
  864. case idealUint:
  865. return uint8(x), nil
  866. //case bool:
  867. //case complex64:
  868. //case complex128:
  869. case float32:
  870. return uint8(x), nil
  871. case float64:
  872. return uint8(x), nil
  873. case int8:
  874. return uint8(x), nil
  875. case int16:
  876. return uint8(x), nil
  877. case int32:
  878. return uint8(x), nil
  879. case int64:
  880. return uint8(x), nil
  881. //case string:
  882. case uint8:
  883. return uint8(x), nil
  884. case uint16:
  885. return uint8(x), nil
  886. case uint32:
  887. return uint8(x), nil
  888. case uint64:
  889. return uint8(x), nil
  890. case *big.Int:
  891. return uint8(x.Int64()), nil
  892. case time.Duration:
  893. return uint8(x), nil
  894. default:
  895. return invConv(val, typ)
  896. }
  897. case qUint16:
  898. switch x := val.(type) {
  899. //case nil:
  900. //case idealComplex:
  901. case idealFloat:
  902. if _, frac := math.Modf(float64(x)); frac != 0 {
  903. return truncConv(x)
  904. }
  905. return uint16(x), nil
  906. case idealInt:
  907. return uint16(x), nil
  908. case idealRune:
  909. return uint16(x), nil
  910. case idealUint:
  911. return uint16(x), nil
  912. //case bool:
  913. //case complex64:
  914. //case complex128:
  915. case float32:
  916. return uint16(x), nil
  917. case float64:
  918. return uint16(x), nil
  919. case int8:
  920. return uint16(x), nil
  921. case int16:
  922. return uint16(x), nil
  923. case int32:
  924. return uint16(x), nil
  925. case int64:
  926. return uint16(x), nil
  927. //case string:
  928. case uint8:
  929. return uint16(x), nil
  930. case uint16:
  931. return uint16(x), nil
  932. case uint32:
  933. return uint16(x), nil
  934. case uint64:
  935. return uint16(x), nil
  936. case *big.Int:
  937. return uint16(x.Int64()), nil
  938. case time.Duration:
  939. return uint16(x), nil
  940. default:
  941. return invConv(val, typ)
  942. }
  943. case qUint32:
  944. switch x := val.(type) {
  945. //case nil:
  946. //case idealComplex:
  947. case idealFloat:
  948. if _, frac := math.Modf(float64(x)); frac != 0 {
  949. return truncConv(x)
  950. }
  951. return uint32(x), nil
  952. case idealInt:
  953. return uint32(x), nil
  954. case idealRune:
  955. return uint32(x), nil
  956. case idealUint:
  957. return uint32(x), nil
  958. //case bool:
  959. //case complex64:
  960. //case complex128:
  961. case float32:
  962. return uint32(x), nil
  963. case float64:
  964. return uint32(x), nil
  965. case int8:
  966. return uint32(x), nil
  967. case int16:
  968. return uint32(x), nil
  969. case int32:
  970. return uint32(x), nil
  971. case int64:
  972. return uint32(x), nil
  973. //case string:
  974. case uint8:
  975. return uint32(x), nil
  976. case uint16:
  977. return uint32(x), nil
  978. case uint32:
  979. return uint32(x), nil
  980. case uint64:
  981. return uint32(x), nil
  982. case *big.Int:
  983. return uint32(x.Int64()), nil
  984. case time.Duration:
  985. return uint32(x), nil
  986. default:
  987. return invConv(val, typ)
  988. }
  989. case qUint64:
  990. switch x := val.(type) {
  991. //case nil:
  992. //case idealComplex:
  993. case idealFloat:
  994. if _, frac := math.Modf(float64(x)); frac != 0 {
  995. return truncConv(x)
  996. }
  997. return uint64(x), nil
  998. case idealInt:
  999. return uint64(x), nil
  1000. case idealRune:
  1001. return uint64(x), nil
  1002. case idealUint:
  1003. return uint64(x), nil
  1004. //case bool:
  1005. //case complex64:
  1006. //case complex128:
  1007. case float32:
  1008. return uint64(x), nil
  1009. case float64:
  1010. return uint64(x), nil
  1011. case int8:
  1012. return uint64(x), nil
  1013. case int16:
  1014. return uint64(x), nil
  1015. case int32:
  1016. return uint64(x), nil
  1017. case int64:
  1018. return uint64(x), nil
  1019. //case string:
  1020. case uint8:
  1021. return uint64(x), nil
  1022. case uint16:
  1023. return uint64(x), nil
  1024. case uint32:
  1025. return uint64(x), nil
  1026. case uint64:
  1027. return uint64(x), nil
  1028. case *big.Int:
  1029. return x.Uint64(), nil
  1030. case time.Duration:
  1031. return uint64(x), nil
  1032. default:
  1033. return invConv(val, typ)
  1034. }
  1035. case qBlob:
  1036. switch x := val.(type) {
  1037. case string:
  1038. return []byte(x), nil
  1039. case []byte:
  1040. return x, nil
  1041. default:
  1042. return invConv(val, typ)
  1043. }
  1044. case qBigInt:
  1045. switch x := val.(type) {
  1046. // case blob
  1047. // case bool
  1048. //case idealComplex:
  1049. case idealFloat:
  1050. if _, frac := math.Modf(float64(x)); frac != 0 {
  1051. return truncConv(x)
  1052. }
  1053. rr := big.NewRat(1, 1).SetFloat64(float64(x))
  1054. ii := big.NewInt(0).Set(rr.Num())
  1055. ii.Quo(ii, rr.Denom())
  1056. return ii, nil
  1057. case idealInt:
  1058. return big.NewInt(0).SetInt64(int64(x)), nil
  1059. case idealRune:
  1060. return big.NewInt(0).SetInt64(int64(x)), nil
  1061. case idealUint:
  1062. return big.NewInt(0).SetUint64(uint64(x)), nil
  1063. //case complex64
  1064. //case complex128
  1065. case float32:
  1066. rr := big.NewRat(1, 1).SetFloat64(float64(x))
  1067. ii := big.NewInt(0).Set(rr.Num())
  1068. ii.Quo(ii, rr.Denom())
  1069. return ii, nil
  1070. case float64:
  1071. rr := big.NewRat(1, 1).SetFloat64(float64(x))
  1072. ii := big.NewInt(0).Set(rr.Num())
  1073. ii.Quo(ii, rr.Denom())
  1074. return ii, nil
  1075. case int8:
  1076. return big.NewInt(0).SetInt64(int64(x)), nil
  1077. case int16:
  1078. return big.NewInt(0).SetInt64(int64(x)), nil
  1079. case int32:
  1080. return big.NewInt(0).SetInt64(int64(x)), nil
  1081. case int64:
  1082. return big.NewInt(0).SetInt64(x), nil
  1083. case string:
  1084. y := big.NewInt(0)
  1085. if _, ok := y.SetString(x, 0); !ok {
  1086. return invConv(val, typ)
  1087. }
  1088. return y, nil
  1089. case uint8:
  1090. return big.NewInt(0).SetUint64(uint64(x)), nil
  1091. case uint16:
  1092. return big.NewInt(0).SetUint64(uint64(x)), nil
  1093. case uint32:
  1094. return big.NewInt(0).SetUint64(uint64(x)), nil
  1095. case uint64:
  1096. return big.NewInt(0).SetUint64(x), nil
  1097. case *big.Int:
  1098. return x, nil
  1099. case *big.Rat:
  1100. ii := big.NewInt(0).Set(x.Num())
  1101. ii.Div(ii, x.Denom())
  1102. return ii, nil
  1103. default:
  1104. return invConv(val, typ)
  1105. }
  1106. case qBigRat:
  1107. switch x := val.(type) {
  1108. // case blob
  1109. // case bool
  1110. //case idealComplex:
  1111. case idealFloat:
  1112. return big.NewRat(1, 1).SetFloat64(float64(x)), nil
  1113. case idealInt:
  1114. return big.NewRat(1, 1).SetInt64(int64(x)), nil
  1115. case idealRune:
  1116. return big.NewRat(1, 1).SetInt64(int64(x)), nil
  1117. case idealUint:
  1118. return big.NewRat(1, 1).SetInt(big.NewInt(0).SetUint64(uint64(x))), nil
  1119. //case complex64
  1120. //case complex128
  1121. case float32:
  1122. return big.NewRat(1, 1).SetFloat64(float64(x)), nil
  1123. case float64:
  1124. return big.NewRat(1, 1).SetFloat64(x), nil
  1125. case int8:
  1126. return big.NewRat(1, 1).SetInt64(int64(x)), nil
  1127. case int16:
  1128. return big.NewRat(1, 1).SetInt64(int64(x)), nil
  1129. case int32:
  1130. return big.NewRat(1, 1).SetInt64(int64(x)), nil
  1131. case int64:
  1132. return big.NewRat(1, 1).SetInt64(x), nil
  1133. case string:
  1134. y := big.NewRat(1, 1)
  1135. if _, ok := y.SetString(x); !ok {
  1136. return invConv(val, typ)
  1137. }
  1138. return y, nil
  1139. case uint8:
  1140. return big.NewRat(1, 1).SetInt64(int64(x)), nil
  1141. case uint16:
  1142. return big.NewRat(1, 1).SetInt64(int64(x)), nil
  1143. case uint32:
  1144. return big.NewRat(1, 1).SetInt64(int64(x)), nil
  1145. case uint64:
  1146. return big.NewRat(1, 1).SetInt(big.NewInt(0).SetUint64(x)), nil
  1147. case *big.Int:
  1148. return big.NewRat(1, 1).SetInt(x), nil
  1149. case *big.Rat:
  1150. return x, nil
  1151. default:
  1152. return invConv(val, typ)
  1153. }
  1154. case qDuration:
  1155. switch x := val.(type) {
  1156. // case blob
  1157. // case bool
  1158. //case idealComplex:
  1159. case idealFloat:
  1160. return time.Duration(x), nil
  1161. case idealInt:
  1162. return time.Duration(x), nil
  1163. case idealRune:
  1164. return time.Duration(x), nil
  1165. case idealUint:
  1166. return time.Duration(x), nil
  1167. //case complex64
  1168. //case complex128
  1169. case float32:
  1170. return time.Duration(x), nil
  1171. case float64:
  1172. return time.Duration(x), nil
  1173. case int8:
  1174. return time.Duration(x), nil
  1175. case int16:
  1176. return time.Duration(x), nil
  1177. case int32:
  1178. return time.Duration(x), nil
  1179. case int64:
  1180. return time.Duration(x), nil
  1181. case string:
  1182. return time.ParseDuration(x)
  1183. case uint8:
  1184. return time.Duration(x), nil
  1185. case uint16:
  1186. return time.Duration(x), nil
  1187. case uint32:
  1188. return time.Duration(x), nil
  1189. case uint64:
  1190. return time.Duration(x), nil
  1191. case *big.Int:
  1192. return time.Duration(x.Int64()), nil
  1193. case *big.Rat:
  1194. f, _ := x.Float64()
  1195. return time.Duration(f), nil
  1196. case time.Duration:
  1197. return x, nil
  1198. default:
  1199. return invConv(val, typ)
  1200. }
  1201. case qTime:
  1202. switch x := val.(type) {
  1203. // case blob
  1204. // case bool
  1205. //case idealComplex:
  1206. //case idealFloat:
  1207. //case idealInt:
  1208. //case idealRune:
  1209. //case idealUint:
  1210. //case complex64
  1211. //case complex128
  1212. //case float32:
  1213. //case float64:
  1214. //case int8:
  1215. //case int16:
  1216. //case int32:
  1217. //case int64:
  1218. //case string:
  1219. //case uint8:
  1220. //case uint16:
  1221. //case uint32:
  1222. //case uint64:
  1223. //case *big.Int:
  1224. //case *big.Rat:
  1225. //case time.Duration:
  1226. case time.Time:
  1227. return x, nil
  1228. default:
  1229. return invConv(val, typ)
  1230. }
  1231. default:
  1232. panic("internal error 006")
  1233. }
  1234. }
  1235. func invShiftRHS(lhs, rhs interface{}) (interface{}, error) {
  1236. return nil, fmt.Errorf("invalid operation: %v << %v (shift count type %T, must be unsigned integer)", lhs, rhs, rhs)
  1237. }
  1238. func invTruncInt(v interface{}) error {
  1239. return fmt.Errorf("constant %v truncated to integer", v)
  1240. }
  1241. func overflow(v interface{}, typ int) error {
  1242. return fmt.Errorf("constant %v overflows %s", v, typeStr(typ))
  1243. }
  1244. func typeCheck1(val interface{}, c *col) (interface{}, error) {
  1245. rec := []interface{}{val}
  1246. c = c.clone()
  1247. c.index = 0
  1248. if err := typeCheck(rec, []*col{c}); err != nil {
  1249. return nil, err
  1250. }
  1251. return rec[0], nil
  1252. }
  1253. func typeCheck(rec []interface{}, cols []*col) (err error) {
  1254. for _, c := range cols {
  1255. i := c.index
  1256. if v := rec[i]; !c.typeCheck(v) {
  1257. switch v.(type) {
  1258. case idealComplex:
  1259. y := complex128(v.(idealComplex))
  1260. switch c.typ {
  1261. case qBool:
  1262. case qComplex64:
  1263. rec[i] = complex64(y)
  1264. continue
  1265. case qComplex128:
  1266. rec[i] = complex128(y)
  1267. continue
  1268. case qFloat32, qFloat64, qInt8, qInt16, qInt32, qInt64, qUint8, qUint16, qUint32, qUint64:
  1269. return fmt.Errorf("constant %v truncated to real", y)
  1270. }
  1271. case idealFloat:
  1272. y := float64(v.(idealFloat))
  1273. switch c.typ {
  1274. case qBool:
  1275. case qComplex64:
  1276. rec[i] = complex(float32(y), 0)
  1277. continue
  1278. case qComplex128:
  1279. rec[i] = complex(float64(y), 0)
  1280. continue
  1281. case qFloat32:
  1282. rec[i] = float32(y)
  1283. continue
  1284. case qFloat64:
  1285. rec[i] = float64(y)
  1286. continue
  1287. case qInt8:
  1288. if math.Floor(y) != y {
  1289. return invTruncInt(y)
  1290. }
  1291. if y < math.MinInt8 || y > math.MaxInt8 {
  1292. return overflow(y, c.typ)
  1293. }
  1294. rec[i] = int8(y)
  1295. continue
  1296. case qInt16:
  1297. if math.Floor(y) != y {
  1298. return invTruncInt(y)
  1299. }
  1300. if y < math.MinInt16 || y > math.MaxInt16 {
  1301. return overflow(y, c.typ)
  1302. }
  1303. rec[i] = int16(y)
  1304. continue
  1305. case qInt32:
  1306. if math.Floor(y) != y {
  1307. return invTruncInt(y)
  1308. }
  1309. if y < math.MinInt32 || y > math.MaxInt32 {
  1310. return overflow(y, c.typ)
  1311. }
  1312. rec[i] = int32(y)
  1313. continue
  1314. case qInt64:
  1315. if math.Floor(y) != y {
  1316. return invTruncInt(y)
  1317. }
  1318. if y < math.MinInt64 || y > math.MaxInt64 {
  1319. return overflow(y, c.typ)
  1320. }
  1321. rec[i] = int64(y)
  1322. continue
  1323. case qString:
  1324. case qUint8:
  1325. if math.Floor(y) != y {
  1326. return invTruncInt(y)
  1327. }
  1328. if y < 0 || y > math.MaxUint8 {
  1329. return overflow(y, c.typ)
  1330. }
  1331. rec[i] = uint8(y)
  1332. continue
  1333. case qUint16:
  1334. if math.Floor(y) != y {
  1335. return invTruncInt(y)
  1336. }
  1337. if y < 0 || y > math.MaxUint16 {
  1338. return overflow(y, c.typ)
  1339. }
  1340. rec[i] = uint16(y)
  1341. continue
  1342. case qUint32:
  1343. if math.Floor(y) != y {
  1344. return invTruncInt(y)
  1345. }
  1346. if y < 0 || y > math.MaxUint32 {
  1347. return overflow(y, c.typ)
  1348. }
  1349. rec[i] = uint32(y)
  1350. continue
  1351. case qUint64:
  1352. if math.Floor(y) != y {
  1353. return invTruncInt(y)
  1354. }
  1355. if y < 0 || y > math.MaxUint64 {
  1356. return overflow(y, c.typ)
  1357. }
  1358. rec[i] = uint64(y)
  1359. continue
  1360. case qBigInt:
  1361. if math.Floor(y) != y {
  1362. return invTruncInt(y)
  1363. }
  1364. rr := big.NewRat(1, 1).SetFloat64(y)
  1365. ii := big.NewInt(0)
  1366. ii.Set(rr.Num())
  1367. ii.Quo(ii, rr.Denom())
  1368. rec[i] = ii
  1369. continue
  1370. case qBigRat:
  1371. rec[i] = big.NewRat(1, 1).SetFloat64(y)
  1372. continue
  1373. }
  1374. case idealInt:
  1375. y := int64(v.(idealInt))
  1376. switch c.typ {
  1377. case qBool:
  1378. case qComplex64:
  1379. rec[i] = complex(float32(y), 0)
  1380. continue
  1381. case qComplex128:
  1382. rec[i] = complex(float64(y), 0)
  1383. continue
  1384. case qFloat32:
  1385. rec[i] = float32(y)
  1386. continue
  1387. case qFloat64:
  1388. rec[i] = float64(y)
  1389. continue
  1390. case qInt8:
  1391. if y < math.MinInt8 || y > math.MaxInt8 {
  1392. return overflow(y, c.typ)
  1393. }
  1394. rec[i] = int8(y)
  1395. continue
  1396. case qInt16:
  1397. if y < math.MinInt16 || y > math.MaxInt16 {
  1398. return overflow(y, c.typ)
  1399. }
  1400. rec[i] = int16(y)
  1401. continue
  1402. case qInt32:
  1403. if y < math.MinInt32 || y > math.MaxInt32 {
  1404. return overflow(y, c.typ)
  1405. }
  1406. rec[i] = int32(y)
  1407. continue
  1408. case qInt64:
  1409. if y < math.MinInt64 || y > math.MaxInt64 {
  1410. return overflow(y, c.typ)
  1411. }
  1412. rec[i] = int64(y)
  1413. continue
  1414. case qString:
  1415. case qUint8:
  1416. if y < 0 || y > math.MaxUint8 {
  1417. return overflow(y, c.typ)
  1418. }
  1419. rec[i] = uint8(y)
  1420. continue
  1421. case qUint16:
  1422. if y < 0 || y > math.MaxUint16 {
  1423. return overflow(y, c.typ)
  1424. }
  1425. rec[i] = uint16(y)
  1426. continue
  1427. case qUint32:
  1428. if y < 0 || y > math.MaxUint32 {
  1429. return overflow(y, c.typ)
  1430. }
  1431. rec[i] = uint32(y)
  1432. continue
  1433. case qUint64:
  1434. if y < 0 {
  1435. return overflow(y, c.typ)
  1436. }
  1437. rec[i] = uint64(y)
  1438. continue
  1439. case qBigInt:
  1440. rec[i] = big.NewInt(y)
  1441. continue
  1442. case qBigRat:
  1443. rec[i] = big.NewRat(1, 1).SetInt64(y)
  1444. continue
  1445. }
  1446. case idealRune:
  1447. y := int64(v.(idealRune))
  1448. switch c.typ {
  1449. case qBool:
  1450. case qComplex64:
  1451. rec[i] = complex(float32(y), 0)
  1452. continue
  1453. case qComplex128:
  1454. rec[i] = complex(float64(y), 0)
  1455. continue
  1456. case qFloat32:
  1457. rec[i] = float32(y)
  1458. continue
  1459. case qFloat64:
  1460. rec[i] = float64(y)
  1461. continue
  1462. case qInt8:
  1463. if y < math.MinInt8 || y > math.MaxInt8 {
  1464. return overflow(y, c.typ)
  1465. }
  1466. rec[i] = int8(y)
  1467. continue
  1468. case qInt16:
  1469. if y < math.MinInt16 || y > math.MaxInt16 {
  1470. return overflow(y, c.typ)
  1471. }
  1472. rec[i] = int16(y)
  1473. continue
  1474. case qInt32:
  1475. if y < math.MinInt32 || y > math.MaxInt32 {
  1476. return overflow(y, c.typ)
  1477. }
  1478. rec[i] = int32(y)
  1479. continue
  1480. case qInt64:
  1481. if y < math.MinInt64 || y > math.MaxInt64 {
  1482. return overflow(y, c.typ)
  1483. }
  1484. rec[i] = int64(y)
  1485. continue
  1486. case qString:
  1487. case qUint8:
  1488. if y < 0 || y > math.MaxUint8 {
  1489. return overflow(y, c.typ)
  1490. }
  1491. rec[i] = uint8(y)
  1492. continue
  1493. case qUint16:
  1494. if y < 0 || y > math.MaxUint16 {
  1495. return overflow(y, c.typ)
  1496. }
  1497. rec[i] = uint16(y)
  1498. continue
  1499. case qUint32:
  1500. if y < 0 {
  1501. return overflow(y, c.typ)
  1502. }
  1503. rec[i] = uint32(y)
  1504. continue
  1505. case qUint64:
  1506. if y < 0 {
  1507. return overflow(y, c.typ)
  1508. }
  1509. rec[i] = uint64(y)
  1510. continue
  1511. case qBigInt:
  1512. rec[i] = big.NewInt(y)
  1513. continue
  1514. case qBigRat:
  1515. rec[i] = big.NewRat(1, 1).SetInt64(y)
  1516. continue
  1517. }
  1518. case idealUint:
  1519. y := uint64(v.(idealUint))
  1520. switch c.typ {
  1521. case qBool:
  1522. case qComplex64:
  1523. rec[i] = complex(float32(y), 0)
  1524. continue
  1525. case qComplex128:
  1526. rec[i] = complex(float64(y), 0)
  1527. continue
  1528. case qFloat32:
  1529. rec[i] = float32(y)
  1530. continue
  1531. case qFloat64:
  1532. rec[i] = float64(y)
  1533. continue
  1534. case qInt8:
  1535. if y > math.MaxInt8 {
  1536. return overflow(y, c.typ)
  1537. }
  1538. rec[i] = int8(y)
  1539. continue
  1540. case qInt16:
  1541. if y > math.MaxInt16 {
  1542. return overflow(y, c.typ)
  1543. }
  1544. rec[i] = int16(y)
  1545. continue
  1546. case qInt32:
  1547. if y > math.MaxInt32 {
  1548. return overflow(y, c.typ)
  1549. }
  1550. rec[i] = int32(y)
  1551. continue
  1552. case qInt64:
  1553. if y > math.MaxInt64 {
  1554. return overflow(y, c.typ)
  1555. }
  1556. rec[i] = int64(y)
  1557. continue
  1558. case qString:
  1559. rec[i] = string(y)
  1560. continue
  1561. case qUint8:
  1562. if y > math.MaxUint8 {
  1563. return overflow(y, c.typ)
  1564. }
  1565. rec[i] = uint8(y)
  1566. continue
  1567. case qUint16:
  1568. if y > math.MaxUint16 {
  1569. return overflow(y, c.typ)
  1570. }
  1571. rec[i] = uint16(y)
  1572. continue
  1573. case qUint32:
  1574. if y > math.MaxUint32 {
  1575. return overflow(y, c.typ)
  1576. }
  1577. rec[i] = uint32(y)
  1578. continue
  1579. case qUint64:
  1580. rec[i] = uint64(y)
  1581. continue
  1582. case qBigInt:
  1583. rec[i] = big.NewInt(0).SetUint64(y)
  1584. continue
  1585. case qBigRat:
  1586. ii := big.NewInt(0).SetUint64(y)
  1587. rec[i] = big.NewRat(1, 1).SetInt(ii)
  1588. continue
  1589. }
  1590. }
  1591. return fmt.Errorf("cannot use %v (type %T) in assignment to, or comparison with, column %s (type %s)", v, ideal(v), c.name, typeStr(c.typ))
  1592. }
  1593. }
  1594. return
  1595. }
  1596. //TODO collate1 should return errors instead of panicing
  1597. func collate1(a, b interface{}) int {
  1598. switch x := a.(type) {
  1599. case nil:
  1600. if b != nil {
  1601. return -1
  1602. }
  1603. return 0
  1604. case bool:
  1605. switch y := b.(type) {
  1606. case nil:
  1607. return 1
  1608. case bool:
  1609. if !x && y {
  1610. return -1
  1611. }
  1612. if x == y {
  1613. return 0
  1614. }
  1615. return 1
  1616. default:
  1617. // Make bool collate before anything except nil and
  1618. // other bool for index seeking first non NULL value.
  1619. return -1
  1620. }
  1621. case idealComplex:
  1622. switch y := b.(type) {
  1623. case nil:
  1624. return 1
  1625. case idealComplex:
  1626. if x == y {
  1627. return 0
  1628. }
  1629. if real(x) < real(y) {
  1630. return -1
  1631. }
  1632. if real(x) > real(y) {
  1633. return 1
  1634. }
  1635. if imag(x) < imag(y) {
  1636. return -1
  1637. }
  1638. return 1
  1639. case complex64:
  1640. {
  1641. x, y := complex64(x), complex64(y)
  1642. if x == y {
  1643. return 0
  1644. }
  1645. if real(x) < real(y) {
  1646. return -1
  1647. }
  1648. if real(x) > real(y) {
  1649. return 1
  1650. }
  1651. if imag(x) < imag(y) {
  1652. return -1
  1653. }
  1654. return 1
  1655. }
  1656. case complex128:
  1657. {
  1658. x := complex128(x)
  1659. if x == y {
  1660. return 0
  1661. }
  1662. if real(x) < real(y) {
  1663. return -1
  1664. }
  1665. if real(x) > real(y) {
  1666. return 1
  1667. }
  1668. if imag(x) < imag(y) {
  1669. return -1
  1670. }
  1671. return 1
  1672. }
  1673. default:
  1674. panic("internal error 012")
  1675. }
  1676. case idealUint:
  1677. switch y := b.(type) {
  1678. case nil:
  1679. return 1
  1680. case idealUint:
  1681. if x < y {
  1682. return -1
  1683. }
  1684. if x == y {
  1685. return 0
  1686. }
  1687. return 1
  1688. case uint8:
  1689. {
  1690. x, y := uint64(x), uint64(y)
  1691. if x < y {
  1692. return -1
  1693. }
  1694. if x == y {
  1695. return 0
  1696. }
  1697. return 1
  1698. }
  1699. case uint16:
  1700. {
  1701. x, y := uint64(x), uint64(y)
  1702. if x < y {
  1703. return -1
  1704. }
  1705. if x == y {
  1706. return 0
  1707. }
  1708. return 1
  1709. }
  1710. case uint32:
  1711. {
  1712. x, y := uint64(x), uint64(y)
  1713. if x < y {
  1714. return -1
  1715. }
  1716. if x == y {
  1717. return 0
  1718. }
  1719. return 1
  1720. }
  1721. case uint64:
  1722. {
  1723. x, y := uint64(x), uint64(y)
  1724. if x < y {
  1725. return -1
  1726. }
  1727. if x == y {
  1728. return 0
  1729. }
  1730. return 1
  1731. }
  1732. case uint:
  1733. {
  1734. x, y := uint64(x), uint64(y)
  1735. if x < y {
  1736. return -1
  1737. }
  1738. if x == y {
  1739. return 0
  1740. }
  1741. return 1
  1742. }
  1743. default:
  1744. panic("internal error 013")
  1745. }
  1746. case idealRune:
  1747. switch y := b.(type) {
  1748. case nil:
  1749. return 1
  1750. case idealRune:
  1751. if x < y {
  1752. return -1
  1753. }
  1754. if x == y {
  1755. return 0
  1756. }
  1757. return 1
  1758. case int8:
  1759. {
  1760. x, y := int64(x), int64(y)
  1761. if x < y {
  1762. return -1
  1763. }
  1764. if x == y {
  1765. return 0
  1766. }
  1767. return 1
  1768. }
  1769. case int16:
  1770. {
  1771. x, y := int64(x), int64(y)
  1772. if x < y {
  1773. return -1
  1774. }
  1775. if x == y {
  1776. return 0
  1777. }
  1778. return 1
  1779. }
  1780. case int32:
  1781. {
  1782. x, y := int64(x), int64(y)
  1783. if x < y {
  1784. return -1
  1785. }
  1786. if x == y {
  1787. return 0
  1788. }
  1789. return 1
  1790. }
  1791. case int64:
  1792. {
  1793. x, y := int64(x), int64(y)
  1794. if x < y {
  1795. return -1
  1796. }
  1797. if x == y {
  1798. return 0
  1799. }
  1800. return 1
  1801. }
  1802. case int:
  1803. {
  1804. x, y := int64(x), int64(y)
  1805. if x < y {
  1806. return -1
  1807. }
  1808. if x == y {
  1809. return 0
  1810. }
  1811. return 1
  1812. }
  1813. default:
  1814. panic("internal error 014")
  1815. }
  1816. case idealInt:
  1817. switch y := b.(type) {
  1818. case nil:
  1819. return 1
  1820. case idealInt:
  1821. if x < y {
  1822. return -1
  1823. }
  1824. if x == y {
  1825. return 0
  1826. }
  1827. return 1
  1828. case int8:
  1829. {
  1830. x, y := int64(x), int64(y)
  1831. if x < y {
  1832. return -1
  1833. }
  1834. if x == y {
  1835. return 0
  1836. }
  1837. return 1
  1838. }
  1839. case int16:
  1840. {
  1841. x, y := int64(x), int64(y)
  1842. if x < y {
  1843. return -1
  1844. }
  1845. if x == y {
  1846. return 0
  1847. }
  1848. return 1
  1849. }
  1850. case int32:
  1851. {
  1852. x, y := int64(x), int64(y)
  1853. if x < y {
  1854. return -1
  1855. }
  1856. if x == y {
  1857. return 0
  1858. }
  1859. return 1
  1860. }
  1861. case int64:
  1862. {
  1863. x, y := int64(x), int64(y)
  1864. if x < y {
  1865. return -1
  1866. }
  1867. if x == y {
  1868. return 0
  1869. }
  1870. return 1
  1871. }
  1872. case int:
  1873. {
  1874. x, y := int64(x), int64(y)
  1875. if x < y {
  1876. return -1
  1877. }
  1878. if x == y {
  1879. return 0
  1880. }
  1881. return 1
  1882. }
  1883. default:
  1884. panic("internal error 015")
  1885. }
  1886. case idealFloat:
  1887. switch y := b.(type) {
  1888. case nil:
  1889. return 1
  1890. case idealFloat:
  1891. if x < y {
  1892. return -1
  1893. }
  1894. if x == y {
  1895. return 0
  1896. }
  1897. return 1
  1898. case float32:
  1899. {
  1900. x, y := float64(x), float64(y)
  1901. if x < y {
  1902. return -1
  1903. }
  1904. if x == y {
  1905. return 0
  1906. }
  1907. return 1
  1908. }
  1909. case float64:
  1910. {
  1911. x, y := float64(x), float64(y)
  1912. if x < y {
  1913. return -1
  1914. }
  1915. if x == y {
  1916. return 0
  1917. }
  1918. return 1
  1919. }
  1920. default:
  1921. panic("internal error 016")
  1922. }
  1923. case complex64:
  1924. switch y := b.(type) {
  1925. case nil:
  1926. return 1
  1927. case complex64:
  1928. if x == y {
  1929. return 0
  1930. }
  1931. if real(x) < real(y) {
  1932. return -1
  1933. }
  1934. if real(x) > real(y) {
  1935. return 1
  1936. }
  1937. if imag(x) < imag(y) {
  1938. return -1
  1939. }
  1940. return 1
  1941. case idealComplex:
  1942. {
  1943. x, y := complex64(x), complex64(y)
  1944. if x == y {
  1945. return 0
  1946. }
  1947. if real(x) < real(y) {
  1948. return -1
  1949. }
  1950. if real(x) > real(y) {
  1951. return 1
  1952. }
  1953. if imag(x) < imag(y) {
  1954. return -1
  1955. }
  1956. return 1
  1957. }
  1958. default:
  1959. panic("internal error 017")
  1960. }
  1961. case complex128:
  1962. switch y := b.(type) {
  1963. case nil:
  1964. return 1
  1965. case complex128:
  1966. if x == y {
  1967. return 0
  1968. }
  1969. if real(x) < real(y) {
  1970. return -1
  1971. }
  1972. if real(x) > real(y) {
  1973. return 1
  1974. }
  1975. if imag(x) < imag(y) {
  1976. return -1
  1977. }
  1978. return 1
  1979. case idealComplex:
  1980. {
  1981. x, y := complex128(x), complex128(y)
  1982. if x == y {
  1983. return 0
  1984. }
  1985. if real(x) < real(y) {
  1986. return -1
  1987. }
  1988. if real(x) > real(y) {
  1989. return 1
  1990. }
  1991. if imag(x) < imag(y) {
  1992. return -1
  1993. }
  1994. return 1
  1995. }
  1996. default:
  1997. panic("internal error 018")
  1998. }
  1999. case float32:
  2000. switch y := b.(type) {
  2001. case nil:
  2002. return 1
  2003. case float32:
  2004. if x < y {
  2005. return -1
  2006. }
  2007. if x == y {
  2008. return 0
  2009. }
  2010. return 1
  2011. case idealFloat:
  2012. {
  2013. x, y := float32(x), float32(y)
  2014. if x < y {
  2015. return -1
  2016. }
  2017. if x == y {
  2018. return 0
  2019. }
  2020. return 1
  2021. }
  2022. default:
  2023. panic("internal error 019")
  2024. }
  2025. case float64:
  2026. switch y := b.(type) {
  2027. case nil:
  2028. return 1
  2029. case float64:
  2030. if x < y {
  2031. return -1
  2032. }
  2033. if x == y {
  2034. return 0
  2035. }
  2036. return 1
  2037. case idealFloat:
  2038. {
  2039. x, y := float64(x), float64(y)
  2040. if x < y {
  2041. return -1
  2042. }
  2043. if x == y {
  2044. return 0
  2045. }
  2046. return 1
  2047. }
  2048. default:
  2049. panic("internal error 020")
  2050. }
  2051. case int8:
  2052. switch y := b.(type) {
  2053. case nil:
  2054. return 1
  2055. case int8:
  2056. if x < y {
  2057. return -1
  2058. }
  2059. if x == y {
  2060. return 0
  2061. }
  2062. return 1
  2063. case idealInt:
  2064. {
  2065. x, y := int64(x), int64(y)
  2066. if x < y {
  2067. return -1
  2068. }
  2069. if x == y {
  2070. return 0
  2071. }
  2072. return 1
  2073. }
  2074. default:
  2075. panic("internal error 021")
  2076. }
  2077. case int16:
  2078. switch y := b.(type) {
  2079. case nil:
  2080. return 1
  2081. case int16:
  2082. if x < y {
  2083. return -1
  2084. }
  2085. if x == y {
  2086. return 0
  2087. }
  2088. return 1
  2089. case idealInt:
  2090. {
  2091. x, y := int64(x), int64(y)
  2092. if x < y {
  2093. return -1
  2094. }
  2095. if x == y {
  2096. return 0
  2097. }
  2098. return 1
  2099. }
  2100. default:
  2101. panic("internal error 022")
  2102. }
  2103. case int32:
  2104. switch y := b.(type) {
  2105. case nil:
  2106. return 1
  2107. case int32:
  2108. if x < y {
  2109. return -1
  2110. }
  2111. if x == y {
  2112. return 0
  2113. }
  2114. return 1
  2115. case idealInt:
  2116. {
  2117. x, y := int64(x), int64(y)
  2118. if x < y {
  2119. return -1
  2120. }
  2121. if x == y {
  2122. return 0
  2123. }
  2124. return 1
  2125. }
  2126. default:
  2127. panic("internal error 023")
  2128. }
  2129. case int64:
  2130. switch y := b.(type) {
  2131. case nil:
  2132. return 1
  2133. case int64:
  2134. if x < y {
  2135. return -1
  2136. }
  2137. if x == y {
  2138. return 0
  2139. }
  2140. return 1
  2141. case idealInt:
  2142. {
  2143. x, y := int64(x), int64(y)
  2144. if x < y {
  2145. return -1
  2146. }
  2147. if x == y {
  2148. return 0
  2149. }
  2150. return 1
  2151. }
  2152. default:
  2153. panic("internal error 024")
  2154. }
  2155. case uint8:
  2156. switch y := b.(type) {
  2157. case nil:
  2158. return 1
  2159. case uint8:
  2160. if x < y {
  2161. return -1
  2162. }
  2163. if x == y {
  2164. return 0
  2165. }
  2166. return 1
  2167. case idealInt:
  2168. {
  2169. x, y := uint64(x), uint64(y)
  2170. if x < y {
  2171. return -1
  2172. }
  2173. if x == y {
  2174. return 0
  2175. }
  2176. return 1
  2177. }
  2178. case idealUint:
  2179. {
  2180. x, y := uint64(x), uint64(y)
  2181. if x < y {
  2182. return -1
  2183. }
  2184. if x == y {
  2185. return 0
  2186. }
  2187. return 1
  2188. }
  2189. default:
  2190. panic("internal error 025")
  2191. }
  2192. case uint16:
  2193. switch y := b.(type) {
  2194. case nil:
  2195. return 1
  2196. case uint16:
  2197. if x < y {
  2198. return -1
  2199. }
  2200. if x == y {
  2201. return 0
  2202. }
  2203. return 1
  2204. case idealInt:
  2205. {
  2206. x, y := uint64(x), uint64(y)
  2207. if x < y {
  2208. return -1
  2209. }
  2210. if x == y {
  2211. return 0
  2212. }
  2213. return 1
  2214. }
  2215. case idealUint:
  2216. {
  2217. x, y := uint64(x), uint64(y)
  2218. if x < y {
  2219. return -1
  2220. }
  2221. if x == y {
  2222. return 0
  2223. }
  2224. return 1
  2225. }
  2226. default:
  2227. panic("internal error 026")
  2228. }
  2229. case uint32:
  2230. switch y := b.(type) {
  2231. case nil:
  2232. return 1
  2233. case uint32:
  2234. if x < y {
  2235. return -1
  2236. }
  2237. if x == y {
  2238. return 0
  2239. }
  2240. return 1
  2241. case idealInt:
  2242. {
  2243. x, y := uint64(x), uint64(y)
  2244. if x < y {
  2245. return -1
  2246. }
  2247. if x == y {
  2248. return 0
  2249. }
  2250. return 1
  2251. }
  2252. case idealUint:
  2253. {
  2254. x, y := uint64(x), uint64(y)
  2255. if x < y {
  2256. return -1
  2257. }
  2258. if x == y {
  2259. return 0
  2260. }
  2261. return 1
  2262. }
  2263. default:
  2264. panic("internal error 027")
  2265. }
  2266. case uint64:
  2267. switch y := b.(type) {
  2268. case nil:
  2269. return 1
  2270. case uint64:
  2271. if x < y {
  2272. return -1
  2273. }
  2274. if x == y {
  2275. return 0
  2276. }
  2277. return 1
  2278. case idealInt:
  2279. {
  2280. x, y := uint64(x), uint64(y)
  2281. if x < y {
  2282. return -1
  2283. }
  2284. if x == y {
  2285. return 0
  2286. }
  2287. return 1
  2288. }
  2289. case idealUint:
  2290. {
  2291. x, y := uint64(x), uint64(y)
  2292. if x < y {
  2293. return -1
  2294. }
  2295. if x == y {
  2296. return 0
  2297. }
  2298. return 1
  2299. }
  2300. default:
  2301. panic("internal error 028")
  2302. }
  2303. case string:
  2304. switch y := b.(type) {
  2305. case nil:
  2306. return 1
  2307. case string:
  2308. if x < y {
  2309. return -1
  2310. }
  2311. if x == y {
  2312. return 0
  2313. }
  2314. return 1
  2315. default:
  2316. panic("internal error 029")
  2317. }
  2318. case []byte:
  2319. switch y := b.(type) {
  2320. case nil:
  2321. return 1
  2322. case []byte:
  2323. return bytes.Compare(x, y)
  2324. default:
  2325. panic("internal error 030")
  2326. }
  2327. case *big.Int:
  2328. switch y := b.(type) {
  2329. case nil:
  2330. return 1
  2331. case *big.Int:
  2332. return x.Cmp(y)
  2333. case idealInt:
  2334. {
  2335. y := big.NewInt(int64(y))
  2336. return x.Cmp(y)
  2337. }
  2338. case idealUint:
  2339. {
  2340. u := big.NewInt(0)
  2341. u.SetUint64(uint64(y))
  2342. return x.Cmp(u)
  2343. }
  2344. default:
  2345. panic("internal error 031")
  2346. }
  2347. case *big.Rat:
  2348. switch y := b.(type) {
  2349. case nil:
  2350. return 1
  2351. case *big.Rat:
  2352. return x.Cmp(y)
  2353. case idealInt:
  2354. {
  2355. y := big.NewRat(int64(y), 1)
  2356. return x.Cmp(y)
  2357. }
  2358. case idealUint:
  2359. {
  2360. u := big.NewInt(0)
  2361. u.SetUint64(uint64(y))
  2362. var y big.Rat
  2363. y.SetInt(u)
  2364. return x.Cmp(&y)
  2365. }
  2366. default:
  2367. panic("internal error 032")
  2368. }
  2369. case time.Time:
  2370. switch y := b.(type) {
  2371. case nil:
  2372. return 1
  2373. case time.Time:
  2374. if x.Before(y) {
  2375. return -1
  2376. }
  2377. if x.Equal(y) {
  2378. return 0
  2379. }
  2380. return 1
  2381. default:
  2382. panic("internal error 033")
  2383. }
  2384. case time.Duration:
  2385. switch y := b.(type) {
  2386. case nil:
  2387. return 1
  2388. case time.Duration:
  2389. if x < y {
  2390. return -1
  2391. }
  2392. if x == y {
  2393. return 0
  2394. }
  2395. return 1
  2396. default:
  2397. panic("internal error 034")
  2398. }
  2399. case chunk:
  2400. switch y := b.(type) {
  2401. case nil:
  2402. return 1
  2403. case chunk:
  2404. a, err := x.expand()
  2405. if err != nil {
  2406. panic(err)
  2407. }
  2408. b, err := y.expand()
  2409. if err != nil {
  2410. panic(err)
  2411. }
  2412. return collate1(a, b)
  2413. default:
  2414. panic("internal error 035")
  2415. }
  2416. default:
  2417. //dbg("%T(%v) %T(%v)", a, a, b, b)
  2418. panic("internal error 036")
  2419. }
  2420. }
  2421. //TODO collate should return errors from collate1
  2422. func collate(x, y []interface{}) (r int) {
  2423. //defer func() { dbg("%v %v -> %v", x, y, r) }()
  2424. nx, ny := len(x), len(y)
  2425. switch {
  2426. case nx == 0 && ny != 0:
  2427. return -1
  2428. case nx == 0 && ny == 0:
  2429. return 0
  2430. case nx != 0 && ny == 0:
  2431. return 1
  2432. }
  2433. r = 1
  2434. if nx > ny {
  2435. x, y, r = y, x, -r
  2436. }
  2437. for i, xi := range x {
  2438. if c := collate1(xi, y[i]); c != 0 {
  2439. return c * r
  2440. }
  2441. }
  2442. if nx == ny {
  2443. return 0
  2444. }
  2445. return -r
  2446. }
  2447. var collators = map[bool]func(a, b []interface{}) int{false: collateDesc, true: collate}
  2448. func collateDesc(a, b []interface{}) int {
  2449. return -collate(a, b)
  2450. }
  2451. func isOrderedType(v interface{}) (y interface{}, r bool, err error) {
  2452. //dbg("====")
  2453. //dbg("%T(%v)", v, v)
  2454. //defer func() { dbg("%T(%v)", y, y) }()
  2455. switch x := v.(type) {
  2456. case idealFloat, idealInt, idealRune, idealUint,
  2457. float32, float64,
  2458. int8, int16, int32, int64,
  2459. uint8, uint16, uint32, uint64,
  2460. string:
  2461. return v, true, nil
  2462. case *big.Int, *big.Rat, time.Time, time.Duration:
  2463. return x, true, nil
  2464. case chunk:
  2465. if y, err = x.expand(); err != nil {
  2466. return
  2467. }
  2468. return isOrderedType(y)
  2469. }
  2470. return v, false, nil
  2471. }
  2472. var isSystemName = map[string]bool{
  2473. "__Column": true,
  2474. "__Column2": true,
  2475. "__Index": true,
  2476. "__Index2": true,
  2477. "__Index2_Column": true,
  2478. "__Index2_Expr": true,
  2479. "__Table": true,
  2480. }
  2481. func qnames(l []string) []string {
  2482. r := make([]string, len(l))
  2483. for i, v := range l {
  2484. r[i] = fmt.Sprintf("%q", v)
  2485. }
  2486. return r
  2487. }