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


##########
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:
   Make sense, I think I will name it `STRUCT_CONSTRUCTOR`
   based on:
   ```
   * <p>Only commonly-used nodes have their own type; other nodes are of type
    * {@link #OTHER}. Some of the values, such as {@link #SET_QUERY}, represent
    * aggregates.</p>
   ```
   
   In my PR for CALCITE-5624, the `array` Apache Spark function should have 
been of kind `ARRAY_VALUE_CONSTRUCTOR` I think.
   
   So if we implement BigQuery's struct constructor we could share the Kind 
`STRUCT_CONSTRUCTOR`:
   
https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#constructing_a_struct



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