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

added hex function #4004

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions h2/src/main/org/h2/command/Parser.java
Expand Up @@ -4138,6 +4138,8 @@ private Expression readBuiltinFunctionIf(String upperName) {
return new StringFunction1(readSingleArgument(), StringFunction1.HEXTORAW);
case "RAWTOHEX":
return new StringFunction1(readSingleArgument(), StringFunction1.RAWTOHEX);
case "HEX":
return new StringFunction1(readSingleArgument(), StringFunction1.HEX);
case "SPACE":
return new StringFunction1(readSingleArgument(), StringFunction1.SPACE);
case "QUOTE_IDENT":
Expand Down
4 changes: 2 additions & 2 deletions h2/src/main/org/h2/expression/function/BuiltinFunctions.java
Expand Up @@ -23,7 +23,7 @@ public final class BuiltinFunctions {
"ABS", "MOD", "FLOOR", "CEIL", "ROUND", "ROUNDMAGIC", "SIGN", "TRUNC", "TRUNCATE",
// MathFunction1
"SIN", "COS", "TAN", "COT", "SINH", "COSH", "TANH", "ASIN", "ACOS", "ATAN", //
"LOG10", "LN", "EXP", "SQRT", "DEGREES", "RADIANS",
"LOG10", "LN", "EXP", "SQRT", "DEGREES", "RADIANS",
// MathFunction2
"ATAN2", "LOG", "POWER",
// BitFunction
Expand All @@ -42,7 +42,7 @@ public final class BuiltinFunctions {
"LOCATE", "INSERT", "REPLACE", "LPAD", "RPAD", "TRANSLATE",
// StringFunction1
"UPPER", "LOWER", "ASCII", "CHAR", "CHR", "STRINGENCODE", "STRINGDECODE", "STRINGTOUTF8",
"UTF8TOSTRING", "HEXTORAW", "RAWTOHEX", "SPACE", "QUOTE_IDENT",
"UTF8TOSTRING", "HEXTORAW", "RAWTOHEX", "HEX", "SPACE", "QUOTE_IDENT",
// StringFunction2
/* LEFT and RIGHT are keywords */ "REPEAT",
// SubstringFunction
Expand Down
26 changes: 24 additions & 2 deletions h2/src/main/org/h2/expression/function/StringFunction1.java
Expand Up @@ -83,10 +83,15 @@ public final class StringFunction1 extends Function1 {
*/
public static final int RAWTOHEX = HEXTORAW + 1;

/**
* HEX() (non-standard).
*/
public static final int HEX = RAWTOHEX + 1;

/**
* SPACE() (non-standard).
*/
public static final int SPACE = RAWTOHEX + 1;
public static final int SPACE = HEX + 1;

/**
* QUOTE_IDENT() (non-standard).
Expand All @@ -95,7 +100,7 @@ public final class StringFunction1 extends Function1 {

private static final String[] NAMES = { //
"UPPER", "LOWER", "ASCII", "CHAR", "STRINGENCODE", "STRINGDECODE", "STRINGTOUTF8", "UTF8TOSTRING",
"HEXTORAW", "RAWTOHEX", "SPACE", "QUOTE_IDENT" //
"HEXTORAW", "RAWTOHEX", "HEX", "SPACE", "QUOTE_IDENT" //
};

private final int function;
Expand Down Expand Up @@ -148,6 +153,9 @@ public Value getValue(SessionLocal session) {
case RAWTOHEX:
v = ValueVarchar.get(rawToHex(v, session.getMode()), session);
break;
case HEX:
v = ValueVarchar.get(hex(v, session.getMode()), session);
break;
case SPACE: {
byte[] chars = new byte[Math.max(0, v.getInt())];
Arrays.fill(chars, (byte) ' ');
Expand Down Expand Up @@ -202,6 +210,11 @@ private static String rawToHex(Value v, Mode mode) {
return buff.toString();
}

private static String hex(Value v, Mode mode) {
long l=v.getLong();
return Long.toHexString(l);
}

@Override
public Expression optimize(SessionLocal session) {
arg = arg.optimize(session);
Expand Down Expand Up @@ -266,6 +279,15 @@ public Expression optimize(SessionLocal session) {
precision <= Long.MAX_VALUE / mul ? precision * mul : Long.MAX_VALUE, 0, null);
break;
}
case HEX: {
TypeInfo t = arg.getType();
long precision = t.getPrecision();
int mul = DataType.isBinaryStringOrSpecialBinaryType(t.getValueType()) ? 2
: session.getMode().getEnum() == ModeEnum.Oracle ? 6 : 4;
type = TypeInfo.getTypeInfo(Value.VARCHAR,
precision <= Long.MAX_VALUE / mul ? precision * mul : Long.MAX_VALUE, 0, null);
break;
}
default:
throw DbException.getInternalError("function=" + function);
}
Expand Down
2 changes: 1 addition & 1 deletion h2/src/tools/org/h2/build/doc/dictionary.txt
Expand Up @@ -510,7 +510,7 @@ quotas quote quoted quotes quoting race rad radians radic radio radius radix
rafel rail railo railroad railroads rainbow raise ram ramiere ran rand random
randomize randomized randomly randomness rang range ranges ranging rank rapid
rapidshare rapping raquo rarr raspberry rate rates rather rathsack ratio ravioli
raw rawbyte rawtohex rawtypes razor razuna rceil rcon rdbms rdf rdfs rdonly rdquo
raw rawbyte rawtohex hex rawtypes razor razuna rceil rcon rdbms rdf rdfs rdonly rdquo
reach reachable reached reaches read readability readable reader readers reading
readonly reads readwrite ready real reality really realm realtime reaper reason
reasonable reasonably reasoning reasons rebind rebuild rebuilt rec recalculate
Expand Down