DeltaQuery.Results (DeltaQuery v0.2.3)

Copy Markdown View Source

In-memory operations on query results.

A %Results{} struct contains a dataframe returned from Query.execute/1. All functions in this module operate on data that has already been fetched.

Filtering

Stage 1: Prefer Query.where/2 for initial filtering** - it's more efficient. Stage 2: Use Results.filter/2 to apply additional filters on already-fetched data.

This module handles stage 2.

Summary

Functions

Aggregate results by grouping on a column and counting occurrences.

Return the number of rows in the results.

Check if results are empty.

Apply additional predicate filters to already-fetched results.

Return the first row as a map, or nil if empty.

Join two result sets on a common column.

Sum a numeric column, returning 0 if the column doesn't exist.

Apply text search to results using case-insensitive substring matching.

Convert results to a list of maps (rows).

Types

join_how()

@type join_how() :: :inner | :left | :right | :outer | :cross

join_opt()

@type join_opt() :: {:on, String.t() | [String.t()]} | {:how, join_how()}

join_opts()

@type join_opts() :: [join_opt()]

t()

@type t() :: %DeltaQuery.Results{
  dataframe: Explorer.DataFrame.t(),
  files_processed: non_neg_integer(),
  total_files: non_neg_integer()
}

Functions

aggregate_by_column(result, column)

@spec aggregate_by_column(t(), atom()) :: [map()]

Aggregate results by grouping on a column and counting occurrences.

Returns a list of maps with the column value and count, sorted by count descending.

Examples

aggregated = Results.aggregate_by_column(results, :book_id)
# => [%{book_id: 1001, count: 5}, %{book_id: 1002, count: 3}]

count(results)

@spec count(t()) :: non_neg_integer()

Return the number of rows in the results.

Examples

results |> Results.count()
# => 42

empty?(results)

@spec empty?(t()) :: boolean()

Check if results are empty.

Examples

results |> Results.empty?()
# => false

filter(result, predicates)

@spec filter(t(), [String.t()]) :: {:ok, t()} | {:error, String.t()}

Apply additional predicate filters to already-fetched results.

Prefer Query.where/2 for initial filtering. This function applies filters in-memory on data that has already been downloaded. Use it only for:

  • Filtering joined results from multiple queries
  • Applying filters determined after initial query execution

Returns {:ok, results} on success or {:error, reason} if any predicate is invalid.

Examples

# Filter joined results
{:ok, books} = Query.new("books") |> Query.execute()
{:ok, publishers} = Query.new("publishers") |> Query.execute()
joined = Results.join(books, publishers, on: "book_id")
{:ok, filtered} = Results.filter(joined, ["page_count > 300"])

first(results)

@spec first(t()) :: map() | nil

Return the first row as a map, or nil if empty.

Examples

results |> Results.first()
# => %{"book_id" => 1001, "title" => "The Great Gatsby"}

join(left, right, opts)

@spec join(t(), t(), join_opts()) :: t()

Join two result sets on a common column.

Options

  • :on - Column name or list of column names to join on (required)
  • :how - Join type: :left (default), :right, :inner, :outer, :cross

Examples

"books"
|> Query.new()
|> Query.execute!()
|> Results.join(publishers_result, on: "book_id")
|> Results.to_rows()

sum(results, column)

@spec sum(t(), String.t()) :: number()

Sum a numeric column, returning 0 if the column doesn't exist.

Examples

results |> Results.sum("amount")
# => 12500.0

text_search(result, search_text, columns)

@spec text_search(t(), String.t(), [String.t()]) :: {:ok, t()} | {:error, String.t()}

Apply text search to results using case-insensitive substring matching.

Searches across the specified columns and returns rows where any column contains the search text.

Examples

{:ok, searched} = Results.text_search(results, "fiction", ["genre", "title", "notes"])

to_rows(results)

@spec to_rows(t()) :: [map()]

Convert results to a list of maps (rows).

Examples

"books"
|> Query.new()
|> Query.execute!()
|> Results.to_rows()