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

Add typeof function. #123

Merged
merged 6 commits into from
Sep 29, 2022
Merged

Add typeof function. #123

merged 6 commits into from
Sep 29, 2022

Conversation

Yury-Fridlyand
Copy link

@Yury-Fridlyand Yury-Fridlyand commented Sep 20, 2022

Signed-off-by: Yury-Fridlyand [email protected]

Description

TYPEOF function is useful for debugging to check types of other functions.
I also fixed cast to DATETIME.

Usage

opensearchsql> SELECT typeof(CAST('1961-04-12 09:07:00' AS DATETIME)), typeof(CAST('1961-04-12 09:07:00' AS TIMESTAMP)), typeof(CAST('09:07:00' AS TIME)), typeof(CAST('1961-04-12' AS DATE));
Output longer than terminal width
Do you want to display data vertically for better visual effect? [y/N]: y
fetched rows / total rows = 1/1
-[ RECORD 1 ]-------------------------
typeof(CAST('1961-04-12 09:07:00' AS DATETIME))  | DATETIME
typeof(CAST('1961-04-12 09:07:00' AS TIMESTAMP)) | TIMESTAMP
typeof(CAST('09:07:00' AS TIME))                 | TIME
typeof(CAST('1961-04-12' AS DATE))               | DATE

Check List

  • New functionality includes testing.
    • All tests pass, including unit test, integration test and doctest
  • New functionality has been documented.
    • New functionality has javadoc added
    • New functionality has user manual doc added
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@codecov

This comment was marked as abuse.

Yury-Fridlyand and others added 3 commits September 23, 2022 16:29
…ions (opensearch-project#754)

* Add implementation of `now`, `sysdate`, `localtime` and similar functions (#92)

Signed-off-by: Yury-Fridlyand <[email protected]>

* Rework on `now` function implementation (#113)

Signed-off-by: Yury-Fridlyand <[email protected]>

* Minor SQL ANTLR clean-up.

Signed-off-by: Yury-Fridlyand <[email protected]>

Signed-off-by: Yury-Fridlyand <[email protected]>
…n-2.13.4

Update com.fasterxml.jackson to 2.13.4 to match opensearch repo.
@@ -60,6 +62,7 @@ public static void register(BuiltinFunctionRepository repository) {
repository.register(castToTime());
repository.register(castToTimestamp());
repository.register(castToDatetime());
repository.register(typeof());

Choose a reason for hiding this comment

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

Could you please move typeof into a separate class?

Copy link
Author

Choose a reason for hiding this comment

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

How can I call it? Is Misc OK?

Choose a reason for hiding this comment

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

TypeOfOperator seems right to me.

Copy link
Author

Choose a reason for hiding this comment

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

Fixed in 9296c62.

…ch-project#835)

* Add datetime functions `FROM_UNIXTIME` and `UNIX_TIMESTAMP` (#114)

* Add implementation for `FROM_UNIXTIME` and `UNIX_TIMESTAMP` functions, UT and IT.

Signed-off-by: Yury-Fridlyand <[email protected]>

* Collent all DateTime formatters into one place.

Signed-off-by: Yury-Fridlyand <[email protected]>

* Rename `DateFormatters` -> `DateTimeFormatters`.

Signed-off-by: Yury-Fridlyand <[email protected]>

Signed-off-by: Yury-Fridlyand <[email protected]>
Copy link

@acarbonetto acarbonetto left a comment

Choose a reason for hiding this comment

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

LGTM - just a few comments

@@ -361,4 +372,27 @@ void castToDatetime() {
assertEquals(new ExprDatetimeValue("2012-08-07 00:00:00"), expression.valueOf(null));
}

@Test
void typeof() {
assertEquals("UNDEFINED", typeofGetValue(ExprNullValue.of()));

Choose a reason for hiding this comment

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

is it possible to test UNKNOWN?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, see typeof(NULL) in integration tests

// Auxiliary function useful for debugging
private static DefaultFunctionResolver typeof() {
return FunctionDSL.define(BuiltinFunctionName.TYPEOF.getName(),
impl(TypeOfOperator::exprTypeOf, STRING, UNKNOWN),

Choose a reason for hiding this comment

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

nit: would be nice to have these in alphabetic order like the list above in the same file.

+ " | fields `datetime`, `timestamp`, `time`, `date`",
TEST_INDEX_DATATYPE_NUMERIC));
verifyDataRows(response,
rows("DATETIME", "TIMESTAMP", "TIME", "DATE"));

Choose a reason for hiding this comment

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

please add a test for complex data types, like ARRAY, STRUCT, and INTERVAL.

Copy link
Author

Choose a reason for hiding this comment

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

To test with ARRAY and STRUCT I have to update parser and AstBuilder/AstExpressionBuilder. I can't imagine where it could be used especially as a return value of another function.
typeof(somefunc(...)) -> STRUCT.

Signed-off-by: Yury-Fridlyand <[email protected]>
@Yury-Fridlyand Yury-Fridlyand merged commit 349bfcb into integ-add-typeof Sep 29, 2022
@Yury-Fridlyand Yury-Fridlyand deleted the dev-add-typeof branch September 29, 2022 03:49
return Stream.of(
Arguments.of(840101d, LocalDateTime.of(1984, 1, 1, 0, 0, 0)),
Arguments.of(840101112233d, LocalDateTime.of(1984, 1, 1, 11,22,33)),
Arguments.of(840101112233.123456, LocalDateTime.of(1984, 1, 1, 11, 22, 33, 123456000)),

Choose a reason for hiding this comment

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

do the argument.of's need a d at the end too?

Copy link
Author

Choose a reason for hiding this comment

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

d for Double, otherwise it is interpreted as int/long and test crashes, because the test method expect double as the first arg.

| value_1 | value_2 |
|----------------------------+----------------------------|
| 2022-08-02 15:39:05.173069 | 2022-08-02 15:39:05.173069 |
+----------------------------+----------------------------+

Choose a reason for hiding this comment

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

I get this result from mySQL for this query. Should NOW return with .nnnnnn?

mysql> SELECT NOW() as value_1, NOW() as value_2;
+---------------------+---------------------+
| value_1             | value_2             |
+---------------------+---------------------+
| 2022-09-29 07:30:55 | 2022-09-29 07:30:55 |
+---------------------+---------------------+
1 row in set (0.00 sec) 

| CURRENT_TIMESTAMP() | CURRENT_TIMESTAMP |
|----------------------------+----------------------------|
| 2022-08-02 15:54:19.209361 | 2022-08-02 15:54:19.209361 |
+----------------------------+----------------------------+

Choose a reason for hiding this comment

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

mysql> SELECT CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP;
+---------------------+---------------------+
| CURRENT_TIMESTAMP() | CURRENT_TIMESTAMP   |
+---------------------+---------------------+
| 2022-09-29 07:32:49 | 2022-09-29 07:32:49 |
+---------------------+

MySQL return

andy-k-improving pushed a commit that referenced this pull request Nov 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants