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


##########
core/src/main/java/org/apache/calcite/sql/fun/SqlColonOperator.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * 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.SqlUtil;
+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 org.apache.calcite.util.Static.RESOURCE;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * The colon operator {@code :}, used for variant path access.
+ *
+ * <p>Operands are {@code (base, path)}, where {@code path} is a
+ * {@link SqlNodeList} of segments.
+ */
+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);
+  }
+
+  @Override public RelDataType deriveType(SqlValidator validator,
+      SqlValidatorScope scope, SqlCall call) {
+    final RelDataType baseType =
+        requireNonNull(validator.deriveType(scope, call.operand(0)));
+    final SqlTypeName name = baseType.getSqlTypeName();
+    if (name != SqlTypeName.VARIANT && name != SqlTypeName.ANY) {

Review Comment:
   Good point, created `OperandTypes.COLON`.
   
   On STRING handling, VARIANT is intentional for this PR. The API is kept 
simple enough that STRING can be added later by extending the existing operand 
checker and convertlet. Happy to do this in a follow-up Jira.
   



##########
core/src/main/java/org/apache/calcite/sql/fun/SqlColonOperator.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * 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.SqlUtil;
+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 org.apache.calcite.util.Static.RESOURCE;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * The colon operator {@code :}, used for variant path access.
+ *
+ * <p>Operands are {@code (base, path)}, where {@code path} is a
+ * {@link SqlNodeList} of segments.
+ */
+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);
+  }
+
+  @Override public RelDataType deriveType(SqlValidator validator,
+      SqlValidatorScope scope, SqlCall call) {
+    final RelDataType baseType =
+        requireNonNull(validator.deriveType(scope, call.operand(0)));
+    final SqlTypeName name = baseType.getSqlTypeName();
+    if (name != SqlTypeName.VARIANT && name != SqlTypeName.ANY) {
+      throw SqlUtil.newContextException(call.operand(0).getParserPosition(),
+          RESOURCE.incompatibleTypes());
+    }
+    final RelDataType type =
+        validator.getTypeFactory().createTypeWithNullability(
+            validator.getTypeFactory().createSqlType(SqlTypeName.VARIANT), 
true);
+    validator.setValidatedNodeType(call, type);
+    return type;
+  }
+
+  @Override public void validateCall(SqlCall call, SqlValidator validator,
+      SqlValidatorScope scope, SqlValidatorScope operandScope) {
+    assert call.getOperator() == this;
+    call.operand(0).validateExpr(validator, operandScope);
+  }
+
+  @Override public boolean validRexOperands(final int count, final Litmus 
litmus) {
+    return litmus.fail("COLON is valid only for SqlCall not for RexCall");
+  }
+
+  @Override public String getAllowedSignatures(String name) {
+    return "<A>:<PATH>";

Review Comment:
   Whops, fixed it, also moved this part to `OperandTypes`.



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