Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TEIID-4974 TEIID-5030 TEIID-5031 TEIID-5033 TEIID-5034 TEIID-5035
TEIID-5036 TEIID-5037 addressing various couchbase issues
- Loading branch information
Showing
13 changed files
with
380 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...tor-couchbase/src/main/java/org/teiid/translator/couchbase/SubstringFunctionModifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags and | ||
* the COPYRIGHT.txt file distributed with this work. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.teiid.translator.couchbase; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.teiid.language.Comparison; | ||
import org.teiid.language.Comparison.Operator; | ||
import org.teiid.language.Expression; | ||
import org.teiid.language.Function; | ||
import org.teiid.language.Literal; | ||
import org.teiid.language.SearchedCase; | ||
import org.teiid.language.SearchedWhenClause; | ||
import org.teiid.translator.SourceSystemFunctions; | ||
import org.teiid.translator.TypeFacility; | ||
import org.teiid.translator.jdbc.AliasModifier; | ||
|
||
public class SubstringFunctionModifier extends AliasModifier { | ||
|
||
public SubstringFunctionModifier() { | ||
super("substr"); //$NON-NLS-1$ | ||
} | ||
|
||
@Override | ||
public List<?> translate(Function function) { | ||
this.modify(function); | ||
|
||
Expression from = function.getParameters().get(1); | ||
Boolean isFromNegative = isNegative(from); | ||
|
||
Function length = new Function( | ||
SourceSystemFunctions.LENGTH, | ||
Arrays.asList(function.getParameters().get(0)), | ||
TypeFacility.RUNTIME_TYPES.INTEGER); | ||
|
||
if (function.getParameters().size() == 2 && (isFromNegative == null || isFromNegative)) { | ||
//couchbase does not handle default length with a negative from index | ||
function.getParameters().add(length); | ||
} | ||
|
||
if (function.getParameters().size() == 3) { | ||
//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 = isNegative(forLength); | ||
|
||
Expression adjustedFrom = from; | ||
if (isFromNegative == null || isFromNegative) { | ||
adjustedFrom = new SearchedCase(Arrays.asList(new SearchedWhenClause(new Comparison(from, new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER), Operator.LT), new Function( | ||
SourceSystemFunctions.ADD_OP, | ||
Arrays.asList( | ||
new Function(SourceSystemFunctions.ADD_OP, Arrays.asList(length, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)),TypeFacility.RUNTIME_TYPES.INTEGER), | ||
from), | ||
TypeFacility.RUNTIME_TYPES.INTEGER))), | ||
from, TypeFacility.RUNTIME_TYPES.INTEGER); | ||
} | ||
|
||
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); | ||
} | ||
|
||
Expression adjustedFrom = function.getParameters().get(1); | ||
if (isFromNegative == null) { | ||
//case when start > 0 then start - 1 else start end | ||
SearchedCase sc = new SearchedCase(Arrays.asList(new SearchedWhenClause(new Comparison(adjustedFrom, new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER), Operator.GT), | ||
new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(adjustedFrom, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)),TypeFacility.RUNTIME_TYPES.INTEGER))), | ||
from, TypeFacility.RUNTIME_TYPES.INTEGER); | ||
function.getParameters().set(1, sc); | ||
} else if (!isFromNegative){ | ||
function.getParameters().set(1, new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(adjustedFrom, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)),TypeFacility.RUNTIME_TYPES.INTEGER)); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
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; | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...base/translator-couchbase/src/test/java/org/teiid/translator/couchbase/TestCouchbase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags and | ||
* the COPYRIGHT.txt file distributed with this work. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.teiid.translator.couchbase; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.teiid.translator.TranslatorException; | ||
import org.teiid.translator.TypeFacility; | ||
|
||
@Ignore | ||
public class TestCouchbase { | ||
|
||
@Test public void testConvert() throws TranslatorException { | ||
CouchbaseExecutionFactory cef = new CouchbaseExecutionFactory(); | ||
cef.start(); | ||
assertFalse(cef.supportsConvert(TypeFacility.RUNTIME_CODES.STRING, TypeFacility.RUNTIME_CODES.DATE)); | ||
assertTrue(cef.supportsConvert(TypeFacility.RUNTIME_CODES.STRING, TypeFacility.RUNTIME_CODES.DOUBLE)); | ||
} | ||
} |
Oops, something went wrong.