Jackie-Jiang commented on a change in pull request #6246:
URL: https://github.com/apache/incubator-pinot/pull/6246#discussion_r577911293



##########
File path: 
pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java
##########
@@ -902,11 +902,47 @@ private static Expression 
compileFunctionExpression(SqlBasicCall functionNode) {
         operands.add(toExpression(childNode));
       }
     }
+    validateFunction(functionName, operands);
     Expression functionExpression = 
RequestUtils.getFunctionExpression(functionName);
     functionExpression.getFunctionCall().setOperands(operands);
     return functionExpression;
   }
 
+  private static void validateFunction(String functionName, List<Expression> 
operands) {
+    switch (functionName.toUpperCase()) {
+      case "JSONEXTRACTSCALAR":
+        validateJsonExtractScalarFunction(functionName, operands);
+        break;
+      case "JSONEXTRACTKEY":
+        validateJsonExtractKeyFunction(functionName, operands);
+        break;
+    }
+  }
+
+  private static void validateJsonExtractScalarFunction(String functionName, 
List<Expression> operands) {
+    // Check that there are exactly 3 or 4 arguments
+    if (operands.size() < 3 || operands.size() > 4) {
+      throw new SqlCompilationException(
+          "Expected 3/4 arguments for transform function: 
jsonExtractScalar(jsonFieldName, 'jsonPath', 'resultsType', ['defaultValue'])");
+    }
+    if (!operands.get(1).isSetLiteral() || !operands.get(2).isSetLiteral()) {

Review comment:
       Also validate the optional fourth argument?

##########
File path: 
pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java
##########
@@ -902,11 +902,47 @@ private static Expression 
compileFunctionExpression(SqlBasicCall functionNode) {
         operands.add(toExpression(childNode));
       }
     }
+    validateFunction(functionName, operands);
     Expression functionExpression = 
RequestUtils.getFunctionExpression(functionName);
     functionExpression.getFunctionCall().setOperands(operands);
     return functionExpression;
   }
 
+  private static void validateFunction(String functionName, List<Expression> 
operands) {
+    switch (functionName.toUpperCase()) {
+      case "JSONEXTRACTSCALAR":
+        validateJsonExtractScalarFunction(functionName, operands);
+        break;
+      case "JSONEXTRACTKEY":
+        validateJsonExtractKeyFunction(functionName, operands);
+        break;
+    }
+  }
+
+  private static void validateJsonExtractScalarFunction(String functionName, 
List<Expression> operands) {
+    // Check that there are exactly 3 or 4 arguments
+    if (operands.size() < 3 || operands.size() > 4) {

Review comment:
       (nit) For clarity
   ```suggestion
       if (operands.size() != 3 && operands.size() != 4) {
   ```

##########
File path: 
pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java
##########
@@ -902,11 +902,47 @@ private static Expression 
compileFunctionExpression(SqlBasicCall functionNode) {
         operands.add(toExpression(childNode));
       }
     }
+    validateFunction(functionName, operands);
     Expression functionExpression = 
RequestUtils.getFunctionExpression(functionName);
     functionExpression.getFunctionCall().setOperands(operands);
     return functionExpression;
   }
 
+  private static void validateFunction(String functionName, List<Expression> 
operands) {
+    switch (functionName.toUpperCase()) {
+      case "JSONEXTRACTSCALAR":
+        validateJsonExtractScalarFunction(functionName, operands);
+        break;
+      case "JSONEXTRACTKEY":
+        validateJsonExtractKeyFunction(functionName, operands);
+        break;
+    }
+  }
+
+  private static void validateJsonExtractScalarFunction(String functionName, 
List<Expression> operands) {
+    // Check that there are exactly 3 or 4 arguments
+    if (operands.size() < 3 || operands.size() > 4) {
+      throw new SqlCompilationException(
+          "Expected 3/4 arguments for transform function: 
jsonExtractScalar(jsonFieldName, 'jsonPath', 'resultsType', ['defaultValue'])");
+    }
+    if (!operands.get(1).isSetLiteral() || !operands.get(2).isSetLiteral()) {
+      throw new SqlCompilationException(
+          "Expected the second or third argument for transform function: 
jsonExtractScalar(jsonFieldName, 'jsonPath', 'resultsType', ['defaultValue']) 
to be a single-quoted literal value.");
+    }
+  }
+
+  private static void validateJsonExtractKeyFunction(String functionName, 
List<Expression> operands) {
+    // Check that there are exactly 3 or 4 arguments

Review comment:
       Update the comment

##########
File path: 
pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java
##########
@@ -902,11 +902,47 @@ private static Expression 
compileFunctionExpression(SqlBasicCall functionNode) {
         operands.add(toExpression(childNode));
       }
     }
+    validateFunction(functionName, operands);
     Expression functionExpression = 
RequestUtils.getFunctionExpression(functionName);
     functionExpression.getFunctionCall().setOperands(operands);
     return functionExpression;
   }
 
+  private static void validateFunction(String functionName, List<Expression> 
operands) {
+    switch (functionName.toUpperCase()) {
+      case "JSONEXTRACTSCALAR":
+        validateJsonExtractScalarFunction(functionName, operands);
+        break;
+      case "JSONEXTRACTKEY":
+        validateJsonExtractKeyFunction(functionName, operands);
+        break;
+    }
+  }
+
+  private static void validateJsonExtractScalarFunction(String functionName, 
List<Expression> operands) {
+    // Check that there are exactly 3 or 4 arguments
+    if (operands.size() < 3 || operands.size() > 4) {
+      throw new SqlCompilationException(
+          "Expected 3/4 arguments for transform function: 
jsonExtractScalar(jsonFieldName, 'jsonPath', 'resultsType', ['defaultValue'])");

Review comment:
       `"Expect ..."`, same for other exception message

##########
File path: 
pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java
##########
@@ -902,11 +902,47 @@ private static Expression 
compileFunctionExpression(SqlBasicCall functionNode) {
         operands.add(toExpression(childNode));
       }
     }
+    validateFunction(functionName, operands);
     Expression functionExpression = 
RequestUtils.getFunctionExpression(functionName);
     functionExpression.getFunctionCall().setOperands(operands);
     return functionExpression;
   }
 
+  private static void validateFunction(String functionName, List<Expression> 
operands) {
+    switch (functionName.toUpperCase()) {
+      case "JSONEXTRACTSCALAR":
+        validateJsonExtractScalarFunction(functionName, operands);
+        break;
+      case "JSONEXTRACTKEY":
+        validateJsonExtractKeyFunction(functionName, operands);
+        break;
+    }
+  }
+
+  private static void validateJsonExtractScalarFunction(String functionName, 
List<Expression> operands) {
+    // Check that there are exactly 3 or 4 arguments
+    if (operands.size() < 3 || operands.size() > 4) {
+      throw new SqlCompilationException(
+          "Expected 3/4 arguments for transform function: 
jsonExtractScalar(jsonFieldName, 'jsonPath', 'resultsType', ['defaultValue'])");
+    }
+    if (!operands.get(1).isSetLiteral() || !operands.get(2).isSetLiteral()) {
+      throw new SqlCompilationException(
+          "Expected the second or third argument for transform function: 
jsonExtractScalar(jsonFieldName, 'jsonPath', 'resultsType', ['defaultValue']) 
to be a single-quoted literal value.");
+    }
+  }
+
+  private static void validateJsonExtractKeyFunction(String functionName, 
List<Expression> operands) {
+    // Check that there are exactly 3 or 4 arguments
+    if (operands.size() != 2) {
+      throw new SqlCompilationException(
+          "Exactly 2 arguments are required for transform function: 
jsonExtractKey(jsonFieldName, 'jsonPath')");
+    }
+    if (!operands.get(1).isSetLiteral() || !operands.get(2).isSetLiteral()) {

Review comment:
       (Critical)
   ```suggestion
       if (!operands.get(1).isSetLiteral()) {
   ```

##########
File path: 
pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java
##########
@@ -902,11 +902,47 @@ private static Expression 
compileFunctionExpression(SqlBasicCall functionNode) {
         operands.add(toExpression(childNode));
       }
     }
+    validateFunction(functionName, operands);
     Expression functionExpression = 
RequestUtils.getFunctionExpression(functionName);
     functionExpression.getFunctionCall().setOperands(operands);
     return functionExpression;
   }
 
+  private static void validateFunction(String functionName, List<Expression> 
operands) {
+    switch (functionName.toUpperCase()) {

Review comment:
       Remove underscore from the function name to match `json_extract_scalar` 
and `json_extract_key`. Please also add some test cases for function name with 
underscore




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

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



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to