util.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2015 PingCAP, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package mysql
  14. type lengthAndDecimal struct {
  15. length int64
  16. decimal int64
  17. }
  18. // defaultLengthAndDecimal provides default Flen and Decimal for fields
  19. // from CREATE TABLE when they are unspecified.
  20. var defaultLengthAndDecimal = map[byte]lengthAndDecimal{
  21. TypeBit: {1, 0},
  22. TypeTiny: {4, 0},
  23. TypeShort: {6, 0},
  24. TypeInt24: {9, 0},
  25. TypeLong: {11, 0},
  26. TypeLonglong: {20, 0},
  27. TypeDouble: {22, -1},
  28. TypeFloat: {12, -1},
  29. TypeNewDecimal: {10, 0},
  30. TypeDuration: {10, 0},
  31. TypeDate: {10, 0},
  32. TypeTimestamp: {19, 0},
  33. TypeDatetime: {19, 0},
  34. TypeYear: {4, 0},
  35. TypeString: {1, 0},
  36. TypeVarchar: {5, 0},
  37. TypeVarString: {5, 0},
  38. TypeTinyBlob: {255, 0},
  39. TypeBlob: {65535, 0},
  40. TypeMediumBlob: {16777215, 0},
  41. TypeLongBlob: {4294967295, 0},
  42. TypeJSON: {4294967295, 0},
  43. TypeNull: {0, 0},
  44. TypeSet: {-1, 0},
  45. TypeEnum: {-1, 0},
  46. }
  47. // IsIntegerType indicate whether tp is an integer type.
  48. func IsIntegerType(tp byte) bool {
  49. switch tp {
  50. case TypeTiny, TypeShort, TypeInt24, TypeLong, TypeLonglong:
  51. return true
  52. }
  53. return false
  54. }
  55. // GetDefaultFieldLengthAndDecimal returns the default display length (flen) and decimal length for column.
  56. // Call this when no Flen assigned in ddl.
  57. // or column value is calculated from an expression.
  58. // For example: "select count(*) from t;", the column type is int64 and Flen in ResultField will be 21.
  59. // See https://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html
  60. func GetDefaultFieldLengthAndDecimal(tp byte) (flen int64, decimal int64) {
  61. val, ok := defaultLengthAndDecimal[tp]
  62. if ok {
  63. return val.length, val.decimal
  64. }
  65. return -1, -1
  66. }
  67. // defaultLengthAndDecimal provides default Flen and Decimal for fields
  68. // from CAST when they are unspecified.
  69. var defaultLengthAndDecimalForCast = map[byte]lengthAndDecimal{
  70. TypeString: {0, -1}, // Flen & Decimal differs.
  71. TypeDate: {10, 0},
  72. TypeDatetime: {19, 0},
  73. TypeNewDecimal: {10, 0},
  74. TypeDuration: {10, 0},
  75. TypeLonglong: {22, 0},
  76. TypeDouble: {22, -1},
  77. TypeFloat: {12, -1},
  78. TypeJSON: {4194304, 0}, // Flen differs.
  79. }
  80. // GetDefaultFieldLengthAndDecimalForCast returns the default display length (flen) and decimal length for casted column
  81. // when flen or decimal is not specified.
  82. func GetDefaultFieldLengthAndDecimalForCast(tp byte) (flen int64, decimal int64) {
  83. val, ok := defaultLengthAndDecimalForCast[tp]
  84. if ok {
  85. return val.length, val.decimal
  86. }
  87. return -1, -1
  88. }