<commit_before>/*
	This code computes quantile value from a table;
	table_name - is the name of the table from which quantile is to be taken
	col_name - is the name of the column that is to be used for quantile culculation
	quantile - is the qunatile value desired \in (0,1)
	
	Example:
		SELECT quantile(MyTaxEvasionRecords, AnountUnderpaid, .3);
*/

DROP FUNCTION IF EXISTS quantile(table_name TEXT, col_name TEXT, quantile FLOAT);
CREATE OR REPLACE FUNCTION quantile( table_name TEXT, col_name TEXT, quantile FLOAT) RETURNS FLOAT AS $$
declare
  size FLOAT[];
  curr FLOAT;
Begin
EXECUTE	'SELECT ARRAY[COUNT(*), MIN( ' || col_name || ' ), MAX(' || col_name || ' ), AVG( ' || col_name || ' )] FROM ' || table_name || ';' INTO size;
	size[1] = size[1]*quantile;

    LOOP
    	EXECUTE 'SELECT COUNT(*) FROM '|| table_name || ' WHERE ' || col_name  || ' <= ' || size[4] || ';'  INTO curr;
    	RAISE INFO 'VALUES NOW: % WANT: % FRACT: % MIN: % MAX: % CURR: %', curr, size[1], curr/size[1] + size[1]/curr, size[2], size[3], size[4];
    	IF((curr - size[1]) > 1) THEN
    		size[3] = size[4];
    		size[4] = (size[2]+size[4])/2.0;
    	ELSIF((size[1] - curr) > 1) THEN
    		size[2] = size[4];
    		size[4] = (size[3]+size[4])/2.0;
    	ELSE
    		RETURN size[4];
    	END IF;
    END LOOP;

    RETURN size[4];
end
$$ LANGUAGE plpgsql;<commit_msg>added formated comments<commit_after>--! @file quatile.sql
--! @File contains quantile function 
--!
--!	@About
--! This function computes specifed quantile value
--!
--! This function takes as the input the name of the table and the specific column to be used and
--! computed the quantile value based on the fraction specified as the third argument. 
--!
--! @Prerequisites
--!
--! @param 
--! table_name TEXT - is the name of the table from which quantile is to be taken
--!	col_name TEXT - is the name of the column that is to be used for quantile culculation
--!	quantile FLOAT - is the qunatile value desired \in (0,1)
--!
--! @return FLOAT value of the quantile 
--!
--! @example
--! SELECT quantile(MyTaxEvasionRecords, AmountUnderpaid, .3);


DROP FUNCTION IF EXISTS quantile(table_name TEXT, col_name TEXT, quantile FLOAT);
CREATE OR REPLACE FUNCTION quantile( table_name TEXT, col_name TEXT, quantile FLOAT) RETURNS FLOAT AS $$
declare
  size FLOAT[];
  curr FLOAT;
Begin
EXECUTE	'SELECT ARRAY[COUNT(*), MIN( ' || col_name || ' ), MAX(' || col_name || ' ), AVG( ' || col_name || ' )] FROM ' || table_name || ';' INTO size;
	size[1] = size[1]*quantile;

    LOOP
    	EXECUTE 'SELECT COUNT(*) FROM '|| table_name || ' WHERE ' || col_name  || ' <= ' || size[4] || ';'  INTO curr;
    	RAISE INFO 'VALUES NOW: % WANT: % FRACT: % MIN: % MAX: % CURR: %', curr, size[1], curr/size[1] + size[1]/curr, size[2], size[3], size[4];
    	IF((curr - size[1]) > 1) THEN
    		size[3] = size[4];
    		size[4] = (size[2]+size[4])/2.0;
    	ELSIF((size[1] - curr) > 1) THEN
    		size[2] = size[4];
    		size[4] = (size[3]+size[4])/2.0;
    	ELSE
    		RETURN size[4];
    	END IF;
    END LOOP;

    RETURN size[4];
end
$$ LANGUAGE plpgsql;
{'fraction_non_alphanumeric': 0.16040181464679196, 'fraction_numerical': 0.020738820479585224, 'mean_word_length': 3.2815533980582523, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '1051686', 'n_tokens_mistral': 1201, 'n_tokens_neox': 1169, 'n_words': 351}