package sqlancer.clickhouse.gen;

import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

import sqlancer.clickhouse.ClickHouseErrors;
import sqlancer.clickhouse.ClickHouseProvider.ClickHouseGlobalState;
import sqlancer.clickhouse.ClickHouseSchema.ClickHouseColumn;
import sqlancer.clickhouse.ClickHouseSchema.ClickHouseTable;
import sqlancer.clickhouse.ClickHouseToStringVisitor;
import sqlancer.common.gen.AbstractInsertGenerator;
import sqlancer.common.query.ExpectedErrors;
import sqlancer.common.query.SQLQueryAdapter;

public class ClickHouseInsertGenerator extends AbstractInsertGenerator<ClickHouseColumn> {

    private final ClickHouseGlobalState globalState;
    private final ExpectedErrors errors = new ExpectedErrors();
    private final ClickHouseExpressionGenerator gen;

    public ClickHouseInsertGenerator(ClickHouseGlobalState globalState) {
        this.globalState = globalState;
        gen = new ClickHouseExpressionGenerator(globalState);
        errors.add("Cannot insert NULL value into a column of type");
        errors.add("Memory limit");
        errors.add("Cannot parse string");
        errors.add("Cannot parse Int32 from String, because value is too short");
        errors.add("does not return a value of type UInt8");
        ClickHouseErrors.addExpectedExpressionErrors(errors);
    }

    public static SQLQueryAdapter getQuery(ClickHouseGlobalState globalState) throws SQLException {
        return new ClickHouseInsertGenerator(globalState).get();
    }

    private SQLQueryAdapter get() {
        ClickHouseTable table = globalState.getSchema().getRandomTable(t -> !t.isView());
        List<ClickHouseColumn> columns = table.getRandomNonEmptyColumnSubset().stream()
                .filter(c -> !c.isAlias() && !c.isMaterialized()).collect(Collectors.toList());
        sb.append("INSERT INTO ");
        sb.append(table.getName());
        sb.append("(");
        sb.append(columns.stream().map(c -> c.getName()).collect(Collectors.joining(", ")));
        sb.append(")");
        sb.append(" VALUES ");
        insertColumns(columns);
        return new SQLQueryAdapter(sb.toString(), errors);
    }

    @Override
    protected void insertValue(ClickHouseColumn column) {
        String s = ClickHouseToStringVisitor.asString(gen.generateConstant(column.getType()));
        sb.append(s);
    }

}

@@ -57,16 +57,46 @@
 eption;%0A
+import java.util.Collections;%0A
 import j
@@ -1720,16 +1720,99 @@
 olumns =
+ Collections.emptyList();%0A        while (columns.isEmpty()) %7B%0A            columns =
  table.g
@@ -1854,25 +1854,8 @@
 am()
-%0A                
 .fil
@@ -1899,16 +1899,37 @@
 lized())
+%0A                    
 .collect
@@ -1947,24 +1947,34 @@
 .toList());%0A
+        %7D%0A
         sb.a

Fix ClickHouse Insert Generator error when empty column list was used