Skip to content

Commit

Permalink
Making DuckDB connection configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgSkv committed Jun 30, 2024
1 parent 51c25e6 commit 6602e04
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 34 deletions.
17 changes: 14 additions & 3 deletions colab_logica.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,16 @@ def RunSQL(sql, engine, connection=None, is_final=False):
psql_logica.PostgresExecute(sql, connection)
elif engine == 'duckdb':
if is_final:
return duckdb.sql(sql).df()
df = connection.sql(sql).df()
for c in df.columns:
if df.dtypes[c] == 'float64':
if df[c].isna().values.any():
return df
if set(df[c] - df[c].astype(int)) == {0.0}:
df[c] = df[c].astype(int)
return df
else:
duckdb.sql(sql)
connection.sql(sql)
elif engine == 'sqlite':
try:
if is_final:
Expand Down Expand Up @@ -229,7 +236,11 @@ def __call__(self, sql, engine, is_final):

class DuckdbRunner(object):
def __init__(self):
self.connection = duckdb_logica.SqliteConnect()
global DB_CONNECTION
if not DB_CONNECTION:
DB_CONNECTION = duckdb.connect()
self.connection = DB_CONNECTION

def __call__(self, sql, engine, is_final):
return RunSQL(sql, engine, self.connection, is_final)

Expand Down
8 changes: 2 additions & 6 deletions common/duckdb_logica.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from decimal import Decimal
import sqlite3

def SqliteConnect():
con = sqlite3.connect(':memory:')
return con
# Keeping the placeholder, but we may never needed.
# DuckDB is too easy to connect!
5 changes: 5 additions & 0 deletions compiler/universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ def Preamble(self):
'create schema if not exists logica_home;\n'
'-- Empty record, has to have a field by DuckDB syntax.\n'
'drop type if exists logicarecord893574736 cascade; create type logicarecord893574736 as struct(nirvana numeric);\n'
)
if self.annotations['@Engine']['duckdb'].get('motherduck'):
preamble += '\n' # Sequences are not supported in MotherDuck.
else:
preamble += (
'create sequence if not exists eternal_logical_sequence;\n\n')
return preamble

Expand Down
36 changes: 18 additions & 18 deletions integration_tests/duckdb_flow_test.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
+------+------+--------------------+
| col0 | col1 | logica_value |
+------+------+--------------------+
| 0.0 | 1.0 | 2.9970000000000003 |
| 0.0 | 4.0 | 9.994999999999997 |
| 1.0 | 0.0 | 0.0 |
| 1.0 | 2.0 | 0.0 |
| 1.0 | 5.0 | 9.992999999999999 |
| 2.0 | 1.0 | 6.995999999999998 |
| 2.0 | 3.0 | 2.999 |
| 2.0 | 4.0 | 0.0 |
| 3.0 | 2.0 | 0.0 |
| 3.0 | 5.0 | 0.0 |
| 4.0 | 0.0 | 0.0 |
| 4.0 | 2.0 | 9.994999999999997 |
| 5.0 | 1.0 | 0.0 |
| 5.0 | 3.0 | 9.992999999999999 |
+------+------+--------------------+
+-------+-------+--------------------+
| col0 | col1 | logica_value |
+-------+-------+--------------------+
| 0.000 | 1.000 | 2.9970000000000003 |
| 0.000 | 4.000 | 9.994999999999997 |
| 1.000 | 0.000 | 0.0 |
| 1.000 | 2.000 | 0.0 |
| 1.000 | 5.000 | 9.992999999999999 |
| 2.000 | 1.000 | 6.995999999999998 |
| 2.000 | 3.000 | 2.999 |
| 2.000 | 4.000 | 0.0 |
| 3.000 | 2.000 | 0.0 |
| 3.000 | 5.000 | 0.0 |
| 4.000 | 0.000 | 0.0 |
| 4.000 | 2.000 | 9.994999999999997 |
| 5.000 | 1.000 | 0.0 |
| 5.000 | 3.000 | 9.992999999999999 |
+-------+-------+--------------------+
2 changes: 1 addition & 1 deletion integration_tests/psqld_empty_list_type_duck_test.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
+--------------+
| logica_value |
+--------------+
| nan |
| None |
+--------------+
7 changes: 3 additions & 4 deletions logica.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,9 @@ def main(argv):
format).encode()
elif engine == 'duckdb':
import duckdb
df = duckdb.sql(formatted_sql).df()
o = sqlite3_logica.ArtisticTable(list(df.columns),
df.values
.tolist()).encode()
cur = duckdb.sql(formatted_sql)
o = sqlite3_logica.ArtisticTable(cur.columns,
cur.fetchall()).encode()
elif engine == 'psql':
connection_str = os.environ.get('LOGICA_PSQL_CONNECTION')
if connection_str:
Expand Down
5 changes: 3 additions & 2 deletions tools/run_in_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ def RunSQL(sql, engine, connection=None, is_final=False,
elif engine == 'duckdb':
import duckdb
if is_final:
df = duckdb.sql(sql).df()
return list(df.columns), df.values.tolist()
import duckdb
cur = duckdb.sql(sql)
return cur.columns, cur.fetchall()
else:
duckdb.sql(sql)

Expand Down

0 comments on commit 6602e04

Please sign in to comment.