Skip to content
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

IGNITE-24022 Sql. Fix CreateSchemaCommand catalog command validation #5084

Merged
merged 10 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.function.LongSupplier;
import org.apache.ignite.internal.catalog.commands.AlterZoneSetDefaultCommand;
import org.apache.ignite.internal.catalog.commands.CreateSchemaCommand;
import org.apache.ignite.internal.catalog.commands.CreateSystemSchemaCommand;
import org.apache.ignite.internal.catalog.commands.CreateZoneCommand;
import org.apache.ignite.internal.catalog.commands.StorageProfileParams;
import org.apache.ignite.internal.catalog.descriptors.CatalogIndexDescriptor;
Expand Down Expand Up @@ -391,7 +392,7 @@ private CompletableFuture<Void> initCatalog(Catalog emptyCatalog) {
.build(),
// Add schemas
CreateSchemaCommand.builder().name(SqlCommon.DEFAULT_SCHEMA_NAME).build(),
CreateSchemaCommand.builder().name(SYSTEM_SCHEMA_NAME).build()
CreateSystemSchemaCommand.builder().name(SYSTEM_SCHEMA_NAME).build()
);

List<UpdateEntry> entries = new BulkUpdateProducer(initCommands).get(emptyCatalog);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import org.apache.ignite.internal.catalog.Catalog;
import org.apache.ignite.internal.catalog.CatalogCommand;
import org.apache.ignite.internal.catalog.CatalogValidationException;
import org.apache.ignite.internal.catalog.SchemaExistsException;
import org.apache.ignite.internal.catalog.descriptors.CatalogIndexDescriptor;
import org.apache.ignite.internal.catalog.descriptors.CatalogSchemaDescriptor;
Expand Down Expand Up @@ -56,6 +57,10 @@ public boolean ifNotExists() {
/** {@inheritDoc} */
@Override
public List<UpdateEntry> get(Catalog catalog) {
if (CatalogUtils.isSystemSchema(schemaName)) {
throw new CatalogValidationException("Reserved system schema with name '{}' can't be created.", schemaName);
}

int id = catalog.objectIdGenState();

CatalogSchemaDescriptor schema = catalog.schema(schemaName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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.ignite.internal.catalog.commands;

import static org.apache.ignite.internal.catalog.CatalogManagerImpl.INITIAL_CAUSALITY_TOKEN;
import static org.apache.ignite.internal.catalog.CatalogParamsValidationUtils.validateIdentifier;
import static org.apache.ignite.internal.lang.IgniteStringFormatter.format;

import java.util.List;
import org.apache.ignite.internal.catalog.Catalog;
import org.apache.ignite.internal.catalog.CatalogCommand;
import org.apache.ignite.internal.catalog.CatalogValidationException;
import org.apache.ignite.internal.catalog.descriptors.CatalogIndexDescriptor;
import org.apache.ignite.internal.catalog.descriptors.CatalogSchemaDescriptor;
import org.apache.ignite.internal.catalog.descriptors.CatalogSystemViewDescriptor;
import org.apache.ignite.internal.catalog.descriptors.CatalogTableDescriptor;
import org.apache.ignite.internal.catalog.storage.NewSchemaEntry;
import org.apache.ignite.internal.catalog.storage.ObjectIdGenUpdateEntry;
import org.apache.ignite.internal.catalog.storage.UpdateEntry;

/**
* Command to create a system schema.
*/
public class CreateSystemSchemaCommand implements CatalogCommand {
private final String schemaName;

private CreateSystemSchemaCommand(String schemaName) {
validateIdentifier(schemaName, "Name of the schema");

if (!CatalogUtils.isSystemSchema(schemaName)) {
throw new CatalogValidationException(format("Not a system schema, schema: '{}'", schemaName));
}

this.schemaName = schemaName;
}

/** {@inheritDoc} */
@Override
public List<UpdateEntry> get(Catalog catalog) {
int id = catalog.objectIdGenState();

if (catalog.schema(schemaName) != null) {
throw new CatalogValidationException(format("Schema with name '{}' already exists", schemaName));
}

CatalogSchemaDescriptor schema = new CatalogSchemaDescriptor(
id,
schemaName,
new CatalogTableDescriptor[0],
new CatalogIndexDescriptor[0],
new CatalogSystemViewDescriptor[0],
INITIAL_CAUSALITY_TOKEN
);

return List.of(
new NewSchemaEntry(schema),
new ObjectIdGenUpdateEntry(1)
);
}

/** Returns builder to create a command to create a system schema. */
public static CreateSystemSchemaCommand.Builder builder() {
return new CreateSystemSchemaCommand.Builder();
}

/** Implementation of {@link CreateSchemaCommandBuilder}. */
public static class Builder implements CreateSystemSchemaCommandBuilder {

private String name;

/** {@inheritDoc} */
@Override
public CreateSystemSchemaCommandBuilder name(String name) {
this.name = name;
return this;
}

/** {@inheritDoc} */
@Override
public CatalogCommand build() {
return new CreateSystemSchemaCommand(name);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.ignite.internal.catalog.commands;

import org.apache.ignite.internal.catalog.CatalogCommand;

/**
* Builder for a {@link CreateSystemSchemaCommand}.
*/
public interface CreateSystemSchemaCommandBuilder {
/** Sets schema name. Should not be null or blank. */
CreateSystemSchemaCommandBuilder name(String name);

/** Creates new schema command. */
CatalogCommand build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@

package org.apache.ignite.internal.catalog;

import static org.apache.ignite.internal.catalog.commands.CatalogUtils.SYSTEM_SCHEMAS;
import static org.apache.ignite.internal.lang.IgniteStringFormatter.format;
import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrowFast;
import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willSucceedFast;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.Locale;
import java.util.stream.Stream;
import org.apache.ignite.internal.catalog.commands.CreateSchemaCommand;
import org.apache.ignite.internal.catalog.commands.DropSchemaCommand;
import org.apache.ignite.internal.sql.SqlCommon;
import org.apache.ignite.internal.testframework.IgniteTestUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/** Validation tests for schema related commands. */
public class CatalogSchemaValidationTest extends BaseCatalogManagerTest {
Expand Down Expand Up @@ -86,11 +90,18 @@ public void testCreateSchemaWithNullOrEmptyNameIsRejected() {
}

@ParameterizedTest
@ValueSource(strings = {
CatalogService.SYSTEM_SCHEMA_NAME,
CatalogService.INFORMATION_SCHEMA,
CatalogService.DEFINITION_SCHEMA
})
@MethodSource("reservedSchemaNames")
public void testCreateSystemSchemaIsRejected(String schemaName) {
CatalogCommand createCmd = CreateSchemaCommand.builder().name(schemaName).build();

assertThat(manager.execute(createCmd),
willThrowFast(CatalogValidationException.class, format("Reserved system schema with name '{}' can't be created.",
schemaName))
);
}

@ParameterizedTest
@MethodSource("reservedSchemaNames")
public void testDropSystemSchemaIsForbidden(String schemaName) {
CatalogCommand dropCmd = DropSchemaCommand.builder().name(schemaName).build();

Expand All @@ -100,6 +111,23 @@ public void testDropSystemSchemaIsForbidden(String schemaName) {
);
}

private static Stream<Arguments> reservedSchemaNames() {
return SYSTEM_SCHEMAS.stream().map(Arguments::of);
}

@ParameterizedTest
@MethodSource("reservedSchemaNames")
public void testDropNotExistSchemas(String schemaName) {
schemaName = schemaName.toLowerCase(Locale.ROOT);

CatalogCommand dropCmd = DropSchemaCommand.builder().name(schemaName).build();

assertThat(
manager.execute(dropCmd),
willThrowFast(CatalogValidationException.class, format("Schema with name '{}' not found", schemaName))
);
}

@Test
@SuppressWarnings("ThrowableNotThrown")
public void testDropSchemaWithNullOrEmptyNameIsRejected() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
/**
* Tests to verify validation of {@link CreateSchemaCommand}.
*/
@SuppressWarnings({"ThrowableNotThrown"})
public class CreateSchemaCommandValidationTest extends AbstractCommandValidationTest {

@ParameterizedTest(name = "[{index}] ''{argumentsWithNames}''")
Expand Down Expand Up @@ -55,6 +56,8 @@ void commandFailsWhenSchemaAlreadyExists() {
() -> builder.build().get(catalog),
"Schema with name 'TEST' already exists"
);

builder.ifNotExists(true).build().get(catalog);
}

private static Catalog catalogWithSchema(String schemaName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.ignite.internal.catalog.commands;

import static org.apache.ignite.internal.catalog.CatalogService.INFORMATION_SCHEMA;
import static org.apache.ignite.internal.lang.IgniteStringFormatter.format;
import static org.apache.ignite.internal.testframework.IgniteTestUtils.assertThrows;

import java.util.Locale;
import org.apache.ignite.internal.catalog.Catalog;
import org.apache.ignite.internal.catalog.CatalogValidationException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Tests to verify validation of {@link CreateSystemSchemaCommand}.
*/
@SuppressWarnings({"ThrowableNotThrown"})
public class CreateSystemSchemaCommandValidationTest extends AbstractCommandValidationTest {

@ParameterizedTest(name = "[{index}] ''{argumentsWithNames}''")
@MethodSource("nullAndBlankStrings")
void schemaNameMustNotBeNullOrBlank(String name) {
CreateSystemSchemaCommandBuilder builder = CreateSystemSchemaCommand.builder().name(name);

assertThrows(
CatalogValidationException.class,
builder::build,
"Name of the schema can't be null or blank"
);
}

@Test
void commandFailsWithNonSystemSchema() {
String schemaName = INFORMATION_SCHEMA.toLowerCase(Locale.ROOT);

Catalog catalog = catalogWithSchema(INFORMATION_SCHEMA);

assertThrows(
CatalogValidationException.class,
() -> CreateSystemSchemaCommand.builder().name(schemaName).build().get(catalog),
format("Not a system schema, schema: '{}'", schemaName)
);
}

@Test
void commandFailsWhenSchemaAlreadyExists() {
CreateSystemSchemaCommandBuilder builder = CreateSystemSchemaCommand.builder().name(INFORMATION_SCHEMA);

Catalog catalog = catalogWithSchema(INFORMATION_SCHEMA);

assertThrows(
CatalogValidationException.class,
() -> builder.build().get(catalog),
format("Schema with name '{}' already exists", INFORMATION_SCHEMA)
);
}

private static Catalog catalogWithSchema(String schemaName) {
return catalog(CreateSystemSchemaCommand.builder().name(schemaName).build());
}
}
Loading