<commit_before>/* This file is part of VoltDB.
 * Copyright (C) 2008-2019 VoltDB Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package org.voltdb.regressionsuites;

import java.util.Arrays;

import org.voltdb.BackendTarget;
import org.voltdb.VoltTable;
import org.voltdb.client.Client;
import org.voltdb.compiler.VoltProjectBuilder;
import org.voltdb_testprocs.regressionsuites.delete.DeleteOrderByLimit;
import org.voltdb_testprocs.regressionsuites.delete.DeleteOrderByLimitOffset;
import org.voltdb_testprocs.regressionsuites.fixedsql.Insert;

/**
 * System tests for DELETE
 * This is mostly cloned and modified from TestSqlUpdateSuite
 */

public class TestSqlDeleteSuite extends RegressionSuite {

    /** Procedures used by this suite */
    static final Class<?>[] MP_PROCEDURES = {
        DeleteOrderByLimit.class,
        DeleteOrderByLimitOffset.class
        };

    static final int ROWS = 10;

    private static void insertOneRow(Client client, String tableName,
            long id, String desc, long num, double ratio) throws Exception {
        VoltTable vt = client.callProcedure("@AdHoc",
                "insert into " + tableName + " values ("
                + id + ", '" + desc + "', " + num + ", " + ratio + ")")
                .getResults()[0];
        vt.advanceRow();
        assertEquals(vt.getLong(0), 1);
    }

    private static void insertRows(Client client, String tableName, int numRows)
            throws Exception {
        for (int i = 0; i < numRows; ++i) {
            insertOneRow(client, tableName, i, "desc", i, 14.5);
        }

    }

    private void executeAndTestDelete(String tableName, String deleteStmt, int numExpectedRowsChanged)
            throws Exception {

        Client client = getClient();

        insertRows(client, tableName, ROWS);

        VoltTable[] results = client.callProcedure("@AdHoc", deleteStmt).getResults();
        assertEquals(numExpectedRowsChanged, results[0].asScalarLong());

        int indexOfWhereClause = deleteStmt.toLowerCase().indexOf("where");
        String deleteWhereClause;
        if (indexOfWhereClause != -1) {
            deleteWhereClause = deleteStmt.substring(indexOfWhereClause);
        }
        else {
            deleteWhereClause = "";
        }

        // Get the full table reference, including alias if one exists. This is needed
        // because the where clause can contain aliases.
        int indexOfTableRef = deleteStmt.toLowerCase().indexOf(tableName.toLowerCase());
        String deleteTableRef;
        if (indexOfTableRef != -1) {
            if (indexOfWhereClause != -1) {
                deleteTableRef = deleteStmt.substring(indexOfTableRef, indexOfWhereClause - 1);
            }
            else {
                deleteTableRef = deleteStmt.substring(indexOfTableRef);
            }
        }
        else {
            deleteTableRef = "";
        }

        String query = String.format("select count(*) from %s %s",
                                      deleteTableRef, deleteWhereClause);
        results = client.callProcedure("@AdHoc", query).getResults();
        assertEquals(0, results[0].asScalarLong());
    }

    private static void insertMoreRows(Client client, String tableName,
            int tens, int ones) throws Exception {
        for (int i = 0; i < tens; ++i) {
            for (int j = 0; j < ones; ++j) {
                insertOneRow(client, tableName,
                        i * 10, "desc", i * 10 + j, 14.5);
            }
        }
    }

    public void testDelete()
            throws Exception
    {
        String[] tables = {"P1", "R1"};
        for (String table : tables)
        {
            String delete = String.format("delete from %s",
                    table, table);
            // Expect all rows to be deleted
            executeAndTestDelete(table, delete, ROWS);
        }
        verifyStmtFails(getClient(), "DELETE FROM P1 WHERE COUNT(*) = 1", "invalid WHERE expression");
    }

    public void testDeleteWithEqualToIndexPredicate()
            throws Exception
    {
        String[] tables = {"P1", "R1"};
        for (String table : tables)
        {
            String delete = String.format("delete from %s where %s.ID = 5",
                    table, table);
            // Only row with ID = 5 should be deleted
            executeAndTestDelete(table, delete, 1);
        }
    }
    
    public void testDeleteWithEqualToNonIndexPredicate()
            throws Exception<commit_msg>remove whitespaces on newline<commit_after>/* This file is part of VoltDB.
 * Copyright (C) 2008-2019 VoltDB Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package org.voltdb.regressionsuites;

import java.util.Arrays;

import org.voltdb.BackendTarget;
import org.voltdb.VoltTable;
import org.voltdb.client.Client;
import org.voltdb.compiler.VoltProjectBuilder;
import org.voltdb_testprocs.regressionsuites.delete.DeleteOrderByLimit;
import org.voltdb_testprocs.regressionsuites.delete.DeleteOrderByLimitOffset;
import org.voltdb_testprocs.regressionsuites.fixedsql.Insert;

/**
 * System tests for DELETE
 * This is mostly cloned and modified from TestSqlUpdateSuite
 */

public class TestSqlDeleteSuite extends RegressionSuite {

    /** Procedures used by this suite */
    static final Class<?>[] MP_PROCEDURES = {
        DeleteOrderByLimit.class,
        DeleteOrderByLimitOffset.class
        };

    static final int ROWS = 10;

    private static void insertOneRow(Client client, String tableName,
            long id, String desc, long num, double ratio) throws Exception {
        VoltTable vt = client.callProcedure("@AdHoc",
                "insert into " + tableName + " values ("
                + id + ", '" + desc + "', " + num + ", " + ratio + ")")
                .getResults()[0];
        vt.advanceRow();
        assertEquals(vt.getLong(0), 1);
    }

    private static void insertRows(Client client, String tableName, int numRows)
            throws Exception {
        for (int i = 0; i < numRows; ++i) {
            insertOneRow(client, tableName, i, "desc", i, 14.5);
        }

    }

    private void executeAndTestDelete(String tableName, String deleteStmt, int numExpectedRowsChanged)
            throws Exception {

        Client client = getClient();

        insertRows(client, tableName, ROWS);

        VoltTable[] results = client.callProcedure("@AdHoc", deleteStmt).getResults();
        assertEquals(numExpectedRowsChanged, results[0].asScalarLong());

        int indexOfWhereClause = deleteStmt.toLowerCase().indexOf("where");
        String deleteWhereClause;
        if (indexOfWhereClause != -1) {
            deleteWhereClause = deleteStmt.substring(indexOfWhereClause);
        }
        else {
            deleteWhereClause = "";
        }

        // Get the full table reference, including alias if one exists. This is needed
        // because the where clause can contain aliases.
        int indexOfTableRef = deleteStmt.toLowerCase().indexOf(tableName.toLowerCase());
        String deleteTableRef;
        if (indexOfTableRef != -1) {
            if (indexOfWhereClause != -1) {
                deleteTableRef = deleteStmt.substring(indexOfTableRef, indexOfWhereClause - 1);
            }
            else {
                deleteTableRef = deleteStmt.substring(indexOfTableRef);
            }
        }
        else {
            deleteTableRef = "";
        }

        String query = String.format("select count(*) from %s %s",
                                      deleteTableRef, deleteWhereClause);
        results = client.callProcedure("@AdHoc", query).getResults();
        assertEquals(0, results[0].asScalarLong());
    }

    private static void insertMoreRows(Client client, String tableName,
            int tens, int ones) throws Exception {
        for (int i = 0; i < tens; ++i) {
            for (int j = 0; j < ones; ++j) {
                insertOneRow(client, tableName,
                        i * 10, "desc", i * 10 + j, 14.5);
            }
        }
    }

    public void testDelete()
            throws Exception
    {
        String[] tables = {"P1", "R1"};
        for (String table : tables)
        {
            String delete = String.format("delete from %s",
                    table, table);
            // Expect all rows to be deleted
            executeAndTestDelete(table, delete, ROWS);
        }
        verifyStmtFails(getClient(), "DELETE FROM P1 WHERE COUNT(*) = 1", "invalid WHERE expression");
    }

    public void testDeleteWithEqualToIndexPredicate()
            throws Exception
    {
        String[] tables = {"P1", "R1"};
        for (String table : tables)
        {
            String delete = String.format("delete from %s where %s.ID = 5",
                    table, table);
            // Only row with ID = 5 should be deleted
            executeAndTestDelete(table, delete, 1);
        }
    }

    public void testDeleteWithEqualToNonIndexPredicate()
            throws Exception
{'fraction_non_alphanumeric': 0.09224623206827674, 'fraction_numerical': 0.00762665698202288, 'mean_word_length': 2.4443402126328957, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4154159', 'n_tokens_mistral': 2971, 'n_tokens_neox': 2871, 'n_words': 962}