|
5 | 5 | import java.util.Collections; |
6 | 6 | import java.util.List; |
7 | 7 | import java.util.Map; |
| 8 | +import java.util.function.Function; |
8 | 9 | import java.util.stream.Collectors; |
9 | 10 | import java.util.stream.Stream; |
10 | 11 |
|
11 | 12 | import sqlancer.IgnoreMeException; |
12 | 13 | import sqlancer.Randomly; |
| 14 | +import sqlancer.common.gen.CERTGenerator; |
13 | 15 | import sqlancer.common.gen.ExpressionGenerator; |
14 | 16 | import sqlancer.common.gen.NoRECGenerator; |
15 | 17 | import sqlancer.common.gen.TLPWhereGenerator; |
16 | 18 | import sqlancer.common.schema.AbstractTables; |
| 19 | +import sqlancer.postgres.PostgresBugs; |
17 | 20 | import sqlancer.postgres.PostgresCompoundDataType; |
18 | 21 | import sqlancer.postgres.PostgresGlobalState; |
19 | 22 | import sqlancer.postgres.PostgresProvider; |
|
63 | 66 | import sqlancer.postgres.ast.PostgresSelect.PostgresSubquery; |
64 | 67 | import sqlancer.postgres.ast.PostgresSelect.SelectType; |
65 | 68 | import sqlancer.postgres.ast.PostgresSimilarTo; |
| 69 | +import sqlancer.postgres.ast.PostgresTableReference; |
66 | 70 |
|
67 | 71 | public class PostgresExpressionGenerator implements ExpressionGenerator<PostgresExpression>, |
68 | 72 | NoRECGenerator<PostgresSelect, PostgresJoin, PostgresExpression, PostgresTable, PostgresColumn>, |
69 | | - TLPWhereGenerator<PostgresSelect, PostgresJoin, PostgresExpression, PostgresTable, PostgresColumn> { |
| 73 | + TLPWhereGenerator<PostgresSelect, PostgresJoin, PostgresExpression, PostgresTable, PostgresColumn>, |
| 74 | + CERTGenerator<PostgresSelect, PostgresJoin, PostgresExpression, PostgresTable, PostgresColumn> { |
70 | 75 |
|
71 | 76 | private final int maxDepth; |
72 | 77 |
|
@@ -739,4 +744,137 @@ public String generateUnoptimizedQueryString(PostgresSelect select, PostgresExpr |
739 | 744 |
|
740 | 745 | return "SELECT SUM(count) FROM (" + select.asString() + ") as res"; |
741 | 746 | } |
| 747 | + |
| 748 | + @Override |
| 749 | + public String generateExplainQuery(PostgresSelect select) { |
| 750 | + return "EXPLAIN " + select.asString(); |
| 751 | + } |
| 752 | + |
| 753 | + @Override |
| 754 | + public boolean mutate(PostgresSelect select) { |
| 755 | + List<Function<PostgresSelect, Boolean>> mutators = new ArrayList<>(); |
| 756 | + |
| 757 | + mutators.add(this::mutateJoin); |
| 758 | + mutators.add(this::mutateWhere); |
| 759 | + mutators.add(this::mutateGroupBy); |
| 760 | + mutators.add(this::mutateHaving); |
| 761 | + if (!PostgresBugs.bug18643) { |
| 762 | + mutators.add(this::mutateAnd); |
| 763 | + mutators.add(this::mutateOr); |
| 764 | + } |
| 765 | + // mutators.add(this::mutateLimit); |
| 766 | + mutators.add(this::mutateDistinct); |
| 767 | + |
| 768 | + return Randomly.fromList(mutators).apply(select); |
| 769 | + } |
| 770 | + |
| 771 | + boolean mutateJoin(PostgresSelect select) { |
| 772 | + if (select.getJoinList().isEmpty()) { |
| 773 | + return false; |
| 774 | + } |
| 775 | + PostgresJoin join = (PostgresJoin) Randomly.fromList(select.getJoinList()); |
| 776 | + |
| 777 | + // Exclude CROSS for on condition |
| 778 | + if (join.getType() == PostgresJoinType.CROSS) { |
| 779 | + List<PostgresColumn> columns = new ArrayList<>(); |
| 780 | + columns.addAll(((PostgresTableReference) join.getLeftTable()).getTable().getColumns()); |
| 781 | + columns.addAll(((PostgresTableReference) join.getRightTable()).getTable().getColumns()); |
| 782 | + PostgresExpressionGenerator joinGen2 = new PostgresExpressionGenerator(globalState).setColumns(columns); |
| 783 | + join.setOnClause(joinGen2.generateExpression(0, PostgresDataType.BOOLEAN)); |
| 784 | + } |
| 785 | + |
| 786 | + PostgresJoinType newJoinType = PostgresJoinType.INNER; |
| 787 | + if (join.getType() == PostgresJoinType.LEFT || join.getType() == PostgresJoinType.RIGHT) { |
| 788 | + newJoinType = PostgresJoinType.getRandomExcept(PostgresJoinType.LEFT, PostgresJoinType.RIGHT); |
| 789 | + } else { |
| 790 | + newJoinType = PostgresJoinType.getRandomExcept(join.getType()); |
| 791 | + } |
| 792 | + boolean increase = join.getType().ordinal() < newJoinType.ordinal(); |
| 793 | + join.setType(newJoinType); |
| 794 | + if (newJoinType == PostgresJoinType.CROSS) { |
| 795 | + join.setOnClause(null); |
| 796 | + } |
| 797 | + return increase; |
| 798 | + } |
| 799 | + |
| 800 | + boolean mutateDistinct(PostgresSelect select) { |
| 801 | + PostgresSelect.SelectType selectType = select.getSelectOption(); |
| 802 | + if (selectType != PostgresSelect.SelectType.ALL) { |
| 803 | + select.setSelectType(PostgresSelect.SelectType.ALL); |
| 804 | + return true; |
| 805 | + } else { |
| 806 | + select.setSelectType(PostgresSelect.SelectType.DISTINCT); |
| 807 | + return false; |
| 808 | + } |
| 809 | + } |
| 810 | + |
| 811 | + boolean mutateWhere(PostgresSelect select) { |
| 812 | + boolean increase = select.getWhereClause() != null; |
| 813 | + if (increase) { |
| 814 | + select.setWhereClause(null); |
| 815 | + } else { |
| 816 | + select.setWhereClause(generateExpression(0, PostgresDataType.BOOLEAN)); |
| 817 | + } |
| 818 | + return increase; |
| 819 | + } |
| 820 | + |
| 821 | + boolean mutateGroupBy(PostgresSelect select) { |
| 822 | + boolean increase = select.getGroupByExpressions().size() > 0; |
| 823 | + if (increase) { |
| 824 | + select.clearGroupByExpressions(); |
| 825 | + } else { |
| 826 | + select.setGroupByExpressions(select.getFetchColumns()); |
| 827 | + } |
| 828 | + return increase; |
| 829 | + } |
| 830 | + |
| 831 | + boolean mutateHaving(PostgresSelect select) { |
| 832 | + if (select.getGroupByExpressions().size() == 0) { |
| 833 | + select.setGroupByExpressions(select.getFetchColumns()); |
| 834 | + select.setHavingClause(generateExpression(0, PostgresDataType.BOOLEAN)); |
| 835 | + return false; |
| 836 | + } else { |
| 837 | + if (select.getHavingClause() == null) { |
| 838 | + select.setHavingClause(generateExpression(0, PostgresDataType.BOOLEAN)); |
| 839 | + return false; |
| 840 | + } else { |
| 841 | + select.setHavingClause(null); |
| 842 | + return true; |
| 843 | + } |
| 844 | + } |
| 845 | + } |
| 846 | + |
| 847 | + boolean mutateAnd(PostgresSelect select) { |
| 848 | + if (select.getWhereClause() == null) { |
| 849 | + select.setWhereClause(generateExpression(0, PostgresDataType.BOOLEAN)); |
| 850 | + } else { |
| 851 | + PostgresExpression newWhere = new PostgresBinaryLogicalOperation(select.getWhereClause(), |
| 852 | + generateExpression(0, PostgresDataType.BOOLEAN), BinaryLogicalOperator.AND); |
| 853 | + select.setWhereClause(newWhere); |
| 854 | + } |
| 855 | + return false; |
| 856 | + } |
| 857 | + |
| 858 | + boolean mutateOr(PostgresSelect select) { |
| 859 | + if (select.getWhereClause() == null) { |
| 860 | + select.setWhereClause(generateExpression(0, PostgresDataType.BOOLEAN)); |
| 861 | + return false; |
| 862 | + } else { |
| 863 | + PostgresExpression newWhere = new PostgresBinaryLogicalOperation(select.getWhereClause(), |
| 864 | + generateExpression(0, PostgresDataType.BOOLEAN), BinaryLogicalOperator.OR); |
| 865 | + select.setWhereClause(newWhere); |
| 866 | + return true; |
| 867 | + } |
| 868 | + } |
| 869 | + |
| 870 | + boolean mutateLimit(PostgresSelect select) { |
| 871 | + boolean increase = select.getLimitClause() != null; |
| 872 | + if (increase) { |
| 873 | + select.setLimitClause(null); |
| 874 | + } else { |
| 875 | + Randomly r = new Randomly(); |
| 876 | + select.setLimitClause(PostgresConstant.createIntConstant((int) Math.abs(r.getInteger()))); |
| 877 | + } |
| 878 | + return increase; |
| 879 | + } |
742 | 880 | } |
0 commit comments