from clickhouse_cli.clickhouse.sqlparse_patch import KEYWORDS as sqlparse_keywords


# TODO: Run `SELECT name FROM system.functions` instead of this hardcoding
FUNCTIONS = (
    'abs',
    'acos',
    'addDays',
    'addHours',
    'addMinutes',
    'addMonths',
    'addQuarters',
    'addSeconds',
    'addWeeks',
    'addYears',
    'alphaTokens',
    'and',
    'any',
    'anyHeavy',
    'anyLast',
    'appendTrailingCharIfAbsent',
    'argMax',
    'argMin',
    'array',
    'arrayAll',
    'arrayConcat',
    'arrayCount',
    'arrayCumSum',
    'arrayCumSumNonNegative',
    'arrayDifference',
    'arrayDistinct',
    'arrayElement',
    'arrayEnumerate',
    'arrayEnumerateDense',
    'arrayEnumerateDenseRanked',
    'arrayEnumerateUniq',
    'arrayEnumerateUniqRanked',
    'arrayExists',
    'arrayFilter',
    'arrayFirst',
    'arrayFirstIndex',
    'arrayFlatten',
    'arrayIntersect',
    'arrayJoin',
    'arrayMap',
    'arrayPopBack',
    'arrayPopFront',
    'arrayPushBack',
    'arrayPushFront',
    'arrayReduce',
    'arrayResize',
    'arrayReverse',
    'arrayReverseSort',
    'arraySlice',
    'arraySort',
    'arrayStringConcat',
    'arraySum',
    'arrayUniq',
    'arrayWithConstant',
    'asin',
    'assumeNotNull',
    'atan',
    'avg',
    'bar',
    'base64Decode',
    'base64Encode',
    'basename',
    'BIT_AND',
    'BIT_OR',
    'BIT_XOR',
    'bitAnd',
    'bitmapAnd',
    'bitmapAndCardinality',
    'bitmapAndnot',
    'bitmapAndnotCardinality',
    'bitmapBuild',
    'bitmapCardinality',
    'bitmapContains',
    'bitmapHasAll',
    'bitmapHasAny',
    'bitmapOr',
    'bitmapOrCardinality',
    'bitmapToArray',
    'bitmapXor',
    'bitmapXorCardinality',
    'bitmaskToArray',
    'bitmaskToList',
    'bitNot',
    'bitOr',
    'bitRotateLeft',
    'bitRotateRight',
    'bitShiftLeft',
    'bitShiftRight',
    'bitTest',
    'bitTestAll',
    'bitTestAny',
    'bitXor',
    'blockNumber',
    'blockSize',
    'boundingRatio',
    'caseWithExpr',
    'caseWithExpression',
    'caseWithoutExpr',
    'caseWithoutExpression',
    'CAST',
    'cbrt',
    'ceil',
    'ceiling',
    'CHAR_LENGTH',
    'CHARACTER_LENGTH',
    'cityHash64',
    'coalesce',
    'concat',
    'concatAssumeInjective',
    'convertCharset',
    'corr',
    'corrStable',
    'cos',
    'count',
    'countEqual',
    'COVAR_POP',
    'COVAR_SAMP',
    'covarPop',
    'covarPopStable',
    'covarSamp',
    'covarSampStable',
    'CRC32',
    'currentDatabase',
    'cutFragment',
    'cutIPv6',
    'cutQueryString',
    'cutQueryStringAndFragment',
    'cutToFirstSignificantSubdomain',
    'cutURLParameter',
    'cutWWW',
    'dateDiff',
    'decodeURLComponent',
    'defaultValueOfArgumentType',
    'dictGet',
    'dictGetDate',
    'dictGetDateOrDefault',
    'dictGetDateTime',
    'dictGetDateTimeOrDefault',
    'dictGetFloat32',
    'dictGetFloat32OrDefault',
    'dictGetFloat64',
    'dictGetFloat64OrDefault',
    'dictGetHierarchy',
    'dictGetInt16',
    'dictGetInt16OrDefault',
    'dictGetInt32',
    'dictGetInt32OrDefault',
    'dictGetInt64',
    'dictGetInt64OrDefault',
    'dictGetInt8',
    'dictGetInt8OrDefault',
    'dictGetOrDefault',
    'dictGetString',
    'dictGetStringOrDefault',
    'dictGetUInt16',
    'dictGetUInt16OrDefault',
    'dictGetUInt32',
    'dictGetUInt32OrDefault',
    'dictGetUInt64',
    'dictGetUInt64OrDefault',
    'dictGetUInt8',
    'dictGetUInt8OrDefault',
    'dictGetUUID',
    'dictGetUUIDOrDefault',
    'dictHas',
    'dictIsIn',
    'divide',
    'domain',
    'domainWithoutWWW',
    'dumpColumnStructure',
    'e',
    'empty',
    'emptyArrayDate',
    'emptyArrayDateTime',
    'emptyArrayFloat32',
    'emptyArrayFloat64',
    'emptyArrayInt16',
    'emptyArrayInt32',
    'emptyArrayInt64',
    'emptyArrayInt8',
    'emptyArrayString',
    'emptyArrayToSingle',
    'emptyArrayUInt16',
    'emptyArrayUInt32',
    'emptyArrayUInt64',
    'emptyArrayUInt8',
    'endsWith',
    'entropy',
    'equals',
    'erf',
    'erfc',
    'evalMLMethod',
    'exp',
    'exp10',
    'exp2',
    'extract',
    'extractAll',
    'extractURLParameter',
    'extractURLParameterNames',
    'extractURLParameters',
    'farmHash64',
    'filesystemAvailable',
    'filesystemCapacity',
    'filesystemFree',
    'finalizeAggregation',
    'findClusterIndex',
    'findClusterValue',
    'firstSignificantSubdomain',
    'flatten',
    'floor',
    'format',
    'formatDateTime',
    'formatReadableSize',
    'fragment',
    'gccMurmurHash',
    'gcd',
    'generateUUIDv4',
    'geohashDecode',
    'geohashEncode',
    'geoToH3',
    'getSizeOfEnumType',
    'globalIn',
    'globalNotIn',
    'greatCircleDistance',
    'greater',
    'greaterOrEquals',
    'greatest',
    'groupArray',
    'groupArrayInsertAt',
    'groupArrayMovingAvg',
    'groupArrayMovingSum',
    'groupBitAnd',
    'groupBitmap',
    'groupBitOr',
    'groupBitXor',
    'groupUniqArray',
    'halfMD5',
    'has',
    'hasAll',
    'hasAny',
    'hasColumnInTable',
    'hex',
    'histogram',
    'hiveHash',
    'hostName',
    'identity',
    'if',
    'ifNull',
    'ignore',
    'ignoreExceptNull',
    'in',
    'indexHint',
    'indexOf',
    'intDiv',
    'intDivOrZero',
    'intExp10',
    'intExp2',
    'intHash32',
    'intHash64',
    'IPv4CIDRToRange',
    'IPv4NumToString',
    'IPv4NumToStringClassC',
    'IPv4StringToNum',
    'IPv4ToIPv6',
    'IPv6CIDRToRange',
    'IPv6NumToString',
    'IPv6StringToNum',
    'isFinite',
    'isInfinite',
    'isNaN',
    'isNotNull',
    'isNull',
    'isValidUTF8',
    'javaHash',
    'joinGet',
    'JSONExtract',
    'JSONExtractBool',
    'JSONExtractFloat',
    'JSONExtractInt',
    'JSONExtractKeysAndValues',
    'JSONExtractRaw',
    'JSONExtractString',
    'JSONExtractUInt',
    'JSONHas',
    'JSONKey',
    'JSONLength',
    'JSONType',
    'jumpConsistentHash',
    'kurtPop',
    'kurtSamp',
    'lcase',
    'lcm',
    'least',
    'length',
    'lengthUTF8',
    'less',
    'lessOrEquals',
    'lgamma',
    'like',
    'ln',
    'locate',
    'log',
    'log10',
    'log2',
    'lowCardinalityIndices',
    'lowCardinalityKeys',
    'lower',
    'lowerUTF8',
    'MACNumToString',
    'MACStringToNum',
    'MACStringToOUI',
    'match',
    'materialize',
    'max',
    'maxIntersections',
    'maxIntersectionsPosition',
    'MD5',
    'median',
    'medianDeterministic',
    'medianExact',
    'medianExactWeighted',
    'medianTDigest',
    'medianTDigestWeighted',
    'medianTiming',
    'medianTimingWeighted',
    'metroHash64',
    'mid',
    'min',
    'minus',
    'modelEvaluate',
    'modulo',
    'multiFuzzyMatchAny',
    'multiFuzzyMatchAnyIndex',
    'multiIf',
    'multiMatchAny',
    'multiMatchAnyIndex',
    'multiply',
    'multiSearchAllPositions',
    'multiSearchAllPositionsCaseInsensitive',
    'multiSearchAllPositionsCaseInsensitiveUTF8',
    'multiSearchAllPositionsUTF8',
    'multiSearchAny',
    'multiSearchAnyCaseInsensitive',
    'multiSearchAnyCaseInsensitiveUTF8',
    'multiSearchAnyUTF8',
    'multiSearchFirstIndex',
    'multiSearchFirstIndexCaseInsensitive',
    'multiSearchFirstIndexCaseInsensitiveUTF8',
    'multiSearchFirstIndexUTF8',
    'multiSearchFirstPosition',
    'multiSearchFirstPositionCaseInsensitive',
    'multiSearchFirstPositionCaseInsensitiveUTF8',
    'multiSearchFirstPositionUTF8',
    'murmurHash2_32',
    'murmurHash2_64',
    'murmurHash3_128',
    'murmurHash3_32',
    'murmurHash3_64',
    'negate',
    'ngramDistance',
    'ngramDistanceCaseInsensitive',
    'ngramDistanceCaseInsensitiveUTF8',
    'ngramDistanceUTF8',
    'ngramSearch',
    'ngramSearchCaseInsensitive',
    'ngramSearchCaseInsensitiveUTF8',
    'ngramSearchUTF8',
    'not',
    'notEmpty',
    'notEquals',
    'notIn',
    'notLike',
    'now',
    'nullIf',
    'or',
    'OSHierarchy',
    'OSIn',
    'OSToRoot',
    'parseDateTimeBestEffort',
    'parseDateTimeBestEffortOrNull',
    'parseDateTimeBestEffortOrZero',
    'path',
    'pathFull',
    'pi',
    'plus',
    'pointInEllipses',
    'pointInPolygon',
    'position',
    'positionCaseInsensitive',
    'positionCaseInsensitiveUTF8',
    'positionUTF8',
    'pow',
    'power',
    'protocol',
    'quantile',
    'quantileDeterministic',
    'quantileExact',
    'quantileExactWeighted',
    'quantiles',
    'quantilesDeterministic',
    'quantilesExact',
    'quantilesExactWeighted',
    'quantilesTDigest',
    'quantilesTDigestWeighted',
    'quantilesTiming',
    'quantilesTimingWeighted',
    'quantileTDigest',
    'quantileTDigestWeighted',
    'quantileTiming',
    'quantileTimingWeighted',
    'queryString',
    'queryStringAndFragment',
    'rand',
    'rand64',
    'randConstant',
    'range',
    'regexpQuoteMeta',
    'regionHierarchy',
    'regionIn',
    'regionToArea',
    'regionToCity',
    'regionToContinent',
    'regionToCountry',
    'regionToDistrict',
    'regionToName',
    'regionToPopulation',
    'regionToTopContinent',
    'reinterpretAsDate',
    'reinterpretAsDateTime',
    'reinterpretAsFixedString',
    'reinterpretAsFloat32',
    'reinterpretAsFloat64',
    'reinterpretAsInt16',
    'reinterpretAsInt32',
    'reinterpretAsInt64',
    'reinterpretAsInt8',
    'reinterpretAsString',
    'reinterpretAsUInt16',
    'reinterpretAsUInt32',
    'reinterpretAsUInt64',
    'reinterpretAsUInt8',
    'replace',
    'replaceAll',
    'replaceOne',
    'replaceRegexpAll',
    'replaceRegexpOne',
    'replicate',
    'retention',
    'reverse',
    'reverseUTF8',
    'round',
    'roundAge',
    'roundDown',
    'roundDuration',
    'roundToExp2',
    'rowNumberInAllBlocks',
    'rowNumberInBlock',
    'runningAccumulate',
    'runningDifference',
    'runningDifferenceStartingWithFirstValue',
    'SEHierarchy',
    'SEIn',
    'sequenceCount',
    'sequenceMatch',
    'SEToRoot',
    'SHA1',
    'SHA224',
    'SHA256',
    'simpleLinearRegression',
    'sin',
    'sipHash128',
    'sipHash64',
    'skewPop',
    'skewSamp',
    'sleep',
    'sleepEachRow',
    'splitByChar',
    'splitByString',
    'sqrt',
    'startsWith',
    'STDDEV_POP',
    'STDDEV_SAMP',
    'stddevPop',
    'stddevPopStable',
    'stddevSamp',
    'stddevSampStable',
    'stochasticLinearRegression',
    'stochasticLogisticRegression',
    'substr',
    'substring',
    'substringUTF8',
    'subtractDays',
    'subtractHours',
    'subtractMinutes',
    'subtractMonths',
    'subtractQuarters',
    'subtractSeconds',
    'subtractWeeks',
    'subtractYears',
    'sum',
    'sumburConsistentHash',
    'sumKahan',
    'sumMap',
    'sumMapFiltered',
    'sumMapFilteredWithOverflow',
    'sumMapWithOverflow',
    'sumWithOverflow',
    'tan',
    'tgamma',
    'throwIf',
    'timeSeriesGroupRateSum',
    'timeSeriesGroupSum',
    'timeSlot',
    'timeSlots',
    'timezone',
    'toColumnTypeName',
    'toDate',
    'toDateOrNull',
    'toDateOrZero',
    'toDateTime',
    'toDateTimeOrNull',
    'toDateTimeOrZero',
    'today',
    'toDayOfMonth',
    'toDayOfWeek',
    'toDayOfYear',
    'toDecimal128',
    'toDecimal128OrNull',
    'toDecimal128OrZero',
    'toDecimal32',
    'toDecimal32OrNull',
    'toDecimal32OrZero',
    'toDecimal64',
    'toDecimal64OrNull',
    'toDecimal64OrZero',
    'toFixedString',
    'toFloat32',
    'toFloat32OrNull',
    'toFloat32OrZero',
    'toFloat64',
    'toFloat64OrNull',
    'toFloat64OrZero',
    'toHour',
    'toInt16',
    'toInt16OrNull',
    'toInt16OrZero',
    'toInt32',
    'toInt32OrNull',
    'toInt32OrZero',
    'toInt64',
    'toInt64OrNull',
    'toInt64OrZero',
    'toInt8',
    'toInt8OrNull',
    'toInt8OrZero',
    'toIntervalDay',
    'toIntervalHour',
    'toIntervalMinute',
    'toIntervalMonth',
    'toIntervalQuarter',
    'toIntervalSecond',
    'toIntervalWeek',
    'toIntervalYear',
    'toIPv4',
    'toIPv6',
    'toISOWeek',
    'toISOYear',
    'toLowCardinality',
    'toMinute',
    'toMonday',
    'toMonth',
    'toNullable',
    'topK',
    'topKWeighted',
    'topLevelDomain',
    'toQuarter',
    'toRelativeDayNum',
    'toRelativeHourNum',
    'toRelativeMinuteNum',
    'toRelativeMonthNum',
    'toRelativeQuarterNum',
    'toRelativeSecondNum',
    'toRelativeWeekNum',
    'toRelativeYearNum',
    'toSecond',
    'toStartOfDay',
    'toStartOfFifteenMinutes',
    'toStartOfFiveMinute',
    'toStartOfHour',
    'toStartOfInterval',
    'toStartOfISOYear',
    'toStartOfMinute',
    'toStartOfMonth',
    'toStartOfQuarter',
    'toStartOfTenMinutes',
    'toStartOfYear',
    'toString',
    'toStringCutToZero',
    'toTime',
    'toTimeZone',
    'toTypeName',
    'toUInt16',
    'toUInt16OrNull',
    'toUInt16OrZero',
    'toUInt32',
    'toUInt32OrNull',
    'toUInt32OrZero',
    'toUInt64',
    'toUInt64OrNull',
    'toUInt64OrZero',
    'toUInt8',
    'toUInt8OrNull',
    'toUInt8OrZero',
    'toUnixTimestamp',
    'toUUID',
    'toValidUTF8',
    'toYear',
    'toYYYYMM',
    'toYYYYMMDD',
    'toYYYYMMDDhhmmss',
    'transform',
    'trimBoth',
    'trimLeft',
    'trimRight',
    'trunc',
    'truncate',
    'tryBase64Decode',
    'tuple',
    'tupleElement',
    'ucase',
    'unhex',
    'uniq',
    'uniqCombined',
    'uniqExact',
    'uniqHLL12',
    'uniqUpTo',
    'upper',
    'upperUTF8',
    'uptime',
    'URLHash',
    'URLHierarchy',
    'URLPathHierarchy',
    'UUIDNumToString',
    'UUIDStringToNum',
    'VAR_POP',
    'VAR_SAMP',
    'varPop',
    'varPopStable',
    'varSamp',
    'varSampStable',
    'version',
    'visibleWidth',
    'visitParamExtractBool',
    'visitParamExtractFloat',
    'visitParamExtractInt',
    'visitParamExtractRaw',
    'visitParamExtractString',
    'visitParamExtractUInt',
    'visitParamHas',
    'windowFunnel',
    'xor',
    'xxHash32',
    'xxHash64',
    'yandexConsistentHash',
    'yesterday',
)

CASE_INSENSITIVE_FUNCTIONS = (
    'COUNT',

    'CORR',
    'VAR_SAMP',
    'VAR_POP',
    'STDDEV_SAMP',
    'STDDEV_POP',
    'COVAR_SAMP',
    'COVAR_POP',

    'AVG',
    'SUM',
    'MIN',
    'MAX',
)

AGGREGATION_FUNCTIONS_BASE = (
    'any',
    'anyHeavy',
    'anyLast',
    'argMax',
    'argMin',
    'avg',
    'corr',
    'count',
    'covarPop',
    'covarSamp',
    'groupArray',
    'groupArrayInsertAt',
    'groupBitAnd',
    'groupBitmap',
    'groupBitOr',
    'groupBitXor',
    'groupUniqArray',
    'kurtPop',
    'kurtSamp',
    'max',
    'median',
    'min',
    'quantile',
    'quantileDeterministic',
    'quantileExact',
    'quantileExactWeighted',
    'quantiles',
    'quantileTDigest',
    'quantileTiming',
    'quantileTimingWeighted',
    'simpleLinearRegression',
    'skewPop',
    'skewSamp',
    'stddevPop',
    'stddevSamp',
    'stochasticLinearRegression',
    'stochasticLogisticRegression',
    'sum',
    'sumMap',
    'sumWithOverflow',
    'timeSeriesGroupRateSum',
    'timeSeriesGroupSum',
    'topK',
    'uniq',
    'uniqCombined',
    'uniqExact',
    'uniqHLL12',
    'varPop',
    'varSamp',
)

AGGREGATION_FUNCTIONS = (
    AGGREGATION_FUNCTIONS_BASE +
    tuple(name + 'If' for name in AGGREGATION_FUNCTIONS_BASE) +
    tuple(name + 'Array' for name in AGGREGATION_FUNCTIONS_BASE) +
    tuple(name + 'Merge' for name in AGGREGATION_FUNCTIONS_BASE) +
    tuple(name + 'State' for name in AGGREGATION_FUNCTIONS_BASE) +
    tuple(name + 'MergeState' for name in AGGREGATION_FUNCTIONS_BASE) +
    tuple(name + 'forEach' for name in AGGREGATION_FUNCTIONS_BASE)
)

DATATYPES = (
    'UInt8',
    'UInt16',
    'UInt32',
    'UInt64',
    'Int8',
    'Int16',
    'Int32',
    'Int64',
    'Float32',
    'Float64',
    'Decimal',
    'Boolean',
    'UUID',
    'String',
    'FixedString',
    'Date',
    'DateTime',
    'Enum',
    'Array',
    'Nullable',
    'Tuple',
    'Nested',
)

OPERATORS = (
    'LIKE',
    'NOT LIKE',
    'IN',
    'NOT IN',
    'GLOBAL IN',
    'GLOBAL NOT IN',
    'AND',
    'OR',
    'NOT',
    'BETWEEN',
)

PRETTY_FORMATS = (
    'Pretty',
    'PrettyCompact',
    'PrettyCompactMonoBlock',
    'PrettyCompactNoEscapes',
    'PrettyNoEscapes',
    'PrettySpace',
    'PrettySpaceNoEscapes',
)

FORMATS = PRETTY_FORMATS + (
    'TabSeparated',
    'TabSeparatedRaw',
    'TabSeparatedWithNames',
    'TabSeparatedWithNamesAndTypes',
    'CSV',
    'CSVWithNames',
    'Values',
    'Vertical',
    'JSON',
    'JSONCompact',
    'JSONEachRow',
    'TSKV',
    'Protobuf',
    'Parquet',
    'RowBinary',
    'Native',
    'Null',
    'XML',
    'CapnProto',
)

READ_QUERIES = (
    'SELECT',
    'SHOW',
    'DESC',
    'DESCRIBE',
    'USE',
    'EXISTS',
    'WITH',
)

WRITE_QUERIES = (
    'INSERT',
    'CREATE',
    'ATTACH',
    'DETACH',
    'DROP',
    'RENAME',
    'ALTER',
    'SET',
    'OPTIMIZE',
)

FORMATTABLE_QUERIES = (
    'INSERT',
    'SELECT',
    'WITH',
    'SHOW',
    'DESC',
    'DESCRIBE',
    'EXISTS',
)

KEYWORDS = tuple(sqlparse_keywords.keys())

EXIT_COMMANDS = (
    'exit',
    'quit',
    'logout',
    'учше',
    'йгше',
    'дщпщге',
    'exit;',
    'quit;',
    'logout;',
    'учшеж',
    'йгшеж',
    'дщпщгеж',
    'q',
    'й',
    r'\q',
    r'\Q',
    ':q',
    r'\й',
    r'\Й',
    'Жй',
)

CREATE_SUBCOMMANDS = (
    'DATABASE',
    'TABLE',
    'VIEW',
)

DROP_SUBCOMMANDS = (
    'DATABASE',
    'TABLE'
)

SHOW_SUBCOMMANDS = (
    'DATABASES',
    'TABLES',
    'PROCESSLIST',
    'CREATE TABLE',
)

HELP_COMMANDS = (
    'help',
    r'\?',
)

REDIRECTION_COMMANDS = (
    r'\d',
    r'\d+',
    r'\dt',
    r'\c',
    r'\l',
    r'\ps',
    r'\kill',
)

INTERNAL_COMMANDS = EXIT_COMMANDS + HELP_COMMANDS + REDIRECTION_COMMANDS
