tkalkirill commented on code in PR #13311:
URL: https://github.com/apache/ignite/pull/13311#discussion_r3665558911


##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/IgniteSqlValidator.java:
##########
@@ -241,54 +241,81 @@ private void validateTableModify(SqlNode table) {
 
     /** {@inheritDoc} */
     @Override protected void validateSelect(SqlSelect select, RelDataType 
targetRowType) {
-        checkIntegerLimit(select.getFetch(), "fetch / limit");
-        checkIntegerLimit(select.getOffset(), "offset");
-
         super.validateSelect(select, targetRowType);
-    }
 
-    /** {@inheritDoc} */
-    @Override protected void validateNamespace(SqlValidatorNamespace 
namespace, RelDataType targetRowType) {
-        SqlValidatorTable table = namespace.getTable();
-
-        if (table != null) {
-            IgniteCacheTable igniteTable = 
table.unwrap(IgniteCacheTable.class);
-
-            if (igniteTable != null)
-                igniteTable.ensureCacheStarted();
-        }
-
-        super.validateNamespace(namespace, targetRowType);
+        invalidateFetchOffset(select.getFetch(), "fetch / limit");
+        invalidateFetchOffset(select.getOffset(), "offset");
     }
 
     /**
-     * @param n Node to check limit.
+     * Invalidate fetch/offset params restrictions.
+     *
+     * @param n        Node to check limit.
      * @param nodeName Node name.
      */
-    private void checkIntegerLimit(SqlNode n, String nodeName) {
+    private void invalidateFetchOffset(@Nullable SqlNode n, String nodeName) {
+        if (n == null) {
+            return;
+        }
+
         if (n instanceof SqlLiteral) {
-            BigDecimal offFetchLimit = ((SqlLiteral)n).bigDecimalValue();
+            BigDecimal offsetFetchLimit = ((SqlLiteral)n).bigDecimalValue();
 
-            if (offFetchLimit.compareTo(DEC_INT_MAX) > 0 || 
offFetchLimit.compareTo(BigDecimal.ZERO) < 0)
-                throw newValidationError(n, 
IgniteResource.INSTANCE.correctIntegerLimit(nodeName));
+            checkLimitOffset(offsetFetchLimit, n, nodeName);
         }
-        else if (n instanceof SqlDynamicParam) {
-            // will fail in params check.
+        else if (n instanceof SqlDynamicParam dynamicParam) {
             if (F.isEmpty(parameters))
                 return;
 
-            int idx = ((SqlDynamicParam)n).getIndex();
+            if (dynamicParam.getIndex() < parameters.length) {
+                Object param = parameters[dynamicParam.getIndex()];
+
+                if (!(param instanceof Number)) {
+                    SqlTypeName expectType = SqlTypeName.BIGINT;
+                    Resources.ExInst<SqlValidatorException> err;
+
+                    if (param == null)
+                        err = 
IgniteResource.INSTANCE.incorrectDynamicParameterType(expectType.toString(), 
"null");
+                    else {
+                        SqlTypeName paramType = 
typeFactory().createType(param.getClass()).getSqlTypeName();
+                        err = 
IgniteResource.INSTANCE.incorrectDynamicParameterType(expectType.toString(), 
paramType.getName());
+                    }
 
-            if (idx < parameters.length) {
-                Object param = parameters[idx];
-                if (parameters[idx] instanceof Integer) {
-                    if ((Integer)param < 0)
-                        throw newValidationError(n, 
IgniteResource.INSTANCE.correctIntegerLimit(nodeName));
+                    throw newValidationError(n, err);
                 }
+                else
+                    checkLimitOffset((Number)param, n, nodeName);
             }
         }
     }
 
+    /** */
+    private void checkLimitOffset(Number offsetFetchLimit, @Nullable SqlNode 
n, String nodeName) {
+        try {
+            long res = IgniteMath.convertToLongExact(offsetFetchLimit);
+
+            if (res < 0)

Review Comment:
   If we support the `SqlTypeName.DECIMAL` as in Calcite, then it will need to 
be fixed.



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/IgniteSqlValidator.java:
##########
@@ -84,9 +87,6 @@
 /** Validator. */
 @Value.Enclosing
 public class IgniteSqlValidator extends SqlValidatorImpl {

Review Comment:
   Maybe we should use `SqlTypeName.DECIMAL`, similar to the CALCITE-7624?
   org.apache.calcite.sql.validate.SqlValidatorImpl#handleOffsetFetch
   
   A question regarding rounding might arise; we can use the Calcite 
`org.apache.calcite.adapter.enumerable.FetchOffsetRoundingPolicy` interface, 
and it will handle rounding when creating nodes.
   



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/IgniteSqlValidator.java:
##########
@@ -241,54 +241,81 @@ private void validateTableModify(SqlNode table) {
 
     /** {@inheritDoc} */
     @Override protected void validateSelect(SqlSelect select, RelDataType 
targetRowType) {
-        checkIntegerLimit(select.getFetch(), "fetch / limit");
-        checkIntegerLimit(select.getOffset(), "offset");
-
         super.validateSelect(select, targetRowType);
-    }
 
-    /** {@inheritDoc} */
-    @Override protected void validateNamespace(SqlValidatorNamespace 
namespace, RelDataType targetRowType) {
-        SqlValidatorTable table = namespace.getTable();
-
-        if (table != null) {
-            IgniteCacheTable igniteTable = 
table.unwrap(IgniteCacheTable.class);
-
-            if (igniteTable != null)
-                igniteTable.ensureCacheStarted();
-        }
-
-        super.validateNamespace(namespace, targetRowType);
+        invalidateFetchOffset(select.getFetch(), "fetch / limit");
+        invalidateFetchOffset(select.getOffset(), "offset");
     }
 
     /**
-     * @param n Node to check limit.
+     * Invalidate fetch/offset params restrictions.
+     *
+     * @param n        Node to check limit.
      * @param nodeName Node name.
      */
-    private void checkIntegerLimit(SqlNode n, String nodeName) {
+    private void invalidateFetchOffset(@Nullable SqlNode n, String nodeName) {
+        if (n == null) {
+            return;
+        }
+
         if (n instanceof SqlLiteral) {
-            BigDecimal offFetchLimit = ((SqlLiteral)n).bigDecimalValue();
+            BigDecimal offsetFetchLimit = ((SqlLiteral)n).bigDecimalValue();
 
-            if (offFetchLimit.compareTo(DEC_INT_MAX) > 0 || 
offFetchLimit.compareTo(BigDecimal.ZERO) < 0)
-                throw newValidationError(n, 
IgniteResource.INSTANCE.correctIntegerLimit(nodeName));
+            checkLimitOffset(offsetFetchLimit, n, nodeName);
         }
-        else if (n instanceof SqlDynamicParam) {
-            // will fail in params check.
+        else if (n instanceof SqlDynamicParam dynamicParam) {
             if (F.isEmpty(parameters))
                 return;
 
-            int idx = ((SqlDynamicParam)n).getIndex();
+            if (dynamicParam.getIndex() < parameters.length) {
+                Object param = parameters[dynamicParam.getIndex()];
+
+                if (!(param instanceof Number)) {
+                    SqlTypeName expectType = SqlTypeName.BIGINT;

Review Comment:
   If we support the `SqlTypeName.DECIMAL` as in Calcite, then it will need to 
be 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