Skip to content
Closed
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
6 changes: 6 additions & 0 deletions core/src/main/java/org/apache/calcite/rex/RexBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,12 @@ public RexNode ensureType(
if (matchNullability) {
targetType = matchNullability(type, node);
}

if (targetType.getSqlTypeName() == SqlTypeName.ANY
&& (!matchNullability
|| (matchNullability && targetType.isNullable() == node.getType().isNullable()))) {
return node;
}
if (!node.getType().equals(targetType)) {
return makeCast(targetType, node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,18 @@ private RelDataType leastRestrictiveSqlType(List<RelDataType> types) {
int nullCount = 0;
int nullableCount = 0;
int javaCount = 0;
int anyCount = 0;

for (RelDataType type : types) {
final SqlTypeName typeName = type.getSqlTypeName();
if (typeName == null) {
return null;
}

if (typeName == SqlTypeName.ANY) {
anyCount++;
}

if (type.isNullable()) {
++nullableCount;
}
Expand All @@ -236,6 +242,12 @@ private RelDataType leastRestrictiveSqlType(List<RelDataType> types) {
}
}


// if any of the inputs are ANY, the output is ANY
if (anyCount > 0) {
return createTypeWithNullability(createSqlType(SqlTypeName.ANY), nullCount > 0);
}

for (int i = 0; i < types.size(); ++i) {
RelDataType type = types.get(i);
RelDataTypeFamily family = type.getFamily();
Expand Down
84 changes: 84 additions & 0 deletions core/src/test/java/org/apache/calcite/rex/RexBuilderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.calcite.rex;

import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rel.type.RelDataTypeSystem;
import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
import org.apache.calcite.sql.type.SqlTypeName;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

/**
* Test for {@link RexBuilder}
*/
public class RexBuilderTest {

/**
* Test RexBuilder.ensureType()
*/
@Test
public void testEnsureTypeWithAny() {
final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
RexBuilder builder = new RexBuilder(typeFactory);

RexNode node = new RexLiteral(
Boolean.TRUE, typeFactory.createSqlType(SqlTypeName.BOOLEAN), SqlTypeName.BOOLEAN);
RexNode ensuredNode = builder.ensureType(
typeFactory.createSqlType(SqlTypeName.ANY), node, true);

assertEquals(node, ensuredNode);
}

/**
* Test RexBuilder.ensureType()
*/
@Test
public void testEnsureTypeWithItself() {
final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
RexBuilder builder = new RexBuilder(typeFactory);

RexNode node = new RexLiteral(
Boolean.TRUE, typeFactory.createSqlType(SqlTypeName.BOOLEAN), SqlTypeName.BOOLEAN);
RexNode ensuredNode = builder.ensureType(
typeFactory.createSqlType(SqlTypeName.BOOLEAN), node, true);

assertEquals(node, ensuredNode);
}

/**
* Test RexBuilder.ensureType()
*/
@Test
public void testEnsureTypeWithDifference() {
final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
RexBuilder builder = new RexBuilder(typeFactory);

RexNode node = new RexLiteral(
Boolean.TRUE, typeFactory.createSqlType(SqlTypeName.BOOLEAN), SqlTypeName.BOOLEAN);
RexNode ensuredNode = builder.ensureType(
typeFactory.createSqlType(SqlTypeName.INTEGER), node, true);

assertNotEquals(node, ensuredNode);
assertEquals(ensuredNode.getType(), typeFactory.createSqlType(SqlTypeName.INTEGER));
}

}
// End RexBuilderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.calcite.sql.type;

import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeSystem;

import com.google.common.collect.Lists;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Test for {@link SqlTypeFactoryImpl}
*/
public class SqlTypeFactoryTest {

@Test
public void testLeastRestrictiveWithAny() {
SqlTypeFactoryImpl typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
final RelDataType sqlBigInt = typeFactory.createSqlType(SqlTypeName.BIGINT);
final RelDataType sqlAny = typeFactory.createSqlType(SqlTypeName.ANY);

RelDataType leastRestrictive =
typeFactory.leastRestrictive(Lists.newArrayList(sqlBigInt, sqlAny));
assertEquals(leastRestrictive.getSqlTypeName(), SqlTypeName.ANY);
}

@Test
public void testLeastRestrictiveWithNumbers() {
SqlTypeFactoryImpl typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
final RelDataType sqlBigInt = typeFactory.createSqlType(SqlTypeName.BIGINT);
final RelDataType sqlInt = typeFactory.createSqlType(SqlTypeName.INTEGER);

RelDataType leastRestrictive =
typeFactory.leastRestrictive(Lists.newArrayList(sqlBigInt, sqlInt));
assertEquals(leastRestrictive.getSqlTypeName(), SqlTypeName.BIGINT);
}
}
// End SqlTypeFactoryTest.java
4 changes: 4 additions & 0 deletions core/src/test/java/org/apache/calcite/test/CalciteSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.calcite.plan.volcano.VolcanoPlannerTraitTest;
import org.apache.calcite.rel.RelCollationTest;
import org.apache.calcite.rel.rel2sql.RelToSqlConverterTest;
import org.apache.calcite.rex.RexBuilderTest;
import org.apache.calcite.rex.RexExecutorTest;
import org.apache.calcite.runtime.BinarySearchTest;
import org.apache.calcite.runtime.EnumerablesTest;
Expand All @@ -36,6 +37,7 @@
import org.apache.calcite.sql.test.SqlOperatorTest;
import org.apache.calcite.sql.test.SqlPrettyWriterTest;
import org.apache.calcite.sql.test.SqlTypeNameTest;
import org.apache.calcite.sql.type.SqlTypeFactoryTest;
import org.apache.calcite.test.enumerable.EnumerableCorrelateTest;
import org.apache.calcite.tools.FrameworksTest;
import org.apache.calcite.tools.PlannerTest;
Expand Down Expand Up @@ -94,6 +96,8 @@
ExceptionMessageTest.class,
InduceGroupingTypeTest.class,
RelOptPlanReaderTest.class,
RexBuilderTest.class,
SqlTypeFactoryTest.class,

// medium tests (above 0.1s)
SqlParserTest.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3363,7 +3363,7 @@ LogicalProject(**=[$0], **0=[$1])
<![CDATA[
LogicalAggregate(group=[{0}], CNT=[COUNT()])
LogicalProject(N_REGIONKEY=[ITEM($0, 'N_REGIONKEY')])
LogicalFilter(condition=[>(CAST(ITEM($0, 'N_NATIONKEY')):INTEGER, 5)])
LogicalFilter(condition=[>(ITEM($0, 'N_NATIONKEY'), 5)])
LogicalProject(**=[$0])
LogicalTableScan(table=[[CATALOG, SALES, NATION]])
]]>
Expand Down Expand Up @@ -3391,7 +3391,7 @@ LogicalProject(**=[$0], **0=[$1])
<Resource name="plan">
<![CDATA[
LogicalProject(**=[$1], R_REGIONKEY=[$2], R_NAME=[$3], R_COMMENT=[$4])
LogicalFilter(condition=[=(CAST($0):INTEGER, $2)])
LogicalFilter(condition=[=($0, $2)])
LogicalJoin(condition=[true], joinType=[inner])
LogicalTableScan(table=[[CATALOG, SALES, NATION]])
LogicalTableScan(table=[[CATALOG, SALES, REGION]])
Expand Down