Provider.sql()#

relationalai
#Provider.sql(
    query: str,
    params: list[Any]|None = None,
    format: str = "list",
)

Execute a SQL query and return the results as a list of Snowpark Row objects. Set format to "lazy" to return a SnowPark DataFrame, or "pandas" to return a pandas DataFrame.

Parameters#

NameTypeDescription
querystrThe SQL query to execute.
paramslist[Any]Optional list of binding parameters to pass to the query.
formatstrThe format of the returned DataFrame. Must be one of:

Returns#

A list of Snowpark Row objects, a Snowpark DataFrame, or a pandas DataFrame depending on the format parameter.

Example#

Use .sql() to execute a SQL query and return the results as a Snowpark DataFrame:

#import relationalai as rai

app = rai.Provider()
app.sql(
    query="SELECT * FROM values (?, ?), (?, ?)",
    params=[1, "a", 2, "b"],
)
# [Row(COLUMN1=1, COLUMN2='a'), Row(COLUMN1=2, COLUMN2='b')]

Change the format parameter to change the return type:

## Return a Snowpark DataFrame.
df = app.sql(
    query="SELECT * FROM values (?, ?), (?, ?)",
    params=[1, "a", 2, "b"],
    format="lazy"
)
df.show()
# -------------------------
# |"COLUMN1"  |"COLUMN2"  |
# -------------------------
# |1          |a          |
# |2          |b          |
# -------------------------


# Return a pandas DataFrame.
df = app.sql(
    query="SELECT * FROM values (?, ?), (?, ?)",
    params=[1, "a", 2, "b"],
    format="pandas"
)
print(df)
#    COLUMN1 COLUMN2
# 0        1       a
# 1        2       b