-
Notifications
You must be signed in to change notification settings - Fork 986
DRILL-6454: Native MapR DB plugin support for Hive MapR-DB json table #1314
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
Closed
Changes from all commits
Commits
Show all changes
2 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 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
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
186 changes: 186 additions & 0 deletions
186
...apache/drill/exec/planner/sql/logical/ConvertHiveMapRDBJsonScanToDrillMapRDBJsonScan.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,186 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.drill.exec.planner.sql.logical; | ||
|
|
||
| import org.apache.calcite.plan.RelOptRuleCall; | ||
| import org.apache.calcite.rel.type.RelDataType; | ||
| import org.apache.calcite.rel.type.RelDataTypeFactory; | ||
| import org.apache.calcite.rel.type.RelDataTypeField; | ||
| import org.apache.drill.common.exceptions.UserException; | ||
| import org.apache.drill.common.expression.SchemaPath; | ||
| import org.apache.drill.exec.ExecConstants; | ||
| import org.apache.drill.exec.planner.logical.DrillScanRel; | ||
| import org.apache.drill.exec.planner.logical.RelOptHelper; | ||
| import org.apache.drill.exec.store.StoragePluginOptimizerRule; | ||
| import org.apache.drill.exec.store.hive.HiveMetadataProvider; | ||
| import org.apache.drill.exec.store.hive.HiveReadEntry; | ||
| import org.apache.drill.exec.store.hive.HiveScan; | ||
| import org.apache.drill.exec.store.mapr.db.MapRDBFormatPlugin; | ||
| import org.apache.drill.exec.store.mapr.db.MapRDBFormatPluginConfig; | ||
| import org.apache.drill.exec.store.mapr.db.json.JsonScanSpec; | ||
| import org.apache.drill.exec.store.mapr.db.json.JsonTableGroupScan; | ||
| import org.ojai.DocumentConstants; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static org.apache.drill.exec.store.hive.HiveUtilities.nativeReadersRuleMatches; | ||
|
|
||
| /** | ||
| * Convert Hive scan to use Drill's native MapR-DB reader instead of Hive's MapR-DB JSON Handler. | ||
| */ | ||
| public class ConvertHiveMapRDBJsonScanToDrillMapRDBJsonScan extends StoragePluginOptimizerRule { | ||
| private static final org.slf4j.Logger logger = | ||
| org.slf4j.LoggerFactory.getLogger(ConvertHiveMapRDBJsonScanToDrillMapRDBJsonScan.class); | ||
|
|
||
| public static final ConvertHiveMapRDBJsonScanToDrillMapRDBJsonScan INSTANCE = | ||
| new ConvertHiveMapRDBJsonScanToDrillMapRDBJsonScan(); | ||
|
|
||
| /** | ||
| * The constants from org.apache.hadoop.hive.maprdb.json.conf.MapRDBConstants | ||
| */ | ||
| private static final String MAPRDB_PFX = "maprdb."; | ||
| private static final String MAPRDB_TABLE_NAME = MAPRDB_PFX + "table.name"; | ||
| private static final String ID_KEY = DocumentConstants.ID_KEY; | ||
| private static final String MAPRDB_COLUMN_ID = MAPRDB_PFX + "column.id"; | ||
|
|
||
| private ConvertHiveMapRDBJsonScanToDrillMapRDBJsonScan() { | ||
| super(RelOptHelper.any(DrillScanRel.class), "ConvertHiveScanToHiveDrillNativeScan:MapR-DB"); | ||
| } | ||
|
|
||
| /** | ||
| * {@see org.apache.drill.exec.store.hive.HiveUtilities#nativeReadersRuleMatches} | ||
| */ | ||
| @Override | ||
| public boolean matches(RelOptRuleCall call) { | ||
| try { | ||
| return nativeReadersRuleMatches(call, | ||
| Class.forName("org.apache.hadoop.hive.maprdb.json.input.HiveMapRDBJsonInputFormat")); | ||
| } catch (ClassNotFoundException e) { | ||
| throw UserException.resourceError(e) | ||
| .message("Current Drill build is not designed for working with Hive MapR-DB tables. " + | ||
| "Please disable \"%s\" option", ExecConstants.HIVE_OPTIMIZE_MAPRDB_JSON_SCAN_WITH_NATIVE_READER) | ||
| .build(logger); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onMatch(RelOptRuleCall call) { | ||
| try { | ||
| DrillScanRel hiveScanRel = call.rel(0); | ||
|
|
||
| HiveScan hiveScan = (HiveScan) hiveScanRel.getGroupScan(); | ||
| HiveReadEntry hiveReadEntry = hiveScan.getHiveReadEntry(); | ||
| HiveMetadataProvider hiveMetadataProvider = new HiveMetadataProvider(hiveScan.getUserName(), hiveReadEntry, | ||
| hiveScan.getStoragePlugin().getHiveConf()); | ||
| if (hiveMetadataProvider.getInputSplits(hiveReadEntry).isEmpty()) { | ||
| // table is empty, use original scan | ||
| return; | ||
| } | ||
|
|
||
| if (hiveScan.getHiveReadEntry().getTable().isSetPartitionKeys()) { | ||
| logger.warn("Hive MapR-DB JSON Handler doesn't support table partitioning. Consider recreating table without " + | ||
| "partitions"); | ||
| } | ||
|
|
||
| DrillScanRel nativeScanRel = createNativeScanRel(hiveScanRel); | ||
| call.transformTo(nativeScanRel); | ||
|
|
||
| /* | ||
| Drill native scan should take precedence over Hive since it's more efficient and faster. | ||
| Hive does not always give correct costing (i.e. for external tables Hive does not have number of rows | ||
| and we calculate them approximately). On the contrary, Drill calculates number of rows exactly | ||
| and thus Hive Scan can be chosen instead of Drill native scan because costings allegedly lower for Hive. | ||
| To ensure Drill MapR-DB Json scan will be chosen, reduce Hive scan importance to 0. | ||
| */ | ||
| call.getPlanner().setImportance(hiveScanRel, 0.0); | ||
| } catch (final Exception e) { | ||
| logger.warn("Failed to convert HiveScan to JsonScanSpec", e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Helper method which creates a DrillScanRel with native Drill HiveScan. | ||
| */ | ||
| private DrillScanRel createNativeScanRel(final DrillScanRel hiveScanRel) throws Exception { | ||
| RelDataTypeFactory typeFactory = hiveScanRel.getCluster().getTypeFactory(); | ||
| HiveScan hiveScan = (HiveScan) hiveScanRel.getGroupScan(); | ||
| Map<String, String> parameters = hiveScan.getHiveReadEntry().getHiveTableWrapper().getParameters(); | ||
|
|
||
| JsonScanSpec scanSpec = new JsonScanSpec(parameters.get(MAPRDB_TABLE_NAME), null); | ||
| MapRDBFormatPlugin mapRDBFormatPlugin = new MapRDBFormatPlugin( | ||
| "hive-maprdb", | ||
| hiveScan.getStoragePlugin().getContext(), | ||
| hiveScan.getHiveConf(), | ||
| hiveScan.getStoragePlugin().getConfig(), | ||
| new MapRDBFormatPluginConfig() | ||
| ); | ||
| List<SchemaPath> hiveScanCols = hiveScanRel.getColumns().stream() | ||
| .map(colNameSchemaPath -> replaceOverriddenSchemaPath(parameters, colNameSchemaPath)) | ||
| .collect(Collectors.toList()); | ||
| JsonTableGroupScan nariveMapRDBScan = | ||
| new JsonTableGroupScan( | ||
| hiveScan.getUserName(), | ||
| hiveScan.getStoragePlugin(), | ||
| mapRDBFormatPlugin, | ||
| scanSpec, | ||
| hiveScanCols | ||
| ); | ||
|
|
||
| List<String> nativeScanColNames = hiveScanRel.getRowType().getFieldList().stream() | ||
| .map(field -> replaceOverriddenColumnId(parameters, field.getName())) | ||
| .collect(Collectors.toList()); | ||
| List<RelDataType> nativeScanColTypes = hiveScanRel.getRowType().getFieldList().stream() | ||
| .map(RelDataTypeField::getType) | ||
| .collect(Collectors.toList()); | ||
| RelDataType nativeScanRowType = typeFactory.createStructType(nativeScanColTypes, nativeScanColNames); | ||
|
|
||
| return new DrillScanRel( | ||
| hiveScanRel.getCluster(), | ||
| hiveScanRel.getTraitSet(), | ||
| hiveScanRel.getTable(), | ||
| nariveMapRDBScan, | ||
| nativeScanRowType, | ||
| hiveScanCols); | ||
| } | ||
|
|
||
| /** | ||
| * Hive maps column id "_id" with custom user column id name. Replace it for {@link DrillScanRel} | ||
| * | ||
| * @param parameters Hive table properties | ||
| * @param colName Hive column name | ||
| * @return original column name, null if colName is absent | ||
| */ | ||
| private String replaceOverriddenColumnId(Map<String, String> parameters, String colName) { | ||
| return colName != null && colName.equals(parameters.get(MAPRDB_COLUMN_ID)) ? ID_KEY : colName; | ||
| } | ||
|
|
||
| /** | ||
| * The same as above, but for {@link SchemaPath} object | ||
| * | ||
| * @param parameters Hive table properties | ||
| * @param colNameSchemaPath SchemaPath with Hive column name | ||
| * @return SchemaPath with original column name | ||
| */ | ||
| private SchemaPath replaceOverriddenSchemaPath(Map<String, String> parameters, SchemaPath colNameSchemaPath) { | ||
| String hiveColumnName = colNameSchemaPath.getRootSegmentPath(); | ||
| return hiveColumnName != null && hiveColumnName.equals(parameters.get(MAPRDB_COLUMN_ID)) | ||
| ? SchemaPath.getSimplePath(ID_KEY) : colNameSchemaPath; | ||
| } | ||
| } |
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.
Where is the dependency introduced?
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.
This is
contrib/format-maprdbmodule from default profile.Therefore it is used here in default profile too.
Sources from this dependency is used in
ConvertHiveMapRDBJsonScanToDrillMapRDBJsonScan.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.
OK, I see. I was confused due to Drill practice to use the same package name in different modules/jars. This practice is not common and far from best practices.
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.
Could you clarify what package name would be better to use?
Do you mean short names instead of full ones for the common packages?
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.
No, I don't refer to using short names. I refer to using "org.apache.drill.exec" package name outside of "exec". As it is already widely used in Drill I don't suggest to change it in this PR.