Skip to content

[v0.14.8-SNAPSHOT] Preserve constraint for decimal, string in dynamic typer #1551

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

Merged
merged 4 commits into from
Aug 19, 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
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
Thank you to all who have contributed!
-->

## [Unreleased]

### Added

### Changed

### Deprecated

### Fixed
- Case When Branch inference will preserve type constraint for String Type and Decimal Type, if no coercion is required.
### Removed

### Security

### Contributors
Thank you to all who have contributed!

## [0.14.7]

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group=org.partiql
version=0.14.7
version=0.14.8-SNAPSHOT

ossrhUsername=EMPTY
ossrhPassword=EMPTY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

package org.partiql.planner.internal.typer

import org.partiql.types.DecimalType
import org.partiql.types.MissingType
import org.partiql.types.NullType
import org.partiql.types.NumberConstraint
import org.partiql.types.StaticType
import org.partiql.types.StringType
import org.partiql.value.PartiQLValueExperimental
import org.partiql.value.PartiQLValueType
import org.partiql.value.PartiQLValueType.ANY
Expand Down Expand Up @@ -35,6 +38,7 @@ import org.partiql.value.PartiQLValueType.STRUCT
import org.partiql.value.PartiQLValueType.SYMBOL
import org.partiql.value.PartiQLValueType.TIME
import org.partiql.value.PartiQLValueType.TIMESTAMP
import kotlin.math.max

/**
* Graph of super types for quick lookup because we don't have a tree.
Expand Down Expand Up @@ -127,19 +131,27 @@ internal class DynamicTyper {
val modifiers = mutableSetOf<StaticType>()
if (nullable) modifiers.add(StaticType.NULL)
if (missable) modifiers.add(StaticType.MISSING)
// If at top supertype, then return union of all accumulated types
if (supertype == ANY) {
return StaticType.unionOf(types + modifiers).flatten() to null
}
// If a collection, then return union of all accumulated types as these coercion rules are not defined by SQL.
if (supertype == STRUCT || supertype == BAG || supertype == LIST || supertype == SEXP) {
return StaticType.unionOf(types + modifiers) to null
}
// If not initialized, then return null, missing, or null|missing.
val s = supertype
if (s == null) {
val t = if (modifiers.isEmpty()) StaticType.MISSING else StaticType.unionOf(modifiers).flatten()
return t to null
val s = when (val superT = supertype) {
// If not initialized, then return null, missing, or null|missing.
null -> {
val t = if (modifiers.isEmpty()) StaticType.MISSING else StaticType.unionOf(modifiers).flatten()
return t to null
}
// If at top supertype, then return union of all accumulated types
ANY -> return StaticType.unionOf(types + modifiers).flatten() to null
// If a collection, then return union of all accumulated types as these coercion rules are not defined by SQL.
STRUCT, BAG, LIST, SEXP -> return StaticType.unionOf(types + modifiers) to null
DECIMAL -> {
val type = computeDecimal()
// coercion required. fall back
if (type == null) superT else return StaticType.unionOf(setOf(type) + modifiers).flatten() to null
}
STRING -> {
val type = computeString()
// coercion required. fall back
if (type == null) superT else return StaticType.unionOf(setOf(type) + modifiers).flatten() to null
}
else -> superT
}
// Otherwise, return the supertype along with the coercion mapping
val type = s.toNonNullStaticType()
Expand All @@ -151,6 +163,34 @@ internal class DynamicTyper {
}
}

private fun computeDecimal(): DecimalType? {
val (precision, scale) = types.fold((0 to 0)) { acc, staticType ->
val decimalType = staticType as? DecimalType ?: return null
val constr = decimalType.precisionScaleConstraint as DecimalType.PrecisionScaleConstraint.Constrained
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a need to check for Unconstrained in this situation? You do it further below in computeString(). This might throw a ClassCastException leading to unexpected runtime errors.

Especially since we know that CASE WHENs with multiple output types might lead to an unconstrained decimal. So, a nested CASE WHEN may very well end up in this situation.

Copy link
Member

@alancai98 alancai98 Aug 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

computeDecimal is only called when the supertype is a constrained DECIMAL (i.e. PartiQLValueType.DECIMAL) and not an arbitrary decimal (i.e. PartiQLValueType.DECIMAL_ARBITRARY) -- see L144. So a constrained decimal should never be in the list of types when we enter this block.

I can add the same logic from string constraints to prevent any accidental class cast exception in case there's a bug.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in the v0.14.8 release PR

val precision = max(constr.precision, acc.first)
val scale = max(constr.scale, acc.second)
precision to scale
}
return DecimalType(DecimalType.PrecisionScaleConstraint.Constrained(precision, scale))
}

private fun computeString(): StringType? {
val (length, isVarchar) = types.fold((0 to false)) { acc, staticType ->
staticType as? StringType ?: return null
when (val constr = staticType.lengthConstraint) {
is StringType.StringLengthConstraint.Constrained -> return@fold when (val l = constr.length) {
is NumberConstraint.Equals -> max(acc.first, l.value) to acc.second
is NumberConstraint.UpTo -> max(acc.first, l.value) to true
}
StringType.StringLengthConstraint.Unconstrained -> return StaticType.STRING
}
}
return when (isVarchar) {
true -> StringType(StringType.StringLengthConstraint.Constrained(NumberConstraint.UpTo(length)))
false -> StringType(StringType.StringLengthConstraint.Constrained(NumberConstraint.Equals(length)))
}
}

private fun calculate(type: PartiQLValueType) {
val s = supertype
// Initialize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ import org.partiql.types.AnyType
import org.partiql.types.BagType
import org.partiql.types.DecimalType
import org.partiql.types.ListType
import org.partiql.types.NumberConstraint
import org.partiql.types.SexpType
import org.partiql.types.StaticType
import org.partiql.types.StaticType.Companion.MISSING
import org.partiql.types.StaticType.Companion.NULL
import org.partiql.types.StaticType.Companion.unionOf
import org.partiql.types.StringType
import org.partiql.types.StructType
import org.partiql.types.TupleConstraint
import java.util.stream.Stream
Expand Down Expand Up @@ -2683,6 +2685,66 @@ class PlanTyperTestsPorted {
catalog = "pql",
expected = StaticType.ANY
),
SuccessTestCase(
key = PartiQLTest.Key("basics", "case-when-35"),
catalog = "pql",
expected = DecimalType(DecimalType.PrecisionScaleConstraint.Constrained(10, 5)).asNullable()
),
SuccessTestCase(
key = PartiQLTest.Key("basics", "case-when-36"),
catalog = "pql",
expected = DecimalType(DecimalType.PrecisionScaleConstraint.Constrained(10, 5))
),
SuccessTestCase(
key = PartiQLTest.Key("basics", "case-when-37"),
catalog = "pql",
expected = StringType(StringType.StringLengthConstraint.Constrained(NumberConstraint.UpTo(10))).asNullable()
),
SuccessTestCase(
key = PartiQLTest.Key("basics", "case-when-38"),
catalog = "pql",
expected = StringType(StringType.StringLengthConstraint.Constrained(NumberConstraint.UpTo(10)))
),
SuccessTestCase(
key = PartiQLTest.Key("basics", "case-when-39"),
catalog = "pql",
expected = StringType(StringType.StringLengthConstraint.Constrained(NumberConstraint.Equals(10))).asNullable()
),
SuccessTestCase(
key = PartiQLTest.Key("basics", "case-when-40"),
catalog = "pql",
expected = StringType(StringType.StringLengthConstraint.Constrained(NumberConstraint.Equals(10)))
),
SuccessTestCase(
key = PartiQLTest.Key("basics", "case-when-41"),
catalog = "pql",
expected = StringType(StringType.StringLengthConstraint.Constrained(NumberConstraint.UpTo(10)))
),
SuccessTestCase(
key = PartiQLTest.Key("basics", "case-when-42"),
catalog = "pql",
expected = StringType(StringType.StringLengthConstraint.Constrained(NumberConstraint.UpTo(10)))
),
SuccessTestCase(
key = PartiQLTest.Key("basics", "case-when-43"),
catalog = "pql",
expected = StaticType.DECIMAL
),
SuccessTestCase(
key = PartiQLTest.Key("basics", "case-when-44"),
catalog = "pql",
expected = StaticType.DECIMAL
),
SuccessTestCase(
key = PartiQLTest.Key("basics", "case-when-45"),
catalog = "pql",
expected = StaticType.STRING
),
SuccessTestCase(
key = PartiQLTest.Key("basics", "case-when-46"),
catalog = "pql",
expected = StaticType.STRING
),
)

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,53 @@
{
name: "t_str",
type: [ "clob", "string" ],
}
},
// fixed precision decimal
{
name: "t_decimal_10_5",
type: {
type: "decimal",
precision: 10,
scale: 5
}
},
{
name: "t_decimal_5_3",
type: {
type: "decimal",
precision: 10,
scale: 5
}
},
{
name: "t_varchar_10",
type: {
type: "string",
max_length: 10,
}
},
{
name: "t_varchar_5",
type: {
type: "string",
max_length: 5,
}
},
{
name: "t_char_10",
type: {
type: "string",
max_length: 10,
min_length: 10,
}
},
{
name: "t_char_5",
type: {
type: "string",
max_length: 5,
min_length: 5,
}
},
]
}
86 changes: 86 additions & 0 deletions partiql-planner/src/testFixtures/resources/inputs/basics/case.sql
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,92 @@ CASE t_item.t_string
ELSE t_item.t_any
END;

--#[case-when-35]
-- type: decimal(10,5)|null
CASE t_item.t_string
WHEN 'a' THEN t_item.t_decimal_10_5
ELSE null
END;

--#[case-when-36]
-- type: decimal(10,5)
CASE t_item.t_string
WHEN 'a' THEN t_item.t_decimal_10_5
ELSE t_item.t_decimal_5_3
END;

--#[case-when-37]
-- type: varchar(10)|null
CASE t_item.t_string
WHEN 'a' THEN t_item.t_varchar_10
ELSE null
END;

--#[case-when-38]
-- type: varchar(10)
CASE t_item.t_string
WHEN 'a' THEN t_item.t_varchar_10
ELSE t_item.t_varchar_5
END;

--#[case-when-39]
-- type: char(10)|null
CASE t_item.t_string
WHEN 'a' THEN t_item.t_char_10
ELSE null
END;

--#[case-when-40]
-- type: char(10)
CASE t_item.t_string
WHEN 'a' THEN t_item.t_char_10
ELSE t_item.t_char_5
END;

--#[case-when-41]
-- type: varchar(10)
CASE t_item.t_string
WHEN 'a' THEN t_item.t_char_10
ELSE t_item.t_varchar_10
END;

--#[case-when-42]
-- type: varchar(10)|null
CASE t_item.t_string
WHEN 'a' THEN t_item.t_char_10
ELSE t_item.t_varchar_5
END;

--#[case-when-43]
-- type: decimal
CASE t_item.t_string
WHEN 'a' THEN t_item.t_decimal_10_5
ELSE t_item.t_decimal
END;

--#[case-when-44]
-- type: decimal
-- coercion required. Common super type is arbitrary decimal
CASE t_item.t_string
WHEN 'a' THEN t_item.t_decimal_10_5
ELSE t_item.t_int32
END;

--#[case-when-45]
-- type: string -- unconstrained
CASE t_item.t_string
WHEN 'a' THEN t_item.t_string
ELSE t_item.t_varchar_5
END;

--#[case-when-46]
-- type: string -- unconstrained
CASE t_item.t_string
WHEN 'a' THEN t_item.t_string
ELSE t_item.t_char_5
END;


-- -----------------------------
-- (Unused) old tests
-- -----------------------------
Expand Down
Loading
Loading