Optional
frames: Record<string, pl.DataFrame<any> | LazyDataFrame<any>>A valid string SQL query.
Apply the query eagerly, returning DataFrame
instead of LazyFrame
.
If unset, the value of the init-time parameter "eager_execution" will be
used. (Note that the query itself is always executed in lazy-mode; this
parameter only impacts the type of the returned frame).
Declare frame data and register with a SQLContext:
const df = pl.DataFrame({
data: [
("The Godfather", 1972, 6_000_000, 134_821_952, 9.2),
("The Dark Knight", 2008, 185_000_000, 533_316_061, 9.0),
("Schindler's List", 1993, 22_000_000, 96_067_179, 8.9),
("Pulp Fiction", 1994, 8_000_000, 107_930_000, 8.9),
("The Shawshank Redemption", 1994, 25_000_000, 28_341_469, 9.3),
],
schema: ["title", "release_year", "budget", "gross", "imdb_score"],
});
const ctx = pl.SQLContext({ films: df });
Execute a SQL query against the registered frame data:
const result = ctx.execute(`
SELECT title, release_year, imdb_score
FROM films
WHERE release_year > 1990
ORDER BY imdb_score DESC
`, { eager: true });
console.log(result);
// shape: (4, 3)
// ┌──────────────────────────┬──────────────┬────────────┐
// │ title ┆ release_year ┆ imdb_score │
// │ --- ┆ --- ┆ --- │
// ╞══════════════════════════╪══════════════╪════════════╡
// │ The Shawshank Redemption ┆ 1994 ┆ 9.3 │
// │ The Dark Knight ┆ 2008 ┆ 9.0 │
// │ Schindler's List ┆ 1993 ┆ 8.9 │
// │ Pulp Fiction ┆ 1994 ┆ 8.9 │
// └──────────────────────────┴──────────────┴────────────┘
Execute a GROUP BY query:
ctx.execute(`
SELECT
MAX(release_year / 10) * 10 AS decade,
SUM(gross) AS total_gross,
COUNT(title) AS n_films,
FROM films
GROUP BY (release_year / 10) -- decade
ORDER BY total_gross DESC
`, { eager: true });
// shape: (3, 3)
// ┌────────┬─────────────┬─────────┐
// │ decade ┆ total_gross ┆ n_films │
// │ --- ┆ --- ┆ --- │
// ╞════════╪═════════════╪═════════╡
// │ 2000 ┆ 533316061 ┆ 1 │
// │ 1990 ┆ 232338648 ┆ 3 │
// │ 1970 ┆ 134821952 ┆ 1 │
// └────────┴─────────────┴─────────┘
Parse the given SQL query and execute it against the registered frame data.
A valid string SQL query.
Declare frame data and register with a SQLContext:
const df = pl.DataFrame({
data: [
("The Godfather", 1972, 6_000_000, 134_821_952, 9.2),
("The Dark Knight", 2008, 185_000_000, 533_316_061, 9.0),
("Schindler's List", 1993, 22_000_000, 96_067_179, 8.9),
("Pulp Fiction", 1994, 8_000_000, 107_930_000, 8.9),
("The Shawshank Redemption", 1994, 25_000_000, 28_341_469, 9.3),
],
schema: ["title", "release_year", "budget", "gross", "imdb_score"],
});
const ctx = pl.SQLContext({ films: df });
Execute a SQL query against the registered frame data:
const result = ctx.execute(`
SELECT title, release_year, imdb_score
FROM films
WHERE release_year > 1990
ORDER BY imdb_score DESC
`, { eager: true });
console.log(result);
// shape: (4, 3)
// ┌──────────────────────────┬──────────────┬────────────┐
// │ title ┆ release_year ┆ imdb_score │
// │ --- ┆ --- ┆ --- │
// ╞══════════════════════════╪══════════════╪════════════╡
// │ The Shawshank Redemption ┆ 1994 ┆ 9.3 │
// │ The Dark Knight ┆ 2008 ┆ 9.0 │
// │ Schindler's List ┆ 1993 ┆ 8.9 │
// │ Pulp Fiction ┆ 1994 ┆ 8.9 │
// └──────────────────────────┴──────────────┴────────────┘
Execute a GROUP BY query:
ctx.execute(`
SELECT
MAX(release_year / 10) * 10 AS decade,
SUM(gross) AS total_gross,
COUNT(title) AS n_films,
FROM films
GROUP BY (release_year / 10) -- decade
ORDER BY total_gross DESC
`, { eager: true });
// shape: (3, 3)
// ┌────────┬─────────────┬─────────┐
// │ decade ┆ total_gross ┆ n_films │
// │ --- ┆ --- ┆ --- │
// ╞════════╪═════════════╪═════════╡
// │ 2000 ┆ 533316061 ┆ 1 │
// │ 1990 ┆ 232338648 ┆ 3 │
// │ 1970 ┆ 134821952 ┆ 1 │
// └────────┴─────────────┴─────────┘
Parse the given SQL query and execute it against the registered frame data.
A valid string SQL query.
Apply the query eagerly, returning DataFrame
instead of LazyFrame
.
If unset, the value of the init-time parameter "eager_execution" will be
used. (Note that the query itself is always executed in lazy-mode; this
parameter only impacts the type of the returned frame).
Declare frame data and register with a SQLContext:
const df = pl.DataFrame({
data: [
("The Godfather", 1972, 6_000_000, 134_821_952, 9.2),
("The Dark Knight", 2008, 185_000_000, 533_316_061, 9.0),
("Schindler's List", 1993, 22_000_000, 96_067_179, 8.9),
("Pulp Fiction", 1994, 8_000_000, 107_930_000, 8.9),
("The Shawshank Redemption", 1994, 25_000_000, 28_341_469, 9.3),
],
schema: ["title", "release_year", "budget", "gross", "imdb_score"],
});
const ctx = pl.SQLContext({ films: df });
Execute a SQL query against the registered frame data:
const result = ctx.execute(`
SELECT title, release_year, imdb_score
FROM films
WHERE release_year > 1990
ORDER BY imdb_score DESC
`, { eager: true });
console.log(result);
// shape: (4, 3)
// ┌──────────────────────────┬──────────────┬────────────┐
// │ title ┆ release_year ┆ imdb_score │
// │ --- ┆ --- ┆ --- │
// ╞══════════════════════════╪══════════════╪════════════╡
// │ The Shawshank Redemption ┆ 1994 ┆ 9.3 │
// │ The Dark Knight ┆ 2008 ┆ 9.0 │
// │ Schindler's List ┆ 1993 ┆ 8.9 │
// │ Pulp Fiction ┆ 1994 ┆ 8.9 │
// └──────────────────────────┴──────────────┴────────────┘
Execute a GROUP BY query:
ctx.execute(`
SELECT
MAX(release_year / 10) * 10 AS decade,
SUM(gross) AS total_gross,
COUNT(title) AS n_films,
FROM films
GROUP BY (release_year / 10) -- decade
ORDER BY total_gross DESC
`, { eager: true });
// shape: (3, 3)
// ┌────────┬─────────────┬─────────┐
// │ decade ┆ total_gross ┆ n_films │
// │ --- ┆ --- ┆ --- │
// ╞════════╪═════════════╪═════════╡
// │ 2000 ┆ 533316061 ┆ 1 │
// │ 1990 ┆ 232338648 ┆ 3 │
// │ 1970 ┆ 134821952 ┆ 1 │
// └────────┴─────────────┴─────────┘
Parse the given SQL query and execute it against the registered frame data.
A valid string SQL query.
Apply the query eagerly, returning DataFrame
instead of LazyFrame
.
If unset, the value of the init-time parameter "eager_execution" will be
used. (Note that the query itself is always executed in lazy-mode; this
parameter only impacts the type of the returned frame).
Declare frame data and register with a SQLContext:
const df = pl.DataFrame({
data: [
("The Godfather", 1972, 6_000_000, 134_821_952, 9.2),
("The Dark Knight", 2008, 185_000_000, 533_316_061, 9.0),
("Schindler's List", 1993, 22_000_000, 96_067_179, 8.9),
("Pulp Fiction", 1994, 8_000_000, 107_930_000, 8.9),
("The Shawshank Redemption", 1994, 25_000_000, 28_341_469, 9.3),
],
schema: ["title", "release_year", "budget", "gross", "imdb_score"],
});
const ctx = pl.SQLContext({ films: df });
Execute a SQL query against the registered frame data:
const result = ctx.execute(`
SELECT title, release_year, imdb_score
FROM films
WHERE release_year > 1990
ORDER BY imdb_score DESC
`, { eager: true });
console.log(result);
// shape: (4, 3)
// ┌──────────────────────────┬──────────────┬────────────┐
// │ title ┆ release_year ┆ imdb_score │
// │ --- ┆ --- ┆ --- │
// ╞══════════════════════════╪══════════════╪════════════╡
// │ The Shawshank Redemption ┆ 1994 ┆ 9.3 │
// │ The Dark Knight ┆ 2008 ┆ 9.0 │
// │ Schindler's List ┆ 1993 ┆ 8.9 │
// │ Pulp Fiction ┆ 1994 ┆ 8.9 │
// └──────────────────────────┴──────────────┴────────────┘
Execute a GROUP BY query:
ctx.execute(`
SELECT
MAX(release_year / 10) * 10 AS decade,
SUM(gross) AS total_gross,
COUNT(title) AS n_films,
FROM films
GROUP BY (release_year / 10) -- decade
ORDER BY total_gross DESC
`, { eager: true });
// shape: (3, 3)
// ┌────────┬─────────────┬─────────┐
// │ decade ┆ total_gross ┆ n_films │
// │ --- ┆ --- ┆ --- │
// ╞════════╪═════════════╪═════════╡
// │ 2000 ┆ 533316061 ┆ 1 │
// │ 1990 ┆ 232338648 ┆ 3 │
// │ 1970 ┆ 134821952 ┆ 1 │
// └────────┴─────────────┴─────────┘
Register a single frame as a table, using the given name.
name : string Name of the table. frame : DataFrame | LazyFrame | null Eager/lazy frame to associate with this table name.
register_globals register_many unregister
const df = pl.DataFrame({"hello": ["world"]}); const ctx = pl.SQLContext(); ctx.register("frame_data", df).execute("SELECT * FROM frame_data").collect(); returns: shape: (1, 1) ┌───────┐ │ hello │ │ --- │ │ str │ ╞═══════╡ │ world │ └───────┘
Register a single frame as a table, using the given name.
name : string Name of the table. frame : DataFrame | LazyFrame | null Eager/lazy frame to associate with this table name.
register_globals register_many unregister
const df = pl.DataFrame({"hello": ["world"]}); const ctx = pl.SQLContext(); ctx.register("frame_data", df).execute("SELECT * FROM frame_data").collect(); returns: shape: (1, 1) ┌───────┐ │ hello │ │ --- │ │ str │ ╞═══════╡ │ world │ └───────┘
Register multiple DataFrames as tables, using the associated names.
An {name: df, ...}
mapping.
The SQLContext with registered DataFrames.
Register multiple DataFrames as tables, using the associated names.
An {name: df, ...}
mapping.
The SQLContext with registered DataFrames.
Returns a list of the registered table names.
An array of the registered table names.
The tables
method will return the same values as the "SHOW TABLES" SQL statement, but as a list instead of a frame.
Executing as SQL:
const frame_data = pl.DataFrame({"hello": ["world"]});
const ctx = pl.SQLContext({ hello_world: frame_data });
console.log(ctx.execute("SHOW TABLES", { eager: true }));
// shape: (1, 1)
// ┌─────────────┐
// │ name │
// │ --- │
// │ str │
// ╞═════════════╡
// │ hello_world │
// └─────────────┘
Calling the method:
console.log(ctx.tables());
// ['hello_world']
Returns a list of the registered table names.
An array of the registered table names.
The tables
method will return the same values as the "SHOW TABLES" SQL statement, but as a list instead of a frame.
Executing as SQL:
const frame_data = pl.DataFrame({"hello": ["world"]});
const ctx = pl.SQLContext({ hello_world: frame_data });
console.log(ctx.execute("SHOW TABLES", { eager: true }));
// shape: (1, 1)
// ┌─────────────┐
// │ name │
// │ --- │
// │ str │
// ╞═════════════╡
// │ hello_world │
// └─────────────┘
Calling the method:
console.log(ctx.tables());
// ['hello_world']
Unregister one or more eager/lazy frames by name.
Names of the tables to unregister.
You can also control table registration lifetime by using SQLContext
as a
context manager; this can often be more useful when such control is wanted.
Frames registered in-scope are automatically unregistered on scope-exit. Note that frames registered on construction will persist through subsequent scopes.
const df0 = pl.DataFrame({"colx": [0, 1, 2]});
const df1 = pl.DataFrame({"colx": [1, 2, 3]});
const df2 = pl.DataFrame({"colx": [2, 3, 4]});
// Register one frame at construction time, and the other two in-scope
const ctx = pl.SQLContext({ tbl0: df0 });
ctx.register("tbl1", df1);
ctx.register("tbl2", df2);
console.log(ctx.tables()); // Output: ['tbl0', 'tbl1', 'tbl2']
// After scope exit, none of the tables registered in-scope remain
const df0 = pl.DataFrame({"ints": [9, 8, 7, 6, 5]});
const lf1 = pl.LazyDataFrame({"text": ["a", "b", "c"]});
const lf2 = pl.LazyDataFrame({"misc": ["testing1234"]});
// Register with a SQLContext object
const ctx = pl.SQLContext({ test1: df0, test2: lf1, test3: lf2 });
console.log(ctx.tables()); // Output: ['test1', 'test2', 'test3']
// Unregister one or more of the tables
ctx.unregister(["test1", "test3"]);
console.log(ctx.tables()); // Output: ['test2']
ctx.unregister("test2");
console.log(ctx.tables()); // Output: []
Unregister one or more eager/lazy frames by name.
Names of the tables to unregister.
You can also control table registration lifetime by using SQLContext
as a
context manager; this can often be more useful when such control is wanted.
Frames registered in-scope are automatically unregistered on scope-exit. Note that frames registered on construction will persist through subsequent scopes.
const df0 = pl.DataFrame({"colx": [0, 1, 2]});
const df1 = pl.DataFrame({"colx": [1, 2, 3]});
const df2 = pl.DataFrame({"colx": [2, 3, 4]});
// Register one frame at construction time, and the other two in-scope
const ctx = pl.SQLContext({ tbl0: df0 });
ctx.register("tbl1", df1);
ctx.register("tbl2", df2);
console.log(ctx.tables()); // Output: ['tbl0', 'tbl1', 'tbl2']
// After scope exit, none of the tables registered in-scope remain
const df0 = pl.DataFrame({"ints": [9, 8, 7, 6, 5]});
const lf1 = pl.LazyDataFrame({"text": ["a", "b", "c"]});
const lf2 = pl.LazyDataFrame({"misc": ["testing1234"]});
// Register with a SQLContext object
const ctx = pl.SQLContext({ test1: df0, test2: lf1, test3: lf2 });
console.log(ctx.tables()); // Output: ['test1', 'test2', 'test3']
// Unregister one or more of the tables
ctx.unregister(["test1", "test3"]);
console.log(ctx.tables()); // Output: ['test2']
ctx.unregister("test2");
console.log(ctx.tables()); // Output: []
Parse the given SQL query and execute it against the registered frame data.