Search in sources :

Example 81 with DataType

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));
}
Also used : ArrayType(io.crate.types.ArrayType) DataType(io.crate.types.DataType) Test(org.junit.Test)

Example 82 with DataType

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));
}
Also used : ArrayType(io.crate.types.ArrayType) DataType(io.crate.types.DataType) Test(org.junit.Test)

Example 83 with DataType

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));
}
Also used : ArrayType(io.crate.types.ArrayType) DataType(io.crate.types.DataType) Test(org.junit.Test)

Example 84 with DataType

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));
}
Also used : Matchers.arrayContaining(org.hamcrest.Matchers.arrayContaining) Arrays(java.util.Arrays) NOT_FOUND(io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND) UseRandomizedSchema(io.crate.testing.UseRandomizedSchema) Random(java.util.Random) JsonXContent(org.elasticsearch.common.xcontent.json.JsonXContent) Matchers.hasItems(org.hamcrest.Matchers.hasItems) TestingHelpers(io.crate.testing.TestingHelpers) Matchers.closeTo(org.hamcrest.Matchers.closeTo) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ResultSet(java.sql.ResultSet) Locale(java.util.Locale) UNDEFINED_TABLE(io.crate.protocols.postgres.PGErrorStatus.UNDEFINED_TABLE) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Is.is(org.hamcrest.core.Is.is) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) RandomizedContext(com.carrotsearch.randomizedtesting.RandomizedContext) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Lists2(io.crate.common.collections.Lists2) RandomizedTest.$(com.carrotsearch.randomizedtesting.RandomizedTest.$) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) ESIntegTestCase(org.elasticsearch.test.ESIntegTestCase) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) DataTypes(io.crate.types.DataTypes) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) TestingHelpers.printedTable(io.crate.testing.TestingHelpers.printedTable) INTERNAL_ERROR(io.crate.protocols.postgres.PGErrorStatus.INTERNAL_ERROR) Matchers.containsString(org.hamcrest.Matchers.containsString) RandomPicks(com.carrotsearch.randomizedtesting.generators.RandomPicks) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Asserts.assertThrowsMatches(io.crate.testing.Asserts.assertThrowsMatches) BAD_REQUEST(io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST) DeprecationHandler(org.elasticsearch.common.xcontent.DeprecationHandler) DataTypeTesting(io.crate.testing.DataTypeTesting) Properties(java.util.Properties) DataType(io.crate.types.DataType) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) File(java.io.File) Point(org.locationtech.spatial4j.shape.Point) Rule(org.junit.Rule) Paths(java.nio.file.Paths) SQLErrorMatcher.isSQLError(io.crate.testing.SQLErrorMatcher.isSQLError) Statement(java.sql.Statement) SQLExceptions(io.crate.exceptions.SQLExceptions) UseJdbc(io.crate.testing.UseJdbc) TemporaryFolder(org.junit.rules.TemporaryFolder) DriverManager(java.sql.DriverManager) Random(java.util.Random) ArrayList(java.util.ArrayList) DataType(io.crate.types.DataType) Matchers.containsString(org.hamcrest.Matchers.containsString) Point(org.locationtech.spatial4j.shape.Point) Test(org.junit.Test)

Example 85 with DataType

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;
}
Also used : ParamTypeHints(io.crate.analyze.ParamTypeHints) JoinCriteria(io.crate.sql.tree.JoinCriteria) UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) RelationName(io.crate.metadata.RelationName) RowType(io.crate.types.RowType) DefaultTraversalVisitor(io.crate.sql.tree.DefaultTraversalVisitor) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) Node(io.crate.sql.tree.Node) ArrayType(io.crate.types.ArrayType) JoinType(io.crate.planner.node.dql.join.JoinType) JoinOn(io.crate.sql.tree.JoinOn) Locale(java.util.Locale) Map(java.util.Map) SelectAnalysis(io.crate.analyze.relations.select.SelectAnalysis) ValuesList(io.crate.sql.tree.ValuesList) Union(io.crate.sql.tree.Union) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) NodeContext(io.crate.metadata.NodeContext) SortItem(io.crate.sql.tree.SortItem) Collection(java.util.Collection) QuerySpecification(io.crate.sql.tree.QuerySpecification) TableSubquery(io.crate.sql.tree.TableSubquery) Set(java.util.Set) SelectAnalyzer(io.crate.analyze.relations.select.SelectAnalyzer) Intersect(io.crate.sql.tree.Intersect) WhereClauseValidator(io.crate.analyze.where.WhereClauseValidator) Iterables(io.crate.common.collections.Iterables) Lists2(io.crate.common.collections.Lists2) Relation(io.crate.sql.tree.Relation) List(java.util.List) OrderBy(io.crate.analyze.OrderBy) Symbol(io.crate.expression.symbol.Symbol) FunctionImplementation(io.crate.metadata.FunctionImplementation) DataTypes(io.crate.types.DataTypes) Singleton(org.elasticsearch.common.inject.Singleton) Values(io.crate.sql.tree.Values) Optional(java.util.Optional) Expression(io.crate.sql.tree.Expression) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) SemanticSortValidator(io.crate.analyze.validator.SemanticSortValidator) AmbiguousColumnAliasException(io.crate.exceptions.AmbiguousColumnAliasException) Tuple(io.crate.common.collections.Tuple) SubqueryAnalyzer(io.crate.analyze.expressions.SubqueryAnalyzer) FunctionCall(io.crate.sql.tree.FunctionCall) QualifiedNameReference(io.crate.sql.tree.QualifiedNameReference) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) SearchPath(io.crate.metadata.SearchPath) Operation(io.crate.metadata.table.Operation) HavingSymbolValidator(io.crate.analyze.validator.HavingSymbolValidator) AliasedRelation(io.crate.sql.tree.AliasedRelation) Inject(org.elasticsearch.common.inject.Inject) ArrayList(java.util.ArrayList) ValuesFunction(io.crate.expression.tablefunctions.ValuesFunction) Join(io.crate.sql.tree.Join) Symbols(io.crate.expression.symbol.Symbols) SqlParser(io.crate.sql.parser.SqlParser) Query(io.crate.sql.tree.Query) QualifiedName(io.crate.sql.tree.QualifiedName) TableFunction(io.crate.sql.tree.TableFunction) Nullable(javax.annotation.Nullable) RelationValidationException(io.crate.exceptions.RelationValidationException) ArrayFunction(io.crate.expression.scalar.arithmetic.ArrayFunction) OrderByWithAggregationValidator(io.crate.planner.consumer.OrderByWithAggregationValidator) ViewMetadata(io.crate.metadata.view.ViewMetadata) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) TableFunctionFactory(io.crate.expression.tablefunctions.TableFunctionFactory) Table(io.crate.sql.tree.Table) DataType(io.crate.types.DataType) Function(io.crate.expression.symbol.Function) GroupAndAggregateSemantics(io.crate.expression.symbol.GroupAndAggregateSemantics) JoinUsing(io.crate.sql.tree.JoinUsing) TableFunctionImplementation(io.crate.metadata.tablefunctions.TableFunctionImplementation) Except(io.crate.sql.tree.Except) Literal(io.crate.expression.symbol.Literal) GroupBySymbolValidator(io.crate.analyze.validator.GroupBySymbolValidator) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) Schemas(io.crate.metadata.Schemas) RelationUnknown(io.crate.exceptions.RelationUnknown) Collections(java.util.Collections) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) ArrayList(java.util.ArrayList) ArrayType(io.crate.types.ArrayType) ValuesFunction(io.crate.expression.tablefunctions.ValuesFunction) TableFunction(io.crate.sql.tree.TableFunction) ArrayFunction(io.crate.expression.scalar.arithmetic.ArrayFunction) Function(io.crate.expression.symbol.Function) DataType(io.crate.types.DataType) ValuesList(io.crate.sql.tree.ValuesList) List(java.util.List) ArrayList(java.util.ArrayList) FunctionImplementation(io.crate.metadata.FunctionImplementation) TableFunctionImplementation(io.crate.metadata.tablefunctions.TableFunctionImplementation) ValuesList(io.crate.sql.tree.ValuesList) Expression(io.crate.sql.tree.Expression) SubqueryAnalyzer(io.crate.analyze.expressions.SubqueryAnalyzer)

Aggregations

DataType (io.crate.types.DataType)95 ArrayType (io.crate.types.ArrayType)35 Test (org.junit.Test)33 ArrayList (java.util.ArrayList)17 Map (java.util.Map)17 CrateUnitTest (io.crate.test.integration.CrateUnitTest)14 List (java.util.List)12 Symbol (io.crate.expression.symbol.Symbol)11 Literal (io.crate.expression.symbol.Literal)9 ColumnIdent (io.crate.metadata.ColumnIdent)9 HashMap (java.util.HashMap)9 FunctionIdent (io.crate.metadata.FunctionIdent)8 NodeContext (io.crate.metadata.NodeContext)8 Reference (io.crate.metadata.Reference)8 Row (io.crate.data.Row)7 Symbols (io.crate.expression.symbol.Symbols)7 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)7 Locale (java.util.Locale)7 Lists2 (io.crate.common.collections.Lists2)6 FunctionInfo (io.crate.metadata.FunctionInfo)6