wnob commented on code in PR #3023:
URL: https://github.com/apache/calcite/pull/3023#discussion_r1074030646


##########
core/src/main/java/org/apache/calcite/sql/SqlUnknownLiteral.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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;
+
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.parser.SqlParserUtil;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.util.NlsString;
+import org.apache.calcite.util.Util;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Literal whose type is not yet known.
+ */
+public class SqlUnknownLiteral extends SqlLiteral {
+  public final String tag;
+
+  SqlUnknownLiteral(String tag, String value, SqlParserPos pos) {
+    super(requireNonNull(value, "value"), SqlTypeName.UNKNOWN, pos);
+    this.tag = requireNonNull(tag, "tag");
+  }
+
+  @Override public String getValue() {
+    return (String) requireNonNull(super.getValue(), "value");
+  }
+
+  @Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) 
{
+    final NlsString nlsString = new NlsString(getValue(), null, null);
+    writer.keyword(tag);
+    writer.literal(nlsString.asSql(true, true, writer.getDialect()));
+  }
+
+
+  /** Converts this unknown literal to a literal of known type. */
+  public SqlLiteral resolve(SqlTypeName typeName) {
+    switch (typeName) {
+    case DATE:
+      return SqlParserUtil.parseDateLiteral(getValue(), pos);
+    case TIME:
+      return SqlParserUtil.parseTimeLiteral(getValue(), pos);
+    case TIMESTAMP:
+      return SqlParserUtil.parseTimestampLiteral(getValue(), pos);
+    case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+      return SqlParserUtil.parseTimestampWithLocalTimeZoneLiteral(getValue(), 
pos);
+    default:

Review Comment:
   See in `SqlValidatorImpl.resolveLiteral` (which calls this function):
   ```
         final SqlIdentifier identifier =
             new SqlIdentifier(unknownLiteral.tag, SqlParserPos.ZERO);
         final @Nullable RelDataType type = 
catalogReader.getNamedType(identifier);
         final SqlTypeName typeName;
         if (type != null) {
           typeName = type.getSqlTypeName();
         } else {
           typeName = SqlTypeName.lookup(unknownLiteral.tag);
         }
         return unknownLiteral.resolve(typeName);
   ```
   So, it makes sense to call `SqlLiteral.createUnknown("DATETIME")`, passing 
"DATETIME" as the `tag`. The tag just says "this is what the type called 
itself", and in order to resolve it, we look up in `catalogReader` to see if 
it's mapped to anything. In the case of BQ, it will map to `TIMESTAMP`. In the 
case of everything else, it would be null, so validation would fail when it 
invokes `SqlTypeName.lookup(unknownLiteral.tag)`.



-- 
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: commits-unsubscr...@calcite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to