korlov42 commented on a change in pull request #9199:
URL: https://github.com/apache/ignite/pull/9199#discussion_r658867489



##########
File path: 
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rex/RexExecutorImpl.java
##########
@@ -0,0 +1,189 @@
+/*
+ * 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.rex;

Review comment:
       it would be better to place this under 
`org.apache.ignite.internal.processors.query.calcite.exec.exp` package

##########
File path: 
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rex/RexExecutorImpl.java
##########
@@ -0,0 +1,189 @@
+/*
+ * 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.rex;
+
+import java.lang.reflect.Modifier;
+import java.lang.reflect.Type;
+import java.util.List;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.calcite.DataContext;
+import org.apache.calcite.adapter.enumerable.EnumUtils;

Review comment:
       all invocations of EnumUtils should be replaced with ConverterUtils

##########
File path: 
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/ConverterUtils.java
##########
@@ -175,6 +175,54 @@ public static Expression convert(Expression operand, Type 
toType) {
         return convert(operand, fromType, toType);
     }
 
+    /**
+     * Convert {@code operand} from {@code targetType} to BigDecimal type.
+     *
+     * @param operand The expression to convert
+     * @param targetType Target type
+     * @param fromType Field type
+     * @return An expression with BidDecimal type, which calls 
IgniteSqlFunctions.toBigDecimal function.
+     */
+    public static Expression convertToDecimal(Expression operand, Type 
fromType, RelDataType targetType) {
+        final Primitive fromBox = Primitive.ofBox(fromType);
+        final Primitive fromPrimitive = Primitive.of(fromType);
+        if (fromBox != null) {
+            // E.g. from "Integer" to "BigDecimal".
+            // Generate "x == null ? null : new BigDecimal(x.intValue())"
+
+            return Expressions.condition(
+                Expressions.equal(operand, RexImpTable.NULL_EXPR),
+                RexImpTable.NULL_EXPR,
+                Expressions.call(
+                    IgniteSqlFunctions.class,
+                    "toBigDecimal",
+                    Expressions.unbox(operand, fromBox),
+                    Expressions.constant(targetType.getPrecision()),
+                    Expressions.constant(targetType.getScale())));
+        }
+        if (fromPrimitive != null) {
+            // E.g. from "int" to "BigDecimal".

Review comment:
       looks like only few primitives are supported: flood and double. How is 
would work with other types like int or short? 
   
   Let's write a simple test that verifies a whole spectrum of conversions to 
decimal types: from byte, short, int, long, float, double. For both Nullable 
and Non Nullable cases.
   
   Negative cases could be covered too, like attempt to convert from bool, for 
example 

##########
File path: 
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/ConverterUtils.java
##########
@@ -175,6 +175,54 @@ public static Expression convert(Expression operand, Type 
toType) {
         return convert(operand, fromType, toType);
     }
 
+    /**
+     * Convert {@code operand} from {@code targetType} to BigDecimal type.
+     *
+     * @param operand The expression to convert
+     * @param targetType Target type
+     * @param fromType Field type
+     * @return An expression with BidDecimal type, which calls 
IgniteSqlFunctions.toBigDecimal function.
+     */
+    public static Expression convertToDecimal(Expression operand, Type 
fromType, RelDataType targetType) {
+        final Primitive fromBox = Primitive.ofBox(fromType);
+        final Primitive fromPrimitive = Primitive.of(fromType);
+        if (fromBox != null) {
+            // E.g. from "Integer" to "BigDecimal".
+            // Generate "x == null ? null : new BigDecimal(x.intValue())"
+
+            return Expressions.condition(
+                Expressions.equal(operand, RexImpTable.NULL_EXPR),
+                RexImpTable.NULL_EXPR,
+                Expressions.call(
+                    IgniteSqlFunctions.class,
+                    "toBigDecimal",
+                    Expressions.unbox(operand, fromBox),

Review comment:
       why do we need to unbox operand? 

##########
File path: 
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/IgniteSqlFunctions.java
##########
@@ -53,6 +59,46 @@ public static ScannableTable systemRange(Object rangeStart, 
Object rangeEnd, Obj
         return new RangeTable(rangeStart, rangeEnd, increment);
     }
 
+    /** CAST(DECIMAL AS VARCHAR). */
+    public static String toString(BigDecimal x) {
+        return x.toPlainString();
+    }
+
+    /** CAST(VARCHAR AS DECIMAL). */
+    public static BigDecimal toBigDecimal(String s, int precision, int scale) {
+        BigDecimal decimal = new BigDecimal(s.trim());
+        return precision == PRECISION_NOT_SPECIFIED ? decimal : 
decimal.setScale(scale, RoundingMode.HALF_UP);
+    }
+
+    /** CAST(FLOAT AS DECIMAL). */
+    public static BigDecimal toBigDecimal(float val, int precision, int scale) 
{
+        BigDecimal decimal = new BigDecimal(String.valueOf(val));
+        return precision == PRECISION_NOT_SPECIFIED ? decimal : 
decimal.setScale(scale, RoundingMode.HALF_UP);
+    }
+
+    /** CAST(DOUBLE AS DECIMAL). */
+    public static BigDecimal toBigDecimal(double val, int precision, int 
scale) {
+        BigDecimal decimal = BigDecimal.valueOf(val);
+        return precision == PRECISION_NOT_SPECIFIED ? decimal : 
decimal.setScale(scale, RoundingMode.HALF_UP);
+    }
+
+    /** CAST(REAL AS DECIMAL). */

Review comment:
       REAL == FLOAT

##########
File path: 
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/ConverterUtils.java
##########
@@ -304,32 +352,6 @@ else if (fromPrimitive != null && toBox != null) {
             if (operand != originTypedOperand)
               return originTypedOperand;
         }
-        if (toType == BigDecimal.class) {

Review comment:
       let's throw an assertion here with link to the method that should be 
used instead

##########
File path: 
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/ConverterUtils.java
##########
@@ -175,6 +175,54 @@ public static Expression convert(Expression operand, Type 
toType) {
         return convert(operand, fromType, toType);
     }
 
+    /**
+     * Convert {@code operand} from {@code targetType} to BigDecimal type.
+     *
+     * @param operand The expression to convert
+     * @param targetType Target type
+     * @param fromType Field type
+     * @return An expression with BidDecimal type, which calls 
IgniteSqlFunctions.toBigDecimal function.
+     */
+    public static Expression convertToDecimal(Expression operand, Type 
fromType, RelDataType targetType) {
+        final Primitive fromBox = Primitive.ofBox(fromType);

Review comment:
       let's put an assertion here to validate that `targetType` _is_ a DECIMAL

##########
File path: 
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/ConverterUtils.java
##########
@@ -175,6 +175,54 @@ public static Expression convert(Expression operand, Type 
toType) {
         return convert(operand, fromType, toType);
     }
 
+    /**
+     * Convert {@code operand} from {@code targetType} to BigDecimal type.

Review comment:
       actually it converts an operand of _fromType_ to the target type




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to