Skip to content
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

IGNITE-23178 : Calcite. Least restrictive type priority DOUBLE over FLOAT #11520

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,18 @@ else if (type instanceof IgniteCustomType)
if (types.size() == 1 || allEquals(types))
return F.first(types);

return super.leastRestrictive(types);
RelDataType res = super.leastRestrictive(types);

// Calcite compares approximate numerics by their precisions. While FLOAT has the same precision as DOUBLE, the
// least restrictive may variate between them and issue FLOAT instead of DOUBLE. DOUBLE is more preferable.
if (res != null && res.getSqlTypeName() == SqlTypeName.FLOAT && types.size() > 1) {
for (RelDataType type : types) {
if (type.getSqlTypeName() == SqlTypeName.DOUBLE && type.getPrecision() >= res.getPrecision())
return type;
}
}

return res;
}

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
/*
* 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.ignite.internal.processors.query.calcite.planner;
Vladsz83 marked this conversation as resolved.
Show resolved Hide resolved

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeSystem;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory;
import org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeSystem;
import org.jetbrains.annotations.Nullable;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/** Test for numeric types precisions. */
public class NumericTypesPrecisionsTest {
/** */
private static final IgniteTypeFactory TYPE_FACTORY = new IgniteTypeFactory();
Vladsz83 marked this conversation as resolved.
Show resolved Hide resolved

/** */
private static final int STRING_PRECISION = 65536;

/** */
private static final int DECIMAL_PRECISION = 32767;

/** */
private static final int DECIMAL_SCALE = 32767;

/** */
private final RelDataTypeSystem typeSys = IgniteTypeSystem.INSTANCE;

/** */
private static final RelDataType TINYINT = TYPE_FACTORY.createSqlType(SqlTypeName.TINYINT);

/** */
private static final RelDataType SMALLINT = TYPE_FACTORY.createSqlType(SqlTypeName.SMALLINT);

/** */
private static final RelDataType INTEGER = TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER);

/** */
private static final RelDataType REAL = TYPE_FACTORY.createSqlType(SqlTypeName.REAL);

/** */
private static final RelDataType FLOAT = TYPE_FACTORY.createSqlType(SqlTypeName.FLOAT);

/** */
private static final RelDataType DOUBLE = TYPE_FACTORY.createSqlType(SqlTypeName.DOUBLE);

/** */
private static final RelDataType DECIMAL = TYPE_FACTORY.createSqlType(SqlTypeName.DECIMAL, 1000, 10);

/** */
private static final RelDataType BIGINT = TYPE_FACTORY.createSqlType(SqlTypeName.BIGINT);

/** */
private static final RelDataType[] TEST_SUITE = new RelDataType[] {TINYINT, SMALLINT, INTEGER, REAL, FLOAT, DOUBLE, DECIMAL, BIGINT};

/** */
@Test
public void testLeastRestrictiveTinyInt() {
RelDataType[] expected = new RelDataType[] {TINYINT, SMALLINT, INTEGER, FLOAT, REAL, DOUBLE, DECIMAL, BIGINT};

doTestExpectedLeastRestrictive(TINYINT, expected);
}

/** */
@Test
public void testLeastRestrictiveSmallInt() {
RelDataType[] expected = new RelDataType[] {SMALLINT, SMALLINT, INTEGER, FLOAT, REAL, DOUBLE, DECIMAL, BIGINT};

doTestExpectedLeastRestrictive(SMALLINT, expected);
}

/** */
@Test
public void testLeastRestrictiveInteger() {
RelDataType[] expected = new RelDataType[] {INTEGER, INTEGER, INTEGER, FLOAT, REAL, DOUBLE, DECIMAL, BIGINT};

doTestExpectedLeastRestrictive(INTEGER, expected);
}

/** */
@Test
public void testLeastRestrictiveFloat() {
RelDataType[] expected = new RelDataType[] {FLOAT, FLOAT, FLOAT, FLOAT, FLOAT, DOUBLE, DOUBLE, FLOAT};

doTestExpectedLeastRestrictive(FLOAT, expected);
}

/** */
@Test
public void testLeastRestrictiveDouble() {
RelDataType[] expected = new RelDataType[] {DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE};

doTestExpectedLeastRestrictive(DOUBLE, expected);
}

/** */
@Test
public void testLeastRestrictiveDecimal() {
RelDataType[] expected = new RelDataType[] {DECIMAL, DECIMAL, DECIMAL, DOUBLE, DOUBLE, DOUBLE, DECIMAL, DECIMAL};

doTestExpectedLeastRestrictive(DECIMAL, expected);
}

/** */
@Test
public void testLeastRestrictiveBigInt() {
RelDataType[] expected = new RelDataType[] {BIGINT, BIGINT, BIGINT, FLOAT, REAL, DOUBLE, DECIMAL, BIGINT};

doTestExpectedLeastRestrictive(BIGINT, expected);
}

/** */
@Test
public void testMaxPrecision() {
assertEquals(STRING_PRECISION, typeSys.getMaxPrecision(SqlTypeName.CHAR));
assertEquals(STRING_PRECISION, typeSys.getMaxPrecision(SqlTypeName.VARCHAR));
assertEquals(STRING_PRECISION, typeSys.getMaxPrecision(SqlTypeName.BINARY));
assertEquals(STRING_PRECISION, typeSys.getMaxPrecision(SqlTypeName.VARBINARY));

assertEquals(3, typeSys.getMaxPrecision(SqlTypeName.TINYINT));
assertEquals(5, typeSys.getMaxPrecision(SqlTypeName.SMALLINT));
assertEquals(10, typeSys.getMaxPrecision(SqlTypeName.INTEGER));
assertEquals(19, typeSys.getMaxPrecision(SqlTypeName.BIGINT));
assertEquals(7, typeSys.getMaxPrecision(SqlTypeName.REAL));
assertEquals(15, typeSys.getMaxPrecision(SqlTypeName.FLOAT));
assertEquals(15, typeSys.getMaxPrecision(SqlTypeName.DOUBLE));
assertEquals(DECIMAL_PRECISION, typeSys.getMaxPrecision(SqlTypeName.DECIMAL));

assertEquals(0, typeSys.getMaxPrecision(SqlTypeName.DATE));
assertEquals(3, typeSys.getMaxPrecision(SqlTypeName.TIME));
assertEquals(3, typeSys.getMaxPrecision(SqlTypeName.TIME_WITH_LOCAL_TIME_ZONE));
assertEquals(3, typeSys.getMaxPrecision(SqlTypeName.TIMESTAMP));
assertEquals(3, typeSys.getMaxPrecision(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE));
}

/** */
@Test
public void testDefaultPrecision() {
assertEquals(1, typeSys.getDefaultPrecision(SqlTypeName.CHAR));
assertEquals(RelDataType.PRECISION_NOT_SPECIFIED, typeSys.getDefaultPrecision(SqlTypeName.VARCHAR));
assertEquals(1, typeSys.getDefaultPrecision(SqlTypeName.BINARY));
assertEquals(RelDataType.PRECISION_NOT_SPECIFIED, typeSys.getDefaultPrecision(SqlTypeName.VARBINARY));

assertEquals(3, typeSys.getDefaultPrecision(SqlTypeName.TINYINT));
assertEquals(5, typeSys.getDefaultPrecision(SqlTypeName.SMALLINT));
assertEquals(10, typeSys.getDefaultPrecision(SqlTypeName.INTEGER));
assertEquals(19, typeSys.getDefaultPrecision(SqlTypeName.BIGINT));
assertEquals(7, typeSys.getDefaultPrecision(SqlTypeName.REAL));
assertEquals(15, typeSys.getDefaultPrecision(SqlTypeName.FLOAT));
assertEquals(15, typeSys.getDefaultPrecision(SqlTypeName.DOUBLE));
assertEquals(DECIMAL_PRECISION, typeSys.getDefaultPrecision(SqlTypeName.DECIMAL));

assertEquals(0, typeSys.getDefaultPrecision(SqlTypeName.DATE));
assertEquals(0, typeSys.getDefaultPrecision(SqlTypeName.TIME));
assertEquals(3, typeSys.getDefaultPrecision(SqlTypeName.TIME_WITH_LOCAL_TIME_ZONE));
assertEquals(3, typeSys.getDefaultPrecision(SqlTypeName.TIMESTAMP));
assertEquals(0, typeSys.getDefaultPrecision(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE));
}


/** */
@Test
public void testMaxNumericPrecision() {
assertEquals(DECIMAL_PRECISION, typeSys.getMaxNumericPrecision());
}

/** */
@Test
public void testMaxNumericScale() {
assertEquals(DECIMAL_SCALE, typeSys.getMaxNumericScale());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need these tests? Values for max/default prcision are hardcoded in calcite and looks like we just compare one hardcoded value with another one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We manually implement several values. Like:

 @Override public int getMaxNumericScale() {
        return Short.MAX_VALUE;
    }

    /** {@inheritDoc} */
    @Override public int getMaxNumericPrecision() {
        return Short.MAX_VALUE;
    }

or

 @Override public int getDefaultPrecision(SqlTypeName typeName) {
        // Timestamps internally stored as millis, precision more than 3 is redundant. At the same time,
        // default Calcite precision 0 causes truncation when converting to TIMESTAMP without specifying precision.
        if (typeName == SqlTypeName.TIMESTAMP || typeName == SqlTypeName.TIME_WITH_LOCAL_TIME_ZONE)
            return 3;

        return super.getDefaultPrecision(typeName);
    }

It looks usefult to be sure that nothing would change. Or we can remove the tests. WDYT?


/** */
private static final class TypesHolder {
/** */
private final RelDataType type1;

/** */
private final RelDataType type2;

/** */
private final RelDataType expectedLeast;

/** */
private TypesHolder(RelDataType type1, RelDataType type2, @Nullable RelDataType least) {
this.type1 = type1;
this.type2 = type2;
this.expectedLeast = least;
}
}

/** */
private static void doTestExpectedLeastRestrictive(RelDataType testType, RelDataType[] expectedLeast) {
assert expectedLeast.length == TEST_SUITE.length;

List<TypesHolder> types = new ArrayList<>(TEST_SUITE.length);

for (int i = 0; i < types.size(); ++i)
Vladsz83 marked this conversation as resolved.
Show resolved Hide resolved
types.add(new TypesHolder(testType, TEST_SUITE[i], expectedLeast[i]));

for (TypesHolder holder : types) {
RelDataType actualType = TYPE_FACTORY.leastRestrictive(Arrays.asList(holder.type1, holder.type2));

assertEquals("leastRestrictive(" + holder.type1 + ", " + holder.type2 + ")", holder.expectedLeast, actualType);

assertEquals("leastRestrictive(" + holder.type2 + ", " + holder.type1 + ")", holder.expectedLeast, actualType);
Vladsz83 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Vladsz83 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.ignite.internal.processors.query.calcite.exec.exp.IgniteSqlFunctionsTest;
import org.apache.ignite.internal.processors.query.calcite.exec.tracker.MemoryTrackerTest;
import org.apache.ignite.internal.processors.query.calcite.message.CalciteCommunicationMessageSerializationTest;
import org.apache.ignite.internal.processors.query.calcite.planner.NumericTypesPrecisionsTest;
import org.apache.ignite.internal.processors.query.calcite.sql.SqlCustomParserTest;
import org.apache.ignite.internal.processors.query.calcite.sql.SqlReservedWordsTest;
import org.junit.runner.RunWith;
Expand All @@ -47,6 +48,8 @@

ScriptTestSuite.class,
CalciteCommunicationMessageSerializationTest.class,

NumericTypesPrecisionsTest.class,
})
public class IgniteCalciteTestSuite {
}