|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.lang; |
| 7 | + |
| 8 | +import static org.opensearch.sql.executor.QueryType.PPL; |
| 9 | + |
| 10 | +import org.opensearch.sql.data.type.ExprType; |
| 11 | +import org.opensearch.sql.executor.QueryType; |
| 12 | + |
| 13 | +/** |
| 14 | + * Represents a language specification for query processing. |
| 15 | + * |
| 16 | + * <p>This interface defines basic methods for language-specific behaviors, such as determining the |
| 17 | + * language type and mapping expression types to type names. Two language specifications are |
| 18 | + * provided: SQL and PPL. |
| 19 | + */ |
| 20 | +public interface LangSpec { |
| 21 | + |
| 22 | + /** The default SQL language specification instance. */ |
| 23 | + LangSpec SQL_SPEC = new LangSpec() {}; |
| 24 | + |
| 25 | + /** |
| 26 | + * Returns a language specification instance based on the provided language name. |
| 27 | + * |
| 28 | + * @param language the name of the language, case-insensitive. |
| 29 | + * @return the PPL language specification if the language is PPL (ignoring case); otherwise, the |
| 30 | + * SQL language specification. |
| 31 | + */ |
| 32 | + static LangSpec fromLanguage(String language) { |
| 33 | + if (PPL.name().equalsIgnoreCase(language)) { |
| 34 | + return PPLLangSpec.PPL_SPEC; |
| 35 | + } else { |
| 36 | + return SQL_SPEC; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Returns the language type of this specification. |
| 42 | + * |
| 43 | + * <p>By default, the language is considered SQL. |
| 44 | + * |
| 45 | + * @return the language type, SQL by default. |
| 46 | + */ |
| 47 | + default QueryType language() { |
| 48 | + return QueryType.SQL; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Returns the type name for the given expression type. |
| 53 | + * |
| 54 | + * <p>This default implementation returns the result of {@code exprType.typeName()}. |
| 55 | + * |
| 56 | + * @param exprType the expression type. |
| 57 | + * @return the type name of the expression. |
| 58 | + */ |
| 59 | + default String typeName(ExprType exprType) { |
| 60 | + return exprType.typeName(); |
| 61 | + } |
| 62 | +} |
0 commit comments