Create a random query using various language features. The initial call to this method should only use tables in the table_exprs parameter, and not inline views or WITH definitions. The other types of table exprs may be added as part of the query generation. Due to imp
(self,
table_exprs,
allow_with_clause=True,
allow_union_clause=True,
select_item_data_types=None,
required_select_item_type=None,
required_table_expr_col_type=None,
require_aggregate=None,
dml_table=None,
table_alias_prefix='t')
| 95 | self.max_nested_query_count = None |
| 96 | |
| 97 | def generate_statement(self, |
| 98 | table_exprs, |
| 99 | allow_with_clause=True, |
| 100 | allow_union_clause=True, |
| 101 | select_item_data_types=None, |
| 102 | required_select_item_type=None, |
| 103 | required_table_expr_col_type=None, |
| 104 | require_aggregate=None, |
| 105 | dml_table=None, |
| 106 | table_alias_prefix='t'): |
| 107 | '''Create a random query using various language features. |
| 108 | |
| 109 | The initial call to this method should only use tables in the table_exprs |
| 110 | parameter, and not inline views or WITH definitions. The other types of |
| 111 | table exprs may be added as part of the query generation. |
| 112 | |
| 113 | Due to implementation limitations, nested queries are not distributed evenly by |
| 114 | default even if the query profile assigns an equal likelihood of use to each |
| 115 | possible nested query decision. This is because the implementation chooses nested |
| 116 | queries in a fixed order (WITH -> FROM -> WHERE). For example if only one nested |
| 117 | query were allowed and each clause had a 50% chance of using a subquery, the |
| 118 | resulting set of generated queries would contain a subquery in the WITH clause |
| 119 | 50% of the time, in the FROM 25% of the time, and WHERE 12.5% of the time. If the |
| 120 | max nested queries were two, it's most likely that the generated query would |
| 121 | contain a WITH clause which has the second nested query within it.... |
| 122 | |
| 123 | If select_item_data_types is specified it must be a sequence or iterable of |
| 124 | DataType. The generated query.select_clause.select will have data types suitable |
| 125 | for use in a UNION. |
| 126 | |
| 127 | required_select_item_type may be set to a DataType to force at least one of the |
| 128 | SELECT items to be of the given type. This can be used to ensure that inline |
| 129 | views will have at least one joinable column. |
| 130 | |
| 131 | required_table_expr_col_type may be set to ensure that at least one of the |
| 132 | TableExprs used in the FROM clause will have a column of the given type. This |
| 133 | can be used to unsure that a correlation condition can be made for a Subquery. |
| 134 | |
| 135 | require_aggregate can be set to True or False to force or disable the creation |
| 136 | of an aggregate query. This is used during Subquery creation where the context |
| 137 | may require an aggregate or non-aggregate. |
| 138 | |
| 139 | dml_table exists for kwargs compatibility with other statement generators but is |
| 140 | ignored |
| 141 | ''' |
| 142 | if not table_exprs: |
| 143 | raise Exception("At least one TableExpr is needed") |
| 144 | |
| 145 | query = Query() |
| 146 | query.parent = self.current_query |
| 147 | self.queries_under_construction.append(query) |
| 148 | self.profile.query = query |
| 149 | |
| 150 | # Make a copy so tables can be added if a WITH clause is used |
| 151 | table_exprs = TableExprList(table_exprs) |
| 152 | |
| 153 | if self.max_nested_query_count is None: |
| 154 | self.max_nested_query_count = self.profile.get_max_nested_query_count() |