From 05c591ce80e88598a10faa4434e42d70f33000fe Mon Sep 17 00:00:00 2001 From: "R. C. Howell" Date: Fri, 19 Jan 2024 09:36:32 -0800 Subject: [PATCH] Adds the explicit decimal to integer downcasts (#1342) --- .../planner/internal/typer/TypeLattice.kt | 42 +++++++++++------ .../internal/typer/PlanTyperTestsPorted.kt | 46 +++++++++++++++++++ .../catalogs/default/pql/numbers.ion | 5 ++ .../inputs/schema_inferencer/casts.sql | 17 +++++++ 4 files changed, 97 insertions(+), 13 deletions(-) create mode 100644 partiql-planner/src/testFixtures/resources/inputs/schema_inferencer/casts.sql diff --git a/partiql-planner/src/main/kotlin/org/partiql/planner/internal/typer/TypeLattice.kt b/partiql-planner/src/main/kotlin/org/partiql/planner/internal/typer/TypeLattice.kt index bdf022269..767dd9192 100644 --- a/partiql-planner/src/main/kotlin/org/partiql/planner/internal/typer/TypeLattice.kt +++ b/partiql-planner/src/main/kotlin/org/partiql/planner/internal/typer/TypeLattice.kt @@ -266,41 +266,57 @@ internal class TypeLattice private constructor( SYMBOL to explicit(), ) graph[DECIMAL] = relationships( + BOOL to explicit(), INT8 to explicit(), INT16 to explicit(), INT32 to explicit(), INT64 to explicit(), - BOOL to explicit(), - DECIMAL to explicit(), - DECIMAL_ARBITRARY to explicit(), + INT to explicit(), + DECIMAL to coercion(), + DECIMAL_ARBITRARY to coercion(), FLOAT32 to explicit(), FLOAT64 to explicit(), STRING to explicit(), SYMBOL to explicit(), ) - graph[FLOAT32] = relationships( + graph[DECIMAL_ARBITRARY] = relationships( BOOL to explicit(), - DECIMAL to explicit(), + INT8 to explicit(), + INT16 to explicit(), + INT32 to explicit(), + INT64 to explicit(), + INT to explicit(), + DECIMAL to coercion(), DECIMAL_ARBITRARY to coercion(), - FLOAT32 to coercion(), - FLOAT64 to coercion(), + FLOAT32 to explicit(), + FLOAT64 to explicit(), STRING to explicit(), SYMBOL to explicit(), ) - graph[FLOAT64] = relationships( + graph[FLOAT32] = relationships( BOOL to explicit(), - DECIMAL to explicit(), + INT8 to unsafe(), + INT16 to unsafe(), + INT32 to unsafe(), + INT64 to unsafe(), + INT to unsafe(), + DECIMAL to unsafe(), DECIMAL_ARBITRARY to coercion(), + FLOAT32 to coercion(), FLOAT64 to coercion(), STRING to explicit(), SYMBOL to explicit(), ) - graph[DECIMAL_ARBITRARY] = relationships( + graph[FLOAT64] = relationships( BOOL to explicit(), - DECIMAL to explicit(), + INT8 to unsafe(), + INT16 to unsafe(), + INT32 to unsafe(), + INT64 to unsafe(), + INT to unsafe(), + DECIMAL to unsafe(), DECIMAL_ARBITRARY to coercion(), - FLOAT32 to explicit(), - FLOAT64 to explicit(), + FLOAT64 to coercion(), STRING to explicit(), SYMBOL to explicit(), ) diff --git a/partiql-planner/src/test/kotlin/org/partiql/planner/internal/typer/PlanTyperTestsPorted.kt b/partiql-planner/src/test/kotlin/org/partiql/planner/internal/typer/PlanTyperTestsPorted.kt index 0f7b548c9..ad8114067 100644 --- a/partiql-planner/src/test/kotlin/org/partiql/planner/internal/typer/PlanTyperTestsPorted.kt +++ b/partiql-planner/src/test/kotlin/org/partiql/planner/internal/typer/PlanTyperTestsPorted.kt @@ -33,6 +33,7 @@ import org.partiql.types.BagType import org.partiql.types.ListType import org.partiql.types.SexpType import org.partiql.types.StaticType +import org.partiql.types.StaticType.Companion.unionOf import org.partiql.types.StructType import org.partiql.types.TupleConstraint import java.util.stream.Stream @@ -448,6 +449,46 @@ class PlanTyperTestsPorted { ), ) + @JvmStatic + fun castCases() = listOf( + SuccessTestCase( + name = "DECIMAL AS INT2", + key = key("cast-00"), + catalog = "pql", + expected = StaticType.INT2, + ), + SuccessTestCase( + name = "DECIMAL AS INT4", + key = key("cast-01"), + catalog = "pql", + expected = StaticType.INT4, + ), + SuccessTestCase( + name = "DECIMAL AS INT8", + key = key("cast-02"), + catalog = "pql", + expected = StaticType.INT8, + ), + SuccessTestCase( + name = "DECIMAL AS INT", + key = key("cast-03"), + catalog = "pql", + expected = StaticType.INT, + ), + SuccessTestCase( + name = "DECIMAL AS BIGINT", + key = key("cast-04"), + catalog = "pql", + expected = StaticType.INT8, + ), + SuccessTestCase( + name = "DECIMAL_ARBITRARY AS DECIMAL", + key = key("cast-05"), + catalog = "pql", + expected = StaticType.DECIMAL, + ), + ) + @JvmStatic fun sessionVariables() = listOf( SuccessTestCase( @@ -3040,6 +3081,11 @@ class PlanTyperTestsPorted { @Execution(ExecutionMode.CONCURRENT) fun testIsType(tc: TestCase) = runTest(tc) + @ParameterizedTest + @MethodSource("castCases") + @Execution(ExecutionMode.CONCURRENT) + fun testCasts(tc: TestCase) = runTest(tc) + // --------- Finish Parameterized Tests ------ // diff --git a/partiql-planner/src/testFixtures/resources/catalogs/default/pql/numbers.ion b/partiql-planner/src/testFixtures/resources/catalogs/default/pql/numbers.ion index 14c03f713..311e5a474 100644 --- a/partiql-planner/src/testFixtures/resources/catalogs/default/pql/numbers.ion +++ b/partiql-planner/src/testFixtures/resources/catalogs/default/pql/numbers.ion @@ -1,5 +1,6 @@ { type: "struct", + constraints: [closed], fields: [ { name: "nullable_int16s", @@ -69,6 +70,10 @@ items: "int", }, }, + { + name: "d", + type: "decimal", + }, { name: "decimals", type: { diff --git a/partiql-planner/src/testFixtures/resources/inputs/schema_inferencer/casts.sql b/partiql-planner/src/testFixtures/resources/inputs/schema_inferencer/casts.sql new file mode 100644 index 000000000..c1416bad8 --- /dev/null +++ b/partiql-planner/src/testFixtures/resources/inputs/schema_inferencer/casts.sql @@ -0,0 +1,17 @@ +--#[cast-00] +CAST(numbers.d AS INT2); + +--#[cast-01] +CAST(numbers.d AS INT4); + +--#[cast-02] +CAST(numbers.d AS INT8); + +--#[cast-03] +CAST(numbers.d AS INT); + +--#[cast-04] +CAST(numbers.d AS BIGINT); + +--#[cast-05] +CAST(numbers.d AS DECIMAL);