io.crate.types.IntegerType Java Examples

The following examples show how to use io.crate.types.IntegerType. 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 File: HyperLogLogDistinctAggregation.java    From crate with 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 #2
Source File: CrateDocumentConverterTest.java    From spring-data-crate with Apache License 2.0 6 votes vote down vote up
@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 #3
Source File: GroupByMaps.java    From crate with Apache License 2.0 6 votes vote down vote up
public static <K, V> Supplier<Map<K, V>> mapForType(DataType<K> type) {
    switch (type.id()) {
        case ByteType.ID:
            return () -> (Map) new PrimitiveMapWithNulls<>(new ByteObjectHashMap<>());
        case ShortType.ID:
            return () -> (Map) new PrimitiveMapWithNulls<>(new ShortObjectHashMap<>());
        case IntegerType.ID:
            return () -> (Map) new PrimitiveMapWithNulls<>(new IntObjectHashMap<>());

        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
            return () -> (Map) new PrimitiveMapWithNulls<>(new LongObjectHashMap<>());

        default:
            return HashMap::new;
    }
}
 
Example #4
Source File: SumAggregation.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public DocValueAggregator<?> getDocValueAggregator(List<DataType<?>> argumentTypes, List<MappedFieldType> fieldTypes) {
    switch (argumentTypes.get(0).id()) {
        case ShortType.ID:
        case IntegerType.ID:
        case LongType.ID:
            return new SumLong(fieldTypes.get(0).name());

        case FloatType.ID:
        case DoubleType.ID:
            return new SumDouble(fieldTypes.get(0).name());

        default:
            return null;
    }
}
 
Example #5
Source File: AverageAggregation.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public DocValueAggregator<?> getDocValueAggregator(List<DataType<?>> argumentTypes, List<MappedFieldType> fieldTypes) {
    switch (argumentTypes.get(0).id()) {
        case ShortType.ID:
        case IntegerType.ID:
        case LongType.ID:
            return new AvgLong(fieldTypes.get(0).name());

        case FloatType.ID:
        case DoubleType.ID:
            return new AvgDouble(fieldTypes.get(0).name());

        default:
            return null;
    }
}
 
Example #6
Source File: StmtExecutor.java    From Elasticsearch with 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 #7
Source File: NegateLiterals.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public Literal visitLiteral(Literal symbol, Void context) {
    Object value = symbol.value();
    if (value == null) {
        return symbol;
    }
    DataType valueType = symbol.valueType();
    switch (valueType.id()) {
        case DoubleType.ID:
            return Literal.of(valueType, (Double) value * -1);
        case FloatType.ID:
            return Literal.of(valueType, (Double) value * -1);
        case ShortType.ID:
            return Literal.of(valueType, (Short) value * -1);
        case IntegerType.ID:
            return Literal.of(valueType, (Integer) value * -1);
        case LongType.ID:
            return Literal.of(valueType, (Long) value * -1);
        default:
            throw new UnsupportedOperationException(Symbols.format(
                "Cannot negate %s. You may need to add explicit type casts", symbol));
    }
}
 
Example #8
Source File: ArrayLengthQuery.java    From crate with 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 #9
Source File: NullSentinelValues.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * @return a sentinel value that is used to indicate a `null` value.
 *         The returned value here is **not** necessarily compatible with the value type of the given `type`.
 *         This can be used for in places where {@link org.apache.lucene.search.ScoreDoc} is used,
 *         which for example uses LONG also for columns of type INTEGER.
 */
public static Object nullSentinelForScoreDoc(DataType<?> type, boolean reverseFlag, Boolean nullFirst) {
    boolean min = reverseFlag ^ (nullFirst != null ? nullFirst : reverseFlag);
    switch (type.id()) {
        case ByteType.ID:
        case ShortType.ID:
        case IntegerType.ID:
        case BooleanType.ID:
        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
            return min ? Long.MIN_VALUE : Long.MAX_VALUE;

        case FloatType.ID:
            return min ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;

        case DoubleType.ID:
            return min ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;

        default:
            return null;
    }
}
 
Example #10
Source File: ArithmeticOperatorsFactory.java    From crate with Apache License 2.0 5 votes vote down vote up
static BiFunction getSubtractFunction(DataType<?> fstArgDataType, DataType<?> sndArgDataType) {
    switch (fstArgDataType.id()) {
        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
            if (IntervalType.ID == sndArgDataType.id()) {
                var signature = IntervalTimestampArithmeticScalar.signatureFor(
                    fstArgDataType,
                    ArithmeticFunctions.Names.SUBTRACT
                );
                return new IntervalTimestampArithmeticScalar(
                    "-",
                    signature,
                    signature
                );
            }
            return SUB_LONG_FUNCTION;
        case DoubleType.ID:
            return SUB_DOUBLE_FUNCTION;
        case FloatType.ID:
            return SUB_FLOAT_FUNCTION;
        case ByteType.ID:
        case ShortType.ID:
        case IntegerType.ID:
            return SUB_INTEGER_FUNCTION;
        default:
            throw new UnsupportedOperationException(
                "Cannot create subtract function for data type " + fstArgDataType.getName());
    }
}
 
Example #11
Source File: LuceneReferenceResolver.java    From crate with 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 #12
Source File: NullSentinelValues.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Similar to {@link #nullSentinelForScoreDoc(DataType, boolean, Boolean)}
 * but returns a value that is compatible with the value type of the given DataType.
 */
@Nullable
public static Object nullSentinel(DataType<?> dataType, NullValueOrder nullValueOrder, boolean reversed) {
    boolean min = nullValueOrder == NullValueOrder.FIRST ^ reversed;
    switch (dataType.id()) {
        case ByteType.ID:
            return min ? Byte.MIN_VALUE : Byte.MAX_VALUE;

        case ShortType.ID:
            return min ? Short.MIN_VALUE : Short.MAX_VALUE;

        case IntegerType.ID:
            return min ? Integer.MIN_VALUE : Integer.MAX_VALUE;

        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
            return min ? Long.MIN_VALUE : Long.MAX_VALUE;

        case FloatType.ID:
            return min ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;

        case DoubleType.ID:
            return min ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;

        default:
            return null;
    }
}
 
Example #13
Source File: ArithmeticOperatorsFactory.java    From crate with Apache License 2.0 5 votes vote down vote up
static BiFunction getAddFunction(DataType<?> fstArgDataType, DataType<?> sndArgDataType) {
    switch (fstArgDataType.id()) {
        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
            if (IntervalType.ID == sndArgDataType.id()) {
                var signature = IntervalTimestampArithmeticScalar.signatureFor(
                    fstArgDataType,
                    ArithmeticFunctions.Names.ADD
                );
                return new IntervalTimestampArithmeticScalar(
                    "+",
                    signature,
                    signature
                );
            }
            return ADD_LONG_FUNCTION;
        case DoubleType.ID:
            return ADD_DOUBLE_FUNCTION;
        case FloatType.ID:
            return ADD_FLOAT_FUNCTION;
        case ByteType.ID:
        case ShortType.ID:
        case IntegerType.ID:
            return ADD_INTEGER_FUNCTION;
        default:
            throw new UnsupportedOperationException(
                "Cannot create add function for data type " + fstArgDataType.getName());
    }
}
 
Example #14
Source File: SettingsAppliers.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected Object validate(Object value) {
    Integer convertedValue = IntegerType.INSTANCE.value(value);
    IntSetting setting = (IntSetting) this.setting;
    if (convertedValue < setting.minValue() || convertedValue > setting.maxValue()) {
        throw invalidException();
    }
    return convertedValue;
}
 
Example #15
Source File: RootTaskTest.java    From crate with Apache License 2.0 4 votes vote down vote up
@Test
public void testFailureClosesAllSubContexts() throws Exception {
    String localNodeId = "localNodeId";
    RoutedCollectPhase collectPhase = Mockito.mock(RoutedCollectPhase.class);
    Routing routing = Mockito.mock(Routing.class);
    when(routing.containsShards(localNodeId)).thenReturn(false);
    when(collectPhase.routing()).thenReturn(routing);
    when(collectPhase.maxRowGranularity()).thenReturn(RowGranularity.DOC);

    RootTask.Builder builder =
        new RootTask.Builder(logger, UUID.randomUUID(), coordinatorNode, Collections.emptySet(), mock(JobsLogs.class));

    CollectTask collectChildTask = new CollectTask(
        collectPhase,
        CoordinatorTxnCtx.systemTransactionContext(),
        mock(MapSideDataCollectOperation.class),
        RamAccounting.NO_ACCOUNTING,
        ramAccounting -> new OnHeapMemoryManager(ramAccounting::addBytes),
        new TestingRowConsumer(),
        mock(SharedShardContexts.class),
        Version.CURRENT,
        4096
    );
    TestingRowConsumer batchConsumer = new TestingRowConsumer();

    PageBucketReceiver pageBucketReceiver = new CumulativePageBucketReceiver(
        "n1",
        2,
        MoreExecutors.directExecutor(),
        new Streamer[]{IntegerType.INSTANCE.streamer()},
        batchConsumer,
        PassThroughPagingIterator.oneShot(),
        1);
    DistResultRXTask distResultRXTask = spy(new DistResultRXTask(
        2,
        "dummy",
        pageBucketReceiver,
        RamAccounting.NO_ACCOUNTING,
        1));

    builder.addTask(collectChildTask);
    builder.addTask(distResultRXTask);
    RootTask rootTask = builder.build();

    Exception failure = new Exception("failure!");
    collectChildTask.kill(failure);
    // other contexts must be killed with same failure
    verify(distResultRXTask, times(1)).kill(failure);

    final Field tasksByPhaseId = RootTask.class.getDeclaredField("tasksByPhaseId");
    tasksByPhaseId.setAccessible(true);
    int size = ((ConcurrentMap<Integer, Task>) tasksByPhaseId.get(rootTask)).size();

    assertThat(size, is(0));
}
 
Example #16
Source File: DataTypeTesting.java    From crate with 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 #17
Source File: CrateDocumentConverterTest.java    From spring-data-crate with Apache License 2.0 3 votes vote down vote up
@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));
}