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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.ignite.internal.processors.query.calcite.exec;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -236,6 +237,13 @@ public IgniteLogger logger() {
return baseDataContext.get(name);
}

/** */
public Object getParameter(String name, Type storageType) {
assert name.startsWith("?") : name;

return TypeUtils.toInternal(this, params.get(name), storageType);
}

/**
* Gets correlated value.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.exec.exp;

import java.math.BigDecimal;
import java.math.RoundingMode;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rex.RexBuilder;
import org.apache.calcite.rex.RexLiteral;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.ignite.internal.processors.query.calcite.util.TypeUtils;
import org.checkerframework.checker.nullness.qual.Nullable;

/** */
public class IgniteRexBuilder extends RexBuilder {
/** */
public IgniteRexBuilder(RelDataTypeFactory typeFactory) {
super(typeFactory);
}

/** {@inheritDoc} */
@Override protected RexLiteral makeLiteral(@Nullable Comparable o, RelDataType type, SqlTypeName typeName) {
if (o != null && typeName == SqlTypeName.DECIMAL && TypeUtils.hasScale(type))
return super.makeLiteral(((BigDecimal)o).setScale(type.getScale(), RoundingMode.HALF_UP), type, typeName);

return super.makeLiteral(o, type, typeName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import org.apache.calcite.util.BuiltInMethod;
import org.apache.calcite.util.ControlFlowException;
import org.apache.calcite.util.Pair;
import org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory;
import org.apache.ignite.internal.processors.query.calcite.util.IgniteMethod;

import static org.apache.calcite.sql.fun.SqlStdOperatorTable.CASE;
Expand Down Expand Up @@ -176,7 +177,7 @@ public static List<Expression> translateProjects(RexProgram program,
storageTypes.add(outputPhysType.getJavaFieldType(i));
}
return new RexToLixTranslator(program, typeFactory, root, inputGetter,
list, new RexBuilder(typeFactory), conformance, null)
list, new IgniteRexBuilder(typeFactory), conformance, null)
.setCorrelates(correlates)
.translateList(program.getProjectList(), storageTypes);
}
Expand Down Expand Up @@ -1188,10 +1189,15 @@ private Result toInnerStorageType(final Result result, final Type storageType) {

final Type storageType = currentStorageType != null
? currentStorageType : typeFactory.getJavaClass(dynamicParam.getType());
final Expression valueExpression = ConverterUtils.convert(
Expressions.call(root, BuiltInMethod.DATA_CONTEXT_GET.method,
Expressions.constant("?" + dynamicParam.getIndex())),
storageType);

final Type paramType = ((IgniteTypeFactory)typeFactory).getResultClass(dynamicParam.getType());

final Expression ctxGet = Expressions.call(root, IgniteMethod.CONTEXT_GET_PARAMETER_VALUE.method(),
Expressions.constant("?" + dynamicParam.getIndex()), Expressions.constant(paramType));

final Expression valueExpression = SqlTypeUtil.isDecimal(dynamicParam.getType()) ?
ConverterUtils.convertToDecimal(ctxGet, dynamicParam.getType()) : ConverterUtils.convert(ctxGet, storageType);

final ParameterExpression valueVariable =
Expressions.parameter(valueExpression.getType(), list.newName("value_dynamic_param"));
list.add(Expressions.declare(Modifier.FINAL, valueVariable, valueExpression));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.ignite.IgniteException;
import org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext;
import org.apache.ignite.internal.processors.query.calcite.exec.RowHandler;
import org.apache.ignite.internal.processors.query.calcite.exec.exp.IgniteRexBuilder;
import org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory;
import org.apache.ignite.internal.processors.query.calcite.util.Commons;
import org.apache.ignite.internal.util.typedef.F;
Expand Down Expand Up @@ -118,7 +119,7 @@ private static Function<Object, Object> compileCast(IgniteTypeFactory typeFactor
PhysTypeImpl.of(typeFactory, rowType,
JavaRowFormat.SCALAR, false))));

RexBuilder builder = new RexBuilder(typeFactory);
RexBuilder builder = new IgniteRexBuilder(typeFactory);
RexProgramBuilder programBuilder = new RexProgramBuilder(rowType, builder);
RexNode cast = builder.makeCast(to, builder.makeInputRef(from, 0));
programBuilder.addProject(cast, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.apache.calcite.tools.Frameworks;
import org.apache.ignite.IgniteLogger;
import org.apache.ignite.internal.processors.query.GridQueryCancel;
import org.apache.ignite.internal.processors.query.calcite.exec.exp.IgniteRexBuilder;
import org.apache.ignite.internal.processors.query.calcite.metadata.cost.IgniteCostFactory;
import org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory;
import org.apache.ignite.logger.NullLogger;
Expand Down Expand Up @@ -104,7 +105,7 @@ public final class BaseQueryContext extends AbstractQueryContext {
RelDataTypeSystem typeSys = CALCITE_CONNECTION_CONFIG.typeSystem(RelDataTypeSystem.class, FRAMEWORK_CONFIG.getTypeSystem());
TYPE_FACTORY = new IgniteTypeFactory(typeSys);

REX_BUILDER = new RexBuilder(TYPE_FACTORY);
REX_BUILDER = new IgniteRexBuilder(TYPE_FACTORY);

CLUSTER = RelOptCluster.create(EMPTY_PLANNER, REX_BUILDER);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Objects;

import org.apache.calcite.rel.type.RelDataType;
import org.apache.ignite.internal.processors.query.calcite.util.TypeUtils;
import org.jetbrains.annotations.Nullable;

/** Definies a particular column within table. */
Expand Down Expand Up @@ -71,13 +72,13 @@ public boolean nullable() {
* @return Column's precision.
*/
public Integer precision() {
return type.getPrecision() != RelDataType.PRECISION_NOT_SPECIFIED ? type.getPrecision() : null;
return TypeUtils.hasPrecision(type) ? type.getPrecision() : null;
}

/**
* @return Column's scale.
*/
public Integer scale() {
return type.getScale() != RelDataType.SCALE_NOT_SPECIFIED ? type.getScale() : null;
return TypeUtils.hasScale(type) ? type.getScale() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.apache.calcite.rel.type.RelDataType.PRECISION_NOT_SPECIFIED;
import static org.apache.calcite.rel.type.RelDataType.SCALE_NOT_SPECIFIED;

/**
*
Expand Down Expand Up @@ -685,7 +687,7 @@ private KeyValDescriptor(String name, Class<?> type, boolean isKey, int fieldIdx
/** {@inheritDoc} */
@Override public RelDataType logicalType(IgniteTypeFactory f) {
if (logicalType == null)
logicalType = TypeUtils.sqlType(f, f.createJavaType(storageType));
logicalType = TypeUtils.sqlType(f, storageType, PRECISION_NOT_SPECIFIED, SCALE_NOT_SPECIFIED);

return logicalType;
}
Expand Down Expand Up @@ -764,8 +766,11 @@ private FieldDescriptor(GridQueryProperty desc, int fieldIdx) {

/** {@inheritDoc} */
@Override public RelDataType logicalType(IgniteTypeFactory f) {
if (logicalType == null)
logicalType = TypeUtils.sqlType(f, f.createJavaType(storageType));
if (logicalType == null) {
logicalType = TypeUtils.sqlType(f, storageType,
desc.precision() == -1 ? PRECISION_NOT_SPECIFIED : desc.precision(),
desc.scale() == -1 ? SCALE_NOT_SPECIFIED : desc.scale());
}

return logicalType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import org.apache.ignite.spi.systemview.view.SystemViewRowAttributeWalker;
import org.jetbrains.annotations.Nullable;

import static org.apache.calcite.rel.type.RelDataType.PRECISION_NOT_SPECIFIED;
import static org.apache.calcite.rel.type.RelDataType.SCALE_NOT_SPECIFIED;
import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.toSqlName;

/**
Expand Down Expand Up @@ -259,7 +261,7 @@ private SystemViewColumnDescriptorImpl(String name, Class<?> type, int fieldIdx,
/** {@inheritDoc} */
@Override public RelDataType logicalType(IgniteTypeFactory f) {
if (logicalType == null)
logicalType = TypeUtils.sqlType(f, f.createJavaType(type));
logicalType = TypeUtils.sqlType(f, type, PRECISION_NOT_SPECIFIED, SCALE_NOT_SPECIFIED);

return logicalType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.ignite.internal.processors.query.calcite.util;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import org.apache.calcite.avatica.util.ByteString;
import org.apache.calcite.linq4j.tree.Types;
import org.apache.calcite.sql.SqlIntervalQualifier;
Expand Down Expand Up @@ -53,6 +54,9 @@ public enum IgniteMethod {
/** See {@link ExecutionContext#getCorrelated(int)} */
CONTEXT_GET_CORRELATED_VALUE(ExecutionContext.class, "getCorrelated", int.class),

/** See {@link ExecutionContext#getParameter(String, Type)} */
CONTEXT_GET_PARAMETER_VALUE(ExecutionContext.class, "getParameter", String.class, Type.class),

/** See {@link SingleScalar#execute(ExecutionContext, Object, Object)} */
SCALAR_EXECUTE(SingleScalar.class, "execute", ExecutionContext.class, Object.class, Object.class),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.ignite.internal.processors.query.calcite.util;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
Expand Down Expand Up @@ -64,6 +65,7 @@
import org.apache.ignite.internal.processors.query.calcite.schema.ColumnDescriptor;
import org.apache.ignite.internal.processors.query.calcite.schema.TableDescriptor;
import org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory;
import org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeSystem;
import org.apache.ignite.internal.util.typedef.F;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -168,7 +170,18 @@ private static RelDataType createRowType(IgniteTypeFactory typeFactory, List<Rel
}

/** */
public static RelDataType sqlType(IgniteTypeFactory typeFactory, RelDataType rowType) {
public static RelDataType sqlType(IgniteTypeFactory typeFactory, Class<?> cls, int precision, int scale) {
RelDataType javaType = typeFactory.createJavaType(cls);

if (javaType.getSqlTypeName().allowsPrecScale(true, true) &&
(precision != RelDataType.PRECISION_NOT_SPECIFIED || scale != RelDataType.SCALE_NOT_SPECIFIED))
return typeFactory.createSqlType(javaType.getSqlTypeName(), precision, scale);

return sqlType(typeFactory, javaType);
}

/** */
private static RelDataType sqlType(IgniteTypeFactory typeFactory, RelDataType rowType) {
if (!rowType.isStruct())
return typeFactory.toSql(rowType);

Expand Down Expand Up @@ -273,6 +286,26 @@ private static boolean hasConvertableFields(RelDataType resultType) {
.anyMatch(TypeUtils::isConvertableType);
}

/** */
public static boolean hasPrecision(RelDataType type) {
// Special case for DECIMAL type without precision and scale specified.
if (type.getSqlTypeName() == SqlTypeName.DECIMAL &&
type.getPrecision() == IgniteTypeSystem.INSTANCE.getDefaultPrecision(SqlTypeName.DECIMAL))
return false;

return type.getPrecision() != RelDataType.PRECISION_NOT_SPECIFIED;
}

/** */
public static boolean hasScale(RelDataType type) {
// Special case for DECIMAL type without precision and scale specified.
if (type.getSqlTypeName() == SqlTypeName.DECIMAL &&
type.getPrecision() == IgniteTypeSystem.INSTANCE.getDefaultPrecision(SqlTypeName.DECIMAL))
return false;

return type.getScale() != RelDataType.SCALE_NOT_SPECIFIED;
}

/** */
public static Object toInternal(DataContext ctx, Object val) {
return val == null ? null : toInternal(ctx, val, val.getClass());
Expand All @@ -298,6 +331,19 @@ else if (storageType == Period.class)
return (int)((Period)val).toTotalMonths();
else if (storageType == byte[].class)
return new ByteString((byte[])val);
else if (val instanceof Number && storageType != val.getClass()) {
// For dynamic parameters we don't know exact parameter type in compile time. To avoid casting errors in
// runtime we should convert parameter value to expected type.
Number num = (Number)val;

return Byte.class.equals(storageType) || byte.class.equals(storageType) ? SqlFunctions.toByte(num) :
Short.class.equals(storageType) || short.class.equals(storageType) ? SqlFunctions.toShort(num) :
Integer.class.equals(storageType) || int.class.equals(storageType) ? SqlFunctions.toInt(num) :
Long.class.equals(storageType) || long.class.equals(storageType) ? SqlFunctions.toLong(num) :
Float.class.equals(storageType) || float.class.equals(storageType) ? SqlFunctions.toFloat(num) :
Double.class.equals(storageType) || double.class.equals(storageType) ? SqlFunctions.toDouble(num) :
BigDecimal.class.equals(storageType) ? SqlFunctions.toBigDecimal(num) : num;
}
else
return val;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
* limitations under the License.
*/

package org.apache.ignite.internal.processors.query.calcite;
package org.apache.ignite.internal.processors.query.calcite.integration;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
Expand All @@ -25,7 +26,6 @@
import org.apache.ignite.IgniteCache;
import org.apache.ignite.cache.QueryEntity;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.internal.processors.query.calcite.integration.AbstractBasicIntegrationTest;
import org.apache.ignite.internal.util.typedef.F;
import org.junit.Test;

Expand Down Expand Up @@ -345,4 +345,60 @@ public void testBinaryConcat() {
executeSql("DROP TABLE IF EXISTS tbl");
}
}

/** */
@Test
public void testDecimalScale() {
sql("CREATE TABLE t (id INT PRIMARY KEY, val1 DECIMAL(5, 3), val2 DECIMAL(3), val3 DECIMAL)");

// Check literals scale.
sql("INSERT INTO t values (0, 0, 0, 0)");
sql("INSERT INTO t values (1.1, 1.1, 1.1, 1.1)");
sql("INSERT INTO t values (2.123, 2.123, 2.123, 2.123)");
sql("INSERT INTO t values (3.123456, 3.123456, 3.123456, 3.123456)");

// Check dynamic parameters scale.
List<Number> params = F.asList(4, 5L, 6f, 7.25f, 8d, 9.03125d, new BigDecimal("10"),
new BigDecimal("11.1"), new BigDecimal("12.123456"));

for (Object val : params)
sql("INSERT INTO t values (?, ?, ?, ?)", val, val, val, val);

assertQuery("SELECT * FROM t")
.returns(0, new BigDecimal("0.000"), new BigDecimal("0"), new BigDecimal("0"))
.returns(1, new BigDecimal("1.100"), new BigDecimal("1"), new BigDecimal("1.1"))
.returns(2, new BigDecimal("2.123"), new BigDecimal("2"), new BigDecimal("2.123"))
.returns(3, new BigDecimal("3.123"), new BigDecimal("3"), new BigDecimal("3.123456"))
.returns(4, new BigDecimal("4.000"), new BigDecimal("4"), new BigDecimal("4"))
.returns(5, new BigDecimal("5.000"), new BigDecimal("5"), new BigDecimal("5"))
.returns(6, new BigDecimal("6.000"), new BigDecimal("6"), new BigDecimal("6"))
.returns(7, new BigDecimal("7.250"), new BigDecimal("7"), new BigDecimal("7.25"))
.returns(8, new BigDecimal("8.000"), new BigDecimal("8"), new BigDecimal("8"))
.returns(9, new BigDecimal("9.031"), new BigDecimal("9"), new BigDecimal("9.03125"))
.returns(10, new BigDecimal("10.000"), new BigDecimal("10"), new BigDecimal("10"))
.returns(11, new BigDecimal("11.100"), new BigDecimal("11"), new BigDecimal("11.1"))
.returns(12, new BigDecimal("12.123"), new BigDecimal("12"), new BigDecimal("12.123456"))
.check();
}

/** */
@Test
public void testNumericConversion() {
sql("CREATE TABLE t (v1 TINYINT, v2 SMALLINT, v3 INT, v4 BIGINT, v5 DECIMAL, v6 FLOAT, v7 DOUBLE)");

List<Number> params = F.asList((byte)1, (short)2, 3, 4L, BigDecimal.valueOf(5), 6f, 7d);

for (Object val : params)
sql("INSERT INTO t values (?, ?, ?, ?, ?, ?, ?)", val, val, val, val, val, val, val);

assertQuery("SELECT * FROM t")
.returns((byte)1, (short)1, 1, 1L, BigDecimal.valueOf(1), 1f, 1d)
.returns((byte)2, (short)2, 2, 2L, BigDecimal.valueOf(2), 2f, 2d)
.returns((byte)3, (short)3, 3, 3L, BigDecimal.valueOf(3), 3f, 3d)
.returns((byte)4, (short)4, 4, 4L, BigDecimal.valueOf(4), 4f, 4d)
.returns((byte)5, (short)5, 5, 5L, BigDecimal.valueOf(5), 5f, 5d)
.returns((byte)6, (short)6, 6, 6L, BigDecimal.valueOf(6), 6f, 6d)
.returns((byte)7, (short)7, 7, 7L, BigDecimal.valueOf(7), 7f, 7d)
.check();
}
}
Loading