>From Vijay Sarathy <[email protected]>:

Vijay Sarathy has uploaded this change for review. ( 
https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/17404 )


Change subject: [ASTERIXDB-3120] CBO Analyze: improve error message if seed 
value is invalid [ASTERIXDB-3121] CBO Analyze: Improve error message if sample 
value is invalid
......................................................................

[ASTERIXDB-3120] CBO Analyze: improve error message if seed value is invalid
[ASTERIXDB-3121] CBO Analyze: Improve error message if sample value is invalid

Change-Id: I599005f4f4814f719e422da4ff7d58c2253e5ed4
---
M 
asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/statement/AnalyzeStatement.java
M 
asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java
M asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties
3 files changed, 75 insertions(+), 11 deletions(-)



  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb 
refs/changes/04/17404/1

diff --git 
a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java
 
b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java
index 44471a2..189f550 100644
--- 
a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java
+++ 
b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java
@@ -267,6 +267,9 @@
     INVALID_TIMEZONE(1172),
     INVALID_PARAM_VALUE_ALLOWED_VALUE(1173),
     SAMPLE_HAS_ZERO_ROWS(1174),
+    INVALID_SAMPLE_SIZE_STRING_OPTIONS(1175),
+    INVALID_SAMPLE_SIZE_NUMBER_OPTIONS(1176),
+    INVALID_SAMPLE_SEED_STRING_OPTIONS(1177),

     // Feed errors
     DATAFLOW_ILLEGAL_STATE(3001),
diff --git 
a/asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties 
b/asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties
index c9ab080..03889ce 100644
--- a/asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties
+++ b/asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties
@@ -269,7 +269,9 @@
 1172 = Provided timezone is invalid: '%1$s'
 1173 = Invalid value for parameter '%1$s', allowed value(s): %2$s
 1174 = Sample has zero rows
-
+1175 = Sample size options are "low", "medium", "high", a positive numeric 
value, or a string convertible to a positive numeric value
+1176 = Sample size has to be between %1$s and %2$s
+1177 = Sample seed has to be a numeric value or a string convertible to a 
numeric value
 # Feed Errors
 3001 = Illegal state.
 3002 = Tuple is too large for a frame
diff --git 
a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/statement/AnalyzeStatement.java
 
b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/statement/AnalyzeStatement.java
index cbf2c07..2cdd0ee 100644
--- 
a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/statement/AnalyzeStatement.java
+++ 
b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/statement/AnalyzeStatement.java
@@ -19,14 +19,22 @@

 package org.apache.asterix.lang.common.statement;

+import java.util.List;
 import java.util.Locale;

 import org.apache.asterix.common.exceptions.CompilationException;
 import org.apache.asterix.common.exceptions.ErrorCode;
 import org.apache.asterix.common.metadata.DataverseName;
 import org.apache.asterix.lang.common.base.AbstractStatement;
+import org.apache.asterix.lang.common.base.Expression;
+import org.apache.asterix.lang.common.base.Literal;
+import org.apache.asterix.lang.common.expression.FieldBinding;
+import org.apache.asterix.lang.common.expression.LiteralExpr;
 import org.apache.asterix.lang.common.expression.RecordConstructor;
+import org.apache.asterix.lang.common.expression.UnaryExpr;
+import org.apache.asterix.lang.common.struct.UnaryExprType;
 import org.apache.asterix.lang.common.util.ExpressionUtils;
+import org.apache.asterix.lang.common.util.LangRecordParseUtil;
 import org.apache.asterix.lang.common.visitor.base.ILangVisitor;
 import org.apache.asterix.object.base.AdmBigIntNode;
 import org.apache.asterix.object.base.AdmDoubleNode;
@@ -57,20 +65,49 @@
             throws CompilationException {
         this.dataverseName = dataverseName;
         this.datasetName = datasetName;
-        this.options = options == null ? null : 
validateOptions(ExpressionUtils.toNode(options));
+        this.options = options == null ? null : validateOptions(options);
     }

-    private static AdmObjectNode validateOptions(AdmObjectNode options) throws 
CompilationException {
-        for (String fieldName : options.getFieldNames()) {
-            switch (fieldName) {
+    private static AdmObjectNode validateOptions(RecordConstructor options) 
throws CompilationException {
+        final List<FieldBinding> fbList = options.getFbList();
+        for (int i = 0; i < fbList.size(); i++) {
+            FieldBinding binding = fbList.get(i);
+            String key = 
LangRecordParseUtil.exprToStringLiteral(binding.getLeftExpr()).getStringValue();
+            Expression value = binding.getRightExpr();
+            if (value.getKind() == Expression.Kind.UNARY_EXPRESSION) {
+                UnaryExpr unaryExpr = (UnaryExpr) value;
+                UnaryExprType unaryExprType = unaryExpr.getExprType();
+                if (unaryExprType == UnaryExprType.POSITIVE || unaryExprType 
== UnaryExprType.NEGATIVE) {
+                    Expression uexpr = unaryExpr.getExpr();
+                    if (uexpr.getKind() == Expression.Kind.LITERAL_EXPRESSION) 
{
+                        if (unaryExprType == UnaryExprType.POSITIVE) {
+                            binding.setRightExpr(uexpr);
+                        } else if (key.equals(SAMPLE_SEED_FIELD_NAME)) {
+                            // Seed value can be negative
+                            Literal lit = ((LiteralExpr) uexpr).getValue();
+                            binding.setRightExpr(new 
LiteralExpr(ExpressionUtils.reverseSign(lit)));
+                        }
+                        // The rightExpr may have changed.
+                        value = binding.getRightExpr();
+                    }
+                }
+            }
+            switch (key) {
                 case SAMPLE_FIELD_NAME:
+                    if (value.getKind() != Expression.Kind.LITERAL_EXPRESSION) 
{
+                        throw new 
CompilationException(ErrorCode.INVALID_SAMPLE_SIZE_STRING_OPTIONS);
+                    }
+                    break;
                 case SAMPLE_SEED_FIELD_NAME:
+                    if (value.getKind() != Expression.Kind.LITERAL_EXPRESSION) 
{
+                        throw new 
CompilationException(ErrorCode.INVALID_SAMPLE_SEED_STRING_OPTIONS);
+                    }
                     break;
                 default:
-                    throw new CompilationException(ErrorCode.INVALID_PARAM, 
fieldName);
+                    throw new CompilationException(ErrorCode.INVALID_PARAM, 
key);
             }
         }
-        return options;
+        return (ExpressionUtils.toNode(options));
     }

     @Override
@@ -102,18 +139,30 @@
                     case SAMPLE_HIGH:
                         return SAMPLE_HIGH_SIZE;
                     default:
-                        throw new 
CompilationException(ErrorCode.INVALID_PROPERTY_FORMAT, SAMPLE_FIELD_NAME);
+                        int v;
+                        try {
+                            v = Integer.parseInt(s);
+                        } catch (NumberFormatException e) {
+                            throw new 
CompilationException(ErrorCode.INVALID_SAMPLE_SIZE_STRING_OPTIONS);
+                        }
+                        if (!isValidSampleSize(v)) {
+                            throw new 
CompilationException(ErrorCode.INVALID_SAMPLE_SIZE_NUMBER_OPTIONS,
+                                    SAMPLE_LOW_SIZE, SAMPLE_HIGH_SIZE);
+                        }
+                        return v;
                 }
             case BIGINT:
                 int v = (int) ((AdmBigIntNode) n).get();
                 if (!isValidSampleSize(v)) {
-                    throw new 
CompilationException(ErrorCode.INVALID_PROPERTY_FORMAT, SAMPLE_FIELD_NAME);
+                    throw new 
CompilationException(ErrorCode.INVALID_SAMPLE_SIZE_NUMBER_OPTIONS, 
SAMPLE_LOW_SIZE,
+                            SAMPLE_HIGH_SIZE);
                 }
                 return v;
             case DOUBLE:
                 v = (int) ((AdmDoubleNode) n).get();
                 if (!isValidSampleSize(v)) {
-                    throw new 
CompilationException(ErrorCode.INVALID_PROPERTY_FORMAT, SAMPLE_FIELD_NAME);
+                    throw new 
CompilationException(ErrorCode.INVALID_SAMPLE_SIZE_NUMBER_OPTIONS, 
SAMPLE_LOW_SIZE,
+                            SAMPLE_HIGH_SIZE);
                 }
                 return v;
             default:
@@ -138,7 +187,7 @@
                 try {
                     return Long.parseLong(s);
                 } catch (NumberFormatException e) {
-                    throw new 
CompilationException(ErrorCode.INVALID_PROPERTY_FORMAT, SAMPLE_SEED_FIELD_NAME);
+                    throw new 
CompilationException(ErrorCode.INVALID_SAMPLE_SEED_STRING_OPTIONS);
                 }
             default:
                 throw new 
CompilationException(ErrorCode.WITH_FIELD_MUST_BE_OF_TYPE, 
SAMPLE_SEED_FIELD_NAME,

--
To view, visit https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/17404
To unsubscribe, or for help writing mail filters, visit 
https://asterix-gerrit.ics.uci.edu/settings

Gerrit-Project: asterixdb
Gerrit-Branch: neo
Gerrit-Change-Id: I599005f4f4814f719e422da4ff7d58c2253e5ed4
Gerrit-Change-Number: 17404
Gerrit-PatchSet: 1
Gerrit-Owner: Vijay Sarathy <[email protected]>
Gerrit-MessageType: newchange

Reply via email to