Search in sources :

Example 71 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.

the class DataTypeIT method primitiveTypeSamples.

@DataProvider
public static Object[][] primitiveTypeSamples() {
    InetAddress address;
    try {
        address = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
    } catch (UnknownHostException uhae) {
        throw new AssertionError("Could not get address from 127.0.0.1", uhae);
    }
    Object[][] samples = new Object[][] { new Object[] { DataTypes.ASCII, "ascii" }, new Object[] { DataTypes.BIGINT, Long.MAX_VALUE }, new Object[] { DataTypes.BIGINT, null, 0L }, new Object[] { DataTypes.BLOB, Bytes.fromHexString("0xCAFE") }, new Object[] { DataTypes.BOOLEAN, Boolean.TRUE }, new Object[] { DataTypes.BOOLEAN, null, false }, new Object[] { DataTypes.DECIMAL, new BigDecimal("12.3E+7") }, new Object[] { DataTypes.DOUBLE, Double.MAX_VALUE }, new Object[] { DataTypes.DOUBLE, null, 0.0 }, new Object[] { DataTypes.FLOAT, Float.MAX_VALUE }, new Object[] { DataTypes.FLOAT, null, 0.0f }, new Object[] { DataTypes.INET, address }, new Object[] { DataTypes.TINYINT, Byte.MAX_VALUE }, new Object[] { DataTypes.TINYINT, null, (byte) 0 }, new Object[] { DataTypes.SMALLINT, Short.MAX_VALUE }, new Object[] { DataTypes.SMALLINT, null, (short) 0 }, new Object[] { DataTypes.INT, Integer.MAX_VALUE }, new Object[] { DataTypes.INT, null, 0 }, new Object[] { DataTypes.DURATION, CqlDuration.from("PT30H20M") }, new Object[] { DataTypes.TEXT, "text" }, new Object[] { DataTypes.TIMESTAMP, Instant.ofEpochMilli(872835240000L) }, new Object[] { DataTypes.DATE, LocalDate.ofEpochDay(16071) }, new Object[] { DataTypes.TIME, LocalTime.ofNanoOfDay(54012123450000L) }, new Object[] { DataTypes.TIMEUUID, UUID.fromString("FE2B4360-28C6-11E2-81C1-0800200C9A66") }, new Object[] { DataTypes.UUID, UUID.fromString("067e6162-3b6f-4ae2-a171-2470b63dff00") }, new Object[] { DataTypes.VARINT, new BigInteger(Integer.toString(Integer.MAX_VALUE) + "000") } };
    Version version = CCM_RULE.getCassandraVersion();
    // Filter types if they aren't supported by cassandra version in use.
    return Arrays.stream(samples).filter(o -> {
        DataType dataType = (DataType) o[0];
        if (dataType == DataTypes.DURATION) {
            return version.compareTo(Version.parse("3.10")) >= 0;
        } else if (dataType == DataTypes.TINYINT || dataType == DataTypes.SMALLINT || dataType == DataTypes.DATE || dataType == DataTypes.TIME) {
            return version.compareTo(Version.V2_2_0) >= 0;
        }
        return true;
    }).toArray(Object[][]::new);
}
Also used : Arrays(java.util.Arrays) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CcmRule(com.datastax.oss.driver.api.testinfra.ccm.CcmRule) SettableByName(com.datastax.oss.driver.api.core.data.SettableByName) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) ByteBuffer(java.nio.ByteBuffer) InetAddress(java.net.InetAddress) BigDecimal(java.math.BigDecimal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultSetType(com.datastax.oss.driver.internal.core.type.DefaultSetType) Map(java.util.Map) LocalTime(java.time.LocalTime) Assert.fail(org.junit.Assert.fail) BigInteger(java.math.BigInteger) ClassRule(org.junit.ClassRule) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) DefaultTupleType(com.datastax.oss.driver.internal.core.type.DefaultTupleType) MapType(com.datastax.oss.driver.api.core.type.MapType) Set(java.util.Set) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) UUID(java.util.UUID) Instant(java.time.Instant) Category(org.junit.experimental.categories.Category) DefaultUserDefinedType(com.datastax.oss.driver.internal.core.type.DefaultUserDefinedType) Collectors(java.util.stream.Collectors) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) DefaultMapType(com.datastax.oss.driver.internal.core.type.DefaultMapType) DataTypes(com.datastax.oss.driver.api.core.type.DataTypes) List(java.util.List) SettableByIndex(com.datastax.oss.driver.api.core.data.SettableByIndex) LocalDate(java.time.LocalDate) TypeCodec(com.datastax.oss.driver.api.core.type.codec.TypeCodec) SetType(com.datastax.oss.driver.api.core.type.SetType) BeforeClass(org.junit.BeforeClass) TestRule(org.junit.rules.TestRule) ListType(com.datastax.oss.driver.api.core.type.ListType) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) ParallelizableTests(com.datastax.oss.driver.categories.ParallelizableTests) ArrayList(java.util.ArrayList) Version(com.datastax.oss.driver.api.core.Version) CqlSession(com.datastax.oss.driver.api.core.CqlSession) TestName(org.junit.rules.TestName) Row(com.datastax.oss.driver.api.core.cql.Row) DefaultListType(com.datastax.oss.driver.internal.core.type.DefaultListType) BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) TupleType(com.datastax.oss.driver.api.core.type.TupleType) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) DataType(com.datastax.oss.driver.api.core.type.DataType) Test(org.junit.Test) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) UnknownHostException(java.net.UnknownHostException) ProtocolConstants(com.datastax.oss.protocol.internal.ProtocolConstants) SessionRule(com.datastax.oss.driver.api.testinfra.session.SessionRule) RuleChain(org.junit.rules.RuleChain) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) Bytes(com.datastax.oss.protocol.internal.util.Bytes) Rule(org.junit.Rule) CqlDuration(com.datastax.oss.driver.api.core.data.CqlDuration) CustomType(com.datastax.oss.driver.api.core.type.CustomType) Collections(java.util.Collections) Statement(com.datastax.oss.driver.api.core.cql.Statement) UnknownHostException(java.net.UnknownHostException) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) Version(com.datastax.oss.driver.api.core.Version) BigInteger(java.math.BigInteger) DataType(com.datastax.oss.driver.api.core.type.DataType) InetAddress(java.net.InetAddress) BigDecimal(java.math.BigDecimal) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 72 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.

the class DataTypeIT method createTable.

@BeforeClass
public static void createTable() {
    // Create a table with all types being tested with.
    // This is a bit more lenient than creating a table for each sample, which would put a lot of
    // burden on C* and
    // the filesystem.
    int counter = 0;
    List<String> columnData = new ArrayList<>();
    for (Object[] sample : typeSamples()) {
        DataType dataType = (DataType) sample[0];
        if (!typeToColumnName.containsKey(dataType)) {
            int columnIndex = ++counter;
            String columnName = "column_" + columnIndex;
            typeToColumnName.put(dataType, columnName);
            columnData.add(String.format("%s %s", columnName, typeFor(dataType)));
        }
    }
    SESSION_RULE.session().execute(SimpleStatement.builder(String.format("CREATE TABLE IF NOT EXISTS %s (k int primary key, %s)", tableName, String.join(",", columnData))).setExecutionProfile(SESSION_RULE.slowProfile()).build());
}
Also used : ArrayList(java.util.ArrayList) DataType(com.datastax.oss.driver.api.core.type.DataType) BeforeClass(org.junit.BeforeClass)

Example 73 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.

the class DefaultCreateDseAggregate method asCql.

@NonNull
@Override
public String asCql() {
    StringBuilder builder = new StringBuilder();
    builder.append("CREATE ");
    if (orReplace) {
        builder.append("OR REPLACE ");
    }
    builder.append("AGGREGATE ");
    if (ifNotExists) {
        builder.append("IF NOT EXISTS ");
    }
    CqlHelper.qualify(keyspace, functionName, builder);
    builder.append(" (");
    boolean first = true;
    for (DataType param : parameters) {
        if (first) {
            first = false;
        } else {
            builder.append(',');
        }
        builder.append(param.asCql(false, true));
    }
    builder.append(')');
    if (sFunc != null) {
        builder.append(" SFUNC ");
        builder.append(sFunc.asCql(true));
    }
    if (sType != null) {
        builder.append(" STYPE ");
        builder.append(sType.asCql(false, true));
    }
    if (finalFunc != null) {
        builder.append(" FINALFUNC ");
        builder.append(finalFunc.asCql(true));
    }
    if (term != null) {
        builder.append(" INITCOND ");
        term.appendTo(builder);
    }
    // deterministic
    if (deterministic) {
        builder.append(" DETERMINISTIC");
    }
    return builder.toString();
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

DataType (com.datastax.oss.driver.api.core.type.DataType)73 NonNull (edu.umd.cs.findbugs.annotations.NonNull)21 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)19 SetType (com.datastax.oss.driver.api.core.type.SetType)10 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)10 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)10 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)9 CheckReturnValue (edu.umd.cs.findbugs.annotations.CheckReturnValue)9 ColumnMetadata (com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata)8 Map (java.util.Map)8 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)7 ClusteringOrder (com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)7 ListType (com.datastax.oss.driver.api.core.type.ListType)7 MapType (com.datastax.oss.driver.api.core.type.MapType)7 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)6 Nullable (edu.umd.cs.findbugs.annotations.Nullable)6 UUID (java.util.UUID)6 TupleType (com.datastax.oss.driver.api.core.type.TupleType)5 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)5 List (java.util.List)5