Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Query

class Query

Used to execute a SQL query against table(s) in Redivis, using the Redivis SQL query syntax, and read out the results.

Queries are lazy: constructing a query doesn't run it. The query is run the first time its results or metadata are needed (e.g., when calling query.toRowsArray() or query.get()), and only once, however many times its results are then read. Query results can be read for up to 24 hours after the query completes.

Constructors

Method
Description

redivis.query(query[, options])

Construct a SQL query within the current default scope (either a dataset or workflow). If no default scope is specified, all tables in the query must be fully qualified. Consult the referencing resources documentation to learn more.

Construct a SQL query scoped to a specific dataset. Tables referenced by the query do not need to be fully qualified, since the table lookup is already scoped to the dataset.

Construct a SQL query scoped to a specific workflow. Tables referenced by the query do not need to be fully qualified, since the table lookup is already scoped to the workflow.

Examples

// Execute any SQL query and read the results
const query = redivis.query("SELECT 1 + 1 AS two, 'foo' AS bar");
await query.toRowsArray();
// [{ two: 2, bar: 'foo' }]

// The query can reference any table on Redivis
const irisQuery = redivis.query(`
  SELECT *
  FROM demo.iris_species.iris
  WHERE SepalLengthCm > 5
`);
await irisQuery.toRowsArray();
// [{ Id: 33, SepalLengthCm: 5.2, SepalWidthCm: 4.1, PetalLengthCm: 1.5, PetalWidthCm: 0.1, Species: 'Iris-setosa' }, ...]

// Other methods to read data:
// await query.toArrowTable()
// query.toArrowBatchIterator()
// await query.toReadStreams()
// To simplify table references, execute a query scoped to a dataset or workflow
const dataset = redivis.organization('demo').dataset('cms_2014_medicare_data');
const query = dataset.query(`
  SELECT
    hospice_providers.name,
    inpatient_charges.drg_definition
  -- The tables inpatient_charges and hospice_providers are assumed to be
  -- within the scoped dataset
  FROM inpatient_charges
  INNER JOIN hospice_providers
    ON hospice_providers.provider_id = inpatient_charges.provider_id
`);

Attributes

Name
Description

properties

An object containing the API resource representation of the query. This will be populated once the query has been run, and can be refreshed by calling query.get().

Methods

Method
Description

Query.download([path, options])

Export the query's results in a particular format and download them to disk (Node.js only).

Return a File within query results that contain a file_id variable.

Run the query (if it hasn't been run yet), and fetch its metadata. Once called, the properties attribute on the query will be populated.

Query.listFiles([maxResults, options])

Return a list of File instances for query results containing a file_id variable.

Query.listVariables([maxResults])

Return a list of Variable instances associated with the query's output.

Query.to*([maxResults, options])

Various methods to read the query's results. Mirrors the various Table.to* methods (e.g., query.toRowsArray()).

Return a Variable within the query's output.

Was this helpful?