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

hand-wrapped the datetimeutils module, fixes #1640 #1812

Merged
merged 12 commits into from
Jan 31, 2022
6 changes: 3 additions & 3 deletions pyintegration/deephaven2/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
#
from dataclasses import dataclass, field
from enum import Enum
from typing import Sequence
from typing import Sequence, Any

import jpy
from deephaven2 import DHError
from deephaven2.dtypes import DType

import deephaven2.dtypes as dtypes
from deephaven2.dtypes import DType
from deephaven2 import DHError

_JColumnHeader = jpy.get_type("io.deephaven.qst.column.header.ColumnHeader")
_JColumn = jpy.get_type("io.deephaven.qst.column.Column")
Expand Down
58 changes: 56 additions & 2 deletions pyintegration/deephaven2/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from enum import Enum, auto
import jpy

_JQueryConstants = jpy.get_type("io.deephaven.util.QueryConstants")
_JTimeZone = jpy.get_type("io.deephaven.time.TimeZone")


class SortDirection(Enum):
"""An enum defining the sorting orders."""
Expand All @@ -15,8 +18,6 @@ class SortDirection(Enum):


# Deephaven Special Null values for primitive types
_JQueryConstants = jpy.get_type("io.deephaven.util.QueryConstants")

NULL_BOOLEAN = _JQueryConstants.NULL_BOOLEAN
""" Null boolean value. """
NULL_CHAR = _JQueryConstants.NULL_CHAR
Expand Down Expand Up @@ -92,3 +93,56 @@ class SortDirection(Enum):
MIN_POS_DOUBLE = _JQueryConstants.MIN_POS_DOUBLE
""" Minimum positive value of type double. """


class TimeZone(Enum):
jmao-denver marked this conversation as resolved.
Show resolved Hide resolved
jmao-denver marked this conversation as resolved.
Show resolved Hide resolved
""" A Enum for known time zones. """
NY = _JTimeZone.TZ_NY
""" America/New_York """
ET = _JTimeZone.TZ_ET
""" America/New_York """
MN = _JTimeZone.TZ_MN
""" America/Chicago """
CT = _JTimeZone.TZ_CT
""" America/Chicago """
MT = _JTimeZone.TZ_MT
""" America/Denver """
PT = _JTimeZone.TZ_PT
""" America/Los_Angeles """
HI = _JTimeZone.TZ_HI
""" Pacific/Honolulu """
BT = _JTimeZone.TZ_BT
""" America/Sao_Paulo """
KR = _JTimeZone.TZ_KR
""" Asia/Seoul """
HK = _JTimeZone.TZ_HK
""" Asia/Hong_Kong """
JP = _JTimeZone.TZ_JP
""" Asia/Tokyo """
AT = _JTimeZone.TZ_AT
""" Canada/Atlantic """
NF = _JTimeZone.TZ_NF
""" Canada/Newfoundland """
AL = _JTimeZone.TZ_AL
""" America/Anchorage """
IN = _JTimeZone.TZ_IN
""" Asia/Kolkata """
CE = _JTimeZone.TZ_CE
""" Europe/Berlin """
SG = _JTimeZone.TZ_SG
""" Asia/Singapore """
LON = _JTimeZone.TZ_LON
""" Europe/London """
MOS = _JTimeZone.TZ_MOS
""" Europe/Moscow """
SHG = _JTimeZone.TZ_SHG
""" Asia/Shanghai """
CH = _JTimeZone.TZ_CH
""" Europe/Zurich """
NL = _JTimeZone.TZ_NL
""" Europe/Amsterdam """
TW = _JTimeZone.TZ_TW
""" Asia/Taipei """
SYD = _JTimeZone.TZ_SYD
""" Australia/Sydney """
UTC = _JTimeZone.TZ_UTC
""" UTC """
24 changes: 11 additions & 13 deletions pyintegration/deephaven2/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
"""
from __future__ import annotations

from typing import Iterable, Any, Tuple, Sequence, Callable
from typing import Any, Sequence, Callable, Iterable

import numpy as np
import pandas as pd
import jpy

from deephaven2 import DHError
from deephaven2.constants import *

_JQstType = jpy.get_type("io.deephaven.qst.type.Type")
_JTableTools = jpy.get_type("io.deephaven.engine.util.TableTools")
Expand All @@ -26,15 +23,16 @@ def _qst_custom_type(cls_name: str):

class DType:
""" A class representing a data type in Deephaven."""
_j_name_map = {}

_j_name_type_map = {}

@classmethod
def from_jtype(cls, j_class: Any) -> DType:
if not j_class:
return None

j_name = j_class.getName()
dtype = DType._j_name_map.get(j_name)
dtype = DType._j_name_type_map.get(j_name)
if not dtype:
return cls(j_name=j_name, j_type=j_class)
else:
Expand All @@ -46,13 +44,16 @@ def __init__(self, j_name: str, j_type: Any = None, qst_type: Any = None, is_pri
self.qst_type = qst_type if qst_type else _qst_custom_type(j_name)
self.is_primitive = is_primitive

DType._j_name_map[j_name] = self
DType._j_name_type_map[j_name] = self

def __repr__(self):
return self.j_name

def __call__(self, *args, **kwargs):
return self.j_type(*args, **kwargs)
try:
return self.j_type(*args, **kwargs)
except Exception as e:
raise DHError(e, f"failed to create an instance of {self.j_name}") from e

def array(self, size: int):
""" Creates a Java array of the same data type of the specified size.
Expand Down Expand Up @@ -111,7 +112,6 @@ def array_from(self, seq: Sequence, remap: Callable[[Any], Any] = None):
return super().array_from(seq, remap)


# region predefined types and aliases
bool_ = DType(j_name="java.lang.Boolean", qst_type=_JQstType.booleanType())
byte = DType(j_name="byte", qst_type=_JQstType.byteType(), is_primitive=True)
int8 = byte
Expand All @@ -130,14 +130,12 @@ def array_from(self, seq: Sequence, remap: Callable[[Any], Any] = None):
string = DType(j_name="java.lang.String", qst_type=_JQstType.stringType())
BigDecimal = DType(j_name="java.math.BigDecimal")
StringSet = DType(j_name="io.deephaven.stringset.StringSet")
DateTime = DType(j_name="io.deephaven.time.DateTime")
Period = DType(j_name="io.deephaven.time.Period")
PyObject = DType(j_name="org.jpy.PyObject")
JObject = DType(j_name="java.lang.Object")
DateTime = DType(j_name="io.deephaven.time.DateTime")
Period = DType(j_name="io.deephaven.time.Period")


# endregion

def j_array_list(values: Iterable):
j_list = jpy.get_type("java.util.ArrayList")(len(values))
try:
Expand Down
24 changes: 24 additions & 0 deletions pyintegration/deephaven2/hmtl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
jmao-denver marked this conversation as resolved.
Show resolved Hide resolved
# Copyright (c) 2016-2021 Deephaven Data Labs and Patent Pending
#
import jpy
jmao-denver marked this conversation as resolved.
Show resolved Hide resolved

from deephaven2 import DHError
from deephaven2.table import Table

_JTableTools = jpy.get_type("io.deephaven.engine.util.TableTools")


def to_html(table: Table) -> str:
""" Returns a printout of a table formatted as HTML. Limit use to small tables to avoid running out of memory.

Returns:
a String of the table printout formatted as HTML

Raises:
DHError
"""
jmao-denver marked this conversation as resolved.
Show resolved Hide resolved
try:
return _JTableTools.html(table.j_table)
except Exception as e:
raise DHError(e, "table to_html failed") from e
10 changes: 0 additions & 10 deletions pyintegration/deephaven2/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,6 @@ def to_string(self, num_rows: int = 10, cols: List[str] = []) -> str:
except Exception as e:
raise DHError(e, "table to_string failed") from e

def to_html(self):
"""
Returns a printout of a table formatted as HTML. Limit use to small tables to avoid running out of memory.

:param source: (io.deephaven.db.tables.Table) - a Deephaven table object
:return: (java.lang.String) a String of the table printout formatted as HTML
"""

return _JTableTools.html(self.j_table)

def coalesce(self) -> Table:
""" Returns a coalesced child table. """
return Table(j_table=self.j_table.coalesce())
Expand Down
21 changes: 15 additions & 6 deletions pyintegration/deephaven2/table_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import jpy

from deephaven2.column import Column
from deephaven2 import DHError
from deephaven2.column import InputColumn
from deephaven2.table import Table

_JTableFactory = jpy.get_type("io.deephaven.engine.table.TableFactory")
Expand Down Expand Up @@ -55,8 +55,17 @@ def time_table(period: str, start_time: str = None) -> Table:
raise DHError(e, "failed to create a time table.") from e


def new_table(cols: List[Column]) -> Table:
""" Creates an in-memory table from a list of columns. Each column must have an equal number of elements.
def new_table(cols: List[InputColumn]) -> Table:
""" Creates an in-memory table from a list of input columns. Each column must have an equal number of elements.

Args:
cols (List[InputColumn]): a list of InputColumn

Returns:
a Table

Raises:
DHError
"""
try:
return Table(j_table=_JTableFactory.newTable(*[col.j_column for col in cols]))
Expand All @@ -72,7 +81,7 @@ def merge(tables: List[Table]):
tables (List[Table]): the source tables

Returns:
a new table
a Table

Raises:
DHError
Expand All @@ -83,7 +92,7 @@ def merge(tables: List[Table]):
raise DHError(e, "merge tables operation failed.") from e


def merge_sorted(tables: List[Table], order_by: str):
def merge_sorted(tables: List[Table], order_by: str) -> Table:
""" Combines two or more tables into one sorted, aggregate table. This essentially stacks the tables one on top
of the other and sorts the result. Null tables are ignored. mergeSorted is more efficient than using merge
followed by sort.
Expand All @@ -93,7 +102,7 @@ def merge_sorted(tables: List[Table], order_by: str):
order_by (str): the name of the key column

Returns:
a new table
a Table

Raises:
DHError
Expand Down
Loading