text:
package org.apache.cassandra.io.compress;

import java.io.File;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.Checksum;

import org.apache.cassandra.io.sstable.SSTableMetadata.Collector;
import org.apache.cassandra.io.util.FileMark;
import org.apache.cassandra.io.util.SequentialWriter;

public class CompressedSequentialWriter extends SequentialWriter
{
    public static SequentialWriter open(String dataFilePath, String indexFilePath, boolean skipIOCache, CompressionParameters parameters, Collector sstableMetadataCollector) throws IOException
    {
        return new CompressedSequentialWriter(new File(dataFilePath), indexFilePath, skipIOCache, parameters, sstableMetadataCollector);
    }

    // holds offset in the file where current chunk should be written
    // changed only by flush() method where data buffer gets compressed and stored to the file
    private long chunkOffset = 0;

    // index file writer (random I/O)
    private final CompressionMetadata.Writer metadataWriter;
    private final ICompressor compressor;

    // used to store compressed data
    private final ICompressor.WrappedArray compressed;

    // holds a number of already written chunks
    private int chunkCount = 0;

    private final Checksum checksum = new CRC32();

    private long originalSize = 0, compressedSize = 0;

    private Collector sstableMetadataCollector;

    public CompressedSequentialWriter(File file, String indexFilePath, boolean skipIOCache, CompressionParameters parameters, Collector sstableMetadataCollector) throws IOException
    {
        super(file, parameters.chunkLength(), skipIOCache);
        this.compressor = parameters.sstableCompressor;

        // buffer for compression should be the same size as buffer itself
        compressed = new ICompressor.WrappedArray(new byte[compressor.initialCompressedBufferLength(buffer.length)]);

        /* Index File (-CompressionInfo.db component) and it's header */
        metadataWriter = new CompressionMetadata.Writer(indexFilePath);
        metadataWriter.writeHeader(parameters);
        this.sstableMetadataCollector = sstableMetadataCollector;
    }

    @Override
    public void sync() throws IOException
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public void flush() throws IOException
    {
        throw new UnsupportedOperationException();
    }

    @Override
    protected void flushData() throws IOException
    {
        seekToChunkStart();

        // compressing data with buffer re-use
        int compressedLength = compressor.compress(buffer, 0, validBufferBytes, compressed, 0);

        originalSize += validBufferBytes;
        compressedSize += compressedLength;

        // update checksum
        checksum.update(buffer, 0, validBufferBytes);

        // write an offset of the newly written chunk to the index file
        metadataWriter.writeLong(chunkOffset);
        chunkCount++;

        assert compressedLength <= compressed.buffer.length;

        // write data itself
        out.write(compressed.buffer, 0, compressedLength);
        // write corresponding checksum
        out.writeInt((int) checksum.getValue());

        // reset checksum object to the blank state for re-use
        checksum.reset();

        // next chunk should be written right after current + length of the checksum (int)
        chunkOffset += compressedLength + 4;
    }

    @Override
    public FileMark mark()
    {
        return new CompressedFileWriterMark(chunkOffset, current, validBufferBytes, chunkCount + 1);
    }

    @Override
    public synchronized void resetAndTruncate(FileMark mark) throws IOException
    {
        assert mark instanceof CompressedFileWriterMark;

        CompressedFileWriterMark realMark = ((CompressedFileWriterMark) mark);

        // reset position
        current = realMark.uncDataOffset;

        if (realMark.chunkOffset == chunkOffset) // current buffer
        {
            // just reset a buffer offset and return
            validBufferBytes = realMark.bufferOffset;
            return;
        }

        // synchronize current buffer with disk
        // because we don't want any data loss
        syncInternal();

        // setting marker as a current offset
        chunkOffset = realMark.chunkOffset;

        // compressed chunk size (- 4 bytes reserved for checksum)
        int chunkSize = (int) (metadataWriter.chunkOffsetBy(realMark.nextChunkIndex) - chunkOffset - 4);
        if (compressed.buffer.length < chunkSize)
            compressed.buffer = new byte[chunkSize];

        out.seek(chunkOffset);
        out.readFully(compressed.buffer, 0, chunkSize);

        // decompress data chunk and store its length
        int validBytes = compressor.uncompress(compressed.buffer, 0, chunkSize, buffer, 0);

        checksum.update(buffer, 0, validBytes);

        if (out.readInt() != (int) checksum.getValue())
            throw new CorruptedBlockException(getPath(), chunkOffset, chunkSize);

        checksum.reset();

        // reset buffer
        validBufferBytes = realMark.bufferOffset;
        bufferOffset = current - validBufferBytes;
        chunkCount = realMark.nextChunkIndex - 1;

        // truncate data and index file
        truncate(chunkOffset);
        metadataWriter.resetAndTruncate(realMark.nextChunkIndex);
    }

    /**
     * Seek to the offset where next compressed data chunk should be stored.
     *
     * @throws IOException on any I/O error.
     */
    private void seekToChunkStart() throws IOException
    {
        if (out.getFilePointer() != chunkOffset)
            out.seek(chunkOffset);
    }

    @Override
    public void close() throws IOException
    {
        if (buffer == null)
            return; // already closed

        super.close();
        sstableMetadataCollector.addCompressionRatio(compressedSize, originalSize);
        metadataWriter.finalizeHeader(current, chunkCount);
        metadataWriter.close();
    }

    /**
     * Class to hold a mark to the position of the file
     */
    protected static class CompressedFileWriterMark implements FileMark
    {
        // chunk offset in the compressed file
        long chunkOffset;
        // uncompressed data offset (real data offset)
        long uncDataOffset;

        int bufferOffset;
        int nextChunkIndex;

        public CompressedFileWriterMark(long chunkOffset, long uncDataOffset, int bufferOffset, int nextChunkIndex)
        {
            this.chunkOffset = chunkOffset;
            this.uncDataOffset = uncDataOffset;
            this.bufferOffset = bufferOffset;
            this.nextChunkIndex = nextChunkIndex;
        }
    }
}


text:
package com.canoo.dp.impl.security;

import com.canoo.platform.core.PlatformConfiguration;
import com.canoo.platform.core.http.HttpURLConnectionHandler;
import com.canoo.platform.core.http.spi.RequestHandlerProvider;
import org.apiguardian.api.API;

import static org.apiguardian.api.API.Status.INTERNAL;

@API(since = "0.19.0", status = INTERNAL)
public class KeycloakSecurityRequestHandlerProvider implements RequestHandlerProvider {

    @Override
    public HttpURLConnectionHandler getHandler(final PlatformConfiguration configuration) {
        return new KeycloakRequestHandler();
    }
}

redpajama_set_name: RedPajamaGithub


text:
class HashTable {
  public:

  HashTable( );

  ~HashTable( );

};

#endif //GRISALLAENGINE_HASHTABLE_H


text:
<?php

namespace hipstercreative\user\controllers;

use yii\helpers\Url;
use yii\web\Controller;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\authclient\ClientInterface;

/**
 * Controller that manages user authentication process.
 *
 * @property \hipstercreative\user\Module $module
 *
 * @author Dmitry Erofeev <dmeroff@gmail.com>
 */
class SecurityController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => ['login', 'auth'],
                        'roles' => ['?']
                    ],
                    [
                        'allow' => true,
                        'actions' => ['logout'],
                        'roles' => ['@']
                    ],
                ]
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post']
                ]
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'auth' => [
                'class' => 'yii\authclient\AuthAction',
                'successCallback' => [$this, 'authenticate'],
            ]
        ];
    }

    /**
     * Displays the login page.
     *
     * @return string|\yii\web\Response
     */
    public function actionLogin()
    {
        $model = $this->module->manager->createLoginForm();

        if ($model->load(\Yii::$app->getRequest()->post()) && $model->login()) {
            return $this->goBack();
        }

        return $this->render('login', [
            'model' => $model
        ]);
    }

    /**
     * Logs the user out and then redirects to the homepage.
     *
     * @return \yii\web\Response
     */
    public function actionLogout()
    {
        \Yii::$app->getUser()->logout();

        return $this->goHome();
    }

    /**
     * Logs the user in if this social account has been already used. Otherwise shows registration form.
     *
     * @param  ClientInterface $client
     * @return \yii\web\Response
     */
    public function authenticate(ClientInterface $client)
    {
        $attributes = $client->getUserAttributes();
        $provider   = $client->getId();
        $clientId   = $attributes['id'];

        if (null === ($account = $this->module->manager->findAccount($provider, $clientId))) {
            $account = $this->module->manager->createAccount([
                'provider'   => $provider,
                'client_id'  => $clientId,
                'properties' => json_encode($attributes)
            ]);
            $account->save(false);
        }

        if (null === ($user = $account->getUser()->one())) {
            $this->action->successUrl = Url::to(['/user/registration/connect', 'account_id' => (string)$account->_id]);
        } else {
            \Yii::$app->user->login($user, $this->module->rememberFor);
        }
    }
}


text:
/**
 * Various build tools for Jake.
 */

/*jshint smarttabs:true, undef:true, node:true, latedef:true, curly:true, bitwise:true */
"use strict";

var fs = require("fs");
var path = require("path");
var child_process = require("child_process");

function extend(a, b) {
	if (b) {
		for (var name in b) {
			a[name] = b[name];
		}
	}

	return a;
}

function getFileModTime(filePath) {
	return fs.existsSync(filePath) ? fs.statSync(filePath).mtime.getTime() : 0;
}

function setFileModTime(filePath, time) {
	return fs.utimesSync(filePath, new Date(time), new Date(time));
}

exports.uglify = function(options) {
	var UglifyJS = require("uglify-js");
	var filePaths = [];

	options = extend({
		mangle : true,
		toplevel : false,
		no_functions : false,
		ascii_only: true
	}, options);

	var toFileModTime = getFileModTime(options.to);
	var fromFileModTime = 0;

	// Combine JS files
	if (options.from instanceof Array) {
		options.from.forEach(function(filePath) {
			if (options.sourceBase) {
				filePath = path.join(options.sourceBase, filePath);
			}

			filePaths.push(filePath);

			fromFileModTime = Math.max(fromFileModTime, getFileModTime(filePath));
		});
	} else {
		filePaths.push(options.from);
		fromFileModTime = getFileModTime(options.from);
	}

	if (options.force === true || fromFileModTime !== toFileModTime) {
		var result = UglifyJS.minify(filePaths, {
		});

		fs.writeFileSync(options.to, result.code);
		setFileModTime(options.to, fromFileModTime);
	}
};

exports.less = function (options) {
	var source = "", lastMod = 0, less = require('less');

	var sourceFile = options.from;
	var outputFile = options.toCss;

	options = extend({
		compress: true,
		yuicompress: true,
		optimization: 1,
		silent: false,
		paths: [],
		color: true,
		strictImports: false
	}, options);

	var parser = new less.Parser({
		paths: [options.baseDir || path.dirname(sourceFile)],
		filename: path.basename(sourceFile),
		optimization: options.optimization,
		strictImports: options.strictImports
	});

	// Parse one or multiple files
	if (sourceFile instanceof Array) {
		sourceFile.forEach(function(sourceFile) {
			lastMod = Math.max(lastMod, getFileModTime(path.join(options.baseDir, sourceFile)));
		});

		if (options.force !== true && lastMod === getFileModTime(outputFile)) {
			return;
		}

		sourceFile.forEach(function(sourceFile) {
			source += fs.readFileSync(path.join(options.baseDir, sourceFile), 'utf-8').toString().replace(/^\uFEFF/g, '');
		});

		if (options.toLessDev) {
			var lessImportCode = "";

			sourceFile.forEach(function(sourceFile) {
				lessImportCode += '@import "' + sourceFile + '";\n';
			});

			fs.writeFileSync(options.toLessDev, lessImportCode);
		}

		if (options.toLess) {
			fs.writeFileSync(options.toLess, source);
		}
	} else {
		lastMod = getFileModTime(sourceFile);
		if (options.force !== true && lastMod === getFileModTime(outputFile)) {
			return;
		}

		source = fs.readFileSync(sourceFile).toString();
	}

	parser.parse(source, function (err, tree) {
		if (err) {
			less.writeError(err, options);
			return;
		}

		fs.writeFileSync(outputFile, tree.toCSS({
			compress: options.compress,
			yuicompress: options.yuicompress
		}));

		setFileModTime(outputFile, lastMod);
	});
};

exports.yuidoc = function (sourceDir, outputDir, options) {
	var Y = require('yuidocjs');

	if (!(sourceDir instanceof Array)) {
		sourceDir = [sourceDir];
	}

	options = extend({
		paths: sourceDir,
		outdir: outputDir,
		time: false
	}, options);

	var starttime = new Date().getTime();
	var json = (new Y.YUIDoc(options)).run();

	var builder = new Y.DocBuilder(options, json);
	builder.compile(function() {
		var endtime = new Date().getTime();

		if (options.time) {
			Y.log('Completed in ' + ((endtime - starttime) / 1000) + ' seconds' , 'info', 'yuidoc');
		}
	});
};

exports.jshint = function (options) {
	var jshint = require('jshint').JSHINT, exclude, count = 0;

	function removeComments(str) {
		str = str || "";
		str = str.replace(/\/\*(?:(?!\*\/)[\s\S])*\*\//g, "");
		str = str.replace(/\/\/[^\n\r]*/g, ""); // Everything after '//'

		return str;
	}

	options = options || {};

	var color = function(s, c){
		return (color[c].toLowerCase()||'') + s + color.reset;
	};

	color.reset = '\u001b[39m';
	color.red = '\u001b[31m';
	color.yellow = '\u001b[33m';
	color.green = '\u001b[32m';

	if (fs.existsSync(".jshintrc")) {
		options = extend(JSON.parse(removeComments("" + fs.readFileSync(options.jshintrc || ".jshintrc"))), options);
		delete options.jshintrc;
	}

	if (options.exclude) {
		exclude = options.exclude;
		delete options.exclude;
	}

	function process(filePath) {
		var stat = fs.statSync(filePath);

		// Don't hint on minified files
		if (/\.min\.js$/.test(filePath)) {
			return;
		}

		if (exclude && exclude.indexOf(filePath) != -1) {
			return;
		}

		if (/\.js$/.test(filePath)) {
			if (!jshint(fs.readFileSync(filePath).toString(), options, {define: true})) {
				// Print the errors
				console.log(color('Errors in file ' + filePath, 'red'));
				var out = jshint.data(),
				errors = out.errors;
				Object.keys(errors).forEach(function(error){
					error = errors[error];

					if (error) {
						console.log('line: ' + error.line + ':' + error.character+ ' -> ' + error.reason );
						console.log(color(error.evidence,'yellow'));
						count++;
					}
				});
			}
		}
	}

	var patterns = options.patterns;
	delete options.patterns;
	patterns.forEach(function(filePath) {
		require("glob").sync(filePath).forEach(process);
	});

	if (count > 0) {
		process.exit(1);
	}
};

exports.zip = function (options) {
	var ZipWriter = require('moxie-zip').ZipWriter;
	var archive = new ZipWriter();

	function process(filePath, zipFilePath) {
		var args, stat;

		if (filePath instanceof Buffer) {
			archive.addData(path.join(options.baseDir, zipFilePath), filePath);
			return;
		}

		stat = fs.statSync(filePath);
		zipFilePath = zipFilePath || filePath;
		filePath = filePath.replace(/\\/g, '/');
		zipFilePath = zipFilePath.replace(/\\/g, '/');

		if (options.pathFilter) {
			args = {filePath: filePath, zipFilePath: zipFilePath};
			options.pathFilter(args);
			zipFilePath = args.zipFilePath;
		}

		if (options.exclude) {
			for (var i = 0; i < options.exclude.length; i++) {
				var pattern = options.exclude[i];

				if (pattern instanceof RegExp) {
					if (pattern.test(filePath)) {
						return;
					}
				} else {
					if (filePath === pattern) {
						return;
					}
				}
			}
		}

		if (stat.isFile()) {
			var data = fs.readFileSync(filePath);

			if (options.dataFilter) {
				args = {filePath: filePath, zipFilePath: zipFilePath, data: data};
				options.dataFilter(args);
				data = args.data;
			}

			archive.addData(path.join(options.baseDir, zipFilePath), data);
		} else if (stat.isDirectory()) {
			fs.readdirSync(filePath).forEach(function(fileName) {
				if (fileName.charAt(0) != '.') {
					process(path.join(filePath, fileName), path.join(zipFilePath, fileName));
				}
			});
		}
	}

	options.baseDir = (options.baseDir || '').replace(/\\/g, '/');

	options.from.forEach(function(filePath) {
		if (filePath instanceof Array) {
			process(filePath[0], filePath[1]);
		} else {
			process(filePath);
		}
	});

	archive.saveAs(options.to);
};

exports.compileAmd = function (options) {
	//options.verbose = true;
	require("amdlc").compile(options);
};

exports.parseLessDocs = function (filePath) {
	var matches, docCommentRegExp = /\/\*\*([\s\S]+?)\*\//g, lessFiles = [];
	var source = fs.readFileSync(filePath).toString();

	for (matches = docCommentRegExp.exec(source); matches; matches = docCommentRegExp.exec(source)) {
		var docComment = matches[1];

		var lessMatch = /\@\-x\-less\s+(.+)/g.exec(docComment);
		if (lessMatch) {
			lessFiles.push(lessMatch[1]);
		}
	}

	return lessFiles;
};

exports.getReleaseDetails = function (filePath) {
	var firstLine = ("" + fs.readFileSync(filePath)).split('\n')[0];

	return {
		version: /^Version ([0-9xabrc.]+)/.exec(firstLine)[1],
		releaseDate: /^Version [^\(]+\(([^\)]+)\)/.exec(firstLine)[1]
	};
};

exports.instrumentFile = function(options) {
	var Instrument = require('coverjs').Instrument;
	var source = "" + fs.readFileSync(options.from);

	fs.writeFileSync(options.to, new Instrument(source, {
		name: options.from
	}).instrument());
};

exports.eslint = function(options) {
	var eslint = require('eslint').cli, args = [];

	function globFiles(patterns) {
		var files = [], glob = require("glob");

		if (patterns instanceof Array) {
			patterns.forEach(function(pattern) {
				if (pattern[0] == '!') {
					glob.sync(pattern.substr(1)).forEach(function(file) {
						var idx = files.indexOf(file);

						if (idx != -1) {
							files.splice(idx, 1);
						}
					});
				} else {
					files = files.concat(glob.sync(pattern));
				}
			});
		} else {
			globFiles([patterns]);
		}

		return files;
	}

	if (options.config) {
		args.push('--config', path.resolve(options.config));
	}

	if (options.rulesdir) {
		args.push('--rulesdir', options.rulesdir);
	}

	if (options.format) {
		args.push('--format', options.format);
	}

	eslint.execute(args.concat(globFiles(options.src)).join(' '));
};

exports.nuget = function(options) {
	var http = require("http"), fs = require("fs");
	var child_process = require("child_process");
	var args = [];

	if (!/^win/.test(process.platform)) {
		return;
	}

	function download(fromUrl, toPath, callback) {
		var req = http.get(fromUrl, function(response) {
			var location = response.headers.location;

			if (location) {
				req.abort();
				download(require("url").resolve(fromUrl, location), toPath, callback);
			} else {
				var file = fs.createWriteStream(toPath);
				file.on('finish', callback);
				response.pipe(file);
			}
		});
	}

	function execNuget(nuspec, args) {
		child_process.execFile(options.cmd, ["pack", nuspec].concat(args), function (error, stdout, stderr) {
			if (!options.quiet) {
				if (error !== null) {
					console.log("NuGet exec error: " + error);
				}

				if (stdout !== null) {
					console.log(stdout);
				}

				if (stderr !== null) {
					console.log(stderr);
				}
			}
		});
	}

	if (!fs.existsSync(options.cmd)) {
		download("http://nuget.org/nuget.exe", options.cmd, function() {
			if (fs.existsSync(options.cmd)) {
				exports.nuget(options);
			}
		});

		return;
	}

	if (options.version) {
		args.push("-Version", options.version);
	}

	if (options.dest) {
		args.push("-OutputDirectory", options.dest);
	}

	if (options.nuspec) {
		if (options.nuspec instanceof Array) {
			options.nuspec.forEach(function(nuspec) {
				execNuget(nuspec, args);
			});
		} else {
			execNuget(options.nuspec, args);
		}
	}
};

exports.phantomjs = function(args) {
	var childProcess = require('child_process');
	var phantomjs = require('phantomjs');
	var binPath = phantomjs.path;

	childProcess.execFile(binPath, args, function(err, stdout, stderr) {
		console.log(stdout);
		console.log(stderr);
	});
};

exports.jscs = function(args) {
	var Checker = require('jscs');
	var checker = new Checker();

	checker.registerDefaultRules();
	checker.configure(JSON.parse(fs.readFileSync(args.configFile)));
	checker.checkPath(args.src).then(function(results) {
		var errorsCollection = [].concat.apply([], results);
		var count = 0;

		errorsCollection.forEach(function(errors) {
			if (!errors.isEmpty()) {
				errors.getErrorList().forEach(function(error) {
					console.log(errors.explainError(error, true) + '\n');
					count++;
				});
			}
		});

		if (count > 0) {
			console.log("jscs errors found: " + count);
			process.exit(1);
		}

		if (args.oncomplete) {
			args.oncomplete();
		}
	});
};


text:
/*
 * Lob
 * The Lob API is organized around REST. Our API is designed to have predictable, resource-oriented URLs and uses HTTP response codes to indicate any API errors. <p> Looking for our [previous documentation](https://lob.github.io/legacy-docs/)?
 *
 * The version of the OpenAPI document: 1.3.0
 * Contact: lob-openapi@lob.com
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */

package com.lob.model;

import java.util.Objects;
import java.util.Arrays;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.lob.model.MultipleComponentsIntl;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
/**
 * IntlVerificationsPayload
 */
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class IntlVerificationsPayload {
  public static final String SERIALIZED_NAME_ADDRESSES = "addresses";

  @SerializedName(SERIALIZED_NAME_ADDRESSES)
  private List<MultipleComponentsIntl> addresses = new ArrayList<>();
  public List<MultipleComponentsIntl> getAddresses() {
    if (this.addresses == null) {
      this.addresses = new ArrayList<MultipleComponentsIntl>();
    }
    return this.addresses;
  }

  /*
  public IntlVerificationsPayload addresses(List<MultipleComponentsIntl> addresses) {

    this.addresses = addresses;
    return this;
  }
  */

  public IntlVerificationsPayload addAddressesItem(MultipleComponentsIntl addressesItem) {
    this.addresses.add(addressesItem);
    return this;
  }

  public void setAddresses(List<MultipleComponentsIntl> addresses) {
    this.addresses = addresses;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    IntlVerificationsPayload intlVerificationsPayload = (IntlVerificationsPayload) o;
    return Objects.equals(this.addresses, intlVerificationsPayload.addresses);
  }

  @Override
  public int hashCode() {
    return Objects.hash(addresses);
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("{\n");
    sb.append("    addresses: ").append(toIndentedString(addresses)).append("\n");
    sb.append("}");
    return sb.toString();
  }

  /**
   * Convert the given object to string with each line indented by 4 spaces
   * (except the first line).
   */
  private String toIndentedString(Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }

}


text:
<template name='loginServices'>
	{{#if loginService.length}}

			{{#each loginService}}
				<button type="button" class="button external-login {{service.service}}" title="{{displayName}}" style="{{#if service.buttonColor}}background-color:{{service.buttonColor}};{{/if}}{{#if service.buttonLabelColor}}color:{{service.buttonLabelColor}};{{/if}}">{{service.buttonLabelText}}</button>
			{{/each}}

	{{/if}}
</template>


text:
namespace crc { namespace device {
  bool clock::is_start_;

  void clock::init(void) {
    self::stop();
  }

  void clock::start(void) { self::is_start_ = true; }
  void clock::stop(void) { self::is_start_ = false; }
}}


text:
<?php

namespace Angie\AdminBundle\Form\Type\Post;

use Admingenerated\AngieAdminBundle\Form\BasePostType\FiltersType as BaseFiltersType;

/**
 * FiltersType
 */
class FiltersType extends BaseFiltersType
{
}


text:
<SupportedPlatforms>
	<SupportedPlatform name="All x86 NT">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSType=18]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_Processor WHERE Architecture=0]]></Expression>
	</SupportedPlatform>

  <!-- x86 -->

	<SupportedPlatform name="All x86 Windows 7 Client">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE Version like '6.1%' AND OSType=18 AND ProductType=1]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSArchitecture like '32%']]></Expression>
	</SupportedPlatform>

	<SupportedPlatform name="All x86 Windows 8 Client">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE Version like '6.2%' AND OSType=18 AND ProductType=1]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSArchitecture like '32%']]></Expression>
	</SupportedPlatform>

	<SupportedPlatform name="All x86 Windows 8.1 Client">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE Version like '6.3%' AND OSType=18 AND ProductType=1]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSArchitecture like '32%']]></Expression>
	</SupportedPlatform>

	<!-- x64 -->

	<SupportedPlatform name="All x64 Windows 7 Client">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE Version like '6.1%' AND OSType=18 AND ProductType=1]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSArchitecture like '64%']]></Expression>
	</SupportedPlatform>
	<SupportedPlatform name="All x64 Windows Server 2008 R2">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE Version like '6.1%' AND OSType=18 AND ProductType>1]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSArchitecture like '64%']]></Expression>
	</SupportedPlatform>
	<SupportedPlatform name="All x64 Windows Server 2008 R2 - Full OS (Non-Core)">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE Version like '6.1%' AND OSType=18 AND ProductType>1]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSArchitecture like '64%']]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OperatingSystemSKU < 12 OR OperatingSystemSKU > 14]]></Expression>
	</SupportedPlatform>
	<SupportedPlatform name="All x64 Windows Server 2008 R2- Core OS">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE Version like '6.1%' AND OSType=18 AND ProductType>1]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSArchitecture like '64%']]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OperatingSystemSKU > 11 AND OperatingSystemSKU < 15]]></Expression>
	</SupportedPlatform>

	<SupportedPlatform name="All x64 Windows 8 Client">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE Version like '6.2%' AND OSType=18 AND ProductType=1]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSArchitecture like '64%']]></Expression>
	</SupportedPlatform>
	<SupportedPlatform name="All x64 Windows Server 2012">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE Version like '6.2%' AND OSType=18 AND ProductType>1]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSArchitecture like '64%']]></Expression>
	</SupportedPlatform>
	<SupportedPlatform name="All x64 Windows Server 2012 - Full OS (Non-Core)">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE Version like '6.2%' AND OSType=18 AND ProductType>1]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OperatingSystemSKU < 12 OR OperatingSystemSKU > 14]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSArchitecture like '64%']]></Expression>
	</SupportedPlatform>
	<SupportedPlatform name="All x64 Windows Server 2012 - Core OS">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE Version like '6.2%' AND OSType=18 AND ProductType>1]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OperatingSystemSKU > 11 AND OperatingSystemSKU < 15]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSArchitecture like '64%']]></Expression>
	</SupportedPlatform>

	<SupportedPlatform name="All x64 Windows 8.1 Client">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE Version like '6.3%' AND OSType=18 AND ProductType=1]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSArchitecture like '64%']]></Expression>
	</SupportedPlatform>
	<SupportedPlatform name="All x64 Windows Server 2012 R2">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE Version like '6.3%' AND OSType=18 AND ProductType>1]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSArchitecture like '64%']]></Expression>
	</SupportedPlatform>
	<SupportedPlatform name="All x64 Windows Server 2012 R2 - Full OS (Non-Core)">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE Version like '6.3%' AND OSType=18 AND ProductType>1]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OperatingSystemSKU < 12 OR OperatingSystemSKU > 14]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSArchitecture like '64%']]></Expression>
	</SupportedPlatform>
	<SupportedPlatform name="All x64 Windows Server 2012 R2 - Core OS">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE Version like '6.3%' AND OSType=18 AND ProductType>1]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OperatingSystemSKU > 11 AND OperatingSystemSKU < 15]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSArchitecture like '64%']]></Expression>
	</SupportedPlatform>

	<SupportedPlatform name="All x86 Windows 7 and Newer">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE Version like '6.%' AND Version <> '6.0' AND OSType=18]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSArchitecture like '32%']]></Expression>
	</SupportedPlatform>
	<SupportedPlatform name="All x64 Windows 7 and Newer">
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE Version like '6.%' AND Version <> '6.0' AND OSType=18]]></Expression>
		<Expression><![CDATA[SELECT * FROM Win32_OperatingSystem WHERE OSArchitecture like '64%']]></Expression>
	</SupportedPlatform>
</SupportedPlatforms>


text: Twitter typeahead.js port to purescript


text:
<template name="home">

Welcome to {{App.name}}

Capture your whole cloud in a single image

    Take me to my Dashboard

</template>


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

   <title>TUI:Telescope:Coordinate Systems</title>

TUI:Telescope:Coordinate Systems

The TCC supports the following coordinate systems:

ICRS: the current RA/Dec standard. The default date of observation is J2000. ICRS is FK5 J2000 (for all practical purposes) but has no date of equinox.

FK5: the previous RA/Dec standard. The default date is J2000. The date specifies both the date of equinox (usually J2000) and the date of observation.

FK4: an old RA/Dec standard. The default date is B1950. The date specifies both the date of equinox and date of observation. If your date is Besselian or older than 1975 then then use FK4.

Galactic: IAU 1958 galactic longitude and latitude. There is no default date of observation, but most objects specified in galactic coordinates will effectively be at infinity and so will not need such a date.

Geocentric: apparent geocentric RA/Dec. The date specifies both the date of equinox and the date of observation. A blank date or date of 0 means "now".

Warning: the following coordinate systems are rarely used (except Mount for parking the telescope). They are fixed to the telescope, not the sky, and so do not track sidereally.

Topocentric: current apparent topocentric az/alt (position with respect to the telescope, assuming no atmosphere).

Observed: current apparent topocentric az/alt with refraction correction applied.

Physical: the az/alt of a perfect telescope, ignoring flexure, tilt of the azimuth axis, nonperpendicularity of the axes, etc.

Mount: the actual az/alt of the telescope, as sent to the axis controllers. This is physical position with the telescope model applied and the calibration and guiding offsets added in. Mount position is unique in that no wrap is permitted; the angle you specify is exactly the angle you will get. Thus it is the only safe choice for parking the telescope.

Notes:

Date of observation is used to correct proper motion and parallax.

Date of equinox defines a particular coordinate system for FK4, FK5 and Geocentric coordinates; it is irrelevant to ICRS and Galactic, both of which are fixed with respect to distant objects (e.g. quasars). However, ICRS has conveniently been defined to match FK5 at equinox J2000.


text:
NS_ASSUME_NONNULL_BEGIN

namespace syncer {
class SyncService;
}  // namespace syncer

namespace signin {
class IdentityManager;
}  // namespace signin

class PrefService;

@interface CWVSyncController ()

// All dependencies must out live this class.
- (instancetype)initWithSyncService:(syncer::SyncService*)syncService
                    identityManager:(signin::IdentityManager*)identityManager
                        prefService:(PrefService*)prefService
    NS_DESIGNATED_INITIALIZER;

@end

NS_ASSUME_NONNULL_END

#endif  // IOS_WEB_VIEW_INTERNAL_SYNC_CWV_SYNC_CONTROLLER_INTERNAL_H_


text:
#include "tensorflow/compiler/xla/service/gpu/gpu_conv_algorithm_picker.h"

#include "absl/algorithm/container.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/time/time.h"
#include "absl/types/optional.h"
#include "tensorflow/compiler/xla/literal_util.h"
#include "tensorflow/compiler/xla/service/gpu/backend_configs.pb.h"
#include "tensorflow/compiler/xla/service/gpu/convolution_thunk.h"
#include "tensorflow/compiler/xla/service/gpu/gpu_autotuning.pb.h"
#include "tensorflow/compiler/xla/service/gpu/hlo_algorithm_denylist.h"
#include "tensorflow/compiler/xla/service/gpu/ir_emission_utils.h"
#include "tensorflow/compiler/xla/service/gpu/stream_executor_util.h"
#include "tensorflow/compiler/xla/service/hlo_casting_utils.h"
#include "tensorflow/compiler/xla/service/hlo_instructions.h"
#include "tensorflow/compiler/xla/status_macros.h"
#include "tensorflow/compiler/xla/util.h"
#include "tensorflow/core/lib/strings/numbers.h"
#include "tensorflow/core/platform/logger.h"
#include "tensorflow/core/platform/mutex.h"
#include "tensorflow/core/util/env_var.h"
#include "tensorflow/core/util/proto/proto_utils.h"

#if (defined(GOOGLE_CUDA) && GOOGLE_CUDA)
#include "tensorflow/compiler/xla/service/gpu/buffer_comparator.h"
#include "tensorflow/stream_executor/gpu/redzone_allocator.h"
#endif

namespace xla {
namespace gpu {
namespace {

using absl::optional;
using se::DeviceMemoryBase;
using se::dnn::AlgorithmDesc;
using tensorflow::AutotuneResult;

class ScratchAllocator : public se::ScratchAllocator {
 public:
  ScratchAllocator(int device_ordinal,
                   se::DeviceMemoryAllocator* memory_allocator)
      : device_ordinal_(device_ordinal), memory_allocator_(memory_allocator) {}

  int64 GetMemoryLimitInBytes() override {
    return 1LL << 32;  // 4GB.  TODO(jlebar): Tune this?
  }
  int64 TotalAllocatedBytes() { return total_allocated_bytes_; }

  StatusOr<se::DeviceMemory<uint8>> AllocateBytes(int64 byte_size) override;

  template <typename T>
  StatusOr<se::DeviceMemory<T>> Allocate(int64 num_elements) {
    TF_ASSIGN_OR_RETURN(se::DeviceMemory<uint8> bytes,
                        AllocateBytes(num_elements * sizeof(T)));
    return se::DeviceMemory<T>(bytes);
  }

 private:
  const int device_ordinal_;
  se::DeviceMemoryAllocator* memory_allocator_;
  std::vector<se::OwningDeviceMemory> allocated_buffers_;
  int64 total_allocated_bytes_ = 0;
};

StatusOr<se::DeviceMemory<uint8>> ScratchAllocator::AllocateBytes(
    int64 byte_size) {
  CHECK_GE(byte_size, 0) << "byte_size must be positive.";
  if (byte_size > GetMemoryLimitInBytes()) {
    return se::port::Status(
        se::port::error::RESOURCE_EXHAUSTED,
        absl::StrFormat(
            "Allocating %d bytes exceeds the memory limit of %d bytes.",
            byte_size, GetMemoryLimitInBytes()));
  }

  TF_ASSIGN_OR_RETURN(se::OwningDeviceMemory allocated_buffer,
                      memory_allocator_->Allocate(device_ordinal_, byte_size,
                                                  /*retry_on_failure=*/false));
  total_allocated_bytes_ += byte_size;

  se::DeviceMemoryBase buffer_addr = *allocated_buffer;
  allocated_buffers_.push_back(std::move(allocated_buffer));
  return se::DeviceMemory<uint8>(buffer_addr);
}

std::vector<AlgorithmDesc> GetAlgorithms(CudnnConvKind kind,
                                         se::StreamExecutor* stream_exec) {
  std::vector<AlgorithmDesc> algorithms;
  bool succ = false;
  switch (kind) {
    case CudnnConvKind::kBackwardFilter:
      succ =
          stream_exec->GetConvolveBackwardFilterAlgorithms(true, &algorithms);
      break;
    case CudnnConvKind::kBackwardInput:
      succ = stream_exec->GetConvolveBackwardDataAlgorithms(true, &algorithms);
      break;
    case CudnnConvKind::kForward:
    case CudnnConvKind::kForwardActivation:
      succ = stream_exec->GetConvolveAlgorithms(true, &algorithms);
      break;
  }
  DCHECK(succ);

  return algorithms;
}

StatusOr<std::vector<se::dnn::ProfileResult>> GetMIOpenAlgorithms(
    const HloCustomCallInstruction* instr,
    absl::Span<se::DeviceMemoryBase> operand_buffers,
    se::DeviceMemoryBase result_buffer, se::StreamExecutor* stream_exec,
    ScratchAllocator* scratch_allocator, se::Stream* stream) {
  std::vector<se::dnn::ProfileResult> algorithms;

  TF_ASSIGN_OR_RETURN(GpuConvConfig config, GetGpuConvConfig(instr));

  TF_ASSIGN_OR_RETURN(se::dnn::ConvolutionKind kind,
                      GetDNNConvKindFromCudnnConvKind(config.kind));

  TF_ASSIGN_OR_RETURN(se::dnn::DataType dtype,
                      GetDNNDataTypeFromPrimitiveType(config.output_type));

  TF_ASSIGN_OR_RETURN(GpuConvParams params,
                      GetGpuConvParams(config, operand_buffers, result_buffer));

  bool succ = stream_exec->GetMIOpenConvolveAlgorithms(
      kind, dtype, stream, params.config.input_descriptor, params.input_buf,
      params.config.filter_descriptor, params.filter_buf,
      params.config.output_descriptor, params.output_buf,
      params.config.conv_desc, scratch_allocator, &algorithms);
  DCHECK(succ);

  return algorithms;
}

string NumBytesToString(int64 bytes) {
  return absl::StrCat(tensorflow::strings::HumanReadableNumBytes(bytes), " (",
                      bytes, "B)");
}

tensorflow::CudnnVersion GetCudnnVersion(se::StreamExecutor* stream_executor) {
  tensorflow::CudnnVersion cudnn_version;
  if (auto* dnn = stream_executor->AsDnn()) {
    StatusOr<se::dnn::VersionInfo> version_or = dnn->GetVersion();
    if (version_or.ok()) {
      const auto& version = version_or.ValueOrDie();
      cudnn_version.set_major(version.major_version());
      cudnn_version.set_minor(version.minor_version());
      cudnn_version.set_patch(version.patch());
    }
  }
  return cudnn_version;
}

tensorflow::ComputeCapability GetComputeCapability(
    se::StreamExecutor* stream_executor) {
  tensorflow::ComputeCapability cc;
  se::CudaComputeCapability se_cc =
      stream_executor->GetDeviceDescription().cuda_compute_capability();
  cc.set_major(se_cc.major);
  cc.set_minor(se_cc.minor);
  return cc;
}

void PrintPlatformInfo(const se::Stream* stream) {
  auto* se = stream->parent();
  const auto& desc = se->GetDeviceDescription();
  LOG(ERROR) << "Device: " << desc.name();
  LOG(ERROR) << "Platform: " << desc.platform_version();
  LOG(ERROR) << "Driver: " << desc.driver_version();
  LOG(ERROR) << "Runtime: " << desc.runtime_version();

  auto* dnn = se->AsDnn();
  if (dnn) {
    auto dnn_version = dnn->GetVersion();
    if (dnn_version.ok()) {
      auto v = dnn_version.ValueOrDie();
      LOG(ERROR) << "cudnn version: " << v.major_version() << "."
                 << v.minor_version() << "." << v.patch();
    }
  }
}

#if (defined(GOOGLE_CUDA) && GOOGLE_CUDA)
// Returns true if the redzones in `allocator`'s allocations are unmodified.
//
// If the redzones are modified, logs an error, sets the appropriate failure
// bits on `result`, and returns false.
//
// Returns a status if an unexpected error has occurred, and the stream
// has been poisoned.
//
// `name` is a user-friendly name for the set of redzones being checked, e.g.
// "input/output" or "scratch".
StatusOr<bool> CheckRedzones(const se::RedzoneAllocator& allocator,
                             se::Stream* stream, absl::string_view name,
                             const HloInstruction* instr,
                             AutotuneResult* result) {
  XLA_SCOPED_LOGGING_TIMER_LEVEL("CudnnConvAlgorithmPicker checking redzones",
                                 2);
  using RedzoneCheckStatus = se::RedzoneAllocator::RedzoneCheckStatus;
  TF_ASSIGN_OR_RETURN(RedzoneCheckStatus redzone_check,
                      allocator.CheckRedzones());
  if (redzone_check.ok()) {
    return true;
  }

  auto* fail = result->mutable_failure();
  fail->set_kind(AutotuneResult::REDZONE_MODIFIED);
  *fail->mutable_msg() = redzone_check.RedzoneFailureMsg();
  fail->set_buffer_address(
      reinterpret_cast<uint64>(redzone_check.user_buffer_address));

  LOG(ERROR) << absl::StreamFormat(
      "Detected cudnn out-of-bounds write in conv %s buffer! This is likely a "
      "cudnn bug. We will skip this algorithm in the future, but your GPU "
      "state may already be corrupted, leading to incorrect results. Within "
      "Google, no action is needed on your part. Outside of Google, please "
      "ensure you're running the latest version of cudnn. If that doesn't fix "
      "the problem, please file a bug with this full error message and we'll "
      "contact nvidia.",
      name);
  LOG(ERROR) << redzone_check.RedzoneFailureMsg();
  LOG(ERROR) << "HloInstruction " << instr->ToString();
  PrintPlatformInfo(stream);
  return false;
}
#endif

using ConvCacheKey =
    std::tuple<se::StreamExecutor*,
               /* conv->ToString(HloPrintOptions::Canonical()) */ std::string>;

struct ConvCacheStats {
  int64 cache_hits = 0;
  int64 cache_misses = 0;

  void LogStats() {
    VLOG(2) << "Cache hits: " << cache_hits;
    VLOG(2) << "Cache misses: " << cache_misses;
  }
};

ConvCacheKey AutotuneCacheKeyfromInstruction(
    const HloCustomCallInstruction* conv, se::StreamExecutor* se) {
  auto options = HloPrintOptions::Canonical();
  options.set_print_backend_config(true);
  return std::make_tuple(se, conv->ToString(options));
}

tensorflow::mutex autotune_cache_lock(tensorflow::LINKER_INITIALIZED);
auto& autotune_cache TF_GUARDED_BY(autotune_cache_lock) =
    *new absl::flat_hash_map<ConvCacheKey, AutotuneResult>();
auto& autotune_cache_stats TF_GUARDED_BY(autotune_cache_lock) =
    *new ConvCacheStats();
}  // anonymous namespace

StatusOr<AutotuneResult> GpuConvAlgorithmPicker::PickBestAlgorithm(
    const HloCustomCallInstruction* instr) {
  // Don't run this function concurrently on the same GPU.
  //
  // This is a bit of a hack and doesn't protect us against arbitrary concurrent
  // use of a GPU, but it's sufficient to let us compile two HLO modules
  // concurrently and then run them sequentially.
  //
  // Putting the lock in here rather than in PickBestAlgorithmNoCache lets us
  // avoid ever doing duplicate work.  If we have a cache miss, only one thread
  // will run PickBestAlgorithmImpl for a particular device.
  tensorflow::mutex_lock lock = LockGpu(stream_exec_);

  // We cache the autotuning results to avoid doing the duplicate work,
  // which can greatly improve both stability (deterministic numeric results
  // within a process for a given input) and performance (2x speedup on some
  // models).
  ConvCacheKey key = AutotuneCacheKeyfromInstruction(instr, stream_exec_);
  {
    tensorflow::mutex_lock lock(autotune_cache_lock);
    auto it = autotune_cache.find(key);
    if (it != autotune_cache.end()) {
      autotune_cache_stats.cache_hits++;
      return it->second;
    }
    autotune_cache_stats.cache_misses++;
  }

  // Make sure any previous activity on this executor is done. We don't want to
  // interfere with programs that are still running on the GPU.
  if (!stream_exec_->SynchronizeAllActivity()) {
    return InternalError("Failed to synchronize GPU for autotuning.");
  }

  // allocator either points to this->allocator_ or, if that's null, to a
  // se::StreamExecutorMemoryAllocator for stream_exec_.
  se::DeviceMemoryAllocator* allocator;
  optional<se::StreamExecutorMemoryAllocator> se_allocator;
  if (allocator_ != nullptr) {
    allocator = allocator_;
  } else {
    se_allocator.emplace(stream_exec_);
    allocator = &*se_allocator;
  }

  TF_ASSIGN_OR_RETURN(se::Stream* const stream,
                      allocator->GetStream(stream_exec_->device_ordinal()));
  StatusOr<AutotuneResult> result_or(InternalError("Unknown platform."));
  // Check StreamExecutor on which platform it is. ROCm and Cuda implementation
  // have diverged. Specifically, we need to make sure redzone allocator related
  // utilities are not used in ROCm routine
  if (stream_exec_->platform_kind() == se::PlatformKind::kROCm) {
    result_or = PickBestAlgorithmNoCacheRocm(instr, allocator, stream);
  } else if (stream_exec_->platform_kind() == se::PlatformKind::kCuda) {
#if (defined(GOOGLE_CUDA) && GOOGLE_CUDA)
    result_or = PickBestAlgorithmNoCacheCuda(instr, allocator, stream);
#endif
  }

  if (result_or.ok()) {
    tensorflow::mutex_lock lock(autotune_cache_lock);
    CHECK(autotune_cache.insert({key, result_or.ValueOrDie()}).second);
  }
  return result_or;
}

#if (defined(GOOGLE_CUDA) && GOOGLE_CUDA)
StatusOr<tensorflow::AutotuneResult>
GpuConvAlgorithmPicker::PickBestAlgorithmNoCacheCuda(
    const HloCustomCallInstruction* instr, se::DeviceMemoryAllocator* allocator,
    se::Stream* stream) {
  // Right now Redzone allocator is available in Cuda target only
  XLA_SCOPED_LOGGING_TIMER(absl::StrCat(
      "GpuConvAlgorithmPicker::PickBestAlgorithmImpl for ", instr->ToString()));

  const Shape& result_shape = instr->shape().tuple_shapes(0);
  int64 rng_state = 0;

  const HloModuleConfig& hlo_module_config = instr->GetModule()->config();
  const int32 conv_autotune_level =
      hlo_module_config.debug_options().xla_gpu_autotune_level();
  const bool init_conv_data = conv_autotune_level >= 2;
  const bool check_conv = conv_autotune_level >= 4;
  const auto initialize_buffer = [init_conv_data, &stream, &rng_state](
                                     DeviceMemoryBase buffer,
                                     const Shape& buffer_shape) {
    if (init_conv_data) {
      InitializeBuffer(stream, buffer_shape.element_type(), &rng_state, buffer);
    }
  };

  // Allocate space for the input, filter, and output of the convolution.
  se::RedzoneAllocator input_output_allocator(
      stream, allocator, PtxOptsFromConfig(hlo_module_config));
  std::vector<se::DeviceMemoryBase> operand_buffers;
  for (const auto* operand : instr->operands()) {
    TF_ASSIGN_OR_RETURN(auto buffer,
                        input_output_allocator.AllocateBytes(
                            ShapeUtil::ByteSizeOf(operand->shape())));
    initialize_buffer(buffer, operand->shape());
    operand_buffers.push_back(buffer);
  }
  TF_ASSIGN_OR_RETURN(auto result_buffer,
                      input_output_allocator.AllocateBytes(
                          ShapeUtil::ByteSizeOf(result_shape)));
  initialize_buffer(result_buffer, result_shape);

  TF_ASSIGN_OR_RETURN(auto backend_config,
                      instr->backend_config<CudnnConvBackendConfig>());

  optional<BufferComparator> comparator;
  // Use the first algorithm that's supported as reference. There isn't a
  // particular reason to use it, as any algorithm suffices. It doesn't make
  // this algorithm considered correct, though.
  se::DeviceMemoryBase reference_result_buffer;
  AlgorithmDesc first_algorithm;

  TF_ASSIGN_OR_RETURN(CudnnConvKind kind, GetCudnnConvKind(instr));
  std::vector<AutotuneResult> profile_results;

  const DebugOptions& debug_options =
      instr->GetModule()->config().debug_options();

  const bool crash_on_checking_failure =
      debug_options.xla_gpu_crash_on_verification_failures();

  const auto canonical_hlo =
      std::get<1>(AutotuneCacheKeyfromInstruction(instr, stream_exec_));

  string blas_version;
  if (auto* blas = stream_exec_->AsBlas()) {
    (void)blas->GetVersion(&blas_version);
  }

  absl::Span<const AlgorithmDesc> disabled_algos = GetDisabledConvAlgorithms(
      GetComputeCapability(stream_exec_), GetCudnnVersion(stream_exec_),
      blas_version, canonical_hlo);

  TF_ASSIGN_OR_RETURN(GpuConvConfig config, GetGpuConvConfig(instr));

  for (const AlgorithmDesc& alg : GetAlgorithms(kind, stream_exec_)) {
    XLA_SCOPED_LOGGING_TIMER_LEVEL(
        absl::StrCat("CudnnConvAlgorithmPicker::PickBestAlgorithm algo ",
                     alg.ToString()),
        2);

    if (absl::c_linear_search(disabled_algos, alg)) {
      LOG(INFO) << "Omitted potentially buggy algorithm " << alg.ToString()
                << " for conv " << instr->ToString();
      continue;
    }

    se::RedzoneAllocator scratch_allocator(
        stream, allocator, PtxOptsFromConfig(hlo_module_config));
    se::dnn::ProfileResult profile_result;
    VLOG(3) << "Trying algorithm " << alg.ToString() << " for "
            << instr->ToString();

    // Use assignment instead of brace-list to make GCC 4.9 happy.
    RunConvOptions options;
    options.profile_result = &profile_result;
    options.algo_override = alg;
    Status launch_status =
        RunGpuConv(config, absl::MakeSpan(operand_buffers), result_buffer,
                   &scratch_allocator, stream, options);

    if (!launch_status.ok()) {
      continue;
    }

    if (!profile_result.is_valid()) {
      continue;
    }

    profile_results.emplace_back();
    AutotuneResult& result = profile_results.back();
    result.mutable_conv()->set_algorithm(alg.algo_id());
    result.mutable_conv()->set_tensor_ops_enabled(alg.tensor_ops_enabled());

    int64 scratch_bytes_used =
        scratch_allocator.TotalAllocatedBytesExcludingRedzones();
    result.set_scratch_bytes(scratch_bytes_used);
    *result.mutable_run_time() = tensorflow::proto_utils::ToDurationProto(
        absl::Milliseconds(profile_result.elapsed_time_in_ms()));

    if (!check_conv) {
      continue;
    }

    // Check for writes to redzones.
    TF_ASSIGN_OR_RETURN(bool input_output_allocator_redzone_clear,
                        CheckRedzones(input_output_allocator, stream,
                                      "input/output", instr, &result));

    TF_ASSIGN_OR_RETURN(
        bool scratch_allocator_redzone_clear,
        CheckRedzones(scratch_allocator, stream, "scratch", instr, &result));

    if (!input_output_allocator_redzone_clear ||
        !scratch_allocator_redzone_clear) {
      AlgorithmDenylist proto;
      auto entry = proto.add_entries();
      entry->set_hlo(canonical_hlo);
      *entry->mutable_cc() = GetComputeCapability(stream_exec_);
      *entry->mutable_cudnn_version() = GetCudnnVersion(stream_exec_);
      entry->set_blas_version(blas_version);
      auto algo = entry->add_algos();
      algo->set_id(alg.algo_id());
      algo->set_tensor_ops(alg.tensor_ops_enabled());

      LOG(ERROR) << "To denylist this algorithm for this convolution, "
                    "copy-paste the following "
                    "proto to the denylist file pointed by XLA_FLAGS "
                    "--xla_gpu_algorithm_denylist_path="
                 << GetDebugOptionsFromFlags().xla_gpu_algorithm_denylist_path()
                 << " : " << proto.ShortDebugString();
      continue;
    }

    if (comparator.has_value()) {
      XLA_SCOPED_LOGGING_TIMER_LEVEL("BufferComparator::CompareEqual", 2);
      StatusOr<bool> compare_result = comparator->CompareEqual(
          stream, reference_result_buffer, result_buffer);
      if (!compare_result.ok()) {
        LOG(ERROR) << "Unable to compare " << first_algorithm.ToString()
                   << " against " << alg.ToString() << " for "
                   << instr->ToString() << ": " << compare_result.status();
        if (compare_result.status().code() ==
            tensorflow::error::RESOURCE_EXHAUSTED) {
          // Possibly OOM. Propagate the error.
          return compare_result.status();
        }
        CHECK(!crash_on_checking_failure);
      } else if (!compare_result.ValueOrDie()) {
        LOG(ERROR)
            << "Results mismatch between different convolution algorithms. "
               "This is likely a bug/unexpected loss of precision in cudnn.\n"
            << instr->ToString() << " for " << first_algorithm.ToString()
            << " vs " << alg.ToString();
        PrintPlatformInfo(stream);
        VLOG(1) << "Full module on failure: \n"
                << instr->GetModule()->ToString();
        auto* fail = result.mutable_failure();
        fail->set_kind(AutotuneResult::WRONG_RESULT);
        fail->set_buffer_address(
            reinterpret_cast<uint64>(result_buffer.opaque()));
        auto* reference_conv = fail->mutable_reference_conv();
        reference_conv->set_algorithm(first_algorithm.algo_id());
        reference_conv->set_tensor_ops_enabled(
            first_algorithm.tensor_ops_enabled());
      }
    } else {
      XLA_SCOPED_LOGGING_TIMER_LEVEL("BufferComparator::Create", 2);
      comparator.emplace(result_shape, hlo_module_config);
      TF_ASSIGN_OR_RETURN(
          reference_result_buffer,
          input_output_allocator.AllocateBytes(result_buffer.size()));
      stream->ThenMemcpy(&reference_result_buffer, result_buffer,
                         result_buffer.size());
      first_algorithm = alg;
    }
  }

  // Log the autotuning result.
  {
    tensorflow::AutotuningLog log;
    {
      ConvInstructionLog instr_log;
      *instr_log.mutable_instruction() = instr->ToProto();
      for (int i = 0; i < instr->operand_count(); i++) {
        *instr_log.add_operand_shapes() = instr->operand(i)->shape().ToProto();
        instr_log.add_operand_addresses(
            reinterpret_cast<uint64>(operand_buffers[i].opaque()));
      }
      instr_log.set_result_address(
          reinterpret_cast<uint64>(result_buffer.opaque()));
      log.mutable_instr()->PackFrom(instr_log);
    }
    for (const auto& profile : profile_results) {
      *log.add_results() = profile;
    }
    *log.mutable_compute_capability() = GetComputeCapability(stream_exec_);
    *log.mutable_cudnn_version() = GetCudnnVersion(stream_exec_);
    log.set_device_pci_bus_id(
        stream_exec_->GetDeviceDescription().pci_bus_id());
    log.set_blas_version(blas_version);
    VLOG(1) << "Autotuning result: " << log.ShortDebugString();
    // If we crash on checking failure, we are in a testing/benchmark mode, thus
    // omitting logging through the logger.
    if (!crash_on_checking_failure) {
      tensorflow::Logger::GetSingleton()->LogProto(log);
    }
  }

  // Crash on miscompares and redzone violations if desired.  Do this after
  // logging the autotuning results, otherwise we won't get any data!
  for (const auto& result : profile_results) {
    if (result.has_failure()) {
      CHECK(!crash_on_checking_failure);
    }
  }

  TF_ASSIGN_OR_RETURN(AutotuneResult selected_algorithm,
                      PickBestResult(profile_results, *instr));
  return selected_algorithm;
}
#endif

StatusOr<tensorflow::AutotuneResult>
GpuConvAlgorithmPicker::PickBestAlgorithmNoCacheRocm(
    const HloCustomCallInstruction* instr, se::DeviceMemoryAllocator* allocator,
    se::Stream* stream) {
  XLA_SCOPED_LOGGING_TIMER(absl::StrCat(
      "GpuConvAlgorithmPicker::PickBestAlgorithmImpl for ", instr->ToString()));

  const auto device_ordinal = stream_exec_->device_ordinal();
  std::vector<se::DeviceMemoryBase> operand_buffers;

  ScratchAllocator input_output_allocator(device_ordinal, allocator);
  const auto initialize_buffer = [stream](DeviceMemoryBase buffer) {
    // Although we don't have evidence this matters, zero out the buffers
    // before autotuning.  It's conceivable that using uninitialized memory as
    // the inputs might affect performance if e.g. the inputs contain
    // denormals, and this is easy enough.
    stream->ThenMemZero(&buffer, buffer.size());
  };

  // Allocate space for the input, filter, and output of the convolution.  We
  // use a ScratchAllocator for this instead of calling allocator_ directly so
  // that our allocations don't leak.
  for (const auto* operand : instr->operands()) {
    TF_ASSIGN_OR_RETURN(auto buffer,
                        input_output_allocator.AllocateBytes(
                            ShapeUtil::ByteSizeOf(operand->shape())));
    initialize_buffer(buffer);
    operand_buffers.push_back(buffer);
  }

  TF_ASSIGN_OR_RETURN(
      auto result_buffer,
      input_output_allocator.AllocateBytes(
          ShapeUtil::ByteSizeOf(instr->shape().tuple_shapes(0))));
  initialize_buffer(result_buffer);

  ScratchAllocator scratch_allocator(device_ordinal, allocator);

  TF_ASSIGN_OR_RETURN(
      std::vector<se::dnn::ProfileResult> algorithms,
      GetMIOpenAlgorithms(instr, absl::MakeSpan(operand_buffers), result_buffer,
                          stream_exec_, &scratch_allocator, stream));

  std::vector<AutotuneResult> profile_results;

  if (algorithms.size() == 1) {
    auto profile_result = algorithms[0];
    profile_results.emplace_back();
    auto& result = profile_results.back();
    result.mutable_conv()->set_algorithm(profile_result.algorithm().algo_id());
    result.mutable_conv()->set_tensor_ops_enabled(
        profile_result.algorithm().tensor_ops_enabled());

    result.set_scratch_bytes(profile_result.scratch_size());
    *result.mutable_run_time() = tensorflow::proto_utils::ToDurationProto(
        absl::Milliseconds(profile_result.elapsed_time_in_ms()));
  } else {
    TF_ASSIGN_OR_RETURN(GpuConvConfig config, GetGpuConvConfig(instr));
    for (const auto& miopen_alg : algorithms) {
      const auto& alg = miopen_alg.algorithm();
      XLA_SCOPED_LOGGING_TIMER_LEVEL(
          absl::StrCat("CudnnConvAlgorithmPicker::PickBestAlgorithm algo ",
                       alg.ToString()),
          2);

      se::dnn::ProfileResult profile_result;
      VLOG(3) << "Trying algorithm " << alg.ToString() << " for "
              << instr->ToString();

      // Use assignment instead of brace-list to make GCC 4.9 happy.
      RunConvOptions options;
      options.profile_result = &profile_result;
      options.algo_override = alg;
      options.scratch_size_override = miopen_alg.scratch_size();
      Status launch_status =
          RunGpuConv(config, absl::MakeSpan(operand_buffers), result_buffer,
                     &scratch_allocator, stream, options);

      if (!launch_status.ok()) {
        continue;
      }

      if (!profile_result.is_valid()) {
        continue;
      }

      profile_results.emplace_back();
      AutotuneResult& result = profile_results.back();
      result.mutable_conv()->set_algorithm(alg.algo_id());
      result.mutable_conv()->set_tensor_ops_enabled(alg.tensor_ops_enabled());

      int64 scratch_bytes_used = scratch_allocator.TotalAllocatedBytes();
      result.set_scratch_bytes(scratch_bytes_used);
      *result.mutable_run_time() = tensorflow::proto_utils::ToDurationProto(
          absl::Milliseconds(profile_result.elapsed_time_in_ms()));
    }
  }

  TF_ASSIGN_OR_RETURN(AutotuneResult selected_algorithm,
                      PickBestResult(profile_results, *instr));
  return selected_algorithm;
}

StatusOr<bool> GpuConvAlgorithmPicker::RunOnInstruction(HloInstruction* instr) {
  CHECK(IsCustomCallToDnnConvolution(*instr));

  StatusOr<AutotuneResult> best_algo_or =
      PickBestAlgorithm(Cast<HloCustomCallInstruction>(instr));
  if (!best_algo_or.ok()) {
    LOG(WARNING) << "Failed to determine best cudnn convolution algorithm: "
                 << best_algo_or.status()
                 << "\n\nConvolution performance may be suboptimal.";
    return false;
  }

  auto best_algo = std::move(best_algo_or).ValueOrDie();
  VLOG(2) << "Setting cudnn conv to use algorithm "
          << best_algo.conv().algorithm() << " and "
          << NumBytesToString(best_algo.scratch_bytes())
          << " of scratch memory: " << instr->ToString()
          << " tensor_ops_enabled: " << best_algo.conv().tensor_ops_enabled();

  // Replace instr with a new CustomCall which has the correct algorithm, and
  // whose output shape has the appropriate amount of scratch memory.
  HloComputation* computation = instr->parent();
  Shape new_call_shape = ShapeUtil::MakeTupleShape(
      {instr->shape().tuple_shapes(0),
       ShapeUtil::MakeShape(U8, {best_algo.scratch_bytes()})});

  TF_ASSIGN_OR_RETURN(CudnnConvBackendConfig backend_config,
                      instr->backend_config<CudnnConvBackendConfig>());
  backend_config.set_algorithm(best_algo.conv().algorithm());
  backend_config.set_tensor_ops_enabled(best_algo.conv().tensor_ops_enabled());

  HloInstruction* new_call = computation->AddInstruction(
      instr->CloneWithNewOperands(new_call_shape, instr->operands()));

  VLOG(2) << "Replacing convolution " << instr->ToString() << " with "
          << new_call->ToString();

  TF_RETURN_IF_ERROR(new_call->set_backend_config(backend_config));

  // Repackage new_call so it has the same shape as the original call, namely
  // (conv_result, u8[0]).
  HloInstruction* new_tuple =
      computation->AddInstruction(HloInstruction::CreateTuple(
          {computation->AddInstruction(HloInstruction::CreateGetTupleElement(
               new_call_shape.tuple_shapes(0), new_call, 0)),
           computation->AddInstruction(HloInstruction::CreateConstant(
               LiteralUtil::CreateR1<uint8>({})))}));

  TF_RETURN_IF_ERROR(instr->parent()->ReplaceInstruction(instr, new_tuple));
  return true;
}

StatusOr<bool> GpuConvAlgorithmPicker::RunOnComputation(
    HloComputation* computation) {
  std::vector<HloInstruction*> convs;
  for (auto* instr : computation->instructions()) {
    if (IsCustomCallToDnnConvolution(*instr)) {
      convs.push_back(instr);
    }
  }

  bool changed = false;
  for (auto* instr : convs) {
    TF_ASSIGN_OR_RETURN(bool result, RunOnInstruction(instr));
    changed |= result;
  }
  return changed;
}

StatusOr<bool> GpuConvAlgorithmPicker::Run(HloModule* module) {
  XLA_SCOPED_LOGGING_TIMER("GpuConvAlgorithmPicker");

  if (module->config().debug_options().xla_gpu_autotune_level() == 0) {
    VLOG(2) << "Convolution auto-tuning disabled, GpuConvAlgorithmPicker "
               "returning early.";
    return false;
  }

  bool changed = false;
  for (HloComputation* computation : module->MakeNonfusionComputations()) {
    TF_ASSIGN_OR_RETURN(bool result, RunOnComputation(computation));
    changed |= result;
  }

  {
    tensorflow::mutex_lock lock(autotune_cache_lock);
    autotune_cache_stats.LogStats();
  }

  return changed;
}

}  // namespace gpu
}  // namespace xla


text:
layout: post
title: Learning is Slow, but Necessary
author: Jon Wrona
# if you don't have a github account or you do not wish to share it
#  leave the next line out
github: jonwrona
# change to true to publish
published: true
# first line followed by a ...
excerpt: I'm going to start by saying that there has been a lot of progress on the new Table Tome in just a few months...
---

# {{ page.title }}

{{ page.date | date_to_string }}
{: .meta}

I'm going to start by saying that there has been a lot of progress on the new Table Tome in just a few months. A couple of weeks ago I was telling myself that there wasn't too much of a chance that myself and a few others would have completed all of the features that had previously existed (in their messy state) plus more. In fact I didn't think that we'd have it to the previous state at all.

I was very wrong. I underestimated how much time I had spent learning instead of implementing while developing the first iteration of Table Tome. Learning is a slow process, but it is necessary. This time around I was able to focus less on figuring out how to use Node and Angular and more on writing clean and concise code.

On top of that, there are **TONS** of services out there that only make a developer's job easier. Right now I'm thinking of [*Auth0*](https://auth0.com/). The last iteration used a user authentication system that I created myself. This included everything from sending emails regarding registration and email confirmation to user settings. *Auth0* made implementing all of that incredibly fast and (in my opinion) very easy to do well.

I'm proud to say that Table Tome is not only in the state function wise that it was before, but that it is much more extensible and the code is better documented (it can and will improve though).

The next step for me once a few more features are in place is to go back and learn once again how to test javascript code, both on the front-end and the back-end. This will most likely slow down my development effort over the summer (as well as starting a full-time job), but this project isn't going anywhere if I'm still around to contribute.

Maybe I'll manage to get some help from a couple of students from the [Rensselaer Center for Open Source](https://rcos.io) during the next school year too...

-- _{{ page.author }}_ {% if page.github %}_([github@{{ page.github }}](https://github.com/{{ page.github }}))_{% endif %}
{: .meta}


text:
CREATE OR REPLACE TYPE typ_dq_tbl_vc2 as table of varchar2(4000);
/

CREATE OR REPLACE TYPE typ_dq_tbl_tbl_vc2 as table of typ_dq_tbl_vc2;
/

/**
   * Object parameters.
   *
*/
CREATE OR REPLACE TYPE "DQ_TYP_PARAMS" AS OBJECT (
   id integer,
   label    VARCHAR2(255),
   name    VARCHAR2(255),
   original_text VARCHAR2(255),
   bool_yes    VARCHAR2(255),
   bool_no    VARCHAR2(255)
 );
/

/**
   * Object columns.
   *
*/
CREATE OR REPLACE TYPE "DQ_TYP_COLUMNS" AS OBJECT (
   id integer,
   name    VARCHAR2(255)
 );
/

CREATE OR REPLACE TYPE "DQ_TBL_PARAMS" IS TABLE OF dq_typ_params;
/

CREATE OR REPLACE TYPE "DQ_TBL_COLUMNS" IS TABLE OF dq_typ_columns;
/

CREATE TABLE "DQ_GROUP_QUERIES"
   (	"GROUP_QUERY_ID" NUMBER(*,0) NOT NULL ENABLE,
	"GROUP_QUERY_PARENT_ID" NUMBER(*,0) NOT NULL ENABLE,
	"GROUP_QUERY_NAME" VARCHAR2(70) NOT NULL ENABLE,
	"GROUP_QUERY_NUMBER" NUMBER(*,0) NOT NULL ENABLE
   ) ;

CREATE TABLE "DQ_QUERIES"
   (	"QUERY_ID" NUMBER(*,0) NOT NULL ENABLE,
	"GROUP_QUERY_ID" NUMBER(*,0) NOT NULL ENABLE,
	"QUERY_NAME" VARCHAR2(70) NOT NULL ENABLE,
	"QUERY_COMMENT" VARCHAR2(2000),
	"QUERY_SQL" CLOB,
	"QUERY_NUMBER" NUMBER(*,0) NOT NULL ENABLE,
	"QUERY_RESULT_FORMAT_ID" VARCHAR2(100),
	"QUERY_SQL_DB" CLOB,
	"QUERY_COLUMNS" "DQ_TBL_COLUMNS" ,
	"QUERY_PARAMS" "DQ_TBL_PARAMS" ,
	"QUERY_PATTERN" BLOB
   )
 NESTED TABLE "QUERY_COLUMNS" STORE AS "NESTED_QUERY_COLUMNS"
 RETURN AS VALUE
 NESTED TABLE "QUERY_PARAMS" STORE AS "NESTED_QUERY_PARAMS"
 RETURN AS VALUE;

CREATE TABLE "DQ_QUERY_RESULT_FORMATS"
   (	"QUERY_RESULT_FORMAT_ID" VARCHAR2(100),
	"QUERY_RESULT_FORMAT_NAME" VARCHAR2(255)
   ) ;

CREATE TABLE "DQ_QUERY_STATISTICS"
   (	"QUERY_STATISTICS_USER_LOGIN" VARCHAR2(255),
	"QUERY_ID" NUMBER(*,0),
	"QUERY_STATISTICS_RUN_COUNT" NUMBER(*,0) DEFAULT 0
   ) ;

CREATE TABLE DQ_QUERY_PARAMS
(
  QUERY_PARAM_NAME     VARCHAR2(255 BYTE),
  QUERY_PARAM_CAPTION  VARCHAR2(255 BYTE),
  QUERY_PARAM_QRY      CLOB
);

 CREATE SEQUENCE  "SEQ_DQ_GROUP_QUERIES"  MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 5 NOCACHE  NOORDER  NOCYCLE ;

 CREATE SEQUENCE  "SEQ_DQ_QUERIES"  MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 50 NOCACHE  NOORDER  NOCYCLE ;

CREATE OR REPLACE PACKAGE utl
AS
   FUNCTION isEqual (a IN NUMBER, b IN NUMBER)
      RETURN BOOLEAN;

   FUNCTION isEqual (a IN VARCHAR2, b IN VARCHAR2)
      RETURN BOOLEAN;

   FUNCTION isEqual (a IN DATE, b IN DATE)
      RETURN BOOLEAN;

   FUNCTION blob_to_clob (blob_in IN BLOB) RETURN CLOB;

    end;
/

CREATE OR REPLACE PACKAGE BODY utl
AS
   FUNCTION isEqual (a IN NUMBER, b IN NUMBER)
      RETURN BOOLEAN
   IS
   BEGIN
      RETURN    (a IS NOT NULL AND b IS NOT NULL AND a = b)
             OR (a IS NULL AND b IS NULL);
   END;

   FUNCTION isEqual (a IN VARCHAR2, b IN VARCHAR2)
      RETURN BOOLEAN
   IS
   BEGIN
      RETURN    (a IS NOT NULL AND b IS NOT NULL AND a = b)
             OR (a IS NULL AND b IS NULL);
   END;

   FUNCTION isEqual (a IN DATE, b IN DATE)
      RETURN BOOLEAN
   IS
   BEGIN
      RETURN    (a IS NOT NULL AND b IS NOT NULL AND a = b)
             OR (a IS NULL AND b IS NULL);
   END;

   FUNCTION blob_to_clob (blob_in IN BLOB)
      RETURN CLOB
   AS
      v_clob      CLOB;
      v_varchar   VARCHAR2 (32767);
      v_start     PLS_INTEGER := 1;
      v_buffer    PLS_INTEGER := 32767;
   BEGIN
      DBMS_LOB.CREATETEMPORARY (v_clob, TRUE);

      FOR i IN 1 .. CEIL (DBMS_LOB.GETLENGTH (blob_in) / v_buffer)
      LOOP
         v_varchar :=
            UTL_RAW.CAST_TO_VARCHAR2 (
               DBMS_LOB.SUBSTR (blob_in, v_buffer, v_start));

         DBMS_LOB.WRITEAPPEND (v_clob, LENGTH (v_varchar), v_varchar);
         v_start :=
            v_start +
            v_buffer;
      END LOOP;

      RETURN v_clob;
   END blob_to_clob;
END;
/

CREATE OR REPLACE PACKAGE DQ
AS
   /**
   * Project:         DynamiQ (<a href="https://github.com/VladMl/DynamiQ)

   * Description:     User definad query management for Oracle APEX

   * DB impact:       YES

   * Commit inside:   NO

   * Rollback inside: NO

   * @headcom
   */

   /**
   * Record of query data.
   *
   * @param parameters parameters list
   * @param text  query text
   */
   TYPE t_query IS RECORD
   (
      parameters dq_tbl_params,
      text VARCHAR2 (32767 BYTE)
   );

   /**
   * Get query data
   *
   * @param pi_query_id                       Query id  dq_queries.query_id
   * @param pv_values                         List of params delimiter |
   * @param pv_query_result_format_id         Output format
   * @return                                  result Blob
   */
   FUNCTION Get_Query_Result (pi_query_id IN INTEGER,
                              pv_values IN VARCHAR2,
                              pv_query_result_format_id VARCHAR2)
      RETURN BLOB;

   /**
   * Prepare collection for IR
   *
   * @param pi_query_id       Query id  dq_queries.query_id
   * @param pv_query_result_format_id         Output format -- not used in this procedure
   * @param pv_values         List of params delimiter |
   */
   PROCEDURE Get_Query_Result_Ir (
      pi_query_id IN DQ_QUERIES.QUERY_ID%TYPE,
      pv_values IN VARCHAR2,
      pv_query_result_format_id DQ_QUERIES.QUERY_RESULT_FORMAT_ID%TYPE);

   /**
   * Parse query on Insert or Update. Save columns and parameters
   *
   * @param pi_query_id       Query id  dq_queries.query_id
   */
   PROCEDURE Parse_Query (pv_query_id NUMBER);

END;
/

 CREATE OR REPLACE PACKAGE BODY DQ
/**
* Project:         DynamiQ (<a href="https://github.com/VladMl/DynamiQ)

* Description:     User definad query management for Oracle APEX

* DB impact:       YES

* Commit inside:   NO

* Rollback inside: NO

* @headcom
*/
AS
   /**
   * Parse query text and get parameters and modified text.
   *
   * @param pi_query_id       Query id dq_queries.query_id
   * @return          list of parameters, modified query text t_query
   */
   FUNCTION Get_Parameters_From_Text (
      pi_query_id IN DQ_QUERIES.QUERY_ID%TYPE)
      RETURN t_query
   IS
      lc_qry CLOB;
      li_length PLS_INTEGER;
      lv_curr_param VARCHAR2 (4000);
      lv_curr_param_name VARCHAR2 (4000);
      li_start_param PLS_INTEGER;
      li_end_param PLS_INTEGER;
      lt_query t_query;

      ltbl_parameters dq_tbl_params;

      li_count_params INTEGER;
   BEGIN
      SELECT query_sql
        INTO lc_qry
        FROM dq_queries
       WHERE query_id = pi_query_id;

      ltbl_parameters := dq_tbl_params ();

      li_length := DBMS_LOB.getlength (lc_qry);

      FOR i IN 1 .. li_length
      LOOP
         IF SUBSTR (lc_qry, i, 1) = '['
         THEN
            li_start_param := i;
         END IF;

         IF SUBSTR (lc_qry, i, 1) = ']'
         THEN
            li_end_param := i;
            lv_curr_param :=
               SUBSTR (lc_qry,
                       li_start_param + 1,
                       li_end_param - li_start_param - 1);
            lv_curr_param_name :=
               SUBSTR (lv_curr_param, 1, INSTR (lv_curr_param, ':') - 1);

            SELECT COUNT (*)
              INTO li_count_params
              FROM TABLE (ltbl_parameters)
             WHERE name = lv_curr_param_name;

            IF li_count_params = 0
            THEN
               ltbl_parameters.EXTEND;
               ltbl_parameters (ltbl_parameters.COUNT) :=
                  dq_typ_params (
                     1,
                     SUBSTR (lv_curr_param,
                             INSTR (lv_curr_param, ':') + 1,
                             LENGTH (lc_qry)),
                     lv_curr_param_name,
                     SUBSTR (lc_qry,
                             li_start_param,
                             li_end_param + 1 - li_start_param),
                     '',
                     '');
            END IF;
         END IF;
      END LOOP;

      FOR indx IN 1 .. ltbl_parameters.COUNT
      LOOP
         lc_qry :=
            REPLACE (lc_qry,
                     ltbl_parameters (indx).original_text,
                     ':' || ltbl_parameters (indx).name);
      END LOOP;

      lt_query.parameters := ltbl_parameters;
      lt_query.text := lc_qry;

      RETURN lt_query;
   END Get_Parameters_From_Text;

   /**
   * Replace template value for Excel (XML) file. Mask ## , #!
   *
   * @param pi_query_id       Query id dq_queries.query_id
   * @pv_source_str           Source string
   * @ptbl_values             Values collection
   * @return          list of parameters, modified query text t_query
   */
   FUNCTION Replace_Template_Value (pi_query_id DQ_QUERIES.QUERY_ID%TYPE,
                                    pv_source_str VARCHAR2,
                                    ptbl_values typ_dq_tbl_vc2)
      RETURN VARCHAR2
   IS
      li_pos_mark_symb NUMBER;
      li_column_id NUMBER;
      lv_ret_str VARCHAR2 (32000);
   BEGIN
      lv_ret_str := pv_source_str;

      li_pos_mark_symb := INSTR (pv_source_str, '##');

      IF li_pos_mark_symb = 0
      THEN
         li_pos_mark_symb := INSTR (pv_source_str, '#!');
      END IF;

      IF li_pos_mark_symb > 0
      THEN
         BEGIN
            SELECT id
              INTO li_column_id
              FROM dq_queries, TABLE (dq_queries.query_columns)
             WHERE     query_id = pi_query_id
                   AND name =
                          SUBSTR (
                             pv_source_str,
                             li_pos_mark_symb + 2,
                             INSTR (pv_source_str, '<', li_pos_mark_symb) -
                             li_pos_mark_symb - 2);

            lv_ret_str :=
               REPLACE (
                  pv_source_str,
                  SUBSTR (pv_source_str,
                          li_pos_mark_symb,
                          INSTR (pv_source_str, '<', li_pos_mark_symb) -
                          li_pos_mark_symb),
                  ptbl_values (li_column_id));
         EXCEPTION
            WHEN NO_DATA_FOUND
            THEN
               NULL;
         END;
      END IF;

      RETURN lv_ret_str;
   END;

   /**
   * Get XLS  for Excel (XML) data
   *
   * @param pi_query_id       Query id  dq_queries.query_id
   * @param ptbl_data_set           Query dataset dq_queries.query_id
   * @return                  XLS Blob
   */
   FUNCTION Get_XLS (pi_query_id IN DQ_QUERIES.QUERY_ID%TYPE,
                     ptbl_data_set typ_dq_tbl_tbl_vc2)
      RETURN BLOB
   IS
      lb_pattern BLOB;
      lc_pattern CLOB;
      lb_result BLOB;
      lv_cur_str VARCHAR2 (32000);
      lv_cur_row_str VARCHAR2 (32000);
      li_line_count PLS_INTEGER;
      li_pos_row_symb INTEGER;
      li_pos_expanded_row INTEGER;
      li_data_row_index INTEGER := 0;
      li_row_index INTEGER := 0;
      lv_row_first VARCHAR2 (32000);
      lv_row_last VARCHAR2 (32000);
      ltbl_row APEX_APPLICATION_GLOBAL.VC_ARR2;
   BEGIN
      -- Get pattern
      SELECT query_pattern
        INTO lb_pattern
        FROM dq_queries
       WHERE query_id = pi_query_id;

      DBMS_LOB.createtemporary (lb_result, TRUE);
      DBMS_LOB.open (lb_result, DBMS_LOB.lob_readwrite);

      IF lb_pattern IS NOT NULL
      THEN
         -- Convert BLOB to CLOB
         lc_pattern := utl.blob_to_clob (lb_pattern);

         -- Read CLOB
         li_line_count :=
            (LENGTH (REGEXP_REPLACE (lc_pattern,
             '^.*',
             NULL,
             1,
             0,
             'm')) + 1);

         FOR i IN 1 .. li_line_count
         LOOP
            lv_cur_str :=
               REGEXP_SUBSTR (lc_pattern,
                              '^.*',
                              1,
                              i,
                              'm');

            li_pos_expanded_row := INSTR (lv_cur_str, 'ss:ExpandedRowCount');

            IF li_pos_expanded_row > 0
            THEN
               lv_cur_str :=
                  REPLACE (
                     lv_cur_str,
                     SUBSTR (
                        lv_cur_str,
                        li_pos_expanded_row,
                        INSTR (lv_cur_str,
                        '"',
                        INSTR  (lv_cur_str, '"', li_pos_expanded_row) + 1) -
                        li_pos_expanded_row + 1),
                     '');
            END IF;

            IF INSTR (lv_cur_str, '<Row') > 0
            THEN
               lv_row_first := lv_cur_str;
            END IF;

            IF INSTR (lv_cur_str, '</Row>') > 0 AND ltbl_row.COUNT > 0
            THEN
               lv_row_last := lv_cur_str;
               li_data_row_index := li_data_row_index + 1;

               DBMS_LOB.append (lb_result,
                                UTL_RAW.cast_to_raw (lv_cur_str || CHR (10)));

               FOR i IN 2 .. ptbl_data_set.COUNT
               LOOP
                  DBMS_LOB.append (
                     lb_result,
                     UTL_RAW.cast_to_raw (lv_row_first || CHR (10)));

                  FOR j IN 1 .. ltbl_row.COUNT
                  LOOP
                     lv_cur_row_str :=
                        Replace_Template_Value (pi_query_id,
                                                ltbl_row (j),
                                                ptbl_data_set (i));
                     DBMS_LOB.append (
                        lb_result,
                        UTL_RAW.cast_to_raw (lv_cur_row_str || CHR (10)));
                  END LOOP;

                  DBMS_LOB.append (
                     lb_result,
                     UTL_RAW.cast_to_raw (lv_row_last || CHR (10)));
               END LOOP;

               CONTINUE;
            END IF;

            li_pos_row_symb := INSTR (lv_cur_str, '#!');

            IF li_pos_row_symb > 0
            THEN
               li_row_index := li_row_index + 1;
               ltbl_row (li_row_index) := lv_cur_str;

               lv_cur_str :=
                  Replace_Template_Value (pi_query_id,
                                          lv_cur_str,
                                          ptbl_data_set (1));
            ELSE
               lv_cur_str :=
                  Replace_Template_Value (pi_query_id,
                                          lv_cur_str,
                                          ptbl_data_set (1));
            END IF;

            DBMS_LOB.append (lb_result,
                             UTL_RAW.cast_to_raw (lv_cur_str || CHR (10)));
         END LOOP;
      END IF;

      RETURN lb_result;
   END;

   /**
   * Get plain  for Word (XML), Text data
   *
   * @param pi_query_id       Query id  dq_queries.query_id
   * @param ptbl_data_set           Query dataset dq_queries.query_id
   * @return                  XLS Blob
   */
   FUNCTION Get_Plain (pi_query_id IN DQ_QUERIES.QUERY_ID%TYPE,
                       ptbl_data_set typ_dq_tbl_tbl_vc2)
      RETURN BLOB
   IS
      lb_pattern BLOB;
      lc_pattern CLOB;
      lb_result BLOB;
      dest_offset INTEGER;
      src_offset INTEGER;
      lang_context INTEGER;
      warning VARCHAR2 (1000);
   BEGIN
      -- Get pattern
      SELECT query_pattern
        INTO lb_pattern
        FROM dq_queries
       WHERE query_id = pi_query_id;

      DBMS_LOB.createtemporary (lb_result, TRUE);
      DBMS_LOB.open (lb_result, DBMS_LOB.lob_readwrite);

      IF lb_pattern IS NOT NULL
      THEN
         -- Convert BLOB to CLOB
         lc_pattern := utl.blob_to_clob (lb_pattern);

         FOR cur IN (SELECT id, name
                       FROM dq_queries, TABLE (dq_queries.query_columns)
                      WHERE query_id = pi_query_id)
         LOOP
            lc_pattern :=
               REPLACE (lc_pattern,
                        '##' || cur.name,
                        TO_CHAR (ptbl_data_set (1) (cur.id)));
         END LOOP;

         dest_offset := 1;
         src_offset := 1;
         lang_context := 0;
         DBMS_LOB.converttoblob (lb_result,
                                 lc_pattern,
                                 DBMS_LOB.getlength (lc_pattern),
                                 dest_offset,
                                 src_offset,
                                 0,
                                 lang_context,
                                 warning);
      END IF;

      RETURN lb_result;
   END;

   /**
   * Get query data
   *
   * @param pi_query_id                       Query id  dq_queries.query_id
   * @param pv_values                         List of params delimiter |
   * @param pv_query_result_format_id         Output format
   * @return                                  result Blob
   */

   FUNCTION Get_Query_Result (pi_query_id IN INTEGER,
                              pv_values IN VARCHAR2,
                              pv_query_result_format_id VARCHAR2)
      RETURN BLOB
   IS
      TYPE typ_cur_qry IS REF CURSOR;

      lt_cur_qry typ_cur_qry;

      ln_cur_cols NUMBER;
      ln_cur_qry NUMBER;
      ln_cur_qry_exe NUMBER;

      l_columns_desc DBMS_SQL.desc_tab;
      ln_column_cnt NUMBER;
      lv_stmt_txt VARCHAR2 (4000);
      lv_header_str VARCHAR2 (32000);
      lv_cur_str VARCHAR2 (32000);
      ltbl_result typ_dq_tbl_tbl_vc2;
      lt_params wwv_flow_global.vc_arr2;
      lt_values wwv_flow_global.vc_arr2;
      lb_result BLOB;

      lc_query_sql CLOB;
   BEGIN
      SELECT name
        BULK COLLECT INTO lt_params
        FROM dq_queries, TABLE (dq_queries.query_params)
       WHERE query_id = pi_query_id;

      SELECT query_sql_db
        INTO lc_query_sql
        FROM dq_queries
       WHERE query_id = pi_query_id;

      lt_values := APEX_UTIL.string_to_table (pv_values, '|');

      ln_cur_cols := DBMS_SQL.open_cursor;

      DBMS_SQL.parse (ln_cur_cols, lc_query_sql, DBMS_SQL.native);
      DBMS_SQL.describe_columns (ln_cur_cols, ln_column_cnt, l_columns_desc);
      DBMS_SQL.close_cursor (ln_cur_cols);

      lv_stmt_txt := 'select typ_dq_tbl_vc2(';

      FOR i IN 1 .. ln_column_cnt
      LOOP
         lv_header_str :=
               lv_header_str
            || REPLACE (l_columns_desc (i).col_name, '_', ' ')
            || ';';

         IF i != 1
         THEN
            lv_stmt_txt := lv_stmt_txt || ',';
         END IF;

         lv_stmt_txt := lv_stmt_txt || l_columns_desc (i).col_name;
      END LOOP;

      lv_stmt_txt := lv_stmt_txt || ') from ' || '(' || lc_query_sql || ')';

      ln_cur_qry := DBMS_SQL.OPEN_CURSOR;

      DBMS_SQL.PARSE (ln_cur_qry, lv_stmt_txt, DBMS_SQL.NATIVE);

      FOR iParams IN 1 .. lt_params.COUNT
      LOOP
         DBMS_SQL.bind_variable (ln_cur_qry,
                                 TRIM (lt_params (iParams)),
                                 TRIM (lt_values (iParams)));
      END LOOP;

      ln_cur_qry_exe := DBMS_SQL.EXECUTE (ln_cur_qry);

      lt_cur_qry := DBMS_SQL.TO_REFCURSOR (ln_cur_qry);

      FETCH lt_cur_qry BULK COLLECT INTO ltbl_result;

      -- CSV
      IF pv_query_result_format_id = 'CSV'
      THEN
         DBMS_LOB.createtemporary (lb_result, TRUE);
         DBMS_LOB.open (lb_result, DBMS_LOB.lob_readwrite);

         DBMS_LOB.append (lb_result,
                          UTL_RAW.cast_to_raw (lv_header_str || CHR (10)));

         FOR i IN 1 .. ltbl_result.COUNT
         LOOP
            lv_cur_str := '';

            FOR j IN 1 .. ltbl_result (i).COUNT
            LOOP
               lv_cur_str := lv_cur_str || ltbl_result (i) (j) || ';';
            END LOOP;

            DBMS_LOB.append (lb_result,
                             UTL_RAW.cast_to_raw (lv_cur_str || CHR (10)));

            NULL;
         END LOOP;
      END IF;

      IF    pv_query_result_format_id = 'XLS'
         OR pv_query_result_format_id = 'XHTML'
      THEN
         lb_result := Get_XLS (pi_query_id, ltbl_result);
      END IF;

      IF    pv_query_result_format_id = 'DOC'
         OR pv_query_result_format_id = 'WHTML'
      THEN
         lb_result := Get_Plain (pi_query_id, ltbl_result);
      END IF;

      CLOSE lt_cur_qry;

      MERGE INTO dq_query_statistics dqs
           USING DUAL
              ON (    dqs.query_statistics_user_login = v ('APP_USER')
                  AND dqs.query_id = pi_query_id)
      WHEN NOT MATCHED
      THEN
         INSERT     (query_statistics_user_login,
                     query_id,
                     query_statistics_run_count)
             VALUES (v ('APP_USER'), pi_query_id, 1)
      WHEN MATCHED
      THEN
         UPDATE SET
            query_statistics_run_count = query_statistics_run_count + 1;

      RETURN lb_result;
   END;

   /**
   * Prepare collection for IR
   *
   * @param pi_query_id       Query id  dq_queries.query_id
   * @param pv_query_result_format_id         Output format -- not used in this procedure
   * @param pv_values         List of params delimiter |
   */

   PROCEDURE Get_Query_Result_Ir (pi_query_id IN DQ_QUERIES.QUERY_ID%TYPE,
                                  pv_values IN VARCHAR2,
                                  pv_query_result_format_id DQ_QUERIES.QUERY_RESULT_FORMAT_ID%TYPE)
   IS
      lt_params wwv_flow_global.vc_arr2;
      lc_query_sql CLOB;
   BEGIN
      SELECT name
        BULK COLLECT INTO lt_params
        FROM dq_queries, TABLE (dq_queries.query_params)
       WHERE query_id = pi_query_id;

      SELECT query_sql_db
        INTO lc_query_sql
        FROM dq_queries
       WHERE query_id = pi_query_id;

      IF apex_collection.collection_exists (
            p_collection_name => v ('APP_USER'))
      THEN
         apex_collection.delete_collection (
            p_collection_name => v ('APP_USER'));
      END IF;

      apex_collection.CREATE_COLLECTION_FROM_QUERY_B (
         p_collection_name => v ('APP_USER'),
         p_query => lc_query_sql,
         p_names => lt_params,
         p_values => APEX_UTIL.string_to_table (pv_values, '|'),
         p_max_row_count => 10000);

      MERGE INTO dq_query_statistics dqs
           USING DUAL
              ON (    dqs.query_statistics_user_login = v ('APP_USER')
                  AND dqs.query_id = pi_query_id)
      WHEN NOT MATCHED
      THEN
         INSERT     (query_statistics_user_login,
                     query_id,
                     query_statistics_run_count)
             VALUES (v ('APP_USER'), pi_query_id, 1)
      WHEN MATCHED
      THEN
         UPDATE SET
            query_statistics_run_count = query_statistics_run_count + 1;
   END;

   /**
   * Parse query on Insert or Update. Save columns and parameters
   *
   * @param pi_query_id       Query id  dq_queries.query_id
   */

   PROCEDURE Parse_Query (pv_query_id NUMBER)
   IS
      ln_cur_cols NUMBER;
      ln_column_cnt NUMBER;
      l_columns_desc DBMS_SQL.desc_tab;

      lt_query t_query;

      ltt_params DQ_TBL_PARAMS;
      ltt_columns DQ_TBL_COLUMNS;
   BEGIN
      lt_query := Get_Parameters_From_Text (pv_query_id);

      BEGIN
         DELETE FROM TABLE (SELECT query_params
                              FROM dq_queries
                             WHERE query_id = pv_query_id);

         DELETE FROM TABLE (SELECT query_columns
                              FROM dq_queries
                             WHERE query_id = pv_query_id);
      EXCEPTION
         WHEN OTHERS
         THEN
            NULL;
      END;

      ltt_params := dq_tbl_params ();

      FOR indx IN 1 .. lt_query.parameters.COUNT
      LOOP
         ltt_params.EXTEND;
         ltt_params (ltt_params.COUNT) := lt_query.parameters (indx);
      END LOOP;

      ln_cur_cols := DBMS_SQL.open_cursor;

      DBMS_SQL.parse (ln_cur_cols, lt_query.text, DBMS_SQL.native);
      DBMS_SQL.describe_columns (ln_cur_cols, ln_column_cnt, l_columns_desc);
      DBMS_SQL.close_cursor (ln_cur_cols);

      ltt_columns := DQ_TBL_COLUMNS ();

      FOR i IN 1 .. ln_column_cnt
      LOOP
         ltt_columns.EXTEND;
         ltt_columns (ltt_columns.COUNT) :=
            dq_typ_columns (i, l_columns_desc (i).col_name);
      END LOOP;

      UPDATE dq_queries
         SET query_params = ltt_params,
             query_columns = ltt_columns,
             query_sql_db = lt_query.text
       WHERE query_id = pv_query_id;
   END;

   FUNCTION Get_Columns (pv_query_id NUMBER)
      RETURN CLOB
   IS
      l_json CLOB;
   BEGIN
      apex_json.initialize_clob_output (DBMS_LOB.CALL, TRUE, 2);
      apex_json.open_object ();
      apex_json.open_array ('cols');

      FOR i IN (  SELECT dqc.name
                    FROM dq_queries dq, TABLE (dq.query_columns) dqc
                   WHERE dq.query_id = 6
                ORDER BY dqc.id)
      LOOP
         apex_json.open_object ();
         apex_json.write ('name', i.name);
         apex_json.close_object;
      END LOOP;

      apex_json.close_array;
      apex_json.close_object;
      RETURN apex_json.get_clob_output;
   END;
END;
/

CREATE OR REPLACE PROCEDURE "DQ_RUN_QRY" (pi_query_id INTEGER, pv_query_values  VARCHAR2 DEFAULT NULL, pi_query_result_format_id INTEGER DEFAULT NULL)
IS
   lb_excel BLOB;
   lv_query_name VARCHAR2(255);
   lc_query_sql CLOB;

BEGIN

   SELECT
   query_name
   INTO
   lv_query_name
   FROM
   dq_queries
   WHERE query_id = pi_query_id;

   lb_excel := dq.Get_Query_Result(pi_query_id,pv_query_values,pi_query_result_format_id);

   htp.p('Content-Type: application/csv; charset=UTF-8');
   htp.p('Content-Disposition: inline; filename="'||lv_query_name ||'.csv";');

   htp.p('Content-Length: ' || dbms_lob.getlength(lb_excel));
   htp.p('Cache-Control: "no-cache"');
   htp.p('Set-Cookie: fileDownload=true; path=/');
   htp.p('');

   wpg_docload.download_file(lb_excel);
   htmldb_application.g_unrecoverable_error := true;

END;
/