<commit_before>package query

import (
	"encoding/json"
	// "fmt"
	"github.com/adam-hanna/PDQdb/data"
	error_ "github.com/adam-hanna/PDQdb/error"
	"github.com/adam-hanna/PDQdb/index"
	"github.com/adam-hanna/arrayOperations"
	"log"
	"net/http"
)

// this struct holds all of the possible query attributes
type queryStruct struct {
	SELECT  []string               `json:"SELECT"`
	COUNT   string                 `json:"COUNT"`
	WHERE   map[string]interface{} `json:"WHERE"`
	GROUPBY string                 `json:"GROUP BY"`
}

func QueryDB(req *http.Request) interface{} {
	// get a variable ready to hold the incoming query
	var query queryStruct

	// read the incoming html post body and hold it in the query var made above
	decoder := json.NewDecoder(req.Body)

	err := decoder.Decode(&query)
	if err != nil {
		log.Panic(err)
	}

	// what type of query is this, an aggregation or is data being returned?
	switch {
	case len(query.SELECT) == 0 && query.COUNT != "":
		// they want a count!
		return countQuery(query)

	case len(query.SELECT) > 0 && query.COUNT == "":
		// they want some data returned!
		return selectQuery(query)

	default:
		log.Panic(error_.New("Not a valid query! Valid queries must have one of SELECT / COUNT!"))
	}

	// should never get here!
	// NOTE(@adam-hanna): need to implement proper error handling!
	return nil
}

// NOTE(@adam-hanna): do error handling!
func countQuery(query queryStruct) map[string]int {
	// make a map to hold the return
	mReturn := make(map[string]int)
	// make a slice to hold the idx's that match the query
	tempKeysMatched := make([][]uint64, 0)

	// is a group by present?
	switch query.GROUPBY {
	case "":
		// nope, no group by
		// loop through the "WHERE" key/vals. No logical operator (i.e. and / or) means "and" (i.e. intersect not union)
		// write the matching keys to our multi-dimensional array
		// NOTE(@adam-hanna): what if a field in the where is an ID or not an indexed field?
		// NOTE(@adam-hanna): this should be a private function. It will be used many times.
		evalWhereClause(query.WHERE, &tempKeysMatched)

		// find the intersection of the keys
		finalKeysMatched := arrayOperations.IntersectUint64Arr(tempKeysMatched)

		// write to the output map
		mReturn["COUNT"] = len(finalKeysMatched)

	default:
		// yup, there's a group by!
		// first, grab the unique vals and their locations in the data set of the group-by col
		groupByIndex := index.GetIndexByColName(query.GROUPBY)

		// Next, loop through the "WHERE" key/vals. No logical operator (i.e. and / or) means "and" (i.e. intersect not union)
		// write the matching keys to our multi-dimensional array
		// NOTE(@adam-hanna): what if a field in the where is an ID or not an indexed field?
		// NOTE(@adam-hanna): this should be a private function. It will be used many times.
		evalWhereClause(query.WHERE, &tempKeysMatched)

		// lastly, find the intersection of the where and groupby idx's
		for key, val := range groupByIndex {
			// push the idx's of the index to the keys matched in the whereby
			tempKeysMatched = append(tempKeysMatched, val)

			// find the intersection
			mReturn[key] = len(arrayOperations.IntersectUint64Arr(tempKeysMatched))

			// pop the idx's of the index out of the whereby idx's to get ready for the next index idx's
			tempKeysMatched = tempKeysMatched[:len(tempKeysMatched)-1]<commit_msg>changed how arrays were intersected in the group by, hopefully improving performance<commit_after>package query

import (
	"encoding/json"
	// "fmt"
	"github.com/adam-hanna/PDQdb/data"
	error_ "github.com/adam-hanna/PDQdb/error"
	"github.com/adam-hanna/PDQdb/index"
	"github.com/adam-hanna/arrayOperations"
	"log"
	"net/http"
)

// this struct holds all of the possible query attributes
type queryStruct struct {
	SELECT  []string               `json:"SELECT"`
	COUNT   string                 `json:"COUNT"`
	WHERE   map[string]interface{} `json:"WHERE"`
	GROUPBY string                 `json:"GROUP BY"`
}

func QueryDB(req *http.Request) interface{} {
	// get a variable ready to hold the incoming query
	var query queryStruct

	// read the incoming html post body and hold it in the query var made above
	decoder := json.NewDecoder(req.Body)

	err := decoder.Decode(&query)
	if err != nil {
		log.Panic(err)
	}

	// what type of query is this, an aggregation or is data being returned?
	switch {
	case len(query.SELECT) == 0 && query.COUNT != "":
		// they want a count!
		return countQuery(query)

	case len(query.SELECT) > 0 && query.COUNT == "":
		// they want some data returned!
		return selectQuery(query)

	default:
		log.Panic(error_.New("Not a valid query! Valid queries must have one of SELECT / COUNT!"))
	}

	// should never get here!
	// NOTE(@adam-hanna): need to implement proper error handling!
	return nil
}

// NOTE(@adam-hanna): do error handling!
func countQuery(query queryStruct) map[string]int {
	// make a map to hold the return
	mReturn := make(map[string]int)
	// make a slice to hold the idx's that match the query
	tempKeysMatched := make([][]uint64, 0)

	// is a group by present?
	switch query.GROUPBY {
	case "":
		// nope, no group by
		// loop through the "WHERE" key/vals. No logical operator (i.e. and / or) means "and" (i.e. intersect not union)
		// write the matching keys to our multi-dimensional array
		// NOTE(@adam-hanna): what if a field in the where is an ID or not an indexed field?
		// NOTE(@adam-hanna): this should be a private function. It will be used many times.
		evalWhereClause(query.WHERE, &tempKeysMatched)

		// find the intersection of the keys
		finalKeysMatched := arrayOperations.IntersectUint64Arr(tempKeysMatched)

		// write to the output map
		mReturn["COUNT"] = len(finalKeysMatched)

	default:
		// yup, there's a group by!
		// first, grab the unique vals and their locations in the data set of the group-by col
		groupByIndex := index.GetIndexByColName(query.GROUPBY)

		// Next, loop through the "WHERE" key/vals. No logical operator (i.e. and / or) means "and" (i.e. intersect not union)
		// write the matching keys to our multi-dimensional array
		// NOTE(@adam-hanna): what if a field in the where is an ID or not an indexed field?
		// NOTE(@adam-hanna): this should be a private function. It will be used many times.
		evalWhereClause(query.WHERE, &tempKeysMatched)

		// find the intersection of the where keys
		whereIntersect := arrayOperations.IntersectUint64Arr(tempKeysMatched)

		// lastly, find the intersection of the where and groupby idx's
		for key, val := range groupByIndex {
			// find the intersection
			mReturn[key] = len(arrayOperations.IntersectUint64(whereIntersect, val))
{'fraction_non_alphanumeric': 0.10908543016423082, 'fraction_numerical': 0.003164080156697303, 'mean_word_length': 3.461021505376344, 'pattern_counts': {'":': 6, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2918268', 'n_tokens_mistral': 2195, 'n_tokens_neox': 2033, 'n_words': 761}