/* This file is part of VoltDB.
 * Copyright (C) 2008-2013 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.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import org.voltdb.BackendTarget;
import org.voltdb.client.ProcCallException;
import org.voltdb.compiler.VoltProjectBuilder;


public class TestPlansGroupByComplexMaterializedViewSuite extends RegressionSuite {

    public void testTest() throws IOException, ProcCallException {
    }

    //
    // Suite builder boilerplate
    //

    public TestPlansGroupByComplexMaterializedViewSuite(String name) {
        super(name);
    }

    static public junit.framework.Test suite() throws Exception {
        VoltServerConfig config = null;
        MultiConfigSuiteBuilder builder = new MultiConfigSuiteBuilder(
                TestPlansGroupByComplexMaterializedViewSuite.class);
        ByteArrayOutputStream capturer = new ByteArrayOutputStream();
        PrintStream capturing = new PrintStream(capturer);

        VoltProjectBuilder project1 = new VoltProjectBuilder();
        project1.setCompilerDebugPrintStream(capturing);
        String literalSchema =
                "CREATE TABLE F ( " +
                "F_PKEY INTEGER NOT NULL, " +
                "F_D1   INTEGER NOT NULL, " +
                "F_D2   INTEGER NOT NULL, " +
                "F_D3   INTEGER NOT NULL, " +
                "F_VAL1 INTEGER NOT NULL, " +
                "F_VAL2 INTEGER NOT NULL, " +
                "F_VAL3 INTEGER NOT NULL, " +
                "PRIMARY KEY (F_PKEY) ); " +

                "CREATE VIEW V1 (V_D1_PKEY, V_D2_PKEY, V_D3_PKEY, V_F_PKEY, CNT, SUM_V1, SUM_V2, SUM_V3) " +
                "AS SELECT F_D1, F_D2, F_D3, F_PKEY, COUNT(*) + 1, SUM(F_VAL1), SUM(F_VAL2), SUM(F_VAL3) " +
                "FROM F  GROUP BY F_D1, F_D2, F_D3, F_PKEY;"
                ;
        try {
            project1.addLiteralSchema(literalSchema);
        } catch (IOException e) {
            assertFalse(true);
        }

        config = new LocalCluster("plansgroupby-onesite.jar", 1, 1, 0, BackendTarget.NATIVE_EE_JNI);
        boolean success = config.compile(project1);

        assertFalse(success);
        String captured = capturer.toString("UTF-8");
        String[] lines = captured.split("\n");

        assertTrue(foundLineMatching(lines, ".*V1.*complex aggregation expressions not supported.*"));

        //
        VoltProjectBuilder project2 = new VoltProjectBuilder();
        project2.setCompilerDebugPrintStream(capturing);
        literalSchema =
                "CREATE TABLE F ( " +
                "F_PKEY INTEGER NOT NULL, " +
                "F_D1   INTEGER NOT NULL, " +
                "F_D2   INTEGER NOT NULL, " +
                "F_D3   INTEGER NOT NULL, " +
                "F_VAL1 INTEGER NOT NULL, " +
                "F_VAL2 INTEGER NOT NULL, " +
                "F_VAL3 INTEGER NOT NULL, " +
                "PRIMARY KEY (F_PKEY) ); " +

                "CREATE VIEW V2 (V2_F_D1, V2_F_D2) " +
                "AS SELECT ABS(F_D1), SUM(F_D2) FROM F  GROUP BY ABS(F_D1);"
                ;
        try {
            project2.addLiteralSchema(literalSchema);
        } catch (IOException e) {
            assertFalse(true);
        }
        config = new LocalCluster("plansgroupby-onesite.jar", 1, 1, 0, BackendTarget.NATIVE_EE_JNI);
        success = config.compile(project2);

        assertFalse(success);
        captured = capturer.toString("UTF-8");
        lines = captured.split("\n");
        assertTrue(foundLineMatching(lines, ".*V2.*GROUP BY with complex expressions not supported.*"));


        builder.addServerConfig(config);

//        config = new LocalCluster("plansgroupby-hsql.jar", 1, 1, 0, BackendTarget.HSQLDB_BACKEND);
//        success = config.compile(project);
//        assertTrue(success);
//        builder.addServerConfig(config);
//
//        // Cluster
//        config = new LocalCluster("plansgroupby-cluster.jar", 2, 3, 1, BackendTarget.NATIVE_EE_JNI);
//        success = config.compile(project);
//        assertTrue(success);

        return builder;
    }
    static private boolean foundLineMatching(String[] lines, String pattern) {
        for (String string : lines) {
            if (string.matches(pattern)) {
                return true;
            }
        }
        return false;
    }
}

@@ -4679,16 +4679,923 @@
 *%22));%0A%0A%0A
+        VoltProjectBuilder project3 = new VoltProjectBuilder();%0A        literalSchema =%0A                %22CREATE TABLE R1 ( %22 +%0A                %22ID INTEGER DEFAULT '0' NOT NULL, %22 +%0A                %22WAGE INTEGER, %22 +%0A                %22DEPT INTEGER, %22 +%0A                %22TM TIMESTAMP DEFAULT NULL, %22 +%0A                %22PRIMARY KEY (ID) );%22 +%0A                %22CREATE TABLE P1 ( %22 +%0A                %22ID INTEGER DEFAULT '0' NOT NULL, %22 +%0A                %22WAGE INTEGER, %22 +%0A                %22DEPT INTEGER, %22 +%0A                %22TM TIMESTAMP DEFAULT NULL, %22 +%0A                %22PRIMARY KEY (ID) );%22;%0A        try %7B%0A            project3.addLiteralSchema(literalSchema);%0A        %7D catch (IOException e) %7B%0A            assertFalse(true);%0A        %7D%0A%0A        config = new LocalCluster(%22plansgroupby-onesite.jar%22, 1, 1, 0, BackendTarget.NATIVE_EE_JNI);%0A        success = config.compile(project3);%0A        assertTrue(success);%0A
         

Fix the materialized view expected failed test case