Skip to main content

GPU SQL

Overview​

@luma.gl/graph/gpu-sql provides a bounded SQL frontend for GPUDataFrame. GPUSQLContext registers existing GPU dataframes by name and lowers supported statements into the same immutable GPU dataframe planner. Planning does not upload, submit, allocate query output, or read GPU buffers.

The optional loaders.gl integration lives in the GPU data analysis example. Its planGPUDataFrameQuery() helper consumes the loaders.gl plan directly. Copy the adapter into your application and install @loaders.gl/core and @loaders.gl/sql when needed. The @luma.gl/graph/gpu-sql module exports the native frontend without a loaders.gl dependency.

When to use it​

Use GPU SQL when an application already has GPU dataframe inputs and a constrained SQL surface is more convenient than manually composing expressions, filters, aggregations, ordering, and joins. Use GPU Dataframe directly when an application needs behavior outside the supported grammar.

Use the loaders.gl adapter when a query originates in a loader, catalog, or application component that already emits TableQueryOptions and SQLPredicate values:

import {parseSQLPredicate} from '@loaders.gl/sql';
import {GPUCommandGraph} from '@luma.gl/graph/gpu-core';
import {planGPUDataFrameQuery} from './loaders-sql-query';

const predicate = parseSQLPredicate('fare >= :minimum AND category IN (0, 1)', {
preserveParameters: true
});
const query = planGPUDataFrameQuery(frame, {
predicate,
columns: ['fare', 'category'],
parameters: {minimum: 8}
});
const compiled = query.compile(new GPUCommandGraph(device));

const commandEncoder = device.createCommandEncoder();
compiled.encode(commandEncoder, {minimum: 10});
device.submit(commandEncoder.finish());

The compiled result stays GPU-resident as table, validity, dictionaries, rowIndices, and selectedCounts. Rendering or another command graph can consume those resources directly. Arrow readback remains an explicit adapter operation rather than the query's required result type.

Execution boundary​

The execution boundary is explicit: Arrow input → GPU dataframe execution → GPU consumers. Use makeGPUAnalyticsTableFromArrowTable() to upload input, construct a GPUDataFrame, compile the query into a caller-owned GPUCommandGraph, then encode and submit the graph. Downstream rendering and compute can use the compiled GPU resources directly. Call makeArrowTableFromGPUAnalyticsTable() only when the application explicitly needs CPU readback.

Supported grammar​

SQRT, LN, LOG10, ASIN, and ACOS accept Float32 inputs. Square root requires a nonnegative value, logarithms require a positive value, and inverse sine and cosine accept the interval [-1, 1]. Invalid domains and non-finite inputs produce null in GPU SQL. SIN, COS, TAN, ATAN, and EXP also accept Float32 inputs and return null for non-finite inputs. EXP returns null when its result would overflow Float32. Trigonometric functions use radians. RADIANS and DEGREES convert Float32 angles. Both return null for non-finite inputs, and DEGREES also returns null if the converted magnitude would overflow. ATAN2(y, x) takes two Float32 values and returns the quadrant-aware angle in radians. A null or non-finite argument, or the origin where both arguments are zero, yields null.

MOD(dividend, divisor) takes matching Float32, Int32, or Uint32 inputs. The remainder keeps the dividend's sign. Null arguments, non-finite inputs, and zero divisors yield null. Float32 remainder is computed without materializing a floating-point quotient.

POWER(base, exponent) accepts Float32 arguments, with integral exponents required for negative bases. GPU Float32 overflow and invalid domains yield null. Results use GPU floating-point precision and very small results may round to zero.

TRY(expression[, fallback]) can recover checked row errors from scalar arithmetic, POWER, zero divisors in MOD, and domain-checked unary math such as SQRT and LN. Genuine nulls remain null. An omitted fallback yields null. The fallback must use the same scalar format. Boolean TRY expressions are supported in predicates. As in Arrow SQL, COALESCE and null tests do not catch checked math errors inside TRY. GPU execution evaluates both expressions and chooses the fallback only for tracked errors. Scalar / checks Float32, Int32, and Uint32 division by zero, Int32 overflow, and Float32 overflow from finite inputs. Integer quotients truncate toward zero. A checked division error produces null unless TRY supplies a fallback. Scalar +, -, and * also check Int32 and Uint32 range errors and Float32 overflow from finite inputs. Casts use their own conversion rules.

CAST supports scalar Float32, Int32, and Uint32 results. Float-to-integer casts truncate toward zero and clamp out-of-range finite values to the target limits. Null and non-finite inputs produce null when converting from Float32 to an integer.

COALESCE returns the first valid value from 2–64 scalar numeric arguments. NULLIF returns null when its two valid numeric arguments compare equal. Columns in either function use matching scalar formats.

ABS, FLOOR, CEILING, ROUND, and TRUNC accept one numeric argument and preserve nulls. SIGN preserves the input format and returns -1, 0, or 1 for finite scalar numeric inputs. ROUND rounds halfway values away from zero. ABS supports Float32 and Uint32. The rounding functions also accept Int32 and return integer inputs unchanged. Calls on untyped literals or parameters use Float32, so a comparison with an integer column requires matching formats.

Searched CASE WHEN … THEN … ELSE … END can produce numeric columns. Conditions that are null skip the branch. Numeric column branches must have the same scalar format. An omitted ELSE yields null.

  • SELECT *, named columns, numeric expressions, and explicit AS aliases. Projection-only queries can pass through packed GPU columns and retain their source buffers.

  • One registered FROM table with optional aliases.

  • WHERE comparisons, AND, OR, NOT, parentheses, IS NULL, and IS NOT NULL.

  • IN and NOT IN compare a source column against up to 64 literals or named parameters. A null candidate makes a nonmatching result unknown, which WHERE excludes.

  • Named :parameter values with planning defaults and encoding-time updates.

  • One or more numeric ORDER BY expressions, each with its own direction and null ordering, and an optional bounded LIMIT. Keys are evaluated in priority order. Rows that tie on every key retain their input order. Sort keys can use source fields outside the projection and are excluded from the output schema.

    A one-based position refers to the SELECT output, including computed aliases and expanded *.

  • OFFSET skips selected rows before applying LIMIT. A single-table, nonaggregate query without ORDER BY pages filtered rows in source batch order using a GPU prefix scan. It exposes the page through the usual per-batch selection mask, row indices, and selected counts.

  • COUNT(*), COUNT(column), SUM, MIN, MAX, and AVG, with optional GROUP BY. COUNT(column) counts valid values after filtering, while COUNT(*) counts all selected rows. Without GROUP BY, HAVING filters the single aggregate row. It accepts SELECT aliases and aggregate calls, including calls omitted from SELECT. ORDER BY can refer to an output name, position, or aggregate call. LIMIT and OFFSET apply after HAVING. Read the filtered or paged row using rowIndices and selectedCounts. Numeric aggregate inputs such as SUM(fare * 2) AS total require an explicit output alias. SELECT can also compute expressions over reductions and group keys. For example, MAX(fare) - MIN(fare) AS spread evaluates after the reductions. Computed aggregate outputs require an alias, which HAVING and ORDER BY can reference.

    Refer to a selected key by source name, a matching expression, a one-based SELECT position, or an unqualified alias that does not shadow a source column. GROUP BY without aggregates returns one row per observed key or key tuple, including nulls. It supports HAVING, ORDER BY, LIMIT, and OFFSET. For example, use SELECT fare FROM trips GROUP BY fare ORDER BY fare LIMIT 20 to page through unique fares. GROUP BY keys may be omitted from SELECT. Queries with hidden keys return only groups observed after WHERE. A projected computed key such as FLOOR(fare) AS bucket requires an explicit SELECT alias. Computed keys use observed grouping. HAVING filters grouped results before ordering and pagination. Without ORDER BY, LIMIT and OFFSET page the groups in GPU emission order. It can reference SELECT output names, grouping keys (including exact computed grouping expressions), and aggregate calls. Grouping keys and aggregate calls can be omitted from SELECT. For unordered HAVING results, pass rowIndices and selectedCounts to Arrow readback. Set maxReductionScratchBytes in the query options to cap temporary partial buffers per grouped SUM or AVG. AVG counts both the sum and count buffers. The GPU reuses scratch across key ranges. maxMeanScratchBytes can also constrain the size of an individual AVG partial buffer. Grouped queries accept ORDER BY on SELECT positions or numeric expressions over SELECT output names, grouping keys, and aggregate calls. Grouping expressions and aggregate calls can be omitted from SELECT. Apply LIMIT and OFFSET to the sorted groups. Pass globalRowIndices and globalSelectedCount to Arrow readback for ordered groups. Grouped results preserve SELECT column order and retain key dictionaries under their output names. A projected single dictionary key returns the full category domain. Single numeric keys and multi-key queries return only groups observed after WHERE. Observed grouping supports uint32, sint32, float32, and dictionary-encoded keys, including nulls. Float keys group both signed zeros together and treat all NaNs as a single value. For observed results, pass the compiled query’s rowIndices and selectedCounts to Arrow readback to read the observed rows from the capacity-backed result.

    Default aggregate aliases use the bound field name, such as sum_fare for SUM(trips.fare).

  • One INNER, LEFT, SEMI, or ANTI JOIN over a single equality key. Keys can use UInt32, Int32, or Float32. Mixed Int32 and UInt32 keys match only values in their shared numeric range. Both sides use Float32 for a floating-point join. Float32 joins treat both signs of zero as equal and skip NaN keys. Null keys never match. Int32 supports the full signed range, including -1. Aggregates can consume the selected left rows of a unique-right join directly on the GPU. The join and aggregate execute in one graph. An invalid right key index contributes no rows. The SELECT list controls result order and may omit either join key. INNER JOIN and LEFT JOIN can project direct right-side packed 32-bit numeric columns, including two- to four-component vectors and dictionary codes with adapter metadata. For example, SELECT t.identifier, a.score AS account_score FROM trips AS t LEFT JOIN accounts AS a ON t.identifier = a.identifier. The GPU gathers right payloads into the original left batch layout. For multiple left batches, the join can stage contiguous right payload chunks in GPU scratch, then gather each staged range once per left batch and projected column. Each staged range spans consecutive right chunks that fit in one storage binding. The GPU copies chunks with copy-source usage directly and can stage storage-only chunks with compute passes when the staging and gather steps together use fewer compute passes than direct gathering. The join gathers directly from any remaining source chunks. Set maxJoinProjectionScratchBytes to cap the combined staged payload and validity bytes across projected right columns. The planner considers columns in SELECT order and can stage a partial range when the whole column exceeds the remaining budget. A budget of zero disables staging. The cap applies to projection staging only. Multiple aliases for the same right source column share its gather work, GPU values, validity mask, and staging allocation. The result retains each alias in SELECT order. An unmatched left row yields null for every right field. SELECT expressions can combine left and right numeric inputs and use named parameters. Expressions run after matching, so COALESCE can replace a null from an unmatched right row. WHERE predicates that read right columns run after INNER or LEFT JOIN matching and before aggregation, ordering, or pagination. On a LEFT JOIN, WHERE r.score IS NULL can retain unmatched rows, while WHERE r.score > 0 excludes them. Predicates that read only left columns can still filter before matching. Global and grouped aggregates over INNER and LEFT JOIN can consume direct right columns or scalar expressions over both inputs, including in HAVING and aggregate sort expressions. GROUP BY accepts direct and computed right keys and emits observed groups for those keys. Unmatched rows have null right inputs, so COUNT of a direct right column excludes them. A computed input can explicitly substitute a value with COALESCE. Joined row results support ORDER BY on numeric left and right expressions, output aliases, and SELECT positions. With ORDER BY, use globalRowIndices and globalSelectedCount for ordered Arrow readback. Without ORDER BY, LIMIT and OFFSET page matching left rows in source order. Per-batch join row IDs and validity remain aligned with the final page. Qualify the ON operands when the join sides cannot be determined from their field names.

Compound queries​

UNION ALL combines row-returning SELECT branches, including projections, filters, and unique-right joins. Fields align by SELECT position and take their names from the first branch. Corresponding GPU formats and dictionary codebooks must match. If any branch is nullable, the combined column is nullable.

SELECT id, score FROM current_rows WHERE score > :minimum
UNION ALL
SELECT archive_id, archived_score FROM archived_rows
ORDER BY score DESC, 1 ASC
LIMIT 100 OFFSET 20

Duplicates are preserved. The trailing ORDER BY, LIMIT, and OFFSET apply to the combined result. Without ORDER BY, branches retain their input order. ORDER BY accepts output names and one-based positions and preserves branch and row order across ties.

The CompiledGPUDataFrameUnionAll result keeps the branch batches on the GPU. Read globalRowIndices and globalSelectedCount for ordered results, or use rowIndices and selectedCounts in source order. Aggregate and grouped UNION ALL branches are outside the current GPU route.

SQL keywords and registered table names are case-insensitive. Column names must match the dataframe schema exactly, even when unquoted. Double-quote reserved names such as "limit" and "order". A quoted identifier such as "fare.total" names a single field. In trips."fare.total", trips qualifies the field. The period inside the quotes remains part of its name.

Unsupported statements, unknown columns, unregistered tables, unsupported strings, ambiguous expressions and pagination on unsupported query routes fail during CPU-only planning. Applications still own command submission, synchronization, result readback, and cleanup.

The loaders.gl adapter currently supports predicate pushdown and output projection. It accepts finite numeric, Boolean, and null predicate values, including reusable named parameters. Portable source-order limit, streaming, in-flight cancellation, string/binary/date values, and relational extensions are reported or rejected as unsupported instead of silently falling back to CPU work.

Inspecting a plan​

Call context.query(sql).explain() to inspect the bound execution route, projected and working columns, key count, join type, and pagination before compiling. The frozen description is generated during CPU planning and allocates no GPU buffers.

Benchmark right-side projection​

Run the opt-in WebGPU benchmark with fixed row counts and varying right batch counts:

yarn test-browser-benchmarks modules/graph/test/gpu-sql/gpu-join-projection-benchmark.spec.ts

The benchmark checks result parity and records projection compute passes, copy passes, and staging bytes. It separately reports graph compilation, command encoding, and the wall time from encoding to GPU completion. Separate GPU timestamp runs measure the graph's compute passes when the device exposes timestamp queries. Native copies contribute to wall time. Arrow readback runs outside the timed samples.