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

Support LPAD and RPAD sql function #7388

Merged
merged 5 commits into from
Apr 22, 2019
Merged

Conversation

xueyumusic
Copy link
Contributor

LPAD and RPAD are sql functions supported by oracle and hive etc. This PR tries to add them in druid.
The syntax is LPAD(base, len, pad).
It adds padding characters to the left or right side of a base string up to a given length. The default padding character is a space. If the string's length is greater than the required length, it will be trimmed (excess characters will be removed).

Copy link
Member

@clintropolis clintropolis left a comment

Choose a reason for hiding this comment

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

overall lgtm 👍


String base = NullHandling.nullToEmptyIfNeeded(args.get(0).eval(bindings).asString());
int len = args.get(1).eval(bindings).asInt();
String pad = NullHandling.nullToEmptyIfNeeded(args.get(2).eval(bindings).asString());
Copy link
Member

Choose a reason for hiding this comment

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

Does this make sense for the pad? Seems like it should maybe be an error if it's null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@clintropolis Thanks for review. When base or pad is null, it does not make sense to do these.

throw new IAE("Function[%s] needs 3 arguments", name());
}

String base = NullHandling.nullToEmptyIfNeeded(args.get(0).eval(bindings).asString());
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to pad empty values when sql compatible null handling is not enabled?

* @param pad The pad string
* @return the string left-padded with pad to a lenght of len
*/
public static String lpad(String base, Integer len, String pad)
Copy link
Member

Choose a reason for hiding this comment

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

I believe guava and commons-lang also have string padding functions, how do these compare to those implementations? (Not a suggestion to use either of those, just curious about the choice)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@clintropolis I looked at guava and commons-lang3 StringUtils, it has different behavior from expected for length less than base string, such as lpad("bat", 1, "yz") expected b but commons-lang3 gave bat. And guava only pad single char. This method is migrated from Flink's scala method

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the explanation, seems reasonable 👍

Copy link
Member

Choose a reason for hiding this comment

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

Could you add this link in the javadoc for this method?

* @param pad The pad string
* @return the string left-padded with pad to a lenght of len
*/
public static String lpad(String base, Integer len, String pad)
Copy link
Member

Choose a reason for hiding this comment

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

Could you add this link in the javadoc for this method?

* @param base The base string to be padded
* @param len The length of padded string
* @param pad The pad string
* @return the string left-padded with pad to a lenght of len
Copy link
Member

Choose a reason for hiding this comment

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

typo lenght -> length

* @param base The base string to be padded
* @param len The length of padded string
* @param pad The pad string
* @return the string right-padded with pad to a lenght of len
Copy link
Member

Choose a reason for hiding this comment

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

same lenght typo

int len = args.get(1).eval(bindings).asInt();
String pad = args.get(2).eval(bindings).asString();

if (base == null || pad == null) {
Copy link
Member

Choose a reason for hiding this comment

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

I feel like pad being null should be an error case instead of silently returning null, but what do Oracle and Hive do in this case?

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, interesting, did a bit of searching and from the docs I've found, it seems like they have different behavior, with Oracle having a default pad value of a single blank character, and Hive returning null as you are doing here. If this is true, I'm not actually sure what is best thing to do here...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, @clintropolis , Postgres/Orcale has two func signatures which are two params or three params. When only having two params such as lpad('abc',5) it will pad with default blank char.

For three params, I tested postgres, oracle and hive when padding or base is null.
Postgres and hive:

select lpad('b',5,null) is null;  ##result is true
select lpad(null,5,'x') is null;  ##result is true

Oracle

create table test1(a int);
insert into test1(a) values(5);
select count(1) from test1 where lpad('abc',5,null) is null;# result is 1
select count(1) from test1 where lpad(null,5,'x') is null; #result is 1
select count(1) from test1 where lpad('abc',5) is null;   # result is 0

It looks the results are all null when base or pad is null and the same

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for testing! null seems like the thing to do then 👍

// The length of the padding needed
int pos = Math.max(len - base.length(), 0);

// Copy the padding
Copy link
Contributor

Choose a reason for hiding this comment

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

For the sake of conciseness, can this be shortened to use for loops?

    // Copy the padding
    for (int i = 0; i < pos; i += pad.length()) {
      for (int j = 0; j < pad.length() && j < pos - i; j++) {
        data[i + j] = padChars[j];
      }
    }

    // Copy the base
    for (int i = 0; pos + i < len && i < base.length(); i++) {
      data[pos + i] = baseChars[i];
    }

}

char[] data = new char[len];
char[] baseChars = base.toCharArray();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason you copy base and pad to their own char arrays instead of just calling charAt(index) on the original strings?

@xueyumusic
Copy link
Contributor Author

Hi, @clintropolis @justinborromeo Thanks a lot for review! I updated the codes according to your comments.

Copy link
Contributor

@justinborromeo justinborromeo left a comment

Choose a reason for hiding this comment

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

Overall lgtm - some minor formatting and comment nits

@Test
public void testSelectPadFamily() throws Exception
{
// TRIM has some whacky parsing. Make sure the different forms work.
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment doesn't seem to apply to this test.

}
);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit - please include a newline at the end of the file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, @justinborromeo , thanks for review. It seems that this file is ended as 0x7d 0x0a which are }\n. From raw file it looks having a blank ending line. Most other java files seems the same. I an not sure whether I understood correctly..Thank you

}
);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit - please include a newline at the end of the file

@justinborromeo
Copy link
Contributor

@xueyumusic Could you also add the LPAD and RPAD functions to the SQL doc?

@xueyumusic
Copy link
Contributor Author

Thanks for review! @justinborromeo , updated according to your comments

@@ -85,6 +85,8 @@ The following built-in functions are available.
|upper|upper(expr) converts a string to uppercase|
|reverse|reverse(expr) reverses a string|
|repeat|repeat(expr, N) repeats a string N times|
|lpad|lpad(expr, length, chars) returns a string, left-padded to a specified length with the specified characters; or, when the string to be padded is longer than the length specified after padding, only that portion of the expression that fits into the specified length.
Copy link
Member

Choose a reason for hiding this comment

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

The documentation for these functions appears to be a near word for word copy of the Oracle documentation of these functions, with 'expression' replaced with 'string' in most cases. I think this is risky, https://www.oracle.com/legal/copyright.html indicates that both code and documentation is covered, and I think these descriptions need rewritten.

Maybe something like

lpad(expr, length, chars) returns a string of length from expr left-padded with chars. If either expr or chars are null, the result will be null.

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 a lot for reminding, @clintropolis . I totally agree with you and updated the doc.

Copy link
Member

@clintropolis clintropolis left a comment

Choose a reason for hiding this comment

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

👍

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.

5 participants