Skip to content

Commit 5157d8e

Browse files
committed
wip
1 parent 5b057f0 commit 5157d8e

File tree

18 files changed

+87
-61
lines changed

18 files changed

+87
-61
lines changed

src/expr/src/relation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1707,7 +1707,7 @@ impl MirRelationExpr {
17071707
.column_types
17081708
.iter()
17091709
.zip_eq(typ.column_types.iter())
1710-
.all(|(t1, t2)| t1.scalar_type.base_eq(&t2.scalar_type)));
1710+
.all(|(t1, t2)| t1.scalar_type.physical_eq(&t2.scalar_type)));
17111711
}
17121712
let mut typ = typ.unwrap_or_else(|| self.typ());
17131713
typ.keys = vec![vec![]];

src/expr/src/scalar/func.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ use mz_ore::fmt::FormatBuffer;
3333
use mz_ore::lex::LexBuf;
3434
use mz_ore::option::OptionExt;
3535
use mz_ore::result::ResultExt;
36-
use mz_ore::{soft_assert_eq_or_log, soft_assert_or_log};
3736
use mz_ore::str::StrExt;
37+
use mz_ore::{soft_assert_eq_or_log, soft_assert_or_log};
3838
use mz_pgrepr::Type;
3939
use mz_pgtz::timezone::{Timezone, TimezoneSpec};
4040
use mz_proto::chrono::any_naive_datetime;
@@ -8034,7 +8034,9 @@ impl VariadicFunc {
80348034
.nullable(true),
80358035
ArrayCreate { elem_type } => {
80368036
soft_assert_or_log!(
8037-
input_types.iter().all(|t| t.scalar_type.base_eq(elem_type)),
8037+
input_types
8038+
.iter()
8039+
.all(|t| t.scalar_type.physical_eq(elem_type)),
80388040
"Args to ArrayCreate should have types that are compatible with the elem_type"
80398041
);
80408042
match elem_type {

src/repr/src/relation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl ColumnType {
6969
nullable: self.nullable || other.nullable,
7070
})
7171
}
72-
(scalar_type, other_scalar_type) if scalar_type.base_eq(&other_scalar_type) => {
72+
(scalar_type, other_scalar_type) if scalar_type.physical_eq(&other_scalar_type) => {
7373
Ok(ColumnType {
7474
scalar_type: scalar_type.without_modifiers(),
7575
nullable: self.nullable || other.nullable,

src/repr/src/scalar.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,12 +2908,25 @@ impl ScalarType {
29082908
self.eq_inner(other, false)
29092909
}
29102910

2911-
// Determines equality among scalar types that ignores any custom OIDs or
2912-
// embedded values.
2911+
/// Determines equality among scalar types that ignores any custom OIDs or
2912+
/// embedded values.
29132913
pub fn structural_eq(&self, other: &ScalarType) -> bool {
29142914
self.eq_inner(other, true)
29152915
}
29162916

2917+
/// //////////// todo: comment
2918+
pub fn physical_eq(&self, other: &ScalarType) -> bool {
2919+
use ScalarType::*;
2920+
if self.base_eq(other) {
2921+
return true;
2922+
}
2923+
match (self, other) {
2924+
(String, VarChar { max_length: None }) => true,
2925+
(VarChar { max_length: None }, String) => true,
2926+
_ => false,
2927+
}
2928+
}
2929+
29172930
pub fn eq_inner(&self, other: &ScalarType, structure_only: bool) -> bool {
29182931
use ScalarType::*;
29192932
match (self, other) {

src/sql/src/plan/lowering.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use std::iter::repeat;
4141

4242
use itertools::Itertools;
4343
use mz_expr::visit::Visit;
44-
use mz_expr::{AccessStrategy, AggregateFunc, MirRelationExpr, MirScalarExpr};
44+
use mz_expr::{AccessStrategy, AggregateFunc, MirRelationExpr, MirScalarExpr, UnaryFunc};
4545
use mz_ore::collections::CollectionExt;
4646
use mz_ore::stack::maybe_grow;
4747
use mz_repr::*;
@@ -955,17 +955,23 @@ impl HirScalarExpr {
955955
Literal(row, typ) => SS::Literal(Ok(row), typ),
956956
Parameter(_) => panic!("cannot decorrelate expression with unbound parameters"),
957957
CallUnmaterializable(func) => SS::CallUnmaterializable(func),
958-
CallUnary { func, expr } => SS::CallUnary {
959-
func,
960-
expr: Box::new(expr.applied_to(
961-
id_gen,
962-
col_map,
963-
cte_map,
964-
inner,
965-
subquery_map,
966-
context,
967-
)?),
968-
},
958+
CallUnary { func, expr } => {
959+
if !matches!(func, UnaryFunc::CastVarCharToString(..)) {
960+
SS::CallUnary {
961+
func,
962+
expr: Box::new(expr.applied_to(
963+
id_gen,
964+
col_map,
965+
cte_map,
966+
inner,
967+
subquery_map,
968+
context,
969+
)?),
970+
}
971+
} else {
972+
expr.applied_to(id_gen, col_map, cte_map, inner, subquery_map, context)?
973+
}
974+
}
969975
CallBinary { func, expr1, expr2 } => SS::CallBinary {
970976
func,
971977
expr1: Box::new(expr1.applied_to(
@@ -1621,10 +1627,16 @@ impl HirScalarExpr {
16211627
Column(ColumnRef { level: 0, column }) => SS::Column(column),
16221628
Literal(datum, typ) => SS::Literal(Ok(datum), typ),
16231629
CallUnmaterializable(func) => SS::CallUnmaterializable(func),
1624-
CallUnary { func, expr } => SS::CallUnary {
1625-
func,
1626-
expr: Box::new(expr.lower_uncorrelated()?),
1627-
},
1630+
CallUnary { func, expr } => {
1631+
if !matches!(func, UnaryFunc::CastVarCharToString(..)) {
1632+
SS::CallUnary {
1633+
func,
1634+
expr: Box::new(expr.lower_uncorrelated()?),
1635+
}
1636+
} else {
1637+
expr.lower_uncorrelated()?
1638+
}
1639+
}
16281640
CallBinary { func, expr1, expr2 } => SS::CallBinary {
16291641
func,
16301642
expr1: Box::new(expr1.lower_uncorrelated()?),

src/transform/src/column_knowledge.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ impl DatumKnowledge {
635635
unreachable!();
636636
};
637637

638-
if !s_typ.base_eq(o_typ) {
638+
if !s_typ.physical_eq(o_typ) {
639639
::tracing::error!("Undefined join of non-equal base types {s_typ:?} != {o_typ:?}");
640640
*self = Self::top();
641641
} else if s_val != o_val {
@@ -735,7 +735,7 @@ impl DatumKnowledge {
735735
unreachable!();
736736
};
737737

738-
if !s_typ.base_eq(o_typ) {
738+
if !s_typ.physical_eq(o_typ) {
739739
soft_panic_or_log!("Undefined meet of non-equal base types {s_typ:?} != {o_typ:?}");
740740
*self = Self::top(); // this really should be Nothing
741741
} else if s_val != o_val {

src/transform/src/normalize_lets.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ mod support {
318318
.column_types
319319
.iter()
320320
.zip(typ.column_types.iter())
321-
.all(|(t1, t2)| t1.scalar_type.base_eq(&t2.scalar_type))
321+
.all(|(t1, t2)| t1.scalar_type.physical_eq(&t2.scalar_type))
322322
{
323323
Err(crate::TransformError::Internal(format!(
324324
"scalar types do not match: {:?} v {:?}",

src/transform/src/typecheck.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,7 @@ pub fn scalar_subtype_difference(sub: &ScalarType, sup: &ScalarType) -> Vec<Colu
397397
}
398398
}
399399
(_, _) => {
400-
// TODO(mgree) confirm that we don't want to allow numeric subtyping
401-
if ScalarBaseType::from(sub) != ScalarBaseType::from(sup) {
400+
if !sub.physical_eq(sup) {
402401
diffs.push(ColumnTypeDifference::NotSubtype {
403402
sub: sub.clone(),
404403
sup: sup.clone(),

src/transform/tests/test_transforms/typecheck.spec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ Return
225225
mismatched column types: couldn't compute union of column types in let rec: Can't union types: Bool and Int64
226226
got Int64
227227
expected Bool?
228-
Bool is a not a subtype of Int64
228+
Bool is not a subtype of Int64
229229
Bool? is nullable but Int64 is not
230230
----
231231
----
@@ -344,7 +344,7 @@ Filter #2
344344
mismatched column types: expected boolean condition
345345
got String
346346
expected Bool?
347-
String is a not a subtype of Bool
347+
String is not a subtype of Bool
348348
----
349349
----
350350

test/sqllogictest/freshmart.slt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ Explained Query:
291291
Get l6
292292
Return
293293
Project (#0, #13, #11)
294-
Map (case when (#8) IS NULL then null else #7 end, (((((#2 * case when (#3 <= 3) then 1.2 else case when ((#3 <= 10) AND (#3 >= 4)) then 1.1 else 0.9 end end) * coalesce((1 - (#5 / 100)), 1)) * case when (#12 <= 3) then 1.1 else case when ((#12 <= 10) AND (#12 >= 4)) then 1.05 else 1 end end) * case when (#2 > #4) then (1 + ((#2 - #4) / #4)) else (1 - ((#4 - #2) / #4)) end) * case when ilike["%cheap%"](varchar_to_text(#1)) then 0.8 else 1 end))
294+
Map (case when (#8) IS NULL then null else #7 end, (((((#2 * case when (#3 <= 3) then 1.2 else case when ((#3 <= 10) AND (#3 >= 4)) then 1.1 else 0.9 end end) * coalesce((1 - (#5 / 100)), 1)) * case when (#12 <= 3) then 1.1 else case when ((#12 <= 10) AND (#12 >= 4)) then 1.05 else 1 end end) * case when (#2 > #4) then (1 + ((#2 - #4) / #4)) else (1 - ((#4 - #2) / #4)) end) * case when ilike["%cheap%"](#1) then 0.8 else 1 end))
295295
Join on=(#0 = #6 = #9 = #10) type=delta
296296
ArrangeBy keys=[[#0]]
297297
Get l6
@@ -639,7 +639,7 @@ Explained Query:
639639
Get l7
640640
cte l11 =
641641
Project (#0, #1, #3, #5, #18)
642-
Map (case when (#14) IS NULL then null else #13 end, (((((#8 * case when (#9 <= 3) then 1.2 else case when ((#9 <= 10) AND (#9 >= 4)) then 1.1 else 0.9 end end) * coalesce((1 - (#11 / 100)), 1)) * case when (#17 <= 3) then 1.1 else case when ((#17 <= 10) AND (#17 >= 4)) then 1.05 else 1 end end) * case when (#8 > #10) then (1 + ((#8 - #10) / #10)) else (1 - ((#10 - #8) / #10)) end) * case when ilike["%cheap%"](varchar_to_text(#7)) then 0.8 else 1 end))
642+
Map (case when (#14) IS NULL then null else #13 end, (((((#8 * case when (#9 <= 3) then 1.2 else case when ((#9 <= 10) AND (#9 >= 4)) then 1.1 else 0.9 end end) * coalesce((1 - (#11 / 100)), 1)) * case when (#17 <= 3) then 1.1 else case when ((#17 <= 10) AND (#17 >= 4)) then 1.05 else 1 end end) * case when (#8 > #10) then (1 + ((#8 - #10) / #10)) else (1 - ((#10 - #8) / #10)) end) * case when ilike["%cheap%"](#7) then 0.8 else 1 end))
643643
Join on=(#0 = #2 = #6 = #12 = #15 = #16 AND #3 = #4) type=delta
644644
ArrangeBy keys=[[#0]]
645645
Project (#0, #1)
@@ -895,7 +895,7 @@ Explained Query:
895895
Get l7
896896
cte l11 =
897897
Project (#0, #1, #3, #5, #18)
898-
Map (case when (#14) IS NULL then null else #13 end, (((((#8 * case when (#9 <= 3) then 1.2 else case when ((#9 <= 10) AND (#9 >= 4)) then 1.1 else 0.9 end end) * coalesce((1 - (#11 / 100)), 1)) * case when (#17 <= 3) then 1.1 else case when ((#17 <= 10) AND (#17 >= 4)) then 1.05 else 1 end end) * case when (#8 > #10) then (1 + ((#8 - #10) / #10)) else (1 - ((#10 - #8) / #10)) end) * case when ilike["%cheap%"](varchar_to_text(#7)) then 0.8 else 1 end))
898+
Map (case when (#14) IS NULL then null else #13 end, (((((#8 * case when (#9 <= 3) then 1.2 else case when ((#9 <= 10) AND (#9 >= 4)) then 1.1 else 0.9 end end) * coalesce((1 - (#11 / 100)), 1)) * case when (#17 <= 3) then 1.1 else case when ((#17 <= 10) AND (#17 >= 4)) then 1.05 else 1 end end) * case when (#8 > #10) then (1 + ((#8 - #10) / #10)) else (1 - ((#10 - #8) / #10)) end) * case when ilike["%cheap%"](#7) then 0.8 else 1 end))
899899
Join on=(#0 = #2 = #6 = #12 = #15 = #16 AND #3 = #4) type=delta
900900
ArrangeBy keys=[[#0]]
901901
Project (#0, #1)

0 commit comments

Comments
 (0)