-
Notifications
You must be signed in to change notification settings - Fork 149
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
Unified OpenSearch PPL Data Type #3345
Open
penghuo
wants to merge
6
commits into
opensearch-project:main
Choose a base branch
from
penghuo:issue3339
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
core/src/main/java/org/opensearch/sql/lang/PPLLangSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
core/src/test/java/org/opensearch/sql/lang/LangSpecTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
core/src/test/java/org/opensearch/sql/lang/PPLLangSpecTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Since this is a breaking change, why don't we directly change the old type to the new type, instead of introducing
LangSpec
? Isn't this unnecessarily adding complexity?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.
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.