Skip to content

Commit

Permalink
Merge pull request #4799 from szarnyasg/nits-20250214a
Browse files Browse the repository at this point in the history
Update example snippets on the landing page
  • Loading branch information
szarnyasg authored Feb 14, 2025
2 parents 7dc31d9 + 36036df commit a294f0f
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 34 deletions.
3 changes: 2 additions & 1 deletion _includes/landing-page/java/appender.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion _includes/landing-page/java/sql-query.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;");
Expand Down
11 changes: 6 additions & 5 deletions _includes/landing-page/nodejs/sql-query.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
8 changes: 5 additions & 3 deletions _includes/landing-page/nodejs/web-service-integration.mjs
Original file line number Diff line number Diff line change
@@ -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"));
6 changes: 3 additions & 3 deletions _includes/landing-page/python/udf.py
Original file line number Diff line number Diff line change
@@ -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);
""")
7 changes: 4 additions & 3 deletions _includes/landing-page/r/dplyr-integration.r
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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) |>
Expand Down
13 changes: 13 additions & 0 deletions _includes/landing-page/r/duckplyr.r
Original file line number Diff line number Diff line change
@@ -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()
16 changes: 8 additions & 8 deletions _includes/landing-page/r/sql-query.r
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 4 additions & 4 deletions _includes/landing-page/sql/join-full.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions _includes/landing-page/sql/join.sql
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 1 addition & 2 deletions _includes/landing-page/sql/spatial-extension.sql
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
7 changes: 7 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ <h3>{{ event.title }}</h3>
<!-- specific example list for R -->
<div data-language="r">
<option value="r-sql-query">SQL query</option>
<option value="r-duckplyr">duckplyr</option>
<option value="r-dplyr-integration">dplyr integration</option>
</div>

Expand Down Expand Up @@ -286,6 +287,12 @@ <h3>{{ event.title }}</h3>
{% endhighlight %}
</div>

<div data-language="r" data-example="r-duckplyr" data-buttontxt="R documentation" data-buttonurl="/docs/api/r">
{% highlight r %}
{% include landing-page/r/duckplyr.r %}
{% endhighlight %}
</div>

<div data-language="r" data-example="r-dplyr-integration" data-buttontxt="R documentation" data-buttonurl="/docs/api/r">
{% highlight r %}
{% include landing-page/r/dplyr-integration.r %}
Expand Down

0 comments on commit a294f0f

Please sign in to comment.