use of io.crate.metadata.table.TableInfo in project crate by crate.
the class ValueNormalizer method normalizeObjectValue.
@SuppressWarnings("unchecked")
private void normalizeObjectValue(Map<String, Object> value, Reference info) {
for (Map.Entry<String, Object> entry : value.entrySet()) {
AnalyzedColumnDefinition.validateName(entry.getKey());
ColumnIdent nestedIdent = ColumnIdent.getChild(info.ident().columnIdent(), entry.getKey());
TableInfo tableInfo = schemas.getTableInfo(info.ident().tableIdent());
Reference nestedInfo = tableInfo.getReference(nestedIdent);
if (nestedInfo == null) {
if (info.columnPolicy() == ColumnPolicy.IGNORED) {
continue;
}
DynamicReference dynamicReference = null;
if (tableInfo instanceof DocTableInfo) {
dynamicReference = ((DocTableInfo) tableInfo).getDynamic(nestedIdent, true);
}
if (dynamicReference == null) {
throw new ColumnUnknownException(nestedIdent.sqlFqn());
}
DataType type = DataTypes.guessType(entry.getValue());
if (type == null) {
throw new ColumnValidationException(info.ident().columnIdent().sqlFqn(), "Invalid value");
}
dynamicReference.valueType(type);
nestedInfo = dynamicReference;
} else {
if (entry.getValue() == null) {
continue;
}
}
if (nestedInfo.valueType() == DataTypes.OBJECT && entry.getValue() instanceof Map) {
normalizeObjectValue((Map<String, Object>) entry.getValue(), nestedInfo);
} else if (isObjectArray(nestedInfo.valueType()) && entry.getValue() instanceof Object[]) {
normalizeObjectArrayValue((Object[]) entry.getValue(), nestedInfo);
} else {
entry.setValue(normalizePrimitiveValue(entry.getValue(), nestedInfo));
}
}
}
use of io.crate.metadata.table.TableInfo in project crate by crate.
the class HandlerSideLevelCollectTest method testInformationSchemaColumns.
@Test
public void testInformationSchemaColumns() throws Exception {
InformationSchemaInfo schemaInfo = internalCluster().getInstance(InformationSchemaInfo.class);
TableInfo tableInfo = schemaInfo.getTableInfo("columns");
assert tableInfo != null;
Routing routing = tableInfo.getRouting(WhereClause.MATCH_ALL, null);
List<Symbol> toCollect = new ArrayList<>();
for (Reference ref : tableInfo.columns()) {
toCollect.add(ref);
}
RoutedCollectPhase collectNode = collectNode(routing, toCollect, RowGranularity.DOC);
Bucket result = collect(collectNode);
String expected = "id| string| NULL| false| true| 1| cluster| sys\n" + "master_node| string| NULL| false| true| 2| cluster| sys\n" + "name| string| NULL| false| true| 3| cluster| sys\n" + "settings| object| NULL| false| true| 4| cluster| sys\n";
assertThat(TestingHelpers.printedTable(result), Matchers.containsString(expected));
// second time - to check if the internal iterator resets
result = collect(collectNode);
assertThat(TestingHelpers.printedTable(result), Matchers.containsString(expected));
}
use of io.crate.metadata.table.TableInfo 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);
}
use of io.crate.metadata.table.TableInfo 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;
}
use of io.crate.metadata.table.TableInfo in project crate by crate.
the class Schemas method getTableInfo.
/**
* @param ident the table ident to get a TableInfo for
* @return an instance of TableInfo for the given ident, guaranteed to be not null
* @throws io.crate.exceptions.SchemaUnknownException if schema given in <code>ident</code>
* does not exist
* @throws io.crate.exceptions.TableUnknownException if table given in <code>ident</code> does
* not exist in the given schema
*/
public TableInfo getTableInfo(TableIdent ident) {
SchemaInfo schemaInfo = getSchemaInfo(ident);
TableInfo info;
info = schemaInfo.getTableInfo(ident.name());
if (info == null) {
throw new TableUnknownException(ident);
}
return info;
}
Aggregations