-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Cryptographic hash functions (#788)
* Implement Cryptographic hash functions Signed-off-by: Gokul R <[email protected]> * update documentation Signed-off-by: Gokul R <[email protected]> * added integration tests and updated readme file Signed-off-by: Gokul R <[email protected]> * format the code Signed-off-by: Gokul R <[email protected]> * fix integration tests Signed-off-by: Gokul R <[email protected]> --------- Signed-off-by: Gokul R <[email protected]> Signed-off-by: Gokul-Radhakrishnan <[email protected]>
- Loading branch information
1 parent
b09a6e3
commit 0b6da30
Showing
9 changed files
with
211 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
## PPL Cryptographic Functions | ||
|
||
### `MD5` | ||
|
||
**Description** | ||
|
||
Calculates the MD5 digest and returns the value as a 32 character hex string. | ||
|
||
Usage: `md5('hello')` | ||
|
||
**Argument type:** | ||
- STRING | ||
- Return type: **STRING** | ||
|
||
Example: | ||
|
||
os> source=people | eval `MD5('hello')` = MD5('hello') | fields `MD5('hello')` | ||
fetched rows / total rows = 1/1 | ||
+----------------------------------+ | ||
| MD5('hello') | | ||
|----------------------------------| | ||
| 5d41402abc4b2a76b9719d911017c592 | | ||
+----------------------------------+ | ||
|
||
### `SHA1` | ||
|
||
**Description** | ||
|
||
Returns the hex string result of SHA-1 | ||
|
||
Usage: `sha1('hello')` | ||
|
||
**Argument type:** | ||
- STRING | ||
- Return type: **STRING** | ||
|
||
Example: | ||
|
||
os> source=people | eval `SHA1('hello')` = SHA1('hello') | fields `SHA1('hello')` | ||
fetched rows / total rows = 1/1 | ||
+------------------------------------------+ | ||
| SHA1('hello') | | ||
|------------------------------------------| | ||
| aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d | | ||
+------------------------------------------+ | ||
|
||
### `SHA2` | ||
|
||
**Description** | ||
|
||
Returns the hex string result of SHA-2 family of hash functions (SHA-224, SHA-256, SHA-384, and SHA-512). The numBits indicates the desired bit length of the result, which must have a value of 224, 256, 384, 512 | ||
|
||
Usage: `sha2('hello',256)` | ||
|
||
Usage: `sha2('hello',512)` | ||
|
||
**Argument type:** | ||
- STRING, INTEGER | ||
- Return type: **STRING** | ||
|
||
Example: | ||
|
||
os> source=people | eval `SHA2('hello',256)` = SHA2('hello',256) | fields `SHA2('hello',256)` | ||
fetched rows / total rows = 1/1 | ||
+------------------------------------------------------------------+ | ||
| SHA2('hello',256) | | ||
|------------------------------------------------------------------| | ||
| 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 | | ||
+------------------------------------------------------------------+ | ||
|
||
os> source=people | eval `SHA2('hello',512)` = SHA2('hello',512) | fields `SHA2('hello',512)` | ||
fetched rows / total rows = 1/1 | ||
+----------------------------------------------------------------------------------------------------------------------------------+ | ||
| SHA2('hello',512) | | ||
|----------------------------------------------------------------------------------------------------------------------------------| | ||
| 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043 | | ||
+----------------------------------------------------------------------------------------------------------------------------------+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
.../opensearch/flint/spark/ppl/PPLLogicalPlanCryptographicFunctionsTranslatorTestSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.spark.ppl | ||
|
||
import org.opensearch.flint.spark.ppl.PlaneUtils.plan | ||
import org.opensearch.sql.ppl.{CatalystPlanContext, CatalystQueryPlanVisitor} | ||
import org.opensearch.sql.ppl.utils.DataTypeTransformer.seq | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import org.apache.spark.SparkFunSuite | ||
import org.apache.spark.sql.catalyst.analysis.{UnresolvedAttribute, UnresolvedFunction, UnresolvedRelation, UnresolvedStar} | ||
import org.apache.spark.sql.catalyst.expressions.{Alias, EqualTo, GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual, Literal, Not} | ||
import org.apache.spark.sql.catalyst.plans.PlanTest | ||
import org.apache.spark.sql.catalyst.plans.logical.{Filter, Project} | ||
|
||
class PPLLogicalPlanCryptographicFunctionsTranslatorTestSuite | ||
extends SparkFunSuite | ||
with PlanTest | ||
with LogicalPlanTestUtils | ||
with Matchers { | ||
|
||
private val planTransformer = new CatalystQueryPlanVisitor() | ||
private val pplParser = new PPLSyntaxParser() | ||
|
||
test("test md5") { | ||
val context = new CatalystPlanContext | ||
val logPlan = planTransformer.visit(plan(pplParser, "source=t a = md5(b)"), context) | ||
|
||
val table = UnresolvedRelation(Seq("t")) | ||
val filterExpr = EqualTo( | ||
UnresolvedAttribute("a"), | ||
UnresolvedFunction("md5", seq(UnresolvedAttribute("b")), isDistinct = false)) | ||
val filterPlan = Filter(filterExpr, table) | ||
val projectList = Seq(UnresolvedStar(None)) | ||
val expectedPlan = Project(projectList, filterPlan) | ||
comparePlans(expectedPlan, logPlan, false) | ||
} | ||
|
||
test("test sha1") { | ||
val context = new CatalystPlanContext | ||
val logPlan = planTransformer.visit(plan(pplParser, "source=t a = sha1(b)"), context) | ||
|
||
val table = UnresolvedRelation(Seq("t")) | ||
val filterExpr = EqualTo( | ||
UnresolvedAttribute("a"), | ||
UnresolvedFunction("sha1", seq(UnresolvedAttribute("b")), isDistinct = false)) | ||
val filterPlan = Filter(filterExpr, table) | ||
val projectList = Seq(UnresolvedStar(None)) | ||
val expectedPlan = Project(projectList, filterPlan) | ||
comparePlans(expectedPlan, logPlan, false) | ||
} | ||
|
||
test("test sha2") { | ||
val context = new CatalystPlanContext | ||
val logPlan = planTransformer.visit(plan(pplParser, "source=t a = sha2(b,256)"), context) | ||
|
||
val table = UnresolvedRelation(Seq("t")) | ||
val filterExpr = EqualTo( | ||
UnresolvedAttribute("a"), | ||
UnresolvedFunction("sha2", seq(UnresolvedAttribute("b"), Literal(256)), isDistinct = false)) | ||
val filterPlan = Filter(filterExpr, table) | ||
val projectList = Seq(UnresolvedStar(None)) | ||
val expectedPlan = Project(projectList, filterPlan) | ||
comparePlans(expectedPlan, logPlan, false) | ||
} | ||
} |