mihaibudiu commented on code in PR #4844:
URL: https://github.com/apache/calcite/pull/4844#discussion_r3158251190


##########
core/src/main/java/org/apache/calcite/sql/fun/SqlColonOperator.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.sql.fun;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.SqlOperandCountRange;
+import org.apache.calcite.sql.SqlSpecialOperator;
+import org.apache.calcite.sql.SqlWriter;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.type.SqlOperandCountRanges;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.util.SqlBasicVisitor;
+import org.apache.calcite.sql.util.SqlVisitor;
+import org.apache.calcite.sql.validate.SqlValidator;
+import org.apache.calcite.sql.validate.SqlValidatorScope;
+import org.apache.calcite.util.Litmus;
+
+import java.util.Arrays;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * The colon operator {@code :}, which preserves field/item path access syntax
+ * in the SQL tree.
+ *
+ * <p>Operands are {@code (base, path)}, where {@code path} is a
+ * {@link SqlNodeList} of structural segments only: identifiers (dot-notation),
+ * character literals (bracketed string keys), or numeric literals (bracketed
+ * indexes).
+ *
+ * <p>Calcite does not lower this operator to Rex. Engines must register a

Review Comment:
   Frankly, the way it's used is not the business of the class.
   The JavaDoc should describe just what this class does, not what other 
classes do with it. If the other classes change, this comment will become 
obsolete.
   This comment should probably be in the convertlet table if at all.



##########
core/src/main/java/org/apache/calcite/sql/fun/SqlColonOperator.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.sql.fun;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.SqlOperandCountRange;
+import org.apache.calcite.sql.SqlSpecialOperator;
+import org.apache.calcite.sql.SqlWriter;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.type.SqlOperandCountRanges;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.util.SqlBasicVisitor;
+import org.apache.calcite.sql.util.SqlVisitor;
+import org.apache.calcite.sql.validate.SqlValidator;
+import org.apache.calcite.sql.validate.SqlValidatorScope;
+import org.apache.calcite.util.Litmus;
+
+import java.util.Arrays;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * The colon operator {@code :}, which preserves field/item path access syntax
+ * in the SQL tree.
+ *
+ * <p>Operands are {@code (base, path)}, where {@code path} is a
+ * {@link SqlNodeList} of structural segments only: identifiers (dot-notation),
+ * character literals (bracketed string keys), or numeric literals (bracketed
+ * indexes).
+ *
+ * <p>Calcite does not lower this operator to Rex. Engines must register a
+ * convertlet for {@link SqlKind#COLON}; without one, sql2rel fails.
+ */
+public class SqlColonOperator extends SqlSpecialOperator {
+  SqlColonOperator() {
+    super("COLON", SqlKind.COLON, 100, true, null, null, null);
+  }
+
+  // Path segments are structural literals/identifiers, never column refs, so
+  // they must not be routed through expression visitors such as AggChecker.
+  @Override public <R> void acceptCall(SqlVisitor<R> visitor, SqlCall call,
+      boolean onlyExpressions, SqlBasicVisitor.ArgHandler<R> argHandler) {
+    if (onlyExpressions) {
+      argHandler.visitChild(visitor, call, 0, call.operand(0));
+    } else {
+      super.acceptCall(visitor, call, onlyExpressions, argHandler);
+    }
+  }
+
+  @Override public ReduceResult reduceExpr(int ordinal, TokenSequence list) {
+    final SqlNode left = list.node(ordinal - 1);
+    final SqlNode right = list.node(ordinal + 1);
+    return new ReduceResult(ordinal - 1, ordinal + 2,
+        createCall(
+            SqlParserPos.sum(
+                Arrays.asList(
+                    requireNonNull(left, "left").getParserPosition(),
+                    requireNonNull(right, "right").getParserPosition(),
+                    list.pos(ordinal))),
+            left,
+            right));
+  }
+
+  @Override public void unparse(SqlWriter writer, SqlCall call, int leftPrec,
+      int rightPrec) {
+    final SqlWriter.Frame frame =
+        writer.startList(SqlWriter.FrameTypeEnum.IDENTIFIER);
+    call.operand(0).unparse(writer, leftPrec, 0);
+    writer.setNeedWhitespace(false);
+    writer.keyword(":");
+    writer.setNeedWhitespace(false);
+    boolean first = true;
+    for (SqlNode segment : (SqlNodeList) call.operand(1)) {
+      if (segment instanceof SqlIdentifier) {
+        if (!first) {
+          writer.sep(".");
+        }
+        segment.unparse(writer, 0, 0);
+      } else {
+        final SqlWriter.Frame b = writer.startList("[", "]");
+        segment.unparse(writer, 0, 0);
+        writer.endList(b);
+      }
+      first = false;
+    }
+    writer.endList(frame);
+  }
+
+  @Override public SqlOperandCountRange getOperandCountRange() {
+    return SqlOperandCountRanges.of(2);
+  }
+
+  // Returns nullable ANY: path access can yield NULL on a missing key, and
+  // the concrete type is up to the engine's convertlet.

Review Comment:
   You again assume something about the convertlet table.
   This type seems overly general, can't you compute the type of a colon 
expression from the types of its operands?



##########
core/src/main/java/org/apache/calcite/sql/validate/SqlConformance.java:
##########
@@ -260,6 +260,21 @@ enum SelectAliasLookup {
    */
   boolean allowHyphenInUnquotedTableName();
 
+  /**
+   * Whether {@code :} is allowed as a field/item access operator.
+   *
+   * <p>If true, expressions such as {@code v:field} and {@code v:['field']}
+   * are valid. In this mode, JSON constructors must use the
+   * {@code KEY ... VALUE} form rather than {@code :} or comma-pair syntax.
+   *
+   * <p>No built-in conformance level enables this; engines that want

Review Comment:
   Again you describe in JavaDoc what other classes do.
   I think you should only refer to what is happening here.



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