2019-08-03 14:10:47 +02:00
package postgres
2022-05-06 11:54:44 +02:00
import (
"github.com/go-jet/jet/v2/internal/jet"
)
2019-08-03 14:10:47 +02:00
2022-02-11 13:09:49 +01:00
// This functions can be used, instead of its method counterparts, to have a better indentation of a complex condition
// in the Go code and in the generated SQL.
var (
// AND function adds AND operator between expressions.
AND = jet . AND
// OR function adds OR operator between expressions.
OR = jet . OR
)
2023-03-28 13:16:57 +02:00
// ROW function is used to create a tuple value that consists of a set of expressions or column values.
2024-10-17 14:12:21 +02:00
func ROW ( expressions ... Expression ) RowExpression {
return jet . ROW ( Dialect , expressions ... )
}
2019-08-03 14:10:47 +02:00
// ------------------ Mathematical functions ---------------//
2019-08-17 14:49:35 +02:00
// ABSf calculates absolute value from float expression
2019-08-03 14:10:47 +02:00
var ABSf = jet . ABSf
2019-08-17 14:49:35 +02:00
// ABSi calculates absolute value from int expression
2019-08-03 14:10:47 +02:00
var ABSi = jet . ABSi
2019-08-17 14:49:35 +02:00
// POW calculates power of base with exponent
2019-08-03 14:10:47 +02:00
var POW = jet . POW
2019-08-17 14:49:35 +02:00
// POWER calculates power of base with exponent
2019-08-03 14:10:47 +02:00
var POWER = jet . POWER
2019-08-17 14:49:35 +02:00
// SQRT calculates square root of numeric expression
2019-08-03 14:10:47 +02:00
var SQRT = jet . SQRT
2019-08-17 14:49:35 +02:00
// CBRT calculates cube root of numeric expression
2019-08-03 14:10:47 +02:00
var CBRT = jet . CBRT
2019-08-17 14:49:35 +02:00
// CEIL calculates ceil of float expression
2019-08-03 14:10:47 +02:00
var CEIL = jet . CEIL
2019-08-17 14:49:35 +02:00
// FLOOR calculates floor of float expression
2019-08-03 14:10:47 +02:00
var FLOOR = jet . FLOOR
2019-08-17 14:49:35 +02:00
// ROUND calculates round of a float expressions with optional precision
2019-08-03 14:10:47 +02:00
var ROUND = jet . ROUND
2019-08-17 14:49:35 +02:00
// SIGN returns sign of float expression
2019-08-03 14:10:47 +02:00
var SIGN = jet . SIGN
2019-08-17 14:49:35 +02:00
// TRUNC calculates trunc of float expression with optional precision
2019-08-03 14:10:47 +02:00
var TRUNC = jet . TRUNC
2019-08-17 14:49:35 +02:00
// LN calculates natural algorithm of float expression
2019-08-03 14:10:47 +02:00
var LN = jet . LN
2019-08-17 14:49:35 +02:00
// LOG calculates logarithm of float expression
2019-08-03 14:10:47 +02:00
var LOG = jet . LOG
// ----------------- Aggregate functions -------------------//
2019-08-17 14:49:35 +02:00
// AVG is aggregate function used to calculate avg value from numeric expression
2019-08-03 14:10:47 +02:00
var AVG = jet . AVG
2019-08-17 14:49:35 +02:00
// BIT_AND is aggregate function used to calculates the bitwise AND of all non-null input values, or null if none.
2019-08-03 14:10:47 +02:00
var BIT_AND = jet . BIT_AND
2019-08-17 14:49:35 +02:00
// BIT_OR is aggregate function used to calculates the bitwise OR of all non-null input values, or null if none.
2019-08-03 14:10:47 +02:00
var BIT_OR = jet . BIT_OR
2019-08-17 14:49:35 +02:00
// BOOL_AND is aggregate function. Returns true if all input values are true, otherwise false
2019-08-03 14:10:47 +02:00
var BOOL_AND = jet . BOOL_AND
2019-08-17 14:49:35 +02:00
// BOOL_OR is aggregate function. Returns true if at least one input value is true, otherwise false
2019-08-03 14:10:47 +02:00
var BOOL_OR = jet . BOOL_OR
2019-08-17 14:49:35 +02:00
// COUNT is aggregate function. Returns number of input rows for which the value of expression is not null.
2019-08-03 14:10:47 +02:00
var COUNT = jet . COUNT
2019-08-17 14:49:35 +02:00
// EVERY is aggregate function. Returns true if all input values are true, otherwise false
2019-08-03 14:10:47 +02:00
var EVERY = jet . EVERY
2019-08-17 14:49:35 +02:00
2019-10-18 10:09:56 +02:00
// MAX is aggregate function. Returns maximum value of expression across all input values
2019-09-30 14:42:04 +02:00
var MAX = jet . MAX
2019-08-17 14:49:35 +02:00
// MAXf is aggregate function. Returns maximum value of float expression across all input values
2019-08-03 14:10:47 +02:00
var MAXf = jet . MAXf
2019-08-17 14:49:35 +02:00
// MAXi is aggregate function. Returns maximum value of int expression across all input values
2019-08-03 14:10:47 +02:00
var MAXi = jet . MAXi
2019-08-17 14:49:35 +02:00
2019-09-30 14:42:04 +02:00
// MIN is aggregate function. Returns minimum value of expression across all input values.
var MIN = jet . MIN
2019-08-17 14:49:35 +02:00
// MINf is aggregate function. Returns minimum value of float expression across all input values
2019-08-03 14:10:47 +02:00
var MINf = jet . MINf
2019-08-17 14:49:35 +02:00
// MINi is aggregate function. Returns minimum value of int expression across all input values
2019-08-03 14:10:47 +02:00
var MINi = jet . MINi
2019-08-17 14:49:35 +02:00
2020-05-24 17:56:17 +02:00
// SUM is aggregate function. Returns sum of all expressions
var SUM = jet . SUM
2019-08-17 14:49:35 +02:00
// SUMf is aggregate function. Returns sum of expression across all float expressions
2019-08-03 14:10:47 +02:00
var SUMf = jet . SUMf
2019-08-17 14:49:35 +02:00
// SUMi is aggregate function. Returns sum of expression across all integer expression.
2019-08-03 14:10:47 +02:00
var SUMi = jet . SUMi
2019-09-17 13:34:47 +02:00
// -------------------- Window functions -----------------------//
// ROW_NUMBER returns number of the current row within its partition, counting from 1
var ROW_NUMBER = jet . ROW_NUMBER
// RANK of the current row with gaps; same as row_number of its first peer
var RANK = jet . RANK
// DENSE_RANK returns rank of the current row without gaps; this function counts peer groups
var DENSE_RANK = jet . DENSE_RANK
// PERCENT_RANK calculates relative rank of the current row: (rank - 1) / (total partition rows - 1)
var PERCENT_RANK = jet . PERCENT_RANK
// CUME_DIST calculates cumulative distribution: (number of partition rows preceding or peer with current row) / total partition rows
var CUME_DIST = jet . CUME_DIST
// NTILE returns integer ranging from 1 to the argument value, dividing the partition as equally as possible
var NTILE = jet . NTILE
// LAG returns value evaluated at the row that is offset rows before the current row within the partition;
// if there is no such row, instead return default (which must be of the same type as value).
// Both offset and default are evaluated with respect to the current row.
// If omitted, offset defaults to 1 and default to null
var LAG = jet . LAG
// LEAD returns value evaluated at the row that is offset rows after the current row within the partition;
// if there is no such row, instead return default (which must be of the same type as value).
// Both offset and default are evaluated with respect to the current row.
// If omitted, offset defaults to 1 and default to null
var LEAD = jet . LEAD
// FIRST_VALUE returns value evaluated at the row that is the first row of the window frame
var FIRST_VALUE = jet . FIRST_VALUE
// LAST_VALUE returns value evaluated at the row that is the last row of the window frame
var LAST_VALUE = jet . LAST_VALUE
// NTH_VALUE returns value evaluated at the row that is the nth row of the window frame (counting from 1); null if no such row
var NTH_VALUE = jet . NTH_VALUE
2019-08-03 14:10:47 +02:00
//--------------------- String functions ------------------//
2019-08-17 14:49:35 +02:00
// BIT_LENGTH returns number of bits in string expression
2019-08-03 14:10:47 +02:00
var BIT_LENGTH = jet . BIT_LENGTH
2019-08-17 14:49:35 +02:00
// CHAR_LENGTH returns number of characters in string expression
2019-08-03 14:10:47 +02:00
var CHAR_LENGTH = jet . CHAR_LENGTH
2019-08-17 14:49:35 +02:00
// OCTET_LENGTH returns number of bytes in string expression
2019-08-03 14:10:47 +02:00
var OCTET_LENGTH = jet . OCTET_LENGTH
2019-08-17 14:49:35 +02:00
// LOWER returns string expression in lower case
2019-08-03 14:10:47 +02:00
var LOWER = jet . LOWER
2019-08-17 14:49:35 +02:00
// UPPER returns string expression in upper case
2019-08-03 14:10:47 +02:00
var UPPER = jet . UPPER
2019-08-17 14:49:35 +02:00
// BTRIM removes the longest string consisting only of characters
// in characters (a space by default) from the start and end of string
2019-08-03 14:10:47 +02:00
var BTRIM = jet . BTRIM
2019-08-17 14:49:35 +02:00
// LTRIM removes the longest string containing only characters
// from characters (a space by default) from the start of string
2019-08-03 14:10:47 +02:00
var LTRIM = jet . LTRIM
2019-08-17 14:49:35 +02:00
// RTRIM removes the longest string containing only characters
// from characters (a space by default) from the end of string
2019-08-03 14:10:47 +02:00
var RTRIM = jet . RTRIM
2019-08-17 14:49:35 +02:00
// CHR returns character with the given code.
2019-08-03 14:10:47 +02:00
var CHR = jet . CHR
2019-08-06 10:29:04 +02:00
2019-08-17 14:49:35 +02:00
// CONCAT adds two or more expressions together
2019-08-06 10:29:04 +02:00
var CONCAT = func ( expressions ... Expression ) StringExpression {
2019-08-11 09:52:02 +02:00
return jet . CONCAT ( explicitLiteralCasts ( expressions ... ) ... )
2019-08-06 10:29:04 +02:00
}
2019-08-17 14:49:35 +02:00
// CONCAT_WS adds two or more expressions together with a separator.
2019-08-14 10:11:43 +02:00
func CONCAT_WS ( separator Expression , expressions ... Expression ) StringExpression {
return jet . CONCAT_WS ( explicitLiteralCast ( separator ) , explicitLiteralCasts ( expressions ... ) ... )
2019-08-06 10:29:04 +02:00
}
2025-02-28 18:23:15 +01:00
// Character encodings for CONVERT, CONVERT_FROM and CONVERT_TO functions
var (
2025-03-08 19:01:37 +01:00
UTF8 = StringExp ( jet . FixedLiteral ( "UTF8" ) )
LATIN1 = StringExp ( jet . FixedLiteral ( "LATIN1" ) )
LATIN2 = StringExp ( jet . FixedLiteral ( "LATIN2" ) )
LATIN3 = StringExp ( jet . FixedLiteral ( "LATIN3" ) )
LATIN4 = StringExp ( jet . FixedLiteral ( "LATIN4" ) )
WIN1252 = StringExp ( jet . FixedLiteral ( "WIN1252" ) )
ISO_8859_5 = StringExp ( jet . FixedLiteral ( "ISO_8859_5" ) )
ISO_8859_6 = StringExp ( jet . FixedLiteral ( "ISO_8859_6" ) )
ISO_8859_7 = StringExp ( jet . FixedLiteral ( "ISO_8859_7" ) )
ISO_8859_8 = StringExp ( jet . FixedLiteral ( "ISO_8859_8" ) )
KOI8R = StringExp ( jet . FixedLiteral ( "KOI8R" ) )
KOI8U = StringExp ( jet . FixedLiteral ( "KOI8U" ) )
2025-02-28 18:23:15 +01:00
)
2019-08-17 14:49:35 +02:00
// CONVERT converts string to dest_encoding. The original encoding is
// specified by src_encoding. The string must be valid in this encoding.
2025-02-28 18:23:15 +01:00
func CONVERT ( str ByteaExpression , srcEncoding StringExpression , destEncoding StringExpression ) ByteaExpression {
return jet . CONVERT ( str , srcEncoding , destEncoding )
}
2019-08-17 14:49:35 +02:00
// CONVERT_FROM converts string to the database encoding. The original
// encoding is specified by src_encoding. The string must be valid in this encoding.
2019-08-03 14:10:47 +02:00
var CONVERT_FROM = jet . CONVERT_FROM
2019-08-17 14:49:35 +02:00
// CONVERT_TO converts string to dest_encoding.
2019-08-03 14:10:47 +02:00
var CONVERT_TO = jet . CONVERT_TO
2019-08-17 14:49:35 +02:00
2025-02-28 18:23:15 +01:00
// ENCODE/DECODE textual formats
var (
2025-03-08 19:01:37 +01:00
Base64 = StringExp ( jet . FixedLiteral ( "base64" ) )
Escape = StringExp ( jet . FixedLiteral ( "escape" ) )
Hex = StringExp ( jet . FixedLiteral ( "hex" ) )
2025-02-28 18:23:15 +01:00
)
2019-08-17 14:49:35 +02:00
// ENCODE encodes binary data into a textual representation.
// Supported formats are: base64, hex, escape. escape converts zero bytes and
// high-bit-set bytes to octal sequences (\nnn) and doubles backslashes.
2019-08-03 14:10:47 +02:00
var ENCODE = jet . ENCODE
2019-08-17 14:49:35 +02:00
// DECODE decodes binary data from textual representation in string.
// Options for format are same as in encode.
2019-08-03 14:10:47 +02:00
var DECODE = jet . DECODE
2019-08-06 10:29:04 +02:00
2025-02-28 18:23:15 +01:00
// FORMAT formats the arguments according to a format string. This function is similar to the C function sprintf.
2019-08-06 10:29:04 +02:00
func FORMAT ( formatStr StringExpression , formatArgs ... Expression ) StringExpression {
2019-08-11 09:52:02 +02:00
return jet . FORMAT ( formatStr , explicitLiteralCasts ( formatArgs ... ) ... )
2019-08-06 10:29:04 +02:00
}
2019-08-17 14:49:35 +02:00
// INITCAP converts the first letter of each word to upper case
// and the rest to lower case. Words are sequences of alphanumeric
// characters separated by non-alphanumeric characters.
2019-08-03 14:10:47 +02:00
var INITCAP = jet . INITCAP
2019-08-17 14:49:35 +02:00
// LEFT returns first n characters in the string.
// When n is negative, return all but last |n| characters.
2019-08-03 14:10:47 +02:00
var LEFT = jet . LEFT
2019-08-17 14:49:35 +02:00
// RIGHT returns last n characters in the string.
// When n is negative, return all but first |n| characters.
2019-08-03 14:10:47 +02:00
var RIGHT = jet . RIGHT
2019-08-17 14:49:35 +02:00
// LENGTH returns number of characters in string with a given encoding
2019-08-03 14:10:47 +02:00
var LENGTH = jet . LENGTH
2019-08-17 14:49:35 +02:00
// LPAD fills up the string to length length by prepending the characters
// fill (a space by default). If the string is already longer than length
// then it is truncated (on the right).
2019-08-03 14:10:47 +02:00
var LPAD = jet . LPAD
2019-08-17 14:49:35 +02:00
// RPAD fills up the string to length length by appending the characters
// fill (a space by default). If the string is already longer than length then it is truncated.
2019-08-03 14:10:47 +02:00
var RPAD = jet . RPAD
2019-08-17 14:49:35 +02:00
2025-02-28 18:23:15 +01:00
// BIT_COUNT returns the number of bits set in the binary string (also known as “popcount”).
var BIT_COUNT = jet . BIT_COUNT
// GET_BIT extracts n'th bit from binary string.
func GET_BIT ( bytes ByteaExpression , n IntegerExpression ) IntegerExpression {
return IntExp ( Func ( "GET_BIT" , bytes , n ) )
}
// GET_BYTE extracts n'th byte from binary string.
func GET_BYTE ( bytes ByteaExpression , n IntegerExpression ) IntegerExpression {
return IntExp ( Func ( "GET_BYTE" , bytes , n ) )
}
// SET_BIT sets n'th bit in binary string to newvalue.
func SET_BIT ( bytes ByteaExpression , n IntegerExpression , newValue IntegerExpression ) ByteaExpression {
return ByteaExp ( Func ( "SET_BIT" , bytes , n , newValue ) )
}
// SET_BYTE sets n'th byte in binary string to newvalue.
func SET_BYTE ( bytes ByteaExpression , n IntegerExpression , newValue IntegerExpression ) ByteaExpression {
return ByteaExp ( Func ( "SET_BYTE" , bytes , n , newValue ) )
}
// SHA224 computes the SHA-224 hash of the binary string.
func SHA224 ( bytes ByteaExpression ) ByteaExpression {
return ByteaExp ( Func ( "SHA224" , bytes ) )
}
// SHA256 computes the SHA-256 hash of the binary string.
func SHA256 ( bytes ByteaExpression ) ByteaExpression {
return ByteaExp ( Func ( "SHA256" , bytes ) )
}
// SHA384 computes the SHA-384 hash of the binary string.
func SHA384 ( bytes ByteaExpression ) ByteaExpression {
return ByteaExp ( Func ( "SHA384" , bytes ) )
}
// SHA512 computes the SHA-512 hash of the binary string.
func SHA512 ( bytes ByteaExpression ) ByteaExpression {
return ByteaExp ( Func ( "SHA512" , bytes ) )
}
2019-08-17 14:49:35 +02:00
// MD5 calculates the MD5 hash of string, returning the result in hexadecimal
2019-08-03 14:10:47 +02:00
var MD5 = jet . MD5
2019-08-17 14:49:35 +02:00
// REPEAT repeats string the specified number of times
2019-08-03 14:10:47 +02:00
var REPEAT = jet . REPEAT
2019-08-17 14:49:35 +02:00
// REPLACE replaces all occurrences in string of substring from with substring to
2019-08-03 14:10:47 +02:00
var REPLACE = jet . REPLACE
2019-08-17 14:49:35 +02:00
// REVERSE returns reversed string.
2019-08-03 14:10:47 +02:00
var REVERSE = jet . REVERSE
2019-08-17 14:49:35 +02:00
// STRPOS returns location of specified substring (same as position(substring in string),
// but note the reversed argument order)
2019-08-03 14:10:47 +02:00
var STRPOS = jet . STRPOS
2019-08-17 14:49:35 +02:00
// SUBSTR extracts substring
2019-08-03 14:10:47 +02:00
var SUBSTR = jet . SUBSTR
2019-08-17 14:49:35 +02:00
// TO_ASCII convert string to ASCII from another encoding
2019-08-03 14:10:47 +02:00
var TO_ASCII = jet . TO_ASCII
2019-08-17 14:49:35 +02:00
// TO_HEX converts number to its equivalent hexadecimal representation
2019-08-03 14:10:47 +02:00
var TO_HEX = jet . TO_HEX
2025-10-16 15:09:07 +02:00
//---------- Range Functions ----------------------//
2019-08-03 14:10:47 +02:00
2024-01-31 15:30:09 +01:00
// LOWER_BOUND returns range expressions lower bound
func LOWER_BOUND [ T Expression ] ( expression jet . Range [ T ] ) T {
return jet . LOWER_BOUND [ T ] ( expression )
}
// UPPER_BOUND returns range expressions upper bound
func UPPER_BOUND [ T Expression ] ( expression jet . Range [ T ] ) T {
return jet . UPPER_BOUND [ T ] ( expression )
}
2025-10-16 15:09:07 +02:00
// ---------- Array Functions ----------------------//
// ANY should be used in combination with a boolean operator. The result of ANY is "true" if any true result is obtained
func ANY [ E Expression ] ( arr Array [ E ] ) E {
return jet . CastToArrayElemType ( arr , Func ( "ANY" , arr ) )
}
// ALL should be used in combination with a boolean operator. The result of ALL is “true” if all comparisons yield true
func ALL [ E Expression ] ( arr Array [ E ] ) E {
return jet . CastToArrayElemType ( arr , Func ( "ALL" , arr ) )
}
// ARRAY_APPEND appends an element to the end of an array
func ARRAY_APPEND [ E Expression ] ( arr Array [ E ] , elem E ) Array [ E ] {
return ArrayExp [ E ] ( Func ( "ARRAY_APPEND" , arr , elem ) )
}
// ARRAY_CAT concatenates two arrays
func ARRAY_CAT [ E Expression ] ( arr1 , arr2 Array [ E ] ) Array [ E ] {
return ArrayExp [ E ] ( Func ( "ARRAY_CAT" , arr1 , arr2 ) )
}
// ARRAY_DIMS returns a text representation of the array's dimensions.
func ARRAY_DIMS [ E Expression ] ( arr Array [ E ] ) StringExpression {
return StringExp ( Func ( "ARRAY_DIMS" , arr ) )
}
// ARRAY_LENGTH returns the length of the requested array dimension.
// Produces NULL instead of 0 for empty or missing array dimensions.
func ARRAY_LENGTH [ E Expression ] ( arr Array [ E ] , elem IntegerExpression ) IntegerExpression {
return IntExp ( Func ( "ARRAY_LENGTH" , arr , elem ) )
}
// ARRAY_LOWER returns the lower bound of the requested array dimension.
func ARRAY_LOWER [ E Expression ] ( arr Array [ E ] ) IntegerExpression {
return IntExp ( Func ( "ARRAY_LOWER" , arr ) )
}
// ARRAY_NDIMS returns the number of dimensions of the array.
func ARRAY_NDIMS [ E Expression ] ( arr Array [ E ] ) IntegerExpression {
return IntExp ( Func ( "ARRAY_NDIMS" , arr ) )
}
// ARRAY_POSITION returns the subscript of the first occurrence of the second argument in the array, or NULL if it's not present.
// If the third argument is given, the search begins at that subscript.
// The array must be one-dimensional.
// Comparisons are done using IS NOT DISTINCT FROM semantics, so it is possible to search for NULL.
2025-10-17 13:41:08 +02:00
func ARRAY_POSITION [ E Expression ] ( arr Array [ E ] , elem E , start ... IntegerExpression ) IntegerExpression {
return IntExp ( Func ( "ARRAY_POSITION" , optionalAppend ( [ ] Expression { arr , elem } , start ) ... ) )
2025-10-16 15:09:07 +02:00
}
// ARRAY_POSITIONS returns an array of the subscripts of all occurrences of the second argument in the array given as first argument.
// The array must be one-dimensional.
// Comparisons are done using IS NOT DISTINCT FROM semantics, so it is possible to search for NULL.
// NULL is returned only if the array is NULL; if the value is not found in the array, an empty array is returned.
func ARRAY_POSITIONS [ E Expression ] ( arr Array [ E ] , elem E ) Array [ IntegerExpression ] {
return ArrayExp [ IntegerExpression ] ( Func ( "ARRAY_POSITIONS" , arr , elem ) )
}
// ARRAY_PREPEND prepends an element to the beginning of an array
func ARRAY_PREPEND [ E Expression ] ( el E , arr Array [ E ] ) Array [ E ] {
return ArrayExp [ E ] ( Func ( "ARRAY_PREPEND" , el , arr ) )
}
// ARRAY_REMOVE removes all elements equal to the given value from the array. The array must be one-dimensional.
// Comparisons are done using IS NOT DISTINCT FROM semantics, so it is possible to remove NULLs.
func ARRAY_REMOVE [ E Expression ] ( arr Array [ E ] , elem Expression ) IntegerExpression {
return IntExp ( Func ( "ARRAY_REMOVE" , arr , elem ) )
}
// ARRAY_REPLACE replaces each array element equal to the second argument with the third argument.
func ARRAY_REPLACE [ E Expression ] ( arr Array [ E ] , existing E , new E ) Array [ E ] {
return ArrayExp [ E ] ( Func ( "ARRAY_REPLACE" , arr , existing , new ) )
}
// ARRAY_REVERSE reverses the first dimension of the array.
func ARRAY_REVERSE [ E Expression ] ( arr Array [ E ] ) Array [ E ] {
return ArrayExp [ E ] ( Func ( "ARRAY_REVERSE" , arr ) )
}
// ARRAY_SAMPLE returns an array of n items randomly selected from array.
// n may not exceed the length of array's first dimension.
// If array is multi-dimensional, an “item” is a slice having a given first subscript.
func ARRAY_SAMPLE [ E Expression ] ( arr Array [ E ] , n IntegerExpression ) Array [ E ] {
return ArrayExp [ E ] ( Func ( "ARRAY_SAMPLE" , arr , n ) )
}
// ARRAY_SHUFFLE randomly shuffles the first dimension of the array.
func ARRAY_SHUFFLE [ E Expression ] ( arr Array [ E ] ) Array [ E ] {
return ArrayExp [ E ] ( Func ( "ARRAY_SHUFFLE" , arr ) )
}
// ARRAY_SORT sorts the first dimension of the array.
// The sort order is determined by the default sort ordering of the array's element type; however, if the element type is collatable, the collation to use can be specified by adding a COLLATE clause to the array argument.
//
// If descending is true then sort in descending order, otherwise ascending order.
// If omitted, the default is ascending order.
// If nulls_first is true then nulls appear before non-null values, otherwise nulls appear after non-null values.
// If omitted, nulls_first is taken to have the same value as descending.
func ARRAY_SORT [ E Expression ] ( arr Array [ E ] , desc BoolExpression , nullFirst ... BoolExpression ) Array [ E ] {
return ArrayExp [ E ] ( Func ( "ARRAY_SORT" , optionalAppend ( [ ] Expression { arr , desc } , nullFirst ) ... ) )
}
// ARRAY_TO_STRING Converts each array element to its text representation, and concatenates those separated by the delimiter string.
// If null_string is given and is not NULL, then NULL array entries are represented by that string; otherwise, they are omitted.
func ARRAY_TO_STRING [ T Expression ] ( arr Array [ T ] , delim StringExpression ) StringExpression {
return StringExp ( Func ( "ARRAY_TO_STRING" , arr , delim ) )
}
// ARRAY_UPPER returns the upper bound of the requested array dimension.
func ARRAY_UPPER [ E Expression ] ( arr Array [ E ] , dim IntegerExpression ) IntegerExpression {
return IntExp ( Func ( "ARRAY_UPPER" , arr , dim ) )
}
// CARDINALITY returns the total number of elements in the array, or 0 if the array is empty.
func CARDINALITY [ E Expression ] ( arr Array [ E ] ) IntegerExpression {
return IntExp ( Func ( "CARDINALITY" , arr ) )
}
// TRIM_ARRAY trims an array by removing the last n elements. If the array is multidimensional, only the first dimension is trimmed.
func TRIM_ARRAY [ E Expression ] ( arr Array [ E ] , n IntegerExpression ) Array [ E ] {
return ArrayExp [ E ] ( Func ( "TRIM_ARRAY" , arr , n ) )
}
// ARRAY constructor is an expression that builds an array value using values for its member elements.
func ARRAY [ T Expression ] ( elems ... T ) Array [ T ] {
return jet . ARRAY [ T ] ( elems ... )
}
func optionalAppend [ O Expression ] ( elem [ ] Expression , optional [ ] O ) [ ] Expression {
if len ( optional ) == 0 {
return elem
}
return append ( elem , optional [ 0 ] )
}
//---------- Data Type Formatting Functions ----------------------//
2024-01-31 15:30:09 +01:00
2019-08-17 14:49:35 +02:00
// TO_CHAR converts expression to string with format
2019-08-03 14:10:47 +02:00
var TO_CHAR = jet . TO_CHAR
2019-08-17 14:49:35 +02:00
// TO_DATE converts string to date using format
2019-08-03 14:10:47 +02:00
var TO_DATE = jet . TO_DATE
2019-08-17 14:49:35 +02:00
// TO_NUMBER converts string to numeric using format
2019-08-03 14:10:47 +02:00
var TO_NUMBER = jet . TO_NUMBER
2019-08-17 14:49:35 +02:00
// TO_TIMESTAMP converts string to time stamp with time zone using format
2019-08-03 14:10:47 +02:00
var TO_TIMESTAMP = jet . TO_TIMESTAMP
//----------------- Date/Time Functions and Operators ------------//
2022-05-06 11:54:44 +02:00
// Additional time unit types for EXTRACT function
const (
DOW unit = MILLENNIUM + 1 + iota
DOY
EPOCH
ISODOW
ISOYEAR
JULIAN
QUARTER
TIMEZONE
TIMEZONE_HOUR
TIMEZONE_MINUTE
)
// EXTRACT function retrieves subfields such as year or hour from date/time values
2022-08-23 12:38:16 +02:00
//
// EXTRACT(DAY, User.CreatedAt)
2022-05-06 11:54:44 +02:00
func EXTRACT ( field unit , from Expression ) FloatExpression {
return FloatExp ( jet . EXTRACT ( unitToString ( field ) , from ) )
}
2019-08-17 14:49:35 +02:00
// CURRENT_DATE returns current date
2019-08-03 14:10:47 +02:00
var CURRENT_DATE = jet . CURRENT_DATE
2019-08-17 14:49:35 +02:00
// CURRENT_TIME returns current time with time zone
2019-08-03 14:10:47 +02:00
var CURRENT_TIME = jet . CURRENT_TIME
2019-08-17 14:49:35 +02:00
// CURRENT_TIMESTAMP returns current timestamp with time zone
2019-08-03 14:10:47 +02:00
var CURRENT_TIMESTAMP = jet . CURRENT_TIMESTAMP
2019-08-17 14:49:35 +02:00
// LOCALTIME returns local time of day using optional precision
2019-08-03 14:10:47 +02:00
var LOCALTIME = jet . LOCALTIME
2019-08-17 14:49:35 +02:00
// LOCALTIMESTAMP returns current date and time using optional precision
2019-08-03 14:10:47 +02:00
var LOCALTIMESTAMP = jet . LOCALTIMESTAMP
2019-08-17 14:49:35 +02:00
// NOW returns current date and time
2019-08-03 14:10:47 +02:00
var NOW = jet . NOW
2024-09-23 09:11:53 +02:00
// DATE_TRUNC returns the truncated date and time using optional time zone.
// Use TimestampzExp if you need timestamp with time zone and IntervalExp if you need interval.
2024-09-20 09:47:40 +02:00
func DATE_TRUNC ( field unit , source Expression , timezone ... string ) TimestampExpression {
if len ( timezone ) > 0 {
return jet . NewTimestampFunc ( "DATE_TRUNC" , jet . FixedLiteral ( unitToString ( field ) ) , source , jet . FixedLiteral ( timezone [ 0 ] ) )
}
return jet . NewTimestampFunc ( "DATE_TRUNC" , jet . FixedLiteral ( unitToString ( field ) ) , source )
}
2024-09-29 14:55:05 +02:00
// GENERATE_SERIES generates a series of values from start to stop, with a step size of step.
func GENERATE_SERIES ( start Expression , stop Expression , step ... Expression ) Expression {
if len ( step ) > 0 {
2026-02-02 13:18:19 +01:00
return Func ( "GENERATE_SERIES" , start , stop , step [ 0 ] )
2024-09-29 14:55:05 +02:00
}
2026-02-02 13:18:19 +01:00
return Func ( "GENERATE_SERIES" , start , stop )
2024-09-29 14:55:05 +02:00
}
2019-08-03 14:10:47 +02:00
// --------------- Conditional Expressions Functions -------------//
2019-08-17 14:49:35 +02:00
// COALESCE function returns the first of its arguments that is not null.
2019-08-03 14:10:47 +02:00
var COALESCE = jet . COALESCE
2019-08-17 14:49:35 +02:00
// NULLIF function returns a null value if value1 equals value2; otherwise it returns value1.
2019-08-03 14:10:47 +02:00
var NULLIF = jet . NULLIF
2019-08-17 14:49:35 +02:00
// GREATEST selects the largest value from a list of expressions
2019-08-03 14:10:47 +02:00
var GREATEST = jet . GREATEST
2019-08-17 14:49:35 +02:00
// LEAST selects the smallest value from a list of expressions
2019-08-03 14:10:47 +02:00
var LEAST = jet . LEAST
2019-08-17 14:49:35 +02:00
// EXISTS checks for existence of the rows in subQuery
2019-08-03 14:10:47 +02:00
var EXISTS = jet . EXISTS
2019-08-17 14:49:35 +02:00
// CASE create CASE operator with optional list of expressions
2019-08-03 14:10:47 +02:00
var CASE = jet . CASE
2019-08-06 10:29:04 +02:00
2019-08-11 09:52:02 +02:00
func explicitLiteralCasts ( expressions ... Expression ) [ ] jet . Expression {
2019-08-06 10:29:04 +02:00
ret := [ ] jet . Expression { }
for _ , exp := range expressions {
2019-08-11 09:52:02 +02:00
ret = append ( ret , explicitLiteralCast ( exp ) )
2019-08-06 10:29:04 +02:00
}
return ret
}
2019-08-11 09:52:02 +02:00
func explicitLiteralCast ( expresion Expression ) jet . Expression {
2019-08-06 10:29:04 +02:00
if _ , ok := expresion . ( jet . LiteralExpression ) ; ! ok {
return expresion
}
switch expresion . ( type ) {
case jet . BoolExpression :
return CAST ( expresion ) . AS_BOOL ( )
case jet . IntegerExpression :
return CAST ( expresion ) . AS_INTEGER ( )
case jet . FloatExpression :
return CAST ( expresion ) . AS_NUMERIC ( )
case jet . StringExpression :
return CAST ( expresion ) . AS_TEXT ( )
}
return expresion
}
2021-12-07 14:07:44 +01:00
// MODE computes the most frequent value of the aggregated argument
var MODE = jet . MODE
// PERCENTILE_CONT computes a value corresponding to the specified fraction within the ordered set of
// aggregated argument values. This will interpolate between adjacent input items if needed.
func PERCENTILE_CONT ( fraction FloatExpression ) * jet . OrderSetAggregateFunc {
return jet . PERCENTILE_CONT ( castFloatLiteral ( fraction ) )
}
// PERCENTILE_DISC computes the first value within the ordered set of aggregated argument values whose position
// in the ordering equals or exceeds the specified fraction. The aggregated argument must be of a sortable type.
func PERCENTILE_DISC ( fraction FloatExpression ) * jet . OrderSetAggregateFunc {
return jet . PERCENTILE_DISC ( castFloatLiteral ( fraction ) )
}
func castFloatLiteral ( fraction FloatExpression ) FloatExpression {
if _ , ok := fraction . ( jet . LiteralExpression ) ; ok {
return CAST ( fraction ) . AS_DOUBLE ( ) // to make postgres aware of the type
}
return fraction
}
2023-03-28 13:16:57 +02:00
// ----------------- Group By operators --------------------------//
// GROUPING_SETS operator allows grouping of the rows in a table by multiple sets of columns(or expressions) in a single query.
// This can be useful when we want to analyze data by different combinations of columns, without having to write separate
// queries for each combination. GROUPING_SETS sets of columns are constructed with WRAP method.
//
// GROUPING_SETS(
// WRAP(Inventory.FilmID, Inventory.StoreID),
// WRAP(),
// ),
var GROUPING_SETS = jet . GROUPING_SETS
2024-10-17 14:12:21 +02:00
// WRAP surrounds a list of expressions or columns with parentheses, producing new row: (expression1, expression2, ...)
// The construct (a, b) is normally recognized in expressions as a row constructor. WRAP and ROW methods behave exactly the same,
// except when used in GROUPING_SETS and VALUES. In these contexts, WRAP must be used instead of ROW.
func WRAP ( expressions ... Expression ) RowExpression {
return jet . WRAP ( Dialect , expressions ... )
}
2023-03-28 13:16:57 +02:00
// ROLLUP operator is used with the GROUP BY clause to generate all prefixes of a group of columns including the empty list.
// It creates extra rows in the result set that represent the subtotal values for each combination of columns.
var ROLLUP = jet . ROLLUP
// CUBE operator is used with the GROUP BY clause to generate subtotals for all possible combinations of a group of columns.
// It creates extra rows in the result set that represent the subtotal values for each combination of columns.
var CUBE = jet . CUBE
// GROUPING function is used to identify which columns are included in a grouping set or a subtotal row. It takes as input
// the name of a column and returns 1 if the column is not included in the current grouping set, and 0 otherwise.
// It can be also used with multiple parameters to check if a set of columns is included in the current grouping set. The result
// of the GROUPING function would then be an integer bit mask having 1’ s for the arguments which have GROUPING(argument) as 1.
var GROUPING = jet . GROUPING
2024-01-31 15:30:09 +01:00
2024-02-27 10:48:57 +01:00
// range constructor functions
2024-01-31 15:30:09 +01:00
var (
// DATE_RANGE constructor function to create a date range
DATE_RANGE = jet . DateRange
2024-02-27 10:48:57 +01:00
// NUM_RANGE constructor function to create a numeric range
NUM_RANGE = jet . NumRange
// TS_RANGE constructor function to create a timestamp range
TS_RANGE = jet . TsRange
// TSTZ_RANGE constructor function to create a timestampz range
TSTZ_RANGE = jet . TstzRange
2024-01-31 15:30:09 +01:00
// INT4_RANGE constructor function to create a int4 range
INT4_RANGE = jet . Int4Range
// INT8_RANGE constructor function to create a int8 range
INT8_RANGE = jet . Int8Range
)