Skip to content

Commit 8a491b9

Browse files
committed
Add support for range functions
1 parent 54e29a5 commit 8a491b9

File tree

3 files changed

+420
-0
lines changed

3 files changed

+420
-0
lines changed

examples/range_test.go

+220
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/**
2+
* @file range_test.go
3+
*
4+
* @section LICENSE
5+
*
6+
* The MIT License
7+
*
8+
* @copyright Copyright (c) 2019 TileDB, Inc.
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* in the Software without restriction, including without limitation the rights
13+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
* copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be included in
18+
* all copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
* THE SOFTWARE.
27+
*
28+
* @section DESCRIPTION
29+
*
30+
* This is a part of the TileDB range tutorial:
31+
* https://docs.tiledb.io/en/latest/range.html
32+
*
33+
* When run, this program will create a simple 2D dense array, write some data
34+
* to it, and read a slice of the data back in the layout of the user's choice
35+
* (passed as an argument to the program: "row", "col", or "global").
36+
*
37+
*/
38+
39+
package examples
40+
41+
import (
42+
"fmt"
43+
"os"
44+
45+
tiledb "github.com/TileDB-Inc/TileDB-Go"
46+
)
47+
48+
// Name of array.
49+
var rangeArrayName = "range_array"
50+
51+
func createRangeArray() {
52+
// Create a TileDB context.
53+
ctx, err := tiledb.NewContext(nil)
54+
checkError(err)
55+
56+
// The array will be 4x4 with dimensions "rows" and "cols", with domain [1,4].
57+
domain, err := tiledb.NewDomain(ctx)
58+
checkError(err)
59+
rowDim, err := tiledb.NewDimension(ctx, "rows", []int32{1, 4}, int32(4))
60+
checkError(err)
61+
colDim, err := tiledb.NewDimension(ctx, "cols", []int32{1, 4}, int32(4))
62+
checkError(err)
63+
err = domain.AddDimensions(rowDim, colDim)
64+
checkError(err)
65+
66+
// The array will be dense.
67+
schema, err := tiledb.NewArraySchema(ctx, tiledb.TILEDB_DENSE)
68+
err = schema.SetDomain(domain)
69+
checkError(err)
70+
err = schema.SetCellOrder(tiledb.TILEDB_ROW_MAJOR)
71+
checkError(err)
72+
err = schema.SetTileOrder(tiledb.TILEDB_ROW_MAJOR)
73+
checkError(err)
74+
75+
// Add a single attribute "a" so each (i,j) cell can store an integer.
76+
a, err := tiledb.NewAttribute(ctx, "a", tiledb.TILEDB_INT32)
77+
checkError(err)
78+
err = schema.AddAttributes(a)
79+
checkError(err)
80+
81+
// Create the (empty) array on disk.
82+
array, err := tiledb.NewArray(ctx, rangeArrayName)
83+
checkError(err)
84+
err = array.Create(schema)
85+
checkError(err)
86+
}
87+
88+
func writeRangeArray() {
89+
ctx, err := tiledb.NewContext(nil)
90+
checkError(err)
91+
92+
// Prepare some data for the array
93+
data := []int32{
94+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
95+
96+
// Open the array for writing and create the query.
97+
array, err := tiledb.NewArray(ctx, rangeArrayName)
98+
checkError(err)
99+
err = array.Open(tiledb.TILEDB_WRITE)
100+
checkError(err)
101+
query, err := tiledb.NewQuery(ctx, array)
102+
checkError(err)
103+
err = query.SetLayout(tiledb.TILEDB_ROW_MAJOR)
104+
checkError(err)
105+
_, err = query.SetBuffer("a", data)
106+
checkError(err)
107+
108+
// Perform the write and close the array.
109+
err = query.Submit()
110+
checkError(err)
111+
err = array.Close()
112+
checkError(err)
113+
}
114+
115+
func addRange() {
116+
ctx, err := tiledb.NewContext(nil)
117+
checkError(err)
118+
119+
// Prepare the array for reading
120+
array, err := tiledb.NewArray(ctx, rangeArrayName)
121+
checkError(err)
122+
err = array.Open(tiledb.TILEDB_READ)
123+
checkError(err)
124+
125+
// Prepare the query
126+
query, err := tiledb.NewQuery(ctx, array)
127+
checkError(err)
128+
129+
var startFloat float32 = 1
130+
var endFloat float32 = 3
131+
132+
// Try with invalid dimension types
133+
err = query.AddRange(0, &startFloat, &endFloat)
134+
fmt.Println(err)
135+
136+
var start int32 = 1
137+
var end int32 = 3
138+
139+
// Try with invalid dimension index
140+
err = query.AddRange(2, &start, &end)
141+
fmt.Println(err)
142+
143+
// Try using valid index, range
144+
err = query.AddRange(0, &start, &end)
145+
checkError(err)
146+
147+
err = array.Close()
148+
checkError(err)
149+
}
150+
151+
func getRangeNum() {
152+
ctx, err := tiledb.NewContext(nil)
153+
checkError(err)
154+
155+
// Prepare the array for reading
156+
array, err := tiledb.NewArray(ctx, rangeArrayName)
157+
checkError(err)
158+
err = array.Open(tiledb.TILEDB_READ)
159+
checkError(err)
160+
161+
// Prepare the query
162+
query, err := tiledb.NewQuery(ctx, array)
163+
checkError(err)
164+
165+
// Try using valid index
166+
rangeNum, err := query.GetRangeNum(0)
167+
checkError(err)
168+
169+
fmt.Printf("Number of ranges across dimension 0 is: %d\n", *rangeNum)
170+
171+
err = array.Close()
172+
checkError(err)
173+
}
174+
175+
func getRange() {
176+
ctx, err := tiledb.NewContext(nil)
177+
checkError(err)
178+
179+
// Prepare the array for reading
180+
array, err := tiledb.NewArray(ctx, rangeArrayName)
181+
checkError(err)
182+
err = array.Open(tiledb.TILEDB_READ)
183+
checkError(err)
184+
185+
// Prepare the query
186+
query, err := tiledb.NewQuery(ctx, array)
187+
checkError(err)
188+
189+
// Try using valid dimension index and range index
190+
start, end, err := query.GetRange(0, 0)
191+
checkError(err)
192+
193+
fmt.Printf("Range start for dimension 0, range 0 is: %d\n", start.(int32))
194+
fmt.Printf("Range end for dimension 0, range 0 is: %d\n", end.(int32))
195+
196+
err = array.Close()
197+
checkError(err)
198+
}
199+
200+
// ExampleRange shows an example of creation, writing of a dense array
201+
// and useage of range functions
202+
func ExampleRange() {
203+
createRangeArray()
204+
writeRangeArray()
205+
addRange()
206+
getRangeNum()
207+
getRange()
208+
209+
// Cleanup example so unit tests are clean
210+
if _, err := os.Stat(rangeArrayName); err == nil {
211+
err = os.RemoveAll(rangeArrayName)
212+
checkError(err)
213+
}
214+
215+
// Output: Error adding query range: [TileDB::Subarray] Error: Cannot add range to dimension; Range must be in the domain the subarray is constructed from
216+
// Error adding query range: [TileDB::Subarray] Error: Cannot add range to dimension; Invalid dimension index
217+
// Number of ranges across dimension 0 is: 1
218+
// Range start for dimension 0, range 0 is: 1
219+
// Range end for dimension 0, range 0 is: 4
220+
}

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/TileDB-Inc/TileDB-Go
22

33
require github.com/stretchr/testify v1.3.0
4+
5+
go 1.13

0 commit comments

Comments
 (0)