forked from xinyual/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Implement implict cast and type validation with Calcite #6
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
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
844664d
Enable SqlValidator on ppl & converted validated SqlNode back to RelNode
yuancu fa9e656
Experiment blacklist certain coercion by extending TypeCoercionImpl
yuancu 70e1e72
Allow validation on UDFs
yuancu e2d631d
Convert all string functions to operators
yuancu d8d2763
Reimplement atan, sqrt, strcmp, xor with SqlCall rewrite
yuancu 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 hidden or 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
27 changes: 27 additions & 0 deletions
27
core/src/main/java/org/opensearch/sql/calcite/PplRelToSqlConverter.java
This file contains hidden or 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,27 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.calcite; | ||
|
|
||
| import org.apache.calcite.rel.rel2sql.RelToSqlConverter; | ||
| import org.apache.calcite.sql.SqlDialect; | ||
|
|
||
| /** | ||
| * An extension of {@link RelToSqlConverter} to convert a relation algebra tree, translated from of | ||
| * PPL query, into a SQL statement. | ||
| * | ||
| * <p>Currently, we haven't implemented any specific change to it, just leaving it for future | ||
| * extension. | ||
| */ | ||
| public class PplRelToSqlConverter extends RelToSqlConverter { | ||
| /** | ||
| * Creates a RelToSqlConverter. | ||
| * | ||
| * @param dialect the SQL dialect to use | ||
| */ | ||
| public PplRelToSqlConverter(SqlDialect dialect) { | ||
| super(dialect); | ||
| } | ||
| } |
This file contains hidden or 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
109 changes: 109 additions & 0 deletions
109
core/src/main/java/org/opensearch/sql/calcite/validate/PplOpTable.java
This file contains hidden or 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,109 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.calcite.validate; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.function.Consumer; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.StreamSupport; | ||
| import org.apache.calcite.sql.SqlFunction; | ||
| import org.apache.calcite.sql.SqlFunctionCategory; | ||
| import org.apache.calcite.sql.SqlIdentifier; | ||
| import org.apache.calcite.sql.SqlOperator; | ||
| import org.apache.calcite.sql.SqlOperatorTable; | ||
| import org.apache.calcite.sql.SqlSyntax; | ||
| import org.apache.calcite.sql.validate.SqlNameMatcher; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
| import org.opensearch.sql.expression.function.BuiltinFunctionName; | ||
|
|
||
| /** | ||
| * PPLOpTable is a custom implementation of {@link SqlOperatorTable} that provides a way to register | ||
| * and look up PPL operators. | ||
| */ | ||
| public class PplOpTable implements SqlOperatorTable { | ||
| // Implementation notes: | ||
| // - Did not extend ListSqlOperatorTable because it does not support registering multiple | ||
| // SqlOperator to one name. | ||
| // - Did not extend ReflectiveSqlOperatorTable because it relies on reflectively looking for | ||
| // member fields of | ||
| // SqlOperator type, which is not suitable for our use case. | ||
| // - Did not add SqlOperatorTable to PPLFuncImpTable to reduce chaos with existing implementation | ||
|
|
||
| protected Map<BuiltinFunctionName, ArrayList<SqlOperator>> operators; | ||
|
|
||
| private static final PplOpTable INSTANCE = new PplOpTable(); | ||
|
|
||
| public static PplOpTable getInstance() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| private PplOpTable() { | ||
| this.operators = new HashMap<>(); | ||
| } | ||
|
|
||
| @Override | ||
| public void lookupOperatorOverloads( | ||
| SqlIdentifier opName, | ||
| @Nullable SqlFunctionCategory category, | ||
| SqlSyntax syntax, | ||
| List<SqlOperator> operatorList, | ||
| SqlNameMatcher nameMatcher) { | ||
| if (!opName.isSimple()) { | ||
| return; | ||
| } | ||
| final String simpleName = opName.getSimple(); | ||
| lookUpOperators( | ||
| simpleName, | ||
| op -> { | ||
| if (op.getSyntax() != syntax && op.getSyntax().family != syntax.family) { | ||
| // Allow retrieval on exact syntax or family; for example, | ||
| // CURRENT_DATETIME has FUNCTION_ID syntax but can also be called with | ||
| // both FUNCTION_ID and FUNCTION syntax (e.g. SELECT CURRENT_DATETIME, | ||
| // CURRENT_DATETIME('UTC')). | ||
| return; | ||
| } | ||
| if (category != null | ||
| && category != category(op) | ||
| && !category.isUserDefinedNotSpecificFunction()) { | ||
| return; | ||
| } | ||
| operatorList.add(op); | ||
| }); | ||
| } | ||
|
|
||
| protected void lookUpOperators(String name, Consumer<SqlOperator> consumer) { | ||
| final Optional<BuiltinFunctionName> funcNameOpt = BuiltinFunctionName.of(name); | ||
| if (funcNameOpt.isEmpty()) { | ||
| return; // No such function | ||
| } | ||
| operators.get(funcNameOpt.get()).forEach(consumer); | ||
| } | ||
|
|
||
| protected static SqlFunctionCategory category(SqlOperator operator) { | ||
| if (operator instanceof SqlFunction) { | ||
| return ((SqlFunction) operator).getFunctionType(); | ||
| } else { | ||
| return SqlFunctionCategory.SYSTEM; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public List<SqlOperator> getOperatorList() { | ||
| return operators.values().stream() | ||
| .flatMap(iterable -> StreamSupport.stream(iterable.spliterator(), false)) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public void add(BuiltinFunctionName name, SqlOperator operator) { | ||
| ArrayList<SqlOperator> list = operators.getOrDefault(name, new ArrayList<>()); | ||
| list.add(operator); | ||
| operators.put(name, list); | ||
| } | ||
| } | ||
63 changes: 63 additions & 0 deletions
63
core/src/main/java/org/opensearch/sql/calcite/validate/PplTypeCoercion.java
This file contains hidden or 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,63 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.calcite.validate; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.IntStream; | ||
| import org.apache.calcite.rel.type.RelDataType; | ||
| import org.apache.calcite.rel.type.RelDataTypeFactory; | ||
| import org.apache.calcite.sql.SqlCallBinding; | ||
| import org.apache.calcite.sql.type.SqlTypeFamily; | ||
| import org.apache.calcite.sql.validate.SqlValidator; | ||
| import org.apache.calcite.sql.validate.implicit.TypeCoercionImpl; | ||
|
|
||
| public class PplTypeCoercion extends TypeCoercionImpl { | ||
| // A blacklist of coercions that are not allowed in PPL. | ||
| // key cannot be cast from values | ||
| private static final Map<SqlTypeFamily, Set<SqlTypeFamily>> BLACKLISTED_COERCIONS; | ||
|
|
||
| static { | ||
| // Initialize the blacklist for coercions that are not allowed in PPL. | ||
| BLACKLISTED_COERCIONS = | ||
| Map.of( | ||
| SqlTypeFamily.CHARACTER, | ||
| Set.of(SqlTypeFamily.NUMERIC), | ||
| SqlTypeFamily.STRING, | ||
| Set.of(SqlTypeFamily.NUMERIC), | ||
| SqlTypeFamily.NUMERIC, | ||
| Set.of(SqlTypeFamily.CHARACTER, SqlTypeFamily.STRING)); | ||
| } | ||
|
|
||
| public PplTypeCoercion(RelDataTypeFactory typeFactory, SqlValidator validator) { | ||
| super(typeFactory, validator); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean builtinFunctionCoercion( | ||
| SqlCallBinding binding, | ||
| List<RelDataType> operandTypes, | ||
| List<SqlTypeFamily> expectedFamilies) { | ||
| assert binding.getOperandCount() == operandTypes.size(); | ||
| if (IntStream.range(0, operandTypes.size()) | ||
| .anyMatch(i -> isBlacklistedCoercion(operandTypes.get(i), expectedFamilies.get(i)))) { | ||
| return false; | ||
| } | ||
| return super.builtinFunctionCoercion(binding, operandTypes, expectedFamilies); | ||
| } | ||
|
|
||
| // This method tries to blacklist coercions that are not allowed in PPL. | ||
| private boolean isBlacklistedCoercion(RelDataType operandType, SqlTypeFamily expectedFamily) { | ||
| if (BLACKLISTED_COERCIONS.containsKey(expectedFamily)) { | ||
| Set<SqlTypeFamily> blacklistedFamilies = BLACKLISTED_COERCIONS.get(expectedFamily); | ||
| if (blacklistedFamilies.contains(operandType.getSqlTypeName().getFamily())) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
core/src/main/java/org/opensearch/sql/calcite/validate/PplValidator.java
This file contains hidden or 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,29 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.calcite.validate; | ||
|
|
||
| import org.apache.calcite.rel.type.RelDataTypeFactory; | ||
| import org.apache.calcite.sql.SqlOperatorTable; | ||
| import org.apache.calcite.sql.validate.SqlValidatorCatalogReader; | ||
| import org.apache.calcite.sql.validate.SqlValidatorImpl; | ||
|
|
||
| public class PplValidator extends SqlValidatorImpl { | ||
| /** | ||
| * Creates a validator. | ||
| * | ||
| * @param opTab Operator table | ||
| * @param catalogReader Catalog reader | ||
| * @param typeFactory Type factory | ||
| * @param config Config | ||
| */ | ||
| protected PplValidator( | ||
| SqlOperatorTable opTab, | ||
| SqlValidatorCatalogReader catalogReader, | ||
| RelDataTypeFactory typeFactory, | ||
| Config config) { | ||
| super(opTab, catalogReader, typeFactory, config); | ||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
56
core/src/main/java/org/opensearch/sql/calcite/validate/TypeChecker.java
This file contains hidden or 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,56 @@ | ||||||||||||
| /* | ||||||||||||
| * Copyright OpenSearch Contributors | ||||||||||||
| * SPDX-License-Identifier: Apache-2.0 | ||||||||||||
| */ | ||||||||||||
|
|
||||||||||||
| package org.opensearch.sql.calcite.validate; | ||||||||||||
|
|
||||||||||||
| import org.apache.calcite.jdbc.CalcitePrepare; | ||||||||||||
| import org.apache.calcite.jdbc.CalciteSchema; | ||||||||||||
| import org.apache.calcite.prepare.CalciteCatalogReader; | ||||||||||||
| import org.apache.calcite.rel.type.RelDataTypeFactory; | ||||||||||||
| import org.apache.calcite.schema.SchemaPlus; | ||||||||||||
| import org.apache.calcite.server.CalciteServerStatement; | ||||||||||||
| import org.apache.calcite.sql.type.SqlTypeCoercionRule; | ||||||||||||
| import org.apache.calcite.sql.validate.SqlValidator; | ||||||||||||
| import org.apache.calcite.sql.validate.implicit.TypeCoercion; | ||||||||||||
| import org.apache.calcite.tools.FrameworkConfig; | ||||||||||||
| import org.opensearch.sql.calcite.utils.OpenSearchTypeFactory; | ||||||||||||
|
|
||||||||||||
| public class TypeChecker { | ||||||||||||
| public static SqlValidator getValidator( | ||||||||||||
| CalciteServerStatement statement, FrameworkConfig config) { | ||||||||||||
|
||||||||||||
| CalciteServerStatement statement, FrameworkConfig config) { | |
| CalciteServerStatement statement, FrameworkConfig config) { | |
| if (statement == null) { | |
| throw new IllegalArgumentException("CalciteServerStatement parameter cannot be null"); | |
| } |
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.
Potential NullPointerException if operators.get(funcNameOpt.get()) returns null when the function name is not registered in the operators map.