Search in sources :

Example 71 with DocTableInfo

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

the class CopyAnalyzer method convertCopyTo.

CopyToAnalyzedStatement convertCopyTo(CopyTo node, Analysis analysis) {
    if (!node.directoryUri()) {
        throw new UnsupportedOperationException("Using COPY TO without specifying a DIRECTORY is not supported");
    }
    TableInfo tableInfo = schemas.getTableInfo(TableIdent.of(node.table(), analysis.sessionContext().defaultSchema()));
    if (!(tableInfo instanceof DocTableInfo)) {
        throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "Cannot COPY %s TO. COPY TO only supports user tables", tableInfo.ident()));
    }
    Operation.blockedRaiseException(tableInfo, Operation.READ);
    DocTableRelation tableRelation = new DocTableRelation((DocTableInfo) tableInfo);
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.MUTATE, null, tableRelation);
    ExpressionAnalyzer expressionAnalyzer = createExpressionAnalyzer(analysis, tableRelation);
    ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
    Settings settings = GenericPropertiesConverter.settingsFromProperties(node.genericProperties(), analysis.parameterContext(), SETTINGS_APPLIERS).build();
    WriterProjection.CompressionType compressionType = settingAsEnum(WriterProjection.CompressionType.class, settings.get(COMPRESSION_SETTINGS.name()));
    WriterProjection.OutputFormat outputFormat = settingAsEnum(WriterProjection.OutputFormat.class, settings.get(OUTPUT_FORMAT_SETTINGS.name()));
    Symbol uri = expressionAnalyzer.convert(node.targetUri(), expressionAnalysisContext);
    uri = normalizer.normalize(uri, analysis.transactionContext());
    List<String> partitions = resolvePartitions(node, analysis, tableRelation);
    List<Symbol> outputs = new ArrayList<>();
    QuerySpec querySpec = new QuerySpec();
    WhereClause whereClause = createWhereClause(node.whereClause(), tableRelation, partitions, normalizer, expressionAnalyzer, expressionAnalysisContext, analysis.transactionContext());
    querySpec.where(whereClause);
    Map<ColumnIdent, Symbol> overwrites = null;
    boolean columnsDefined = false;
    List<String> outputNames = null;
    if (!node.columns().isEmpty()) {
        outputNames = new ArrayList<>(node.columns().size());
        for (Expression expression : node.columns()) {
            Symbol symbol = expressionAnalyzer.convert(expression, expressionAnalysisContext);
            symbol = normalizer.normalize(symbol, analysis.transactionContext());
            outputNames.add(SymbolPrinter.INSTANCE.printSimple(symbol));
            outputs.add(DocReferenceConverter.convertIf(symbol));
        }
        columnsDefined = true;
    } else {
        Reference sourceRef;
        if (tableRelation.tableInfo().isPartitioned() && partitions.isEmpty()) {
            // table is partitioned, insert partitioned columns into the output
            overwrites = new HashMap<>();
            for (Reference reference : tableRelation.tableInfo().partitionedByColumns()) {
                if (!(reference instanceof GeneratedReference)) {
                    overwrites.put(reference.ident().columnIdent(), reference);
                }
            }
            if (overwrites.size() > 0) {
                sourceRef = tableRelation.tableInfo().getReference(DocSysColumns.DOC);
            } else {
                sourceRef = tableRelation.tableInfo().getReference(DocSysColumns.RAW);
            }
        } else {
            sourceRef = tableRelation.tableInfo().getReference(DocSysColumns.RAW);
        }
        outputs = ImmutableList.<Symbol>of(sourceRef);
    }
    querySpec.outputs(outputs);
    if (!columnsDefined && outputFormat == WriterProjection.OutputFormat.JSON_ARRAY) {
        throw new UnsupportedFeatureException("Output format not supported without specifying columns.");
    }
    QueriedDocTable subRelation = new QueriedDocTable(tableRelation, querySpec);
    return new CopyToAnalyzedStatement(subRelation, settings, uri, compressionType, outputFormat, outputNames, columnsDefined, overwrites);
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.analyze.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) Settings(org.elasticsearch.common.settings.Settings) UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) WriterProjection(io.crate.planner.projection.WriterProjection) QueriedDocTable(io.crate.analyze.relations.QueriedDocTable) DocTableRelation(io.crate.analyze.relations.DocTableRelation)

Example 72 with DocTableInfo

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

the class InsertFromValuesAnalyzer method analyzeValues.

private void analyzeValues(DocTableRelation tableRelation, ValueNormalizer valueNormalizer, EvaluatingNormalizer normalizer, ExpressionAnalyzer expressionAnalyzer, ExpressionAnalysisContext expressionAnalysisContext, TransactionContext transactionContext, ValuesResolver valuesResolver, ExpressionAnalyzer valuesAwareExpressionAnalyzer, ValuesList node, List<Assignment> assignments, InsertFromValuesAnalyzedStatement statement, ParameterContext parameterContext, ReferenceToLiteralConverter.Context referenceToLiteralContext) {
    validateValuesSize(node.values(), statement, tableRelation);
    try {
        DocTableInfo tableInfo = statement.tableInfo();
        int numPks = tableInfo.primaryKey().size();
        Function<List<BytesRef>, String> idFunction = Id.compileWithNullValidation(tableInfo.primaryKey(), tableInfo.clusteredBy());
        if (parameterContext.numBulkParams() > 0) {
            for (int i = 0; i < parameterContext.numBulkParams(); i++) {
                parameterContext.setBulkIdx(i);
                addValues(tableRelation, valueNormalizer, normalizer, expressionAnalyzer, expressionAnalysisContext, transactionContext, valuesResolver, valuesAwareExpressionAnalyzer, node, assignments, statement, referenceToLiteralContext, numPks, idFunction, i);
            }
        } else {
            addValues(tableRelation, valueNormalizer, normalizer, expressionAnalyzer, expressionAnalysisContext, transactionContext, valuesResolver, valuesAwareExpressionAnalyzer, node, assignments, statement, referenceToLiteralContext, numPks, idFunction, -1);
        }
    } catch (IOException e) {
        // can't throw IOException directly because of visitor interface
        throw new RuntimeException(e);
    }
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) IOException(java.io.IOException)

Example 73 with DocTableInfo

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

the class AlterTableAddColumnAnalyzer method analyze.

public AddColumnAnalyzedStatement analyze(AlterTableAddColumn node, Analysis analysis) {
    TableIdent tableIdent = TableIdent.of(node.table(), analysis.sessionContext().defaultSchema());
    DocTableInfo tableInfo = schemas.getWritableTable(tableIdent);
    if (!node.table().partitionProperties().isEmpty()) {
        throw new UnsupportedOperationException("Adding a column to a single partition is not supported");
    }
    Operation.blockedRaiseException(tableInfo, Operation.ALTER);
    AnalyzedTableElements tableElements = TableElementsAnalyzer.analyze(node.tableElement(), analysis.parameterContext().parameters(), fulltextAnalyzerResolver, tableInfo);
    for (AnalyzedColumnDefinition column : tableElements.columns()) {
        ensureColumnLeafsAreNew(column, tableInfo);
    }
    addExistingPrimaryKeys(tableInfo, tableElements);
    ensureNoIndexDefinitions(tableElements.columns());
    tableElements.finalizeAndValidate(tableInfo.ident(), tableInfo.columns(), functions, analysis.parameterContext(), analysis.sessionContext());
    int numCurrentPks = tableInfo.primaryKey().size();
    if (tableInfo.primaryKey().contains(DocSysColumns.ID)) {
        numCurrentPks -= 1;
    }
    boolean hasNewPrimaryKeys = tableElements.primaryKeys().size() > numCurrentPks;
    boolean hasGeneratedColumns = tableElements.hasGeneratedColumns();
    return new AddColumnAnalyzedStatement(tableInfo, tableElements, hasNewPrimaryKeys, hasGeneratedColumns);
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo)

Example 74 with DocTableInfo

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

the class ShardReferenceResolver method addPartitions.

private void addPartitions(Index index, Schemas schemas, ImmutableMap.Builder<ReferenceIdent, ReferenceImplementation> builder) {
    PartitionName partitionName;
    try {
        partitionName = PartitionName.fromIndexOrTemplate(index.name());
    } catch (IllegalArgumentException e) {
        throw new UnhandledServerException(String.format(Locale.ENGLISH, "Unable to load PARTITIONED BY columns from partition %s", index.name()), e);
    }
    TableIdent tableIdent = partitionName.tableIdent();
    try {
        DocTableInfo info = (DocTableInfo) schemas.getTableInfo(tableIdent);
        if (!schemas.isOrphanedAlias(info)) {
            assert info.isPartitioned() : "table must be partitioned";
            int i = 0;
            int numPartitionedColumns = info.partitionedByColumns().size();
            assert partitionName.values().size() == numPartitionedColumns : "invalid number of partitioned columns";
            for (Reference partitionedInfo : info.partitionedByColumns()) {
                builder.put(partitionedInfo.ident(), new PartitionedColumnExpression(partitionedInfo, partitionName.values().get(i)));
                i++;
            }
        } else {
            LOGGER.error("Orphaned partition '{}' with missing table '{}' found", index, tableIdent.fqn());
        }
    } catch (ResourceUnknownException e) {
        LOGGER.error("Orphaned partition '{}' with missing table '{}' found", index, tableIdent.fqn());
    }
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) PartitionedColumnExpression(io.crate.operation.reference.partitioned.PartitionedColumnExpression) UnhandledServerException(io.crate.exceptions.UnhandledServerException) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException)

Example 75 with DocTableInfo

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

the class Schemas method getWritableTable.

public DocTableInfo getWritableTable(TableIdent tableIdent) {
    TableInfo tableInfo = getTableInfo(tableIdent);
    if (!(tableInfo instanceof DocTableInfo)) {
        throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "The table %s is read-only. Write, Drop or Alter operations are not supported", tableInfo.ident()));
    }
    DocTableInfo docTableInfo = (DocTableInfo) tableInfo;
    if (docTableInfo.isAlias() && !docTableInfo.isPartitioned()) {
        throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "%s is an alias. Write, Drop or Alter operations are not supported", tableInfo.ident()));
    }
    return docTableInfo;
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo)

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