Skip to content

Commit

Permalink
Promote substring function from alias instead of substr
Browse files Browse the repository at this point in the history
  • Loading branch information
ebyhr committed Jun 7, 2020
1 parent 2dba71a commit 1971e9c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
14 changes: 7 additions & 7 deletions presto-docs/src/main/sphinx/functions/string.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,24 +150,24 @@ String Functions

.. function:: substr(string, start) -> varchar

This is an alias for :func:`substring`.

.. function:: substring(string, start) -> varchar

Returns the rest of ``string`` from the starting position ``start``.
Positions start with ``1``. A negative starting position is interpreted
as being relative to the end of the string.

.. function:: substring(string, start) -> varchar
.. function:: substr(string, start, length) -> varchar

This is an alias for :func:`substr`.
This is an alias for :func:`substring`.

.. function:: substr(string, start, length) -> varchar
.. function:: substring(string, start, length) -> varchar

Returns a substring from ``string`` of length ``length`` from the starting
position ``start``. Positions start with ``1``. A negative starting
position is interpreted as being relative to the end of the string.

.. function:: substring(string, start, length) -> varchar

This is an alias for :func:`substr`.

.. function:: trim(string) -> varchar

Removes leading and trailing whitespace from ``string``.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,10 @@ private static long stringPositionFromEnd(Slice string, Slice substring, long in
}

@Description("Suffix starting at given index")
@ScalarFunction(alias = "substring")
@ScalarFunction(alias = "substr")
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice substr(@SqlType("varchar(x)") Slice utf8, @SqlType(StandardTypes.BIGINT) long start)
public static Slice substring(@SqlType("varchar(x)") Slice utf8, @SqlType(StandardTypes.BIGINT) long start)
{
if ((start == 0) || utf8.length() == 0) {
return Slices.EMPTY_SLICE;
Expand Down Expand Up @@ -307,19 +307,19 @@ public static Slice substr(@SqlType("varchar(x)") Slice utf8, @SqlType(StandardT
}

@Description("Suffix starting at given index")
@ScalarFunction(value = "substr", alias = "substring")
@ScalarFunction(value = "substring", alias = "substr")
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice charSubstr(@LiteralParameter("x") Long x, @SqlType("char(x)") Slice utf8, @SqlType(StandardTypes.BIGINT) long start)
public static Slice charSubstring(@LiteralParameter("x") Long x, @SqlType("char(x)") Slice utf8, @SqlType(StandardTypes.BIGINT) long start)
{
return substr(padSpaces(utf8, x.intValue()), start);
return substring(padSpaces(utf8, x.intValue()), start);
}

@Description("Substring of given length starting at an index")
@ScalarFunction(alias = "substring")
@ScalarFunction(alias = "substr")
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice substr(@SqlType("varchar(x)") Slice utf8, @SqlType(StandardTypes.BIGINT) long start, @SqlType(StandardTypes.BIGINT) long length)
public static Slice substring(@SqlType("varchar(x)") Slice utf8, @SqlType(StandardTypes.BIGINT) long start, @SqlType(StandardTypes.BIGINT) long length)
{
if (start == 0 || (length <= 0) || (utf8.length() == 0)) {
return Slices.EMPTY_SLICE;
Expand Down Expand Up @@ -365,12 +365,12 @@ public static Slice substr(@SqlType("varchar(x)") Slice utf8, @SqlType(StandardT
}

@Description("Substring of given length starting at an index")
@ScalarFunction(value = "substr", alias = "substring")
@ScalarFunction(value = "substring", alias = "substr")
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice charSubstr(@LiteralParameter("x") Long x, @SqlType("char(x)") Slice utf8, @SqlType(StandardTypes.BIGINT) long start, @SqlType(StandardTypes.BIGINT) long length)
{
return substr(padSpaces(utf8, x.intValue()), start, length);
return substring(padSpaces(utf8, x.intValue()), start, length);
}

@ScalarFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import static io.prestosql.operator.scalar.StringFunctions.lower;
import static io.prestosql.operator.scalar.StringFunctions.reverse;
import static io.prestosql.operator.scalar.StringFunctions.rightTrim;
import static io.prestosql.operator.scalar.StringFunctions.substr;
import static io.prestosql.operator.scalar.StringFunctions.substring;
import static io.prestosql.operator.scalar.StringFunctions.trim;
import static io.prestosql.operator.scalar.StringFunctions.upper;
import static java.lang.Character.MAX_CODE_POINT;
Expand Down Expand Up @@ -72,31 +72,31 @@ public Slice benchmarkSubstringStart(BenchmarkData data)
{
Slice slice = data.getSlice();
int length = data.getLength();
return substr(slice, (length / 2) - 1);
return substring(slice, (length / 2) - 1);
}

@Benchmark
public Slice benchmarkSubstringStartLength(BenchmarkData data)
{
Slice slice = data.getSlice();
int length = data.getLength();
return substr(slice, (length / 2) - 1, length / 2);
return substring(slice, (length / 2) - 1, length / 2);
}

@Benchmark
public Slice benchmarkSubstringStartFromEnd(BenchmarkData data)
{
Slice slice = data.getSlice();
int length = data.getLength();
return substr(slice, -((length / 2) + 1));
return substring(slice, -((length / 2) + 1));
}

@Benchmark
public Slice benchmarkSubstringStartLengthFromEnd(BenchmarkData data)
{
Slice slice = data.getSlice();
int length = data.getLength();
return substr(slice, -((length / 2) + 1), length / 2);
return substring(slice, -((length / 2) + 1), length / 2);
}

@Benchmark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ public void testFunctionCall()
expected = null;
}
else {
expected = StringFunctions.substr(utf8Slice(value), start, length).toStringUtf8();
expected = StringFunctions.substring(utf8Slice(value), start, length).toStringUtf8();
}
VarcharType expectedType = value != null ? createVarcharType(value.length()) : VARCHAR;

Expand Down

0 comments on commit 1971e9c

Please sign in to comment.