Search in sources :

Example 6 with DocTableInfo

use of io.crate.metadata.doc.DocTableInfo in project crate by crate.

the class RestoreSnapshotAnalyzer method analyze.

public RestoreSnapshotAnalyzedStatement analyze(RestoreSnapshot node, Analysis analysis) {
    List<String> nameParts = node.name().getParts();
    Preconditions.checkArgument(nameParts.size() == 2, "Snapshot name not supported, only <repository>.<snapshot> works.");
    String repositoryName = nameParts.get(0);
    repositoryService.failIfRepositoryDoesNotExist(repositoryName);
    // validate and extract settings
    Settings settings = GenericPropertiesConverter.settingsFromProperties(node.properties(), analysis.parameterContext(), SETTINGS).build();
    if (node.tableList().isPresent()) {
        List<Table> tableList = node.tableList().get();
        Set<RestoreSnapshotAnalyzedStatement.RestoreTableInfo> restoreTables = new HashSet<>(tableList.size());
        for (Table table : tableList) {
            TableIdent tableIdent = TableIdent.of(table, analysis.sessionContext().defaultSchema());
            boolean tableExists = schemas.tableExists(tableIdent);
            if (tableExists) {
                if (table.partitionProperties().isEmpty()) {
                    throw new TableAlreadyExistsException(tableIdent);
                }
                TableInfo tableInfo = schemas.getTableInfo(tableIdent);
                if (!(tableInfo instanceof DocTableInfo)) {
                    throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Cannot restore snapshot of tables in schema '%s'", tableInfo.ident().schema()));
                }
                DocTableInfo docTableInfo = ((DocTableInfo) tableInfo);
                PartitionName partitionName = PartitionPropertiesAnalyzer.toPartitionName(tableIdent, docTableInfo, table.partitionProperties(), analysis.parameterContext().parameters());
                if (docTableInfo.partitions().contains(partitionName)) {
                    throw new PartitionAlreadyExistsException(partitionName);
                }
                restoreTables.add(new RestoreSnapshotAnalyzedStatement.RestoreTableInfo(tableIdent, partitionName));
            } else {
                if (table.partitionProperties().isEmpty()) {
                    restoreTables.add(new RestoreSnapshotAnalyzedStatement.RestoreTableInfo(tableIdent, null));
                } else {
                    PartitionName partitionName = PartitionPropertiesAnalyzer.toPartitionName(tableIdent, null, table.partitionProperties(), analysis.parameterContext().parameters());
                    restoreTables.add(new RestoreSnapshotAnalyzedStatement.RestoreTableInfo(tableIdent, partitionName));
                }
            }
        }
        return RestoreSnapshotAnalyzedStatement.forTables(nameParts.get(1), repositoryName, settings, ImmutableList.copyOf(restoreTables));
    } else {
        return RestoreSnapshotAnalyzedStatement.all(nameParts.get(1), repositoryName, settings);
    }
}
Also used : TableAlreadyExistsException(io.crate.exceptions.TableAlreadyExistsException) DocTableInfo(io.crate.metadata.doc.DocTableInfo) Table(io.crate.sql.tree.Table) TableIdent(io.crate.metadata.TableIdent) PartitionName(io.crate.metadata.PartitionName) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) Settings(org.elasticsearch.common.settings.Settings) PartitionAlreadyExistsException(io.crate.exceptions.PartitionAlreadyExistsException) HashSet(java.util.HashSet)

Example 7 with DocTableInfo

use of io.crate.metadata.doc.DocTableInfo in project crate by crate.

the class TableAnalyzer method filteredIndices.

static Collection<String> filteredIndices(ParameterContext parameterContext, List<Assignment> partitionProperties, DocTableInfo tableInfo) {
    if (partitionProperties.isEmpty()) {
        return Arrays.asList(tableInfo.concreteIndices());
    } else {
        DocTableInfo docTableInfo = tableInfo;
        PartitionName partitionName = PartitionPropertiesAnalyzer.toPartitionName(docTableInfo, partitionProperties, parameterContext.parameters());
        if (!docTableInfo.partitions().contains(partitionName)) {
            throw new PartitionUnknownException(tableInfo.ident().fqn(), partitionName.ident());
        }
        return Collections.singletonList(partitionName.asIndexName());
    }
}
Also used : PartitionName(io.crate.metadata.PartitionName) DocTableInfo(io.crate.metadata.doc.DocTableInfo) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException)

Example 8 with DocTableInfo

use of io.crate.metadata.doc.DocTableInfo in project crate by crate.

the class RelationAnalyzer method visitTable.

@Override
protected AnalyzedRelation visitTable(Table node, StatementAnalysisContext context) {
    TableInfo tableInfo = schemas.getTableInfo(TableIdent.of(node, context.sessionContext().defaultSchema()));
    Operation.blockedRaiseException(tableInfo, context.currentOperation());
    AnalyzedRelation tableRelation;
    // Dispatching of doc relations is based on the returned class of the schema information.
    if (tableInfo instanceof DocTableInfo) {
        tableRelation = new DocTableRelation((DocTableInfo) tableInfo);
    } else {
        tableRelation = new TableRelation(tableInfo);
    }
    context.currentRelationContext().addSourceRelation(tableInfo.ident().schema(), tableInfo.ident().name(), tableRelation);
    return tableRelation;
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) SysClusterTableInfo(io.crate.metadata.sys.SysClusterTableInfo)

Example 9 with DocTableInfo

use of io.crate.metadata.doc.DocTableInfo in project crate by crate.

the class AlterTableAnalyzer method analyze.

public AlterTableAnalyzedStatement analyze(AlterTable node, Row parameters, String defaultSchema) {
    Table table = node.table();
    DocTableInfo tableInfo = schemas.getWritableTable(TableIdent.of(table, defaultSchema));
    PartitionName partitionName = getPartitionName(table.partitionProperties(), tableInfo, parameters);
    TableParameterInfo tableParameterInfo = getTableParameterInfo(table, tableInfo, partitionName);
    TableParameter tableParameter = getTableParameter(node, parameters, tableParameterInfo);
    maybeRaiseBlockedException(tableInfo, tableParameter.settings());
    return new AlterTableAnalyzedStatement(tableInfo, partitionName, tableParameter, table.excludePartitions());
}
Also used : PartitionName(io.crate.metadata.PartitionName) DocTableInfo(io.crate.metadata.doc.DocTableInfo) AlterTable(io.crate.sql.tree.AlterTable) Table(io.crate.sql.tree.Table)

Example 10 with DocTableInfo

use of io.crate.metadata.doc.DocTableInfo in project crate by crate.

the class LuceneQueryBuilderTest method prepare.

@Before
public void prepare() throws Exception {
    DocTableInfo users = TestingTableInfo.builder(new TableIdent(null, "users"), null).add("name", DataTypes.STRING).add("x", DataTypes.INTEGER).add("d", DataTypes.DOUBLE).add("d_array", new ArrayType(DataTypes.DOUBLE)).add("y_array", new ArrayType(DataTypes.LONG)).add("shape", DataTypes.GEO_SHAPE).add("point", DataTypes.GEO_POINT).build();
    TableRelation usersTr = new TableRelation(users);
    sources = ImmutableMap.of(new QualifiedName("users"), usersTr);
    expressions = new SqlExpressions(sources, usersTr);
    builder = new LuceneQueryBuilder(expressions.getInstance(Functions.class));
    indexCache = mock(IndexCache.class, Answers.RETURNS_MOCKS.get());
    Path tempDir = createTempDir();
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put("path.home", tempDir).build();
    Index index = new Index(users.ident().indexName());
    when(indexCache.indexSettings()).thenReturn(indexSettings);
    AnalysisService analysisService = createAnalysisService(indexSettings, index);
    mapperService = createMapperService(index, indexSettings, analysisService);
    // @formatter:off
    XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("default").startObject("properties").startObject("name").field("type", "string").endObject().startObject("x").field("type", "integer").endObject().startObject("d").field("type", "double").endObject().startObject("point").field("type", "geo_point").endObject().startObject("shape").field("type", "geo_shape").endObject().startObject("d_array").field("type", "array").startObject("inner").field("type", "double").endObject().endObject().startObject("y_array").field("type", "array").startObject("inner").field("type", "integer").endObject().endObject().endObject().endObject().endObject();
    // @formatter:on
    mapperService.merge("default", new CompressedXContent(xContentBuilder.bytes()), MapperService.MergeReason.MAPPING_UPDATE, true);
    indexFieldDataService = mock(IndexFieldDataService.class);
    IndexFieldData geoFieldData = mock(IndexGeoPointFieldData.class);
    when(geoFieldData.getFieldNames()).thenReturn(new MappedFieldType.Names("point"));
    when(indexFieldDataService.getForField(mapperService.smartNameFieldType("point"))).thenReturn(geoFieldData);
}
Also used : Path(java.nio.file.Path) DocTableInfo(io.crate.metadata.doc.DocTableInfo) QualifiedName(io.crate.sql.tree.QualifiedName) TableIdent(io.crate.metadata.TableIdent) Index(org.elasticsearch.index.Index) TableRelation(io.crate.analyze.relations.TableRelation) ArrayType(io.crate.types.ArrayType) IndexFieldDataService(org.elasticsearch.index.fielddata.IndexFieldDataService) IndexCache(org.elasticsearch.index.cache.IndexCache) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexFieldData(org.elasticsearch.index.fielddata.IndexFieldData) IndicesAnalysisService(org.elasticsearch.indices.analysis.IndicesAnalysisService) AnalysisService(org.elasticsearch.index.analysis.AnalysisService) SqlExpressions(io.crate.testing.SqlExpressions) Settings(org.elasticsearch.common.settings.Settings) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Before(org.junit.Before)

Aggregations

DocTableInfo (io.crate.metadata.doc.DocTableInfo)127 Test (org.junit.Test)56 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)40 Symbol (io.crate.expression.symbol.Symbol)27 Reference (io.crate.metadata.Reference)27 SQLExecutor (io.crate.testing.SQLExecutor)25 RelationName (io.crate.metadata.RelationName)24 DocTableRelation (io.crate.analyze.relations.DocTableRelation)20 ColumnIdent (io.crate.metadata.ColumnIdent)20 TableInfo (io.crate.metadata.table.TableInfo)18 Assignments (io.crate.expression.symbol.Assignments)16 Row (io.crate.data.Row)14 PlannerContext (io.crate.planner.PlannerContext)13 Before (org.junit.Before)13 RowConsumer (io.crate.data.RowConsumer)12 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)12 PartitionName (io.crate.metadata.PartitionName)12 DependencyCarrier (io.crate.planner.DependencyCarrier)12 ArrayList (java.util.ArrayList)12 Map (java.util.Map)12