-
Notifications
You must be signed in to change notification settings - Fork 28
feat: Add Spark SQL as a shallow query engine #2241
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
Merged
+784
−59
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,5 +21,6 @@ public enum Dialect { | |
| FLINK, | ||
| POSTGRES, | ||
| SNOWFLAKE, | ||
| DUCKDB | ||
| DUCKDB, | ||
| SPARK_SQL | ||
| } | ||
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
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
28 changes: 28 additions & 0 deletions
28
sqrl-planner/src/main/java/com/datasqrl/calcite/convert/SparkSqlSqlConverters.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,28 @@ | ||
| /* | ||
| * Copyright © 2021 DataSQRL (contact@datasqrl.com) | ||
| * | ||
| * Licensed 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 com.datasqrl.calcite.convert; | ||
|
|
||
| import com.datasqrl.calcite.Dialect; | ||
| import com.datasqrl.calcite.dialect.ExtendedSparkSqlDialect; | ||
| import com.google.auto.service.AutoService; | ||
|
|
||
| @AutoService(SqlConverters.class) | ||
| public class SparkSqlSqlConverters extends AbstractSqlConverters { | ||
|
|
||
| public SparkSqlSqlConverters() { | ||
| super(Dialect.SPARK_SQL, ExtendedSparkSqlDialect.DEFAULT, false); | ||
| } | ||
| } |
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
95 changes: 95 additions & 0 deletions
95
sqrl-planner/src/main/java/com/datasqrl/calcite/dialect/ExtendedSparkSqlDialect.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,95 @@ | ||
| /* | ||
| * Copyright © 2021 DataSQRL (contact@datasqrl.com) | ||
| * | ||
| * Licensed 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 com.datasqrl.calcite.dialect; | ||
|
|
||
| import org.apache.calcite.config.NullCollation; | ||
| import org.apache.calcite.rel.type.RelDataType; | ||
| import org.apache.calcite.sql.SqlAlienSystemTypeNameSpec; | ||
| import org.apache.calcite.sql.SqlDataTypeSpec; | ||
| import org.apache.calcite.sql.SqlDialect; | ||
| import org.apache.calcite.sql.SqlNode; | ||
| import org.apache.calcite.sql.dialect.SparkSqlDialect; | ||
| import org.apache.calcite.sql.parser.SqlParserPos; | ||
|
|
||
| public class ExtendedSparkSqlDialect extends SparkSqlDialect { | ||
|
|
||
| public static final Context DEFAULT_CONTEXT = | ||
| SqlDialect.EMPTY_CONTEXT | ||
| .withDatabaseProduct(DatabaseProduct.SPARK) | ||
| .withNullCollation(NullCollation.LOW) | ||
| .withIdentifierQuoteString("`"); | ||
|
|
||
| public static final SqlDialect DEFAULT = new ExtendedSparkSqlDialect(DEFAULT_CONTEXT); | ||
|
|
||
| public ExtendedSparkSqlDialect(Context context) { | ||
| super(context); | ||
| } | ||
|
|
||
| @Override | ||
| public SqlNode getCastSpec(RelDataType type) { | ||
| var castSpec = getSparkType(type); | ||
|
|
||
| return new SqlDataTypeSpec( | ||
| new SqlAlienSystemTypeNameSpec(castSpec, type.getSqlTypeName(), SqlParserPos.ZERO), | ||
| SqlParserPos.ZERO); | ||
| } | ||
|
|
||
| private String getSparkType(RelDataType type) { | ||
| String castSpec; | ||
| switch (type.getSqlTypeName()) { | ||
| case CHAR: | ||
| case VARCHAR: | ||
| castSpec = "STRING"; | ||
| break; | ||
| case BINARY: | ||
| case VARBINARY: | ||
| castSpec = "BINARY"; | ||
| break; | ||
| case TIMESTAMP_WITH_LOCAL_TIME_ZONE: | ||
| castSpec = "TIMESTAMP_LTZ"; | ||
| break; | ||
| case TIMESTAMP: | ||
| castSpec = "TIMESTAMP_NTZ"; | ||
| break; | ||
| case ARRAY: | ||
| castSpec = "ARRAY<" + getSparkType(type.getComponentType()) + ">"; | ||
| break; | ||
| case MAP: | ||
| castSpec = | ||
| "MAP<" | ||
| + getSparkType(type.getKeyType()) | ||
| + ", " | ||
| + getSparkType(type.getValueType()) | ||
| + ">"; | ||
| break; | ||
| case ROW: | ||
| castSpec = | ||
| "STRUCT<" | ||
| + type.getFieldList().stream() | ||
| .map( | ||
| field -> | ||
| quoteIdentifier(field.getName()) + ": " + getSparkType(field.getType())) | ||
| .reduce((left, right) -> left + ", " + right) | ||
| .orElse("") | ||
| + ">"; | ||
| break; | ||
| default: | ||
| return super.getCastSpec(type).toString(); | ||
| } | ||
|
|
||
| return castSpec; | ||
| } | ||
| } | ||
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
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
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
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
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
43 changes: 43 additions & 0 deletions
43
sqrl-planner/src/main/java/com/datasqrl/engine/database/relational/SparkSqlEngine.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,43 @@ | ||
| /* | ||
| * Copyright © 2021 DataSQRL (contact@datasqrl.com) | ||
| * | ||
| * Licensed 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 com.datasqrl.engine.database.relational; | ||
|
|
||
| import com.datasqrl.config.ConnectorFactoryFactory; | ||
| import com.datasqrl.config.JdbcDialect; | ||
| import com.datasqrl.config.PackageJson; | ||
| import jakarta.inject.Inject; | ||
| import lombok.NonNull; | ||
|
|
||
| public class SparkSqlEngine extends AbstractJDBCShallowQueryEngine { | ||
|
|
||
| @Inject | ||
| public SparkSqlEngine(@NonNull PackageJson json, ConnectorFactoryFactory connectorFactory) { | ||
| super( | ||
| SparkSqlEngineFactory.ENGINE_NAME, | ||
| json.getEngines().getEngineConfigOrEmpty(SparkSqlEngineFactory.ENGINE_NAME), | ||
| connectorFactory); | ||
| } | ||
|
|
||
| @Override | ||
| protected JdbcDialect getDialect() { | ||
| return JdbcDialect.Iceberg; | ||
| } | ||
|
|
||
| @Override | ||
| public JdbcStatementFactory getStatementFactory() { | ||
| return new SparkSqlStatementFactory(); | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
...-planner/src/main/java/com/datasqrl/engine/database/relational/SparkSqlEngineFactory.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,36 @@ | ||
| /* | ||
| * Copyright © 2021 DataSQRL (contact@datasqrl.com) | ||
| * | ||
| * Licensed 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 com.datasqrl.engine.database.relational; | ||
|
|
||
| import com.datasqrl.config.EngineFactory; | ||
| import com.datasqrl.engine.database.DatabaseEngineFactory; | ||
| import com.google.auto.service.AutoService; | ||
|
|
||
| @AutoService(EngineFactory.class) | ||
| public class SparkSqlEngineFactory implements DatabaseEngineFactory { | ||
|
|
||
| public static final String ENGINE_NAME = "sparksql"; | ||
|
|
||
| @Override | ||
| public String getEngineName() { | ||
| return ENGINE_NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public Class getFactoryClass() { | ||
| return SparkSqlEngine.class; | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.