Exemple #1
0
    def test_const_expr_role(self):
        t = true()
        is_(expect(roles.ConstExprRole, t), t)

        f = false()
        is_(expect(roles.ConstExprRole, f), f)

        is_instance_of(expect(roles.ConstExprRole, True), True_)

        is_instance_of(expect(roles.ConstExprRole, False), False_)

        is_instance_of(expect(roles.ConstExprRole, None), Null)
Exemple #2
0
    def test_anonymized_from_clause_role(self):
        is_true(expect(roles.AnonymizedFromClauseRole, t).compare(t.alias()))

        # note the compare for subquery().alias(), even if it is two
        # plain Alias objects (which it won't be once we introduce the
        # Subquery class), still compares based on alias() being present
        # twice, that is, alias().alias() builds an alias of an alias, rather
        # than just replacing the outer alias.
        is_true(
            expect(
                roles.AnonymizedFromClauseRole, select([t]).subquery()
            ).compare(select([t]).subquery().alias())
        )
Exemple #3
0
 def test_strict_from_clause_role(self):
     stmt = select([t]).subquery()
     is_true(
         expect(roles.StrictFromClauseRole, stmt).compare(
             select([t]).subquery()
         )
     )
    def test_labeled_role(self):
        stmt = select([table1.c.myid])

        with testing.expect_deprecated(
            "coercing SELECT object to scalar "
            "subquery in a column-expression context is deprecated"
        ):
            coerced = coercions.expect(roles.LabeledColumnExprRole, stmt)
            is_true(coerced.compare(stmt.scalar_subquery().label(None)))

        with testing.expect_deprecated(
            "coercing SELECT object to scalar "
            "subquery in a column-expression context is deprecated"
        ):
            coerced = coercions.expect(
                roles.LabeledColumnExprRole, stmt.alias()
            )
            is_true(coerced.compare(stmt.scalar_subquery().label(None)))
    def test_fromclause_subquery(self):
        stmt = select([table1.c.myid])
        with testing.expect_deprecated(
            "Implicit coercion of SELECT and textual SELECT constructs "
            "into FROM clauses is deprecated"
        ):
            coerced = coercions.expect(
                roles.StrictFromClauseRole, stmt, allow_select=True
            )

        is_true(coerced.compare(stmt.subquery()))
Exemple #6
0
    def test_labeled_column_expr_role(self):
        c = column("q")
        is_true(expect(roles.LabeledColumnExprRole, c).compare(c))

        is_true(
            expect(roles.LabeledColumnExprRole, c.label("foo")).compare(
                c.label("foo")
            )
        )

        is_true(
            expect(
                roles.LabeledColumnExprRole,
                select([column("q")]).scalar_subquery(),
            ).compare(select([column("q")]).label(None))
        )

        is_true(
            expect(roles.LabeledColumnExprRole, not_a_thing1).compare(
                literal(not_a_thing1).label(None)
            )
        )
    def test_column_roles(self):
        stmt = select([table1.c.myid])

        for role in [
            roles.WhereHavingRole,
            roles.ExpressionElementRole,
            roles.ByOfRole,
            roles.OrderByRole,
            # roles.LabeledColumnExprRole
        ]:
            with testing.expect_deprecated(
                "coercing SELECT object to scalar "
                "subquery in a column-expression context is deprecated"
            ):
                coerced = coercions.expect(role, stmt)
                is_true(coerced.compare(stmt.scalar_subquery()))

            with testing.expect_deprecated(
                "coercing SELECT object to scalar "
                "subquery in a column-expression context is deprecated"
            ):
                coerced = coercions.expect(role, stmt.alias())
                is_true(coerced.compare(stmt.scalar_subquery()))
Exemple #8
0
 def __init__(self, expression):
     self.clause = coercions.expect(roles.ExpressionElementRole,
                                    expression)
Exemple #9
0
 def test_statement_text_coercion(self):
     with testing.expect_deprecated_20(
             "Using plain strings to indicate SQL statements"):
         is_true(
             expect(roles.StatementRole, "select * from table").compare(
                 text("select * from table")))
    def visit_create_index(self, create):
        preparer = self.preparer
        index = create.element
        self._verify_index_table(index)
        text = "CREATE "
        if index.unique:
            distributed_by = index.table.dialect_options['greenplum'][
                'distributed_by']
            # logger.info('table_cols={}'.format(str(index.table.columns)))
            if distributed_by is None:
                primary_key_cols = [c.name for c in index.table.primary_key]
                logger.info('primary_key_cols={}'.format(
                    str(primary_key_cols)))
                distributed_by_cols = primary_key_cols
                if len(distributed_by_cols) == 0:
                    distributed_by_cols = [index.table.columns[0]]
            elif distributed_by == 'RANDOM':
                distributed_by_cols = []
            else:
                distributed_by_cols = distributed_by.split(',')
            logger.info('distributed_by_cols={}'.format(
                str(distributed_by_cols)))
            index_cols = [c.name for c in index.columns]
            logger.info('index_cols={}'.format(str(index_cols)))
            if set(index_cols).issuperset(set(distributed_by_cols)):
                text += "UNIQUE "
        text += "INDEX "

        if self.dialect._supports_create_index_concurrently:
            concurrently = index.dialect_options['greenplum']['concurrently']
            if concurrently:
                text += "CONCURRENTLY "

        text += "%s ON %s " % (self._prepared_index_name(
            index, include_schema=False), preparer.format_table(index.table))

        using = index.dialect_options['greenplum']['using']
        if using:
            text += ("USING %s " % self.preparer.validate_sql_phrase(
                using, base.IDX_USING).lower())

        ops = index.dialect_options["greenplum"]["ops"]
        text += "(%s)" \
                % (
                    ', '.join([
                        self.sql_compiler.process(
                            expr.self_group()
                            if not isinstance(expr, expression.ColumnClause)
                            else expr,
                            include_table=False,
                            literal_binds=True,
                        )
                        + (
                            (' ' + ops[expr.key])
                            if hasattr(expr, 'key')
                            and expr.key in ops else ''
                        )
                        for expr in index.expressions
                    ])
                )

        withclause = index.dialect_options['greenplum']['with']

        if withclause:
            text += " WITH (%s)" % (', '.join([
                '%s = %s' % storage_parameter
                for storage_parameter in withclause.items()
            ]))

        tablespace_name = index.dialect_options['greenplum']['tablespace']

        if tablespace_name:
            text += " TABLESPACE %s" % preparer.quote(tablespace_name)

        whereclause = index.dialect_options["greenplum"]["where"]

        if whereclause is not None:
            whereclause = coercions.expect(roles.DDLExpressionRole,
                                           whereclause)
            where_compiled = self.sql_compiler.process(
                whereclause,
                include_table=False,
                literal_binds=True,
            )
            text += " WHERE " + where_compiled
        return text
Exemple #11
0
 def test_statement_coercion_sequence(self):
     s1 = Sequence("hi")
     is_(expect(roles.CoerceTextStatementRole, s1), s1)
Exemple #12
0
 def test_columns_clause_role(self):
     is_(expect(roles.ColumnsClauseRole, t.c.q), t.c.q)
Exemple #13
0
 def test_statement_coercion_ddl(self):
     d1 = DDL("hi")
     is_(expect(roles.CoerceTextStatementRole, d1), d1)
Exemple #14
0
 def test_statement_text_coercion(self):
     is_true(
         expect(roles.CoerceTextStatementRole,
                "select * from table").compare(text("select * from table")))
Exemple #15
0
 def test_strict_from_clause_role(self):
     stmt = select(t).subquery()
     is_true(
         expect(roles.StrictFromClauseRole,
                stmt).compare(select(t).subquery()))
Exemple #16
0
 def test_statement_coercion_ddl(self):
     d1 = DDL("hi")
     is_(expect(roles.CoerceTextStatementRole, d1), d1)
Exemple #17
0
 def test_text_as_from_select_statement(self):
     is_true(
         expect(
             roles.SelectStatementRole,
             text("select * from table").columns(t.c.q),
         ).compare(text("select * from table").columns(t.c.q)))
Exemple #18
0
 def go(x):
     return coercions.expect(roles.WhereHavingRole, lambda: t1.c.q == x)
Exemple #19
0
 def test_truncated_label_role(self):
     is_instance_of(
         expect(roles.TruncatedLabelRole, "foobar"), _truncated_label
     )
Exemple #20
0
 def test_columns_clause_role(self):
     is_(expect(roles.ColumnsClauseRole, t.c.q), t.c.q)
Exemple #21
0
 def test_statement_coercion_sequence(self):
     s1 = Sequence("hi")
     is_(expect(roles.CoerceTextStatementRole, s1), s1)
Exemple #22
0
 def test_truncated_label_role(self):
     is_instance_of(expect(roles.TruncatedLabelRole, "foobar"),
                    _truncated_label)
Exemple #23
0
 def test_statement_text_coercion(self):
     is_true(
         expect(
             roles.CoerceTextStatementRole, "select * from table"
         ).compare(text("select * from table"))
     )
Exemple #24
0
 def test_statement_coercion_select(self):
     is_true(
         expect(roles.CoerceTextStatementRole,
                select(t)).compare(select(t)))
Exemple #25
0
 def test_statement_coercion_select(self):
     is_true(
         expect(roles.CoerceTextStatementRole, select([t])).compare(
             select([t])
         )
     )