Skip to content

Commit

Permalink
add support for using custom schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
seut committed Dec 11, 2014
1 parent 05b7ab0 commit 39479a0
Show file tree
Hide file tree
Showing 37 changed files with 273 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private void addColumnToTable(AddColumnAnalyzedStatement analysis, final Settabl
final Map<String, Object> mapping = analysis.analyzedTableElements().toMapping();

if (updateTemplate) {
String templateName = PartitionName.templateName(analysis.table().ident().name());
String templateName = PartitionName.templateName(analysis.table().ident().esName());
IndexTemplateMetaData indexTemplateMetaData =
clusterService.state().metaData().templates().get(templateName);
if (indexTemplateMetaData == null) {
Expand Down Expand Up @@ -310,7 +310,7 @@ private String[] getIndexNames(TableInfo tableInfo, @Nullable PartitionName part
indexNames = new String[] { partitionName.stringValue() };
}
} else {
indexNames = new String[] { tableInfo.ident().name() };
indexNames = new String[] { tableInfo.ident().esName() };
}
return indexNames;
}
Expand Down Expand Up @@ -378,11 +378,11 @@ public ListenableFuture<Long> visitAlterTableStatement(final AlterTableAnalyzedS
tableSettingsInfo.partitionTableSettingsInfo().supportedInternalSettings());
}
} else {
indices = new String[]{ analysis.table().ident().name() };
indices = new String[]{ analysis.table().ident().esName() };
}

if (analysis.table().isAlias()) {
throw new AlterTableAliasException(analysis.table().ident().name());
throw new AlterTableAliasException(analysis.table().ident().fqn());
}

final List<ListenableFuture<?>> results = new ArrayList<>(
Expand All @@ -393,7 +393,7 @@ public ListenableFuture<Long> visitAlterTableStatement(final AlterTableAnalyzedS
results.add(templateFuture);

// update template
final String templateName = PartitionName.templateName(analysis.table().ident().name());
final String templateName = PartitionName.templateName(analysis.table().ident().esName());
GetIndexTemplatesRequest getRequest = new GetIndexTemplatesRequest(templateName);

transportActionProvider.transportGetIndexTemplatesAction().execute(getRequest, new ActionListener<GetIndexTemplatesResponse>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package io.crate.analyze;

import io.crate.Constants;
import io.crate.exceptions.InvalidSchemaNameException;
import io.crate.exceptions.InvalidTableNameException;
import io.crate.metadata.TableIdent;

Expand All @@ -36,7 +37,10 @@ protected AbstractDDLAnalyzedStatement(ParameterContext parameterContext) {

@Override
public void table(TableIdent tableIdent) {
if (!isValidTableName(tableIdent.name())) {
if (tableIdent.schema() != null && !isValidTableOrSchemaName(tableIdent.schema())) {
throw new InvalidSchemaNameException(tableIdent.schema());
}
if (!isValidTableOrSchemaName(tableIdent.name())) {
throw new InvalidTableNameException(tableIdent.name());
}
this.tableIdent = tableIdent;
Expand All @@ -57,7 +61,7 @@ public boolean expectsAffectedRows() {
return true;
}

public boolean isValidTableName(String name) {
public boolean isValidTableOrSchemaName(String name) {
for (String illegalCharacter: Constants.INVALID_TABLE_NAME_CHARACTERS) {
if (name.contains(illegalCharacter)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void table(TableIdent tableIdent) {
}
TableInfo tableInfo = referenceInfos.getTableInfo(tableIdent);
if (tableInfo == null) {
throw new TableUnknownException(tableIdent.name());
throw new TableUnknownException(tableIdent.fqn());
}
// if we have a system schema, queries require scalar functions, since those are not using lucene
onlyScalarsAllowed = schemaInfo.systemSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

package io.crate.analyze;

import com.google.common.base.Optional;
import io.crate.PartitionName;
import io.crate.exceptions.SchemaUnknownException;
import io.crate.exceptions.TableUnknownException;
import io.crate.metadata.FulltextAnalyzerResolver;
Expand All @@ -31,8 +29,6 @@
import io.crate.metadata.table.SchemaInfo;
import io.crate.metadata.table.TableInfo;

import javax.annotation.Nullable;

public class AddColumnAnalyzedStatement extends AbstractDDLAnalyzedStatement {

private final ReferenceInfos referenceInfos;
Expand Down Expand Up @@ -60,7 +56,7 @@ public void table(TableIdent tableIdent) {
}
TableInfo tableInfo = schemaInfo.getTableInfo(tableIdent.name());
if (tableInfo == null) {
throw new TableUnknownException(tableIdent.name());
throw new TableUnknownException(tableIdent.fqn());
}
this.tableInfo = tableInfo;
this.tableIdent = tableIdent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void table(TableIdent tableIdent) {

tableInfo = schemaInfo.getTableInfo(tableIdent.name());
if (tableInfo == null) {
throw new TableUnknownException(tableIdent.name());
throw new TableUnknownException(tableIdent.fqn());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void ensureColumnLeafsAreNew(AnalyzedColumnDefinition column, TableInfo
if ((!column.isParentColumn() || !column.hasChildren()) && tableInfo.getReferenceInfo(column.ident()) != null) {
throw new IllegalArgumentException(String.format(
"The table \"%s\" already has a column named \"%s\"",
tableInfo.ident().name(),
tableInfo.ident().fqn(),
column.ident().sqlFqn()));
}
for (AnalyzedColumnDefinition child : column.children()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void table(TableIdent tableIdent) {

tableInfo = schemaInfo.getTableInfo(tableIdent.name());
if (tableInfo == null) {
throw new TableUnknownException(tableIdent.name());
throw new TableUnknownException(tableIdent.fqn());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package io.crate.analyze;

import io.crate.PartitionName;
import io.crate.exceptions.SchemaUnknownException;
import io.crate.exceptions.TableAlreadyExistsException;
import io.crate.exceptions.TableUnknownException;
import io.crate.metadata.ColumnIdent;
Expand Down Expand Up @@ -58,9 +59,9 @@ public void table(TableIdent tableIdent) {
// is it an orphaned alias? allow it,
// as it will be deleted before the actual table creation
if (!isOrphanedAlias(existingTable)) {
throw new TableAlreadyExistsException(existingTable.ident().name());
throw new TableAlreadyExistsException(existingTable.ident().fqn());
}
} catch (TableUnknownException e) {
} catch (TableUnknownException | SchemaUnknownException e) {
// ok, that is expected
}
super.table(tableIdent); // name validated here
Expand Down Expand Up @@ -117,7 +118,7 @@ public boolean isPartitioned() {
*/
public @Nullable String templateName() {
if (isPartitioned()) {
return PartitionName.templateName(tableIdent().name());
return PartitionName.templateName(tableIdent().esName());
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
*/
package io.crate.analyze;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import io.crate.Constants;
import io.crate.metadata.ColumnIdent;
import io.crate.metadata.FulltextAnalyzerResolver;
Expand Down Expand Up @@ -60,8 +58,6 @@ protected Void visitNode(Node node, CreateTableAnalyzedStatement context) {
@Override
public Void visitCreateTable(CreateTable node, CreateTableAnalyzedStatement context) {
TableIdent tableIdent = TableIdent.of(node.name());
Preconditions.checkArgument(Strings.isNullOrEmpty(tableIdent.schema()),
"A custom schema name must not be specified in the CREATE TABLE clause");
context.table(tableIdent);

// apply default in case it is not specified in the genericProperties,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

package io.crate.analyze;

import com.google.common.base.Joiner;
import io.crate.exceptions.SchemaUnknownException;
import io.crate.exceptions.TableUnknownException;
import io.crate.metadata.ReferenceInfos;
Expand All @@ -40,7 +39,7 @@ public DropTableAnalyzedStatement(ReferenceInfos referenceInfos) {
}

public String index() {
return tableIdent.name();
return tableIdent.esName();
}

@Override
Expand All @@ -51,11 +50,11 @@ public void table(TableIdent tableIdent) {
}
if (schemaInfo.systemSchema()) {
throw new UnsupportedOperationException(
String.format("cannot delete '%s'.", Joiner.on('.').join(tableIdent.schema(), tableIdent.name())));
String.format("cannot delete '%s'.", tableIdent.fqn()));
}
TableInfo tableInfo = schemaInfo.getTableInfo(tableIdent.name());
if (tableInfo == null) {
throw new TableUnknownException(tableIdent.name());
throw new TableUnknownException(tableIdent.fqn());
} else if (tableInfo.isAlias()) {
throw new UnsupportedOperationException("Table alias not allowed in DROP TABLE statement.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public static Map<ColumnIdent, Object> assignmentsToMap(List<Assignment> assignm
public static PartitionName toPartitionName(TableInfo tableInfo,
List<Assignment> partitionProperties,
Object[] parameters) {
Preconditions.checkArgument(tableInfo.isPartitioned(), "table '%s' is not partitioned", tableInfo.ident().name());
Preconditions.checkArgument(tableInfo.isPartitioned(), "table '%s' is not partitioned", tableInfo.ident().fqn());
Preconditions.checkArgument(partitionProperties.size() == tableInfo.partitionedBy().size(),
"The table \"%s\" is partitioned by %s columns but the PARTITION clause contains %s columns",
tableInfo.ident().name(),
tableInfo.ident().fqn(),
tableInfo.partitionedBy().size(),
partitionProperties.size()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void table(TableIdent tableIdent) {
}
TableInfo tableInfo = schemaInfo.getTableInfo(tableIdent.name());
if (tableInfo == null) {
throw new TableUnknownException(tableIdent.name());
throw new TableUnknownException(tableIdent.fqn());
}
this.tableInfo = tableInfo;
}
Expand Down Expand Up @@ -83,7 +83,7 @@ public void partitionIdent(String ident) {
if (!table().isPartitioned()) {
throw new IllegalArgumentException(
String.format(Locale.ENGLISH,
"Table '%s' is not partitioned", table().ident().name()));
"Table '%s' is not partitioned", table().ident().fqn()));
}
try {
this.partitionName = PartitionName.fromPartitionIdent(
Expand All @@ -93,12 +93,12 @@ public void partitionIdent(String ident) {
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(
String.format(Locale.ENGLISH, "Invalid partition ident for table '%s': '%s'",
table().ident().name(), ident), e);
table().ident().fqn(), ident), e);
}

if (!table().partitions().contains(this.partitionName)) {
throw new PartitionUnknownException(
this.table().ident().name(),
this.table().ident().fqn(),
this.partitionName.ident());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. Crate 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.
*
* However, if you have executed another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial agreement.
*/

package io.crate.exceptions;

public class InvalidSchemaNameException extends ValidationException {

public InvalidSchemaNameException(String tableName) {
super(String.format("schema name \"%s\" is invalid.", tableName));
}

@Override
public int errorCode() {
return 2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ public DropTableTask(TransportDeleteIndexTemplateAction deleteTemplateAction,
@Override
protected void doStart(List<TaskResult> upstreamResults) {
if (tableInfo.isPartitioned()) {
String templateName = PartitionName.templateName(tableInfo.ident().name());
String templateName = PartitionName.templateName(tableInfo.ident().esName());
deleteTemplateAction.execute(new DeleteIndexTemplateRequest(templateName), new ActionListener<DeleteIndexTemplateResponse>() {
@Override
public void onResponse(DeleteIndexTemplateResponse response) {
if (!response.isAcknowledged()) {
warnNotAcknowledged(String.format(Locale.ENGLISH, "dropping table '%s'", tableInfo.ident().fqn()));
}
if (!tableInfo.partitions().isEmpty()) {
deleteESIndex(tableInfo.ident().name());
deleteESIndex(tableInfo.ident().esName());
} else {
result.set(SUCCESS_RESULT);
}
Expand All @@ -81,7 +81,7 @@ public void onFailure(Throwable e) {
}
});
} else {
deleteESIndex(tableInfo.ident().name());
deleteESIndex(tableInfo.ident().esName());
}

}
Expand Down
Loading

0 comments on commit 39479a0

Please sign in to comment.