Search in sources :

Example 1 with TextColumn

use of org.teiid.query.sql.lang.TextTable.TextColumn in project teiid by teiid.

the class TextTableNode method processHeader.

private void processHeader(List<String> line) throws TeiidProcessingException {
    nameIndexes = new HashMap<String, Integer>();
    this.lineWidth = DataTypeManager.MAX_STRING_LENGTH * line.size();
    for (String string : line) {
        if (string == null) {
            continue;
        }
        nameIndexes.put(string.toUpperCase(), nameIndexes.size());
    }
    for (TextColumn col : table.getColumns()) {
        if (col.isOrdinal()) {
            continue;
        }
        String name = col.getName().toUpperCase();
        if (col.getHeader() != null) {
            name = col.getHeader().toUpperCase();
        }
        Integer index = nameIndexes.get(name);
        if (index == null) {
            throw new TeiidProcessingException(QueryPlugin.Event.TEIID30181, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30181, col.getName(), systemId));
        }
        nameIndexes.put(col.getName(), index);
    }
}
Also used : TextColumn(org.teiid.query.sql.lang.TextTable.TextColumn) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 2 with TextColumn

use of org.teiid.query.sql.lang.TextTable.TextColumn in project teiid by teiid.

the class TextTableNode method process.

private void process() throws TeiidProcessingException {
    while (true) {
        synchronized (this) {
            if (isBatchFull()) {
                return;
            }
            StringBuilder line = readLine(lineWidth, table.isFixedWidth());
            if (line == null) {
                terminateBatches();
                break;
            }
            String parentSelector = null;
            if (table.getSelector() != null) {
                if (line.length() < table.getSelector().length()) {
                    continue;
                }
                if (!line.substring(0, table.getSelector().length()).equals(table.getSelector())) {
                    if (parentLines == null) {
                        // doesn't match any selector
                        continue;
                    }
                    parentSelector = line.substring(0, table.getSelector().length());
                    if (!parentLines.containsKey(parentSelector)) {
                        // doesn't match any selector
                        continue;
                    }
                }
            }
            List<String> vals = parseLine(line);
            if (parentSelector != null) {
                this.parentLines.put(parentSelector, vals);
                continue;
            } else if (table.getSelector() != null && !table.getSelector().equals(vals.get(0))) {
                continue;
            }
            rowNumber++;
            List<Object> tuple = new ArrayList<Object>(projectionIndexes.length);
            for (int output : projectionIndexes) {
                TextColumn col = table.getColumns().get(output);
                String val = null;
                int index = output;
                if (col.isOrdinal()) {
                    if (rowNumber > Integer.MAX_VALUE) {
                        throw new TeiidRuntimeException(new TeiidProcessingException(QueryPlugin.Event.TEIID31174, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31174)));
                    }
                    tuple.add((int) rowNumber);
                    continue;
                }
                if (col.getSelector() != null) {
                    vals = this.parentLines.get(col.getSelector());
                    index = col.getPosition() - 1;
                } else if (nameIndexes != null) {
                    index = nameIndexes.get(col.getName());
                }
                if (vals == null || index >= vals.size()) {
                    // throw new TeiidProcessingException(QueryPlugin.Util.getString("TextTableNode.no_value", col.getName(), textLine, systemId)); //$NON-NLS-1$
                    tuple.add(null);
                    continue;
                }
                val = vals.get(index);
                try {
                    tuple.add(DataTypeManager.transformValue(val, table.getColumns().get(output).getSymbol().getType()));
                } catch (TransformationException e) {
                    throw new TeiidProcessingException(QueryPlugin.Event.TEIID30176, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30176, col.getName(), textLine, systemId));
                }
            }
            addBatchRow(tuple);
            if (rowNumber == limit) {
                terminateBatches();
                break;
            }
        }
    }
}
Also used : TransformationException(org.teiid.core.types.TransformationException) ArrayList(java.util.ArrayList) LanguageObject(org.teiid.query.sql.LanguageObject) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) TextColumn(org.teiid.query.sql.lang.TextTable.TextColumn) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 3 with TextColumn

use of org.teiid.query.sql.lang.TextTable.TextColumn in project teiid by teiid.

the class TextTableNode method initialize.

@Override
public void initialize(CommandContext context, BufferManager bufferManager, ProcessorDataManager dataMgr) {
    super.initialize(context, bufferManager, dataMgr);
    if (projectionIndexes != null) {
        return;
    }
    if (table.getSkip() != null) {
        skip = table.getSkip();
    }
    if (table.getHeader() != null) {
        skip = Math.max(table.getHeader(), skip);
        header = table.getHeader() - 1;
    }
    if (table.isFixedWidth()) {
        for (TextColumn col : table.getColumns()) {
            lineWidth += col.getWidth();
        }
    } else {
        if (table.getDelimiter() == null) {
            delimiter = ',';
        } else {
            delimiter = table.getDelimiter();
        }
        if (table.getQuote() == null) {
            quote = '"';
        } else {
            noQuote = table.isEscape();
            quote = table.getQuote();
        }
        for (TextColumn column : table.getColumns()) {
            if (column.getSelector() != null) {
                if (parentLines == null) {
                    parentLines = new HashMap<String, List<String>>();
                }
                parentLines.put(column.getSelector(), null);
            }
        }
        lineWidth = table.getColumns().size() * DataTypeManager.MAX_STRING_LENGTH;
    }
    if (table.isUsingRowDelimiter()) {
        Character c = table.getRowDelimiter();
        if (c != null) {
            this.newLine = c;
            this.crNewLine = false;
        }
    }
    Map<Expression, Integer> elementMap = createLookupMap(table.getProjectedSymbols());
    this.projectionIndexes = getProjectionIndexes(elementMap, getElements());
}
Also used : Expression(org.teiid.query.sql.symbol.Expression) ArrayList(java.util.ArrayList) List(java.util.List) TextColumn(org.teiid.query.sql.lang.TextTable.TextColumn)

Example 4 with TextColumn

use of org.teiid.query.sql.lang.TextTable.TextColumn in project teiid by teiid.

the class TestParser method testTextTable.

@Test
public void testTextTable() throws Exception {
    // $NON-NLS-1$
    String sql = "SELECT * from texttable(file columns x string WIDTH 1, y date width 10 skip 10) as x";
    Query query = new Query();
    query.setSelect(new Select(Arrays.asList(new MultipleElementSymbol())));
    TextTable tt = new TextTable();
    tt.setFile(new ElementSymbol("file"));
    List<TextTable.TextColumn> columns = new ArrayList<TextTable.TextColumn>();
    columns.add(new TextTable.TextColumn("x", "string", 1, false));
    columns.add(new TextTable.TextColumn("y", "date", 10, false));
    tt.setColumns(columns);
    tt.setSkip(10);
    tt.setName("x");
    query.setFrom(new From(Arrays.asList(tt)));
    helpTest(sql, "SELECT * FROM TEXTTABLE(file COLUMNS x string WIDTH 1, y date WIDTH 10 SKIP 10) AS x", query);
    // $NON-NLS-1$
    sql = "SELECT * from texttable(file columns x string, y date delimiter ',' escape '\"' header skip 10) as x";
    tt.setDelimiter(',');
    tt.setQuote('"');
    tt.setEscape(true);
    tt.setHeader(1);
    for (TextColumn textColumn : columns) {
        textColumn.setWidth(null);
    }
    helpTest(sql, "SELECT * FROM TEXTTABLE(file COLUMNS x string, y date DELIMITER ',' ESCAPE '\"' HEADER SKIP 10) AS x", query);
}
Also used : TextColumn(org.teiid.query.sql.lang.TextTable.TextColumn) ArrayList(java.util.ArrayList) TextColumn(org.teiid.query.sql.lang.TextTable.TextColumn) Test(org.junit.Test)

Example 5 with TextColumn

use of org.teiid.query.sql.lang.TextTable.TextColumn in project teiid by teiid.

the class SQLStringVisitor method visit.

@Override
public void visit(TextTable obj) {
    addHintComment(obj);
    // $NON-NLS-1$
    append("TEXTTABLE(");
    visitNode(obj.getFile());
    if (obj.getSelector() != null) {
        append(SPACE);
        append(NonReserved.SELECTOR);
        append(SPACE);
        outputLiteral(String.class, false, obj.getSelector());
    }
    append(SPACE);
    append(NonReserved.COLUMNS);
    boolean noTrim = obj.isNoTrim();
    for (Iterator<TextColumn> cols = obj.getColumns().iterator(); cols.hasNext(); ) {
        TextColumn col = cols.next();
        append(SPACE);
        outputDisplayName(col.getName());
        append(SPACE);
        if (col.isOrdinal()) {
            append(FOR);
            append(SPACE);
            append(NonReserved.ORDINALITY);
        } else {
            if (col.getHeader() != null) {
                append(NonReserved.HEADER);
                append(SPACE);
                outputLiteral(String.class, false, col.getHeader());
                append(SPACE);
            }
            append(col.getType());
            if (col.getWidth() != null) {
                append(SPACE);
                append(NonReserved.WIDTH);
                append(SPACE);
                append(col.getWidth());
            }
            if (!noTrim && col.isNoTrim()) {
                append(SPACE);
                append(NO);
                append(SPACE);
                append(NonReserved.TRIM);
            }
            if (col.getSelector() != null) {
                append(SPACE);
                append(NonReserved.SELECTOR);
                append(SPACE);
                outputLiteral(String.class, false, col.getSelector());
                append(SPACE);
                append(col.getPosition());
            }
        }
        if (cols.hasNext()) {
            // $NON-NLS-1$
            append(",");
        }
    }
    if (!obj.isUsingRowDelimiter()) {
        append(SPACE);
        append(NO);
        append(SPACE);
        append(ROW);
        append(SPACE);
        append(NonReserved.DELIMITER);
    } else if (obj.getRowDelimiter() != null) {
        append(SPACE);
        append(ROW);
        append(SPACE);
        append(NonReserved.DELIMITER);
        append(SPACE);
        visitNode(new Constant(obj.getRowDelimiter()));
    }
    if (obj.getDelimiter() != null) {
        append(SPACE);
        append(NonReserved.DELIMITER);
        append(SPACE);
        visitNode(new Constant(obj.getDelimiter()));
    }
    if (obj.getQuote() != null) {
        append(SPACE);
        if (obj.isEscape()) {
            append(ESCAPE);
        } else {
            append(NonReserved.QUOTE);
        }
        append(SPACE);
        visitNode(new Constant(obj.getQuote()));
    }
    if (obj.getHeader() != null) {
        append(SPACE);
        append(NonReserved.HEADER);
        if (1 != obj.getHeader()) {
            append(SPACE);
            append(obj.getHeader());
        }
    }
    if (obj.getSkip() != null) {
        append(SPACE);
        append(NonReserved.SKIP);
        append(SPACE);
        append(obj.getSkip());
    }
    if (noTrim) {
        append(SPACE);
        append(NO);
        append(SPACE);
        append(NonReserved.TRIM);
    }
    // $NON-NLS-1$
    append(")");
    append(SPACE);
    append(AS);
    append(SPACE);
    outputDisplayName(obj.getName());
}
Also used : TextColumn(org.teiid.query.sql.lang.TextTable.TextColumn)

Aggregations

TextColumn (org.teiid.query.sql.lang.TextTable.TextColumn)6 ArrayList (java.util.ArrayList)4 TeiidProcessingException (org.teiid.core.TeiidProcessingException)2 List (java.util.List)1 Test (org.junit.Test)1 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)1 TransformationException (org.teiid.core.types.TransformationException)1 LanguageObject (org.teiid.query.sql.LanguageObject)1 Expression (org.teiid.query.sql.symbol.Expression)1