Exemplo n.º 1
0
def check_rename_column_when_privilege_is_granted(table,
                                                  user,
                                                  node,
                                                  column=None):
    """Ensures RENAME COLUMN runs as expected when the privilege is granted
    to the specified user.
    """
    if column is None:
        column = "rename"

    new_column = f"{column}_new"

    with Given(f"I add the column {column}"):
        node.query(f"ALTER TABLE {table} ADD COLUMN {column} String")

    with And("I get the initial contents of the column"):
        # could be either str or float depending on MODIFY COLUMN
        initial_column_data = node.query(
            f"SELECT {column} FROM {table} ORDER BY {column}"
            " FORMAT JSONEachRow").output

    with When(f"I rename column '{column}' to '{new_column}'"):
        node.query(
            f"ALTER TABLE {table} RENAME COLUMN {column} TO {new_column}",
            settings=[("user", user)],
        )

    with Then("I verify that the column was successfully renamed"):
        with When("I verify that the original column does not exist"):
            exitcode, message = errors.missing_columns(column)
            node.query(
                f"SELECT {column} FROM {table} FORMAT JSONEachRow",
                exitcode=exitcode,
                message=message,
            )

        with And(
                "I verify that the new column does exist as expected, with same values"
        ):
            new_column_data = node.query(
                f"SELECT {new_column} FROM {table} ORDER BY"
                f" {new_column} FORMAT JSONEachRow").output

            if initial_column_data == "":
                assert initial_column_data == new_column_data, error()
            else:
                new_column_data_list = new_column_data.split("\n")
                initial_column_data_list = initial_column_data.split("\n")

                for new, initial in zip(new_column_data_list,
                                        initial_column_data_list):
                    assert (json.loads(new)[new_column] == json.loads(initial)
                            [column]), error()

    with Finally(f"I use default user to undo rename"):
        node.query(
            f"ALTER TABLE {table} RENAME COLUMN {new_column} TO {column}")

    with Finally(f"I drop column '{column}'"):
        node.query(f"ALTER TABLE {table} DROP COLUMN {column}")
Exemplo n.º 2
0
def check_order_by_when_privilege_is_granted(table, user, node):
    """Ensures ORDER BY runs as expected when the privilege is granted to the specified user
    """
    column = "order"

    with Given("I run sanity check"):
        node.query(f"ALTER TABLE {table} MODIFY ORDER BY b", settings = [("user", user)])

    with And("I add new column and modify order using that column"):
        node.query(f"ALTER TABLE {table} ADD COLUMN {column} UInt32, MODIFY ORDER BY (b, {column})")

    with When(f"I insert random data into the ordered-by column {column}"):
        data = random.sample(range(1,1000),100)
        values = ', '.join(f'({datum})' for datum in data)
        node.query(f"INSERT INTO {table}({column}) VALUES {values}")

    with Then("I synchronize with optimize table"):
        node.query(f"OPTIMIZE TABLE {table} final")

    with And("I verify that the added data is ordered in the table"):
        data.sort()
        note(data)
        column_data = node.query(f"SELECT {column} FROM {table} FORMAT JSONEachRow").output
        column_data = column_data.split('\n')
        for row, datum in zip(column_data[:10], data[:10]):
            assert json.loads(row) == {column:datum}, error()

    with And("I verify that the sorting key is present in the table"):
        output = json.loads(node.query(f"SHOW CREATE TABLE {table} FORMAT JSONEachRow").output)
        assert f"ORDER BY (b, {column})" in output['statement'], error()

    with But(f"I cannot drop the required column {column}"):
        exitcode, message = errors.missing_columns(column)
        node.query(f"ALTER TABLE {table} DROP COLUMN {column}",
            exitcode=exitcode, message=message)
Exemplo n.º 3
0
def check_sample_by_when_privilege_is_granted(table, user, node):
    """Ensures SAMPLE BY runs as expected when the privilege is granted to the specified user
    """
    column = 'sample'

    with Given(f"I add new column {column}"):
        node.query(f"ALTER TABLE {table} ADD COLUMN {column} UInt32")

    with When(f"I add sample by clause"):
        node.query(f"ALTER TABLE {table} MODIFY SAMPLE BY (d, {column})",
                   settings=[("user", user)])

    with Then("I verify that the sample is in the table"):
        output = json.loads(
            node.query(f"SHOW CREATE TABLE {table} FORMAT JSONEachRow").output)
        assert f"SAMPLE BY (d, {column})" in output['statement'], error()

    with But(f"I cannot drop the required column {column}"):
        exitcode, message = errors.missing_columns(column)
        node.query(f"ALTER TABLE {table} DROP COLUMN {column}",
                   exitcode=exitcode,
                   message=message)