korlov42 commented on code in PR #5949: URL: https://github.com/apache/ignite-3/pull/5949#discussion_r2123856976
########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/format/Scanner.java: ########## @@ -0,0 +1,281 @@ +/* + * 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.ignite.internal.sql.engine.util.format; + +import static org.apache.ignite.internal.lang.IgniteStringFormatter.format; + +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import org.apache.ignite.internal.sql.engine.util.format.DateTimeFormatElement.ElementKind; +import org.apache.ignite.internal.sql.engine.util.format.DateTimeTemplateField.FieldKind; + +/** + * Converts a format string into a sequence of elements (delimiters or fields). + */ +final class Scanner { + private static final Map<String, DateTimeTemplateField> FIELDS_BY_PATTERN; + + static { + FIELDS_BY_PATTERN = new HashMap<>(); + for (var field : DateTimeTemplateField.values()) { + FIELDS_BY_PATTERN.put(field.asPattern(), field); + } + } + + private final String pattern; + + /** Encountered elements. */ + private final List<DateTimeFormatElement> elements = new ArrayList<>(); + + /** A set of encountered fields. Each field can be present at most once. */ + private final EnumSet<DateTimeTemplateField> fields = EnumSet.noneOf(DateTimeTemplateField.class); + + /** A set of encountered fields kind. */ + private final EnumSet<FieldKind> groups = EnumSet.noneOf(FieldKind.class); + + private int start; + + private int current; + + Scanner(String pattern) { + Objects.requireNonNull(pattern, "pattern"); + // the SQL date time format is case-insensitive. + this.pattern = pattern.toUpperCase(Locale.US); + } + + List<DateTimeFormatElement> parse() { + while (!atEnd()) { + start = current; + scanToken(); + } + + validateFields(); + + return elements; + } + + private boolean atEnd() { + return current >= pattern.length(); + } + + private void scanToken() { + char c = advance(); + switch (c) { + case '-': + case '.': + case '/': + case ',': + case '\'': + case ';': + case ':': + case ' ': + addToken(ElementKind.DELIMITER); + break; + default: Review Comment: perhaps, it worth to add `case` branches for every first letter of supported fields patter, so we won't be matching all the patterns, but only subset: ``` case 'Y': if (match("YYYY") || match("YYY") || match("YY") || match("Y")) { addField(); } break; ``` ########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/format/Scanner.java: ########## @@ -0,0 +1,281 @@ +/* + * 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.ignite.internal.sql.engine.util.format; + +import static org.apache.ignite.internal.lang.IgniteStringFormatter.format; + +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import org.apache.ignite.internal.sql.engine.util.format.DateTimeFormatElement.ElementKind; +import org.apache.ignite.internal.sql.engine.util.format.DateTimeTemplateField.FieldKind; + +/** + * Converts a format string into a sequence of elements (delimiters or fields). + */ +final class Scanner { + private static final Map<String, DateTimeTemplateField> FIELDS_BY_PATTERN; + + static { + FIELDS_BY_PATTERN = new HashMap<>(); + for (var field : DateTimeTemplateField.values()) { + FIELDS_BY_PATTERN.put(field.asPattern(), field); + } + } + + private final String pattern; + + /** Encountered elements. */ + private final List<DateTimeFormatElement> elements = new ArrayList<>(); + + /** A set of encountered fields. Each field can be present at most once. */ + private final EnumSet<DateTimeTemplateField> fields = EnumSet.noneOf(DateTimeTemplateField.class); + + /** A set of encountered fields kind. */ + private final EnumSet<FieldKind> groups = EnumSet.noneOf(FieldKind.class); + + private int start; + + private int current; + + Scanner(String pattern) { + Objects.requireNonNull(pattern, "pattern"); + // the SQL date time format is case-insensitive. + this.pattern = pattern.toUpperCase(Locale.US); + } + + List<DateTimeFormatElement> parse() { Review Comment: nitpick: perhaps, `scan()` will be more natural for `scanner` ########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/format/DateTimeTemplateField.java: ########## @@ -0,0 +1,126 @@ +/* + * 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.ignite.internal.sql.engine.util.format; + +/** + * Supported datetime template fields. + */ +enum DateTimeTemplateField { + YYYY, + YYY, + YY, + Y, + RRRR, + RR, + MM, + DD, + DDD, + HH, + HH12, + HH24, + MI, + SS, + SSSSS, + FF1, + FF2, + FF3, + FF4, + FF5, + FF6, + FF7, + FF8, + FF9, + PM, + AM, + TZH, + TZM; + + String asPattern() { + if (this == AM) { + return "A.M."; + } else if (this == PM) { + return "P.M."; + } else { + return this.name(); + } + } + + FieldKind kind() { + switch (this) { Review Comment: nitpick: instead of having switch by `this`, I think it might be better to make it property ########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/format/Scanner.java: ########## @@ -0,0 +1,281 @@ +/* + * 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.ignite.internal.sql.engine.util.format; + +import static org.apache.ignite.internal.lang.IgniteStringFormatter.format; + +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import org.apache.ignite.internal.sql.engine.util.format.DateTimeFormatElement.ElementKind; +import org.apache.ignite.internal.sql.engine.util.format.DateTimeTemplateField.FieldKind; + +/** + * Converts a format string into a sequence of elements (delimiters or fields). + */ +final class Scanner { + private static final Map<String, DateTimeTemplateField> FIELDS_BY_PATTERN; + + static { + FIELDS_BY_PATTERN = new HashMap<>(); + for (var field : DateTimeTemplateField.values()) { + FIELDS_BY_PATTERN.put(field.asPattern(), field); + } + } + + private final String pattern; + + /** Encountered elements. */ + private final List<DateTimeFormatElement> elements = new ArrayList<>(); + + /** A set of encountered fields. Each field can be present at most once. */ + private final EnumSet<DateTimeTemplateField> fields = EnumSet.noneOf(DateTimeTemplateField.class); + + /** A set of encountered fields kind. */ + private final EnumSet<FieldKind> groups = EnumSet.noneOf(FieldKind.class); + + private int start; + + private int current; + + Scanner(String pattern) { + Objects.requireNonNull(pattern, "pattern"); + // the SQL date time format is case-insensitive. + this.pattern = pattern.toUpperCase(Locale.US); + } + + List<DateTimeFormatElement> parse() { + while (!atEnd()) { + start = current; + scanToken(); + } + + validateFields(); + + return elements; + } + + private boolean atEnd() { + return current >= pattern.length(); + } + + private void scanToken() { + char c = advance(); + switch (c) { + case '-': + case '.': + case '/': + case ',': + case '\'': + case ';': + case ':': + case ' ': + addToken(ElementKind.DELIMITER); Review Comment: ```suggestion addDelimiter(c); ``` -- 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: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org