use of org.teiid.language.Literal in project teiid by teiid.
the class TestFileExecutionFactory method testGetTextFiles.
@Test
public void testGetTextFiles() throws Exception {
FileExecutionFactory fef = new FileExecutionFactory();
MetadataFactory mf = new MetadataFactory("vdb", 1, "text", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null);
fef.getMetadata(mf, null);
Procedure p = mf.getSchema().getProcedure("getTextFiles");
FileConnection fc = Mockito.mock(FileConnection.class);
Mockito.stub(fc.getFile("*.txt")).toReturn(new File(UnitTestUtil.getTestDataPath(), "*.txt"));
Call call = fef.getLanguageFactory().createCall("getTextFiles", Arrays.asList(new Argument(Direction.IN, new Literal("*.txt", TypeFacility.RUNTIME_TYPES.STRING), TypeFacility.RUNTIME_TYPES.STRING, null)), p);
ProcedureExecution pe = fef.createProcedureExecution(call, null, null, fc);
pe.execute();
int count = 0;
while (true) {
List<?> val = pe.next();
if (val == null) {
break;
}
assertEquals(5, val.size());
assertTrue(val.get(3) instanceof Timestamp);
assertEquals(Long.valueOf(0), val.get(4));
count++;
}
assertEquals(2, count);
call = fef.getLanguageFactory().createCall("getTextFiles", Arrays.asList(new Argument(Direction.IN, new Literal("*1*", TypeFacility.RUNTIME_TYPES.STRING), TypeFacility.RUNTIME_TYPES.STRING, null)), p);
pe = fef.createProcedureExecution(call, null, null, fc);
Mockito.stub(fc.getFile("*1*")).toReturn(new File(UnitTestUtil.getTestDataPath(), "*1*"));
pe.execute();
count = 0;
while (true) {
if (pe.next() == null) {
break;
}
count++;
}
assertEquals(1, count);
}
use of org.teiid.language.Literal in project teiid by teiid.
the class SubstringFunctionModifier method isNegative.
private Boolean isNegative(Expression ex) {
Boolean isNegative = null;
if (ex instanceof Literal) {
Literal l = (Literal) ex;
int value = (Integer) l.getValue();
isNegative = value < 0;
}
return isNegative;
}
use of org.teiid.language.Literal in project teiid by teiid.
the class SpreadsheetInsertVisitor method visit.
public void visit(Insert obj) {
worksheetTitle = obj.getTable().getName();
if (obj.getTable().getMetadataObject().getNameInSource() != null) {
worksheetTitle = obj.getTable().getMetadataObject().getNameInSource();
}
worksheetKey = info.getWorksheetByName(worksheetTitle).getId();
ExpressionValueSource evs = (ExpressionValueSource) obj.getValueSource();
for (int i = 0; i < evs.getValues().size(); i++) {
Expression e = evs.getValues().get(i);
if (!(e instanceof Literal)) {
throw new SpreadsheetOperationException("Only literals are allowed in the values section");
}
Literal l = (Literal) e;
if (l.getValue() == null) {
continue;
}
ColumnReference columnReference = obj.getColumns().get(i);
columnNameValuePair.put(columnReference.getMetadataObject().getSourceName(), l.getValue());
}
}
use of org.teiid.language.Literal in project teiid by teiid.
the class SubstringFunctionModifier method translate.
@Override
public List<?> translate(Function function) {
this.modify(function);
if (function.getParameters().size() != 3) {
return null;
}
// case when length > LENGTH(string) - start + 1 then LENGTH(string) - start + 1 case when length > 0 then length end
Expression forLength = function.getParameters().get(2);
List<SearchedWhenClause> clauses = new ArrayList<SearchedWhenClause>(2);
Boolean isNegative = null;
if (forLength instanceof Literal) {
Literal l = (Literal) forLength;
int value = (Integer) l.getValue();
isNegative = value < 0;
}
Function length = new Function(SourceSystemFunctions.LENGTH, Arrays.asList(function.getParameters().get(0)), TypeFacility.RUNTIME_TYPES.INTEGER);
Expression from = function.getParameters().get(1);
SearchedCase adjustedFrom = new SearchedCase(Arrays.asList(new SearchedWhenClause(new Comparison(from, length, Operator.GT), new Function(SourceSystemFunctions.ADD_OP, Arrays.asList(length, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER))), from, TypeFacility.RUNTIME_TYPES.INTEGER);
function.getParameters().set(1, adjustedFrom);
Expression maxLength = new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(length, new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(adjustedFrom, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER);
clauses.add(new SearchedWhenClause(new Comparison(forLength, maxLength, Operator.GT), maxLength));
Expression defaultExpr = null;
if (isNegative == null) {
clauses.add(new SearchedWhenClause(new Comparison(forLength, new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER), Operator.GT), forLength));
} else if (isNegative) {
// TODO: could be done in the rewriter
return Arrays.asList(new Literal(null, TypeFacility.RUNTIME_TYPES.STRING));
} else {
defaultExpr = forLength;
}
SearchedCase sc = new SearchedCase(clauses, defaultExpr, TypeFacility.RUNTIME_TYPES.INTEGER);
function.getParameters().set(2, sc);
return null;
}
use of org.teiid.language.Literal in project teiid by teiid.
the class TestSelectSymbolImpl method testGetColumnDataTypes.
public void testGetColumnDataTypes() {
Class<?>[] expectedResults = new Class[2];
List<DerivedColumn> symbols = new ArrayList<DerivedColumn>();
// $NON-NLS-1$//$NON-NLS-2$
symbols.add(new DerivedColumn("c1", new Literal("3", DataTypeManager.DefaultDataClasses.STRING)));
expectedResults[0] = DataTypeManager.DefaultDataClasses.STRING;
// $NON-NLS-1$
symbols.add(new DerivedColumn("c2", new Literal(new Integer(5), DataTypeManager.DefaultDataClasses.INTEGER)));
expectedResults[1] = DataTypeManager.DefaultDataClasses.INTEGER;
Select query = new Select(symbols, false, null, null, null, null, null);
Class<?>[] results = query.getColumnTypes();
assertEquals(results[0], expectedResults[0]);
assertEquals(results[1], expectedResults[1]);
}
Aggregations