Skip to main content

SQL-GPT Contexts

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

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

build_sql() str

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)

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

format_sql() str

Format the clause as a SQL string.

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

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

format_sql() str

Format the clause as a SQL string.