Skip to content

Commit

Permalink
Fix getting type signature for UDT from uppercase type string
Browse files Browse the repository at this point in the history
  • Loading branch information
hantangwangd committed Dec 8, 2023
1 parent 24aec09 commit 2764645
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static TypeSignatureBase of(String name)
{
int pos = name.indexOf(":");
if (pos >= 0) {
return new TypeSignatureBase(QualifiedObjectName.valueOf(name.substring(0, pos)), name.substring(pos + 1));
return new TypeSignatureBase(QualifiedObjectName.valueOf(name.substring(0, pos).toLowerCase(ENGLISH)), name.substring(pos + 1));
}
if (name.chars().noneMatch(c -> c == '.')) {
return new TypeSignatureBase(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ public void parseSignatureWithLiterals()
assertEquals(result.getParameters().get(1).isLongLiteral(), true);
}

@Test
public void parseNamedTypeSignature()
{
assertRowSignature(
"cat.sch.pair:row(a bigint,b array(bigint),c row(a bigint))",
namedRowSignature("cat.sch.pair",
namedParameter("a", false, signature("bigint")),
namedParameter("b", false, array(signature("bigint"))),
namedParameter("c", false, rowSignature(namedParameter("a", false, signature("bigint"))))));

// the UDT's name would be translated to lower case, and it's base type name case would be preserve
assertSignature(
"CAT.SCH.TROW:ROW(a CAT.SCH.TV:VARCHAR,b ARRAY(BIGINT),c ROW(a BIGINT))",
"cat.sch.trow:ROW",
ImmutableList.of("a cat.sch.tv:VARCHAR", "b ARRAY(BIGINT)", "c ROW(a BIGINT)"),
"cat.sch.trow:ROW(a cat.sch.tv:VARCHAR,b ARRAY(BIGINT),c ROW(a BIGINT))");
}

@Test
public void parseRowSignature()
{
Expand Down Expand Up @@ -197,6 +215,11 @@ private static TypeSignature rowSignature(NamedTypeSignature... columns)
return new TypeSignature("row", transform(asList(columns), TypeSignatureParameter::of));
}

private static TypeSignature namedRowSignature(String distinctTypeName, NamedTypeSignature... columns)
{
return new TypeSignature(distinctTypeName + ":row", transform(asList(columns), TypeSignatureParameter::of));
}

private static NamedTypeSignature namedParameter(String name, boolean delimited, TypeSignature value)
{
return new NamedTypeSignature(Optional.of(new RowFieldName(name, delimited)), value);
Expand Down
6 changes: 6 additions & 0 deletions presto-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
<artifactId>presto-common</artifactId>
</dependency>

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-function-namespace-managers</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-tests</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@
package com.facebook.presto.jdbc;

import com.facebook.airlift.log.Logging;
import com.facebook.presto.functionNamespace.SqlInvokedFunctionNamespaceManagerConfig;
import com.facebook.presto.functionNamespace.execution.NoopSqlFunctionExecutor;
import com.facebook.presto.functionNamespace.execution.SqlFunctionExecutors;
import com.facebook.presto.functionNamespace.testing.InMemoryFunctionNamespaceManager;
import com.facebook.presto.server.testing.TestingPrestoServer;
import com.facebook.presto.spi.function.FunctionImplementationType;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
Expand All @@ -37,6 +44,7 @@
import java.time.LocalTime;

import static com.facebook.presto.jdbc.TestPrestoDriver.closeQuietly;
import static com.facebook.presto.spi.function.RoutineCharacteristics.Language.SQL;
import static java.lang.String.format;
import static java.util.concurrent.TimeUnit.DAYS;
import static java.util.concurrent.TimeUnit.HOURS;
Expand All @@ -60,6 +68,14 @@ public void setupServer()
{
Logging.initialize();
server = new TestingPrestoServer();
server.getMetadata().getFunctionAndTypeManager().addFunctionNamespace(
"cat",
new InMemoryFunctionNamespaceManager(
"sch",
new SqlFunctionExecutors(
ImmutableMap.of(SQL, FunctionImplementationType.SQL),
new NoopSqlFunctionExecutor()),
new SqlInvokedFunctionNamespaceManagerConfig().setSupportedFunctionLanguages("sql")));
}

@AfterClass(alwaysRun = true)
Expand Down Expand Up @@ -205,6 +221,16 @@ public void testObjectTypes()
// TODO this should fail, as there no java.sql.Timestamp representation for TIMESTAMP '1970-01-01 00:14:15.227ó' in America/Bahia_Banderas
assertEquals(rs.getTimestamp(column), Timestamp.valueOf(LocalDateTime.of(1969, 12, 31, 15, 14, 15, 227_000_000)));
});

statement.execute("CREATE TYPE cat.sch.dist AS integer");
checkRepresentation("CAST(1 AS cat.sch.dist)", Types.JAVA_OBJECT, (rs, column) -> {
assertEquals(rs.getObject(1), 1);
});

statement.execute("CREATE TYPE cat.sch.pair AS (fst integer, snd varchar)");
checkRepresentation("CAST((1,'1001') AS cat.sch.pair)", Types.JAVA_OBJECT, (rs, column) -> {
assertEquals(rs.getObject(1), Lists.newArrayList(1, "1001"));
});
}

private void checkRepresentation(String expression, int expectedSqlType, Object expectedRepresentation)
Expand Down

0 comments on commit 2764645

Please sign in to comment.