-
Notifications
You must be signed in to change notification settings - Fork 153
Unified OpenSearch PPL Data Type #3345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
c70a069
6cf74f4
7e381d5
d5412a9
4926dc4
e2da843
bd20fc1
516c9b3
d21eb64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.lang; | ||
|
||
import static org.opensearch.sql.lang.LangSpec.LangType.PPL; | ||
|
||
import org.opensearch.sql.data.type.ExprType; | ||
|
||
/** | ||
* Represents a language specification for query processing. | ||
* | ||
* <p>This interface defines basic methods for language-specific behaviors, such as determining the | ||
* language type and mapping expression types to type names. Two language specifications are | ||
* provided: SQL and PPL. | ||
*/ | ||
public interface LangSpec { | ||
/** Enumerates the supported language types. */ | ||
enum LangType { | ||
/** SQL language specification. */ | ||
SQL, | ||
/** PPL (Piped Processing Language) language specification. */ | ||
PPL | ||
} | ||
|
||
/** The default SQL language specification instance. */ | ||
LangSpec SQL_SPEC = new LangSpec() {}; | ||
|
||
/** | ||
* Returns a language specification instance based on the provided language name. | ||
* | ||
* @param language the name of the language, case-insensitive. | ||
* @return the PPL language specification if the language is PPL (ignoring case); otherwise, the | ||
* SQL language specification. | ||
*/ | ||
static LangSpec fromLanguage(String language) { | ||
if (PPL.name().equalsIgnoreCase(language)) { | ||
return PPLLangSpec.PPL_SPEC; | ||
} else { | ||
return SQL_SPEC; | ||
} | ||
} | ||
|
||
/** | ||
* Returns the language type of this specification. | ||
* | ||
* <p>By default, the language is considered SQL. | ||
* | ||
* @return the language type, SQL by default. | ||
*/ | ||
default LangType language() { | ||
return LangType.SQL; | ||
} | ||
|
||
/** | ||
* Returns the type name for the given expression type. | ||
* | ||
* <p>This default implementation returns the result of {@code exprType.typeName()}. | ||
* | ||
* @param exprType the expression type. | ||
* @return the type name of the expression. | ||
*/ | ||
default String typeName(ExprType exprType) { | ||
return exprType.typeName(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.lang; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.opensearch.sql.data.type.ExprCoreType; | ||
import org.opensearch.sql.data.type.ExprType; | ||
|
||
/** | ||
* PPL language specification implementation. | ||
* | ||
* <p>This class provides a singleton implementation of {@link LangSpec} for PPL. It defines a | ||
* custom mapping from expression types to PPL type names. | ||
*/ | ||
public class PPLLangSpec implements LangSpec { | ||
|
||
public static final PPLLangSpec PPL_SPEC = new PPLLangSpec(); | ||
|
||
private static Map<ExprType, String> exprTypeToPPLType = new HashMap<>(); | ||
|
||
static { | ||
exprTypeToPPLType.put(ExprCoreType.BYTE, "tinyint"); | ||
exprTypeToPPLType.put(ExprCoreType.SHORT, "smallint"); | ||
exprTypeToPPLType.put(ExprCoreType.INTEGER, "int"); | ||
exprTypeToPPLType.put(ExprCoreType.LONG, "bigint"); | ||
} | ||
Comment on lines
+26
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is a breaking change, why don't we directly change the old type to the new type, instead of introducing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. The core type was not upgraded because I intended for the data type changes to affect only PPL and not SQL. This PR focuses on unifying PPL data types, while SQL data types can be addressed in a separate issue, as changes there would impact JDBC, ODBC, and CLI. Ideally, the query engine should use well-defined data types, with LangSpec serving as the protocol for translating these engine types to language-specific types. Once the Calcite implementation is complete, CalciteDataType will translate to ExprDataType, and LangSpec will translate from ExprDataType to the PPL response data type. |
||
|
||
private PPLLangSpec() {} | ||
|
||
@Override | ||
public LangType language() { | ||
return LangType.PPL; | ||
} | ||
|
||
/** | ||
* Returns the corresponding PPL type name for the given expression type. If the expression type | ||
* is not mapped, it returns the default type name. | ||
* | ||
* @param exprType the expression type. | ||
* @return the PPL type name associated with the expression type, or the default type name. | ||
*/ | ||
@Override | ||
public String typeName(ExprType exprType) { | ||
return exprTypeToPPLType.getOrDefault(exprType, exprType.typeName()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.lang; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.opensearch.sql.data.type.ExprCoreType; | ||
|
||
class LangSpecTest { | ||
@Test | ||
public void testFromLanguageSQL() { | ||
LangSpec spec = LangSpec.fromLanguage("SQL"); | ||
assertEquals(LangSpec.LangType.SQL, spec.language(), "Expected language type to be SQL"); | ||
assertSame(LangSpec.SQL_SPEC, spec, "Expected SQL_SPEC instance for SQL language."); | ||
} | ||
|
||
@Test | ||
public void testSQLSpecDefaultTypeName() { | ||
String result = LangSpec.SQL_SPEC.typeName(ExprCoreType.BYTE); | ||
assertEquals("BYTE", result, "SQL_SPEC should return the expression type's default type name."); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.lang; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.opensearch.sql.lang.PPLLangSpec.PPL_SPEC; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.opensearch.sql.data.type.ExprCoreType; | ||
|
||
class PPLLangSpecTest { | ||
/** Tests that the language type of the PPL specification is PPL. */ | ||
@Test | ||
public void testPPLSpecLanguage() { | ||
PPLLangSpec spec = PPL_SPEC; | ||
assertEquals(LangSpec.LangType.PPL, spec.language(), "Expected language to be PPL."); | ||
} | ||
|
||
/** | ||
* Tests that the PPL specification returns the correct type name mapping for known expression | ||
* types. Assumes that ExprCoreType constants are available. | ||
*/ | ||
@Test | ||
public void testPPLSpecTypeNameMapping() { | ||
PPLLangSpec spec = PPL_SPEC; | ||
assertEquals("tinyint", spec.typeName(ExprCoreType.BYTE), "BYTE should map to tinyint."); | ||
assertEquals("smallint", spec.typeName(ExprCoreType.SHORT), "SHORT should map to smallint."); | ||
assertEquals("int", spec.typeName(ExprCoreType.INTEGER), "INTEGER should map to int."); | ||
assertEquals("bigint", spec.typeName(ExprCoreType.LONG), "LONG should map to bigint."); | ||
} | ||
|
||
/** | ||
* Tests that an unmapped expression type returns its default type name in the PPL specification. | ||
*/ | ||
@Test | ||
public void testPPLSpecDefaultTypeName() { | ||
String result = PPL_SPEC.typeName(ExprCoreType.STRING); | ||
assertEquals("STRING", result, "Unmapped expression type should return its default type name."); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: a little duplicated with https://github.com/opensearch-project/sql/blob/main/core/src/main/java/org/opensearch/sql/executor/QueryType.java
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.