use of io.crate.types.ArrayType in project crate by crate.
the class JoinPhaseTest method testNestedLoopSerialization.
@Test
public void testNestedLoopSerialization() throws Exception {
NestedLoopPhase node = new NestedLoopPhase(jobId, 1, "nestedLoop", List.of(topNProjection), mp1, mp2, 2, 3, Set.of("node1", "node2"), JoinType.FULL, joinCondition, List.of(DataTypes.LONG, DataTypes.STRING, new ArrayType<>(DataTypes.INTEGER)), 32L, 100_000, true);
BytesStreamOutput output = new BytesStreamOutput();
node.writeTo(output);
StreamInput input = output.bytes().streamInput();
NestedLoopPhase node2 = new NestedLoopPhase(input);
assertThat(node.nodeIds(), is(node2.nodeIds()));
assertThat(node.jobId(), is(node2.jobId()));
assertThat(node.joinCondition(), is(node2.joinCondition()));
assertThat(node.type(), is(node2.type()));
assertThat(node.nodeIds(), is(node2.nodeIds()));
assertThat(node.jobId(), is(node2.jobId()));
assertThat(node.name(), is(node2.name()));
assertThat(node.outputTypes(), is(node2.outputTypes()));
assertThat(node.joinType(), is(node2.joinType()));
assertThat(node.joinCondition(), is(node2.joinCondition()));
assertThat(node.estimatedRowsSizeLeft, is(32L));
assertThat(node.estimatedNumberOfRowsLeft, is(100_000L));
assertThat(node.blockNestedLoop, is(true));
}
use of io.crate.types.ArrayType in project crate by crate.
the class RelationAnalyzer method visitValues.
@Override
public AnalyzedRelation visitValues(Values values, StatementAnalysisContext context) {
var expressionAnalyzer = new ExpressionAnalyzer(context.transactionContext(), nodeCtx, context.paramTyeHints(), FieldProvider.UNSUPPORTED, new SubqueryAnalyzer(this, context));
var expressionAnalysisContext = new ExpressionAnalysisContext(context.sessionContext());
// prevent normalization of the values array, otherwise the value literals are converted to an array literal
// and a special per-value-literal casting logic won't be executed (e.g. FloatLiteral.cast())
expressionAnalysisContext.allowEagerNormalize(false);
java.util.function.Function<Expression, Symbol> expressionToSymbol = e -> expressionAnalyzer.convert(e, expressionAnalysisContext);
// There is a first pass to convert expressions from row oriented format:
// `[[1, a], [2, b]]` to columns `[[1, 2], [a, b]]`
//
// At the same time we determine the column type with the highest precedence,
// so that we don't fail with slight type miss-matches (long vs. int)
List<ValuesList> rows = values.rows();
assert rows.size() > 0 : "Parser grammar enforces at least 1 row";
ValuesList firstRow = rows.get(0);
int numColumns = firstRow.values().size();
ArrayList<List<Symbol>> columns = new ArrayList<>();
ArrayList<DataType<?>> targetTypes = new ArrayList<>(numColumns);
var parentOutputColumns = context.parentOutputColumns();
for (int c = 0; c < numColumns; c++) {
ArrayList<Symbol> columnValues = new ArrayList<>();
DataType<?> targetType;
boolean usePrecedence = true;
if (parentOutputColumns.size() > c) {
targetType = parentOutputColumns.get(c).valueType();
usePrecedence = false;
} else {
targetType = DataTypes.UNDEFINED;
}
for (int r = 0; r < rows.size(); r++) {
List<Expression> row = rows.get(r).values();
if (row.size() != numColumns) {
throw new IllegalArgumentException("VALUES lists must all be the same length. " + "Found row with " + numColumns + " items and another with " + columns.size() + " items");
}
Symbol cell = expressionToSymbol.apply(row.get(c));
columnValues.add(cell);
var cellType = cell.valueType();
if (// skip first cell, we don't have to check for self-conversion
r > 0 && !cellType.isConvertableTo(targetType, false) && targetType.id() != DataTypes.UNDEFINED.id()) {
throw new IllegalArgumentException("The types of the columns within VALUES lists must match. " + "Found `" + targetType + "` and `" + cellType + "` at position: " + c);
}
if (usePrecedence && cellType.precedes(targetType)) {
targetType = cellType;
} else if (targetType == DataTypes.UNDEFINED) {
targetType = cellType;
}
}
targetTypes.add(targetType);
columns.add(columnValues);
}
var normalizer = EvaluatingNormalizer.functionOnlyNormalizer(nodeCtx, f -> f.isDeterministic());
ArrayList<Symbol> arrays = new ArrayList<>(columns.size());
for (int c = 0; c < numColumns; c++) {
DataType<?> targetType = targetTypes.get(c);
ArrayType<?> arrayType = new ArrayType<>(targetType);
List<Symbol> columnValues = Lists2.map(columns.get(c), s -> normalizer.normalize(s.cast(targetType), context.transactionContext()));
arrays.add(new Function(ArrayFunction.SIGNATURE, columnValues, arrayType));
}
FunctionImplementation implementation = nodeCtx.functions().getQualified(ValuesFunction.SIGNATURE, Symbols.typeView(arrays), RowType.EMPTY);
Function function = new Function(implementation.signature(), arrays, RowType.EMPTY);
TableFunctionImplementation<?> tableFunc = TableFunctionFactory.from(implementation);
TableFunctionRelation relation = new TableFunctionRelation(tableFunc, function);
context.startRelation();
context.currentRelationContext().addSourceRelation(relation);
context.endRelation();
return relation;
}
use of io.crate.types.ArrayType in project crate by crate.
the class ReferenceTest method testEquals.
@Test
public void testEquals() {
RelationName relationName = new RelationName("doc", "test");
ReferenceIdent referenceIdent = new ReferenceIdent(relationName, "object_column");
DataType<?> dataType1 = new ArrayType<>(DataTypes.UNTYPED_OBJECT);
DataType<?> dataType2 = new ArrayType<>(DataTypes.UNTYPED_OBJECT);
Symbol defaultExpression1 = Literal.of(Map.of("f", 10));
Symbol defaultExpression2 = Literal.of(Map.of("f", 10));
Reference reference1 = new Reference(referenceIdent, RowGranularity.DOC, dataType1, 1, defaultExpression1);
Reference reference2 = new Reference(referenceIdent, RowGranularity.DOC, dataType2, 1, defaultExpression2);
assertThat(reference1, is(reference2));
}
use of io.crate.types.ArrayType in project crate by crate.
the class ArrayLengthQueryTest method testArrayLengthWithAllSupportedTypes.
@Test
public void testArrayLengthWithAllSupportedTypes() throws Exception {
for (DataType<?> type : DataTypeTesting.ALL_TYPES_EXCEPT_ARRAYS) {
// This is temporary as long as interval is not fully implemented
if (type.storageSupport() == null) {
continue;
}
Supplier dataGenerator = DataTypeTesting.getDataGenerator(type);
Object val1 = dataGenerator.get();
Object val2 = dataGenerator.get();
Object[] arr = { val1, val2 };
Object[] values = new Object[] { arr };
// ensure the test is operating on a fresh, empty cluster state (no tables)
resetClusterService();
try (QueryTester tester = new QueryTester.Builder(createTempDir(), THREAD_POOL, clusterService, Version.CURRENT, "create table \"t_" + type.getName() + "\" (xs array(\"" + type.getName() + "\"))").indexValues("xs", values).build()) {
System.out.println(type);
List<Object> result = tester.runQuery("xs", "array_length(xs, 1) >= 2");
assertThat(result.size(), is(1));
ArrayType arrayType = new ArrayType<>(type);
// having the result size check should be sufficient anyway
if (type.id() != ObjectType.ID) {
assertThat(arrayType.compare((List) result.get(0), Arrays.asList(arr)), is(0));
}
}
}
}
use of io.crate.types.ArrayType in project crate by crate.
the class PGTypesTest method testCrateCollection2PgType.
@Test
public void testCrateCollection2PgType() {
for (DataType type : PRIMITIVE_TYPES) {
assertThat(PGTypes.get(new ArrayType(type)), instanceOf(PGArray.class));
}
assertThat(PGTypes.get(new ArrayType(GEO_POINT)), instanceOf(PGArray.class));
assertThat(PGTypes.get(new ArrayType(GEO_SHAPE)), instanceOf(PGArray.class));
}
Aggregations