Skip to content

Commit

Permalink
feat: add db2 hash and concat support (#800)
Browse files Browse the repository at this point in the history
* feat: add db2 hash and concat support

* fix import error when db2 not installed

* fix: return lowercase hash

* chore: update README

* feat: add rand support for Db2
  • Loading branch information
ngdav committed Apr 14, 2023
1 parent b92eb91 commit c16e2f7
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ The [Examples](https://github.com/GoogleCloudPlatform/professional-services-data

#### Row Validations

(Note: Row hash validation is currently supported for BigQuery, Teradata, Impala/Hive, Oracle, SQL Server, Postgres, and Alloy DB. Struct and array data types are not currently supported.
(Note: Row hash validation is currently supported for BigQuery, Teradata, Impala/Hive, Oracle, SQL Server, Postgres, Db2 and Alloy DB. Struct and array data types are not currently supported.
In addition, please note that SHA256 is not a supported function on Teradata systems.
If you wish to perform this comparison on Teradata you will need to
[deploy a UDF to perform the conversion](https://github.com/akuroda/teradata-udf-sha2/blob/master/src/sha256.c).)
Expand Down
1 change: 1 addition & 0 deletions data_validation/query_builder/random_row_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
clients.OracleClient: "DBMS_RANDOM.VALUE",
PostgreSQLClient: "RANDOM()",
clients.MSSQLClient: "NEWID()",
clients.DB2Client: "RAND()",
}


Expand Down
1 change: 1 addition & 0 deletions third_party/ibis/ibis_DB2/alchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
dt.BIGINT: sa.BIGINT,
}
_ibis_type_to_sqla.update(s_al._ibis_type_to_sqla)
_ibis_type_to_sqla[dts.String] = sa.sql.sqltypes.String(length=3000)


def _to_sqla_type(itype, type_map=None):
Expand Down
3 changes: 2 additions & 1 deletion third_party/ibis/ibis_DB2/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,8 @@ def _mod(t, expr):

def _string_join(t, expr):
sep, elements = expr.op().args
return sa.func.concat(*map(t.translate, elements))
columns = [col.name for col in map(t.translate, elements)]
return sa.sql.literal_column(" || ".join(columns))


def _literal(t, expr):
Expand Down
2 changes: 1 addition & 1 deletion third_party/ibis/ibis_DB2/expr/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def name(value):
_TYPE_KEYS = tuple(_TYPE_RULES.keys())


class TypeParser_Oracle(dt.TypeParser):
class TypeParser_DB2(dt.TypeParser):
def type(self) -> dt.DataType:
if self._accept(Token_DB2.CLOB):
return CLOB()
Expand Down
17 changes: 17 additions & 0 deletions third_party/ibis/ibis_addon/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
from third_party.ibis.ibis_mssql.compiler import MSSQLExprTranslator
from ibis.backends.postgres.compiler import PostgreSQLExprTranslator

# avoid errors if Db2 is not installed and not needed
try:
from third_party.ibis.ibis_DB2.compiler import DB2ExprTranslator
except Exception:
DB2ExprTranslator = None


# from third_party.ibis.ibis_snowflake.compiler import SnowflakeExprTranslator
# from third_party.ibis.ibis_oracle.compiler import OracleExprTranslator <<<<<< DB2

Expand Down Expand Up @@ -213,6 +220,13 @@ def sa_format_hashbytes_oracle(translator, expr):
hash_func = sa.func.standard_hash(compiled_arg, sa.sql.literal_column("'SHA256'"))
return sa.func.lower(hash_func)

def sa_format_hashbytes_db2(translator, expr):
arg, how = expr.op().args
compiled_arg = translator.translate(arg)
hashfunc = sa.func.hash(compiled_arg,sa.sql.literal_column("2"))
hex = sa.func.hex(hashfunc)
return sa.func.lower(hex)

def sa_format_hashbytes_postgres(translator, expr):
arg, how = expr.op().args
compiled_arg = translator.translate(arg)
Expand Down Expand Up @@ -292,3 +306,6 @@ def sa_cast_postgres(t, expr):
PostgreSQLExprTranslator._registry[RawSQL] = sa_format_raw_sql
PostgreSQLExprTranslator._registry[ToChar] = sa_format_to_char
PostgreSQLExprTranslator._registry[Cast] = sa_cast_postgres

if DB2ExprTranslator: #check if Db2 driver is loaded
DB2ExprTranslator._registry[HashBytes] = sa_format_hashbytes_db2

0 comments on commit c16e2f7

Please sign in to comment.