-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexamples.rs
75 lines (62 loc) · 1.8 KB
/
examples.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use chdb_rust::arg::Arg;
use chdb_rust::error::Result;
use chdb_rust::execute;
use chdb_rust::format::InputFormat;
use chdb_rust::format::OutputFormat;
use chdb_rust::log_level::LogLevel;
use chdb_rust::session::SessionBuilder;
#[test]
fn test_stateful() -> Result<()> {
//
// Create session.
//
let tmp = tempdir::TempDir::new("chdb-rust")?;
let session = SessionBuilder::new()
.with_data_path(tmp.path())
.with_arg(Arg::LogLevel(LogLevel::Debug))
.with_arg(Arg::Custom("priority".into(), Some("1".into())))
.with_auto_cleanup(true)
.build()?;
//
// Create database.
//
session.execute("CREATE DATABASE demo; USE demo", Some(&[Arg::MultiQuery]))?;
//
// Create table.
//
session.execute(
"CREATE TABLE logs (id UInt64, msg String) ENGINE = MergeTree() ORDER BY id",
None,
)?;
//
// Insert into table.
//
session.execute("INSERT INTO logs (id, msg) VALUES (1, 'test')", None)?;
//
// Select from table.
//
let len = session.execute(
"SELECT COUNT(*) FROM logs",
Some(&[Arg::OutputFormat(OutputFormat::JSONEachRow)]),
)?;
assert_eq!(len.data_utf8_lossy(), "{\"COUNT()\":1}\n");
let result = session.execute(
"SELECT * FROM logs",
Some(&[Arg::OutputFormat(OutputFormat::JSONEachRow)]),
)?;
assert_eq!(result.data_utf8_lossy(), "{\"id\":1,\"msg\":\"test\"}\n");
Ok(())
}
#[test]
fn test_stateless() -> Result<()> {
let query = format!(
"SELECT * FROM file('tests/logs.csv', {})",
InputFormat::CSV.as_str()
);
let result = execute(
&query,
Some(&[Arg::OutputFormat(OutputFormat::JSONEachRow)]),
)?;
assert_eq!(result.data_utf8_lossy(), "{\"id\":1,\"msg\":\"test\"}\n");
Ok(())
}