Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REVIEW PURPOSE] Add QUOTE java implementation functions #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1717,4 +1717,40 @@ public void eval() {
out.end = outBytea.length;
}
}

/**
* Returns the quoted string (Includes escape character for any single quotes)
*/
@FunctionTemplate(name = "quote", scope = FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL)
public static class Quote implements SimpleFunction {
@Param VarCharHolder in;
@Output VarCharHolder out;
@Inject ArrowBuf buffer;

@Override
public void setup() {}

@Override
public void eval() {
final int len = in.end - in.start;
out.start = 0;
out.buffer = buffer = buffer.reallocIfNeeded(len * 2L);
int counter = 1;
// Set the initial single quote (' -> 39) byte
buffer.setByte(0, 39);
for (int i = in.start; i < in.end; i++) {
// Check if the byte is a single quote
if (in.buffer.getByte(i) == 39) {
out.buffer.setByte(counter, 92);
counter++;
out.buffer.setByte(counter, 39);
} else {
out.buffer.setByte(counter, in.buffer.getByte(i));
}
counter++;
}
out.buffer.setByte(counter, 39);
out.end = counter + 1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,13 @@ public void stringfuncs(){
});
}

@Test
public void quote(){
testFunctionsInterpretedOnly(new Object[][]{
{ "quote(c0)", "dont", "'dont'"},
{ "quote(c0)", "don't", "'don\\'t'"},
{ "quote(c0)", "'", "'\\''"}
});
}

}
35 changes: 27 additions & 8 deletions sabot/kernel/src/test/java/com/dremio/sabot/BaseTestFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
public class BaseTestFunction extends BaseTestOperator {

public static final boolean RUN_INTERPRETED_MODE = true;
public static final boolean RUN_COMPILED_MODE = true;
public static final boolean IGNORE_INTERPRETED_MODE = false;
public static final boolean IGNORE_COMPILED_MODE = false;

public void testFunctions(Object[][] tests){
for(Object[] test : tests){
Expand All @@ -64,18 +66,33 @@ public void testFunctionsCompiledOnly(Object[][] tests){
* @param fields All of the input values (n-1), plus the output value (nth value).
*/
public void testFunction(String stringExpression, Object... fieldsArr) {
testFunctionInner(stringExpression, RUN_INTERPRETED_MODE, fieldsArr);
testFunctionInner(stringExpression, RUN_INTERPRETED_MODE, RUN_COMPILED_MODE, fieldsArr);
}

public void testFunctionsInterpretedOnly(Object[][] tests){
for(Object[] test : tests){
testFunctionInterpretedOnly((String) test[0], Arrays.copyOfRange(test, 1, test.length));
}
}


/**
* Evaluate the given expression only in compiled mode.
* Used for functions that are not supported in interpreted mode for e.g. gandiva only functions.
*/
public void testFunctionCompileOnly(String stringExpression, Object... fieldsArr) {
testFunctionInner(stringExpression, IGNORE_INTERPRETED_MODE, fieldsArr);
testFunctionInner(stringExpression, IGNORE_INTERPRETED_MODE, RUN_COMPILED_MODE, fieldsArr);
}

private void testFunctionInner(String stringExpression, boolean runInterpretedMode,
/**
* Evaluate the given expression only in interpreted mode.
* Used for functions that are only supported in interpreted mode (e.g. Dremio only functions).
*/
public void testFunctionInterpretedOnly(String stringExpression, Object... fieldsArr) {
testFunctionInner(stringExpression, RUN_INTERPRETED_MODE, IGNORE_COMPILED_MODE, fieldsArr);
}

private void testFunctionInner(String stringExpression, boolean runInterpretedMode, boolean runCompiledMode,
Object[] fieldsArr) {
try{
Preconditions.checkArgument(fieldsArr.length > 0, "Must provide an output for a function.");
Expand All @@ -100,11 +117,13 @@ private void testFunctionInner(String stringExpression, boolean runInterpretedMo
final Table output = Fixtures.t(Fixtures.th("out"), Fixtures.tr(fieldsArr[fieldsArr.length - 1]));
Project p = new Project(OpProps.prototype(), null, Arrays.asList(new NamedExpression(expr, new FieldReference("out"))));

try {
validateSingle(p, ProjectOperator.class, input.toGenerator(getTestAllocator()), output, DEFAULT_BATCH);
} catch(AssertionError | Exception e) {
e.printStackTrace();
throw new RuntimeException("Failure while testing function using code compilation.", e);
if (runCompiledMode) {
try {
validateSingle(p, ProjectOperator.class, input.toGenerator(getTestAllocator()), output, DEFAULT_BATCH);
} catch (AssertionError | Exception e) {
e.printStackTrace();
throw new RuntimeException("Failure while testing function using code compilation.", e);
}
}

if (runInterpretedMode) {
Expand Down