Skip to content

Commit

Permalink
TEIID-3760 fixing endswith
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Oct 16, 2015
1 parent 29fb68c commit 3075f4f
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 30 deletions.
Expand Up @@ -42,6 +42,7 @@
import org.teiid.metadata.FunctionMethod;
import org.teiid.metadata.RuntimeMetadata;
import org.teiid.metadata.Table;
import org.teiid.translator.SourceSystemFunctions;
import org.teiid.translator.TranslatorException;

public class ODataSQLVisitor extends HierarchyVisitor {
Expand Down Expand Up @@ -233,12 +234,18 @@ public void visit(Function obj) {
this.filter.append(name)
.append(Tokens.LPAREN);
if (args != null && args.size() != 0) {
for (int i = 0; i < args.size(); i++) {
append(args.get(i));
if (i < args.size()-1) {
this.filter.append(Tokens.COMMA);
}
}
if (SourceSystemFunctions.ENDSWITH.equalsIgnoreCase(name)) {
append(args.get(1));
this.filter.append(Tokens.COMMA);
append(args.get(0));
} else {
for (int i = 0; i < args.size(); i++) {
append(args.get(i));
if (i < args.size()-1) {
this.filter.append(Tokens.COMMA);
}
}
}
}
this.filter.append(Tokens.RPAREN);
}
Expand Down
Expand Up @@ -110,6 +110,11 @@ public void testMultiKeyKeyBasedFilter() throws Exception {
@Test
public void testAddFilter() throws Exception {
helpExecute("select UnitPrice from Order_Details where OrderID = 1 and (Quantity+2) > OrderID", "Order_Details?$filter=OrderID eq 1 and (cast(Quantity,'integer') add 2) gt OrderID&$select=UnitPrice");
}

@Test
public void testEndsWithFilter() throws Exception {
helpExecute("select CompanyName from Customers where endswith('k', CustomerID)", "Customers?$filter=endswith(CustomerID,'k') eq true&$select=CompanyName");
}

@Test
Expand Down
Expand Up @@ -21,32 +21,22 @@
*/
package org.teiid.translator.odata4;

import static org.teiid.language.SQLConstants.Reserved.NOT;
import static org.teiid.language.SQLConstants.Reserved.NULL;
import static org.teiid.language.SQLConstants.Reserved.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import org.teiid.language.AndOr;
import org.teiid.language.ColumnReference;
import org.teiid.language.Comparison;
import org.teiid.language.Condition;
import org.teiid.language.Expression;
import org.teiid.language.Function;
import org.teiid.language.In;
import org.teiid.language.IsNull;
import org.teiid.language.LanguageObject;
import org.teiid.language.Literal;
import org.teiid.language.Not;
import org.teiid.language.*;
import org.teiid.language.SQLConstants.Tokens;
import org.teiid.language.visitor.HierarchyVisitor;
import org.teiid.metadata.Column;
import org.teiid.metadata.FunctionMethod;
import org.teiid.metadata.RuntimeMetadata;
import org.teiid.metadata.Table;
import org.teiid.translator.SourceSystemFunctions;
import org.teiid.translator.TranslatorException;

/**
Expand Down Expand Up @@ -258,12 +248,18 @@ public void visit(Function obj) {
this.exprType.push(type);
} else {
if (args != null && args.size() != 0) {
for (int i = 0; i < args.size(); i++) {
append(args.get(i));
if (i < args.size()-1) {
this.filter.append(Tokens.COMMA);
}
}
if (SourceSystemFunctions.ENDSWITH.equalsIgnoreCase(name)) {
append(args.get(1));
this.filter.append(Tokens.COMMA);
append(args.get(0));
} else {
for (int i = 0; i < args.size(); i++) {
append(args.get(i));
if (i < args.size()-1) {
this.filter.append(Tokens.COMMA);
}
}
}
}
this.exprType.push(odataType(
method.getOutputParameter().getNativeType(),
Expand Down
Expand Up @@ -665,7 +665,7 @@ private void addEndsWithFunction() {
new FunctionParameter[] {
new FunctionParameter("substring", DataTypeManager.DefaultDataTypes.STRING, QueryPlugin.Util.getString("SystemSource.endswith_arg1")), //$NON-NLS-1$ //$NON-NLS-2$
new FunctionParameter("string", DataTypeManager.DefaultDataTypes.STRING, QueryPlugin.Util.getString("SystemSource.endswith_arg2"))}, //$NON-NLS-1$ //$NON-NLS-2$
new FunctionParameter("result", DataTypeManager.DefaultDataTypes.STRING, QueryPlugin.Util.getString("SystemSource.endswith_result")) ); //$NON-NLS-1$ //$NON-NLS-2$
new FunctionParameter("result", DataTypeManager.DefaultDataTypes.BOOLEAN, QueryPlugin.Util.getString("SystemSource.endswith_result")) ); //$NON-NLS-1$ //$NON-NLS-2$
functions.add(f);
}

Expand Down
Expand Up @@ -518,5 +518,10 @@ public void helpTestCommandPayload(Serializable payload, String property, String
@Test public void testTimestampResolving() throws Exception {
assertEval("TIMESTAMPDIFF(SQL_TSI_YEAR, '2000-01-01', '2002-01-01')", "2");
}

@Test public void testEndsWith() throws Exception {
// Test replace-first vs replace-all.
assertEval("endsWith('c', 'abc') = 't'", "true");
}

}
2 changes: 1 addition & 1 deletion odata/src/main/java/org/teiid/odata/ODataSQLBuilder.java
Expand Up @@ -477,7 +477,7 @@ public void visit(EndsWithMethodCallExpression expr) {
Expression target = stack.pop();
visitNode(expr.getValue());
Expression value = stack.pop();
Criteria criteria = new CompareCriteria(new Function("ENDSWITH", new Expression[] {target, value}), CompareCriteria.EQ, new Constant(Boolean.TRUE));
Criteria criteria = new CompareCriteria(new Function("ENDSWITH", new Expression[] {value, target}), CompareCriteria.EQ, new Constant(Boolean.TRUE));
stack.push(criteria);
}

Expand Down
Expand Up @@ -105,7 +105,7 @@ public void testConcat() {

@Test
public void testEndsWith() {
te("endswith(x, 'foo')", "ENDSWITH(x, 'foo') = TRUE");
te("endswith(x, 'foo')", "ENDSWITH('foo', x) = TRUE");
}

@Test
Expand Down
Expand Up @@ -1722,7 +1722,7 @@ QT_Ora9DS SYS
QT_Ora9DS SYS degrees number 1 8 double 17 8 4 10 1 Number <null> 1 YES tsid:00000005c507e1a-000000ec
QT_Ora9DS SYS degrees result 4 8 double 17 8 4 10 1 Convert from radians to degrees <null> 0 YES tsid:00000005c507e1a-000000ef
QT_Ora9DS SYS degrees number 1 2 bigdecimal 2147483647 2147483647 32767 10 1 Number <null> 1 YES tsid:00000005c507e1a-000000ef
QT_Ora9DS SYS endswith result 4 12 string 4000 4000 0 0 1 true if string ends with character sequence of substring; false o$ <null> 0 YES tsid:0000000681c1ab1-00000218
QT_Ora9DS SYS endswith result 4 -7 boolean 1 1 0 10 1 true if string ends with character sequence of substring; false o$ <null> 0 YES tsid:0000000681c1ab1-00000218
QT_Ora9DS SYS endswith substring 1 12 string 4000 4000 0 0 1 substring <null> 1 YES tsid:0000000681c1ab1-00000218
QT_Ora9DS SYS endswith string 1 12 string 4000 4000 0 0 1 string <null> 2 YES tsid:0000000681c1ab1-00000218
QT_Ora9DS SYS env result 4 12 string 4000 4000 0 0 1 Return the string value of the environment variable <null> 0 YES tsid:000000000289be0-00000ac6
Expand Down
Expand Up @@ -1989,7 +1989,7 @@ PartsSupplier SYS
PartsSupplier SYS mvstatus tsid:000000067f44dce-00000d3d result object 0 ReturnValue 0 2147483647 0 0 Nullable tsid:fffffffc874db10-00000d43 true if the view is valid to use
PartsSupplier SYS endswith tsid:0000000681c1ab1-00000218 substring string 1 In 0 4000 0 0 Nullable tsid:00000001fc68144-00000219 substring
PartsSupplier SYS endswith tsid:0000000681c1ab1-00000218 string string 2 In 0 4000 0 0 Nullable tsid:fffffffcafc7304-0000021a string
PartsSupplier SYS endswith tsid:0000000681c1ab1-00000218 result string 0 ReturnValue 0 4000 0 0 Nullable tsid:fffffffc874db10-0000021b true if string ends with character sequence of substring; false o$
PartsSupplier SYS endswith tsid:0000000681c1ab1-00000218 result boolean 0 ReturnValue 1 1 0 10 Nullable tsid:fffffffc874db10-0000021b true if string ends with character sequence of substring; false o$
PartsSupplier SYS xpathvalue tsid:00000006d4e15e7-00000bc0 document string 1 In 0 4000 0 0 Nullable tsid:00000003383e40e-00000bc1 Source document
PartsSupplier SYS xpathvalue tsid:00000006d4e15e7-00000bc0 xpath string 2 In 0 4000 0 0 Nullable tsid:000000006f67b90-00000bc2 XPath expression
PartsSupplier SYS xpathvalue tsid:00000006d4e15e7-00000bc0 result string 0 ReturnValue 0 4000 0 0 Nullable tsid:fffffffc874db10-00000bc3 Single result
Expand Down

0 comments on commit 3075f4f

Please sign in to comment.