From 36036df7936d9586c883d9cb9ea06360ef3aae0e Mon Sep 17 00:00:00 2001 From: Gabor Szarnyas Date: Fri, 14 Feb 2025 07:13:39 +0100 Subject: [PATCH] Update landing page snippets to fit into the box and add duckplyr example --- _includes/landing-page/java/appender.java | 3 ++- _includes/landing-page/java/sql-query.java | 3 ++- _includes/landing-page/nodejs/sql-query.mjs | 11 ++++++----- .../nodejs/web-service-integration.mjs | 8 +++++--- _includes/landing-page/python/udf.py | 6 +++--- _includes/landing-page/r/dplyr-integration.r | 7 ++++--- _includes/landing-page/r/duckplyr.r | 13 +++++++++++++ _includes/landing-page/r/sql-query.r | 16 ++++++++-------- _includes/landing-page/sql/join-full.sql | 8 ++++---- _includes/landing-page/sql/join.sql | 8 ++++---- _includes/landing-page/sql/spatial-extension.sql | 3 +-- index.html | 7 +++++++ 12 files changed, 59 insertions(+), 34 deletions(-) create mode 100644 _includes/landing-page/r/duckplyr.r diff --git a/_includes/landing-page/java/appender.java b/_includes/landing-page/java/appender.java index 8ce1a6ba5bc..9fa0c414a70 100644 --- a/_includes/landing-page/java/appender.java +++ b/_includes/landing-page/java/appender.java @@ -2,7 +2,8 @@ DuckDBConnection conn = (DuckDBConnection) DriverManager.getConnection("jdbc:duckdb:"); Statement st = conn.createStatement(); -st.execute("CREATE TABLE person (name VARCHAR, age INT)"); +st.execute("CREATE TABLE person " + + "(name VARCHAR, age INT)"); var appender = conn.createAppender( DuckDBConnection.DEFAULT_SCHEMA, "person"); diff --git a/_includes/landing-page/java/sql-query.java b/_includes/landing-page/java/sql-query.java index 7892ec4f121..f8f5bf2b34c 100644 --- a/_includes/landing-page/java/sql-query.java +++ b/_includes/landing-page/java/sql-query.java @@ -3,7 +3,8 @@ DriverManager.getConnection("jdbc:duckdb:"); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery( - "SELECT station_name, count(*) AS num_services\n" + + "SELECT station_name,\n" + + " count(*) AS num_services\n" + "FROM train_services\n" + "GROUP BY ALL\n" + "ORDER BY num_services DESC;"); diff --git a/_includes/landing-page/nodejs/sql-query.mjs b/_includes/landing-page/nodejs/sql-query.mjs index ffb415f6176..4c0a4ecd358 100644 --- a/_includes/landing-page/nodejs/sql-query.mjs +++ b/_includes/landing-page/nodejs/sql-query.mjs @@ -4,10 +4,11 @@ const instance = await DuckDBInstance.create(); const connection = await instance.connect(); const reader = await connection.runAndReadAll( `SELECT station_name, count(*) AS num_services - FROM 'http://blobs.duckdb.org/train_services.parquet' - WHERE monthname(date) = 'May' - GROUP BY ALL - ORDER BY num_services DESC - LIMIT 3;` + FROM + 'http://blobs.duckdb.org/train_services.parquet' + WHERE monthname(date) = 'May' + GROUP BY ALL + ORDER BY num_services DESC + LIMIT 3;` ); console.table(reader.getRows()); diff --git a/_includes/landing-page/nodejs/web-service-integration.mjs b/_includes/landing-page/nodejs/web-service-integration.mjs index b087849c22f..722be3a0bf4 100644 --- a/_includes/landing-page/nodejs/web-service-integration.mjs +++ b/_includes/landing-page/nodejs/web-service-integration.mjs @@ -1,13 +1,15 @@ // Web Service Integration: -// create endpoint to generate numbers +// Create endpoint to generate numbers import express from "express"; import { DuckDBInstance } from '@duckdb/node-api'; const app = express(); const instance = await DuckDBInstance.create(); const connection = await instance.connect(); app.get("/getnumbers", async (req, res) => { - const reader = await connection.runAndReadAll("SELECT random() AS num FROM range(10)"); + const reader = await connection.runAndReadAll( + "SELECT random() AS num FROM range(10)"); res.end(JSON.stringify(reader.getRows())); }); -app.listen(8082, () => console.log("Go to: http://localhost:8082/getnumbers")); +app.listen(8082, () => console.log( + "Go to: http://localhost:8082/getnumbers")); diff --git a/_includes/landing-page/python/udf.py b/_includes/landing-page/python/udf.py index e81002c4928..4a80bda9b2f 100644 --- a/_includes/landing-page/python/udf.py +++ b/_includes/landing-page/python/udf.py @@ -1,13 +1,13 @@ # Create custom user-defined function import duckdb -def add_plus_one(x): +def plus_one(x): return x + 1 con = duckdb.connect() -con.create_function('add_plus_one', add_plus_one, +con.create_function('plus_one', plus_one, ['BIGINT'], 'BIGINT', type='native') con.sql(""" - SELECT sum(add_plus_one(i)) FROM range(10) tbl(i); + SELECT sum(plus_one(i)) FROM range(10) tbl(i); """) diff --git a/_includes/landing-page/r/dplyr-integration.r b/_includes/landing-page/r/dplyr-integration.r index d45b7a843f6..16d5c072016 100644 --- a/_includes/landing-page/r/dplyr-integration.r +++ b/_includes/landing-page/r/dplyr-integration.r @@ -1,5 +1,5 @@ -# Integration with dplyr -# Find the largest sepals and petals in the Iris data set +# Find the largest sepals/petals in the Iris data set +# using dplyr library("duckdb") library("dplyr") @@ -8,7 +8,8 @@ duckdb_register(con, "iris", iris) tbl(con, "iris") |> filter(Sepal.Length > 5) |> group_by(Species) |> - summarize(num_observations = count(), + summarize( + num_observations = count(), max_width = max(Sepal.Width), max_petal_length = max(Petal.Length), na.rm = TRUE) |> diff --git a/_includes/landing-page/r/duckplyr.r b/_includes/landing-page/r/duckplyr.r new file mode 100644 index 00000000000..3d5d791ace9 --- /dev/null +++ b/_includes/landing-page/r/duckplyr.r @@ -0,0 +1,13 @@ +# Find the largest sepals/petals in the Iris data set +# using duckplyr +library("duckplyr") + +iris |> + filter(Sepal.Length > 5) |> + group_by(Species) |> + summarize( + num_observations = n(), + max_width = max(Sepal.Width), + max_petal_length = max(Petal.Length), + na.rm = TRUE) |> + collect() diff --git a/_includes/landing-page/r/sql-query.r b/_includes/landing-page/r/sql-query.r index 4e1ffa6c129..19f881f72de 100644 --- a/_includes/landing-page/r/sql-query.r +++ b/_includes/landing-page/r/sql-query.r @@ -1,16 +1,16 @@ -# Find the largest sepals & petals in the Iris data set +# Find the largest sepals/petals in the Iris data set library(duckdb) con <- dbConnect(duckdb()) duckdb_register(con, "iris", iris) query <- r'( -SELECT count(*) AS num_observations, -max("Sepal.Width") AS max_width, -max("Petal.Length") AS max_petal_length -FROM iris -WHERE "Sepal.Length" > 5 -GROUP BY ALL -)' + SELECT count(*) AS num_observations, + max("Sepal.Width") AS max_width, + max("Petal.Length") AS max_petal_length + FROM iris + WHERE "Sepal.Length" > 5 + GROUP BY ALL + )' dbGetQuery(con, query) diff --git a/_includes/landing-page/sql/join-full.sql b/_includes/landing-page/sql/join-full.sql index ea35df5c1f1..9c473265056 100644 --- a/_includes/landing-page/sql/join-full.sql +++ b/_includes/landing-page/sql/join-full.sql @@ -4,10 +4,10 @@ CREATE TABLE distances AS FROM 's3://duckdb-blobs/distances.parquet'; -- Find the top-3 longest domestic train routes -SELECT s1.name_short, s2.name_short, distances.distance -FROM distances -JOIN stations s1 ON distances.station1 = s1.code -JOIN stations s2 ON distances.station2 = s2.code +SELECT s1.name_short, s2.name_short, d.distance +FROM distances d +JOIN stations s1 ON d.station1 = s1.code +JOIN stations s2 ON d.station2 = s2.code WHERE s1.country = s2.country AND s1.code < s2.code ORDER BY distance DESC diff --git a/_includes/landing-page/sql/join.sql b/_includes/landing-page/sql/join.sql index b7c5e8d4535..ffd8cefe1b6 100644 --- a/_includes/landing-page/sql/join.sql +++ b/_includes/landing-page/sql/join.sql @@ -1,8 +1,8 @@ -- Find the top-3 longest domestic train routes -SELECT s1.name_short, s2.name_short, distances.distance -FROM distances -JOIN stations s1 ON distances.station1 = s1.code -JOIN stations s2 ON distances.station2 = s2.code +SELECT s1.name_short, s2.name_short, d.distance +FROM distances d +JOIN stations s1 ON d.station1 = s1.code +JOIN stations s2 ON d.station2 = s2.code WHERE s1.country = s2.country AND s1.code < s2.code ORDER BY distance DESC diff --git a/_includes/landing-page/sql/spatial-extension.sql b/_includes/landing-page/sql/spatial-extension.sql index 1a87afbb315..fe0dc2333ae 100644 --- a/_includes/landing-page/sql/spatial-extension.sql +++ b/_includes/landing-page/sql/spatial-extension.sql @@ -1,5 +1,4 @@ --- What are the top-3 closest Intercity stations --- using aerial distance? +-- List the closest IC stations (as the crow flies) SELECT s1.name_long AS station1, s2.name_long AS station2, diff --git a/index.html b/index.html index f0f6745807e..3ed6d5fff31 100644 --- a/index.html +++ b/index.html @@ -207,6 +207,7 @@

{{ event.title }}

+
@@ -286,6 +287,12 @@

{{ event.title }}

{% endhighlight %} +
+{% highlight r %} +{% include landing-page/r/duckplyr.r %} +{% endhighlight %} +
+
{% highlight r %} {% include landing-page/r/dplyr-integration.r %}