Skip to content

Commit

Permalink
Minor tweaks for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
malhotrashivam committed Nov 9, 2023
1 parent 9b73db5 commit 0870338
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 183 deletions.
12 changes: 6 additions & 6 deletions Integrations/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ services:
- 29092
- 8081

postgres:
extends:
file: ../postgres/docker-compose.yml
service: postgres
expose:
- 5432
# postgres:
# extends:
# file: ../postgres/docker-compose.yml
# service: postgres
# expose:
# - 5432
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,17 @@ public Optional<Class<?>> visit(final LogicalTypeAnnotation.TimeLogicalTypeAnnot
@Override
public Optional<Class<?>> visit(
final LogicalTypeAnnotation.TimestampLogicalTypeAnnotation timestampLogicalType) {
final LogicalTypeAnnotation.TimeUnit unit = timestampLogicalType.getUnit();
final boolean isAdjustedToUTC = timestampLogicalType.isAdjustedToUTC();
if (unit != LogicalTypeAnnotation.TimeUnit.MILLIS && unit != LogicalTypeAnnotation.TimeUnit.MICROS
&& unit != LogicalTypeAnnotation.TimeUnit.NANOS) {
errorString.setValue("TimestampLogicalType, isAdjustedToUTC=" + isAdjustedToUTC + ", unit=" + unit);
return Optional.empty();
switch (timestampLogicalType.getUnit()) {
case MILLIS:
case MICROS:
case NANOS:
// TIMESTAMP fields if adjusted to UTC are read as Instants, else as LocalDatetimes.
return timestampLogicalType.isAdjustedToUTC() ? Optional.of(Instant.class)
: Optional.of(LocalDateTime.class);
}
return isAdjustedToUTC ? Optional.of(Instant.class) : Optional.of(LocalDateTime.class);
errorString.setValue("TimestampLogicalType, isAdjustedToUTC=" + timestampLogicalType.isAdjustedToUTC()
+ ", unit=" + timestampLogicalType.getUnit());
return Optional.empty();
}

@Override
Expand Down
178 changes: 89 additions & 89 deletions py/server/tests/test_dbc.py
Original file line number Diff line number Diff line change
@@ -1,90 +1,90 @@
# #
# # Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending
# #
# import platform
# import unittest
#
# Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending
#
import platform
import unittest

import turbodbc

from deephaven import DHError, read_sql
from deephaven.dbc import odbc as dhodbc, adbc as dhadbc
from tests.testbase import BaseTestCase


# noinspection SqlDialectInspection
class DbcTestCase(BaseTestCase):

def test_read_odbc(self):
connection_string = 'Driver={PostgreSQL};Server=postgres;Port=5432;Database=test;Uid=test;Pwd=test;'
with turbodbc.connect(connection_string=connection_string) as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT t_ts, t_id, t_instrument, t_exchange, t_price, t_size FROM CRYPTO_TRADES")
table = dhodbc.read_cursor(cursor)
self.assertEqual(table.size, cursor.rowcount)

def test_read_adbc(self):
import adbc_driver_postgresql.dbapi

uri = "postgresql://postgres:5432/test?user=test&password=test"
with adbc_driver_postgresql.dbapi.connect(uri) as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT t_ts, t_exchange, t_price, t_size FROM CRYPTO_TRADES LIMIT 10")
table = dhadbc.read_cursor(cursor)
# This is not ideal but ADBC might have a bug regarding cursor.rowcount it currently returns -1
# instead of the actual size
self.assertEqual(table.size, 10)

@unittest.skipIf(platform.machine() != "x86_64", reason="connectorx not available on Linux/Arm64"
"https://github.com/sfu-db/connector-x/issues/386")
def test_read_sql_connectorx(self):
query = "SELECT t_ts, t_id, t_instrument, t_exchange, t_price, t_size FROM CRYPTO_TRADES LIMIT 10"
postgres_url = "postgresql://test:test@postgres:5432/test"
dh_table = read_sql(conn=postgres_url, query=query)
self.assertEqual(len(dh_table.columns), 6)
self.assertEqual(dh_table.size, 10)

with self.assertRaises(DHError) as cm:
dh_table = read_sql(conn="garbage", query=query)

def test_read_sql(self):
query = "SELECT t_ts, t_id, t_instrument, t_exchange, t_price, t_size FROM CRYPTO_TRADES LIMIT 10"

with self.subTest("odbc"):
connection_string = 'Driver={PostgreSQL};Server=postgres;Port=5432;Database=test;Uid=test;Pwd=test;'
dh_table = read_sql(conn=connection_string, query=query, driver="odbc")
self.assertEqual(len(dh_table.columns), 6)
self.assertEqual(dh_table.size, 10)

with self.subTest("adbc"):
uri = "postgresql://postgres:5432/test?user=test&password=test"
dh_table = read_sql(conn=uri, query=query, driver="adbc")
self.assertEqual(len(dh_table.columns), 6)
self.assertEqual(dh_table.size, 10)

with self.subTest("odbc-connection"):
connection_string = 'Driver={PostgreSQL};Server=postgres;Port=5432;Database=test;Uid=test;Pwd=test;'
with turbodbc.connect(connection_string=connection_string) as conn:
dh_table = read_sql(conn=conn, query=query, driver="odbc")
self.assertEqual(len(dh_table.columns), 6)
self.assertEqual(dh_table.size, 10)

with self.subTest("adbc-connection"):
import adbc_driver_postgresql.dbapi
uri = "postgresql://postgres:5432/test?user=test&password=test"
with adbc_driver_postgresql.dbapi.connect(uri) as conn:
dh_table = read_sql(conn=conn, query=query, driver="adbc")
self.assertEqual(len(dh_table.columns), 6)
self.assertEqual(dh_table.size, 10)

with self.assertRaises(DHError) as cm:
dh_table = read_sql(conn=[None], query=query, driver="adbc")

with self.assertRaises(DHError) as cm:
dh_table = read_sql(conn="garbage", query=query, driver="adbc")

with self.assertRaises(Exception) as cm:
dh_table = read_sql(conn="garbage", query=query, driver="odbc")


if __name__ == '__main__':
unittest.main()
# import turbodbc
#
# from deephaven import DHError, read_sql
# from deephaven.dbc import odbc as dhodbc, adbc as dhadbc
# from tests.testbase import BaseTestCase
#
#
# # noinspection SqlDialectInspection
# class DbcTestCase(BaseTestCase):
#
# def test_read_odbc(self):
# connection_string = 'Driver={PostgreSQL};Server=postgres;Port=5432;Database=test;Uid=test;Pwd=test;'
# with turbodbc.connect(connection_string=connection_string) as conn:
# with conn.cursor() as cursor:
# cursor.execute("SELECT t_ts, t_id, t_instrument, t_exchange, t_price, t_size FROM CRYPTO_TRADES")
# table = dhodbc.read_cursor(cursor)
# self.assertEqual(table.size, cursor.rowcount)
#
# def test_read_adbc(self):
# import adbc_driver_postgresql.dbapi
#
# uri = "postgresql://postgres:5432/test?user=test&password=test"
# with adbc_driver_postgresql.dbapi.connect(uri) as conn:
# with conn.cursor() as cursor:
# cursor.execute("SELECT t_ts, t_exchange, t_price, t_size FROM CRYPTO_TRADES LIMIT 10")
# table = dhadbc.read_cursor(cursor)
# # This is not ideal but ADBC might have a bug regarding cursor.rowcount it currently returns -1
# # instead of the actual size
# self.assertEqual(table.size, 10)
#
# @unittest.skipIf(platform.machine() != "x86_64", reason="connectorx not available on Linux/Arm64"
# "https://github.com/sfu-db/connector-x/issues/386")
# def test_read_sql_connectorx(self):
# query = "SELECT t_ts, t_id, t_instrument, t_exchange, t_price, t_size FROM CRYPTO_TRADES LIMIT 10"
# postgres_url = "postgresql://test:test@postgres:5432/test"
# dh_table = read_sql(conn=postgres_url, query=query)
# self.assertEqual(len(dh_table.columns), 6)
# self.assertEqual(dh_table.size, 10)
#
# with self.assertRaises(DHError) as cm:
# dh_table = read_sql(conn="garbage", query=query)
#
# def test_read_sql(self):
# query = "SELECT t_ts, t_id, t_instrument, t_exchange, t_price, t_size FROM CRYPTO_TRADES LIMIT 10"
#
# with self.subTest("odbc"):
# connection_string = 'Driver={PostgreSQL};Server=postgres;Port=5432;Database=test;Uid=test;Pwd=test;'
# dh_table = read_sql(conn=connection_string, query=query, driver="odbc")
# self.assertEqual(len(dh_table.columns), 6)
# self.assertEqual(dh_table.size, 10)
#
# with self.subTest("adbc"):
# uri = "postgresql://postgres:5432/test?user=test&password=test"
# dh_table = read_sql(conn=uri, query=query, driver="adbc")
# self.assertEqual(len(dh_table.columns), 6)
# self.assertEqual(dh_table.size, 10)
#
# with self.subTest("odbc-connection"):
# connection_string = 'Driver={PostgreSQL};Server=postgres;Port=5432;Database=test;Uid=test;Pwd=test;'
# with turbodbc.connect(connection_string=connection_string) as conn:
# dh_table = read_sql(conn=conn, query=query, driver="odbc")
# self.assertEqual(len(dh_table.columns), 6)
# self.assertEqual(dh_table.size, 10)
#
# with self.subTest("adbc-connection"):
# import adbc_driver_postgresql.dbapi
# uri = "postgresql://postgres:5432/test?user=test&password=test"
# with adbc_driver_postgresql.dbapi.connect(uri) as conn:
# dh_table = read_sql(conn=conn, query=query, driver="adbc")
# self.assertEqual(len(dh_table.columns), 6)
# self.assertEqual(dh_table.size, 10)
#
# with self.assertRaises(DHError) as cm:
# dh_table = read_sql(conn=[None], query=query, driver="adbc")
#
# with self.assertRaises(DHError) as cm:
# dh_table = read_sql(conn="garbage", query=query, driver="adbc")
#
# with self.assertRaises(Exception) as cm:
# dh_table = read_sql(conn="garbage", query=query, driver="odbc")
#
#
# if __name__ == '__main__':
# unittest.main()
162 changes: 81 additions & 81 deletions py/server/tests/test_sql.py
Original file line number Diff line number Diff line change
@@ -1,82 +1,82 @@
# #
# # Copyright (c) 2016-2023 Deephaven Data Labs and Patent Pending
# #
#
# Copyright (c) 2016-2023 Deephaven Data Labs and Patent Pending
#

import jpy

from deephaven import read_csv, empty_table
from deephaven.execution_context import ExecutionContext
from deephaven.experimental import sql
from tests.testbase import BaseTestCase
from test_helper import py_dh_session

_JTableSpec = jpy.get_type("io.deephaven.qst.table.TableSpec")

with ExecutionContext(j_exec_ctx=py_dh_session.getExecutionContext()):
some_global_table = empty_table(42)


class SqlTest(BaseTestCase):
def setUp(self):
super().setUp()

def tearDown(self):
super().tearDown()

def test_eval_global(self):
result_table = sql.evaluate("SELECT * FROM some_global_table")
self.assertEqual(result_table.size, 42)

def test_eval_local(self):
# noinspection PyUnusedLocal
test_table = read_csv("tests/data/test_table.csv")
result_table = sql.evaluate("SELECT * FROM test_table LIMIT 5")
self.assertEqual(result_table.size, 5)

def test_dry_run_global(self):
result_spec = sql.evaluate("SELECT * FROM some_global_table", dry_run=True)
self.assertTrue(isinstance(result_spec, jpy.JType))
# Might be nice to extend jpy like this in the future?
# self.assertTrue(isinstance(result_spec, _JTableSpec))
self.assertTrue(_JTableSpec.jclass.isInstance(result_spec))

def test_dry_run_local(self):
# noinspection PyUnusedLocal
test_table = read_csv("tests/data/test_table.csv")
result_spec = sql.evaluate("SELECT * FROM test_table LIMIT 7", dry_run=True)
self.assertTrue(isinstance(result_spec, jpy.JType))
# Might be nice to extend jpy like this in the future?
# self.assertTrue(isinstance(result_spec, _JTableSpec))
self.assertTrue(_JTableSpec.jclass.isInstance(result_spec))

def test_local_takes_precedence(self):
# noinspection PyUnusedLocal,PyShadowingNames
some_global_table = empty_table(13)
result_table = sql.evaluate("SELECT * FROM some_global_table")
self.assertEqual(result_table.size, 13)

def test_nested_non_local(self):
# noinspection PyUnusedLocal
test_table = read_csv("tests/data/test_table.csv")

def do_sql():
nonlocal test_table
return sql.evaluate("SELECT * FROM test_table LIMIT 3")

result_table = do_sql()
self.assertEqual(result_table.size, 3)

def test_current_timestamp(self):
result_table = sql.evaluate("SELECT CURRENT_TIMESTAMP")
self.assertEqual(result_table.size, 1)

def test_inner_func_local(self):
def inner_func(my_table):
return sql.evaluate("SELECT * FROM my_table LIMIT 13")

result_table = inner_func(some_global_table)
self.assertEqual(result_table.size, 13)


if __name__ == "__main__":
unittest.main()
# import jpy
#
# from deephaven import read_csv, empty_table
# from deephaven.execution_context import ExecutionContext
# from deephaven.experimental import sql
# from tests.testbase import BaseTestCase
# from test_helper import py_dh_session
#
# _JTableSpec = jpy.get_type("io.deephaven.qst.table.TableSpec")
#
# with ExecutionContext(j_exec_ctx=py_dh_session.getExecutionContext()):
# some_global_table = empty_table(42)
#
#
# class SqlTest(BaseTestCase):
# def setUp(self):
# super().setUp()
#
# def tearDown(self):
# super().tearDown()
#
# def test_eval_global(self):
# result_table = sql.evaluate("SELECT * FROM some_global_table")
# self.assertEqual(result_table.size, 42)
#
# def test_eval_local(self):
# # noinspection PyUnusedLocal
# test_table = read_csv("tests/data/test_table.csv")
# result_table = sql.evaluate("SELECT * FROM test_table LIMIT 5")
# self.assertEqual(result_table.size, 5)
#
# def test_dry_run_global(self):
# result_spec = sql.evaluate("SELECT * FROM some_global_table", dry_run=True)
# self.assertTrue(isinstance(result_spec, jpy.JType))
# # Might be nice to extend jpy like this in the future?
# # self.assertTrue(isinstance(result_spec, _JTableSpec))
# self.assertTrue(_JTableSpec.jclass.isInstance(result_spec))
#
# def test_dry_run_local(self):
# # noinspection PyUnusedLocal
# test_table = read_csv("tests/data/test_table.csv")
# result_spec = sql.evaluate("SELECT * FROM test_table LIMIT 7", dry_run=True)
# self.assertTrue(isinstance(result_spec, jpy.JType))
# # Might be nice to extend jpy like this in the future?
# # self.assertTrue(isinstance(result_spec, _JTableSpec))
# self.assertTrue(_JTableSpec.jclass.isInstance(result_spec))
#
# def test_local_takes_precedence(self):
# # noinspection PyUnusedLocal,PyShadowingNames
# some_global_table = empty_table(13)
# result_table = sql.evaluate("SELECT * FROM some_global_table")
# self.assertEqual(result_table.size, 13)
#
# def test_nested_non_local(self):
# # noinspection PyUnusedLocal
# test_table = read_csv("tests/data/test_table.csv")
#
# def do_sql():
# nonlocal test_table
# return sql.evaluate("SELECT * FROM test_table LIMIT 3")
#
# result_table = do_sql()
# self.assertEqual(result_table.size, 3)
#
# def test_current_timestamp(self):
# result_table = sql.evaluate("SELECT CURRENT_TIMESTAMP")
# self.assertEqual(result_table.size, 1)
#
# def test_inner_func_local(self):
# def inner_func(my_table):
# return sql.evaluate("SELECT * FROM my_table LIMIT 13")
#
# result_table = inner_func(some_global_table)
# self.assertEqual(result_table.size, 13)
#
#
# if __name__ == "__main__":
# unittest.main()

0 comments on commit 0870338

Please sign in to comment.