liuyongvs commented on code in PR #3231:
URL: https://github.com/apache/calcite/pull/3231#discussion_r1210942313


##########
core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java:
##########
@@ -827,6 +828,47 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding 
operatorBinding,
           .withOperandTypeInference(InferTypes.RETURN_TYPE)
           .withKind(SqlKind.CONCAT2);
 
+  private static RelDataType namedStructReturnType(SqlOperatorBinding 
operatorBinding) {
+    if (operatorBinding.getOperandCount() < 2) {
+      throw 
operatorBinding.newError(Static.RESOURCE.namedStructRequiresTwoOrMoreArgs());
+    }
+    if (operatorBinding.getOperandCount() % 2 != 0) {
+      throw 
operatorBinding.newError(Static.RESOURCE.namedStructRequiresEvenNumberOfArgs());
+    }
+    List<String> keys = new ArrayList<String>();
+    List<RelDataType> values = new ArrayList<RelDataType>();
+    int i = 0;
+    for (RelDataType type : operatorBinding.collectOperandTypes()) {
+      if (i % 2 == 0) {
+        Object key = operatorBinding.getOperandLiteralValue(i, Object.class);
+        if (key == null) {
+          throw operatorBinding.newError(
+            
Static.RESOURCE.namedStructRequiresLiteralStringKeysGotExpression());
+        }
+        if (key instanceof NlsString) {
+          keys.add(((NlsString) key).getValue());
+        } else {
+          String tpe = key.getClass().getSimpleName();
+          throw operatorBinding.newError(
+            
Static.RESOURCE.namedStructRequiresLiteralStringKeysGotOtherType(tpe));
+        }
+
+      } else {
+        values.add(type);
+      }
+      i++;
+    }
+    final RelDataTypeFactory typeFactory = operatorBinding.getTypeFactory();
+    return typeFactory.createStructType(values, keys);
+  }
+
+  /** Creates a struct with the given field names and values. */
+  @LibraryOperator(libraries = {SPARK})
+  public static final SqlFunction NAMED_STRUCT =
+      SqlBasicFunction.create("NAMED_STRUCT",
+          SqlLibraryOperators::namedStructReturnType,
+          OperandTypes.SAME_VARIADIC);

Review Comment:
   put "NAMED_STRUCT" in SqlKind



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