Skip to content

Commit 4b36e1b

Browse files
feat: add R SPCS demo
1 parent 07b0346 commit 4b36e1b

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed

demos/r-spcs/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
This is a small demo that builds on top of the TTYD demo and shows how
2+
interactive R can be run in SPCS. The provided code shows how to establish
3+
connection using ODBC driver and OAUTH authentication that leverages SPCS token.
4+
5+
# Prerequisites
6+
7+
1. SPCS service set up with ttyd service and a network policy that allows access
8+
to cache.nixos.org
9+
10+
2. `SNOWFLAKE_SAMPLE_DATA` setup in account
11+
3. A virtual warehouse accessible to the role that owns SPCS service
12+
13+
# Result
14+
15+
Run following in bash:
16+
17+
```shell
18+
nix develop "github:sfc-gh-vtimofeenko/spcs-ttyd?dir=demos"
19+
```
20+
21+
This will start a development shell with Snowflake ODBC driver and R REPL with
22+
the needed packages pre-installed.
23+
24+
Run following R code to get some sample data from CUSTOMER table in the sample
25+
data set:
26+
27+
```R
28+
snowflake_token <- readLines("/snowflake/session/token", warn = FALSE)
29+
con <- DBI::dbConnect(odbc::odbc()
30+
, "SnowflakeDSII"
31+
# account and server are needed
32+
, token = snowflake_token
33+
, authenticator = 'oauth'
34+
, port = 443
35+
, warehouse = "ADHOC"
36+
, ssl = 'on'
37+
)
38+
data <- dbGetQuery(con,"SELECT * FROM SNOWFLAKE_SAMPLE_DATA.TPCH_SF1.CUSTOMER LIMIT 100")
39+
head(data)
40+
41+
```
42+
43+
# Further reading
44+
45+
* [Running R-Studio in Snowpark Container Services][1]
46+
47+
[1]: https://medium.com/@gabriel.mullen/running-rstudio-in-snowpark-container-services-1a71128b2474

demos/r-spcs/flake.nix

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
description = "Description for the project";
3+
4+
inputs = {
5+
flake-parts.url = "github:hercules-ci/flake-parts";
6+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
7+
};
8+
9+
outputs =
10+
inputs@{ flake-parts, ... }:
11+
flake-parts.lib.mkFlake { inherit inputs; } {
12+
systems = [
13+
"x86_64-linux"
14+
"aarch64-linux"
15+
];
16+
perSystem =
17+
{ self'
18+
, pkgs
19+
, ...
20+
}:
21+
{
22+
packages = rec {
23+
default = snowflake-odbc-driver;
24+
snowflake-odbc-driver = pkgs.callPackage ./snowflake-odbc-driver.nix { };
25+
};
26+
27+
devShells.default = pkgs.stdenv.mkDerivation {
28+
name = "my-snowflake-odbc-project";
29+
buildInputs = [
30+
self'.packages.snowflake-odbc-driver
31+
pkgs.envsubst
32+
];
33+
nativeBuildInputs = [
34+
pkgs.unixODBC
35+
(pkgs.rWrapper.override {
36+
37+
packages = with pkgs.rPackages; [
38+
DBI
39+
dplyr
40+
dbplyr
41+
odbc
42+
];
43+
})
44+
];
45+
shellHook =
46+
let
47+
ODBCINI = pkgs.writeTextDir "odbc.ini" ''
48+
[ODBC Data Sources]
49+
SnowflakeDSII = Snowflake
50+
[SnowflakeDSII]
51+
SERVER = ''${SNOWFLAKE_HOST}
52+
account = ''${SNOWFLAKE_ACCOUNT}
53+
Port = 443
54+
SSL = on
55+
Locale = en-US
56+
Tracing = 0
57+
# Oauth stuffs
58+
authenticator=OAUTH
59+
token= ''${SNOWFLAKE_TOKEN}
60+
'';
61+
in
62+
''
63+
export SNOWFLAKE_TOKEN=$(cat /snowflake/session/token)
64+
envsubst < ${ODBCINI}/odbc.ini > ./odbc.ini
65+
# Looks like it needs to be in a writeable location?
66+
export ODBCINI=$(realpath ./odbc.ini)
67+
'';
68+
};
69+
};
70+
};
71+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{ stdenv
2+
, lib
3+
, unixODBC
4+
, openssl
5+
, fetchurl
6+
,
7+
}:
8+
let
9+
version = "3.5.0";
10+
sources = {
11+
"x86_64-linux" = {
12+
url = "https://sfc-repo.snowflakecomputing.com/odbc/linux/${version}/snowflake_linux_x8664_odbc-${version}.tgz";
13+
sha256 = "sha256-PaYX3Bgt4zqU9f1lUjYtDct685Fgk6LTQBi2K27j/lQ=";
14+
};
15+
"aarch64-linux" = {
16+
url = "https://sfc-repo.snowflakecomputing.com/odbc/linuxaarch64/${version}/snowflake_linux_aarch64_odbc-${version}.tgz";
17+
sha256 = "sha256-MORf5kY5b6059jsr4egPwBc0c3FXMEI8+p446EqY8fk=";
18+
};
19+
};
20+
in
21+
stdenv.mkDerivation {
22+
pname = "snowflake-odbc-driver";
23+
inherit version;
24+
src = fetchurl sources.${stdenv.hostPlatform.system};
25+
26+
# Basically use precompiled so
27+
installPhase = ''
28+
mkdir -p $out/lib
29+
cp -r lib/* $out/lib
30+
cp -r ErrorMessages/en-US $out/lib
31+
'';
32+
33+
postFixup = ''
34+
patchelf --set-rpath ${
35+
lib.makeLibraryPath [
36+
unixODBC
37+
openssl
38+
stdenv.cc.cc
39+
]
40+
} \
41+
$out/lib/libSnowflake.so
42+
'';
43+
44+
passthru = {
45+
fancyName = "SnowflakeDSIIDriver";
46+
driver = "lib/libSnowflake.so";
47+
};
48+
}

0 commit comments

Comments
 (0)