Search in sources :

Example 1 with TiTableInfo

use of com.pingcap.tikv.meta.TiTableInfo in project plugins by qlangtech.

the class TiKVDataSourceFactory method getTableMetadata.

@Override
public List<ColumnMetaData> getTableMetadata(String table) {
    return this.openTiDB((session, c, db) -> {
        TiTableInfo table1 = c.getTable(db, table);
        int[] index = new int[1];
        if (table1 == null) {
            throw new IllegalStateException("table:" + table + " can not find relevant table in db:" + db.getName());
        }
        return table1.getColumns().stream().map((col) -> {
            // ref: com.pingcap.tikv.types.MySQLType
            ColumnMetaData cmd = new ColumnMetaData(index[0]++, col.getName(), map2JdbcType(col.getName(), col.getType()), col.isPrimaryKey(), !col.getType().isNotNull());
            cmd.setSchemaFieldType(typeMap(col.getType()));
            return cmd;
        }).collect(Collectors.toList());
    });
}
Also used : TiDAGRequest(com.pingcap.tikv.meta.TiDAGRequest) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) FormField(com.qlangtech.tis.plugin.annotation.FormField) AtomicReference(java.util.concurrent.atomic.AtomicReference) Context(com.alibaba.citrus.turbine.Context) TiSession(com.pingcap.tikv.TiSession) CollectionUtils(org.apache.commons.collections.CollectionUtils) Lists(com.pingcap.com.google.common.collect.Lists) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) FormFieldType(com.qlangtech.tis.plugin.annotation.FormFieldType) Validator(com.qlangtech.tis.plugin.annotation.Validator) TISExtension(com.qlangtech.tis.extension.TISExtension) Logger(org.slf4j.Logger) TiDBInfo(com.pingcap.tikv.meta.TiDBInfo) RangeSplitter(com.pingcap.tikv.util.RangeSplitter) com.qlangtech.tis.plugin.ds(com.qlangtech.tis.plugin.ds) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) TiConfiguration(com.pingcap.tikv.TiConfiguration) Public(com.qlangtech.tis.annotation.Public) IControlMsgHandler(com.qlangtech.tis.runtime.module.misc.IControlMsgHandler) Catalog(com.pingcap.tikv.catalog.Catalog) Maps(com.pingcap.com.google.common.collect.Maps) Types(java.sql.Types) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo)

Example 2 with TiTableInfo

use of com.pingcap.tikv.meta.TiTableInfo in project tispark by pingcap.

the class PredicateUtilsTest method expressionToIndexRangesTest.

@Test
public void expressionToIndexRangesTest() {
    TiTableInfo table = createTable();
    ColumnRef col1 = ColumnRef.create("c1", table);
    ColumnRef col4 = ColumnRef.create("c4", table);
    ColumnRef col5 = ColumnRef.create("c5", table);
    Constant c1 = Constant.create(1, IntegerType.INT);
    Constant c2 = Constant.create(2, IntegerType.INT);
    Constant c3 = Constant.create(3, IntegerType.INT);
    Constant c4 = Constant.create(4, IntegerType.INT);
    TypedKey key1 = TypedKey.toTypedKey(1, IntegerType.INT);
    TypedKey key2 = TypedKey.toTypedKey(2, IntegerType.INT);
    TypedKey key3 = TypedKey.toTypedKey(3, IntegerType.INT);
    TypedKey key4 = TypedKey.toTypedKey(4, IntegerType.INT);
    Expression predicate1 = or(or(equal(c1, col1), equal(col1, c2)), equal(col1, c1));
    Expression predicate2 = or(equal(c3, col4), equal(c4, col4));
    Expression rangePredicate = notEqual(col5, c1);
    List<IndexRange> indexRanges = PredicateUtils.expressionToIndexRanges(ImmutableList.of(predicate1, predicate2), Optional.of(rangePredicate), table, null);
    assertEquals(8, indexRanges.size());
    Key indexKey1 = CompoundKey.concat(key1, key3);
    Key indexKey2 = CompoundKey.concat(key1, key4);
    Key indexKey3 = CompoundKey.concat(key2, key3);
    Key indexKey4 = CompoundKey.concat(key2, key4);
    Range<TypedKey> baselineRange1 = Range.lessThan(key1);
    Range<TypedKey> baselineRange2 = Range.greaterThan(key1);
    Set<Key> baselineKeys = ImmutableSet.of(indexKey1, indexKey2, indexKey3, indexKey4);
    Set<Range<TypedKey>> baselineRanges = ImmutableSet.of(baselineRange1, baselineRange2);
    for (IndexRange range : indexRanges) {
        assertTrue(baselineKeys.contains(range.getAccessKey()));
        assertTrue(baselineRanges.contains(range.getRange()));
    }
}
Also used : TypedKey(com.pingcap.tikv.key.TypedKey) Expression(com.pingcap.tikv.expression.Expression) Constant(com.pingcap.tikv.expression.Constant) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) ColumnRef(com.pingcap.tikv.expression.ColumnRef) Range(com.google.common.collect.Range) Key(com.pingcap.tikv.key.Key) CompoundKey(com.pingcap.tikv.key.CompoundKey) TypedKey(com.pingcap.tikv.key.TypedKey) Test(org.junit.Test)

Example 3 with TiTableInfo

use of com.pingcap.tikv.meta.TiTableInfo in project tispark by pingcap.

the class TiKVScanAnalyzerTest method buildIndexScanKeyRangeTest.

@Test
public void buildIndexScanKeyRangeTest() {
    TiTableInfo table = createTableWithIndex(6, 5);
    TiIndexInfo index = table.getIndices().get(0);
    Expression eq1 = equal(ColumnRef.create("c1", table), Constant.create(0));
    Expression eq2 = lessEqual(ColumnRef.create("c2", table), Constant.create("wtf"));
    List<Expression> exprs = ImmutableList.of(eq1);
    ScanSpec result = TiKVScanAnalyzer.extractConditions(exprs, table, index);
    List<IndexRange> irs = expressionToIndexRanges(result.getPointPredicates(), result.getRangePredicate(), table, index);
    TiKVScanAnalyzer scanAnalyzer = new TiKVScanAnalyzer();
    Map<Long, List<Coprocessor.KeyRange>> keyRanges = scanAnalyzer.buildIndexScanKeyRange(table, index, irs, null);
    assertEquals(keyRanges.size(), 1);
    Coprocessor.KeyRange keyRange = keyRanges.get(table.getId()).get(0);
    assertEquals(ByteString.copyFrom(new byte[] { 116, -128, 0, 0, 0, 0, 0, 0, 6, 95, 105, -128, 0, 0, 0, 0, 0, 0, 5, 3, -128, 0, 0, 0, 0, 0, 0, 0 }), keyRange.getStart());
    assertEquals(ByteString.copyFrom(new byte[] { 116, -128, 0, 0, 0, 0, 0, 0, 6, 95, 105, -128, 0, 0, 0, 0, 0, 0, 5, 3, -128, 0, 0, 0, 0, 0, 0, 1 }), keyRange.getEnd());
    exprs = ImmutableList.of(eq1, eq2);
    result = TiKVScanAnalyzer.extractConditions(exprs, table, index);
    irs = expressionToIndexRanges(result.getPointPredicates(), result.getRangePredicate(), table, index);
    keyRanges = scanAnalyzer.buildIndexScanKeyRange(table, index, irs, null);
    assertEquals(keyRanges.size(), 1);
    keyRange = keyRanges.get(table.getId()).get(0);
    assertEquals(ByteString.copyFrom(new byte[] { 116, -128, 0, 0, 0, 0, 0, 0, 6, 95, 105, -128, 0, 0, 0, 0, 0, 0, 5, 3, -128, 0, 0, 0, 0, 0, 0, 0, 0 }), keyRange.getStart());
    assertEquals(ByteString.copyFrom(new byte[] { 116, -128, 0, 0, 0, 0, 0, 0, 6, 95, 105, -128, 0, 0, 0, 0, 0, 0, 5, 3, -128, 0, 0, 0, 0, 0, 0, 0, 1, 119, 116, 102, 0, 0, 0, 0, 0, -5 }), keyRange.getEnd());
}
Also used : Coprocessor(org.tikv.kvproto.Coprocessor) Expression(com.pingcap.tikv.expression.Expression) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo) Test(org.junit.Test)

Example 4 with TiTableInfo

use of com.pingcap.tikv.meta.TiTableInfo in project tispark by pingcap.

the class TiKVScanAnalyzerTest method extractConditionsWithPrimaryKeyTest.

@Test
public void extractConditionsWithPrimaryKeyTest() {
    TiTableInfo table = createTableWithPrefix();
    TiIndexInfo index = TiIndexInfo.generateFakePrimaryKeyIndex(table);
    requireNonNull(index);
    assertEquals(1, index.getIndexColumns().size());
    assertEquals("c1", index.getIndexColumns().get(0).getName());
    Expression eq1 = equal(ColumnRef.create("c1", table), Constant.create(0, IntegerType.INT));
    Expression eq2 = equal(ColumnRef.create("c2", table), Constant.create("test", StringType.VARCHAR));
    Expression le1 = lessEqual(ColumnRef.create("c3", table), Constant.create("fxxx", StringType.VARCHAR));
    // Last one should be pushed back
    Expression eq3 = equal(ColumnRef.create("c4", table), Constant.create("fxxx", StringType.VARCHAR));
    List<Expression> exprs = ImmutableList.of(eq1, eq2, le1, eq3);
    ScanSpec result = TiKVScanAnalyzer.extractConditions(exprs, table, index);
    Set<Expression> baselineSet = ImmutableSet.of(eq2, le1, eq3);
    // 3 remains since c2 condition pushed back as well
    assertEquals(baselineSet, result.getResidualPredicates());
    assertEquals(1, result.getPointPredicates().size());
    assertEquals(eq1, result.getPointPredicates().get(0));
    assertFalse(result.getRangePredicate().isPresent());
}
Also used : Expression(com.pingcap.tikv.expression.Expression) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo) Test(org.junit.Test)

Example 5 with TiTableInfo

use of com.pingcap.tikv.meta.TiTableInfo in project tispark by pingcap.

the class TableCodecV2Test method testNewRowTypes.

@Test
public void testNewRowTypes() {
    // TODO: Enable JSON and Set type encoding
    MetaUtils.TableBuilder tableBuilder = MetaUtils.TableBuilder.newBuilder().name("t").addColumn("c1", IntegerType.BIGINT, 1).addColumn("c2", IntegerType.SMALLINT, 22).addColumn("c3", RealType.DOUBLE, 3).addColumn("c4", BytesType.BLOB, 24).addColumn("c5", StringType.CHAR, 25).addColumn("c6", TimestampType.TIMESTAMP, 5).addColumn("c7", TimeType.TIME, 16).addColumn("c8", new DecimalType(6, 4), 8).addColumn("c9", IntegerType.YEAR, 12).addColumn("c10", TEST_ENUM_TYPE, 9).addColumn("c12", UninitializedType.NULL, // null
    11).addColumn("c13", UninitializedType.NULL, // null
    2).addColumn("c14", UninitializedType.NULL, // null
    100).addColumn("c15", RealType.FLOAT, 116).addColumn("c17", TEST_BIT_TYPE, 118).addColumn("c18", StringType.VAR_STRING, 119);
    TiTableInfo tbl = tableBuilder.build();
    Timestamp ts = new Timestamp(1320923471000L);
    ts.setNanos(999999000);
    TestCase testCase = TestCase.createNew(// },
    new int[] { 128, 0, 13, 0, 3, 0, 1, 3, 5, 8, 9, 12, 16, 22, 24, 25, 116, 118, 119, 2, 11, 100, 1, 0, 9, 0, 17, 0, 22, 0, 23, 0, 25, 0, 33, 0, 34, 0, 37, 0, 39, 0, 47, 0, 51, 0, 51, 0, 1, 192, 0, 0, 0, 0, 0, 0, 0, 63, 66, 15, 203, 178, 148, 138, 25, 6, 4, 139, 38, 172, 2, 207, 7, 0, 128, 226, 194, 24, 13, 0, 0, 1, 97, 98, 99, 97, 98, 192, 24, 0, 0, 0, 0, 0, 0, 48, 48, 49, 0 }, tbl, new Object[] { 1L, 1L, 2.0, "abc".getBytes(), "ab", ts, 4 * 3600 * 1000000000L, new BigDecimal("11.9900"), 1999L, "n", // "{\"a\":2}",
    null, null, null, 6.0, // "n1",
    new byte[] { 49, 48, 48 }, "" });
    testCase.test();
}
Also used : MetaUtils(com.pingcap.tikv.meta.MetaUtils) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) DecimalType(com.pingcap.tikv.types.DecimalType) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

TiTableInfo (com.pingcap.tikv.meta.TiTableInfo)24 Test (org.junit.Test)17 Expression (com.pingcap.tikv.expression.Expression)11 TiIndexInfo (com.pingcap.tikv.meta.TiIndexInfo)9 ColumnRef (com.pingcap.tikv.expression.ColumnRef)5 ImmutableList (com.google.common.collect.ImmutableList)4 Constant (com.pingcap.tikv.expression.Constant)4 TiDAGRequest (com.pingcap.tikv.meta.TiDAGRequest)4 ArrayList (java.util.ArrayList)4 TiDBInfo (com.pingcap.tikv.meta.TiDBInfo)3 List (java.util.List)3 Context (com.alibaba.citrus.turbine.Context)2 Range (com.google.common.collect.Range)2 ByteString (com.google.protobuf.ByteString)2 Lists (com.pingcap.com.google.common.collect.Lists)2 Maps (com.pingcap.com.google.common.collect.Maps)2 TiConfiguration (com.pingcap.tikv.TiConfiguration)2 TiSession (com.pingcap.tikv.TiSession)2 Catalog (com.pingcap.tikv.catalog.Catalog)2 IndexMatcher (com.pingcap.tikv.expression.visitor.IndexMatcher)2