SQL-GPT Contexts

class gpudb.GPUdbSqlContext(name: str, tables: List[GPUdbTableClause], samples: GPUdbSamplesClause)[source]

Represents a collection of clauses that comprise a SQL-GPT context.

Example:

sql_context = GPUdbSqlContext(
    name = "sa_quickstart.nyc_ctx",
    tables = [table_ctx],
    samples = samples_ctx)
create_context_sql = sql_context.build_sql()
db.execute(create_context_sql)

Parameters

name (str) –

Fully qualified name of the context. (e.g. “schema.context”)

tables (list[GPUdbTableClause]) –

A list of GPUdbTableClause objects representing the Kinetica tables (and metadata) referenced in this context.

samples (GPUdbSamplesClause) –

A GPUdbSamplesClause object representing query/answer pairs referenced in this context.

build_sql() str[source]

Format this GPUdbSqlContext as a SQL CREATE CONTEXT statement. The result can be passed to GPUDB.execute() to create the context.

Returns (str):

The SQL-formatted CREATE CONTEXT statement.

class gpudb.GPUdbTableClause(table: str, comment: str | None = None, rules: List[str] | None = None, col_comments: Dict[str, str] | None = None)[source]

A GPUdbTableClause represents the table clause of a SQL-GPT context and can convert its contents to the SQL form expected by a CREATE CONTEXT statement.

Example:

table_ctx = GPUdbTableClause(
    table = "sa_quickstart.nyct2020",
    comment = "This table contains spatial boundaries and attributes of the New York City.",
    col_comments = dict(
        gid = "This is the unique identifier for each record in the table.",
        geom = "The spatial boundary in WKT format of each NTA neighborhood.",
        BoroCode = "The code of the borough to which the neighborhood belongs to."),
    rules = ["Join this table using STXY_WITHIN() = 1",
            "Another rule here"])

Parameters

table (str) –

Fully qualified table name (e.g. “schema.table”)

comment (str) –

Comment for the table.

rules (list[str]) –

A list of rules that apply to the table.

col_comments (dict[str,str]) –

A dictionary with mapping of column names to column comments.

format_sql() str[source]

Format the clause as a SQL string.

class gpudb.GPUdbSamplesClause(samples: List[Tuple[str, str]] | None = None)[source]

A GPUdbSamplesClause represents the samples clause of a SQL-GPT context and can convert its contents to the SQL form expected by a CREATE CONTEXT statement.

Example:

samples_ctx = GPUdbSamplesClause(samples = [
    ("What are the shortest, average, and longest trip lengths for each taxi vendor?",
    """
    SELECT
        th.vendor_id,
        MIN(th.trip_distance) AS shortest_trip_length,
        AVG(th.h.trip_distance) AS average_trip_length,
        MAX(th.trip_distance) AS longest_trip_length
    FROM sa_quickstart.taxi_data_historical AS th
    GROUP BY th.vendor_id;
    """),

    ("How many trips did each taxi vendor make to JFK International Airport?",
    """
    SELECT
        th.vendor_id,
        COUNT(*) AS trip_count
    FROM sa_quickstart.taxi_data_historical AS th
    JOIN sa_quickstart.nyct2020 AS n_dropoff ON KI_FN.STXY_WITHIN(th.dropoff_longitude, th.dropoff_latitude, n_dropoff.geom)
    AND n_dropoff.NTAName = 'John F. Kennedy International Airport'
    GROUP BY th.vendor_id;
    """),
    ])

Parameters

samples (list[tuple[str,str]]) –

A list of tuples containing question/sql pairs.

format_sql() str[source]

Format the clause as a SQL string.