-
Notifications
You must be signed in to change notification settings - Fork 18
/
Query.R
592 lines (551 loc) · 25.8 KB
/
Query.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# MIT License
#
# Copyright (c) 2017-2022 TileDB Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#' An S4 class for a TileDB Query object
#'
#' @slot ptr An external pointer to the underlying implementation
#' @exportClass tiledb_query
setClass("tiledb_query",
slots = list(ptr = "externalptr"))
## arr = "ANY"))
## could add arr of type 'ANY' (using shortcut to not have to deal with collate order)
## if array was needed for query object
#' Creates a 'tiledb_query' object
#'
#' @param array A TileDB Array object
#' @param type A character value that must be one of 'READ', 'WRITE', or
#' 'DELETE' (for TileDB >= 2.12.0)
#' @param ctx (optional) A TileDB Ctx object
#' @return 'tiledb_query' object
#' @export tiledb_query
tiledb_query <- function(array,
type = if (tiledb_version(TRUE) >= "2.12.0")
c("READ", "WRITE", "DELETE", "MODIFY_EXCLUSIVE")
else
c("READ", "WRITE"),
ctx = tiledb_get_context()) {
stopifnot(`Argument 'arr' must be a tiledb_array object` = .isArray(array))
type <- match.arg(type)
array <- tiledb_array_open(array, type)
ptr <- libtiledb_query(ctx@ptr, array@ptr, type)
query <- new("tiledb_query", ptr = ptr)
invisible(query)
}
#' Return TileDB Query type
#'
#' @param query A TileDB Query object
#' @return A character value, either 'READ' or 'WRITE'
#' @export
tiledb_query_type <- function(query) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"))
libtiledb_query_type(query@ptr)
}
#' Set TileDB Query layout
#'
#' @param query A TileDB Query object
#' @param layout A character variable with the layout; must be one of
#' "COL_MAJOR", "ROW_MAJOR", "GLOBAL_ORDER", "UNORDERED")
#' @return The modified query object, invisibly
#' @export
tiledb_query_set_layout <- function(query, layout=c("COL_MAJOR", "ROW_MAJOR",
"GLOBAL_ORDER", "UNORDERED")) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"))
layout <- match.arg(layout)
libtiledb_query_set_layout(query@ptr, layout)
invisible(query)
}
#' Get TileDB Query layout
#'
#' @param query A TileDB Query object
#' @return The TileDB Query layout as a string
#' @export
tiledb_query_get_layout <- function(query) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"))
libtiledb_query_layout(query@ptr)
}
#' Set subarray for TileDB Query object
#'
#' @param query A TileDB Query object
#' @param subarray A subarry vector object
#' @param type An optional type as a character, if missing type is
#' inferred from the vector.
#' @return The modified query object, invisibly
#' @export
tiledb_query_set_subarray <- function(query, subarray, type) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"))
if (missing(type)) {
libtiledb_query_set_subarray(query@ptr, subarray)
} else {
libtiledb_query_set_subarray_with_type(query@ptr, subarray, type)
}
invisible(query)
}
#' Set TileDB Query buffer
#'
#' This function allocates query buffers directly from R vectors in
#' case the types match: `integer`, `double`, `logical`. For more
#' general types see \code{tiledb_query_buffer_alloc_ptr} and
#' \code{tiledb_query_buffer_assign_ptr}
#' @param query A TileDB Query object
#' @param attr A character value containing the attribute
#' @param buffer A vector providing the query buffer
#' @return The modified query object, invisisibly
#' @export
tiledb_query_set_buffer <- function(query, attr, buffer) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"),
`Argument 'attr' must be character_variable` = is.character(attr),
`Argument 'buffer' must be integer, numeric or logical` = is.numeric(buffer) || is.logical(buffer))
if (is.numeric(buffer) || tiledb_version(TRUE) < "2.10.0") {
libtiledb_query_set_buffer(query@ptr, attr, buffer)
} else { # logical now maps to BOOL which is a uint8_t, we need a different approach
nr <- NROW(buffer)
bufptr <- libtiledb_query_buffer_alloc_ptr("BOOL", nr, FALSE, 1)
bufptr <- libtiledb_query_buffer_assign_ptr(bufptr, "BOOL", buffer, FALSE)
query@ptr <- libtiledb_query_set_buffer_ptr(query@ptr, attr, bufptr)
}
invisible(query)
}
#' Allocate and populate a Query buffer for writing the given char vector
#'
#' @param query A TileDB Query object
#' @param varvec A vector of strings
#' @return An external pointer to the allocated buffer object
#' @export
tiledb_query_create_buffer_ptr_char <- function(query, varvec) {
stopifnot("Argument 'query' must be a tiledb_query object" = is(query, "tiledb_query"),
"Argument 'varvec' must be a character vector" = is.vector(varvec) && is.character(varvec))
bufptr <- libtiledb_query_buffer_var_char_create(varvec, TRUE)
bufptr
}
#' Allocate a Query buffer for reading a character attribute
#'
#' @param sizeoffsets A numeric value with the size of the offsets vector
#' @param sizedata A numeric value of the size of the data string
#' @param nullable An optional boolean indicating whether the column can have NULLs
#' @return An external pointer to the allocated buffer object
#' @export
tiledb_query_alloc_buffer_ptr_char <- function(sizeoffsets, sizedata, nullable=FALSE) {
stopifnot(`Argument 'sizeoffset' must be numeric` = is.numeric(sizeoffsets),
`Argument 'sizedata' must be numeric` = is.numeric(sizedata))
bufptr <- libtiledb_query_buffer_var_char_alloc_direct(sizeoffsets, sizedata, nullable)
bufptr
}
# ' Allocate a Query buffer for reading a character attribute using a subarray
# '
# ' Note that this uses an API part that may be deprecated in the future.
# ' @param array A TileDB Array object
# ' @param attr A character value containing the attribute
# ' @param subarray A vector of length four describing the subarray required for dense arrays
# ' @param sizeoffsets An optional value of the size of the offsets vector
# ' @param sizedata An optional value of the size of the data string
# ' @return An external pointer to the allocated buffer object
# ' @export
## tiledb_query_alloc_buffer_ptr_char_subarray <- function(array, attr, subarray=NULL,
## sizeoffsets=0, sizedata=0) {
## stopifnot(array_ptr=is(array@ptr, "externalptr"),
## is_vector=is.vector(subarray),
## attribute_string=is.character(attr))
## bufptr <- libtiledb_query_buffer_var_char_alloc(array@ptr, subarray, attr, sizeoffsets, sizedata)
## bufptr
## }
#' Assign a buffer to a Query attribute
#'
#' @param query A TileDB Query object
#' @param attr A character value containing the attribute
#' @param bufptr An external pointer with a query buffer
#' @return The modified query object, invisibly
#' @export
tiledb_query_set_buffer_ptr_char <- function(query, attr, bufptr) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"),
`Argument 'attr' must be a character object` = is.character(attr),
`Argument 'bufptr' must be an external pointer` = is(bufptr, "externalptr"))
libtiledb_query_set_buffer_var_char(query@ptr, attr, bufptr)
invisible(query)
}
#' Allocate a Query buffer for a given type
#'
#' This function allocates a query buffer for the given data type.
#' @param query A TileDB Query object
#' @param datatype A character value containing the data type
#' @param ncells A number of elements (not bytes)
#' @return An external pointer to the allocated buffer object
#' @export
tiledb_query_buffer_alloc_ptr <- function(query, datatype, ncells) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"),
`Argument 'datatype' must be a character object` = is.character(datatype),
`Argument 'ncells' must be numeric` = is.numeric(ncells))
bufptr <- libtiledb_query_buffer_alloc_ptr(datatype, ncells)
bufptr
}
#' Allocate and populate a Query buffer for a given object of a given data type.
#'
#' This function allocates a query buffer for the given data object of the given
#' type and assigns the object content to the buffer.
#' @param query A TileDB Query object
#' @param datatype A character value containing the data type
#' @param object A vector object of the given type
#' @return An external pointer to the allocated buffer object
#' @export
tiledb_query_create_buffer_ptr <- function(query, datatype, object) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"),
#`Argument 'object' must be a vector` = is.vector(object),
`Argument 'datatype' must be a character object` = is.character(datatype))
ncells <- length(object)
bufptr <- libtiledb_query_buffer_alloc_ptr(datatype, ncells)
bufptr <- libtiledb_query_buffer_assign_ptr(bufptr, datatype, object)
bufptr
}
#' Assigns to a Query buffer for a given attribute
#'
#' This function assigns a given query buffer to a query.
#' @param query A TileDB Query object
#' @param attr A character value containing the attribute
#' @param bufptr An external pointer with a query buffer
#' @return The modified query object, invisibly
#' @export
tiledb_query_set_buffer_ptr <- function(query, attr, bufptr) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"),
`Argument 'attr' must be a character object` = is.character(attr),
`Argument 'bufptr' must be an external pointer` = is(bufptr, "externalptr"))
libtiledb_query_set_buffer_ptr(query@ptr, attr, bufptr)
invisible(query)
}
#' Retrieve content from a Query buffer
#'
#' This function uses a query buffer and returns its content.
#' @param bufptr An external pointer with a query buffer
#' @return An R object as resulting from the query
#' @export
tiledb_query_get_buffer_ptr <- function(bufptr) {
stopifnot(`Argument 'bufptr' must be an external pointer` = is(bufptr, "externalptr"))
libtiledb_query_get_buffer_ptr(bufptr)
}
#' Retrieve content from a Query character buffer
#'
#' This function uses a query buffer for a character attribute
#' or dimension and returns its content.
#' @param bufptr An external pointer with a query buffer
#' @param sizeoffsets An optional argument for the length of the internal offsets vector
#' @param sizestring An optional argument for the length of the internal string
#' @return An R object as resulting from the query
#' @export
tiledb_query_get_buffer_char <- function(bufptr, sizeoffsets=0, sizestring=0) {
stopifnot(`Argument 'bufptr' must be an external pointer` = is(bufptr, "externalptr"))
libtiledb_query_get_buffer_var_char(bufptr, sizeoffsets, sizestring)
}
#' Submit TileDB Query
#'
#' Note that the query object may need to be finalized
#' via \code{tiledb_query_finalize}.
#' @param query A TileDB Query object
#' @return The modified query object, invisibly
#' @export
tiledb_query_submit <- function(query) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"))
libtiledb_query_submit(query@ptr)
invisible(query)
}
#' Submit TileDB Query asynchronously without a callback returning immediately
#'
#' Note that the query object may need to be finalized
#' via \code{tiledb_query_finalize}.
#' @param query A TileDB Query object
#' @return The modified query object, invisibly
#' @export
tiledb_query_submit_async <- function(query) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"))
libtiledb_query_submit_async(query@ptr)
invisible(query)
}
#' Finalize TileDB Query
#'
#' @param query A TileDB Query object
#' @return A character value, either 'READ' or 'WRITE'
#' @export
tiledb_query_finalize <- function(query) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"))
libtiledb_query_finalize(query@ptr)
.pkgenv[["query_status"]] <- libtiledb_query_status(query@ptr)
invisible(query)
}
#' Get TileDB Query status
#'
#' @param query A TileDB Query object
#' @return A character value describing the query status
#' @export
tiledb_query_status <- function(query) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"))
libtiledb_query_status(query@ptr)
}
#' Get TileDB Query result buffer element size
#'
#' The underlying library functions returns a pair of values as a vector
#' of length two. The first number is the number of element offsets for variable
#' size attributes (and always zero for fixed-sized attributes and coordinates).
#' The second is the number of elements in the data buffer. For variable-sized
#' attributes the first number is the number of cells read (and hence the number
#' of offsets), the second number is the number of elements in the data buffer.
#'
#' As this function was first made available when only a scalar (corresponding to
#' the second result) was returned, we still return that value.
#'
#' @param query A TileDB Query object
#' @param attr A character value containing the attribute
#' @return A integer with the number of elements in the results buffer
#' for the given attribute
#' @seealso tiledb_query_result_buffer_elements_vec
#' @export
tiledb_query_result_buffer_elements <- function(query, attr) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"),
`Argument 'attr' must be a character object` = is.character(attr))
libtiledb_query_result_buffer_elements(query@ptr, attr, 1) # request 2nd el in pair
}
#' Get TileDB Query result buffer element size pair as vector
#'
#' The underlying library functions returns a pair of values as a vector
#' of length two. The first number is the number of element offsets for variable
#' size attributes (and always zero for fixed-sized attributes and coordinates).
#' The second is the number of elements in the data buffer. For variable-sized
#' attributes the first number is the number of cells read (and hence the number
#' of offsets), the second number is the number of elements in the data buffer.
#' In the case of a nullable attribute, a third element is returned with the size of
#' the validity buffer.
#'
#' @param query A TileDB Query object
#' @param attr A character value containing the attribute
#' @param nullable A logical variable that is \sQuote{TRUE} to signal that the attribute
#' is nullable, and \sQuote{FALSE} otherwise
#' @return A vector with the number of elements in the offsets buffer (and zero
#' for fixed-size attribute or dimensions), the number elements in the results
#' buffer for the given attribute, and (if nullable) a third element with the validity
#' buffer size.
#' @seealso tiledb_query_result_buffer_elements
#' @export
tiledb_query_result_buffer_elements_vec <- function(query, attr, nullable = FALSE) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"),
`Argument 'attr' must be a character object` = is.character(attr),
`Argument 'nullable' must be a logical` = is.logical(nullable))
libtiledb_query_result_buffer_elements_vec(query@ptr, attr, nullable)
}
#' Set a range for a given query
#'
#' @param query A TileDB Query object
#' @param schema A TileDB Schema object
#' @param attr An character variable with a dimension name for which the range is set
#' @param lowval The lower value of the range to be set
#' @param highval The higher value of the range to be set
#' @param stride An optional stride value for the range to be set
#' @return The query object, invisibly
#' @export
tiledb_query_add_range <- function(query, schema, attr, lowval, highval, stride=NULL) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"),
`Argument 'schema' must be a tiledb_array_schema object` = is(schema, "tiledb_array_schema"),
`Argument 'attr' must be a character object` = is.character(attr),
`Argument 'lowval' must be numeric` = is.numeric(lowval),
`Argument 'highval' must be numeric` = is.numeric(highval),
`Argument 'stride' must be numeric (or NULL)` = is.null(stride) || is.numeric(lowval))
names <- tiledb_schema_get_names(schema)
types <- tiledb_schema_get_types(schema)
idx <- which(names == attr)
query <- tiledb_query_add_range_with_type(query, idx-1L, types[idx], lowval, highval, stride)
invisible(query)
}
#' Set a range for a given query, also supplying type
#'
#' @param query A TileDB Query object
#' @param idx An integer index, zero based, of the dimensions
#' @param datatype A character value containing the data type
#' @param lowval The lower value of the range to be set
#' @param highval The highre value of the range to be set
#' @param stride An optional stride value for the range to be set
#' @return The query object, invisibly
#' @export
tiledb_query_add_range_with_type <- function(query, idx, datatype, lowval, highval, stride=NULL) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"),
`Argument 'idx' must be integer` = is.integer(idx),
`Argument 'datatype' must be character` = is.character(datatype),
`Argument 'lowval' must be numeric` = is.numeric(lowval),
`Argument 'highval' must be numeric` = is.numeric(highval),
`Argument 'stride' must be numeric (or NULL)` = is.null(stride) || is.numeric(lowval))
query@ptr <- libtiledb_query_add_range_with_type(query@ptr, idx, datatype, lowval, highval, stride)
invisible(query)
}
#' Retrieve the Number of Fragments for Query
#'
#' This function is only applicable to \sQuote{WRITE} queries.
#' @param query A TileDB Query object
#' @return An integer with the number of fragments for the given query
#' @export
tiledb_query_get_fragment_num <- function(query) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"))
libtiledb_query_get_fragment_num(query@ptr)
}
#' Retrieve the URI for a given Query Fragment
#'
#' This function is only applicable to \sQuote{WRITE} queries.
#' @param query A TileDB Query object
#' @param idx An integer (or numeric) index ranging from zero to the number of fragments minus 1
#' @return An character value with the fragment URI
#' @export
tiledb_query_get_fragment_uri <- function(query, idx) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"),
`Argument 'idx' must be numeric` = is.numeric(idx))
libtiledb_query_get_fragment_uri(query@ptr, idx)
}
#' Retrieve the timestamp range for a given Query Fragment
#'
#' This function is only applicable to \sQuote{WRITE} queries. The time resolution in
#' TileDB is millseconds since the epoch so an R \code{Datetime} vector is returned.
#' @param query A TileDB Query object
#' @param idx An integer (or numeric) index ranging from zero to the number of fragments minus 1
#' @return A two-element datetime vector with the start and end time of the fragment write.
#' @export
tiledb_query_get_fragment_timestamp_range <- function(query, idx) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"),
`Argument 'idx' must be numeric` = is.numeric(idx))
libtiledb_query_get_fragment_timestamp_range(query@ptr, idx)
}
#' Retrieve the estimated result size for a query and attribute
#'
#' When reading from sparse arrays, one cannot know beforehand how big the
#' result will be (unless one actually executes the query). This function
#' offers a way to get the estimated result size for the given attribute.
#' As TileDB does not actually execute the query, getting the estimated
#' result is very fast.
#' @param query A TileDB Query object
#' @param name A variable with an attribute name
#' @return An estimate of the query result size
#' @export
tiledb_query_get_est_result_size <- function(query, name) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"),
`Argument 'name' must be character` = is.character(name))
libtiledb_query_get_est_result_size(query@ptr, name)
}
#' Retrieve the estimated result size for a query and variable-sized attribute
#'
#' When reading variable-length attributes from either dense or sparse arrays,
#' one cannot know beforehand how big the result will be (unless one actually
#' executes the query). This function offers a way to get the estimated result
#' size for the given attribute. As TileDB does not actually execute the query,
#' getting the estimated result is very fast.
#' @param query A TileDB Query object
#' @param name A variable with an attribute name
#' @return An estimate of the query result size
#' @export
tiledb_query_get_est_result_size_var <- function(query, name) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"),
`Argument 'name' must be character` = is.character(name))
libtiledb_query_get_est_result_size_var(query@ptr, name)
}
#' Retrieve the number of ranges for a query dimension
#'
#' @param query A TileDB Query object
#' @param idx An integer or numeric index selecting the dimension
#' @return An integer with the number of query range for the
#' given dimensions
#' @export
tiledb_query_get_range_num <- function(query, idx) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"),
`Argument 'idx' must be numeric` = is.numeric(idx))
libtiledb_query_get_range_num(query@ptr, idx-1)
}
#' Retrieve the query range for a query dimension and range index
#'
#' @param query A TileDB Query object
#' @param dimidx An integer or numeric index selecting the dimension
#' @param rngidx An integer or numeric index selection the given range for the dimension
#' @return An integer vector with elements start, end and stride for the query
#' range for the given dimension and range index
#' @export
tiledb_query_get_range <- function(query, dimidx, rngidx) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"),
`Argument 'dimidx' must be numeric` = is.numeric(dimidx),
`Argument 'rngidx' must be numeric` = is.numeric(rngidx))
libtiledb_query_get_range(query@ptr, dimidx-1, rngidx-1)
}
#' Retrieve the query range for a variable-sized query dimension and range index
#'
#' @param query A TileDB Query object
#' @param dimidx An integer index selecting the dimension
#' @param rngidx An integer index selection the given range for the dimension
#' @return An string vector with elements start and end for the query
#' range for the given dimension and range index
#' @export
tiledb_query_get_range <- function(query, dimidx, rngidx) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"),
`Argument 'dimidx' must be numeric` = is.numeric(dimidx),
`Argument 'rngidx' must be numeric` = is.numeric(rngidx))
libtiledb_query_get_range_var(query@ptr, dimidx-1, rngidx-1)
}
#' Set a query combination object for a query
#'
#' @param query A TileDB Query object
#' @param qc A TileDB Query Combination object
#' @return The modified query object, invisibly
#' @export
tiledb_query_set_condition <- function(query, qc) {
stopifnot(`Argument 'query' must be a tiledb_query object` = is(query, "tiledb_query"),
`Argument 'qc' must be a query_condition object` = is(qc, "tiledb_query_condition"))
query@ptr <- libtiledb_query_set_condition(query@ptr, qc@ptr)
invisible(query)
}
#' Retrieve the cached status of the last finalized query
#'
#' This function accesses the status of the last query without
#' requiring the query object.
#'
#' @return The status of the last query
#' @export
tiledb_get_query_status <- function() {
.pkgenv[["query_status"]]
}
#' Return query statistics as a JSON string
#'
#' @param query A TileDB Query object
#' @return A JSON-formatted string with context statistics
#' @export
tiledb_query_stats <- function(query) {
stopifnot(`The 'query' argument must be a TileDB Query object` = is(query, "tiledb_query"),
`TileDB 2.4.0 or later is required` = tiledb_version(TRUE) >= "2.4.0")
libtiledb_query_stats(query@ptr)
}
#' Return query context object
#'
#' @param query A TileDB Query object
#' @return A TileDB Context object retrieved from the query
#' @export
tiledb_query_ctx <- function(query) {
stopifnot(`The 'query' argument must be a TileDB Query object` = is(query, "tiledb_query"),
`TileDB 2.6.0 or later is required` = tiledb_version(TRUE) >= "2.6.0")
new("tiledb_ctx", ptr = libtiledb_query_get_ctx(query@ptr))
}
## The next function could be used to extract an 'tiledb_array' object from a query object
## if we added it, but the only use so far as been in one test so there was not a strong enough
## case to extend the tiledb_query object with
# ' Return query array object
# '
# ' @param query A TileDB Query object
# ' @return A TileDB Array object retrieved from the query
# ' @ export
#tiledb_query_array <- function(query) {
# stopifnot(`The 'query' must be a TileDB Query object` = is(query, "tiledb_query"))
# query@arr
#}