<commit_before>package istc.bigdawg.packages;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.collect.Sets;

import istc.bigdawg.catalog.CatalogViewer;
import istc.bigdawg.executor.plan.ExecutionNodeFactory;
import istc.bigdawg.executor.plan.QueryExecutionPlan;
import istc.bigdawg.plan.AFLQueryPlan;
import istc.bigdawg.plan.SQLQueryPlan;
import istc.bigdawg.plan.extract.AFLPlanParser;
import istc.bigdawg.plan.extract.SQLPlanParser;
import istc.bigdawg.plan.operators.Aggregate;
import istc.bigdawg.plan.operators.Distinct;
import istc.bigdawg.plan.operators.Join;
import istc.bigdawg.plan.operators.Join.JoinType;
import istc.bigdawg.plan.operators.Limit;
import istc.bigdawg.plan.operators.Operator;
import istc.bigdawg.plan.operators.Scan;
import istc.bigdawg.plan.operators.SeqScan;
import istc.bigdawg.plan.operators.Sort;
import istc.bigdawg.postgresql.PostgreSQLHandler;
import istc.bigdawg.properties.BigDawgConfigProperties;
import istc.bigdawg.query.ConnectionInfo;
import istc.bigdawg.query.DBHandler;
import istc.bigdawg.schema.DataObjectAttribute;
import istc.bigdawg.scidb.SciDBHandler;
import istc.bigdawg.signature.Signature;
import istc.bigdawg.signature.builder.ArraySignatureBuilder;
import istc.bigdawg.signature.builder.RelationalSignatureBuilder;
import istc.bigdawg.utils.IslandsAndCast;
import istc.bigdawg.utils.IslandsAndCast.Scope;
import istc.bigdawg.utils.sqlutil.SQLExpressionUtils;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.Select;

public class CrossIslandQueryNode {
	
	private IslandsAndCast.Scope scope;
	private String query;
	private Select select;
	private String name;
	private Signature signature;
	
	private Map<String, QueryContainerForCommonDatabase> queryContainer;
	private List<Operator> remainderPermutations;
	private List<String> remainderLoc; // if null then we need to look into the container
	
	private Set<String> children;
	private Matcher tagMatcher;
	private DBHandler dbSchemaHandler = null;
	 
	private Map<String, List<String>> originalMap;
	
	private Set<String> joinPredicates;
	private Set<String> joinFilters;
	
	private static final int  psqlSchemaHandlerDBID = BigDawgConfigProperties.INSTANCE.getPostgresSchemaServerDBID();
	private static final int  scidbSchemaHandlerDBID = BigDawgConfigProperties.INSTANCE.getSciDBSchemaServerDBID();
	
	
	public CrossIslandQueryNode (IslandsAndCast.Scope scope, String islandQuery, String name, Map<String, Operator> rootsForSchemas) throws Exception {
		this.scope = scope;
		this.query = islandQuery;
		this.name  = name;
		
		// collect the cross island children
		children = getCrossIslandChildrenReferences();
		
//		System.out.println("CrossIslandChildren: "+children.toString());
//		System.out.println("RootsForSchemas: "+rootsForSchemas.toString());
		
		System.out.println("Island query: " + islandQuery);
		
		// create new tables or arrays for planning use
		if (scope.equals(Scope.RELATIONAL)) {
			this.select = (Select) CCJSqlParserUtil.parse(islandQuery);
			dbSchemaHandler = new PostgreSQLHandler(psqlSchemaHandlerDBID);
			for (String key : rootsForSchemas.keySet()) {
				if (children.contains(key)) {
					System.out.println("key: "+key+"; query: "+rootsForSchemas.get(key).generateSQLCreateTableStatementLocally(key)+"\n\n");
					((PostgreSQLHandler)dbSchemaHandler).executeStatementPostgreSQL(rootsForSchemas.get(key)
							.generateSQLCreateTableStatementLocally(key));
				}
			}
		} else if (scope.equals(Scope.ARRAY)) {
			dbSchemaHandler = new SciDBHandler(scidbSchemaHandlerDBID);
			for (String key : rootsForSchemas.keySet()) {
				if (children.contains(key)) {
					System.out.println("key: "+key+"; query: "+rootsForSchemas.get(key).generateSQLCreateTableStatementLocally(key)+"\n\n");
					((SciDBHandler)dbSchemaHandler).executeStatement(rootsForSchemas.get(key)
							.generateAFLCreateArrayStatementLocally(key));
				}
			}
		} else
			throw new Exception("Unsupported island code : "+scope.toString());
		
		queryContainer = new HashMap<>();
		remainderPermutations = new ArrayList<>();
		remainderLoc = new ArrayList<>();
		joinPredicates = new HashSet<>();
		joinFilters = new HashSet<>();
		populateQueryContainer();
		
		
		this.signature = new Signature(islandQuery, scope, getRemainder(0), getQueryContainer());
		
		
		// removing temporary schema plates
		if (scope.equals(Scope.RELATIONAL)) {
			for (String key : rootsForSchemas.keySet()) 
				if (children.contains(key)) 
					((PostgreSQLHandler)dbSchemaHandler).executeStatementPostgreSQL("drop table "+key);
		} else if (scope.equals(Scope.ARRAY)) {
			for (String key : rootsForSchemas.keySet()) 
				if (children.contains(key)) 
					((SciDBHandler)dbSchemaHandler).executeStatement("remove("+key+")");
		} else
			throw new Exception("Unsupported island code : "+scope.toString());
		
		
	}
	
	public Set<String> getCrossIslandChildrenReferences() {<commit_msg>Added a method for debugging<commit_after>package istc.bigdawg.packages;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.collect.Sets;

import istc.bigdawg.catalog.CatalogViewer;
import istc.bigdawg.executor.plan.ExecutionNodeFactory;
import istc.bigdawg.executor.plan.QueryExecutionPlan;
import istc.bigdawg.plan.AFLQueryPlan;
import istc.bigdawg.plan.SQLQueryPlan;
import istc.bigdawg.plan.extract.AFLPlanParser;
import istc.bigdawg.plan.extract.SQLPlanParser;
import istc.bigdawg.plan.operators.Aggregate;
import istc.bigdawg.plan.operators.Distinct;
import istc.bigdawg.plan.operators.Join;
import istc.bigdawg.plan.operators.Join.JoinType;
import istc.bigdawg.plan.operators.Limit;
import istc.bigdawg.plan.operators.Operator;
import istc.bigdawg.plan.operators.Scan;
import istc.bigdawg.plan.operators.SeqScan;
import istc.bigdawg.plan.operators.Sort;
import istc.bigdawg.postgresql.PostgreSQLHandler;
import istc.bigdawg.properties.BigDawgConfigProperties;
import istc.bigdawg.query.ConnectionInfo;
import istc.bigdawg.query.DBHandler;
import istc.bigdawg.schema.DataObjectAttribute;
import istc.bigdawg.scidb.SciDBHandler;
import istc.bigdawg.signature.Signature;
import istc.bigdawg.signature.builder.ArraySignatureBuilder;
import istc.bigdawg.signature.builder.RelationalSignatureBuilder;
import istc.bigdawg.utils.IslandsAndCast;
import istc.bigdawg.utils.IslandsAndCast.Scope;
import istc.bigdawg.utils.sqlutil.SQLExpressionUtils;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.Select;

public class CrossIslandQueryNode {
	
	private IslandsAndCast.Scope scope;
	private String query;
	private Select select;
	private String name;
	private Signature signature;
	
	private Map<String, QueryContainerForCommonDatabase> queryContainer;
	private List<Operator> remainderPermutations;
	private List<String> remainderLoc; // if null then we need to look into the container
	
	private Set<String> children;
	private Matcher tagMatcher;
	private DBHandler dbSchemaHandler = null;
	 
	private Map<String, List<String>> originalMap;
	
	private Set<String> joinPredicates;
	private Set<String> joinFilters;
	
	private static final int  psqlSchemaHandlerDBID = BigDawgConfigProperties.INSTANCE.getPostgresSchemaServerDBID();
	private static final int  scidbSchemaHandlerDBID = BigDawgConfigProperties.INSTANCE.getSciDBSchemaServerDBID();
	
	
	public CrossIslandQueryNode (IslandsAndCast.Scope scope, String islandQuery, String name, Map<String, Operator> rootsForSchemas) throws Exception {
		this.scope = scope;
		this.query = islandQuery;
		this.name  = name;
		
		// collect the cross island children
		children = getCrossIslandChildrenReferences();
		
//		System.out.println("CrossIslandChildren: "+children.toString());
//		System.out.println("RootsForSchemas: "+rootsForSchemas.toString());
		
		System.out.println("Island query: " + islandQuery);
		
		// create new tables or arrays for planning use
		if (scope.equals(Scope.RELATIONAL)) {
			this.select = (Select) CCJSqlParserUtil.parse(islandQuery);
			dbSchemaHandler = new PostgreSQLHandler(psqlSchemaHandlerDBID);
			for (String key : rootsForSchemas.keySet()) {
				if (children.contains(key)) {
					System.out.println("key: "+key+"; query: "+rootsForSchemas.get(key).generateSQLCreateTableStatementLocally(key)+"\n\n");
					((PostgreSQLHandler)dbSchemaHandler).executeStatementPostgreSQL(rootsForSchemas.get(key)
							.generateSQLCreateTableStatementLocally(key));
				}
			}
		} else if (scope.equals(Scope.ARRAY)) {
			dbSchemaHandler = new SciDBHandler(scidbSchemaHandlerDBID);
			for (String key : rootsForSchemas.keySet()) {
				if (children.contains(key)) {
					System.out.println("key: "+key+"; query: "+rootsForSchemas.get(key).generateSQLCreateTableStatementLocally(key)+"\n\n");
					((SciDBHandler)dbSchemaHandler).executeStatement(rootsForSchemas.get(key)
							.generateAFLCreateArrayStatementLocally(key));
				}
			}
		} else
			throw new Exception("Unsupported island code : "+scope.toString());
		
		queryContainer = new HashMap<>();
		remainderPermutations = new ArrayList<>();
		remainderLoc = new ArrayList<>();
		joinPredicates = new HashSet<>();
		joinFilters = new HashSet<>();
		populateQueryContainer();
		
		
		this.signature = new Signature(islandQuery, scope, getRemainder(0), getQueryContainer());
		
		
		// removing temporary schema plates
		if (scope.equals(Scope.RELATIONAL)) {
			for (String key : rootsForSchemas.keySet()) 
				if (children.contains(key)) 
					((PostgreSQLHandler)dbSchemaHandler).executeStatementPostgreSQL("drop table "+key);
		} else if (scope.equals(Scope.ARRAY)) {
			for (String key : rootsForSchemas.keySet()) 
				if (children.contains(key)) 
					((SciDBHandler)dbSchemaHandler).executeStatement("remove("+key+")");
		} else
			throw new Exception("Unsupported island code : "+scope.toString());
		
		
	}

	public Set<String> getJoinPredicates(){
		return this.joinPredicates;
	}
	
	public Set<String> getCrossIslandChildrenReferences() {
{'fraction_non_alphanumeric': 0.1115733736762481, 'fraction_numerical': 0.00018910741301059002, 'mean_word_length': 5.606495940037477, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3401150', 'n_tokens_mistral': 3624, 'n_tokens_neox': 3324, 'n_words': 607}