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

[python] add digest python function #1127

Merged
merged 3 commits into from
Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- name: Run Linters
run: |
source venv/bin/activate
flake8 python
flake8 python --ignore=E501
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about adding this to setup.cfg so it's applied in all scenarios including local development runs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd defer that to later until more options are used

black --line-length 79 --check python
- name: Run tests
run: |
Expand Down
13 changes: 13 additions & 0 deletions python/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ fn random() -> expression::Expression {
}
}

/// Computes a binary hash of the given data. type is the algorithm to use.
/// Standard algorithms are md5, sha224, sha256, sha384, sha512, blake2s, blake2b, and blake3.
#[pyfunction(value, method)]
fn digest(
value: expression::Expression,
method: expression::Expression,
) -> expression::Expression {
expression::Expression {
expr: logical_plan::digest(value.expr, method.expr),
}
}

/// Concatenates the text representations of all the arguments.
/// NULL arguments are ignored.
#[pyfunction(args = "*")]
Expand Down Expand Up @@ -340,6 +352,7 @@ pub fn init(module: &PyModule) -> PyResult<()> {
module.add_function(wrap_pyfunction!(ltrim, module)?)?;
module.add_function(wrap_pyfunction!(max, module)?)?;
module.add_function(wrap_pyfunction!(md5, module)?)?;
module.add_function(wrap_pyfunction!(digest, module)?)?;
module.add_function(wrap_pyfunction!(min, module)?)?;
module.add_function(wrap_pyfunction!(now, module)?)?;
module.add_function(wrap_pyfunction!(octet_length, module)?)?;
Expand Down
72 changes: 72 additions & 0 deletions python/tests/test_string_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,75 @@ def test_string_functions(df):
]
)
assert result.column(1) == pa.array(["hello", "world", "!"])


def test_hash_functions(df):
df = df.select(
*[
f.digest(f.col("a"), f.lit(m))
for m in ("md5", "sha256", "sha512", "blake2s", "blake3")
]
)
result = df.collect()
assert len(result) == 1
result = result[0]
b = bytearray.fromhex
assert result.column(0) == pa.array(
[
b("8B1A9953C4611296A827ABF8C47804D7"),
b("F5A7924E621E84C9280A9A27E1BCB7F6"),
b("9033E0E305F247C0C3C80D0C7848C8B3"),
]
)
assert result.column(1) == pa.array(
[
b(
"185F8DB32271FE25F561A6FC938B2E264306EC304EDA518007D1764826381969"
),
b(
"78AE647DC5544D227130A0682A51E30BC7777FBB6D8A8F17007463A3ECD1D524"
),
b(
"BB7208BC9B5D7C04F1236A82A0093A5E33F40423D5BA8D4266F7092C3BA43B62"
),
]
)
assert result.column(2) == pa.array(
[
b(
"3615F80C9D293ED7402687F94B22D58E529B8CC7916F8FAC7FDDF7FBD5AF4CF777D3D795A7A00A16BF7E7F3FB9561EE9BAAE480DA9FE7A18769E71886B03F315"
),
b(
"8EA77393A42AB8FA92500FB077A9509CC32BC95E72712EFA116EDAF2EDFAE34FBB682EFDD6C5DD13C117E08BD4AAEF71291D8AACE2F890273081D0677C16DF0F"
),
b(
"3831A6A6155E509DEE59A7F451EB35324D8F8F2DF6E3708894740F98FDEE23889F4DE5ADB0C5010DFB555CDA77C8AB5DC902094C52DE3278F35A75EBC25F093A"
),
]
)
assert result.column(3) == pa.array(
[
b(
"F73A5FBF881F89B814871F46E26AD3FA37CB2921C5E8561618639015B3CCBB71"
),
b(
"B792A0383FB9E7A189EC150686579532854E44B71AC394831DAED169BA85CCC5"
),
b(
"27988A0E51812297C77A433F635233346AEE29A829DCF4F46E0F58F402C6CFCB"
),
]
)
assert result.column(4) == pa.array(
[
b(
"FBC2B0516EE8744D293B980779178A3508850FDCFE965985782C39601B65794F"
),
b(
"BF73D18575A736E4037D45F9E316085B86C19BE6363DE6AA789E13DEAACC1C4E"
),
b(
"C8D11B9F7237E4034ADBCD2005735F9BC4C597C75AD89F4492BEC8F77D15F7EB"
),
]
)