Skip to content

Commit

Permalink
Apply all feedback and bring up to speed with the Looker fork
Browse files Browse the repository at this point in the history
  • Loading branch information
wnob committed Jan 5, 2022
1 parent e88c35e commit 211330a
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,11 @@ private SqlNode callToSql(@Nullable RexProgram program, RexCall call0,
SqlNode fieldOperand = field(ordinal);
return SqlStdOperatorTable.CURSOR.createCall(SqlParserPos.ZERO, fieldOperand);
}
// Ideally the UNKNOWN type would never exist in a fully-formed, validated rel node, but
// it can be useful in certain situations where determining the type of an expression is
// infeasible, such as inserting arbitrary user-provided SQL snippets into an otherwise
// manually-constructed (as opposed to parsed) rel node.
// In such a context, assume that casting anything to UNKNOWN is a no-op.
if (ignoreCast || call.getType().getSqlTypeName() == SqlTypeName.UNKNOWN) {
assert nodeList.size() == 1;
return nodeList.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ private SqlTypeName toVar(RelDataType type) {
return SqlTypeName.ANY;
case NULL:
return SqlTypeName.NULL;
case UNKNOWN:
return SqlTypeName.UNKNOWN;
default:
throw Util.unexpected(sqlTypeName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ public static SqlDataTypeSpec convertTypeToSpec(RelDataType type,
assert typeName != null;

final SqlTypeNameSpec typeNameSpec;
if (isAtomic(type) || isNull(type)) {
if (isAtomic(type) || isNull(type) || type.getSqlTypeName() == SqlTypeName.UNKNOWN) {
int precision = typeName.allowsPrec() ? type.getPrecision() : -1;
// fix up the precision.
if (maxPrecision > 0 && precision > maxPrecision) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private void checkCreateSqlTypeWithPrecision(
}

/** Test that the {code UNKNOWN} type does not does not change class when nullified. */
@Test void testUnknownCreateWithNullabiliityTypeConsistency() {
@Test void testUnknownCreateWithNullabilityTypeConsistency() {
SqlTypeFixture f = new SqlTypeFixture();

RelDataType unknownType = f.typeFactory.createUnknownType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class SqlTypeFixture {
typeFactory.createSqlType(SqlTypeName.VARCHAR), true);
final RelDataType sqlNull = typeFactory.createTypeWithNullability(
typeFactory.createSqlType(SqlTypeName.NULL), false);
final RelDataType sqlUnknown = typeFactory.createTypeWithNullability(
typeFactory.createSqlType(SqlTypeName.UNKNOWN), false);
final RelDataType sqlAny = typeFactory.createTypeWithNullability(
typeFactory.createSqlType(SqlTypeName.ANY), false);
final RelDataType sqlFloat = typeFactory.createTypeWithNullability(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Test of {@link org.apache.calcite.sql.type.SqlTypeUtil}.
Expand Down Expand Up @@ -156,6 +155,10 @@ class SqlTypeUtilTest {
(SqlBasicTypeNameSpec) convertTypeToSpec(f.sqlNull).getTypeNameSpec();
assertThat(nullSpec.getTypeName().getSimple(), is("NULL"));

SqlBasicTypeNameSpec unknownSpec =
(SqlBasicTypeNameSpec) convertTypeToSpec(f.sqlUnknown).getTypeNameSpec();
assertThat(unknownSpec.getTypeName().getSimple(), is("UNKNOWN"));

SqlBasicTypeNameSpec basicSpec =
(SqlBasicTypeNameSpec) convertTypeToSpec(f.sqlBigInt).getTypeNameSpec();
assertThat(basicSpec.getTypeName().getSimple(), is("BIGINT"));
Expand Down Expand Up @@ -242,14 +245,21 @@ private void compareTypesIgnoringNullability(
}
BasicSqlType nullableFromType = fromType.createWithNullability(!fromType.isNullable);

assertTrue(SqlTypeUtil.canCastFrom(unknownType, fromType, false));
assertTrue(SqlTypeUtil.canCastFrom(unknownType, fromType, true));
assertTrue(SqlTypeUtil.canCastFrom(unknownType, nullableFromType, false));
assertTrue(SqlTypeUtil.canCastFrom(unknownType, nullableFromType, true));
assertTrue(SqlTypeUtil.canCastFrom(nullableUnknownType, fromType, false));
assertTrue(SqlTypeUtil.canCastFrom(nullableUnknownType, fromType, true));
assertTrue(SqlTypeUtil.canCastFrom(nullableUnknownType, nullableFromType, false));
assertTrue(SqlTypeUtil.canCastFrom(nullableUnknownType, nullableFromType, true));
assertCanCast(unknownType, fromType);
assertCanCast(unknownType, nullableFromType);
assertCanCast(nullableUnknownType, fromType);
assertCanCast(nullableUnknownType, nullableFromType);
}
}

private static void assertCanCast(RelDataType toType, RelDataType fromType) {
assertThat(
String.format(
"Expected to be able to cast from %s to %s without coercion.", fromType, toType),
SqlTypeUtil.canCastFrom(toType, fromType, /* coerce= */ false), is(true));
assertThat(
String.format(
"Expected to be able to cast from %s to %s with coercion.", fromType, toType),
SqlTypeUtil.canCastFrom(toType, fromType, /* coerce= */ true), is(true));
}
}
3 changes: 2 additions & 1 deletion site/_docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,8 @@ Note:

| Type | Description | Example literals
|:-------- |:---------------------------|:---------------
| ANY | A value of an unknown type |
| ANY | A value of an unknown type, subject to explicit casting to maintain type consistency in expressions |
| UNKNOWN | A value of an unknown type, which Calcite assume's will simply work "as is" without any explicit casting |
| ROW | Row with 1 or more columns | Example: Row(f0 int null, f1 varchar)
| MAP | Collection of keys mapped to values |
| MULTISET | Unordered collection that may contain duplicates | Example: int multiset
Expand Down

0 comments on commit 211330a

Please sign in to comment.