select_json_object_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. package oss
  2. import (
  3. . "gopkg.in/check.v1"
  4. "os"
  5. "io/ioutil"
  6. "strings"
  7. "net/http"
  8. )
  9. type OssSelectJsonSuite struct {
  10. client *Client
  11. bucket *Bucket
  12. }
  13. var _ = Suite(&OssSelectJsonSuite{})
  14. func (s *OssSelectJsonSuite) SetUpSuite(c *C) {
  15. client, err := New(endpoint, accessID, accessKey)
  16. c.Assert(err, IsNil)
  17. s.client = client
  18. s.client.Config.LogLevel = Error // Debug
  19. err = s.client.CreateBucket(bucketName)
  20. c.Assert(err, IsNil)
  21. bucket, err := s.client.Bucket(bucketName)
  22. c.Assert(err, IsNil)
  23. s.bucket = bucket
  24. testLogger.Println("test select json started")
  25. }
  26. func(s *OssSelectJsonSuite) TearDownSuite(c *C){
  27. // Delete objects
  28. marker := Marker("")
  29. for {
  30. lor, err := s.bucket.ListObjects(marker)
  31. c.Assert(err, IsNil)
  32. for _, object := range lor.Objects {
  33. err = s.bucket.DeleteObject(object.Key)
  34. c.Assert(err, IsNil)
  35. }
  36. marker = Marker(lor.NextMarker)
  37. if !lor.IsTruncated {
  38. break
  39. }
  40. }
  41. err := s.client.DeleteBucket(bucketName)
  42. c.Assert(err, IsNil)
  43. testLogger.Println("test select json completed")
  44. }
  45. func (s *OssSelectJsonSuite) SetUpTest(c *C) {
  46. testLogger.Println("test func", c.TestName(), "start")
  47. }
  48. func (s *OssSelectJsonSuite) TearDownTest(c *C) {
  49. testLogger.Println("test func", c.TestName(), "succeed")
  50. }
  51. func (s *OssSelectJsonSuite) TestCreateSelectJsonObjectMeta(c *C){
  52. key := "sample_json_lines.json"
  53. err := s.bucket.PutObjectFromFile(key, "../sample/sample_json_lines.json")
  54. c.Assert(err, IsNil)
  55. jsonMeta := JsonMetaRequest{
  56. InputSerialization: InputSerialization {
  57. JSON: JSON {
  58. JSONType:"LINES",
  59. },
  60. },
  61. }
  62. res,err := s.bucket.CreateSelectJsonObjectMeta(key, jsonMeta)
  63. c.Assert(err, IsNil)
  64. c.Assert(res.RowsCount, Equals, int64(100))
  65. err = s.bucket.DeleteObject(key)
  66. c.Assert(err, IsNil)
  67. }
  68. func (s *OssSelectJsonSuite) TestSelectJsonDocument(c *C){
  69. key := "sample_json.json"
  70. err := s.bucket.PutObjectFromFile(key, "../sample/sample_json.json")
  71. c.Assert(err, IsNil)
  72. selReq := SelectRequest{}
  73. selReq.Expression = "select * from ossobject.objects[*] where party = 'Democrat'"
  74. selReq.InputSerializationSelect.JsonBodyInput.JSONType = "DOCUMENT"
  75. selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
  76. var responseHeader http.Header
  77. body, err := s.bucket.SelectObject(key, selReq, GetResponseHeader(&responseHeader))
  78. c.Assert(err, IsNil)
  79. defer body.Close()
  80. requestId := GetRequestId(responseHeader)
  81. c.Assert(len(requestId) > 0, Equals, true)
  82. p := make([]byte, 512)
  83. n, err := body.Read(p)
  84. c.Assert(err, IsNil)
  85. c.Assert(n, Equals, 512)
  86. p1 := make([]byte, 3)
  87. _, err = body.Read(p1)
  88. c.Assert(err, IsNil)
  89. rets, err := ioutil.ReadAll(body)
  90. c.Assert(err,IsNil)
  91. str,err := readJsonDocument("../sample/sample_json.json")
  92. c.Assert(err, IsNil)
  93. c.Assert(string(p) + string(p1) + string(rets), Equals, str)
  94. err = s.bucket.DeleteObject(key)
  95. c.Assert(err, IsNil)
  96. }
  97. func (s *OssSelectJsonSuite) TestSelectJsonLines(c *C) {
  98. key := "sample_json_lines.json"
  99. err := s.bucket.PutObjectFromFile(key, "../sample/sample_json_lines.json")
  100. c.Assert(err, IsNil)
  101. selReq := SelectRequest{}
  102. selReq.Expression = "select * from ossobject where party = 'Democrat'"
  103. selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
  104. selReq.InputSerializationSelect.JsonBodyInput.JSONType = "LINES"
  105. var responseHeader http.Header
  106. body, err := s.bucket.SelectObject(key, selReq, GetResponseHeader(&responseHeader))
  107. c.Assert(err, IsNil)
  108. defer body.Close()
  109. requestId := GetRequestId(responseHeader)
  110. c.Assert(len(requestId) > 0, Equals, true)
  111. rets, err := ioutil.ReadAll(body)
  112. c.Assert(err,IsNil)
  113. str,err := readJsonDocument("../sample/sample_json.json")
  114. c.Assert(string(rets), Equals, str)
  115. err = s.bucket.DeleteObject(key)
  116. c.Assert(err, IsNil)
  117. }
  118. func (s *OssSelectJsonSuite) TestSelectJsonLinesIntoFile(c *C) {
  119. key := "sample_json_lines.json"
  120. err := s.bucket.PutObjectFromFile(key, "../sample/sample_json_lines.json")
  121. c.Assert(err, IsNil)
  122. jsonMeta := JsonMetaRequest{
  123. InputSerialization: InputSerialization {
  124. JSON: JSON {
  125. JSONType:"LINES",
  126. },
  127. },
  128. }
  129. res,err := s.bucket.CreateSelectJsonObjectMeta(key, jsonMeta)
  130. c.Assert(err, IsNil)
  131. c.Assert(res.RowsCount, Equals, int64(100))
  132. selReq := SelectRequest{}
  133. selReq.Expression = "select * from ossobject where party = 'Democrat'"
  134. selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
  135. selReq.InputSerializationSelect.JsonBodyInput.JSONType = "LINES"
  136. var responseHeader http.Header
  137. outfile := "sample_json_out.json"
  138. err = s.bucket.SelectObjectIntoFile(key, outfile, selReq, GetResponseHeader(&responseHeader))
  139. c.Assert(err, IsNil)
  140. requestId := GetRequestId(responseHeader)
  141. c.Assert(len(requestId) > 0, Equals, true)
  142. _, err = os.Stat(outfile)
  143. c.Assert(err,IsNil)
  144. err = os.Remove(outfile)
  145. c.Assert(err, IsNil)
  146. err = s.bucket.DeleteObject(key)
  147. c.Assert(err, IsNil)
  148. }
  149. func (s *OssSelectJsonSuite) TestSelectJsonDocumentIntoFile(c *C) {
  150. key := "sample_json_lines.json"
  151. err := s.bucket.PutObjectFromFile(key, "../sample/sample_json.json")
  152. c.Assert(err, IsNil)
  153. selReq := SelectRequest{}
  154. selReq.Expression = "select * from ossobject.objects[*] where party = 'Democrat'"
  155. selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
  156. selReq.InputSerializationSelect.JsonBodyInput.JSONType = "DOCUMENT"
  157. var responseHeader http.Header
  158. outfile := "sample_json_out.json"
  159. err = s.bucket.SelectObjectIntoFile(key, outfile, selReq, GetResponseHeader(&responseHeader))
  160. c.Assert(err, IsNil)
  161. requestId := GetRequestId(responseHeader)
  162. c.Assert(len(requestId) > 0, Equals, true)
  163. _, err = os.Stat(outfile)
  164. c.Assert(err,IsNil)
  165. err = os.Remove(outfile)
  166. c.Assert(err, IsNil)
  167. err = s.bucket.DeleteObject(key)
  168. c.Assert(err, IsNil)
  169. }
  170. func (s *OssSelectJsonSuite) TestSelectJsonLinesLike(c *C) {
  171. key := "sample_json_lines.json"
  172. err := s.bucket.PutObjectFromFile(key, "../sample/sample_json_lines.json")
  173. c.Assert(err, IsNil)
  174. selReq := SelectRequest{}
  175. selReq.Expression = "select person.firstname, person.lastname from ossobject where person.birthday like '1959%'"
  176. selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
  177. selReq.InputSerializationSelect.JsonBodyInput.JSONType = "LINES"
  178. jsonMeta := JsonMetaRequest{
  179. InputSerialization: InputSerialization {
  180. JSON: JSON {
  181. JSONType:"LINES",
  182. },
  183. },
  184. }
  185. res,err := s.bucket.CreateSelectJsonObjectMeta(key, jsonMeta)
  186. c.Assert(err, IsNil)
  187. c.Assert(res.RowsCount, Equals, int64(100))
  188. var responseHeader http.Header
  189. body, err := s.bucket.SelectObject(key, selReq, GetResponseHeader(&responseHeader))
  190. c.Assert(err, IsNil)
  191. defer body.Close()
  192. requestId := GetRequestId(responseHeader)
  193. c.Assert(len(requestId) > 0, Equals, true)
  194. rets, err := ioutil.ReadAll(body)
  195. c.Assert(err,IsNil)
  196. str,err := readJsonLinesLike("../sample/sample_json.json")
  197. c.Assert(string(rets), Equals, str)
  198. err = s.bucket.DeleteObject(key)
  199. c.Assert(err, IsNil)
  200. }
  201. func (s *OssSelectJsonSuite) TestSelectJsonLinesRange(c *C) {
  202. key := "sample_json_lines.json"
  203. err := s.bucket.PutObjectFromFile(key, "../sample/sample_json_lines.json")
  204. c.Assert(err, IsNil)
  205. jsonMeta := JsonMetaRequest{
  206. InputSerialization: InputSerialization {
  207. JSON: JSON {
  208. JSONType:"LINES",
  209. },
  210. },
  211. }
  212. res,err := s.bucket.CreateSelectJsonObjectMeta(key, jsonMeta)
  213. c.Assert(err, IsNil)
  214. c.Assert(res.RowsCount, Equals, int64(100))
  215. selReq := SelectRequest{}
  216. selReq.Expression = "select person.firstname as aaa as firstname, person.lastname, extra from ossobject'"
  217. selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
  218. selReq.InputSerializationSelect.JsonBodyInput.JSONType = "LINES"
  219. selReq.InputSerializationSelect.JsonBodyInput.Range = "0-1"
  220. var responseHeader http.Header
  221. body, err := s.bucket.SelectObject(key, selReq, GetResponseHeader(&responseHeader))
  222. c.Assert(err, IsNil)
  223. defer body.Close()
  224. requestId := GetRequestId(responseHeader)
  225. c.Assert(len(requestId) > 0, Equals, true)
  226. rets, err := ioutil.ReadAll(body)
  227. c.Assert(err,IsNil)
  228. str,err := readJsonLinesRange("../sample/sample_json.json", 0, 2)
  229. c.Assert(string(rets), Equals, str)
  230. err = s.bucket.DeleteObject(key)
  231. c.Assert(err, IsNil)
  232. }
  233. func (s *OssSelectJsonSuite) TestSelectJsonDocumentIntAggregation(c *C) {
  234. key := "sample_json.json"
  235. err := s.bucket.PutObjectFromFile(key, "../sample/sample_json.json")
  236. c.Assert(err, IsNil)
  237. selReq := SelectRequest{}
  238. selReq.Expression = `
  239. select
  240. avg(cast(person.cspanid as int)), max(cast(person.cspanid as int)),
  241. min(cast(person.cspanid as int))
  242. from
  243. ossobject.objects[*]
  244. where
  245. person.cspanid = 1011723
  246. `
  247. selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
  248. selReq.InputSerializationSelect.JsonBodyInput.JSONType = "Document"
  249. body, err := s.bucket.SelectObject(key, selReq)
  250. c.Assert(err, IsNil)
  251. defer body.Close()
  252. rets, err := ioutil.ReadAll(body)
  253. c.Assert(err,IsNil)
  254. c.Assert(string(rets), Equals, "{\"_1\":1011723,\"_2\":1011723,\"_3\":1011723},")
  255. err = s.bucket.DeleteObject(key)
  256. c.Assert(err, IsNil)
  257. }
  258. func (s *OssSelectJsonSuite) TestSelectJsonDocumentFloatAggregation(c *C) {
  259. key := "sample_json.json"
  260. err := s.bucket.PutObjectFromFile(key, "../sample/sample_json.json")
  261. c.Assert(err, IsNil)
  262. selReq := SelectRequest{}
  263. selReq.Expression = `
  264. select
  265. avg(cast(person.cspanid as double)), max(cast(person.cspanid as double)),
  266. min(cast(person.cspanid as double))
  267. from
  268. ossobject.objects[*]
  269. `
  270. selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
  271. selReq.InputSerializationSelect.JsonBodyInput.JSONType = "Document"
  272. body, err := s.bucket.SelectObject(key, selReq)
  273. c.Assert(err, IsNil)
  274. defer body.Close()
  275. rets, err := ioutil.ReadAll(body)
  276. c.Assert(err,IsNil)
  277. testLogger.Println(string(rets))
  278. // avg, max, min, err := readJsonFloatAggregation("../sample/sample_json.json")
  279. // fmt.Println(string(rets), "\n", avg, max, min)
  280. // retsArr := strings.Split(string(rets), ":")
  281. // s1 := strconv.FormatFloat(avg, 'f', 6, 64) + ","
  282. // s1 += strconv.FormatFloat(max, 'f', 6, 64) + ","
  283. // s1 += strconv.FormatFloat(min, 'f', 6, 64) + ","
  284. // retS := ""
  285. // l := len(retsArr[1])
  286. // vv, err := strconv.ParseFloat(retsArr[1][:l-35], 64)
  287. // c.Assert(err, IsNil)
  288. // retS += strconv.FormatFloat(vv, 'f', 6, 64) + ","
  289. // l = len(retsArr[2])
  290. // vv, err = strconv.ParseFloat(retsArr[2][:l-6], 64)
  291. // c.Assert(err, IsNil)
  292. // retS += strconv.FormatFloat(vv, 'f', 6, 64) + ","
  293. // l = len(retsArr[3])
  294. // vv, err = strconv.ParseFloat(retsArr[3][:l-2], 64)
  295. // c.Assert(err, IsNil)
  296. // retS += strconv.FormatFloat(vv, 'f', 6, 64) + ","
  297. // c.Assert(retS, Equals, s1)
  298. err = s.bucket.DeleteObject(key)
  299. c.Assert(err, IsNil)
  300. }
  301. func (s *OssSelectJsonSuite) TestSelectJsonDocumentConcat(c *C) {
  302. key := "sample_json.json"
  303. err := s.bucket.PutObjectFromFile(key, "../sample/sample_json.json")
  304. c.Assert(err, IsNil)
  305. selReq := SelectRequest{}
  306. selReq.Expression = `
  307. select
  308. person
  309. from
  310. ossobject.objects[*]
  311. where
  312. (person.firstname || person.lastname) = 'JohnKennedy'
  313. `
  314. selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
  315. selReq.InputSerializationSelect.JsonBodyInput.JSONType = "Document"
  316. body, err := s.bucket.SelectObject(key, selReq)
  317. c.Assert(err, IsNil)
  318. defer body.Close()
  319. rets, err := ioutil.ReadAll(body)
  320. c.Assert(err,IsNil)
  321. str, err := readJsonDocumentConcat("../sample/sample_json.json")
  322. c.Assert(err,IsNil)
  323. c.Assert(string(rets), Equals, str)
  324. err = s.bucket.DeleteObject(key)
  325. c.Assert(err, IsNil)
  326. }
  327. func (s *OssSelectJsonSuite) TestSelectJsonComplicateConcat(c *C) {
  328. key := "sample_json.json"
  329. err := s.bucket.PutObjectFromFile(key, "../sample/sample_json_lines.json")
  330. c.Assert(err, IsNil)
  331. selReq := SelectRequest{}
  332. selReq.Expression = `
  333. select
  334. person.firstname, person.lastname, congress_numbers
  335. from
  336. ossobject
  337. where
  338. startdate > '2017-01-01' and
  339. senator_rank = 'junior' or
  340. state = 'CA' and
  341. party = 'Republican'
  342. `
  343. selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
  344. selReq.InputSerializationSelect.JsonBodyInput.JSONType = "LINES"
  345. body, err := s.bucket.SelectObject(key, selReq)
  346. c.Assert(err, IsNil)
  347. defer body.Close()
  348. rets, err := ioutil.ReadAll(body)
  349. c.Assert(err,IsNil)
  350. str, err := readJsonComplicateConcat("../sample/sample_json.json")
  351. c.Assert(err,IsNil)
  352. c.Assert(string(rets), Equals, str)
  353. err = s.bucket.DeleteObject(key)
  354. c.Assert(err, IsNil)
  355. }
  356. func (s *OssSelectJsonSuite) TestSelectJsonLineInvalidSql(c *C) {
  357. key := "sample_json.json"
  358. err := s.bucket.PutObjectFromFile(key, "../sample/sample_json_lines.json")
  359. c.Assert(err, IsNil)
  360. selReq := SelectRequest{}
  361. selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
  362. selReq.InputSerializationSelect.JsonBodyInput.JSONType = "LINES"
  363. selReq.Expression = `select * from ossobject where avg(cast(person.birthday as int)) > 2016`
  364. _, err = s.bucket.SelectObject(key, selReq)
  365. c.Assert(err, NotNil)
  366. selReq.Expression = ``
  367. _, err = s.bucket.SelectObject(key, selReq)
  368. c.Assert(err, NotNil)
  369. selReq.Expression = `select person.lastname || person.firstname from ossobject`
  370. _, err = s.bucket.SelectObject(key, selReq)
  371. c.Assert(err, NotNil)
  372. selReq.Expression = `select * from ossobject group by person.firstname`
  373. _, err = s.bucket.SelectObject(key, selReq)
  374. c.Assert(err, NotNil)
  375. selReq.Expression = `select * from ossobject order by _1`
  376. _, err = s.bucket.SelectObject(key, selReq)
  377. c.Assert(err, NotNil)
  378. selReq.Expression = `select * from ossobject oss join s3object s3 on oss.CityName = s3.CityName`
  379. _, err = s.bucket.SelectObject(key, selReq)
  380. c.Assert(err, NotNil)
  381. err = s.bucket.PutObjectFromFile(key, "../sample/sample_json.json")
  382. c.Assert(err, IsNil)
  383. selReq.InputSerializationSelect.JsonBodyInput.JSONType = "DOCUMENT"
  384. selReq.Expression = `select _1 from ossobject.objects[*]`
  385. body, err := s.bucket.SelectObject(key, selReq)
  386. c.Assert(err, IsNil)
  387. defer body.Close()
  388. err = s.bucket.DeleteObject(key)
  389. c.Assert(err, IsNil)
  390. }
  391. func (s *OssSelectJsonSuite) TestSelectJsonParseNumAsString(c *C) {
  392. key := "sample_json.json"
  393. content := "{\"a\":123456789.123456789}"
  394. err := s.bucket.PutObject(key, strings.NewReader(content))
  395. c.Assert(err, IsNil)
  396. selReq := SelectRequest{}
  397. selReq.Expression = `select a from ossobject where cast(a as decimal) = 123456789.1234567890`
  398. bo := true
  399. selReq.InputSerializationSelect.JsonBodyInput.ParseJSONNumberAsString = &bo
  400. selReq.InputSerializationSelect.JsonBodyInput.JSONType = "DOCUMENT"
  401. body, err := s.bucket.SelectObject(key, selReq)
  402. c.Assert(err, IsNil)
  403. defer body.Close()
  404. rets, err := ioutil.ReadAll(body)
  405. c.Assert(err,IsNil)
  406. c.Assert(string(rets), Equals, "{\"a\":123456789.123456789}\n")
  407. err = s.bucket.DeleteObject(key)
  408. c.Assert(err, IsNil)
  409. }