Skip to content

Commit 84cc889

Browse files
committed
Fix #135
1 parent d18118b commit 84cc889

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

programs/local/LocalServer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ void LocalServer::tryInitPath()
268268
fs::create_directories(fs::path(path) / "metadata_dropped/");
269269

270270
global_context->setPath(path);
271+
DatabaseCatalog::instance().fixPath(path);
271272

272273
global_context->setTemporaryStoragePath(path + "tmp/", 0);
273274
global_context->setFlagsPath(path + "flags");

src/Interpreters/DatabaseCatalog.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,12 @@ String DatabaseCatalog::getPathForMetadata(const StorageID & table_id) const
10461046
escapeForFileName(table_id.getTableName()) + ".sql";
10471047
}
10481048

1049+
//chdb session query need to fix the path
1050+
void DatabaseCatalog::fixPath(const String & path)
1051+
{
1052+
getContext()->setPath(path);
1053+
}
1054+
10491055
void DatabaseCatalog::enqueueDroppedTableCleanup(StorageID table_id, StoragePtr table, String dropped_metadata_path, bool ignore_delay)
10501056
{
10511057
assert(table_id.hasUUID());

src/Interpreters/DatabaseCatalog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ class DatabaseCatalog : boost::noncopyable, WithMutableContext
250250

251251
String getPathForDroppedMetadata(const StorageID & table_id) const;
252252
String getPathForMetadata(const StorageID & table_id) const;
253+
void fixPath(const String & path);
253254
void enqueueDroppedTableCleanup(StorageID table_id, StoragePtr table, String dropped_metadata_path, bool ignore_delay = false);
254255
void dequeueDroppedTableCleanup(StorageID table_id);
255256

tests/test_issue135.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!python3
2+
3+
import shutil
4+
import unittest
5+
from chdb import session as chs
6+
7+
8+
test_dir = ".state_tmp_auxten_issue135"
9+
10+
11+
class TestReplaceTable(unittest.TestCase):
12+
def setUp(self) -> None:
13+
shutil.rmtree(test_dir, ignore_errors=True)
14+
return super().setUp()
15+
16+
def tearDown(self) -> None:
17+
shutil.rmtree(test_dir, ignore_errors=True)
18+
return super().tearDown()
19+
20+
def test_replace_table(self):
21+
sess = chs.Session(test_dir)
22+
sess.query("CREATE DATABASE IF NOT EXISTS a;", "Debug")
23+
sess.query(
24+
"CREATE OR REPLACE TABLE a.test (id UInt64, updated_at DateTime DEFAULT now(),updated_at_date Date DEFAULT toDate(updated_at)) "
25+
"ENGINE = MergeTree ORDER BY id;"
26+
)
27+
sess.query("INSERT INTO a.test (id) Values (1);")
28+
ret = sess.query("SELECT * FROM a.test;", "CSV")
29+
# something like 1,"2023-11-20 21:59:57","2023-11-20"
30+
parts = str(ret).split(",")
31+
self.assertEqual(len(parts), 3)
32+
self.assertEqual(parts[0], "1")
33+
# regex for datetime
34+
self.assertRegex(parts[1], r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}")
35+
# regex for date
36+
self.assertRegex(parts[2], r"\d{4}-\d{2}-\d{2}")
37+
38+
# replace table
39+
sess.query(
40+
"CREATE OR REPLACE TABLE a.test (id UInt64, updated_at DateTime DEFAULT now(),updated_at_date Date DEFAULT toDate(updated_at)) "
41+
"ENGINE = MergeTree ORDER BY id;"
42+
)
43+
ret = sess.query("SELECT * FROM a.test;", "CSV")
44+
self.assertEqual(str(ret), "")
45+
46+
47+
if __name__ == "__main__":
48+
unittest.main()

tests/test_stateful.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,26 @@ def test_tmp(self):
9393
ret = sess2.query("SELECT chdb_xxx()", "CSV")
9494
self.assertEqual(str(ret), "")
9595

96+
def test_two_sessions(self):
97+
sess1 = session.Session()
98+
sess2 = session.Session()
99+
sess1.query("CREATE FUNCTION chdb_xxx AS () -> 'sess1'", "CSV")
100+
sess2.query("CREATE FUNCTION chdb_xxx AS () -> 'sess2'", "CSV")
101+
sess1.query("CREATE DATABASE IF NOT EXISTS db_xxx ENGINE = Atomic", "CSV")
102+
sess2.query("CREATE DATABASE IF NOT EXISTS db_xxx ENGINE = Atomic", "CSV")
103+
sess1.query("CREATE TABLE IF NOT EXISTS db_xxx.tbl1 (x UInt8) ENGINE = Log;")
104+
sess2.query("CREATE TABLE IF NOT EXISTS db_xxx.tbl2 (x UInt8) ENGINE = Log;")
105+
sess1.query("INSERT INTO db_xxx.tbl1 VALUES (1), (2), (3), (4);")
106+
sess2.query("INSERT INTO db_xxx.tbl2 VALUES (5), (6), (7), (8);")
107+
ret = sess1.query("SELECT chdb_xxx()", "CSV")
108+
self.assertEqual(str(ret), '"sess1"\n')
109+
ret = sess2.query("SELECT chdb_xxx()", "CSV")
110+
self.assertEqual(str(ret), '"sess2"\n')
111+
ret = sess1.query("SELECT * FROM db_xxx.tbl1", "CSV")
112+
self.assertEqual(str(ret), "1\n2\n3\n4\n")
113+
ret = sess2.query("SELECT * FROM db_xxx.tbl2", "CSV")
114+
self.assertEqual(str(ret), "5\n6\n7\n8\n")
115+
96116
def test_context_mgr(self):
97117
with session.Session() as sess:
98118
sess.query("CREATE FUNCTION chdb_xxx AS () -> '0.12.0'", "CSV")

0 commit comments

Comments
 (0)