Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to query postgre table? #225

Open
wonb168 opened this issue Jun 6, 2024 · 7 comments
Open

how to query postgre table? #225

wonb168 opened this issue Jun 6, 2024 · 7 comments
Labels
question Further information is requested Session

Comments

@wonb168
Copy link

wonb168 commented Jun 6, 2024

data may stock in rdb,like postgre,and how to query it by chdb?

@wonb168 wonb168 added the question Further information is requested label Jun 6, 2024
@wonb168
Copy link
Author

wonb168 commented Jun 7, 2024

I tested, but:

>>> import chdb
>>> sql="""CREATE DATABASE chgp
... ENGINE = PostgreSQL('127.0.0.1:2345', 'mdmaster_peacebird_uat2', 'gpadmin', 'mysecretpassword', 'tenant_peacebird_adm',1);
... """
>>> chdb.query(sql)

>>> 
>>> chdb.query("SELECT count(*) FROM chgp.gto_skc_store_step_kpi_summary_duck;")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/python3.8/lib/python3.8/site-packages/chdb/__init__.py", line 78, in query
    raise ChdbError(res.error_message())
chdb.ChdbError: Code: 81. DB::Exception: Database chgp does not exist. (UNKNOWN_DATABASE)
>>> 

I want to use chdb to speed up postgre, calculate in chdb and then write the result back to postgre.
How to do?thanks!

@chdb-io chdb-io deleted a comment from Daniel-Robbins Jun 7, 2024
@auxten
Copy link
Member

auxten commented Jun 7, 2024

Try use session

from chdb import session as chs

## Create DB, Table, View in temp session, auto cleanup when session is deleted.
sess = chs.Session()
sess.query("CREATE DATABASE IF NOT EXISTS db_xxx ENGINE = Atomic")
sess.query("CREATE TABLE IF NOT EXISTS db_xxx.log_table_xxx (x String, y Int) ENGINE = Log;")
sess.query("INSERT INTO db_xxx.log_table_xxx VALUES ('a', 1), ('b', 3), ('c', 2), ('d', 5);")
sess.query(
    "CREATE VIEW db_xxx.view_xxx AS SELECT * FROM db_xxx.log_table_xxx LIMIT 4;"
)
print("Select from view:\n")
print(sess.query("SELECT * FROM db_xxx.view_xxx", "Pretty"))

@wonb168
Copy link
Author

wonb168 commented Jun 7, 2024

Thank you, this way can query greenplum table now, but how to query temp table ?
If use duckdb, like this:

import duckdb as dd
dd.execute("call postgres_execute('gp','create temp table tmp as select 1 id')")
dd.execute("select * from postgres_query('gp','select * from tmp')")

how to do in chdb?

and how to write chdb result back to gp?

thanks!

@auxten
Copy link
Member

auxten commented Jun 7, 2024

@wonb168
Copy link
Author

wonb168 commented Jun 7, 2024

then I query gp table is OK,

sess.query("select * from public.base_update_org limit 1")

but how to create table in chdb?

sess.query("create table tmp Engine=Memory as select * from public.base_update_org limit 1")
sess.query("select * from tmp")

chdb.ChdbError: Code: 60. DB::Exception: Table _local.tmp does not exist. (UNKNOWN_TABLE)

@auxten
Copy link
Member

auxten commented Jun 7, 2024

The default _local db is not persist, you should create a new one to save your modifications.
See:

chdb/tests/test_usedb.py

Lines 26 to 38 in 50fd104

sess.query("CREATE DATABASE IF NOT EXISTS db_xxx ENGINE = Atomic", "CSV")
ret = sess.query("SHOW DATABASES", "CSV")
self.assertIn("db_xxx", str(ret))
sess.query("CREATE TABLE IF NOT EXISTS db_xxx.log_table_xxx (x UInt8) ENGINE = Log;")
sess.query("INSERT INTO db_xxx.log_table_xxx VALUES (1), (2), (3), (4);")
ret = sess.query("USE db_xxx; SELECT * FROM log_table_xxx", "Debug")
self.assertEqual(str(ret), "1\n2\n3\n4\n")
sess.query("USE db_xxx")
ret = sess.query("SELECT * FROM log_table_xxx", "Debug")
self.assertEqual(str(ret), "1\n2\n3\n4\n")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Session
Projects
None yet
Development

No branches or pull requests

2 participants