Skip to content

Commit

Permalink
TEIID-4761: Support lpad/rpad pushdown to sql server (adding lpad/rpad)
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins authored and johnathonlee committed Aug 1, 2018
1 parent ac1e36f commit 1ce8312
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ <h4 class="western">from ${project.version}</h4>
<li/>
<p style="margin-bottom: 0in">
<a href='https://issues.jboss.org/browse/TEIID-4532'>TEIID-4532</a> - Provide one-way or cryptographic hash functions (add and refining hash functions and removing the overlapping function)

<li/>
<p style="margin-bottom: 0in">
<a href='https://issues.jboss.org/browse/TEIID-4761'>TEIID-4761</a> - Support lpad/rpad pushdown to sql server (adding lpad/rpad)
</ul>

<h4 class="western">from 8.12.14.6_4</h4>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,13 @@ public List<String> getSupportedFunctions() {
supportedFunctions.add("LENGTH"); //$NON-NLS-1$
supportedFunctions.add(SourceSystemFunctions.LOCATE);
supportedFunctions.add("LOWER"); //$NON-NLS-1$
//supportedFunctons.add("LPAD"); //$NON-NLS-1$
supportedFunctions.add("LPAD"); //$NON-NLS-1$
supportedFunctions.add("LTRIM"); //$NON-NLS-1$
supportedFunctions.add("REPEAT"); //$NON-NLS-1$
//supportedFunctions.add("RAND"); //$NON-NLS-1$
supportedFunctions.add("REPLACE"); //$NON-NLS-1$
supportedFunctions.add("RIGHT"); //$NON-NLS-1$
//supportedFunctons.add("RPAD"); //$NON-NLS-1$
supportedFunctions.add("RPAD"); //$NON-NLS-1$
supportedFunctions.add("RTRIM"); //$NON-NLS-1$
supportedFunctions.add("SPACE"); //$NON-NLS-1$
supportedFunctions.add("SUBSTRING"); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.teiid.translator.Translator;
import org.teiid.translator.TranslatorException;
import org.teiid.translator.TranslatorProperty;
import org.teiid.translator.TypeFacility;
import org.teiid.translator.jdbc.AliasModifier;
import org.teiid.translator.jdbc.ConvertModifier;
import org.teiid.translator.jdbc.EscapeSyntaxModifier;
Expand Down Expand Up @@ -154,6 +155,24 @@ public List<?> translate(Function function) {
}
});
}
registerFunctionModifier(SourceSystemFunctions.LPAD, new FunctionModifier() {
@Override
public List<?> translate(Function function) {
List<Expression> params = function.getParameters();
return Arrays.asList("RIGHT(REPLICATE(", //$NON-NLS-1$
params.size()>2?params.get(2):new Literal(" ", TypeFacility.RUNTIME_TYPES.STRING), ", ", //$NON-NLS-1$ //$NON-NLS-2$
params.get(1), ") + ", params.get(0), ", ", params.get(1), ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
});
registerFunctionModifier(SourceSystemFunctions.RPAD, new FunctionModifier() {
@Override
public List<?> translate(Function function) {
List<Expression> params = function.getParameters();
return Arrays.asList("LEFT(", params.get(0), " + REPLICATE(", //$NON-NLS-1$ //$NON-NLS-2$
params.size()>2?params.get(2):new Literal(" ", TypeFacility.RUNTIME_TYPES.STRING), ", ", //$NON-NLS-1$ //$NON-NLS-2$
params.get(1), "), ", params.get(1), ")"); //$NON-NLS-1$ //$NON-NLS-2$
}
});
registerFunctionModifier(SourceSystemFunctions.LCASE, new AliasModifier("lower")); //$NON-NLS-1$
registerFunctionModifier(SourceSystemFunctions.IFNULL, new AliasModifier("isnull")); //$NON-NLS-1$
registerFunctionModifier(SourceSystemFunctions.UCASE, new AliasModifier("upper")); //$NON-NLS-1$
Expand Down Expand Up @@ -348,6 +367,7 @@ public List<String> getSupportedFunctions() {
supportedFunctions.add("LEFT"); //$NON-NLS-1$
supportedFunctions.add("LENGTH"); //$NON-NLS-1$
supportedFunctions.add("LOWER"); //$NON-NLS-1$
supportedFunctions.add("LPAD"); //$NON-NLS-1$
supportedFunctions.add("LTRIM"); //$NON-NLS-1$
supportedFunctions.add("REPEAT"); //$NON-NLS-1$
//supportedFunctions.add("RAND"); //$NON-NLS-1$
Expand All @@ -369,6 +389,7 @@ public List<String> getSupportedFunctions() {
supportedFunctions.add("MONTHNAME"); //$NON-NLS-1$
//supportedFunctions.add("NOW"); //$NON-NLS-1$
supportedFunctions.add("QUARTER"); //$NON-NLS-1$
supportedFunctions.add("RPAD"); //$NON-NLS-1$
supportedFunctions.add("SECOND"); //$NON-NLS-1$
supportedFunctions.add("TIMESTAMPADD"); //$NON-NLS-1$
supportedFunctions.add("TIMESTAMPDIFF"); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,13 @@ public void testNonIntMod() throws Exception {
"SELECT 0xABCD1234"); //$NON-NLS-1$
}

@Test
public void testPadFunctions() {
String input = "SELECT lpad(PART_NAME,2), lpad(part_name,2,'x'), rpad(PART_NAME,2), rpad(part_name,2,'x') FROM PARTS"; //$NON-NLS-1$
String output = "SELECT RIGHT(REPLICATE(' ', 2) + PARTS.PART_NAME, 2), RIGHT(REPLICATE('x', 2) + PARTS.PART_NAME, 2), LEFT(PARTS.PART_NAME + REPLICATE(' ', 2), 2), LEFT(PARTS.PART_NAME + REPLICATE('x', 2), 2) FROM PARTS"; //$NON-NLS-1$
helpTestVisitor(getTestVDB(),
input,
output);
}

}

0 comments on commit 1ce8312

Please sign in to comment.