use of io.crate.types.DataType in project crate by crate.
the class DataTypeTest method testForValueWithTimestampArrayAsString.
@Test
public void testForValueWithTimestampArrayAsString() {
String[] strings = { "2013-09-10T21:51:43", "2013-11-10T21:51:43" };
DataType dataType = DataTypes.guessType(strings);
assertEquals(dataType, new ArrayType(DataTypes.STRING));
}
use of io.crate.types.DataType in project crate by crate.
the class DataTypeTest method testForValueWithList.
@Test
public void testForValueWithList() {
List<String> strings = Arrays.asList("foo", "bar");
DataType dataType = DataTypes.guessType(strings);
assertEquals(dataType, new ArrayType(DataTypes.STRING));
List<Integer> integers = Arrays.asList(1, 2, 3);
dataType = DataTypes.guessType(integers);
assertEquals(dataType, new ArrayType(DataTypes.INTEGER));
}
use of io.crate.types.DataType in project crate by crate.
the class DataTypeTest method testForValueWithArray.
@Test
public void testForValueWithArray() {
Boolean[] booleans = new Boolean[] { true, false };
DataType dataType = DataTypes.guessType(booleans);
assertEquals(dataType, new ArrayType(DataTypes.BOOLEAN));
}
use of io.crate.types.DataType in project crate by crate.
the class TransportSQLActionTest method test_primary_key_lookups_returns_inserted_records.
@Test
public void test_primary_key_lookups_returns_inserted_records() throws Exception {
int numKeys = randomIntBetween(1, 3);
Random random = RandomizedContext.current().getRandom();
StringBuilder createTable = new StringBuilder("CREATE TABLE tbl (");
ArrayList<DataType<?>> types = new ArrayList<>();
List<DataType<?>> typeCandidates = DataTypes.PRIMITIVE_TYPES.stream().filter(x -> x.storageSupport() != null).collect(Collectors.toList());
for (int i = 0; i < numKeys; i++) {
var type = RandomPicks.randomFrom(random, typeCandidates);
types.add(type);
createTable.append("col");
createTable.append(i);
createTable.append(' ');
createTable.append(type.getName());
createTable.append(" PRIMARY KEY");
if (i + 1 < numKeys) {
createTable.append(", ");
}
}
createTable.append(")");
execute(createTable.toString());
String insert = "INSERT INTO tbl VALUES (" + String.join(", ", Lists2.map(types, ignored -> "?")) + ")";
Object[] args = new Object[types.size()];
for (int i = 0; i < types.size(); i++) {
var type = types.get(i);
args[i] = DataTypeTesting.getDataGenerator(type).get();
}
execute(insert, args);
StringBuilder selectInlineValues = new StringBuilder("SELECT 1 FROM tbl WHERE ");
StringBuilder selectParams = new StringBuilder("SELECT 1 FROM tbl WHERE ");
for (int i = 0; i < args.length; i++) {
var arg = args[i];
selectInlineValues.append("col");
selectParams.append("col");
selectInlineValues.append(i);
selectParams.append(i);
selectInlineValues.append(" = ");
if (arg instanceof String) {
selectInlineValues.append("'");
selectInlineValues.append(arg);
selectInlineValues.append("'");
} else {
selectInlineValues.append(arg);
}
selectParams.append(" = ?");
if (i + 1 < args.length) {
selectInlineValues.append(" AND ");
selectParams.append(" AND ");
}
}
execute(selectParams.toString(), args);
assertThat(selectParams.toString() + " with values " + Arrays.toString(args) + " must return a record", response.rowCount(), is(1L));
execute(selectInlineValues.toString());
assertThat(selectInlineValues.toString() + " must return a record", response.rowCount(), is(1L));
}
use of io.crate.types.DataType 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;
}
Aggregations