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



##########
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:
       Fixed

##########
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:
       Moved to the exec.exp package

##########
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:
       Added AssertionException with related error message

##########
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:
       Fixed javadocs

##########
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:
       Added an assertion

##########
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:
       Renamed to FLOAT

##########
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:
       Realized that we actually dont need it. Refactored this method. 

##########
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:
       Added IgniteSqlFunctionsTest that covers all the cases.

##########
File path: 
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/RexExecutorImpl.java
##########
@@ -0,0 +1,187 @@
+/*
+ * 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.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.java.JavaTypeFactory;
+import org.apache.calcite.config.CalciteSystemProperty;
+import org.apache.calcite.jdbc.JavaTypeFactoryImpl;
+import org.apache.calcite.linq4j.tree.BlockBuilder;
+import org.apache.calcite.linq4j.tree.Expression;
+import org.apache.calcite.linq4j.tree.Expressions;
+import org.apache.calcite.linq4j.tree.IndexExpression;
+import org.apache.calcite.linq4j.tree.MethodCallExpression;
+import org.apache.calcite.linq4j.tree.MethodDeclaration;
+import org.apache.calcite.linq4j.tree.ParameterExpression;
+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.RexExecutable;
+import org.apache.calcite.rex.RexExecutor;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexProgram;
+import org.apache.calcite.rex.RexProgramBuilder;
+import org.apache.calcite.sql.validate.SqlConformance;
+import org.apache.calcite.sql.validate.SqlConformanceEnum;
+import org.apache.calcite.util.BuiltInMethod;
+import org.apache.calcite.util.Util;
+
+/**
+ * Evaluates a {@link RexNode} expression.
+ *
+ * <p>For this impl, all the public methods should be
+ * static except that it inherits from {@link RexExecutor}.
+ * This pretends that other code in the project assumes
+ * the executor instance is {@link RexExecutorImpl}.
+*/
+public class RexExecutorImpl implements RexExecutor {
+

Review comment:
       fixed




-- 
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