diff --git a/src/main/kotlin/org/partiql/scribe/targets/trino/TrinoTarget.kt b/src/main/kotlin/org/partiql/scribe/targets/trino/TrinoTarget.kt index b38dcec..1b59561 100644 --- a/src/main/kotlin/org/partiql/scribe/targets/trino/TrinoTarget.kt +++ b/src/main/kotlin/org/partiql/scribe/targets/trino/TrinoTarget.kt @@ -27,6 +27,7 @@ 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 @@ -34,6 +35,7 @@ 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 @@ -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") } } diff --git a/src/test/resources/catalogs/default/datatypes/T_CHAR_16_NULL.ion b/src/test/resources/catalogs/default/datatypes/T_CHAR_16_NULL.ion new file mode 100644 index 0000000..69eb7e4 --- /dev/null +++ b/src/test/resources/catalogs/default/datatypes/T_CHAR_16_NULL.ion @@ -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" + ] + } + ] + }, + }, + ] + } +} diff --git a/src/test/resources/catalogs/default/datatypes/T_INT32_NULL.ion b/src/test/resources/catalogs/default/datatypes/T_INT32_NULL.ion new file mode 100644 index 0000000..d584f7f --- /dev/null +++ b/src/test/resources/catalogs/default/datatypes/T_INT32_NULL.ion @@ -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" ] + }, + ] + }, + }, + ] + } +} diff --git a/src/test/resources/catalogs/default/datatypes/T_STRING_16_NULL.ion b/src/test/resources/catalogs/default/datatypes/T_STRING_16_NULL.ion new file mode 100644 index 0000000..49e09d7 --- /dev/null +++ b/src/test/resources/catalogs/default/datatypes/T_STRING_16_NULL.ion @@ -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" + ] + } + ] + }, + }, + ] + } +} diff --git a/src/test/resources/catalogs/default/datatypes/T_STRING_NULL.ion b/src/test/resources/catalogs/default/datatypes/T_STRING_NULL.ion new file mode 100644 index 0000000..1c0a76b --- /dev/null +++ b/src/test/resources/catalogs/default/datatypes/T_STRING_NULL.ion @@ -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" ] + }, + ] + }, + }, + ] + } +} diff --git a/src/test/resources/inputs/basics/exclude.sql b/src/test/resources/inputs/basics/exclude.sql index b648d39..c04d34b 100644 --- a/src/test/resources/inputs/basics/exclude.sql +++ b/src/test/resources/inputs/basics/exclude.sql @@ -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; diff --git a/src/test/resources/outputs/trino/basics/exclude.sql b/src/test/resources/outputs/trino/basics/exclude.sql index a2a570f..309ca58 100644 --- a/src/test/resources/outputs/trino/basics/exclude.sql +++ b/src/test/resources/outputs/trino/basics/exclude.sql @@ -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__";