<commit_before>﻿namespace Rezoom.SQL.SQLite
open System
open System.Data
open System.Collections.Generic
open System.Globalization
open Rezoom.SQL
open Rezoom.SQL.Mapping
open Rezoom.SQL.BackendUtilities
open Rezoom.SQL.Translators

type private SQLiteLiteral() =
    inherit DefaultLiteralTranslator()
    override __.BooleanLiteral(t) = CommandText <| if t then "1" else "0"

type private SQLiteExpression(statement : StatementTranslator, indexer) =
    inherit DefaultExprTranslator(statement, indexer)
    let literal = SQLiteLiteral()
    override __.Literal = upcast literal
    override __.TypeName(name) =
        (Seq.singleton << text) <|
            match name with
            | BooleanTypeName
            | IntegerTypeName Integer8
            | IntegerTypeName Integer16
            | IntegerTypeName Integer32
            | IntegerTypeName Integer64 -> "INT"
            | FloatTypeName Float32
            | FloatTypeName Float64 -> "FLOAT"
            | StringTypeName(_) -> "VARCHAR"
            | BinaryTypeName(_) -> "BLOB"
            | DecimalTypeName
            | DateTimeTypeName
            | DateTimeOffsetTypeName -> failwith <| sprintf "Unsupported type ``%A``" name

type private SQLiteStatement(indexer : IParameterIndexer) as this =
    inherit DefaultStatementTranslator(Name("SQLITE"), indexer)
    let expr = SQLiteExpression(this :> StatementTranslator, indexer)
    override __.Expr = upcast expr

module SQLiteFunctions =
    open Rezoom.SQL.FunctionDeclarations
    let private minmax name =
        let num = a' |> constrained [ int64; float64; decimal ]
        {   FunctionName = Name(name)
            FixedArguments = [| num |]
            VariableArgument = None
            Output = a'
            Aggregate =
                function
                | ArgumentWildcard -> None
                | ArgumentList (_, exprs) ->
                    if exprs.Length = 1 then
                        Some { AllowWildcard = false; AllowDistinct = false }
                    else
                        None
            Idempotent = false
        } |> withVarArg a'
    let functions =
        let int = int64
        [|  // core functions from https://www.sqlite.org/lang_corefunc.html
            func "abs" [ a' ] a'
            proc "changes" [] int
            func "char" [] string |> withVarArg int
            func "coalesce" [ a'; a' ] a' |> withVarArg a'
            func "glob" [ string; string ] boolean
            func "hex" [ binary ] string
            func "ifnull" [ a'; a' ] a'<commit_msg>Remove numeric constraints on arguments to max/min SQLite functions.<commit_after>﻿namespace Rezoom.SQL.SQLite
open System
open System.Data
open System.Collections.Generic
open System.Globalization
open Rezoom.SQL
open Rezoom.SQL.Mapping
open Rezoom.SQL.BackendUtilities
open Rezoom.SQL.Translators

type private SQLiteLiteral() =
    inherit DefaultLiteralTranslator()
    override __.BooleanLiteral(t) = CommandText <| if t then "1" else "0"

type private SQLiteExpression(statement : StatementTranslator, indexer) =
    inherit DefaultExprTranslator(statement, indexer)
    let literal = SQLiteLiteral()
    override __.Literal = upcast literal
    override __.TypeName(name) =
        (Seq.singleton << text) <|
            match name with
            | BooleanTypeName
            | IntegerTypeName Integer8
            | IntegerTypeName Integer16
            | IntegerTypeName Integer32
            | IntegerTypeName Integer64 -> "INT"
            | FloatTypeName Float32
            | FloatTypeName Float64 -> "FLOAT"
            | StringTypeName(_) -> "VARCHAR"
            | BinaryTypeName(_) -> "BLOB"
            | DecimalTypeName
            | DateTimeTypeName
            | DateTimeOffsetTypeName -> failwith <| sprintf "Unsupported type ``%A``" name

type private SQLiteStatement(indexer : IParameterIndexer) as this =
    inherit DefaultStatementTranslator(Name("SQLITE"), indexer)
    let expr = SQLiteExpression(this :> StatementTranslator, indexer)
    override __.Expr = upcast expr

module SQLiteFunctions =
    open Rezoom.SQL.FunctionDeclarations
    let private minmax name =
        {   FunctionName = Name(name)
            FixedArguments = [| a' |]
            VariableArgument = None
            Output = a'
            Aggregate =
                function
                | ArgumentWildcard -> None
                | ArgumentList (_, exprs) ->
                    if exprs.Length = 1 then
                        Some { AllowWildcard = false; AllowDistinct = false }
                    else
                        None
            Idempotent = false
        } |> withVarArg a'
    let functions =
        let int = int64
        [|  // core functions from https://www.sqlite.org/lang_corefunc.html
            func "abs" [ a' ] a'
            proc "changes" [] int
            func "char" [] string |> withVarArg int
            func "coalesce" [ a'; a' ] a' |> withVarArg a'
            func "glob" [ string; string ] boolean
            func "hex" [ binary ] string
            func "ifnull" [ a'; a' ] a'
{'fraction_non_alphanumeric': 0.08595594020456333, 'fraction_numerical': 0.007081038552321007, 'mean_word_length': 2.0485611510791366, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 26, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7419817', 'n_tokens_mistral': 1378, 'n_tokens_neox': 1341, 'n_words': 372}