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 Trino transpilation of nullable single field ROW generated from EXCLUDE #46

Merged
merged 1 commit into from
Jun 4, 2024
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
12 changes: 12 additions & 0 deletions src/main/kotlin/org/partiql/scribe/targets/trino/TrinoTarget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ import org.partiql.scribe.asNonNullable
import org.partiql.scribe.sql.SqlCalls
import org.partiql.scribe.sql.SqlFeatures
import org.partiql.scribe.sql.SqlTarget
import org.partiql.types.AnyOfType
import org.partiql.types.BoolType
import org.partiql.types.CollectionType
import org.partiql.types.DateType
import org.partiql.types.DecimalType
import org.partiql.types.FloatType
import org.partiql.types.IntType
import org.partiql.types.ListType
import org.partiql.types.MissingType
import org.partiql.types.NullType
import org.partiql.types.NumberConstraint
import org.partiql.types.SingleType
Expand Down Expand Up @@ -528,6 +530,16 @@ public open class TrinoTarget : SqlTarget() {
val elementType = elementType.toTrinoString()
head + elementType + ">"
}
is AnyOfType -> {
// Filter out unknown types. Trino types are nullable by default. Once Scribe uses PLK 0.15+,
// `StaticType`s will be nullable + missable by default, so this branch will not be needed.
val typesWithoutUnknown = this.flatten().allTypes.filterNot { it is NullType || it is MissingType }
if (typesWithoutUnknown.size != 1) {
error("Not able to convert StaticType $this to Trino")
}
val singleType = typesWithoutUnknown.first()
singleType.toTrinoString()
}
else -> TODO("Not able to convert StaticType $this to Trino")
}
}
Expand Down
32 changes: 32 additions & 0 deletions src/test/resources/catalogs/default/datatypes/T_CHAR_16_NULL.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
type: "bag",
items: {
type: "struct",
constraints: [ closed, ordered, unique ],
fields: [
{
name: "foo",
type: {
type: "struct",
constraints: [ closed, ordered, unique ],
fields: [
{
name: "bar",
type: "string",
},
{
name: "keep",
type: [
{
type: "char",
length: 16
},
"null"
]
}
]
},
},
]
}
}
26 changes: 26 additions & 0 deletions src/test/resources/catalogs/default/datatypes/T_INT32_NULL.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
type: "bag",
items: {
type: "struct",
constraints: [ closed, ordered, unique ],
fields: [
{
name: "foo",
type: {
type: "struct",
constraints: [ closed, ordered, unique ],
fields: [
{
name: "bar",
type: "string",
},
{
name: "keep",
type: [ "int32", "null" ]
},
]
},
},
]
}
}
32 changes: 32 additions & 0 deletions src/test/resources/catalogs/default/datatypes/T_STRING_16_NULL.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
type: "bag",
items: {
type: "struct",
constraints: [ closed, ordered, unique ],
fields: [
{
name: "foo",
type: {
type: "struct",
constraints: [ closed, ordered, unique ],
fields: [
{
name: "bar",
type: "string",
},
{
name: "keep",
type: [
{
type: "string",
length: 16
},
"null"
]
}
]
},
},
]
}
}
26 changes: 26 additions & 0 deletions src/test/resources/catalogs/default/datatypes/T_STRING_NULL.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
type: "bag",
items: {
type: "struct",
constraints: [ closed, ordered, unique ],
fields: [
{
name: "foo",
type: {
type: "struct",
constraints: [ closed, ordered, unique ],
fields: [
{
name: "bar",
type: "string",
},
{
name: "keep",
type: [ "string", "null" ]
},
]
},
},
]
}
}
16 changes: 16 additions & 0 deletions src/test/resources/inputs/basics/exclude.sql
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,19 @@ SELECT * EXCLUDE t.foo.bar FROM datatypes.T_TIME_6 AS t;
-- timestamp(6)
--#[exclude-31]
SELECT * EXCLUDE t.foo.bar FROM datatypes.T_TIMESTAMP_6 AS t;

-- union(string, null)
--#[exclude-32]
SELECT * EXCLUDE t.foo.bar FROM datatypes.T_STRING_NULL AS t;

-- union(int32, null)
--#[exclude-33]
SELECT * EXCLUDE t.foo.bar FROM datatypes.T_INT32_NULL AS t;

-- union(varchar(16), null)
--#[exclude-34]
SELECT * EXCLUDE t.foo.bar FROM datatypes.T_STRING_16_NULL AS t;

-- union(char(16), null)
--#[exclude-35]
SELECT * EXCLUDE t.foo.bar FROM datatypes.T_CHAR_16_NULL AS t;
16 changes: 16 additions & 0 deletions src/test/resources/outputs/trino/basics/exclude.sql
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,19 @@ SELECT "$__EXCLUDE_ALIAS__"."t".* FROM (SELECT CAST(ROW(CAST(ROW("t"."foo"."keep
-- timestamp(6)
--#[exclude-31]
SELECT "$__EXCLUDE_ALIAS__"."t".* FROM (SELECT CAST(ROW(CAST(ROW("t"."foo"."keep") AS ROW(keep TIMESTAMP(6)))) AS ROW(foo ROW(keep TIMESTAMP(6)))) AS "t" FROM "default"."datatypes"."T_TIMESTAMP_6" AS "t") AS "$__EXCLUDE_ALIAS__";

-- union(string, null)
--#[exclude-32]
SELECT "$__EXCLUDE_ALIAS__"."t".* FROM (SELECT CAST(ROW(CAST(ROW("t"."foo"."keep") AS ROW(keep VARCHAR))) AS ROW(foo ROW(keep VARCHAR))) AS "t" FROM "default"."datatypes"."T_STRING_NULL" AS "t") AS "$__EXCLUDE_ALIAS__";

-- union(int32, null)
--#[exclude-33]
SELECT "$__EXCLUDE_ALIAS__"."t".* FROM (SELECT CAST(ROW(CAST(ROW("t"."foo"."keep") AS ROW(keep INTEGER))) AS ROW(foo ROW(keep INTEGER))) AS "t" FROM "default"."datatypes"."T_INT32_NULL" AS "t") AS "$__EXCLUDE_ALIAS__";

-- union(varchar(16), null)
--#[exclude-34]
SELECT "$__EXCLUDE_ALIAS__"."t".* FROM (SELECT CAST(ROW(CAST(ROW("t"."foo"."keep") AS ROW(keep VARCHAR(16)))) AS ROW(foo ROW(keep VARCHAR(16)))) AS "t" FROM "default"."datatypes"."T_STRING_16_NULL" AS "t") AS "$__EXCLUDE_ALIAS__";

-- union(char(16), null)
--#[exclude-35]
SELECT "$__EXCLUDE_ALIAS__"."t".* FROM (SELECT CAST(ROW(CAST(ROW("t"."foo"."keep") AS ROW(keep CHAR(16)))) AS ROW(foo ROW(keep CHAR(16)))) AS "t" FROM "default"."datatypes"."T_CHAR_16_NULL" AS "t") AS "$__EXCLUDE_ALIAS__";