mkou commented on code in PR #3034:
URL: https://github.com/apache/calcite/pull/3034#discussion_r1080572882


##########
core/src/main/java/org/apache/calcite/sql/FormatModel.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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 static org.apache.calcite.util.format.FormatElementEnum.D;
+import static org.apache.calcite.util.format.FormatElementEnum.DAY;
+import static org.apache.calcite.util.format.FormatElementEnum.DD;
+import static org.apache.calcite.util.format.FormatElementEnum.DDD;
+import static org.apache.calcite.util.format.FormatElementEnum.DY;
+import static org.apache.calcite.util.format.FormatElementEnum.HH24;
+import static org.apache.calcite.util.format.FormatElementEnum.IW;
+import static org.apache.calcite.util.format.FormatElementEnum.MI;
+import static org.apache.calcite.util.format.FormatElementEnum.MM;
+import static org.apache.calcite.util.format.FormatElementEnum.MON;
+import static org.apache.calcite.util.format.FormatElementEnum.MONTH;
+import static org.apache.calcite.util.format.FormatElementEnum.Q;
+import static org.apache.calcite.util.format.FormatElementEnum.SS;
+import static org.apache.calcite.util.format.FormatElementEnum.TZR;
+import static org.apache.calcite.util.format.FormatElementEnum.WW;
+import static org.apache.calcite.util.format.FormatElementEnum.YYYY;
+
+import org.apache.calcite.sql.fun.SqlLibrary;
+import org.apache.calcite.sql.type.OperandTypes;
+import org.apache.calcite.sql.type.ReturnTypes;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeTransforms;
+import org.apache.calcite.util.format.FormatElementEnum;
+import org.apache.calcite.util.format.FormatModelElement;
+import org.apache.calcite.util.format.FormatModelElementAlias;
+import org.apache.calcite.util.format.FormatModelElementLiteral;
+import org.apache.calcite.util.format.FormatModelUtil;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+
+import java.util.Arrays;
+import java.util.List;
+
+
+/** A <a 
href="https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlqr/Format-Models.html";>
+ * format model</a> is a character literal that describes the format of {@code 
DATETIME} or {@code
+ * NUMBER} data stored in a character string.
+ *
+ * <p>{@link #unparse(SqlWriter, SqlCall, int, int)} calls
+ * {@link SqlDialect#getFormatElement(FormatElementEnum)} for known elements 
and aliases. Consider
+ * overriding this method if a dialect's format elements differs from those in 
{@link
+ * FormatElementEnum}
+ */
+public class FormatModel extends SqlInternalOperator {
+
+  private List<FormatModelElement> elements;
+  private ImmutableMap<String, FormatModelElement> fmtModelParseMap;
+
+  /**
+   * TODO(CALCITE-2980): This should live elsewhere and be associated with 
{@link SqlLibrary}
+   * or {@link org.apache.calcite.config.Lex}.
+   */
+  public static final ImmutableMap<String, FormatModelElement> 
BIG_QUERY_FORMAT_ELEMENT_PARSE_MAP =

Review Comment:
   nit:  I know it's everywhere so no need to change it but in general I hake a 
bit when I see BigQuery since we really want to do "Google SQL"



##########
core/src/main/java/org/apache/calcite/sql/dialect/BigQuerySqlDialect.java:
##########
@@ -271,6 +272,50 @@ private static TimeUnit validate(TimeUnit timeUnit) {
     }
   }
 
+  /** {@inheritDoc}
+   *
+   * <p>BigQuery format element reference:
+   * <a 
href="https://cloud.google.com/bigquery/docs/reference/standard-sql/format-elements";>
+   * BigQuery Standard SQL Format Elements</a>.
+   */
+  @Override public String getFormatElement(FormatElementEnum fmtElement) {

Review Comment:
   Yes I like this 🌷 



##########
core/src/main/java/org/apache/calcite/sql/dialect/MysqlSqlDialect.java:
##########
@@ -240,6 +281,17 @@ public MysqlSqlDialect(Context context) {
       unparseListAggCall(writer, call, null, leftPrec, rightPrec);
       break;
 
+    case FORMAT_DATE:
+    case FORMAT_TIME:
+    case FORMAT_TIMESTAMP:
+    case FORMAT_DATETIME:
+      writer.print("DATE_FORMAT(");

Review Comment:
   Like all of them should alias to the same canonical form so you can read the 
canonical form in every language idk if that makes sense



##########
core/src/main/java/org/apache/calcite/sql/dialect/MysqlSqlDialect.java:
##########
@@ -240,6 +281,17 @@ public MysqlSqlDialect(Context context) {
       unparseListAggCall(writer, call, null, leftPrec, rightPrec);
       break;
 
+    case FORMAT_DATE:
+    case FORMAT_TIME:
+    case FORMAT_TIMESTAMP:
+    case FORMAT_DATETIME:
+      writer.print("DATE_FORMAT(");

Review Comment:
   This feels a bit weird for me but I might miss some context.
   Should this live in the definition of FORMAT_DATETIME that DATE_FORMAT is 
actually an aliases with the operands in different order + different formats 
elements?



##########
core/src/main/java/org/apache/calcite/util/format/FormatModelElement.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.util.format;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * A format element in a format string. Knows how to parse and unparse itself.
+ */
+public interface FormatModelElement {

Review Comment:
   Adding examples for each methods would help take out some of the mental load 
of figuring out exactly the differences. (Easier to read) even though the names 
are are self suffiscient that would help 



##########
core/src/main/java/org/apache/calcite/util/format/FormatModelUtil.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.util.format;
+
+import com.google.common.collect.ImmutableMap;
+
+import java.util.List;
+import java.util.Stack;
+
+/**
+ * Utility class used to convert format strings into {@link 
FormatModelElement}s.
+ */
+public class FormatModelUtil {
+
+  private FormatModelUtil() {}
+
+  /**
+   * Parses the {@code fmtString} using element identifiers supplied by {@code 
fmtModel}.
+   *
+   * TODO(CALCITE-2980): make this configurable for multiple parse maps. 
Currently this only works
+   * for BigQuery and MySQL style format strings where elements begin with '%'
+   */
+  public static List<FormatModelElement> parse(String fmtString,

Review Comment:
   Not sure if this is a good advice or not but it looks like a good case to 
just use ReGex and look for the formats defined by each dialect



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