ahmedabu98 commented on code in PR #35787:
URL: https://github.com/apache/beam/pull/35787#discussion_r2376923169
##########
sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/parser/SqlUseDatabase.java:
##########
@@ -66,38 +63,32 @@ public List<SqlNode> getOperandList() {
public void execute(CalcitePrepare.Context context) {
final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true,
databaseName);
Schema schema = pair.left.schema;
- String name = checkStateNotNull(pair.right);
+ String path = databaseName.toString();
+ List<String> components = Lists.newArrayList(Splitter.on(".").split(path));
+ TableName pathOverride = TableName.create(components, "");
- if (!(schema instanceof BeamCalciteSchema)) {
- throw SqlUtil.newContextException(
- databaseName.getParserPosition(),
- RESOURCE.internal("Schema is not of instance BeamCalciteSchema"));
- }
-
- BeamCalciteSchema beamCalciteSchema = (BeamCalciteSchema) schema;
- @Nullable CatalogManager catalogManager =
beamCalciteSchema.getCatalogManager();
- if (catalogManager == null) {
+ if (!(schema instanceof CatalogManagerSchema)) {
throw SqlUtil.newContextException(
databaseName.getParserPosition(),
RESOURCE.internal(
- String.format(
- "Unexpected 'USE DATABASE' call using Schema '%s' that is
not a Catalog.",
- name)));
- }
-
- Catalog catalog = catalogManager.currentCatalog();
- if (!catalog.listDatabases().contains(name)) {
- throw SqlUtil.newContextException(
- databaseName.getParserPosition(),
- RESOURCE.internal(String.format("Cannot use database: '%s' not
found.", name)));
+ "Attempting to create database '"
+ + path
+ + "' with unexpected Calcite Schema of type "
+ + schema.getClass()));
}
- if (name.equals(catalog.currentDatabase())) {
- LOG.info("Database '{}' is already in use.", name);
- return;
+ CatalogManagerSchema catalogManagerSchema = (CatalogManagerSchema) schema;
+ CatalogSchema catalogSchema =
catalogManagerSchema.getCatalogSchema(pathOverride);
+ // if database exists in a different catalog, we need to also switch to
that catalog
+ if (pathOverride.catalog() != null
Review Comment:
Adding a test that does the following:
- create and use catalog C1
- create database D under C1
- switch to catalog C2
- drop catalog C1
- use database D
and verified that an error is thrown because C1 does not exist.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]