Search in sources :

Example 6 with Column

use of org.teiid.metadata.Column in project teiid by teiid.

the class AccumuloQueryExecution method nextRow.

private List<?> nextRow(Map<String, byte[]> values) {
    if (!values.isEmpty()) {
        ArrayList<Object> list = new ArrayList<Object>();
        for (int i = 0; i < this.visitor.projectedColumns().size(); i++) {
            Column column = this.visitor.projectedColumns().get(i);
            String colName = SQLStringVisitor.getRecordName(column);
            byte[] value = values.get(colName);
            list.add(AccumuloDataTypeManager.deserialize(value, this.expectedColumnTypes[i]));
        }
        return list;
    }
    return null;
}
Also used : Column(org.teiid.metadata.Column) ArrayList(java.util.ArrayList)

Example 7 with Column

use of org.teiid.metadata.Column in project teiid by teiid.

the class AccumuloQueryExecution method findMatchingColumn.

private Column findMatchingColumn(Text rowCF, Text rowCQ) {
    String CF = new String(rowCF.getBytes());
    String CQ = new String(rowCQ.getBytes());
    // $NON-NLS-1$
    Column column = this.visitor.lookupColumn(CF + "/" + CQ);
    if (column == null) {
        // this means CQ is not defined; In this pattern CQ is used for value
        column = this.visitor.lookupColumn(CF);
    }
    return column;
}
Also used : Column(org.teiid.metadata.Column)

Example 8 with Column

use of org.teiid.metadata.Column in project teiid by teiid.

the class CouchbaseMetadataProcessor method addColumn.

private void addColumn(String name, String type, Object columnValue, boolean updatable, String nameInSource, Table table, MetadataFactory mf) {
    String columnName = name;
    String columnType = type;
    if (columnType == null && columnValue == null && table.getColumnByName(columnName) == null) {
        columnType = TypeFacility.RUNTIME_NAMES.STRING;
    } else if (columnType == null && columnValue == null && table.getColumnByName(columnName) != null) {
        columnType = table.getColumnByName(columnName).getDatatype().getName();
    }
    if (DataTypeManager.DefaultDataTypes.NULL.equals(columnType)) {
        // how to handle null type?
        columnType = DataTypeManager.DefaultDataTypes.STRING;
    }
    String tableNameInSource = trimWave(table.getNameInSource());
    if (table.getProperty(IS_ARRAY_TABLE, false).equals(FALSE_VALUE) && columnName.startsWith(tableNameInSource)) {
        columnName = columnName.substring(tableNameInSource.length() + 1);
    }
    if (table.getColumnByName(columnName) == null) {
        Column column = mf.addColumn(columnName, columnType, table);
        column.setUpdatable(updatable);
        if (nameInSource != null) {
            column.setNameInSource(nameInSource);
        }
    } else {
        Column column = table.getColumnByName(columnName);
        String existColumnType = column.getDatatype().getName();
        if (!existColumnType.equals(columnType) && !existColumnType.equals(OBJECT) && columnValue != null) {
            Datatype datatype = mf.getDataTypes().get(OBJECT);
            column.setDatatype(datatype, true, 0);
        }
    }
}
Also used : Column(org.teiid.metadata.Column) Datatype(org.teiid.metadata.Datatype)

Example 9 with Column

use of org.teiid.metadata.Column in project teiid by teiid.

the class CouchbaseMetadataProcessor method addTable.

/**
 * Basically, a keyspace be map to a table, keyspace name is the table name, if TranslatorProperty TypeNameList defined,
 * a keyspace may map to several tables, for example, if the TypeNameList=`default`:`type`,
 * then the {@link CouchbaseMetadataProcessor#addTable(MetadataFactory, CouchbaseConnection, namespace, namespace)}
 * will get all distinct `type` attribute referenced values from keyspace, and use all these values as table name.
 *
 * If multiple keyspaces has same typed value, for example, like TypeNameList=`default`:`type`,`default2`:`type`, both default and default2
 * has document defined {"type": "Customer"}, then the default's table name is 'Customer', default2's table name is 'default2_Customer'.
 *
 * Scan row will add columns to table or create sub-table, nested array be map to a separated table.
 *
 * @param mf - MetadataFactory
 * @param conn - CouchbaseConnection
 * @param namespace - couchbase namespace
 * @param keyspace - couchbase  keyspace
 * @throws ResourceException
 */
private void addTable(MetadataFactory mf, CouchbaseConnection conn, String namespace, String keyspace) throws ResourceException {
    String nameInSource = nameInSource(keyspace);
    Map<String, String> tableNameTypeMap = new HashMap<>();
    List<String> typeNameList = getTypeName(nameInSource);
    List<String> dataSrcTableList = new ArrayList<>();
    if (typeNameList != null && typeNameList.size() > 0) {
        String typeQuery = buildN1QLTypeQuery(typeNameList, namespace, keyspace);
        LogManager.logTrace(LogConstants.CTX_CONNECTOR, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29003, typeQuery));
        List<N1qlQueryRow> rows = conn.execute(typeQuery).allRows();
        for (N1qlQueryRow row : rows) {
            JsonObject rowJson = row.value();
            for (String typeName : typeNameList) {
                String type = trimWave(typeName);
                String value = rowJson.getString(type);
                if (value != null) {
                    dataSrcTableList.add(value);
                    tableNameTypeMap.put(value, typeName);
                    break;
                }
            }
        }
    } else {
        dataSrcTableList.add(keyspace);
    }
    for (String name : dataSrcTableList) {
        String tableName = name;
        if (mf.getSchema().getTable(name) != null && !name.equals(keyspace)) {
            // handle multiple keyspaces has same typed table name
            tableName = keyspace + UNDERSCORE + name;
        }
        Table table = mf.addTable(tableName);
        table.setNameInSource(nameInSource);
        table.setSupportsUpdate(true);
        table.setProperty(IS_ARRAY_TABLE, FALSE_VALUE);
        Column column = mf.addColumn(DOCUMENTID, STRING, table);
        column.setUpdatable(true);
        // $NON-NLS-1$
        mf.addPrimaryKey("PK0", Arrays.asList(DOCUMENTID), table);
        if (!name.equals(keyspace)) {
            String namedTypePair = buildNamedTypePair(tableNameTypeMap.get(name), name);
            table.setProperty(NAMED_TYPE_PAIR, namedTypePair);
        }
        // scan row
        boolean hasTypeIdentifier = true;
        if (dataSrcTableList.size() == 1 && dataSrcTableList.get(0).equals(keyspace)) {
            hasTypeIdentifier = false;
        }
        if (this.sampleSize == null || this.sampleSize == 0) {
            // default sample size is 100
            this.sampleSize = 100;
            LogManager.logInfo(LogConstants.CTX_CONNECTOR, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29008, this.sampleSize));
        }
        String query = buildN1QLQuery(tableNameTypeMap.get(name), name, namespace, keyspace, this.sampleSize, hasTypeIdentifier);
        LogManager.logTrace(LogConstants.CTX_CONNECTOR, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29003, query));
        Iterator<N1qlQueryRow> result = conn.execute(query).iterator();
        while (result.hasNext()) {
            // result.next() always can not be null
            JsonObject row = result.next().value();
            JsonObject currentRowJson = row.getObject(keyspace);
            scanRow(keyspace, nameInSource(keyspace), currentRowJson, mf, table, table.getName(), false, new Dimension());
        }
    }
}
Also used : Table(org.teiid.metadata.Table) JsonObject(com.couchbase.client.java.document.json.JsonObject) N1qlQueryRow(com.couchbase.client.java.query.N1qlQueryRow) Column(org.teiid.metadata.Column)

Example 10 with Column

use of org.teiid.metadata.Column in project teiid by teiid.

the class CassandraMetadataProcessor method addTable.

/**
 * Adds table.
 * @param columnFamily
 */
private void addTable(MetadataFactory factory, TableMetadata columnFamily) {
    Table table = factory.addTable(columnFamily.getName());
    addColumnsToTable(factory, table, columnFamily);
    addPrimaryKey(factory, table, columnFamily);
    for (IndexMetadata index : columnFamily.getIndexes()) {
        Column c = table.getColumnByName(index.getTarget());
        if (c != null) {
            c.setSearchType(SearchType.Searchable);
            factory.addIndex(index.getName(), false, Arrays.asList(index.getTarget()), table);
        }
    }
    table.setSupportsUpdate(true);
}
Also used : Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column) IndexMetadata(com.datastax.driver.core.IndexMetadata)

Aggregations

Column (org.teiid.metadata.Column)210 Table (org.teiid.metadata.Table)72 ArrayList (java.util.ArrayList)47 TranslatorException (org.teiid.translator.TranslatorException)47 Test (org.junit.Test)39 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)21 MetadataFactory (org.teiid.metadata.MetadataFactory)20 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)19 KeyRecord (org.teiid.metadata.KeyRecord)18 Schema (org.teiid.metadata.Schema)18 MetadataStore (org.teiid.metadata.MetadataStore)17 Procedure (org.teiid.metadata.Procedure)14 RealMetadataFactory (org.teiid.query.unittest.RealMetadataFactory)14 ColumnReference (org.teiid.language.ColumnReference)12 DerivedColumn (org.teiid.language.DerivedColumn)12 Expression (org.teiid.language.Expression)12 Literal (org.teiid.language.Literal)10 QueryNode (org.teiid.query.mapping.relational.QueryNode)9 Connection (java.sql.Connection)7 ResultSet (java.sql.ResultSet)7