Java Code Examples for io.crate.types.StringType

The following examples show how to use io.crate.types.StringType. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Elasticsearch   Source File: StmtExecutor.java    License: Apache License 2.0 6 votes vote down vote up
public static PrimitiveType getPrimitiveTypeFromESType(DataType t) {
    if (t.getName().equals(StringType.INSTANCE.getName())) {
        return PrimitiveType.STRING;
    } else if (t.getName().equals(BooleanType.INSTANCE.getName())) {
        return PrimitiveType.BOOL;
    } else if (t.getName().equals(ByteType.INSTANCE.getName())) {
        return PrimitiveType.BYTE;
    } else if (t.getName().equals(DoubleType.INSTANCE.getName())) {
        return PrimitiveType.DOUBLE;
    } else if (t.getName().equals(FloatType.INSTANCE.getName())) {
        return PrimitiveType.FLOAT;
    } else if (t.getName().equals(IntegerType.INSTANCE.getName())) {
        return PrimitiveType.INT;
    } else if (t.getName().equals(LongType.INSTANCE.getName())) {
        return PrimitiveType.LONG;
    } else if (t.getName().equals(ShortType.INSTANCE.getName())) {
        return PrimitiveType.SHORT;
    } else if (t.getName().equals(TimestampType.INSTANCE.getName())) {
        return PrimitiveType.TIMESTAMP;
    }
    return PrimitiveType.STRING;
}
 
Example 2
Source Project: Elasticsearch   Source File: PartitionName.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * decodes an encoded ident into it's values
 */
@Nullable
public static List<BytesRef> decodeIdent(@Nullable String ident) {
    if (ident == null) {
        return ImmutableList.of();
    }
    byte[] inputBytes = BASE32.decode(ident.toUpperCase(Locale.ROOT));
    try (StreamInput in = StreamInput.wrap(inputBytes)) {
        int size = in.readVInt();
        List<BytesRef> values = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            values.add(StringType.INSTANCE.streamer().readValueFrom(in));
        }
        return values;
    } catch (IOException e) {
        throw new IllegalArgumentException(
                String.format(Locale.ENGLISH, "Invalid partition ident: %s", ident), e);
    }
}
 
Example 3
Source Project: Elasticsearch   Source File: PartitionName.java    License: Apache License 2.0 6 votes vote down vote up
@Nullable
public static String encodeIdent(Collection<? extends BytesRef> values) {
    if (values.size() == 0) {
        return null;
    }

    BytesStreamOutput streamOutput = new BytesStreamOutput(estimateSize(values));
    try {
        streamOutput.writeVInt(values.size());
        for (BytesRef value : values) {
            StringType.INSTANCE.streamer().writeValueTo(streamOutput, value);
        }
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    String identBase32 = BASE32.encodeAsString(streamOutput.bytes().toBytes()).toLowerCase(Locale.ROOT);
    // decode doesn't need padding, remove it
    int idx = identBase32.indexOf('=');
    if (idx > -1) {
        return identBase32.substring(0, idx);
    }
    return identBase32;
}
 
Example 4
@Test
public void shouldMapSimpleTypes() {
	
	String[] columns = {"string", "integer", "bool", "locale"};
	
	DataType<?>[] types = { StringType.INSTANCE, IntegerType.INSTANCE, 
						    BooleanType.INSTANCE, StringType.INSTANCE };
	
	Object[] row = new Object[]{"DOCUMENT", 1, true, CANADA};
	
	Map<String, Object> expected = new HashMap<String, Object>();
	expected.put("string", "DOCUMENT");
	expected.put("integer", 1);
	expected.put("bool", true);
	expected.put("locale", CANADA);
	
	converter = new CrateDocumentConverter(columns, types, row);
	
	CrateDocument document = converter.toDocument();
	
	assertThat(document, is(notNullValue()));
	assertThat(document.equals(expected), is(true));
}
 
Example 5
Source Project: crate   Source File: AnalyzedColumnDefinition.java    License: Apache License 2.0 6 votes vote down vote up
static void applyAndValidateAnalyzerSettings(AnalyzedColumnDefinition<Object> definition,
                                             FulltextAnalyzerResolver fulltextAnalyzerResolver) {
    if (definition.analyzer == null) {
        if (definition.indexMethod != null) {
            if (definition.indexMethod.equals("plain")) {
                definition.analyzer("keyword");
            } else {
                definition.analyzer("standard");
            }
        }
    } else {
        if (definition.analyzer instanceof Object[]) {
            throw new IllegalArgumentException("array literal not allowed for the analyzer property");
        }

        String analyzerName = StringType.INSTANCE.value(definition.analyzer);
        if (fulltextAnalyzerResolver.hasCustomAnalyzer(analyzerName)) {
            Settings settings = fulltextAnalyzerResolver.resolveFullCustomAnalyzerSettings(analyzerName);
            definition.analyzerSettings(settings);
        }
    }

    for (AnalyzedColumnDefinition<Object> child : definition.children()) {
        applyAndValidateAnalyzerSettings(child, fulltextAnalyzerResolver);
    }
}
 
Example 6
Source Project: crate   Source File: ArrayLengthQuery.java    License: Apache License 2.0 6 votes vote down vote up
private static IntUnaryOperator getNumTermsPerDocFunction(LeafReader reader, Reference ref) {
    DataType elementType = ArrayType.unnest(ref.valueType());
    switch (elementType.id()) {
        case BooleanType.ID:
        case ByteType.ID:
        case ShortType.ID:
        case IntegerType.ID:
        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
        case FloatType.ID:
        case DoubleType.ID:
        case GeoPointType.ID:
            return numValuesPerDocForSortedNumeric(reader, ref.column());

        case StringType.ID:
            return numValuesPerDocForString(reader, ref.column());

        case IpType.ID:
            return numValuesPerDocForIP(reader, ref.column());

        default:
            throw new UnsupportedOperationException("NYI: " + elementType);
    }
}
 
Example 7
Source Project: crate   Source File: InsertAnalyzerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testFromQueryWithOnDuplicateKeyValues() throws Exception {
    var insert = "insert into users (id, name) (select id, name from users) " +
                 "on conflict (id) do update set name = substr(excluded.name, 1, 1)";

    AnalyzedInsertStatement statement = e.analyze(insert);
    Assert.assertThat(statement.onDuplicateKeyAssignments().size(), is(1));

    for (Map.Entry<Reference, Symbol> entry : statement.onDuplicateKeyAssignments().entrySet()) {
        assertThat(entry.getKey(), isReference("name"));
        assertThat(entry.getValue(), isFunction(SubstrFunction.NAME));
        Function function = (Function) entry.getValue();
        assertThat(function.arguments().get(0), instanceOf(InputColumn.class));
        InputColumn inputColumn = (InputColumn) function.arguments().get(0);
        assertThat(inputColumn.index(), is(1));
        assertThat(inputColumn.valueType(), instanceOf(StringType.class));
    }
}
 
Example 8
Source Project: crate   Source File: GeneratedReferenceTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testStreaming() throws Exception {
    ReferenceIdent referenceIdent = new ReferenceIdent(t1Info.ident(), "generated_column");
    String formattedGeneratedExpression = "concat(a, 'bar')";
    GeneratedReference generatedReferenceInfo = new GeneratedReference(null,
        referenceIdent,
        RowGranularity.DOC,
        StringType.INSTANCE, ColumnPolicy.STRICT, Reference.IndexType.ANALYZED,
        formattedGeneratedExpression, false);

    generatedReferenceInfo.generatedExpression(expressions.normalize(executor.asSymbol(formattedGeneratedExpression)));
    generatedReferenceInfo.referencedReferences(ImmutableList.of(t1Info.getReference(new ColumnIdent("a"))));

    BytesStreamOutput out = new BytesStreamOutput();
    Reference.toStream(generatedReferenceInfo, out);

    StreamInput in = out.bytes().streamInput();
    GeneratedReference generatedReferenceInfo2 = Reference.fromStream(in);

    assertThat(generatedReferenceInfo2, is(generatedReferenceInfo));
}
 
Example 9
Source Project: crate   Source File: IndexReferenceTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testStreaming() throws Exception {
    RelationName relationName = new RelationName("doc", "test");
    ReferenceIdent referenceIdent = new ReferenceIdent(relationName, "string_col");
    Reference reference = new Reference(referenceIdent, RowGranularity.DOC, StringType.INSTANCE, null, null);

    ReferenceIdent indexReferenceIdent = new ReferenceIdent(relationName, "index_column");
    IndexReference indexReferenceInfo = new IndexReference(
        null,
        indexReferenceIdent,
        Reference.IndexType.ANALYZED, List.of(reference), "my_analyzer");

    BytesStreamOutput out = new BytesStreamOutput();
    Reference.toStream(indexReferenceInfo, out);

    StreamInput in = out.bytes().streamInput();
    IndexReference indexReferenceInfo2 = Reference.fromStream(in);

    assertThat(indexReferenceInfo2, is(indexReferenceInfo));
}
 
Example 10
Source Project: crate   Source File: DocIndexMetaDataTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test_resolve_string_type_with_length_from_mappings_with_text_and_keyword_types() throws Exception {
    XContentBuilder builder = XContentFactory.jsonBuilder()
        .startObject()
            .startObject(Constants.DEFAULT_MAPPING_TYPE)
                .startObject("properties")
                    .startObject("col")
                        .field("type", "keyword")
                        .field("length_limit", 10)
                        .field("index", "false")
                    .endObject()
                .endObject()
            .endObject()
        .endObject();
    var docIndexMetaData = newMeta(getIndexMetaData("test", builder), "test");

    var column = docIndexMetaData.references().get(new ColumnIdent("col"));
    assertThat(column, is(not(nullValue())));
    assertThat(column.valueType(), is(StringType.of(10)));
}
 
Example 11
Source Project: crate   Source File: SysRepositoriesServiceTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testQueryAllColumns() throws Exception {
    execute("select * from sys.repositories");
    assertThat(response.rowCount(), is(1L));
    assertThat(response.cols().length, is(3));
    assertThat(response.cols(), is(new String[]{"name", "settings", "type"}));
    assertThat(response.columnTypes(), is(new DataType[]{StringType.INSTANCE, DataTypes.UNTYPED_OBJECT, StringType.INSTANCE}));
    assertThat((String) response.rows()[0][0], is("test-repo"));

    Map<String, Object> settings = (Map<String, Object>) response.rows()[0][1];
    assertThat(settings.size(), is(3));
    assertThat((String) settings.get("location"), is(new File(TEMP_FOLDER.getRoot(), "backup").getAbsolutePath()));
    assertThat((String) settings.get("chunk_size"), is("5k"));
    assertThat((String) settings.get("compress"), is("false"));

    assertThat((String) response.rows()[0][2], is("fs"));
}
 
Example 12
Source Project: crate   Source File: HyperLogLogDistinctAggregation.java    License: Apache License 2.0 6 votes vote down vote up
static Murmur3Hash getForType(DataType<?> dataType, boolean allOn4_1) {
    switch (dataType.id()) {
        case DoubleType.ID:
        case FloatType.ID:
            return Double.INSTANCE;
        case LongType.ID:
        case IntegerType.ID:
        case ShortType.ID:
        case ByteType.ID:
        case TimestampType.ID_WITH_TZ:
            return Long.INSTANCE;
        case StringType.ID:
        case BooleanType.ID:
        case IpType.ID:
            if (allOn4_1) {
                return Bytes64.INSTANCE;
            } else {
                return new Bytes();
            }
        default:
            throw new IllegalArgumentException("data type \"" + dataType + "\" is not supported");
    }
}
 
Example 13
Source Project: Elasticsearch   Source File: SizeEstimatorFactory.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> SizeEstimator<T> create(DataType type) {
    switch (type.id()) {
        case StringType.ID:
        case IpType.ID:
            return (SizeEstimator<T>)new BytesRefSizeEstimator();
        default:
            if (type instanceof FixedWidthType) {
                return (SizeEstimator<T>) new ConstSizeEstimator(((FixedWidthType) type).fixedSize());
            }
            throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "Cannot get SizeEstimator for type %s", type));
    }
}
 
Example 14
Source Project: crate   Source File: SizeEstimatorFactory.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> SizeEstimator<T> create(DataType<?> type) {
    switch (type.id()) {
        case UndefinedType.ID:
            return (SizeEstimator<T>) new ConstSizeEstimator(UNKNOWN_DEFAULT_RAM_BYTES_USED);

        case ObjectType.ID:
        case GeoShapeType.ID:
            return (SizeEstimator<T>) new SamplingSizeEstimator<>(SAMPLE_EVERY_NTH, MapSizeEstimator.INSTANCE);

        case StringType.ID:
        case IpType.ID:
            return (SizeEstimator<T>) StringSizeEstimator.INSTANCE;

        case ArrayType.ID:
            var innerEstimator = create(((ArrayType<?>) type).innerType());
            return (SizeEstimator<T>) ArraySizeEstimator.create(innerEstimator);

        case OidVectorType.ID:
            return (SizeEstimator<T>) ArraySizeEstimator.create(create(DataTypes.INTEGER));

        case RowType.ID:
            return (SizeEstimator<T>) new RecordSizeEstimator(Lists2.map(((RowType) type).fieldTypes(), SizeEstimatorFactory::create));

        case RegprocType.ID:
            return (SizeEstimator<T>) RegprocSizeEstimator.INSTANCE;

        default:
            if (type instanceof FixedWidthType) {
                return (SizeEstimator<T>) new ConstSizeEstimator(((FixedWidthType) type).fixedSize());
            }
            throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "Cannot get SizeEstimator for type %s", type));
    }
}
 
Example 15
Source Project: crate   Source File: CreateFunctionPlan.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void executeOrFail(DependencyCarrier dependencies,
                          PlannerContext plannerContext,
                          RowConsumer consumer,
                          Row params,
                          SubQueryResults subQueryResults) throws Exception {
    Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(
        plannerContext.transactionContext(),
        plannerContext.functions(),
        x,
        params,
        subQueryResults
    );

    UserDefinedFunctionMetaData metaData = new UserDefinedFunctionMetaData(
        createFunction.schema(),
        createFunction.name(),
        createFunction.arguments(),
        createFunction.returnType(),
        StringType.INSTANCE.value(eval.apply(createFunction.language())),
        StringType.INSTANCE.value(eval.apply(createFunction.definition()))
    );
    CreateUserDefinedFunctionRequest request = new CreateUserDefinedFunctionRequest(metaData, createFunction.replace());
    OneRowActionListener<AcknowledgedResponse> listener = new OneRowActionListener<>(consumer, r -> new Row1(1L));
    dependencies.createFunctionAction().execute(request, listener);

}
 
Example 16
Source Project: crate   Source File: AnalyzedColumnDefinition.java    License: Apache License 2.0 5 votes vote down vote up
private static void addTypeOptions(Map<String, Object> mapping, AnalyzedColumnDefinition<Object> definition) {
    switch (definition.dataType.id()) {
        case TimestampType.ID_WITH_TZ:
            /*
             * We want 1000 not be be interpreted as year 1000AD but as 1970-01-01T00:00:01.000
             * so prefer date mapping format epoch_millis over strict_date_optional_time
             */
            mapping.put("format", "epoch_millis||strict_date_optional_time");
            break;
        case TimestampType.ID_WITHOUT_TZ:
            mapping.put("format", "epoch_millis||strict_date_optional_time");
            mapping.put("ignore_timezone", true);
            break;
        case GeoShapeType.ID:
            if (definition.geoProperties != null) {
                GeoSettingsApplier.applySettings(mapping, definition.geoProperties, definition.geoTree);
            }
            break;
        case StringType.ID:
            if (definition.analyzer != null) {
                mapping.put("analyzer", DataTypes.STRING.value(definition.analyzer));
            }
            var stringType = (StringType) definition.dataType;
            if (!stringType.unbound()) {
                mapping.put("length_limit", stringType.lengthLimit());
            }
            break;
        default:
            // noop
            break;
    }
}
 
Example 17
Source Project: crate   Source File: LuceneReferenceResolver.java    License: Apache License 2.0 5 votes vote down vote up
private static LuceneCollectorExpression<?> typeSpecializedExpression(final FieldTypeLookup fieldTypeLookup, final Reference ref) {
    final String fqn = ref.column().fqn();
    final MappedFieldType fieldType = fieldTypeLookup.get(fqn);
    if (fieldType == null) {
        return NO_FIELD_TYPES_IDS.contains(unnest(ref.valueType()).id()) || isIgnoredDynamicReference(ref)
            ? DocCollectorExpression.create(toSourceLookup(ref))
            : new NullValueCollectorExpression();
    }
    if (!fieldType.hasDocValues()) {
        return DocCollectorExpression.create(toSourceLookup(ref));
    }
    switch (ref.valueType().id()) {
        case ByteType.ID:
            return new ByteColumnReference(fqn);
        case ShortType.ID:
            return new ShortColumnReference(fqn);
        case IpType.ID:
            return new IpColumnReference(fqn);
        case StringType.ID:
            return new BytesRefColumnReference(fqn);
        case DoubleType.ID:
            return new DoubleColumnReference(fqn);
        case BooleanType.ID:
            return new BooleanColumnReference(fqn);
        case FloatType.ID:
            return new FloatColumnReference(fqn);
        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
            return new LongColumnReference(fqn);
        case IntegerType.ID:
            return new IntegerColumnReference(fqn);
        case GeoPointType.ID:
            return new GeoPointColumnReference(fqn);
        case ArrayType.ID:
            return DocCollectorExpression.create(toSourceLookup(ref));
        default:
            throw new UnhandledServerException("Unsupported type: " + ref.valueType().getName());
    }
}
 
Example 18
Source Project: crate   Source File: PGTypesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testPG2CrateType() {
    assertThat(PGTypes.fromOID(VarCharType.OID), instanceOf(StringType.class));
    assertThat(PGTypes.fromOID(JsonType.OID), instanceOf(ObjectType.class));
    assertThat(PGTypes.fromOID(BooleanType.OID), instanceOf(io.crate.types.BooleanType.class));
    assertThat(PGTypes.fromOID(SmallIntType.OID), instanceOf(ShortType.class));
    assertThat(PGTypes.fromOID(IntegerType.OID), instanceOf(io.crate.types.IntegerType.class));
    assertThat(PGTypes.fromOID(BigIntType.OID), instanceOf(LongType.class));
    assertThat(PGTypes.fromOID(RealType.OID), instanceOf(FloatType.class));
    assertThat(PGTypes.fromOID(DoubleType.OID), instanceOf(io.crate.types.DoubleType.class));
}
 
Example 19
Source Project: crate   Source File: InsertAnalyzerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testFromQueryWithOnDuplicateKey() throws Exception {
    var insert = "insert into users (id, name) (select id, name from users) " +
                 "on conflict (id) do update set name = 'Arthur'";

    AnalyzedInsertStatement statement = e.analyze(insert);
    Assert.assertThat(statement.onDuplicateKeyAssignments().size(), is(1));

    for (Map.Entry<Reference, Symbol> entry : statement.onDuplicateKeyAssignments().entrySet()) {
        assertThat(entry.getKey(), isReference("name"));
        assertThat(entry.getValue(), isLiteral("Arthur", StringType.INSTANCE));
    }
}
 
Example 20
Source Project: crate   Source File: SysSnapshotsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryAllColumns() {
    execute("select * from sys.snapshots");
    assertThat(response.rowCount(), is(1L));
    assertThat(response.cols(), arrayContaining("concrete_indices", "failures", "finished", "name", "repository", "started", "state", "tables", "version"));
    ArrayType<String> stringArray = new ArrayType<>(DataTypes.STRING);
    assertThat(response.columnTypes(), arrayContaining(
        stringArray,
        stringArray,
        TimestampType.INSTANCE_WITH_TZ,
        StringType.INSTANCE,
        StringType.INSTANCE,
        TimestampType.INSTANCE_WITH_TZ,
        StringType.INSTANCE,
        stringArray,
        StringType.INSTANCE
    ));
    Object[] firstRow = response.rows()[0];
    assertThat((List<Object>) firstRow[0], Matchers.contains(getFqn("test_table")));
    assertThat((List<Object>) firstRow[1], Matchers.empty());
    assertThat((Long) firstRow[2], lessThanOrEqualTo(finishedTime));
    assertThat(firstRow[3], is("test_snap_1"));
    assertThat(firstRow[4], is(REPOSITORY_NAME));
    assertThat((Long) firstRow[5], greaterThanOrEqualTo(createdTime));
    assertThat(firstRow[6], is(SnapshotState.SUCCESS.name()));
    assertThat((List<Object>) firstRow[7], Matchers.contains(getFqn("test_table")));
    assertThat(firstRow[8], is(Version.CURRENT.toString()));

}
 
Example 21
@Test
public void shouldCreateEmptyDocument() {
	
	String[] columns = {"string"};
	
	DataType<?>[] types = { StringType.INSTANCE };
	
	Object[] row = new Object[0];
	
	converter = new CrateDocumentConverter(columns, types, row);
	
	CrateDocument document = converter.toDocument();
	
	assertThat(document, is(notNullValue()));
	assertThat(document.isEmpty(), is(true));
	
	converter = new CrateDocumentConverter(columns, types, null);
	
	document = converter.toDocument();
	
	assertThat(document, is(notNullValue()));
	assertThat(document.isEmpty(), is(true));
}
 
Example 22
@Test
public void shouldMapArrayTypes() {
	
	String[] strings = {"C", "R", "A", "T", "E"};
	
	String[] columns = {"strings"};
	
	DataType<?>[] types = { new ArrayType(StringType.INSTANCE) };
	
	Object[] row = new Object[]{strings};
	
	CrateArray stringsArray = new CrateArray();
	stringsArray.addAll(asList(strings));
	Map<String, Object> expected = new HashMap<String, Object>();
	expected.put("strings", stringsArray);
	
	converter = new CrateDocumentConverter(columns, types, row);
	
	CrateDocument document = converter.toDocument();
	
	assertThat(document, is(notNullValue()));
	assertThat(document.equals(expected), is(true));
}
 
Example 23
@Test
@SuppressWarnings("unchecked")
public void shouldMapComplexType() {
	
	Map<String, Object> languageDocument = new HashMap<String, Object>();
	languageDocument.put("name", "aLanguage");
	
	Map<String, Object> emailDocument = new HashMap<String, Object>();
	emailDocument.put("email", "[email protected]");
	
	List<Map<String, Object>> languagesArray = asList(languageDocument);
	List<Map<String, Object>> emailsArray = asList(emailDocument);
	
	Map<String, Object> countryDocument = new HashMap<String, Object>();
	countryDocument.put("name", "aCountry");
	countryDocument.put("languages", languagesArray);
	
	Map<String, Object> addressDocument = new HashMap<String, Object>();
	addressDocument.put("country", countryDocument);
	addressDocument.put("city", "aCity");
	addressDocument.put("street", "aStreet");
	
	Map<String, Object> expected = new HashMap<String, Object>();
	expected.put("name", "aName");
	expected.put("address", addressDocument);
	expected.put("emails", emailsArray);
	
	String[] columns = {"name", "address", "emails"};
	
	DataType<?>[] types = { StringType.INSTANCE, ObjectType.INSTANCE, new ArrayType(ObjectType.INSTANCE) };
	
	Object[] row = new Object[]{"aName", addressDocument, emailsArray};
	
	converter = new CrateDocumentConverter(columns, types, row);
	
	CrateDocument document = converter.toDocument();
	
	assertThat(document, is(notNullValue()));
	assertThat(document.size(), is(3));
	assertThat(document.equals(expected), is(true));
}
 
Example 24
Source Project: crate   Source File: AnalyzedColumnDefinition.java    License: Apache License 2.0 4 votes vote down vote up
String typeNameForESMapping() {
    if (StringType.ID == dataType.id()) {
        return analyzer == null && !isIndex ? "keyword" : "text";
    }
    return DataTypes.esMappingNameFrom(dataType.id());
}
 
Example 25
Source Project: crate   Source File: InformationColumnsTableInfo.java    License: Apache License 2.0 4 votes vote down vote up
public static SystemTable<ColumnContext> create() {
    return SystemTable.<ColumnContext>builder(IDENT)
        .addNonNull("table_schema", STRING, r -> r.info.ident().tableIdent().schema())
        .addNonNull("table_name", STRING, r -> r.info.ident().tableIdent().name())
        .addNonNull("table_catalog", STRING, r -> r.info.ident().tableIdent().schema())
        .addNonNull("column_name", STRING, r -> r.info.column().sqlFqn())
        .addNonNull("ordinal_position", INTEGER, ColumnContext::getOrdinal)
        .addNonNull("data_type", STRING, r -> r.info.valueType().getName())
        .addNonNull("is_generated", STRING, r -> {
            if (r.info instanceof GeneratedReference) {
                return IS_GENERATED_ALWAYS;
            }
            return IS_GENERATED_NEVER;
        })
        .addNonNull("is_nullable", BOOLEAN, r -> !r.tableInfo.primaryKey().contains(r.info.column()) && r.info.isNullable())
        .add("generation_expression", STRING, r -> {
            if (r.info instanceof GeneratedReference) {
                return ((GeneratedReference) r.info).formattedGeneratedExpression();
            }
            return null;
        })
        .add("column_default", STRING, r -> {
            Symbol defaultExpression = r.info.defaultExpression();
            if (defaultExpression != null) {
                return defaultExpression.toString();
            } else {
                return null;
            }
        })
        .add("character_maximum_length", INTEGER, r -> {
            if (r.info.valueType() instanceof StringType) {
                var stringType = ((StringType) r.info.valueType());
                if (stringType.unbound()) {
                    return null;
                } else {
                    return stringType.lengthLimit();
                }
            } else {
                return null;
            }
        })
        .add("character_octet_length", INTEGER, ignored -> null)
        .add("numeric_precision", INTEGER, r -> PRECISION_BY_TYPE_ID.get(r.info.valueType().id()))
        .add("numeric_precision_radix", INTEGER, r -> {
            if (DataTypes.isNumericPrimitive(r.info.valueType())) {
                return NUMERIC_PRECISION_RADIX;
            }
            return null;
        })
        .add("numeric_scale", INTEGER, ignored -> null)
        .add("datetime_precision", INTEGER, r -> {
            if (r.info.valueType() == TIMESTAMPZ || r.info.valueType() == TIMESTAMP) {
                return DATETIME_PRECISION;
            }
            return null;
        })
        .add("interval_type", STRING, ignored -> null)
        .add("interval_precision", INTEGER, ignored -> null)
        .add("character_set_catalog", STRING, ignored -> null)
        .add("character_set_schema", STRING, ignored -> null)
        .add("character_set_name", STRING, ignored -> null)
        .add("collation_catalog", STRING, ignored -> null)
        .add("collation_schema", STRING, ignored -> null)
        .add("collation_name", STRING, ignored -> null)
        .add("domain_catalog", STRING, ignored -> null)
        .add("domain_schema", STRING, ignored -> null)
        .add("domain_name", STRING, ignored -> null)
        .add("udt_catalog", STRING, ignored -> null)
        .add("udt_schema", STRING, ignored -> null)
        .add("udt_name", STRING, ignored -> null)
        .add("check_references", STRING, ignored -> null)
        .add("check_action", INTEGER, ignored -> null)
        .setPrimaryKeys(
            new ColumnIdent("table_catalog"),
            new ColumnIdent("table_name"),
            new ColumnIdent("table_schema"),
            new ColumnIdent("column_name")
        )
        .build();
}
 
Example 26
Source Project: crate   Source File: DataTypeTesting.java    License: Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> Supplier<T> getDataGenerator(DataType<T> type) {
    Random random = RandomizedContext.current().getRandom();
    switch (type.id()) {
        case ByteType.ID:
            return () -> (T) (Byte) (byte) random.nextInt(Byte.MAX_VALUE);
        case BooleanType.ID:
            return () -> (T) (Boolean) random.nextBoolean();

        case StringType.ID:
            return () -> (T) RandomizedTest.randomAsciiLettersOfLength(random.nextInt(10));

        case IpType.ID:
            return () -> {
                if (random.nextBoolean()) {
                    return (T) randomIPv4Address(random);
                } else {
                    return (T) randomIPv6Address(random);
                }
            };

        case DoubleType.ID:
            return () -> (T) (Double) random.nextDouble();

        case FloatType.ID:
            return () -> (T) (Float) random.nextFloat();

        case ShortType.ID:
            return () -> (T) (Short) (short) random.nextInt(Short.MAX_VALUE);

        case IntegerType.ID:
            return () -> (T) (Integer) random.nextInt();

        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
            return () -> (T) (Long) random.nextLong();

        case GeoPointType.ID:
            return () -> (T) new PointImpl(
                BiasedNumbers.randomDoubleBetween(random, -180, 180),
                BiasedNumbers.randomDoubleBetween(random, -90, 90),
                JtsSpatialContext.GEO
            );

        case GeoShapeType.ID:
            return () -> {
                // Can't use immutable Collections.singletonMap; insert-analyzer mutates the map
                Map<String, Object> geoShape = new HashMap<>(2);
                geoShape.put("coordinates", Arrays.asList(10.2d, 32.2d));
                geoShape.put("type", "Point");
                return (T) geoShape;
            };

        case ObjectType.ID:
            Supplier<?> innerValueGenerator = getDataGenerator(randomType());
            return () -> {
                // Can't use immutable Collections.singletonMap; insert-analyzer mutates the map
                HashMap<String, Object> map = new HashMap<>();
                map.put("x", innerValueGenerator.get());
                return (T) map;
            };
        case IntervalType.ID:
            return () -> {
                return (T) new Period().withSeconds(RandomNumbers.randomIntBetween(random, 0, Integer.MAX_VALUE));
            };

    }

    throw new AssertionError("No data generator for type " + type.getName());
}
 
Example 27
@Test
public void shouldMapSimpleCollectionTypes() {
	
	List<String> strings = asList("C", "R", "A", "T", "E");
	List<Integer> integers = asList(1, 2);
	
	String[] columns = {"strings", "integers"};
	
	DataType<?>[] types = { new ArrayType(StringType.INSTANCE), 
							new ArrayType(IntegerType.INSTANCE) };
	
	Object[] row = new Object[]{strings, integers};
	
	CrateArray stringsArray = new CrateArray();
	stringsArray.addAll(strings);
	
	CrateArray integersArray = new CrateArray();
	integersArray.addAll(integers);
	
	Map<String, Object> expected = new HashMap<String, Object>();
	expected.put("strings", stringsArray);
	expected.put("integers", integersArray);
	
	converter = new CrateDocumentConverter(columns, types, row);
	
	CrateDocument document = converter.toDocument();
	
	assertThat(document, is(notNullValue()));
	assertThat(document.equals(expected), is(true));
}