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

[FLINK-9853] [table] Add HEX support #6337

Closed
wants to merge 12 commits into from
Closed

[FLINK-9853] [table] Add HEX support #6337

wants to merge 12 commits into from

Conversation

xueyumusic
Copy link
Contributor

What is the purpose of the change

This PR propose to add HEX in table api and sql, the syntax like mysql, which could take int or string arguments. For a integer argument N, it returns a hexadecimal string representation of the value of N. For a string argument str, it returns a hexadecimal string representation of str where each byte of each character in str is converted to two hexadecimal digits.

Syntax:

HEX(100) = 64

HEX('This is a test String.') = '546869732069732061207465737420537472696e672e'

Brief change log

  • The expressionDsl, scalarSqlFunctions and mathExpressions to add hex
  • The FunctionGenerator to support hex generator

Verifying this change

This change added tests and can be verified as follows:

(example:)

  • Added ScalaFunctionTests tests for table api and sql expressions

Does this pull request potentially affect one of the following parts:

  • Dependencies (does it add or upgrade a dependency): (no)
  • The public API, i.e., is any changed class annotated with @Public(Evolving): (no)
  • The serializers: (no)
  • The runtime per-record code paths (performance sensitive): (no)
  • Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Yarn/Mesos, ZooKeeper: (no)
  • The S3 file system connector: (no)

Documentation

  • Does this pull request introduce a new feature? (yes)
  • If yes, how is the feature documented? (docs)

Copy link
Contributor

@yanghua yanghua left a comment

Choose a reason for hiding this comment

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

some little suggestion

@@ -400,6 +400,12 @@ trait ImplicitExpressionOperations {
* numeric is null. E.g. "4" leads to "100", "12" leads to "1100".
*/
def bin() = Bin(expr)
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

please insert a new blank line

@@ -182,4 +184,6 @@ object ScalarFunctions {

new String(data)
}

def hex(x: String): String = Hex.encodeHexString(x.getBytes)
Copy link
Contributor

Choose a reason for hiding this comment

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

add the doc for the API looks better to me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for review, @yanghua , fixed

@yanghua
Copy link
Contributor

yanghua commented Jul 16, 2018

+1, from my side

@hequn8128
Copy link
Contributor

Thanks for your PR. +1 to merge.

@twalthr twalthr changed the title [FLINK-9853][Tabel API & SQL] add HEX support [FLINK-9853] [table] add HEX support Jul 31, 2018
@twalthr twalthr changed the title [FLINK-9853] [table] add HEX support [FLINK-9853] [table] Add HEX support Jul 31, 2018
Copy link
Contributor

@walterddr walterddr left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution @xueyumusic.

I am a bit confuse regarding the documentation. Seems like both "20" and 20L are valid input to the HEX function. It's worth to point out the difference IMO, especially they return different results: e.g.
10.hex() gives "a" and "10".hex() gives "3130"

+1 to merge once documentation is fixed.

{% endhighlight %}
</td>
<td>
<p>Returns a string representation of an integer numeric value or a string in hex format. Returns null if numeric is null. E.g. "20" leads to "14", "100" leads to "64", "hello,world" leads to "68656c6c6f2c776f726c64".</p>
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe consider using backtick for the number literals? e.g. "20" --> `20`.

Otherwise its confusing whether we should pass in stringify integer or just integer values.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @walterddr , I clarify the doc adding explicit explain for numeric or for string

Copy link
Contributor

Choose a reason for hiding this comment

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

great change and explanations. actually my point was to have "20" --> `20` with specifically the backtick. since backtick quoted strings gets format differently in Flink doc page and it will be immediately clear that this is a numeral literal.

{% endhighlight %}
</td>
<td>
<p>Returns a string representation of an integer numeric value or a string in hex format. Returns null if numeric is null. E.g. "20" leads to "14", "100" leads to "64", "hello,world" leads to "68656c6c6f2c776f726c64".</p>
Copy link
Contributor

Choose a reason for hiding this comment

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

same here.

@@ -406,6 +406,13 @@ trait ImplicitExpressionOperations {
*/
def bin() = Bin(expr)

/**
* Returns a string representation of an integer numeric value in hex format. Returns null if
* numeric is null. E.g. "20" leads to "14", "100" leads to "64", "hello,world" leads to
Copy link
Contributor

Choose a reason for hiding this comment

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

just remove the " if it is numeric

Copy link
Contributor

@twalthr twalthr left a comment

Choose a reason for hiding this comment

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

Thank you @xueyumusic. I added one comment about the result string.

override private[flink] def resultType: TypeInformation[_] = BasicTypeInfo.STRING_TYPE_INFO

override private[flink] def validateInput(): ValidationResult = child.resultType match {
case _: IntegerTypeInfo[_] =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Rely on TypeCheckUtils.isIntegerFamily and TypeCheckUtils#isString instead.

'f2.hex(),
"f2.hex()",
"HEX(f2)",
"2a")
Copy link
Contributor

Choose a reason for hiding this comment

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

MySQL returns upper-case letters. Should we do this as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we should do it as well. Thanks @twalthr , I updated the code

@twalthr
Copy link
Contributor

twalthr commented Aug 14, 2018

Thank you for the quick update @xueyumusic. I will merge this.

twalthr pushed a commit to twalthr/flink that referenced this pull request Aug 14, 2018
twalthr pushed a commit to twalthr/flink that referenced this pull request Aug 14, 2018
@asfgit asfgit closed this in ab1d1df Aug 14, 2018
@twalthr
Copy link
Contributor

twalthr commented Aug 14, 2018

@xueyumusic for future PRs use git rebase master YOURBRANCH instead of merge commits. Thank you.

Clarkkkkk pushed a commit to Clarkkkkk/flink that referenced this pull request Mar 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants