weiqingy commented on code in PR #2448:
URL: https://github.com/apache/auron/pull/2448#discussion_r3718191290


##########
auron-flink-extension/auron-flink-planner/src/main/java/org/apache/auron/flink/table/planner/converter/FlinkDateTimeFormatConverter.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.auron.flink.table.planner.converter;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Translates a {@code java.text.SimpleDateFormat} pattern into the native 
parser's
+ * {@code strftime}-style format string, or reports that the pattern cannot be 
translated.
+ *
+ * <p>The native {@code Flink_UnixTimestamp} function replicates {@code 
SimpleDateFormat} lenient
+ * parsing only for a fixed set of numeric fields. This converter is the 
plan-time gate: it accepts
+ * a pattern only when every token maps to a field the native parser handles 
identically, and
+ * returns {@link Optional#empty()} otherwise so the whole {@code Calc} falls 
back to Flink's engine.
+ *
+ * <p>Accepted fields and their native specifiers:
+ * <ul>
+ *   <li>{@code yyy} / {@code yyyy} &rarr; {@code %Y} (year)
+ *   <li>{@code M} / {@code MM} &rarr; {@code %m} (month)
+ *   <li>{@code d} / {@code dd} &rarr; {@code %d} (day of month)
+ *   <li>{@code H} / {@code HH} &rarr; {@code %H} (hour of day)
+ *   <li>{@code m} / {@code mm} &rarr; {@code %M} (minute)
+ *   <li>{@code s} / {@code ss} &rarr; {@code %S} (second)
+ * </ul>
+ *
+ * <p>Non-alphabetic characters are literals (a literal {@code %} is emitted 
as {@code %%}).
+ * {@code SimpleDateFormat} single-quote escaping applies, including the 
doubled {@code ''} that
+ * denotes a literal quote. Every other ASCII letter and every unlisted run 
length forces a fall
+ * back, because Java reserves all letters and the omitted forms either depend 
on a locale or on the
+ * clock at parse time.
+ *
+ * <p>Adjacency rule: run length is erased by the translation ({@code M} and 
{@code MM} both become
+ * {@code %m}), yet the native parser reads each numeric field at a canonical 
width (year 4; month,
+ * day, hour, minute, second 2) while Java's lenient scan window equals the 
run length. When two
+ * numeric fields are adjacent with no literal separator, those two widths 
must agree, so the left
+ * field's run length must equal its canonical width; otherwise the pattern 
falls back to avoid a
+ * silent divergence (e.g. {@code yyyyMd} on {@code 20201010} yields month 1 
in Java but month 10
+ * natively).
+ */
+public final class FlinkDateTimeFormatConverter {
+
+    private FlinkDateTimeFormatConverter() {
+        // utility class
+    }
+
+    /**
+     * Translates the given Java {@code SimpleDateFormat} pattern to the 
native {@code strftime}-style
+     * format string.
+     *
+     * @param javaPattern the Java date-time format pattern
+     * @return the translated native format string, or {@link 
Optional#empty()} if any part of the
+     *     pattern is outside the natively supported surface
+     */
+    public static Optional<String> translate(String javaPattern) {
+        List<Token> tokens = scan(javaPattern);
+        if (tokens == null) {

Review Comment:
   Added a `requireNonNull` in `cccb1acb`, but deliberately not the 
`Optional.empty()` behavior suggested here.
   
   `Optional.empty()` is this converter's signal that the user wrote a pattern 
outside the native surface, and the whole Calc should fall back to Flink. That 
is a normal user-facing outcome. A null pattern is not that: it would mean our 
own converter never resolved a format, which is a plumbing bug. Mapping it to 
`empty()` would route the bug into the same silent fallback and hide it, so it 
fails fast instead. The same split already exists elsewhere in this path, where 
`isSupported` returns false for user-facing cases and `buildUnixTimestamp` 
throws `IllegalArgumentException` for plumbing bugs.
   
   Null also cannot reach it today. The gate at `isUnixTimestampSupported` 
reads `javaFormat != null && 
FlinkDateTimeFormatConverter.translate(javaFormat).isPresent()`, and `&&` 
short-circuits. The other call site is inside the private `buildUnixTimestamp`, 
which the factory only reaches after `isSupported` returned true.
   
   Covered by `testNullPatternRejectedRatherThanReportedUntranslatable`.



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