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

Fix error message for function calls with lambda arguments #10423

Merged
merged 3 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,9 @@ FunctionBinding resolveFunction(Session session, Collection<FunctionMetadata> al

List<String> expectedParameters = new ArrayList<>();
for (FunctionMetadata function : allCandidates) {
expectedParameters.add(format("%s(%s) %s",
name,
Joiner.on(", ").join(function.getSignature().getArgumentTypes()),
Joiner.on(", ").join(function.getSignature().getTypeVariableConstraints())));
String arguments = Joiner.on(", ").join(function.getSignature().getArgumentTypes());
String constraints = Joiner.on(", ").join(function.getSignature().getTypeVariableConstraints());
expectedParameters.add(format("%s(%s) %s", name, arguments, constraints).stripTrailing());
}

String parameters = Joiner.on(", ").join(parameterTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,4 @@ public static Slice typeof(
{
return utf8Slice(type.getDisplayName());
}

@TypeParameter("T")
@SqlType(StandardTypes.VARCHAR)
public static Slice typeof(
@TypeParameter("T") Type type,
@SqlNullable @SqlType("T") Long value)
{
return typeof(type, (Object) value);
}

@TypeParameter("T")
@SqlType(StandardTypes.VARCHAR)
public static Slice typeof(
@TypeParameter("T") Type type,
@SqlNullable @SqlType("T") Double value)
{
return typeof(type, (Object) value);
}

@TypeParameter("T")
@SqlType(StandardTypes.VARCHAR)
public static Slice typeof(
@TypeParameter("T") Type type,
@SqlNullable @SqlType("T") Boolean value)
{
return typeof(type, (Object) value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static List<TypeSignatureProvider> fromTypeSignatures(List<? extends Type
public String toString()
{
if (hasDependency) {
return super.toString();
return "<function>";
}
return getTypeSignature().toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import static io.trino.operator.scalar.ApplyFunction.APPLY_FUNCTION;
import static io.trino.operator.scalar.InvokeFunction.INVOKE_FUNCTION;
import static io.trino.spi.StandardErrorCode.FUNCTION_NOT_FOUND;
import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.spi.type.BooleanType.BOOLEAN;
import static io.trino.spi.type.DoubleType.DOUBLE;
Expand Down Expand Up @@ -167,6 +168,17 @@ public void testTypeCombinations()
assertFunction("apply(MAP(ARRAY['abc', 'def'], ARRAY[123, 456]), x -> map_keys(x))", new ArrayType(createVarcharType(3)), ImmutableList.of("abc", "def"));
}

@Test
public void testFunctionParameter()
{
assertInvalidFunction("count(x -> x)", FUNCTION_NOT_FOUND, "line 1:1: Unexpected parameters (<function>) for function count. Expected: count(), count(T) T");
assertInvalidFunction("max(x -> x)", FUNCTION_NOT_FOUND, "line 1:1: Unexpected parameters (<function>) for function max. Expected: max(E) E:orderable, max(E, bigint) E:orderable");
assertInvalidFunction("sqrt(x -> x)", FUNCTION_NOT_FOUND, "line 1:1: Unexpected parameters (<function>) for function sqrt. Expected: sqrt(double)");
assertInvalidFunction("sqrt(x -> x, 123, x -> x)", FUNCTION_NOT_FOUND, "line 1:1: Unexpected parameters (<function>, integer, <function>) for function sqrt. Expected: sqrt(double)");
assertInvalidFunction("pow(x -> x, 123)", FUNCTION_NOT_FOUND, "line 1:1: Unexpected parameters (<function>, integer) for function pow. Expected: pow(double, double)");
assertInvalidFunction("pow(123, x -> x)", FUNCTION_NOT_FOUND, "line 1:1: Unexpected parameters (integer, <function>) for function pow. Expected: pow(double, double)");
}

private static String quote(String identifier)
{
return "\"" + identifier.replace("\"", "\"\"") + "\"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import io.trino.spi.type.VarcharType;
import org.testng.annotations.Test;

import static io.trino.spi.StandardErrorCode.FUNCTION_NOT_FOUND;

public class TestTypeOfFunction
extends AbstractTestFunctions
{
Expand Down Expand Up @@ -58,4 +60,10 @@ public void testComplex()
assertFunction("typeof(sin(2))", VarcharType.VARCHAR, "double");
assertFunction("typeof(2+sin(2)+2.3)", VarcharType.VARCHAR, "double");
}

@Test
public void testLambda()
{
assertInvalidFunction("typeof(x -> x)", FUNCTION_NOT_FOUND, "line 1:1: Unexpected parameters (<function>) for function typeof. Expected: typeof(t) T");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ public void testArrayToArrayConcat()

assertThatThrownBy(() -> assertFunction("ARRAY [ARRAY [1]] || ARRAY [ARRAY ['x']]", new ArrayType(new ArrayType(INTEGER)), null))
.isInstanceOf(TrinoException.class)
.hasMessage("line 1:19: Unexpected parameters (array(array(integer)), array(array(varchar(1)))) for function concat. Expected: concat(char(x), char(y)) , concat(array(E), E) E, concat(E, array(E)) E, concat(array(E)) E, concat(varchar) , concat(varbinary) ");
.hasMessage("line 1:19: Unexpected parameters (array(array(integer)), array(array(varchar(1)))) for function concat. Expected: concat(char(x), char(y)), concat(array(E), E) E, concat(E, array(E)) E, concat(array(E)) E, concat(varchar), concat(varbinary)");

assertCachedInstanceHasBoundedRetainedSize("ARRAY [1, NULL] || ARRAY [3]");
}
Expand Down Expand Up @@ -477,7 +477,7 @@ public void testElementArrayConcat()

assertThatThrownBy(() -> assertFunction("ARRAY [ARRAY[1]] || ARRAY ['x']", new ArrayType(new ArrayType(INTEGER)), null))
.isInstanceOf(TrinoException.class)
.hasMessage("line 1:18: Unexpected parameters (array(array(integer)), array(varchar(1))) for function concat. Expected: concat(char(x), char(y)) , concat(array(E), E) E, concat(E, array(E)) E, concat(array(E)) E, concat(varchar) , concat(varbinary) ");
.hasMessage("line 1:18: Unexpected parameters (array(array(integer)), array(varchar(1))) for function concat. Expected: concat(char(x), char(y)), concat(array(E), E) E, concat(E, array(E)) E, concat(array(E)) E, concat(varchar), concat(varbinary)");

assertCachedInstanceHasBoundedRetainedSize("ARRAY [1, NULL] || 3");
assertCachedInstanceHasBoundedRetainedSize("3 || ARRAY [1, NULL]");
Expand Down