forked from dremio/dremio-oss
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
CREATE FOLDER $ RECUSRIVE
SQL Support
- Loading branch information
Peter Wicks
committed
Feb 6, 2020
1 parent
ee4cfb7
commit e3b0da2
Showing
6 changed files
with
211 additions
and
3 deletions.
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
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
99 changes: 99 additions & 0 deletions
99
...c/main/java/com/dremio/exec/planner/sql/handlers/direct/CreateFolderRecursiveHandler.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,99 @@ | ||
/* | ||
* Copyright (C) 2017-2019 Dremio Corporation | ||
* | ||
* 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.dremio.exec.planner.sql.handlers.direct; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.dremio.common.exceptions.UserException; | ||
import com.dremio.exec.catalog.Catalog; | ||
import com.dremio.exec.planner.sql.parser.SqlCreateFolderRecursive; | ||
import com.dremio.service.namespace.NamespaceException; | ||
import com.dremio.service.namespace.NamespaceKey; | ||
import com.dremio.service.namespace.NamespaceService; | ||
import com.dremio.service.namespace.space.proto.FolderConfig; | ||
import com.dremio.service.namespace.space.proto.SpaceConfig; | ||
|
||
import static com.dremio.exec.planner.sql.handlers.direct.SimpleCommandResult.successful; | ||
|
||
import org.apache.calcite.sql.SqlNode; | ||
|
||
|
||
/** | ||
* Handler for <code>CREATE FOLDER RECURSIVE</code> command. | ||
*/ | ||
public class CreateFolderRecursiveHandler extends SimpleDirectHandler { | ||
private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(CreateFolderRecursiveHandler.class); | ||
|
||
private final Catalog catalog; | ||
private final NamespaceService namespaceService; | ||
|
||
public CreateFolderRecursiveHandler(Catalog catalog, NamespaceService namespaceService) { | ||
this.catalog = catalog; | ||
this.namespaceService = namespaceService; | ||
} | ||
|
||
@Override | ||
public List<SimpleCommandResult> toResult(String sql, SqlNode sqlNode) throws Exception { | ||
final SqlCreateFolderRecursive sqlRefreshSourceStatus = SqlNodeUtil.unwrap(sqlNode, SqlCreateFolderRecursive.class); | ||
final NamespaceKey path = sqlRefreshSourceStatus.getPath(); | ||
|
||
String root = path.getRoot(); | ||
if(root.startsWith("@") || root.equalsIgnoreCase("sys") || root.equalsIgnoreCase("INFORMATION_SCHEMA")) { | ||
throw UserException.parseError().message("Unable to create spaces or folders in Home, sys, or INFORMATION_SCHEMA.", path).build(logger); | ||
} | ||
|
||
String message; | ||
try { | ||
// Check if space exists | ||
final NamespaceKey rootKey = new NamespaceKey(path.getRoot()); | ||
try { | ||
namespaceService.getSpace(rootKey); | ||
} catch (NamespaceException nse) { | ||
// Space does not exist, create it | ||
SpaceConfig spaceConfig = new SpaceConfig() | ||
.setName(path.getRoot()); | ||
namespaceService.addOrUpdateSpace(rootKey, spaceConfig); | ||
} | ||
|
||
// For each sub folder, create folder | ||
final List<String> folderPath = new ArrayList<>(); | ||
folderPath.add(path.getRoot()); | ||
for(int p=1;p<path.getPathComponents().size();p++){ | ||
final String folderName = path.getPathComponents().get(p); | ||
folderPath.add(folderName); | ||
final NamespaceKey folderKey = new NamespaceKey(folderPath); | ||
try { | ||
namespaceService.getFolder(folderKey); | ||
} catch (NamespaceException nse) { | ||
// Space does not exist, create it | ||
final FolderConfig folderConfig = new FolderConfig(); | ||
folderConfig.setFullPathList(folderPath); | ||
folderConfig.setName(folderName); | ||
namespaceService.addOrUpdateFolder(folderKey, folderConfig); | ||
} | ||
} | ||
|
||
message = "Created Successfully"; | ||
} catch (Exception ex) { | ||
message = ex.getMessage(); | ||
} | ||
|
||
return singletonList(successful(String.format(message, path.toString()))); | ||
} | ||
} |
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
83 changes: 83 additions & 0 deletions
83
sabot/kernel/src/main/java/com/dremio/exec/planner/sql/parser/SqlCreateFolderRecursive.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,83 @@ | ||
/* | ||
* Copyright (C) 2017-2019 Dremio Corporation | ||
* | ||
* 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.dremio.exec.planner.sql.parser; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.calcite.sql.SqlCall; | ||
import org.apache.calcite.sql.SqlIdentifier; | ||
import org.apache.calcite.sql.SqlKind; | ||
import org.apache.calcite.sql.SqlLiteral; | ||
import org.apache.calcite.sql.SqlNode; | ||
import org.apache.calcite.sql.SqlOperator; | ||
import org.apache.calcite.sql.SqlSpecialOperator; | ||
import org.apache.calcite.sql.SqlWriter; | ||
import org.apache.calcite.sql.parser.SqlParserPos; | ||
import org.apache.calcite.util.ImmutableNullableList; | ||
|
||
import com.dremio.service.namespace.NamespaceKey; | ||
|
||
/** | ||
* SQL node tree for <code>CREATE FOLDER folder_path RECURSIVE </code> | ||
*/ | ||
public class SqlCreateFolderRecursive extends SqlSystemCall { | ||
|
||
public static final SqlSpecialOperator OPERATOR = | ||
new SqlSpecialOperator("CREATE_FOLDER", SqlKind.OTHER) { | ||
@Override public SqlCall createCall(SqlLiteral functionQualifier, | ||
SqlParserPos pos, SqlNode... operands) { | ||
return new SqlCreateFolderRecursive(pos, (SqlIdentifier) operands[0]); | ||
} | ||
}; | ||
|
||
private SqlIdentifier folderPath; | ||
|
||
/** Creates a SqlCreateFolderRecursive. */ | ||
public SqlCreateFolderRecursive(SqlParserPos pos, SqlIdentifier source) { | ||
super(pos); | ||
this.folderPath = source; | ||
} | ||
|
||
@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) { | ||
writer.keyword("CREATE"); | ||
writer.keyword("FOLDER"); | ||
folderPath.unparse(writer, leftPrec, rightPrec); | ||
writer.keyword("RECURSIVE"); | ||
} | ||
|
||
@Override public void setOperand(int i, SqlNode operand) { | ||
switch (i) { | ||
case 0: | ||
folderPath = (SqlIdentifier) operand; | ||
break; | ||
default: | ||
throw new AssertionError(i); | ||
} | ||
} | ||
|
||
@Override public SqlOperator getOperator() { | ||
return OPERATOR; | ||
} | ||
|
||
@Override public List<SqlNode> getOperandList() { | ||
return ImmutableNullableList.<SqlNode>of(folderPath); | ||
} | ||
|
||
public NamespaceKey getPath() { | ||
return new NamespaceKey(folderPath.names); | ||
} | ||
} |