forked from chdb-io/chdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchdbCppAdvanced.cpp
More file actions
53 lines (52 loc) · 1.83 KB
/
Copy pathchdbCppAdvanced.cpp
File metadata and controls
53 lines (52 loc) · 1.83 KB
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
#include "../programs/local/chdb.hpp"
#include <iostream>
#include <string>
#include <vector>
int main() {
try {
auto conn = chdb::connect(":memory:");
conn.query(R"(
CREATE TABLE users (
id UInt32,
name String,
age UInt32,
city String
) ENGINE = Memory
)");
conn.query(R"(
INSERT INTO users VALUES
(1, 'Alice', 25, 'New York'),
(2, 'Bob', 30, 'San Francisco'),
(3, 'Charlie', 35, 'Chicago'),
(4, 'Diana', 28, 'Boston')
)");
std::cout << "CSV Format\n";
auto result1 = conn.query("SELECT * FROM users ORDER BY id", "CSV");
std::cout << result1.str() << std::endl;
std::cout << "JSON Format\n";
auto result2 = conn.query("SELECT name, age, city FROM users WHERE age > 30", "JSON");
std::cout << result2.str() << std::endl;
std::cout << "Aggregation Query\n";
auto result3 = conn.query(R"(
SELECT
city,
count() as user_count,
avg(age) as avg_age
FROM users
GROUP BY city
ORDER BY user_count DESC
)");
std::cout << result3.str() << std::endl;
std::cout << "\n=== Query Statistics ===\n";
std::cout << "Last query elapsed time: " << result3.elapsed() << " seconds\n";
std::cout << "Rows read: " << result3.rows_read() << std::endl;
std::cout << "Storage rows read: " << result3.storage_rows_read() << std::endl;
return 0;
} catch (const chdb::ChdbError& e) {
std::cerr << "ChDB Error: " << e.what() << std::endl;
return 1;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
}