Skip to content

Commit

Permalink
feat: add support of advanced functions (sqlancer#882)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenglin-charlie-li authored Aug 22, 2023
1 parent b694a3a commit 7f37778
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/sqlancer/stonedb/ast/StoneDBAdvancedFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package sqlancer.stonedb.ast;

import java.util.List;

import sqlancer.Randomly;
import sqlancer.common.ast.FunctionNode;
import sqlancer.stonedb.ast.StoneDBAdvancedFunction.StoneDBAdvancedFunc;

public class StoneDBAdvancedFunction extends FunctionNode<StoneDBAdvancedFunc, StoneDBExpression>
implements StoneDBExpression {

// https://stonedb.io/docs/SQL-reference/functions/advanced-functions
public enum StoneDBAdvancedFunc {
IFNULL(2), IF(3), NULLIF(2), BIN(1), BINARY(1), CONV(3);

private int nrArgs;

StoneDBAdvancedFunc(int nrArgs) {
this.nrArgs = nrArgs;
}

public static StoneDBAdvancedFunc getRandom() {
return Randomly.fromOptions(values());
}

public int getNrArgs() {
return nrArgs;
}
}

protected StoneDBAdvancedFunction(StoneDBAdvancedFunc function, List<StoneDBExpression> args) {
super(function, args);
}
}
6 changes: 6 additions & 0 deletions src/sqlancer/stonedb/gen/StoneDBExpressionGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import sqlancer.stonedb.StoneDBProvider.StoneDBGlobalState;
import sqlancer.stonedb.StoneDBSchema.StoneDBColumn;
import sqlancer.stonedb.StoneDBSchema.StoneDBDataType;
import sqlancer.stonedb.ast.StoneDBAdvancedFunction.StoneDBAdvancedFunc;
import sqlancer.stonedb.ast.StoneDBAggregate.StoneDBAggregateFunction;
import sqlancer.stonedb.ast.StoneDBConstant;
import sqlancer.stonedb.ast.StoneDBExpression;
Expand Down Expand Up @@ -118,6 +119,11 @@ protected Node<StoneDBExpression> generateExpression(int depth) {
return new NewFunctionNode<>(generateExpressions(aggregateFunction.getNrArgs(), depth + 1),
aggregateFunction);
}
if (Randomly.getBooleanWithRatherLowProbability()) {
StoneDBAdvancedFunc advancedFunction = StoneDBAdvancedFunc.getRandom();
return new NewFunctionNode<>(generateExpressions(advancedFunction.getNrArgs(), depth + 1),
advancedFunction);
}
List<Expression> possibleOptions = new ArrayList<>(Arrays.asList(Expression.values()));
Expression expr = Randomly.fromList(possibleOptions);
Operator op;
Expand Down

0 comments on commit 7f37778

Please sign in to comment.