diff --git a/src/main/java/net/sf/jsqlparser/expression/CastExpression.java b/src/main/java/net/sf/jsqlparser/expression/CastExpression.java index 9c77ef825..a72c65112 100644 --- a/src/main/java/net/sf/jsqlparser/expression/CastExpression.java +++ b/src/main/java/net/sf/jsqlparser/expression/CastExpression.java @@ -36,20 +36,13 @@ public class CastExpression extends ASTNodeAccessImpl implements Expression { // https://cloud.google.com/bigquery/docs/reference/standard-sql/conversion_functions#cast_as_date private String format = null; - public CastExpression(String keyword, Expression leftExpression, String dataType) { + public CastExpression(String keyword, Expression leftExpression, ColDataType colDataType) { this.keyword = keyword; this.leftExpression = leftExpression; - this.colDataType = new ColDataType(dataType); + this.colDataType = colDataType; } // Implicit Cast - public CastExpression(String dataType, String value) { - this.keyword = null; - this.isImplicitCast = true; - this.colDataType = new ColDataType(dataType); - this.leftExpression = new StringValue(value); - } - public CastExpression(ColDataType colDataType, String value) { this.keyword = null; this.isImplicitCast = true; @@ -71,10 +64,10 @@ public CastExpression(ColDataType colDataType, Double value) { this.leftExpression = new DoubleValue(value); } - public CastExpression(Expression leftExpression, String dataType) { + public CastExpression(Expression leftExpression, ColDataType colDataType) { this.keyword = null; this.leftExpression = leftExpression; - this.colDataType = new ColDataType(dataType); + this.colDataType = colDataType; } diff --git a/src/main/java/net/sf/jsqlparser/statement/create/table/ColDataType.java b/src/main/java/net/sf/jsqlparser/statement/create/table/ColDataType.java index ff90bf875..da08c947d 100644 --- a/src/main/java/net/sf/jsqlparser/statement/create/table/ColDataType.java +++ b/src/main/java/net/sf/jsqlparser/statement/create/table/ColDataType.java @@ -13,18 +13,29 @@ import net.sf.jsqlparser.parser.ASTNodeAccessImpl; import net.sf.jsqlparser.statement.select.PlainSelect; -import java.math.BigInteger; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static java.util.stream.Collectors.joining; +/** + * A data type as written: its name, the arguments in parentheses after the name, and the clauses + * around them (MySQL modifiers, array dimensions, a character set, an INTERVAL qualifier and a SQL + * Server XML schema collection). For VARCHAR(255) the name is VARCHAR and the arguments are [255]; + * for TIMESTAMP(3) WITH TIME ZONE the name is TIMESTAMP WITH TIME ZONE and the arguments are [3]. + */ public class ColDataType extends ASTNodeAccessImpl { + /** A zoned date-time type, whose precision is written after its first word. */ + private static final Pattern ZONED_TYPE_NAME = Pattern.compile( + "(?i)(DATE|DATETIME|TIME|TIMESTAMP|TIMESTAMPTZ)(\\s+WITH(?:OUT)?(?:\\s+LOCAL)?\\s+TIME\\s+ZONE)"); + public enum Signedness { SIGNED, UNSIGNED } @@ -37,9 +48,12 @@ public enum NationalCharacterType { CHAR, VARCHAR } - /** Spelling of a character set clause; MySQL shorthands also select a character set. */ + /** + * Spelling of a character set clause. The MySQL shorthands ASCII, UNICODE and BYTE also select + * a character set: latin1, ucs2 and binary. + */ public enum CharacterSetSyntax { - CHARACTER_SET, CHARSET, ASCII, UNICODE + CHARACTER_SET, CHARSET, ASCII, UNICODE, BYTE } private String dataType; @@ -59,11 +73,6 @@ public void setUsingArrayKeyword(boolean usingArrayKeyword) { this.usingArrayKeyword = usingArrayKeyword; } - private Signedness signedness; - private boolean zerofill; - private BigInteger precision; - private boolean maxPrecision; - private Integer scale; private List typeModifiers; private NationalCharacterType nationalCharacterType; private XmlTypeModifier xmlTypeModifier; @@ -72,49 +81,15 @@ public ColDataType() { // empty constructor } - public ColDataType(String dataType, int precision, int scale) { - this(dataType); - setNumericTypeParameters(precision < 0 ? null - : precision == Integer.MAX_VALUE ? "MAX" : Integer.toString(precision), - scale < 0 ? null : Integer.valueOf(scale)); - } - - /** - * Creates a parameterized type, using {@code null} for an omitted parameter. Unlike the legacy - * primitive constructor, this accepts negative scales, including {@code -1}. The legacy - * {@link Integer#MAX_VALUE} precision sentinel continues to represent MAX. - */ - public static ColDataType fromNumericParameters(String dataType, Integer precision, - Integer scale) { - return fromTypeParameters(dataType, precision == null ? null - : precision == Integer.MAX_VALUE ? "MAX" : precision.toString(), scale); - } - - /** - * Creates a type from its numeric parameter spelling. Unlike the legacy primitive constructor, - * a numeric 2147483647 is distinct from MAX and larger lengths are retained without narrowing. - */ - public static ColDataType fromTypeParameters(String dataType, String precision, Integer scale) { - ColDataType type = new ColDataType(dataType); - type.setNumericTypeParameters(precision, scale); - return type; - } - - private void setNumericTypeParameters(String parameter, Integer scale) { - if (parameter != null) { - maxPrecision = "MAX".equalsIgnoreCase(parameter); - // Oracle's NUMBER(*, s) has no precision of its own. - precision = maxPrecision || "*".equals(parameter) ? null : new BigInteger(parameter); - this.scale = scale; - dataType += " (" + (maxPrecision ? "MAX" : precision != null ? precision : parameter) - + (scale != null ? ", " + scale : "") + ")"; - } - } - public ColDataType(String dataType) { this.dataType = dataType; } + /** + * The arguments in parentheses after the name, each as written, e.g. [255] for VARCHAR(255), + * [10, 2] for DECIMAL(10, 2), [MAX] for VARCHAR(MAX), [10 CHAR] for VARCHAR2(10 CHAR) or ['a', + * 'b'] for ENUM('a', 'b'). Returns null when the type has no arguments. + */ public List getArgumentsStringList() { return argumentsStringList; } @@ -123,56 +98,12 @@ public void setArgumentsStringList(List list) { argumentsStringList = list; } - public String getDataType() { - return dataType; - } - /** - * Returns the type name without parenthesized parameters, retaining qualification, quoting and - * multiword names such as TIMESTAMP WITH TIME ZONE. The legacy {@link #getDataType()} spelling - * is unchanged. Quoted parentheses are part of an identifier and are retained. + * The name of the type without its arguments, e.g. VARCHAR for VARCHAR(255), DOUBLE PRECISION + * or TIMESTAMP WITH TIME ZONE. A qualified or quoted name is kept as written. */ - public String getBaseTypeName() { - if (dataType == null) { - return null; - } - StringBuilder name = new StringBuilder(); - int depth = 0; - char quote = 0; - for (int i = 0; i < dataType.length(); i++) { - char c = dataType.charAt(i); - if (quote != 0) { - if (depth == 0) { - name.append(c); - } - if (c == quote) { - if (i + 1 < dataType.length() && dataType.charAt(i + 1) == quote) { - if (depth == 0) { - name.append(quote); - } - i++; - } else { - quote = 0; - } - } - } else if (c == '\'' || c == '"' || c == '`' || c == '[') { - quote = c == '[' ? ']' : c; - if (depth == 0) { - name.append(c); - } - } else if (c == '(') { - depth++; - while (name.length() > 0 - && Character.isWhitespace(name.charAt(name.length() - 1))) { - name.setLength(name.length() - 1); - } - } else if (c == ')' && depth > 0) { - depth--; - } else if (depth == 0) { - name.append(c); - } - } - return name.toString().trim(); + public String getDataType() { + return dataType; } public void setDataType(String string) { @@ -183,15 +114,17 @@ public void setDataType(List list) { dataType = list.stream().collect(joining(".")); } - /** Returns the character set name, resolving ASCII to latin1 and UNICODE to ucs2. */ + /** + * Returns the character set name, resolving ASCII to latin1, UNICODE to ucs2 and BYTE to + * binary. + */ public String getCharacterSet() { return characterSet; } public void setCharacterSet(String characterSet) { this.characterSet = characterSet; - if (characterSetSyntax == CharacterSetSyntax.ASCII - || characterSetSyntax == CharacterSetSyntax.UNICODE) { + if (isShorthand(characterSetSyntax)) { characterSetSyntax = CharacterSetSyntax.CHARACTER_SET; } } @@ -201,7 +134,7 @@ public CharacterSetSyntax getCharacterSetSyntax() { } /** - * Selects the clause spelling. ASCII selects latin1 and UNICODE selects ucs2; the explicit + * Selects the clause spelling. ASCII selects latin1, UNICODE ucs2 and BYTE binary; the explicit * spellings retain the current character set. Calling {@link #setCharacterSet(String)} after a * shorthand switches to CHARACTER SET. */ @@ -211,25 +144,21 @@ public void setCharacterSetSyntax(CharacterSetSyntax characterSetSyntax) { characterSet = "latin1"; } else if (characterSetSyntax == CharacterSetSyntax.UNICODE) { characterSet = "ucs2"; + } else if (characterSetSyntax == CharacterSetSyntax.BYTE) { + characterSet = "binary"; } } + private static boolean isShorthand(CharacterSetSyntax syntax) { + return syntax == CharacterSetSyntax.ASCII || syntax == CharacterSetSyntax.UNICODE + || syntax == CharacterSetSyntax.BYTE; + } + public ColDataType withCharacterSetSyntax(CharacterSetSyntax characterSetSyntax) { setCharacterSetSyntax(characterSetSyntax); return this; } - /** Whether the character set clause uses MySQL's CHARSET abbreviation. */ - public boolean isUseCharsetKeyword() { - return characterSetSyntax == CharacterSetSyntax.CHARSET; - } - - public void setUseCharsetKeyword(boolean useCharsetKeyword) { - setCharacterSetSyntax(useCharsetKeyword - ? CharacterSetSyntax.CHARSET - : CharacterSetSyntax.CHARACTER_SET); - } - public IntervalQualifier getIntervalQualifier() { return intervalQualifier; } @@ -246,20 +175,27 @@ public void setArrayData(List arrayData) { this.arrayData = arrayData; } + /** + * The signedness the MySQL modifiers give: the last of SIGNED and UNSIGNED, or null when there + * is neither. + */ public Signedness getSignedness() { + Signedness signedness = null; + if (typeModifiers != null) { + for (TypeModifier modifier : typeModifiers) { + if (modifier == TypeModifier.SIGNED) { + signedness = Signedness.SIGNED; + } else if (modifier == TypeModifier.UNSIGNED) { + signedness = Signedness.UNSIGNED; + } + } + } return signedness; } - public void setSignedness(Signedness signedness) { - this.signedness = signedness; - } - + /** Whether the MySQL modifiers include ZEROFILL. */ public boolean isZerofill() { - return zerofill; - } - - public void setZerofill(boolean zerofill) { - this.zerofill = zerofill; + return typeModifiers != null && typeModifiers.contains(TypeModifier.ZEROFILL); } /** Returns MySQL numeric modifiers in their original order, including repetitions. */ @@ -269,13 +205,6 @@ public List getTypeModifiers() { public void setTypeModifiers(List typeModifiers) { this.typeModifiers = typeModifiers; - signedness = null; - zerofill = false; - if (typeModifiers != null) { - for (TypeModifier modifier : typeModifiers) { - updateEffectiveModifier(modifier); - } - } } public void addTypeModifier(TypeModifier typeModifier) { @@ -283,23 +212,6 @@ public void addTypeModifier(TypeModifier typeModifier) { typeModifiers = new ArrayList<>(); } typeModifiers.add(typeModifier); - updateEffectiveModifier(typeModifier); - } - - private void updateEffectiveModifier(TypeModifier modifier) { - switch (modifier) { - case SIGNED: - signedness = Signedness.SIGNED; - break; - case UNSIGNED: - signedness = Signedness.UNSIGNED; - break; - case ZEROFILL: - zerofill = true; - break; - default: - break; - } } public NationalCharacterType getNationalCharacterType() { @@ -310,10 +222,6 @@ public void setNationalCharacterType(NationalCharacterType nationalCharacterType this.nationalCharacterType = nationalCharacterType; } - public boolean isNational() { - return nationalCharacterType != null; - } - public XmlTypeModifier getXmlTypeModifier() { return xmlTypeModifier; } @@ -328,51 +236,47 @@ public ColDataType withXmlTypeModifier(XmlTypeModifier xmlTypeModifier) { } /** - * The first numeric type parameter, e.g. {@code 255} for {@code VARCHAR(255)} or {@code 10} for - * {@code DECIMAL(10, 2)}. {@code MAX} is reported as {@link Integer#MAX_VALUE}. Returns - * {@code null} when the type carries no numeric parameters, e.g. {@code INT} or - * {@code ENUM('a', 'b')}, when the precision is Oracle's {@code *} as in {@code NUMBER(*, 0)}, - * or when a numeric length exceeds the integer range. Use {@link #getNumericPrecision()} for - * the full range and {@link #isMaxPrecision()} to distinguish MAX from the numeric value - * 2147483647. + * The first argument as a number, e.g. 255 for VARCHAR(255), 10 for DECIMAL(10, 2) and for + * VARCHAR2(10 CHAR), or 3 for TIMESTAMP(3) WITH TIME ZONE. Returns null when the type has no + * arguments, or when the first argument is not a number that fits an int, such as MAX, the * of + * NUMBER(*, 2) or a name. {@link #getArgumentsStringList()} has every argument as written. */ public Integer getPrecision() { - return maxPrecision ? Integer.valueOf(Integer.MAX_VALUE) - : precision != null && precision.bitLength() < Integer.SIZE - ? Integer.valueOf(precision.intValue()) - : null; - } - - public void setPrecision(Integer precision) { - setNumericPrecision(precision == null ? null : BigInteger.valueOf(precision)); - } - - /** Returns the exact numeric parameter, or null for an omitted parameter or MAX. */ - public BigInteger getNumericPrecision() { - return precision; - } - - /** Updates numeric metadata without changing the legacy rendered type spelling. */ - public void setNumericPrecision(BigInteger precision) { - this.precision = precision; - maxPrecision = false; - } - - /** Distinguishes the MAX keyword from an equal numeric value in the legacy accessor. */ - public boolean isMaxPrecision() { - return maxPrecision; + if (argumentsStringList == null || argumentsStringList.isEmpty()) { + return null; + } + String first = argumentsStringList.get(0).trim(); + int unit = first.indexOf(' '); + if (unit > 0 && isLengthUnit(first.substring(unit + 1).trim())) { + first = first.substring(0, unit); + } + return first.startsWith("-") ? null : toInteger(first); } /** - * The second numeric type parameter, e.g. {@code 2} for {@code DECIMAL(10, 2)} or {@code -3} - * for PostgreSQL {@code NUMERIC(2, -3)}. Returns {@code null} when absent. + * The second argument as a number when there are two, e.g. 2 for DECIMAL(10, 2) and for + * NUMBER(*, 2), or -3 for PostgreSQL's NUMERIC(2, -3). Returns null otherwise. */ public Integer getScale() { - return scale; + if (argumentsStringList == null || argumentsStringList.size() != 2) { + return null; + } + String first = argumentsStringList.get(0).trim(); + return "*".equals(first) || getPrecision() != null + ? toInteger(argumentsStringList.get(1).trim()) + : null; } - public void setScale(Integer scale) { - this.scale = scale; + private static boolean isLengthUnit(String word) { + return "BYTE".equalsIgnoreCase(word) || "CHAR".equalsIgnoreCase(word); + } + + private static Integer toInteger(String argument) { + if (!argument.matches("-?\\d{1,10}")) { + return null; + } + long value = Long.parseLong(argument); + return value >= Integer.MIN_VALUE && value <= Integer.MAX_VALUE ? (int) value : null; } @Override @@ -391,16 +295,21 @@ public String toString() { } arraySpec.append("]"); } - return dataType - + (intervalQualifier != null ? " " + intervalQualifier.toString() : "") - + (xmlTypeModifier != null ? " " + xmlTypeModifier : "") - + (argumentsStringList != null - ? " " + PlainSelect.getStringList(argumentsStringList, true, true) - : "") + String arguments = argumentsStringList != null && !argumentsStringList.isEmpty() + ? PlainSelect.getStringList(argumentsStringList, true, true) + : null; + Matcher zoned = + arguments != null && dataType != null ? ZONED_TYPE_NAME.matcher(dataType) : null; + String typeWithArguments = zoned != null && zoned.matches() + ? zoned.group(1) + arguments + zoned.group(2) + : dataType + + (intervalQualifier != null ? " " + intervalQualifier.toString() : "") + + (xmlTypeModifier != null ? " " + xmlTypeModifier : "") + + (arguments != null ? " " + arguments : ""); + return typeWithArguments + (typeModifiers != null && !typeModifiers.isEmpty() ? " " + PlainSelect.getStringList(typeModifiers, false, false) - : (signedness != null ? " " + signedness : "") - + (zerofill ? " ZEROFILL" : "")) + : "") + arraySpec.toString() + characterSetClause(); } @@ -412,6 +321,7 @@ private String characterSetClause() { switch (characterSetSyntax) { case ASCII: case UNICODE: + case BYTE: return " " + characterSetSyntax; case CHARSET: return " CHARSET " + characterSet; @@ -445,16 +355,6 @@ public ColDataType withArrayData(List arrayData) { return this; } - public ColDataType withSignedness(Signedness signedness) { - setSignedness(signedness); - return this; - } - - public ColDataType withZerofill(boolean zerofill) { - setZerofill(zerofill); - return this; - } - public ColDataType withTypeModifiers(List typeModifiers) { setTypeModifiers(typeModifiers); return this; @@ -465,16 +365,6 @@ public ColDataType withNationalCharacterType(NationalCharacterType nationalChara return this; } - public ColDataType withPrecision(Integer precision) { - this.setPrecision(precision); - return this; - } - - public ColDataType withScale(Integer scale) { - this.setScale(scale); - return this; - } - public ColDataType addArgumentsStringList(String... argumentsStringList) { List collection = Optional.ofNullable(getArgumentsStringList()).orElseGet(ArrayList::new); @@ -518,8 +408,6 @@ public final boolean equals(Object o) { && Objects.equals(intervalQualifier, that.intervalQualifier) && Objects.equals(arrayData, that.arrayData) && usingArrayKeyword == that.usingArrayKeyword - && signedness == that.signedness - && zerofill == that.zerofill && Objects.equals(typeModifiers, that.typeModifiers) && Objects.equals(xmlTypeModifier, that.xmlTypeModifier) && nationalCharacterType == that.nationalCharacterType; @@ -537,8 +425,6 @@ public int hashCode() { result = 31 * result + Objects.hashCode(intervalQualifier); result = 31 * result + Objects.hashCode(arrayData); result = 31 * result + Boolean.hashCode(usingArrayKeyword); - result = 31 * result + Objects.hashCode(signedness); - result = 31 * result + Boolean.hashCode(zerofill); result = 31 * result + Objects.hashCode(typeModifiers); result = 31 * result + Objects.hashCode(nationalCharacterType); result = 31 * result + Objects.hashCode(xmlTypeModifier); diff --git a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt index 83f8d2464..fabafc004 100644 --- a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt +++ b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt @@ -1511,9 +1511,9 @@ public class CCJSqlParser extends AbstractJSqlParser { try { int k1 = getToken(1).kind; // DT_ZONE (TIMESTAMP WITH TIME ZONE / WITHOUT TIME ZONE) is also a valid - // implicit cast type prefix handled by DataType(), but distinct from DATA_TYPE. + // implicit cast type prefix handled by DataTypeName(), but distinct from DATA_TYPE. if (k1 == DT_ZONE) return true; - // BigQuery JSON 'literal' - JSON lexes as K_JSON, but DataType() accepts it + // BigQuery JSON 'literal' - JSON lexes as K_JSON, but DataTypeName() accepts it if (k1 == K_JSON) return getToken(2).kind == S_CHAR_LITERAL; if (k1 != DATA_TYPE) return false; int k2 = getToken(2).kind; @@ -6140,15 +6140,26 @@ public class CCJSqlParser extends AbstractJSqlParser { } /** - * Extracts the numeric precision embedded in a DT_ZONE token image such as - * "TIMESTAMP(3) WITH TIME ZONE", or null when the image carries no parameter. + * Names a type after a DT_ZONE token such as "TIMESTAMP(3) WITH TIME ZONE": the name is + * "TIMESTAMP WITH TIME ZONE" and the precision written inside it becomes the type's argument. */ - private static Integer zonedTypePrecision(String image) { + private static void setZonedTypeName(ColDataType type, String image) { + String name = image; int open = image.indexOf('('); - if (open < 0) { - return null; + if (open >= 0) { + int close = image.indexOf(')', open); + type.addArgumentsStringList(image.substring(open + 1, close).trim()); + name = image.substring(0, open) + " " + image.substring(close + 1); } - return Integer.valueOf(image.substring(open + 1, image.indexOf(')', open)).trim()); + type.setDataType(name.trim().replaceAll("\\s+", " ")); + } + + /** NATIONAL VARCHAR, NATIONAL CHARACTER VARYING and NVARCHAR vary in length; the others do not. */ + private static ColDataType.NationalCharacterType nationalCharacterType(String name) { + String upper = name.toUpperCase(Locale.ROOT); + return upper.contains("VARCHAR") || upper.contains("VARYING") + ? ColDataType.NationalCharacterType.VARCHAR + : ColDataType.NationalCharacterType.CHAR; } } @@ -7055,7 +7066,7 @@ TOKEN : /* Date/Time with time zones */ TOKEN : /* Data Types */ { - // Dedicated keyword tokens (e.g. K_BINARY/K_CHAR) are consumed by DataType(). + // Dedicated keyword tokens (e.g. K_BINARY/K_CHAR) are consumed by DataTypeName(). // Repeating them here cannot match DATA_TYPE and causes unreachable-token warnings. | | | | | | | | | @@ -19576,7 +19587,8 @@ CastExpression ImplicitCast() #ImplicitCast: Token first = getToken(1); } { - colDataType = DataType() + colDataType = DataTypeName() + [ LOOKAHEAD(2) TypeArguments(colDataType) ] ( tk2 = | @@ -19628,21 +19640,14 @@ CastExpression CastExpression(): ) | ( - type=ColDataType() { - // Preserve the established MySQL CAST type-name API outside PostgreSQL mode. - if (!Dialect.POSTGRESQL.name().equals(getAsString(Feature.dialect)) - && type.isUsingArrayKeyword() && type.getArrayData().size() == 1 - && type.getArrayData().get(0) == null) { - type.setUsingArrayKeyword(false); - type.getArrayData().clear(); - type.setDataType(type.getDataType() + " ARRAY"); - } - retval.setColDataType(type); - } + type=ColDataType() { retval.setColDataType(type); } // MySQL casts to an array of the given type when a multi-valued index key part is - // defined, e.g. CAST(data->'$.zips' AS UNSIGNED ARRAY). ARRAY reads as a further - // word of the type, the way INT UNSIGNED already does. - [ { type.setDataType(type.getDataType() + " ARRAY"); } ] + // defined, e.g. CAST(data->'$.zips' AS UNSIGNED ARRAY), and writes ARRAY after the + // type's character set. The type reads ARRAY itself when no character set comes first. + [ { + type.setUsingArrayKeyword(true); + type.addArrayData((Integer) null); + } ] ) ) @@ -23818,37 +23823,27 @@ SpannerInterleaveIn SpannerInterleaveIn(): } -ColDataType DataType(): +/** + * The name of a built-in type: one or more type words such as VARCHAR, DOUBLE PRECISION or + * TIMESTAMP(3) WITH TIME ZONE, or ARRAY. A zoned type name becomes TIMESTAMP WITH TIME + * ZONE with the precision written inside it as its argument; arguments written after the name are + * read by TypeArguments(). + */ +ColDataType DataTypeName(): { ColDataType colDataType = new ColDataType(); - Token prefix = null; Token tk = null; - Token tk2 = null; - String schema; - String type=""; - List argumentsStringList = new ArrayList(); - List array = new ArrayList(); - List name; + String type; + boolean zoned; ColDataType arrayType; - Integer zonePrecision = null; - - String precision = null; - Integer scale = null; - String scaleText; Token first = getToken(1); } { ( - LOOKAHEAD(2) tk= { - type = tk.image; - return linked(ColDataType.fromTypeParameters(type, precision, scale), first); - } + LOOKAHEAD(2) tk= { colDataType.setDataType(tk.image); } | - LOOKAHEAD(2) tk= ( - ("<" arrayType = ColDataType() ">") { - colDataType.setDataType("ARRAY<" + arrayType.getDataType() + ">"); - } - ) + LOOKAHEAD(2) "<" arrayType = ColDataType() ">" + { colDataType.setDataType("ARRAY<" + arrayType + ">"); } | ( ( tk= | tk= | tk = | tk = | tk = @@ -23856,40 +23851,86 @@ ColDataType DataType(): | tk= | tk= | tk= ) { type = tk.image; - // A DT_ZONE image already contains its parameter, e.g. "TIMESTAMP(3) WITH TIME ZONE". - zonePrecision = tk.kind == DT_ZONE ? zonedTypePrecision(tk.image) : null; + zoned = tk.kind == DT_ZONE; } ( - // Signedness is parsed by ColDataType after optional precision/scale. Do not - // consume CHARACTER when it starts the trailing CHARACTER SET clause. + // Signedness is parsed by ColDataType after the arguments. Do not consume + // CHARACTER when it starts the trailing CHARACTER SET clause. LOOKAHEAD(2, { getToken(1).kind != K_CHARACTER || getToken(2).kind != K_SET }) ( tk = | tk= | tk= | tk= | tk= | tk= | tk= | tk= | tk= ) { type += " " + tk.image; } )* - [ - LOOKAHEAD(2) "(" ( tk= { precision = tk.image; } | tk= { precision = "MAX"; } - // Oracle's NUMBER(*, s) and NUMBER(*) - | "*" { precision = "*"; requireDdlSyntax("NUMBER".equalsIgnoreCase(type), - "Wildcard precision is only supported for NUMBER"); } ) - [ | ] - [ "," scaleText=SignedIntegerTypeArgument() { scale = Integer.valueOf(scaleText); } ] - ")" - ] { - colDataType = ColDataType.fromTypeParameters(type, precision, scale); - if (zonePrecision != null) { - colDataType.setPrecision(zonePrecision); + if (zoned) { + setZonedTypeName(colDataType, type); + } else { + colDataType.setDataType(type); } } ) ) + { return linked(colDataType, first); } +} +/** + * The parenthesized, comma-separated arguments of a type, added to its arguments as written. Empty + * parentheses add none. + */ +void TypeArguments(ColDataType type): +{ + List arguments = new ArrayList(); + String argument; +} +{ + "(" + [ + argument=TypeArgument(type, true) { arguments.add(argument); } + ( "," argument=TypeArgument(type, false) { arguments.add(argument); } )* + ] + ")" { - return linked(colDataType, first); + if (!arguments.isEmpty()) { + type.addArgumentsStringList(arguments); + } } } +/** + * One type argument as written: a number with an optional BYTE or CHAR length unit as in Oracle's + * VARCHAR2(10 CHAR), MAX, a string such as an ENUM value, a name, a nested type such as the value + * type of DuckDB's MAP(VARCHAR, INTEGER), or a * of Oracle: the precision of NUMBER(*, s), or any + * argument of VECTOR(*, *). + */ +String TypeArgument(ColDataType type, boolean first): +{ + ColDataType nestedType; + String argument = null; + Token tk = null; + Token unit = null; +} +{ + ( + LOOKAHEAD(( | ) "(") nestedType=ColDataType() + { argument = nestedType.toString(); } + | + ( argument=SignedIntegerTypeArgument() | tk= { argument = tk.image; } ) + [ LOOKAHEAD(2) ( unit= | unit= ) { argument += " " + unit.image; } ] + | + ( tk= | tk= | tk= | tk= ) + { argument = tk.image; } + | + tk="*" + { + requireDdlSyntax("VECTOR".equalsIgnoreCase(type.getDataType()) + || first && "NUMBER".equalsIgnoreCase(type.getDataType()), + "A wildcard is only supported in VECTOR and as the precision of NUMBER"); + argument = tk.image; + } + ) + { return argument; } +} + /** Shared SQL large-object names; length and array suffixes stay in ColDataType. */ String LargeObjectTypeName(): { @@ -23931,29 +23972,22 @@ ColDataType WeightStringCastType(): { ( type= | type= ) "(" length= ")" { - return linked(ColDataType.fromTypeParameters(type.image, length.image, null), first); + return linked(new ColDataType(type.image).addArgumentsStringList(length.image), first); } } ColDataType ColDataType(): { ColDataType colDataType = new ColDataType(); - Token prefix = null; Token tk = null; - Token tk2 = null; String schema; + boolean zoned = false; String type=""; - List argumentsStringList = new ArrayList(); + List fields = new ArrayList(); List array = new ArrayList(); - List name; ColDataType arrayType; - ColDataType nestedType = null; - String numericArgument = null; + ColDataType fieldType; IntervalQualifier intervalQualifier = null; - Integer zonePrecision = null; - - int precision = -1; - int scale = -1; Token national = null; Token nationalType = null; Token varying = null; @@ -23975,13 +24009,17 @@ ColDataType ColDataType(): "(" type = RelObjectNameExt() - colDataType = ColDataType() { argumentsStringList.add( type + " " + colDataType.toString()); } + fieldType = ColDataType() { fields.add(type + " " + fieldType.toString()); } ( "," type = RelObjectNameExt() - colDataType = ColDataType() { argumentsStringList.add( type + " " + colDataType.toString()); } + fieldType = ColDataType() { fields.add(type + " " + fieldType.toString()); } )* - ")" { colDataType = new ColDataType("STRUCT"); } + ")" + { + colDataType = new ColDataType("STRUCT"); + colDataType.setArgumentsStringList(fields); + } ) | LOOKAHEAD({ isKeywordAhead("NATIONAL") }) ( @@ -23992,25 +24030,17 @@ ColDataType ColDataType(): type = national.image + " " + nationalType.image + (varying != null ? " " + varying.image : ""); colDataType.setDataType(type); - colDataType.setNationalCharacterType( - nationalType.image.equalsIgnoreCase("VARCHAR") || varying != null - ? ColDataType.NationalCharacterType.VARCHAR - : ColDataType.NationalCharacterType.CHAR); + colDataType.setNationalCharacterType(nationalCharacterType(type)); } ) | LOOKAHEAD(2) ( - colDataType = DataType() + colDataType = DataTypeName() { - if (isMySqlDialect() - && colDataType.getDataType().toUpperCase(Locale.ROOT).startsWith("NCHAR")) { - colDataType.setNationalCharacterType( - colDataType.getDataType().toUpperCase(Locale.ROOT).contains("VARCHAR") - ? ColDataType.NationalCharacterType.VARCHAR - : ColDataType.NationalCharacterType.CHAR); - } else if (isMySqlDialect() && colDataType.getDataType().toUpperCase(Locale.ROOT) - .startsWith("NVARCHAR")) { - colDataType.setNationalCharacterType(ColDataType.NationalCharacterType.VARCHAR); + // MySQL's NCHAR and NVARCHAR are the national character types. + String name = colDataType.getDataType().toUpperCase(Locale.ROOT); + if (isMySqlDialect() && (name.startsWith("NCHAR") || name.startsWith("NVARCHAR"))) { + colDataType.setNationalCharacterType(nationalCharacterType(name)); } } ) @@ -24040,8 +24070,7 @@ ColDataType ColDataType(): ) { schema = tk.image; - // A DT_ZONE image already contains its parameter, e.g. "TIMESTAMP(3) WITH TIME ZONE". - zonePrecision = tk.kind == DT_ZONE ? zonedTypePrecision(tk.image) : null; + zoned = tk.kind == DT_ZONE; } // Consume an optional INTERVAL qualifier such as `hour to minute` or @@ -24055,51 +24084,19 @@ ColDataType ColDataType(): ] [ LOOKAHEAD(2) "." arrayType = ColDataType() { schema += "." + arrayType.getDataType(); - // Retain the nested type's structured suffixes instead of flattening them into its name. + // Retain the nested type's arguments and suffixes instead of flattening them into its name. colDataType = arrayType; } ] - { colDataType.setDataType(schema); } - ) - - [ - LOOKAHEAD(2) "(" - ( - { tk2 = null; numericArgument = null; } - ( - LOOKAHEAD(( | ) "(") nestedType=ColDataType() - | - ( - ( - ( numericArgument=SignedIntegerTypeArgument() | tk= ) [ LOOKAHEAD(2) (tk2= | tk2=) ] - ) - | - tk= - | - tk= - | - // DuckDB parameterises types with other types: MAP(VARCHAR, INTEGER) - tk= - | - tk= - | - tk="*" { requireDdlSyntax("VECTOR".equalsIgnoreCase(colDataType.getDataType()), - "Wildcard type arguments are only supported for VECTOR"); } - ) - ) - { - if (nestedType != null) { - argumentsStringList.add(nestedType.toString()); - nestedType = null; + { + if (zoned) { + setZonedTypeName(colDataType, schema); } else { - argumentsStringList.add((numericArgument != null ? numericArgument : tk.image) - + (tk2 != null ? " " + tk2.image : "")); - } + colDataType.setDataType(schema); } + } + ) - [ "," ] - )* - ")" - ] + [ LOOKAHEAD(2) TypeArguments(colDataType) ] ( LOOKAHEAD(1) typeModifier=MySqlTypeModifier() { colDataType.addTypeModifier(typeModifier); } )* ( @@ -24118,30 +24115,19 @@ ColDataType ColDataType(): LOOKAHEAD({ (getToken(1).kind == K_CHARACTER && getToken(2).kind == K_SET) || isKeywordAhead("CHARSET") }) TypeCharacterSet(colDataType) | + // MySQL's ASCII, UNICODE and BYTE stand for CHARACTER SET latin1, ucs2 and binary. LOOKAHEAD({ Dialect.MYSQL.name().equals(getAsString(Feature.dialect)) - && (isKeywordAhead("ASCII") || isKeywordAhead("UNICODE")) }) - tk= + && (isKeywordAhead("ASCII") || isKeywordAhead("UNICODE") + || getToken(1).kind == K_BYTE) }) + ( tk= | tk= ) { colDataType.setCharacterSetSyntax( ColDataType.CharacterSetSyntax.valueOf(tk.image.toUpperCase(Locale.ROOT))); } ] { - requireDdlSyntax(colDataType.getXmlTypeModifier() == null || argumentsStringList.isEmpty(), + requireDdlSyntax(colDataType.getXmlTypeModifier() == null + || colDataType.getArgumentsStringList() == null, "Typed XML cannot have additional type arguments"); - if (argumentsStringList.size() > 0) { - colDataType.setArgumentsStringList(argumentsStringList); - // Integer arguments expose numeric parameters, including negative scales. - if (argumentsStringList.size() == 1 && argumentsStringList.get(0).matches("\\d+")) { - colDataType.setNumericPrecision(new java.math.BigInteger(argumentsStringList.get(0))); - } else if (argumentsStringList.size() == 2 && argumentsStringList.get(0).matches("\\d+") - && argumentsStringList.get(1).matches("-?\\d+")) { - colDataType.setNumericPrecision(new java.math.BigInteger(argumentsStringList.get(0))); - colDataType.setScale(Integer.valueOf(argumentsStringList.get(1))); - } - } - if (zonePrecision != null) { - colDataType.setPrecision(zonePrecision); - } linkAST(colDataType, first); return colDataType; } @@ -24157,7 +24143,12 @@ void TypeCharacterSet(ColDataType type): ( | LOOKAHEAD({ isKeywordAhead("CHARSET") }) { abbreviated = true; } ) name=CharacterSetName() - { type.setCharacterSet(name.image); type.setUseCharsetKeyword(abbreviated); } + { + type.setCharacterSet(name.image); + type.setCharacterSetSyntax(abbreviated + ? ColDataType.CharacterSetSyntax.CHARSET + : ColDataType.CharacterSetSyntax.CHARACTER_SET); + } } /** A MySQL character set or collation name: an identifier, BINARY or a quoted name. */ diff --git a/src/site/sphinx/usage.rst b/src/site/sphinx/usage.rst index 9e35b9d56..27d762f39 100644 --- a/src/site/sphinx/usage.rst +++ b/src/site/sphinx/usage.rst @@ -954,6 +954,91 @@ reflect their dispatch in ``usesColumnActionBody()``, ``usesGeneralBody()`` and override ``getActiveTableDefinition()``. The general-body hook also determines ``hasActiveColumnDefinitions()``. +Column data types +================= + +A ``ColDataType`` is a data type as written: in a column definition, a cast, a type fragment +from ``CCJSqlParserUtil.parseColDataType()`` and the other statements that name a type. Each +part of the type has one accessor. + +.. list-table:: + :header-rows: 1 + :widths: 35 65 + + * - Accessor + - Value + * - ``getDataType()`` + - The name without arguments, for example ``VARCHAR``, ``DOUBLE PRECISION``, + ``TIMESTAMP WITH TIME ZONE`` or ``app.money``. A quoted or qualified name is kept as + written. + * - ``getArgumentsStringList()`` + - The arguments in parentheses, each as written: ``[10, 2]`` for ``DECIMAL(10, 2)``, + ``[MAX]``, ``[10 CHAR]`` for ``VARCHAR2(10 CHAR)`` or ``['a', 'b']`` for an ``ENUM``. + ``null`` when the type has no parentheses. + * - ``getPrecision()``, ``getScale()`` + - The first and the second argument read as numbers. ``null`` for an argument such as + ``MAX``, ``*`` or a name, and for a number outside the ``int`` range; the argument as + written stays in ``getArgumentsStringList()``. + * - ``getTypeModifiers()`` + - MySQL's ``SIGNED``, ``UNSIGNED`` and ``ZEROFILL`` in the order written. + ``getSignedness()`` and ``isZerofill()`` read this list. + * - ``getArrayData()``, ``isUsingArrayKeyword()`` + - Array dimensions, as in ``INT[3]``, ``INT ARRAY[3]`` or MySQL's + ``CAST(x AS UNSIGNED ARRAY)``. + * - ``getCharacterSet()``, ``getCharacterSetSyntax()`` + - The character set and its spelling: ``CHARACTER SET``, ``CHARSET``, or MySQL's + ``ASCII``, ``UNICODE`` and ``BYTE``, which select ``latin1``, ``ucs2`` and ``binary``. + * - ``getIntervalQualifier()``, ``getNationalCharacterType()``, ``getXmlTypeModifier()`` + - The fields of an ``INTERVAL``, the national character types and SQL Server's typed XML. + +``toString()`` writes the parts back in SQL order. The precision of a zoned type stands after +its first word. + +.. code-block:: java + + ColDataType zoned = CCJSqlParserUtil.parseColDataType("TIMESTAMP(3) WITH TIME ZONE", + parser -> parser.withDialect(Dialect.POSTGRESQL)); + zoned.getDataType(); // TIMESTAMP WITH TIME ZONE + zoned.getArgumentsStringList(); // [3] + zoned.getPrecision(); // 3 + + ColDataType decimal = new ColDataType("DECIMAL").addArgumentsStringList("10", "2"); + decimal.addTypeModifier(ColDataType.TypeModifier.UNSIGNED); + decimal.toString(); // DECIMAL (10, 2) UNSIGNED + decimal.getScale(); // 2 + +Migration: earlier releases folded the arguments of some type names into ``getDataType()``, so +``VARCHAR(10)`` had the name ``VARCHAR (10)`` and no arguments while ``MEDIUMINT(9)`` had the +name ``MEDIUMINT`` and the arguments ``[9]``. Every type now has its name in ``getDataType()`` +and its arguments in ``getArgumentsStringList()``; ``toString()`` is unchanged. The API that +read or wrote the folded name, or set a value ``toString()`` did not show, is replaced: + +.. list-table:: + :header-rows: 1 + :widths: 50 50 + + * - Before + - Now + * - ``getDataType()`` of ``VARCHAR(10)``: ``VARCHAR (10)`` + - ``getDataType()``: ``VARCHAR``, ``getArgumentsStringList()``: ``[10]`` + * - ``getDataType()`` of ``TIMESTAMP(3) WITH TIME ZONE``: the name with the precision + - ``TIMESTAMP WITH TIME ZONE`` with the arguments ``[3]`` + * - ``getPrecision()`` of ``VARCHAR(MAX)``: ``Integer.MAX_VALUE`` + - ``null``; ``MAX`` is in ``getArgumentsStringList()`` + * - ``new ColDataType("DECIMAL", 10, 2)`` + - ``new ColDataType("DECIMAL").addArgumentsStringList("10", "2")`` + * - ``setPrecision()``, ``setScale()``, ``withPrecision()``, ``withScale()`` + - ``setArgumentsStringList()``; precision and scale follow the arguments + * - ``setSignedness()``, ``setZerofill()``, ``withSignedness()``, ``withZerofill()`` + - ``setTypeModifiers()``, ``addTypeModifier()`` + * - ``isNational()`` + - ``getNationalCharacterType() != null`` + * - ``getDataType()`` of MySQL's ``CAST(x AS UNSIGNED ARRAY)``: ``UNSIGNED ARRAY`` + - ``UNSIGNED``, with ``isUsingArrayKeyword()`` and one array dimension + * - ``new CastExpression(expression, "VARCHAR(10)")`` and the other ``CastExpression`` + constructors that take the type as a string + - The same constructors with a ``ColDataType`` + Structured column attributes ============================ @@ -1840,10 +1925,8 @@ The shared type grammar accepts negative scales, including ``numeric(2, -3)``, in type fragments, DDL and casts. ``getPrecision()`` returns ``2`` and ``getScale()`` returns ``-3``; an omitted scale returns ``null``. PostgreSQL's precision and scale range checks remain the database's -responsibility. When constructing a ``ColDataType`` directly, -``ColDataType.fromNumericParameters("numeric", 2, -3)`` accepts negative scales; -use ``null`` for omitted parameters. The legacy ``int`` -constructor continues to treat negative arguments as omitted parameters. +responsibility. To construct the type, give its arguments: +``new ColDataType("numeric").addArgumentsStringList("2", "-3")``. String literal values ~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/test/java/net/sf/jsqlparser/expression/CastExpressionTest.java b/src/test/java/net/sf/jsqlparser/expression/CastExpressionTest.java index afb6778ad..a5351be48 100644 --- a/src/test/java/net/sf/jsqlparser/expression/CastExpressionTest.java +++ b/src/test/java/net/sf/jsqlparser/expression/CastExpressionTest.java @@ -11,9 +11,12 @@ import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed; +import java.util.Collections; import net.sf.jsqlparser.JSQLParserException; import net.sf.jsqlparser.expression.operators.relational.ParenthesedExpressionList; import net.sf.jsqlparser.parser.CCJSqlParserUtil; +import net.sf.jsqlparser.schema.Column; +import net.sf.jsqlparser.statement.create.table.ColDataType; import net.sf.jsqlparser.statement.select.PlainSelect; import net.sf.jsqlparser.test.TestUtils; import net.sf.jsqlparser.util.deparser.StatementDeParser; @@ -191,6 +194,17 @@ void testNestedCompositeTypeCastIssue2341() throws JSQLParserException { Assertions.assertInstanceOf(CastExpression.class, parenthesedCast.get(0)); } + @Test + void recognizesATypeWithArgumentsByItsName() throws JSQLParserException { + String[][] cases = {{"VARCHAR(10)", "VARCHAR"}, {"DECIMAL(10, 2)", "DECIMAL"}, + {"TIMESTAMP(3) WITH TIME ZONE", "TIMESTAMP_WITH_TIME_ZONE"}}; + for (String[] c : cases) { + CastExpression cast = (CastExpression) CCJSqlParserUtil + .parseExpression("CAST(a AS " + c[0] + ")"); + Assertions.assertTrue(cast.isOf(CastExpression.DataType.valueOf(c[1])), c[0]); + } + } + @Test public void testCastToArrayIssue2490() throws JSQLParserException { // MySQL casts to an array of the given type when a multi-valued index key part is @@ -202,6 +216,20 @@ public void testCastToArrayIssue2490() throws JSQLParserException { .parse("SELECT CAST(data -> '$.zips' AS UNSIGNED ARRAY) FROM t"); CastExpression cast = Assertions.assertInstanceOf(CastExpression.class, select.getSelectItem(0).getExpression()); - Assertions.assertEquals("UNSIGNED ARRAY", cast.getColDataType().getDataType()); + Assertions.assertEquals("UNSIGNED", cast.getColDataType().getDataType()); + Assertions.assertTrue(cast.getColDataType().isUsingArrayKeyword()); + Assertions.assertEquals(Collections.singletonList(null), + cast.getColDataType().getArrayData()); + Assertions.assertEquals("UNSIGNED ARRAY", cast.getColDataType().toString()); + } + + @Test + void buildsCastsFromATypeObject() { + ColDataType varchar = new ColDataType("VARCHAR").addArgumentsStringList("10"); + Assertions.assertEquals("CAST(a AS VARCHAR (10))", + new CastExpression("CAST", new Column("a"), varchar).toString()); + Assertions.assertEquals("a::VARCHAR (10)", + new CastExpression(new Column("a"), varchar).toString()); + Assertions.assertEquals("VARCHAR (10) 'x'", new CastExpression(varchar, "x").toString()); } } diff --git a/src/test/java/net/sf/jsqlparser/expression/JsonTableCollationTest.java b/src/test/java/net/sf/jsqlparser/expression/JsonTableCollationTest.java index 84eac19a7..c43f755ef 100644 --- a/src/test/java/net/sf/jsqlparser/expression/JsonTableCollationTest.java +++ b/src/test/java/net/sf/jsqlparser/expression/JsonTableCollationTest.java @@ -47,7 +47,7 @@ void collationCanBeInspectedEditedRemovedAndConstructed() throws JSQLParserExcep (JsonTableValueColumnDefinition) table.getColumnsClause().getColumnDefinitions() .get(0); assertEquals("utf8mb4_bin", column.getCollation()); - assertEquals("VARCHAR", column.getDataType().getBaseTypeName()); + assertEquals("VARCHAR", column.getDataType().getDataType()); column.setCollation("utf8mb4_general_ci"); TestUtils.assertStatementCanBeDeparsedAs(select, sql("n VARCHAR(10) COLLATE utf8mb4_general_ci PATH '$.n'"), true); diff --git a/src/test/java/net/sf/jsqlparser/expression/JsonTableExistsTest.java b/src/test/java/net/sf/jsqlparser/expression/JsonTableExistsTest.java index 6f7a4a38a..1d2aa33b4 100644 --- a/src/test/java/net/sf/jsqlparser/expression/JsonTableExistsTest.java +++ b/src/test/java/net/sf/jsqlparser/expression/JsonTableExistsTest.java @@ -57,7 +57,7 @@ void typedExistsCanBeInspectedAndEdited() throws JSQLParserException { (JsonTableValueColumnDefinition) table.getColumnsClause().getColumnDefinitions() .get(0); assertTrue(column.isExists()); - assertEquals("INT", column.getDataType().getBaseTypeName()); + assertEquals("INT", column.getDataType().getDataType()); column.setColumnName("present").setDataType(new ColDataType("BIGINT")) .setPathExpression(new StringValue("$.other")); TestUtils.assertStatementCanBeDeparsedAs(select, diff --git a/src/test/java/net/sf/jsqlparser/expression/WeightStringFunctionTest.java b/src/test/java/net/sf/jsqlparser/expression/WeightStringFunctionTest.java index 1c72fbbeb..e4a94182b 100644 --- a/src/test/java/net/sf/jsqlparser/expression/WeightStringFunctionTest.java +++ b/src/test/java/net/sf/jsqlparser/expression/WeightStringFunctionTest.java @@ -17,6 +17,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.Collections; +import java.util.List; import java.util.function.Consumer; import net.sf.jsqlparser.JSQLParserException; import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; @@ -50,12 +51,13 @@ void castToCharWithMySqlDialect() throws JSQLParserException { assertEquals("WEIGHT_STRING", function.getName()); assertEquals("'x'", function.getParameters().get(0).toString()); // the same ColDataType as in CAST('x' AS CHAR(3)) - assertEquals("CHAR (3)", function.getCastType().getDataType()); + assertEquals("CHAR", function.getCastType().getDataType()); + assertEquals(List.of("3"), function.getCastType().getArgumentsStringList()); CastExpression cast = (CastExpression) CCJSqlParserUtil.parseExpression("CAST('x' AS CHAR(3))"); assertEquals(cast.getColDataType().toString(), function.getCastType().toString()); assertEquals("WEIGHT_STRING('x' AS CHAR (3))", function.toString()); - assertEquals(3, function.getCastType().getNumericPrecision().intValue()); + assertEquals(3, function.getCastType().getPrecision()); // the type spans its own text, inside the call Node node = function.getCastType().getASTNode(); @@ -67,7 +69,8 @@ void castToCharWithMySqlDialect() throws JSQLParserException { void castToBinaryWithoutDialect() throws JSQLParserException { Function function = selectFunction("SELECT weight_string(c AS BINARY(4)) FROM t", 0, null); - assertEquals("BINARY (4)", function.getCastType().getDataType()); + assertEquals("BINARY", function.getCastType().getDataType()); + assertEquals(List.of("4"), function.getCastType().getArgumentsStringList()); assertInstanceOf(Column.class, function.getParameters().get(0)); } diff --git a/src/test/java/net/sf/jsqlparser/parser/PostgreSqlNumericTypeTest.java b/src/test/java/net/sf/jsqlparser/parser/PostgreSqlNumericTypeTest.java index 41148ef5c..74b3c5b4d 100644 --- a/src/test/java/net/sf/jsqlparser/parser/PostgreSqlNumericTypeTest.java +++ b/src/test/java/net/sf/jsqlparser/parser/PostgreSqlNumericTypeTest.java @@ -88,15 +88,13 @@ void rejectsMalformedFragmentsAndTrailingInput(String type) { } @Test - void distinguishesNegativeScaleFromOmittedScaleWithoutChangingLegacyConstructor() { - ColDataType negative = ColDataType.fromNumericParameters("numeric", 2, -1); + void distinguishesNegativeScaleFromOmittedScale() { + ColDataType negative = new ColDataType("numeric").addArgumentsStringList("2", "-1"); assertEquals(-1, negative.getScale()); assertEquals("numeric (2, -1)", negative.toString()); - ColDataType legacy = new ColDataType("numeric", 2, -1); - assertNull(legacy.getScale()); - assertEquals("numeric (2)", legacy.toString()); - ColDataType boxedLegacy = new ColDataType("numeric", 2, Integer.valueOf(-1)); - assertNull(boxedLegacy.getScale()); + ColDataType omitted = new ColDataType("numeric").addArgumentsStringList("2"); + assertNull(omitted.getScale()); + assertEquals("numeric (2)", omitted.toString()); } @Test diff --git a/src/test/java/net/sf/jsqlparser/statement/DeclareStatementTest.java b/src/test/java/net/sf/jsqlparser/statement/DeclareStatementTest.java index 7ec516f7f..327b327b0 100644 --- a/src/test/java/net/sf/jsqlparser/statement/DeclareStatementTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/DeclareStatementTest.java @@ -38,7 +38,7 @@ public void testDeclareType() throws JSQLParserException { DeclareStatement created = new DeclareStatement() .addTypeDefExprList( new TypeDefExpr(new UserVariable().withName("find"), - new ColDataType().withDataType("nvarchar (30)").withPrecision(30), + new ColDataType("nvarchar").addArgumentsStringList("30"), null)) .withDeclareType(DeclareType.TYPE); assertDeparse(created, statement); @@ -51,7 +51,7 @@ public void testDeclareTypeWithDefault() throws JSQLParserException { Statement parsed = assertSqlCanBeParsedAndDeparsed(statement); DeclareStatement created = new DeclareStatement() .addTypeDefExprList(new TypeDefExpr(new UserVariable().withName("find"), - new ColDataType().withDataType("varchar (30)").withPrecision(30), + new ColDataType("varchar").addArgumentsStringList("30"), new StringValue().withValue("Man%"))) .withDeclareType(DeclareType.TYPE); assertDeparse(created, statement); @@ -65,7 +65,7 @@ public void testDeclareTypeList() throws JSQLParserException { DeclareStatement created = new DeclareStatement().addTypeDefExprList(asList( // new TypeDefExpr( new UserVariable().withName("group"), - new ColDataType().withDataType("nvarchar (50)").withPrecision(50), + new ColDataType("nvarchar").addArgumentsStringList("50"), null), new TypeDefExpr(new UserVariable().withName("sales"), new ColDataType().withDataType("money"), null))) diff --git a/src/test/java/net/sf/jsqlparser/statement/PostgreSqlPrepareTest.java b/src/test/java/net/sf/jsqlparser/statement/PostgreSqlPrepareTest.java index be9642db2..35f6751e5 100644 --- a/src/test/java/net/sf/jsqlparser/statement/PostgreSqlPrepareTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/PostgreSqlPrepareTest.java @@ -47,7 +47,7 @@ void preservesDeclaredTypesAndNestedStatements(String sql) throws JSQLParserExce void exposesMutableTypesAndRetainsInferredForm() throws JSQLParserException { PrepareStatement prepare = parse("PREPARE p(bigint, text) AS SELECT $1, $2"); assertEquals(2, prepare.getParameterTypes().size()); - assertEquals("bigint", prepare.getParameterTypes().get(0).getBaseTypeName()); + assertEquals("bigint", prepare.getParameterTypes().get(0).getDataType()); prepare.getParameterTypes().get(0).setDataType("integer"); assertEquals("PREPARE p(integer, text) AS SELECT $1, $2", prepare.toString()); assertRoundTrip(prepare); diff --git a/src/test/java/net/sf/jsqlparser/statement/create/table/CharacterSetShorthandTest.java b/src/test/java/net/sf/jsqlparser/statement/create/table/CharacterSetShorthandTest.java index e85042627..a477a61ad 100644 --- a/src/test/java/net/sf/jsqlparser/statement/create/table/CharacterSetShorthandTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/create/table/CharacterSetShorthandTest.java @@ -25,7 +25,7 @@ class CharacterSetShorthandTest { @ParameterizedTest - @CsvSource({"ASCII,latin1", "UNICODE,ucs2"}) + @CsvSource({"ASCII,latin1", "UNICODE,ucs2", "BYTE,binary"}) void shorthandsExposeCharacterSetAndRetainSpelling(String shorthand, String charset) throws JSQLParserException { PlainSelect select = (PlainSelect) TestUtils.assertSqlCanBeParsedAndDeparsed( @@ -38,7 +38,7 @@ void shorthandsExposeCharacterSetAndRetainSpelling(String shorthand, String char ColDataType jsonType = ((JsonFunction) select.getSelectItem(1).getExpression()).getReturningType(); for (ColDataType type : new ColDataType[] {castType, jsonType}) { - assertEquals("CHAR", type.getBaseTypeName()); + assertEquals("CHAR", type.getDataType()); assertEquals(charset, type.getCharacterSet()); assertEquals(CharacterSetSyntax.valueOf(shorthand), type.getCharacterSetSyntax()); } @@ -83,14 +83,13 @@ void characterSetSyntaxCanBeConstructedChangedAndCleared() throws JSQLParserExce type.setCharacterSet("utf8mb4"); assertEquals(CharacterSetSyntax.CHARACTER_SET, type.getCharacterSetSyntax()); assertEquals("CHAR (10) CHARACTER SET utf8mb4", type.toString()); - type.setUseCharsetKeyword(true); - assertTrue(type.isUseCharsetKeyword()); + type.setCharacterSetSyntax(CharacterSetSyntax.CHARSET); type.setCharacterSet("latin1"); assertEquals("CHAR (10) CHARSET latin1", type.toString()); TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT CAST('ab' AS " + type + ")", true, parser -> parser.withDialect(Dialect.MYSQL)); type.setCharacterSetSyntax(CharacterSetSyntax.ASCII); - type.setUseCharsetKeyword(false); + type.setCharacterSetSyntax(CharacterSetSyntax.CHARACTER_SET); assertEquals("CHAR (10) CHARACTER SET latin1", type.toString()); type.setCharacterSetSyntax(CharacterSetSyntax.UNICODE); type.setCharacterSet(null); @@ -108,9 +107,21 @@ void syntaxParticipatesInEqualityAndHashCode() { assertNotEquals(ascii, explicit); } + @Test + void byteMakesCharABinaryStringWithOrWithoutALength() throws JSQLParserException { + for (String sql : new String[] {"CREATE TABLE t (c CHAR BYTE)", + "CREATE TABLE t (c CHAR (16) BYTE)"}) { + CreateTable create = (CreateTable) TestUtils.assertSqlCanBeParsedAndDeparsed(sql, true, + parser -> parser.withDialect(Dialect.MYSQL)); + ColDataType type = create.getColumnDefinitions().get(0).getColDataType(); + assertEquals("binary", type.getCharacterSet()); + assertEquals(CharacterSetSyntax.BYTE, type.getCharacterSetSyntax()); + } + } + @Test void shorthandIsMySqlSpecificAndDoesNotReserveIdentifiers() throws JSQLParserException { - for (String shorthand : new String[] {"ASCII", "UNICODE"}) { + for (String shorthand : new String[] {"ASCII", "UNICODE", "BYTE"}) { String sql = "SELECT CAST('ab' AS CHAR(10) " + shorthand + ")"; assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil.parse(sql)); assertThrows(JSQLParserException.class, diff --git a/src/test/java/net/sf/jsqlparser/statement/create/table/ColDataTypeCorpusTest.java b/src/test/java/net/sf/jsqlparser/statement/create/table/ColDataTypeCorpusTest.java new file mode 100644 index 000000000..4af5396ea --- /dev/null +++ b/src/test/java/net/sf/jsqlparser/statement/create/table/ColDataTypeCorpusTest.java @@ -0,0 +1,104 @@ +/*- + * #%L + * JSQLParser library + * %% + * Copyright (C) 2004 - 2026 JSQLParser + * %% + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 + * #L% + */ +package net.sf.jsqlparser.statement.create.table; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; +import java.util.function.Consumer; +import java.util.stream.Collectors; +import net.sf.jsqlparser.JSQLParserException; +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; +import net.sf.jsqlparser.parser.CCJSqlParser; +import net.sf.jsqlparser.parser.CCJSqlParserUtil; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +/** + * Pins the values {@link ColDataType} exposes for a corpus of type spellings, kept in + * {@code type-corpus.tsv}. The MySQL and PostgreSQL rows hold every type spelling of MySQL 8.4 and + * PostgreSQL 18 in a column definition, and the spelling each server reports for that column as a + * type fragment, so that a change in how a type is read shows up as a changed row. + */ +class ColDataTypeCorpusTest { + + static final String CORPUS = "type-corpus.tsv"; + + static List rows() throws IOException { + List rows = new ArrayList<>(); + try (InputStream input = Objects.requireNonNull( + ColDataTypeCorpusTest.class.getResourceAsStream(CORPUS), CORPUS); + BufferedReader reader = new BufferedReader( + new InputStreamReader(input, StandardCharsets.UTF_8))) { + boolean header = true; + String line; + while ((line = reader.readLine()) != null) { + if (line.startsWith("#") || line.isEmpty()) { + continue; + } + if (header) { + header = false; + continue; + } + String[] columns = line.split("\t", 4); + rows.add(Arguments.of(columns[0], columns[1], columns[2], columns[3])); + } + } + return rows; + } + + @ParameterizedTest(name = "{0} {1} {2}") + @MethodSource("rows") + void readsTheTypeAsRecorded(String dialect, String source, String spelling, + String expected) { + assertEquals(expected, describe(dialect, source, spelling)); + } + + /** + * The recorded values for one row: the type's values separated by tabs, or {@code error} when + * the spelling does not parse. + */ + static String describe(String dialect, String source, String spelling) { + Consumer options = + "-".equals(dialect) ? null : parser -> parser.withDialect(Dialect.valueOf(dialect)); + try { + if ("fragment".equals(source)) { + return describe(CCJSqlParserUtil.parseColDataType(spelling, options), null); + } + CreateTable table = (CreateTable) CCJSqlParserUtil + .parse("CREATE TABLE t (c " + spelling + ")", options); + ColumnDefinition column = table.getColumnDefinitions().get(0); + return describe(column.getColDataType(), column.getColumnSpecs()); + } catch (JSQLParserException | RuntimeException e) { + return "error"; + } + } + + private static String describe(ColDataType type, List columnSpecs) { + return Arrays.asList(type.getDataType(), type.getArgumentsStringList(), + type.getPrecision(), type.getScale(), type.getTypeModifiers(), + type.getSignedness(), type.isZerofill(), type.getArrayData(), + type.isUsingArrayKeyword(), type.getCharacterSet(), + type.getCharacterSetSyntax(), type.getIntervalQualifier(), + type.getNationalCharacterType(), columnSpecs, type.toString()) + .stream() + .map(value -> value == null ? "-" : value.toString()) + .collect(Collectors.joining("\t")); + } +} diff --git a/src/test/java/net/sf/jsqlparser/statement/create/table/ColDataTypeTest.java b/src/test/java/net/sf/jsqlparser/statement/create/table/ColDataTypeTest.java index 111b8b571..f982c248b 100644 --- a/src/test/java/net/sf/jsqlparser/statement/create/table/ColDataTypeTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/create/table/ColDataTypeTest.java @@ -106,16 +106,20 @@ void testStructuredPrecisionForKeywordTypes() throws JSQLParserException { ColDataType varchar = parseColumnType("CREATE TABLE t (a VARCHAR(255))"); assertEquals(255, varchar.getPrecision()); assertNull(varchar.getScale()); - // the rendered string keeps its historical shape - assertEquals("VARCHAR (255)", varchar.getDataType()); + // the name and the arguments are apart, as for identifier types + assertEquals("VARCHAR", varchar.getDataType()); + assertEquals(java.util.List.of("255"), varchar.getArgumentsStringList()); ColDataType decimal = parseColumnType("CREATE TABLE t (a DECIMAL(10, 2))"); assertEquals(10, decimal.getPrecision()); assertEquals(2, decimal.getScale()); - assertEquals("DECIMAL (10, 2)", decimal.getDataType()); + assertEquals("DECIMAL", decimal.getDataType()); + assertEquals(java.util.List.of("10", "2"), decimal.getArgumentsStringList()); + // MAX is an argument, not a number ColDataType max = parseColumnType("CREATE TABLE t (a VARCHAR(MAX))"); - assertEquals(Integer.MAX_VALUE, max.getPrecision()); + assertEquals(java.util.List.of("MAX"), max.getArgumentsStringList()); + assertNull(max.getPrecision()); ColDataType plain = parseColumnType("CREATE TABLE t (a INT)"); assertNull(plain.getPrecision()); @@ -141,8 +145,10 @@ void testStructuredPrecisionForZonedTypes() throws JSQLParserException { ColDataType zoned = parseColumnType("CREATE TABLE t (a TIMESTAMP(3) WITH TIME ZONE)"); assertEquals(3, zoned.getPrecision()); assertNull(zoned.getScale()); - // the token image keeps its historical shape - assertEquals("TIMESTAMP(3) WITH TIME ZONE", zoned.getDataType()); + // the precision is an argument, and is written after the first word + assertEquals("TIMESTAMP WITH TIME ZONE", zoned.getDataType()); + assertEquals(java.util.List.of("3"), zoned.getArgumentsStringList()); + assertEquals("TIMESTAMP(3) WITH TIME ZONE", zoned.toString()); ColDataType unparameterized = parseColumnType("CREATE TABLE t (a TIMESTAMP WITH TIME ZONE)"); @@ -153,8 +159,8 @@ void testStructuredPrecisionForZonedTypes() throws JSQLParserException { void testOracleNumberWithWildcardPrecision() throws JSQLParserException { ColDataType number = parseColumnType("CREATE TABLE t (a NUMBER(*, 0), b NUMBER(*), c NUMBER(*, 2))"); - assertEquals("NUMBER (*, 0)", number.getDataType()); - assertEquals("NUMBER", number.getBaseTypeName()); + assertEquals("NUMBER", number.getDataType()); + assertEquals(java.util.List.of("*", "0"), number.getArgumentsStringList()); assertNull(number.getPrecision()); assertEquals(0, number.getScale()); diff --git a/src/test/java/net/sf/jsqlparser/statement/create/table/MySqlTypeCharacterSetTest.java b/src/test/java/net/sf/jsqlparser/statement/create/table/MySqlTypeCharacterSetTest.java index e562fddcc..99cae7726 100644 --- a/src/test/java/net/sf/jsqlparser/statement/create/table/MySqlTypeCharacterSetTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/create/table/MySqlTypeCharacterSetTest.java @@ -13,6 +13,7 @@ import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; import net.sf.jsqlparser.parser.CCJSqlParserUtil; import net.sf.jsqlparser.statement.Statement; +import net.sf.jsqlparser.statement.create.table.ColDataType.CharacterSetSyntax; import net.sf.jsqlparser.statement.select.PlainSelect; import net.sf.jsqlparser.util.deparser.StatementDeParser; import org.junit.jupiter.api.Test; @@ -51,10 +52,10 @@ void exposesCharsetAndRetainsAbbreviationWhenMutated() throws Exception { CreateTable table = (CreateTable) parse("CREATE TABLE t (c VARCHAR(20) CHARSET 'utf8mb4')"); ColDataType type = table.getColumnDefinitions().get(0).getColDataType(); assertEquals("'utf8mb4'", type.getCharacterSet()); - assertTrue(type.isUseCharsetKeyword()); + assertEquals(CharacterSetSyntax.CHARSET, type.getCharacterSetSyntax()); type.setCharacterSet("latin1"); assertEquals("CREATE TABLE t (c VARCHAR (20) CHARSET latin1)", table.toString()); - type.setUseCharsetKeyword(false); + type.setCharacterSetSyntax(CharacterSetSyntax.CHARACTER_SET); assertEquals("CREATE TABLE t (c VARCHAR (20) CHARACTER SET latin1)", table.toString()); type.setCharacterSet(null); assertEquals("CREATE TABLE t (c VARCHAR (20))", table.toString()); @@ -69,7 +70,7 @@ void preservesDefaultApiSpellingAndTypeEquality() throws Exception { ((CastExpression) select.getSelectItem(0).getExpression()).getColDataType(); assertEquals(legacy, parsed); assertEquals(legacy.hashCode(), parsed.hashCode()); - parsed.setUseCharsetKeyword(true); + parsed.setCharacterSetSyntax(CharacterSetSyntax.CHARSET); assertNotEquals(legacy, parsed); } diff --git a/src/test/java/net/sf/jsqlparser/statement/create/table/TypeMetadataTest.java b/src/test/java/net/sf/jsqlparser/statement/create/table/TypeMetadataTest.java index d5ec47ec2..1a2f25f13 100644 --- a/src/test/java/net/sf/jsqlparser/statement/create/table/TypeMetadataTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/create/table/TypeMetadataTest.java @@ -10,10 +10,8 @@ package net.sf.jsqlparser.statement.create.table; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; import java.math.BigInteger; import java.util.List; @@ -71,16 +69,15 @@ void exposesMetadataAcrossFragmentsDdlAndPostgreSqlCasts(Dialect dialect, String @ParameterizedTest @ValueSource(strings = {"0", "1", "255", "256", "65535", "65536", "16777215", "16777216", "2147483647", "2147483648", "4294967295"}) - void retainsLargeMySqlLengthsThroughBothRenderers(String length) throws JSQLParserException { - BigInteger expected = new BigInteger(length); + void keepsLargeMySqlLengthsAsWrittenThroughBothRenderers(String length) + throws JSQLParserException { + Integer precision = new BigInteger(length).bitLength() < Integer.SIZE + ? Integer.valueOf(length) + : null; for (String name : List.of("BLOB", "TEXT")) { ColDataType fragment = parseType(name + "(" + length + ")", Dialect.MYSQL); - assertEquals(name, fragment.getBaseTypeName()); - assertEquals(expected, fragment.getNumericPrecision()); - assertFalse(fragment.isMaxPrecision()); - assertFalse(fragment.toString().contains("MAX")); - assertEquals(expected.bitLength() < Integer.SIZE ? Integer.valueOf(length) : null, - fragment.getPrecision()); + assertMetadata(fragment, name, precision, null); + assertEquals(List.of(length), fragment.getArgumentsStringList()); CreateTable table = (CreateTable) CCJSqlParserUtil.parse( "CREATE TABLE type_case (v " + name + "(" + length + "))", p -> p.withDialect(Dialect.MYSQL)); @@ -90,81 +87,74 @@ void retainsLargeMySqlLengthsThroughBothRenderers(String length) throws JSQLPars CreateTable reparsed = (CreateTable) CCJSqlParserUtil.parse(rendered, p -> p.withDialect(Dialect.MYSQL)); ColDataType type = reparsed.getColumnDefinitions().get(0).getColDataType(); - assertEquals(expected, type.getNumericPrecision()); - assertFalse(type.isMaxPrecision()); + assertEquals(List.of(length), type.getArgumentsStringList()); + assertEquals(precision, type.getPrecision()); } } } @Test - void distinguishesMaxKeywordFromItsLegacySentinelValue() throws JSQLParserException { + void keepsMaxAsAnArgumentInsteadOfANumber() throws JSQLParserException { ColDataType max = parseType("VARCHAR(MAX)", Dialect.SQLSERVER); - assertTrue(max.isMaxPrecision()); - assertNull(max.getNumericPrecision()); - assertEquals(Integer.MAX_VALUE, max.getPrecision()); - assertEquals("VARCHAR (MAX)", max.getDataType()); + assertMetadata(max, "VARCHAR", null, null); + assertEquals(List.of("MAX"), max.getArgumentsStringList()); + assertEquals("VARCHAR (MAX)", max.toString()); ColDataType literal = parseType("BLOB(2147483647)", Dialect.MYSQL); - assertFalse(literal.isMaxPrecision()); - assertEquals(BigInteger.valueOf(Integer.MAX_VALUE), literal.getNumericPrecision()); - assertEquals("BLOB (2147483647)", literal.getDataType()); - assertEquals("VARCHAR (MAX)", new ColDataType("VARCHAR", Integer.MAX_VALUE, -1).toString()); - assertNull(new ColDataType("VARCHAR", -1, -1).getNumericPrecision()); + assertMetadata(literal, "BLOB", Integer.MAX_VALUE, null); + assertEquals("BLOB (2147483647)", literal.toString()); } @Test - void preservesNumericFactoryCompatibilityAlongsideExactParameters() { - ColDataType negative = ColDataType.fromNumericParameters("numeric", 2, -1); + void buildsATypeFromItsNameAndArguments() { + ColDataType negative = new ColDataType("numeric").addArgumentsStringList("2", "-1"); assertMetadata(negative, "numeric", 2, -1); assertEquals("numeric (2, -1)", negative.toString()); - ColDataType max = ColDataType.fromNumericParameters("VARCHAR", Integer.MAX_VALUE, null); - assertTrue(max.isMaxPrecision()); - assertNull(max.getNumericPrecision()); + ColDataType max = new ColDataType("VARCHAR").addArgumentsStringList("MAX"); + assertMetadata(max, "VARCHAR", null, null); assertEquals("VARCHAR (MAX)", max.toString()); - ColDataType numeric = ColDataType.fromTypeParameters("BLOB", "2147483647", null); - assertFalse(numeric.isMaxPrecision()); - assertEquals(BigInteger.valueOf(Integer.MAX_VALUE), numeric.getNumericPrecision()); - assertEquals("BLOB (2147483647)", numeric.toString()); + ColDataType zoned = new ColDataType("TIMESTAMP WITH TIME ZONE").addArgumentsStringList("3"); + assertMetadata(zoned, "TIMESTAMP WITH TIME ZONE", 3, null); + assertEquals("TIMESTAMP(3) WITH TIME ZONE", zoned.toString()); - ColDataType omitted = ColDataType.fromNumericParameters("numeric", null, null); + ColDataType omitted = new ColDataType("numeric"); assertMetadata(omitted, "numeric", null, null); assertEquals("numeric", omitted.toString()); } @Test - void metadataSettersDoNotLeaveStaleLargeOrMaxValues() throws JSQLParserException { + void precisionAndScaleFollowTheArguments() throws JSQLParserException { ColDataType type = parseType("BLOB(4294967295)", Dialect.MYSQL); - type.setPrecision(5); - assertEquals(BigInteger.valueOf(5), type.getNumericPrecision()); - type.setPrecision(null); - assertNull(type.getNumericPrecision()); - type = parseType("VARCHAR(MAX)", Dialect.SQLSERVER); - type.setNumericPrecision(BigInteger.valueOf(2147483648L)); - assertFalse(type.isMaxPrecision()); assertNull(type.getPrecision()); - assertEquals(BigInteger.valueOf(2147483648L), type.getNumericPrecision()); + type.setArgumentsStringList(List.of("5")); + assertEquals(5, type.getPrecision()); + assertEquals("BLOB (5)", type.toString()); + type = parseType("DECIMAL(10, 2)", Dialect.MYSQL); + type.setArgumentsStringList(List.of("12", "4")); + assertMetadata(type, "DECIMAL", 12, 4); + assertEquals("DECIMAL (12, 4)", type.toString()); + type.setArgumentsStringList(null); + assertMetadata(type, "DECIMAL", null, null); + assertEquals("DECIMAL", type.toString()); } @ParameterizedTest @ValueSource(strings = {"\"odd(type)\"", "public.\"odd(type)\"", "\"a\"\"(b)\""}) void retainsQuotedParenthesesAndQualification(String name) throws JSQLParserException { - assertEquals(name, parseType(name, Dialect.POSTGRESQL).getBaseTypeName()); - ColDataType manual = new ColDataType(name, 10, 2); - assertEquals(name, manual.getBaseTypeName()); - assertEquals(name + " (10, 2)", manual.getDataType()); + assertEquals(name, parseType(name, Dialect.POSTGRESQL).getDataType()); + ColDataType manual = new ColDataType(name).addArgumentsStringList("10", "2"); + assertMetadata(manual, name, 10, 2); + assertEquals(name + " (10, 2)", manual.toString()); + assertEquals(manual, parseType(manual.toString(), Dialect.POSTGRESQL)); } @Test - void baseNameFollowsLegacyTypeEditsWithoutStaleCache() { - ColDataType type = new ColDataType("DECIMAL", 10, 2); - type.setDataType("VARCHAR (20)"); - assertEquals("VARCHAR", type.getBaseTypeName()); - assertEquals("VARCHAR (20)", type.getDataType()); + void joinsTheWordsOfAQualifiedName() { + ColDataType type = new ColDataType(); type.setDataType(List.of("app", "\"kind(1)\"")); - assertEquals("app.\"kind(1)\"", type.getBaseTypeName()); - type.setDataType((String) null); - assertNull(type.getBaseTypeName()); + assertEquals("app.\"kind(1)\"", type.getDataType()); + assertEquals("app.\"kind(1)\"", type.toString()); } @ParameterizedTest @@ -177,8 +167,9 @@ void rejectsMalformedNumericParameters(String type) { void leavesDatabaseSpecificRangeValidationToTheDatabase() throws JSQLParserException { // Syntactically numeric, but MySQL rejects this BLOB length as out of range. String length = "18446744073709551616"; - assertEquals(new BigInteger(length), - parseType("BLOB(" + length + ")", Dialect.MYSQL).getNumericPrecision()); + ColDataType type = parseType("BLOB(" + length + ")", Dialect.MYSQL); + assertEquals(List.of(length), type.getArgumentsStringList()); + assertNull(type.getPrecision()); } @Test @@ -195,12 +186,10 @@ private static ColDataType parseType(String sql, Dialect dialect) throws JSQLPar return CCJSqlParserUtil.parseColDataType(sql, p -> p.withDialect(dialect)); } - private static void assertMetadata(ColDataType type, String base, Integer precision, + private static void assertMetadata(ColDataType type, String name, Integer precision, Integer scale) { - assertEquals(base, type.getBaseTypeName()); + assertEquals(name, type.getDataType()); assertEquals(precision, type.getPrecision()); - assertEquals(precision == null ? null : BigInteger.valueOf(precision), - type.getNumericPrecision()); assertEquals(scale, type.getScale()); } } diff --git a/src/test/java/net/sf/jsqlparser/statement/select/DuckDBTest.java b/src/test/java/net/sf/jsqlparser/statement/select/DuckDBTest.java index 570fd8614..a9fb74699 100644 --- a/src/test/java/net/sf/jsqlparser/statement/select/DuckDBTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/select/DuckDBTest.java @@ -86,7 +86,7 @@ void testCreateTypeAliasDataType() throws JSQLParserException { "CREATE TYPE reports.money_code AS DECIMAL(12, 2)"); AliasTypeDefinition definition = (AliasTypeDefinition) statement.getDefinition(); Assertions.assertEquals("reports.money_code", statement.getName()); - Assertions.assertEquals("DECIMAL", definition.getDataType().getBaseTypeName()); + Assertions.assertEquals("DECIMAL", definition.getDataType().getDataType()); Assertions.assertEquals(12, definition.getDataType().getPrecision()); Assertions.assertEquals(2, definition.getDataType().getScale()); } diff --git a/src/test/resources/net/sf/jsqlparser/statement/create/table/type-corpus.tsv b/src/test/resources/net/sf/jsqlparser/statement/create/table/type-corpus.tsv new file mode 100644 index 000000000..3484e1b9b --- /dev/null +++ b/src/test/resources/net/sf/jsqlparser/statement/create/table/type-corpus.tsv @@ -0,0 +1,350 @@ +# Column type spellings and the values ColDataType exposes for them (ColDataTypeCorpusTest). +# source: column is the type of a CREATE TABLE column, fragment is CCJSqlParserUtil.parseColDataType. +# The MYSQL and POSTGRESQL column rows are the type spellings of MySQL 8.4 and PostgreSQL 18; their +# fragment rows are the spellings the servers report for those columns: COLUMN_TYPE of +# information_schema.COLUMNS on MySQL 8.4.13 and format_type(atttypid, atttypmod) on PostgreSQL 18. +# The remaining rows are spellings of other dialects. A dash is null, error is a parse failure. +dialect source spelling getDataType getArgumentsStringList getPrecision getScale getTypeModifiers getSignedness isZerofill getArrayData isUsingArrayKeyword getCharacterSet getCharacterSetSyntax getIntervalQualifier getNationalCharacterType columnSpecs toString +MYSQL column tinyint tinyint - - - - - false [] false - CHARACTER_SET - - - tinyint +MYSQL column tinyint(1) tinyint [1] 1 - - - false [] false - CHARACTER_SET - - - tinyint (1) +MYSQL column tinyint(4) unsigned tinyint [4] 4 - [UNSIGNED] UNSIGNED false [] false - CHARACTER_SET - - - tinyint (4) UNSIGNED +MYSQL column smallint unsigned zerofill smallint - - - [UNSIGNED, ZEROFILL] UNSIGNED true [] false - CHARACTER_SET - - - smallint UNSIGNED ZEROFILL +MYSQL column mediumint(8) mediumint [8] 8 - - - false [] false - CHARACTER_SET - - - mediumint (8) +MYSQL column int(11) int [11] 11 - - - false [] false - CHARACTER_SET - - - int (11) +MYSQL column int(11) unsigned zerofill int [11] 11 - [UNSIGNED, ZEROFILL] UNSIGNED true [] false - CHARACTER_SET - - - int (11) UNSIGNED ZEROFILL +MYSQL column int signed int - - - [SIGNED] SIGNED false [] false - CHARACTER_SET - - - int SIGNED +MYSQL column integer integer - - - - - false [] false - CHARACTER_SET - - - integer +MYSQL column int1 int1 - - - - - false [] false - CHARACTER_SET - - - int1 +MYSQL column int2 int2 - - - - - false [] false - CHARACTER_SET - - - int2 +MYSQL column int3 int3 - - - - - false [] false - CHARACTER_SET - - - int3 +MYSQL column int4 int4 - - - - - false [] false - CHARACTER_SET - - - int4 +MYSQL column int8 int8 - - - - - false [] false - CHARACTER_SET - - - int8 +MYSQL column middleint middleint - - - - - false [] false - CHARACTER_SET - - - middleint +MYSQL column bigint(20) unsigned bigint [20] 20 - [UNSIGNED] UNSIGNED false [] false - CHARACTER_SET - - - bigint (20) UNSIGNED +MYSQL column serial serial - - - - - false [] false - CHARACTER_SET - - - serial +MYSQL column bool bool - - - - - false [] false - CHARACTER_SET - - - bool +MYSQL column boolean boolean - - - - - false [] false - CHARACTER_SET - - - boolean +MYSQL column bit bit - - - - - false [] false - CHARACTER_SET - - - bit +MYSQL column bit(5) bit [5] 5 - - - false [] false - CHARACTER_SET - - - bit (5) +MYSQL column decimal decimal - - - - - false [] false - CHARACTER_SET - - - decimal +MYSQL column decimal(10,2) decimal [10, 2] 10 2 - - false [] false - CHARACTER_SET - - - decimal (10, 2) +MYSQL column decimal(10,2) unsigned decimal [10, 2] 10 2 [UNSIGNED] UNSIGNED false [] false - CHARACTER_SET - - - decimal (10, 2) UNSIGNED +MYSQL column dec(5) dec [5] 5 - - - false [] false - CHARACTER_SET - - - dec (5) +MYSQL column fixed(6,1) fixed [6, 1] 6 1 - - false [] false - CHARACTER_SET - - - fixed (6, 1) +MYSQL column numeric(8,3) numeric [8, 3] 8 3 - - false [] false - CHARACTER_SET - - - numeric (8, 3) +MYSQL column float float - - - - - false [] false - CHARACTER_SET - - - float +MYSQL column float(10) float [10] 10 - - - false [] false - CHARACTER_SET - - - float (10) +MYSQL column float(30) float [30] 30 - - - false [] false - CHARACTER_SET - - - float (30) +MYSQL column float(10,2) float [10, 2] 10 2 - - false [] false - CHARACTER_SET - - - float (10, 2) +MYSQL column double double - - - - - false [] false - CHARACTER_SET - - - double +MYSQL column double(10,2) unsigned zerofill double [10, 2] 10 2 [UNSIGNED, ZEROFILL] UNSIGNED true [] false - CHARACTER_SET - - - double (10, 2) UNSIGNED ZEROFILL +MYSQL column double precision double precision - - - - - false [] false - CHARACTER_SET - - - double precision +MYSQL column double precision(10,2) double precision [10, 2] 10 2 - - false [] false - CHARACTER_SET - - - double precision (10, 2) +MYSQL column real real - - - - - false [] false - CHARACTER_SET - - - real +MYSQL column real(7,3) real [7, 3] 7 3 - - false [] false - CHARACTER_SET - - - real (7, 3) +MYSQL column date date - - - - - false [] false - CHARACTER_SET - - - date +MYSQL column time(3) time [3] 3 - - - false [] false - CHARACTER_SET - - - time (3) +MYSQL column datetime datetime - - - - - false [] false - CHARACTER_SET - - - datetime +MYSQL column datetime(6) datetime [6] 6 - - - false [] false - CHARACTER_SET - - - datetime (6) +MYSQL column timestamp(6) timestamp [6] 6 - - - false [] false - CHARACTER_SET - - - timestamp (6) +MYSQL column year year - - - - - false [] false - CHARACTER_SET - - - year +MYSQL column year(4) year [4] 4 - - - false [] false - CHARACTER_SET - - - year (4) +MYSQL column char char - - - - - false [] false - CHARACTER_SET - - - char +MYSQL column char(10) char [10] 10 - - - false [] false - CHARACTER_SET - - - char (10) +MYSQL column character(10) character [10] 10 - - - false [] false - CHARACTER_SET - - - character (10) +MYSQL column varchar(255) varchar [255] 255 - - - false [] false - CHARACTER_SET - - - varchar (255) +MYSQL column character varying(20) character varying [20] 20 - - - false [] false - CHARACTER_SET - - - character varying (20) +MYSQL column nchar(10) nchar [10] 10 - - - false [] false - CHARACTER_SET - CHAR - nchar (10) +MYSQL column national char(10) national char [10] 10 - - - false [] false - CHARACTER_SET - CHAR - national char (10) +MYSQL column national character(10) national character [10] 10 - - - false [] false - CHARACTER_SET - CHAR - national character (10) +MYSQL column nvarchar(10) nvarchar [10] 10 - - - false [] false - CHARACTER_SET - VARCHAR - nvarchar (10) +MYSQL column national varchar(10) national varchar [10] 10 - - - false [] false - CHARACTER_SET - VARCHAR - national varchar (10) +MYSQL column national character varying(10) national character varying [10] 10 - - - false [] false - CHARACTER_SET - VARCHAR - national character varying (10) +MYSQL column nchar varchar(10) nchar varchar [10] 10 - - - false [] false - CHARACTER_SET - VARCHAR - nchar varchar (10) +MYSQL column char(10) binary char [10] 10 - - - false [] false - CHARACTER_SET - - [binary] char (10) +MYSQL column varchar(10) charset latin1 varchar [10] 10 - - - false [] false latin1 CHARSET - - - varchar (10) CHARSET latin1 +MYSQL column varchar(10) character set utf8mb4 varchar [10] 10 - - - false [] false utf8mb4 CHARACTER_SET - - - varchar (10) CHARACTER SET utf8mb4 +MYSQL column char(5) ascii char [5] 5 - - - false [] false latin1 ASCII - - - char (5) ASCII +MYSQL column char(5) unicode char [5] 5 - - - false [] false ucs2 UNICODE - - - char (5) UNICODE +MYSQL column char byte char - - - - - false [] false binary BYTE - - - char BYTE +MYSQL column binary binary - - - - - false [] false - CHARACTER_SET - - - binary +MYSQL column binary(16) binary [16] 16 - - - false [] false - CHARACTER_SET - - - binary (16) +MYSQL column varbinary(64) varbinary [64] 64 - - - false [] false - CHARACTER_SET - - - varbinary (64) +MYSQL column tinyblob tinyblob - - - - - false [] false - CHARACTER_SET - - - tinyblob +MYSQL column blob blob - - - - - false [] false - CHARACTER_SET - - - blob +MYSQL column blob(100) blob [100] 100 - - - false [] false - CHARACTER_SET - - - blob (100) +MYSQL column mediumblob mediumblob - - - - - false [] false - CHARACTER_SET - - - mediumblob +MYSQL column longblob longblob - - - - - false [] false - CHARACTER_SET - - - longblob +MYSQL column tinytext tinytext - - - - - false [] false - CHARACTER_SET - - - tinytext +MYSQL column text text - - - - - false [] false - CHARACTER_SET - - - text +MYSQL column text(100) text [100] 100 - - - false [] false - CHARACTER_SET - - - text (100) +MYSQL column mediumtext mediumtext - - - - - false [] false - CHARACTER_SET - - - mediumtext +MYSQL column longtext longtext - - - - - false [] false - CHARACTER_SET - - - longtext +MYSQL column long long - - - - - false [] false - CHARACTER_SET - - - long +MYSQL column long varchar long varchar - - - - - false [] false - CHARACTER_SET - - - long varchar +MYSQL column long varbinary long varbinary - - - - - false [] false - CHARACTER_SET - - - long varbinary +MYSQL column enum('a','b','it''s') enum ['a', 'b', 'it''s'] - - - - false [] false - CHARACTER_SET - - - enum ('a', 'b', 'it''s') +MYSQL column enum('x','y') character set latin1 enum ['x', 'y'] - - - - false [] false latin1 CHARACTER_SET - - - enum ('x', 'y') CHARACTER SET latin1 +MYSQL column set('r','w','x') set ['r', 'w', 'x'] - - - - false [] false - CHARACTER_SET - - - set ('r', 'w', 'x') +MYSQL column json json - - - - - false [] false - CHARACTER_SET - - - json +MYSQL column geometry geometry - - - - - false [] false - CHARACTER_SET - - - geometry +MYSQL column point point - - - - - false [] false - CHARACTER_SET - - - point +MYSQL column linestring linestring - - - - - false [] false - CHARACTER_SET - - - linestring +MYSQL column polygon polygon - - - - - false [] false - CHARACTER_SET - - - polygon +MYSQL column multipoint multipoint - - - - - false [] false - CHARACTER_SET - - - multipoint +MYSQL column geometrycollection geometrycollection - - - - - false [] false - CHARACTER_SET - - - geometrycollection +MYSQL fragment tinyint tinyint - - - - - false [] false - CHARACTER_SET - - - tinyint +MYSQL fragment tinyint(1) tinyint [1] 1 - - - false [] false - CHARACTER_SET - - - tinyint (1) +MYSQL fragment tinyint unsigned tinyint - - - [UNSIGNED] UNSIGNED false [] false - CHARACTER_SET - - - tinyint UNSIGNED +MYSQL fragment smallint(5) unsigned zerofill smallint [5] 5 - [UNSIGNED, ZEROFILL] UNSIGNED true [] false - CHARACTER_SET - - - smallint (5) UNSIGNED ZEROFILL +MYSQL fragment mediumint mediumint - - - - - false [] false - CHARACTER_SET - - - mediumint +MYSQL fragment int int - - - - - false [] false - CHARACTER_SET - - - int +MYSQL fragment int(11) unsigned zerofill int [11] 11 - [UNSIGNED, ZEROFILL] UNSIGNED true [] false - CHARACTER_SET - - - int (11) UNSIGNED ZEROFILL +MYSQL fragment smallint smallint - - - - - false [] false - CHARACTER_SET - - - smallint +MYSQL fragment bigint bigint - - - - - false [] false - CHARACTER_SET - - - bigint +MYSQL fragment bigint unsigned bigint - - - [UNSIGNED] UNSIGNED false [] false - CHARACTER_SET - - - bigint UNSIGNED +MYSQL fragment bit(1) bit [1] 1 - - - false [] false - CHARACTER_SET - - - bit (1) +MYSQL fragment bit(5) bit [5] 5 - - - false [] false - CHARACTER_SET - - - bit (5) +MYSQL fragment decimal(10,0) decimal [10, 0] 10 0 - - false [] false - CHARACTER_SET - - - decimal (10, 0) +MYSQL fragment decimal(10,2) decimal [10, 2] 10 2 - - false [] false - CHARACTER_SET - - - decimal (10, 2) +MYSQL fragment decimal(10,2) unsigned decimal [10, 2] 10 2 [UNSIGNED] UNSIGNED false [] false - CHARACTER_SET - - - decimal (10, 2) UNSIGNED +MYSQL fragment decimal(5,0) decimal [5, 0] 5 0 - - false [] false - CHARACTER_SET - - - decimal (5, 0) +MYSQL fragment decimal(6,1) decimal [6, 1] 6 1 - - false [] false - CHARACTER_SET - - - decimal (6, 1) +MYSQL fragment decimal(8,3) decimal [8, 3] 8 3 - - false [] false - CHARACTER_SET - - - decimal (8, 3) +MYSQL fragment float float - - - - - false [] false - CHARACTER_SET - - - float +MYSQL fragment double double - - - - - false [] false - CHARACTER_SET - - - double +MYSQL fragment float(10,2) float [10, 2] 10 2 - - false [] false - CHARACTER_SET - - - float (10, 2) +MYSQL fragment double(10,2) unsigned zerofill double [10, 2] 10 2 [UNSIGNED, ZEROFILL] UNSIGNED true [] false - CHARACTER_SET - - - double (10, 2) UNSIGNED ZEROFILL +MYSQL fragment double(10,2) double [10, 2] 10 2 - - false [] false - CHARACTER_SET - - - double (10, 2) +MYSQL fragment double(7,3) double [7, 3] 7 3 - - false [] false - CHARACTER_SET - - - double (7, 3) +MYSQL fragment date date - - - - - false [] false - CHARACTER_SET - - - date +MYSQL fragment time(3) time [3] 3 - - - false [] false - CHARACTER_SET - - - time (3) +MYSQL fragment datetime datetime - - - - - false [] false - CHARACTER_SET - - - datetime +MYSQL fragment datetime(6) datetime [6] 6 - - - false [] false - CHARACTER_SET - - - datetime (6) +MYSQL fragment timestamp(6) timestamp [6] 6 - - - false [] false - CHARACTER_SET - - - timestamp (6) +MYSQL fragment year year - - - - - false [] false - CHARACTER_SET - - - year +MYSQL fragment char(1) char [1] 1 - - - false [] false - CHARACTER_SET - - - char (1) +MYSQL fragment char(10) char [10] 10 - - - false [] false - CHARACTER_SET - - - char (10) +MYSQL fragment varchar(255) varchar [255] 255 - - - false [] false - CHARACTER_SET - - - varchar (255) +MYSQL fragment varchar(20) varchar [20] 20 - - - false [] false - CHARACTER_SET - - - varchar (20) +MYSQL fragment varchar(10) varchar [10] 10 - - - false [] false - CHARACTER_SET - - - varchar (10) +MYSQL fragment char(5) char [5] 5 - - - false [] false - CHARACTER_SET - - - char (5) +MYSQL fragment binary(1) binary [1] 1 - - - false [] false - CHARACTER_SET - - - binary (1) +MYSQL fragment binary(16) binary [16] 16 - - - false [] false - CHARACTER_SET - - - binary (16) +MYSQL fragment varbinary(64) varbinary [64] 64 - - - false [] false - CHARACTER_SET - - - varbinary (64) +MYSQL fragment tinyblob tinyblob - - - - - false [] false - CHARACTER_SET - - - tinyblob +MYSQL fragment blob blob - - - - - false [] false - CHARACTER_SET - - - blob +MYSQL fragment mediumblob mediumblob - - - - - false [] false - CHARACTER_SET - - - mediumblob +MYSQL fragment longblob longblob - - - - - false [] false - CHARACTER_SET - - - longblob +MYSQL fragment tinytext tinytext - - - - - false [] false - CHARACTER_SET - - - tinytext +MYSQL fragment text text - - - - - false [] false - CHARACTER_SET - - - text +MYSQL fragment mediumtext mediumtext - - - - - false [] false - CHARACTER_SET - - - mediumtext +MYSQL fragment longtext longtext - - - - - false [] false - CHARACTER_SET - - - longtext +MYSQL fragment enum('a','b','it''s') enum ['a', 'b', 'it''s'] - - - - false [] false - CHARACTER_SET - - - enum ('a', 'b', 'it''s') +MYSQL fragment enum('x','y') enum ['x', 'y'] - - - - false [] false - CHARACTER_SET - - - enum ('x', 'y') +MYSQL fragment set('r','w','x') set ['r', 'w', 'x'] - - - - false [] false - CHARACTER_SET - - - set ('r', 'w', 'x') +MYSQL fragment json json - - - - - false [] false - CHARACTER_SET - - - json +MYSQL fragment geometry geometry - - - - - false [] false - CHARACTER_SET - - - geometry +MYSQL fragment point point - - - - - false [] false - CHARACTER_SET - - - point +MYSQL fragment linestring linestring - - - - - false [] false - CHARACTER_SET - - - linestring +MYSQL fragment polygon polygon - - - - - false [] false - CHARACTER_SET - - - polygon +MYSQL fragment multipoint multipoint - - - - - false [] false - CHARACTER_SET - - - multipoint +MYSQL fragment geomcollection geomcollection - - - - - false [] false - CHARACTER_SET - - - geomcollection +POSTGRESQL column smallint smallint - - - - - false [] false - CHARACTER_SET - - - smallint +POSTGRESQL column integer integer - - - - - false [] false - CHARACTER_SET - - - integer +POSTGRESQL column int int - - - - - false [] false - CHARACTER_SET - - - int +POSTGRESQL column int2 int2 - - - - - false [] false - CHARACTER_SET - - - int2 +POSTGRESQL column int4 int4 - - - - - false [] false - CHARACTER_SET - - - int4 +POSTGRESQL column int8 int8 - - - - - false [] false - CHARACTER_SET - - - int8 +POSTGRESQL column bigint bigint - - - - - false [] false - CHARACTER_SET - - - bigint +POSTGRESQL column smallserial smallserial - - - - - false [] false - CHARACTER_SET - - - smallserial +POSTGRESQL column serial serial - - - - - false [] false - CHARACTER_SET - - - serial +POSTGRESQL column bigserial bigserial - - - - - false [] false - CHARACTER_SET - - - bigserial +POSTGRESQL column serial4 serial4 - - - - - false [] false - CHARACTER_SET - - - serial4 +POSTGRESQL column numeric numeric - - - - - false [] false - CHARACTER_SET - - - numeric +POSTGRESQL column numeric(10) numeric [10] 10 - - - false [] false - CHARACTER_SET - - - numeric (10) +POSTGRESQL column numeric(10,2) numeric [10, 2] 10 2 - - false [] false - CHARACTER_SET - - - numeric (10, 2) +POSTGRESQL column numeric(2,-3) numeric [2, -3] 2 -3 - - false [] false - CHARACTER_SET - - - numeric (2, -3) +POSTGRESQL column decimal(10,2) decimal [10, 2] 10 2 - - false [] false - CHARACTER_SET - - - decimal (10, 2) +POSTGRESQL column real real - - - - - false [] false - CHARACTER_SET - - - real +POSTGRESQL column float4 float4 - - - - - false [] false - CHARACTER_SET - - - float4 +POSTGRESQL column float8 float8 - - - - - false [] false - CHARACTER_SET - - - float8 +POSTGRESQL column float float - - - - - false [] false - CHARACTER_SET - - - float +POSTGRESQL column float(10) float [10] 10 - - - false [] false - CHARACTER_SET - - - float (10) +POSTGRESQL column float(30) float [30] 30 - - - false [] false - CHARACTER_SET - - - float (30) +POSTGRESQL column double precision double precision - - - - - false [] false - CHARACTER_SET - - - double precision +POSTGRESQL column money money - - - - - false [] false - CHARACTER_SET - - - money +POSTGRESQL column boolean boolean - - - - - false [] false - CHARACTER_SET - - - boolean +POSTGRESQL column bool bool - - - - - false [] false - CHARACTER_SET - - - bool +POSTGRESQL column char char - - - - - false [] false - CHARACTER_SET - - - char +POSTGRESQL column char(10) char [10] 10 - - - false [] false - CHARACTER_SET - - - char (10) +POSTGRESQL column character(10) character [10] 10 - - - false [] false - CHARACTER_SET - - - character (10) +POSTGRESQL column bpchar bpchar - - - - - false [] false - CHARACTER_SET - - - bpchar +POSTGRESQL column "char" "char" - - - - - false [] false - CHARACTER_SET - - - "char" +POSTGRESQL column varchar varchar - - - - - false [] false - CHARACTER_SET - - - varchar +POSTGRESQL column varchar(10) varchar [10] 10 - - - false [] false - CHARACTER_SET - - - varchar (10) +POSTGRESQL column character varying character varying - - - - - false [] false - CHARACTER_SET - - - character varying +POSTGRESQL column character varying(20) character varying [20] 20 - - - false [] false - CHARACTER_SET - - - character varying (20) +POSTGRESQL column text text - - - - - false [] false - CHARACTER_SET - - - text +POSTGRESQL column name name - - - - - false [] false - CHARACTER_SET - - - name +POSTGRESQL column bytea bytea - - - - - false [] false - CHARACTER_SET - - - bytea +POSTGRESQL column date date - - - - - false [] false - CHARACTER_SET - - - date +POSTGRESQL column time time - - - - - false [] false - CHARACTER_SET - - - time +POSTGRESQL column time(3) time [3] 3 - - - false [] false - CHARACTER_SET - - - time (3) +POSTGRESQL column time without time zone time without time zone - - - - - false [] false - CHARACTER_SET - - - time without time zone +POSTGRESQL column time(3) without time zone time without time zone [3] 3 - - - false [] false - CHARACTER_SET - - - time(3) without time zone +POSTGRESQL column time with time zone time with time zone - - - - - false [] false - CHARACTER_SET - - - time with time zone +POSTGRESQL column time(3) with time zone time with time zone [3] 3 - - - false [] false - CHARACTER_SET - - - time(3) with time zone +POSTGRESQL column timetz timetz - - - - - false [] false - CHARACTER_SET - - - timetz +POSTGRESQL column timetz(3) timetz [3] 3 - - - false [] false - CHARACTER_SET - - - timetz (3) +POSTGRESQL column timestamp timestamp - - - - - false [] false - CHARACTER_SET - - - timestamp +POSTGRESQL column timestamp(3) timestamp [3] 3 - - - false [] false - CHARACTER_SET - - - timestamp (3) +POSTGRESQL column timestamp without time zone timestamp without time zone - - - - - false [] false - CHARACTER_SET - - - timestamp without time zone +POSTGRESQL column timestamp(3) without time zone timestamp without time zone [3] 3 - - - false [] false - CHARACTER_SET - - - timestamp(3) without time zone +POSTGRESQL column timestamp with time zone timestamp with time zone - - - - - false [] false - CHARACTER_SET - - - timestamp with time zone +POSTGRESQL column timestamp(3) with time zone timestamp with time zone [3] 3 - - - false [] false - CHARACTER_SET - - - timestamp(3) with time zone +POSTGRESQL column timestamptz timestamptz - - - - - false [] false - CHARACTER_SET - - - timestamptz +POSTGRESQL column timestamptz(3) timestamptz [3] 3 - - - false [] false - CHARACTER_SET - - - timestamptz (3) +POSTGRESQL column interval interval - - - - - false [] false - CHARACTER_SET - - - interval +POSTGRESQL column interval(3) interval [3] 3 - - - false [] false - CHARACTER_SET - - - interval (3) +POSTGRESQL column interval year interval - - - - - false [] false - CHARACTER_SET year - - interval year +POSTGRESQL column interval year to month interval - - - - - false [] false - CHARACTER_SET year TO month - - interval year TO month +POSTGRESQL column interval day to second interval - - - - - false [] false - CHARACTER_SET day TO second - - interval day TO second +POSTGRESQL column interval day to second(3) interval - - - - - false [] false - CHARACTER_SET day TO second(3) - - interval day TO second(3) +POSTGRESQL column interval second(3) interval - - - - - false [] false - CHARACTER_SET second(3) - - interval second(3) +POSTGRESQL column interval minute to second(2) interval - - - - - false [] false - CHARACTER_SET minute TO second(2) - - interval minute TO second(2) +POSTGRESQL column bit bit - - - - - false [] false - CHARACTER_SET - - - bit +POSTGRESQL column bit(3) bit [3] 3 - - - false [] false - CHARACTER_SET - - - bit (3) +POSTGRESQL column bit varying bit varying - - - - - false [] false - CHARACTER_SET - - - bit varying +POSTGRESQL column bit varying(3) bit varying [3] 3 - - - false [] false - CHARACTER_SET - - - bit varying (3) +POSTGRESQL column varbit(3) varbit [3] 3 - - - false [] false - CHARACTER_SET - - - varbit (3) +POSTGRESQL column uuid uuid - - - - - false [] false - CHARACTER_SET - - - uuid +POSTGRESQL column json json - - - - - false [] false - CHARACTER_SET - - - json +POSTGRESQL column jsonb jsonb - - - - - false [] false - CHARACTER_SET - - - jsonb +POSTGRESQL column xml xml - - - - - false [] false - CHARACTER_SET - - - xml +POSTGRESQL column inet inet - - - - - false [] false - CHARACTER_SET - - - inet +POSTGRESQL column cidr cidr - - - - - false [] false - CHARACTER_SET - - - cidr +POSTGRESQL column macaddr macaddr - - - - - false [] false - CHARACTER_SET - - - macaddr +POSTGRESQL column macaddr8 macaddr8 - - - - - false [] false - CHARACTER_SET - - - macaddr8 +POSTGRESQL column point point - - - - - false [] false - CHARACTER_SET - - - point +POSTGRESQL column line line - - - - - false [] false - CHARACTER_SET - - - line +POSTGRESQL column lseg lseg - - - - - false [] false - CHARACTER_SET - - - lseg +POSTGRESQL column box box - - - - - false [] false - CHARACTER_SET - - - box +POSTGRESQL column path path - - - - - false [] false - CHARACTER_SET - - - path +POSTGRESQL column polygon polygon - - - - - false [] false - CHARACTER_SET - - - polygon +POSTGRESQL column circle circle - - - - - false [] false - CHARACTER_SET - - - circle +POSTGRESQL column tsvector tsvector - - - - - false [] false - CHARACTER_SET - - - tsvector +POSTGRESQL column tsquery tsquery - - - - - false [] false - CHARACTER_SET - - - tsquery +POSTGRESQL column int4range int4range - - - - - false [] false - CHARACTER_SET - - - int4range +POSTGRESQL column tstzrange tstzrange - - - - - false [] false - CHARACTER_SET - - - tstzrange +POSTGRESQL column pg_lsn pg_lsn - - - - - false [] false - CHARACTER_SET - - - pg_lsn +POSTGRESQL column oid oid - - - - - false [] false - CHARACTER_SET - - - oid +POSTGRESQL column regclass regclass - - - - - false [] false - CHARACTER_SET - - - regclass +POSTGRESQL column int[] int - - - - - false [null] false - CHARACTER_SET - - - int[] +POSTGRESQL column int[3] int - - - - - false [3] false - CHARACTER_SET - - - int[3] +POSTGRESQL column int[][] int - - - - - false [null, null] false - CHARACTER_SET - - - int[][] +POSTGRESQL column int array int - - - - - false [null] true - CHARACTER_SET - - - int ARRAY +POSTGRESQL column int array[3] int - - - - - false [3] true - CHARACTER_SET - - - int ARRAY[3] +POSTGRESQL column integer[] integer - - - - - false [null] false - CHARACTER_SET - - - integer[] +POSTGRESQL column text[] text - - - - - false [null] false - CHARACTER_SET - - - text[] +POSTGRESQL column varchar(10)[] varchar [10] 10 - - - false [null] false - CHARACTER_SET - - - varchar (10)[] +POSTGRESQL column character varying(10)[] character varying [10] 10 - - - false [null] false - CHARACTER_SET - - - character varying (10)[] +POSTGRESQL column numeric(10,2)[] numeric [10, 2] 10 2 - - false [null] false - CHARACTER_SET - - - numeric (10, 2)[] +POSTGRESQL column timestamp(3) with time zone[] timestamp with time zone [3] 3 - - - false [null] false - CHARACTER_SET - - - timestamp(3) with time zone[] +POSTGRESQL column timestamptz(3)[] timestamptz [3] 3 - - - false [null] false - CHARACTER_SET - - - timestamptz (3)[] +POSTGRESQL column double precision[] double precision - - - - - false [null] false - CHARACTER_SET - - - double precision[] +POSTGRESQL column bit varying(3)[] bit varying [3] 3 - - - false [null] false - CHARACTER_SET - - - bit varying (3)[] +POSTGRESQL column interval day to second(3)[] interval - - - - - false [null] false - CHARACTER_SET day TO second(3) - - interval day TO second(3)[] +POSTGRESQL column public.citext_like public.citext_like - - - - - false [] false - CHARACTER_SET - - - public.citext_like +POSTGRESQL column "MyType" "MyType" - - - - - false [] false - CHARACTER_SET - - - "MyType" +POSTGRESQL fragment smallint smallint - - - - - false [] false - CHARACTER_SET - - - smallint +POSTGRESQL fragment integer integer - - - - - false [] false - CHARACTER_SET - - - integer +POSTGRESQL fragment bigint bigint - - - - - false [] false - CHARACTER_SET - - - bigint +POSTGRESQL fragment numeric numeric - - - - - false [] false - CHARACTER_SET - - - numeric +POSTGRESQL fragment numeric(10,0) numeric [10, 0] 10 0 - - false [] false - CHARACTER_SET - - - numeric (10, 0) +POSTGRESQL fragment numeric(10,2) numeric [10, 2] 10 2 - - false [] false - CHARACTER_SET - - - numeric (10, 2) +POSTGRESQL fragment numeric(2,-3) numeric [2, -3] 2 -3 - - false [] false - CHARACTER_SET - - - numeric (2, -3) +POSTGRESQL fragment real real - - - - - false [] false - CHARACTER_SET - - - real +POSTGRESQL fragment double precision double precision - - - - - false [] false - CHARACTER_SET - - - double precision +POSTGRESQL fragment money money - - - - - false [] false - CHARACTER_SET - - - money +POSTGRESQL fragment boolean boolean - - - - - false [] false - CHARACTER_SET - - - boolean +POSTGRESQL fragment character(1) character [1] 1 - - - false [] false - CHARACTER_SET - - - character (1) +POSTGRESQL fragment character(10) character [10] 10 - - - false [] false - CHARACTER_SET - - - character (10) +POSTGRESQL fragment bpchar bpchar - - - - - false [] false - CHARACTER_SET - - - bpchar +POSTGRESQL fragment "char" "char" - - - - - false [] false - CHARACTER_SET - - - "char" +POSTGRESQL fragment character varying character varying - - - - - false [] false - CHARACTER_SET - - - character varying +POSTGRESQL fragment character varying(10) character varying [10] 10 - - - false [] false - CHARACTER_SET - - - character varying (10) +POSTGRESQL fragment character varying(20) character varying [20] 20 - - - false [] false - CHARACTER_SET - - - character varying (20) +POSTGRESQL fragment text text - - - - - false [] false - CHARACTER_SET - - - text +POSTGRESQL fragment name name - - - - - false [] false - CHARACTER_SET - - - name +POSTGRESQL fragment bytea bytea - - - - - false [] false - CHARACTER_SET - - - bytea +POSTGRESQL fragment date date - - - - - false [] false - CHARACTER_SET - - - date +POSTGRESQL fragment time without time zone time without time zone - - - - - false [] false - CHARACTER_SET - - - time without time zone +POSTGRESQL fragment time(3) without time zone time without time zone [3] 3 - - - false [] false - CHARACTER_SET - - - time(3) without time zone +POSTGRESQL fragment time with time zone time with time zone - - - - - false [] false - CHARACTER_SET - - - time with time zone +POSTGRESQL fragment time(3) with time zone time with time zone [3] 3 - - - false [] false - CHARACTER_SET - - - time(3) with time zone +POSTGRESQL fragment timestamp without time zone timestamp without time zone - - - - - false [] false - CHARACTER_SET - - - timestamp without time zone +POSTGRESQL fragment timestamp(3) without time zone timestamp without time zone [3] 3 - - - false [] false - CHARACTER_SET - - - timestamp(3) without time zone +POSTGRESQL fragment timestamp with time zone timestamp with time zone - - - - - false [] false - CHARACTER_SET - - - timestamp with time zone +POSTGRESQL fragment timestamp(3) with time zone timestamp with time zone [3] 3 - - - false [] false - CHARACTER_SET - - - timestamp(3) with time zone +POSTGRESQL fragment interval interval - - - - - false [] false - CHARACTER_SET - - - interval +POSTGRESQL fragment interval(3) interval [3] 3 - - - false [] false - CHARACTER_SET - - - interval (3) +POSTGRESQL fragment interval year interval - - - - - false [] false - CHARACTER_SET year - - interval year +POSTGRESQL fragment interval year to month interval - - - - - false [] false - CHARACTER_SET year TO month - - interval year TO month +POSTGRESQL fragment interval day to second interval - - - - - false [] false - CHARACTER_SET day TO second - - interval day TO second +POSTGRESQL fragment interval day to second(3) interval - - - - - false [] false - CHARACTER_SET day TO second(3) - - interval day TO second(3) +POSTGRESQL fragment interval second(3) interval - - - - - false [] false - CHARACTER_SET second(3) - - interval second(3) +POSTGRESQL fragment interval minute to second(2) interval - - - - - false [] false - CHARACTER_SET minute TO second(2) - - interval minute TO second(2) +POSTGRESQL fragment bit(1) bit [1] 1 - - - false [] false - CHARACTER_SET - - - bit (1) +POSTGRESQL fragment bit(3) bit [3] 3 - - - false [] false - CHARACTER_SET - - - bit (3) +POSTGRESQL fragment bit varying bit varying - - - - - false [] false - CHARACTER_SET - - - bit varying +POSTGRESQL fragment bit varying(3) bit varying [3] 3 - - - false [] false - CHARACTER_SET - - - bit varying (3) +POSTGRESQL fragment uuid uuid - - - - - false [] false - CHARACTER_SET - - - uuid +POSTGRESQL fragment json json - - - - - false [] false - CHARACTER_SET - - - json +POSTGRESQL fragment jsonb jsonb - - - - - false [] false - CHARACTER_SET - - - jsonb +POSTGRESQL fragment xml xml - - - - - false [] false - CHARACTER_SET - - - xml +POSTGRESQL fragment inet inet - - - - - false [] false - CHARACTER_SET - - - inet +POSTGRESQL fragment cidr cidr - - - - - false [] false - CHARACTER_SET - - - cidr +POSTGRESQL fragment macaddr macaddr - - - - - false [] false - CHARACTER_SET - - - macaddr +POSTGRESQL fragment macaddr8 macaddr8 - - - - - false [] false - CHARACTER_SET - - - macaddr8 +POSTGRESQL fragment point point - - - - - false [] false - CHARACTER_SET - - - point +POSTGRESQL fragment line line - - - - - false [] false - CHARACTER_SET - - - line +POSTGRESQL fragment lseg lseg - - - - - false [] false - CHARACTER_SET - - - lseg +POSTGRESQL fragment box box - - - - - false [] false - CHARACTER_SET - - - box +POSTGRESQL fragment path path - - - - - false [] false - CHARACTER_SET - - - path +POSTGRESQL fragment polygon polygon - - - - - false [] false - CHARACTER_SET - - - polygon +POSTGRESQL fragment circle circle - - - - - false [] false - CHARACTER_SET - - - circle +POSTGRESQL fragment tsvector tsvector - - - - - false [] false - CHARACTER_SET - - - tsvector +POSTGRESQL fragment tsquery tsquery - - - - - false [] false - CHARACTER_SET - - - tsquery +POSTGRESQL fragment int4range int4range - - - - - false [] false - CHARACTER_SET - - - int4range +POSTGRESQL fragment tstzrange tstzrange - - - - - false [] false - CHARACTER_SET - - - tstzrange +POSTGRESQL fragment pg_lsn pg_lsn - - - - - false [] false - CHARACTER_SET - - - pg_lsn +POSTGRESQL fragment oid oid - - - - - false [] false - CHARACTER_SET - - - oid +POSTGRESQL fragment regclass regclass - - - - - false [] false - CHARACTER_SET - - - regclass +POSTGRESQL fragment integer[] integer - - - - - false [null] false - CHARACTER_SET - - - integer[] +POSTGRESQL fragment text[] text - - - - - false [null] false - CHARACTER_SET - - - text[] +POSTGRESQL fragment character varying(10)[] character varying [10] 10 - - - false [null] false - CHARACTER_SET - - - character varying (10)[] +POSTGRESQL fragment numeric(10,2)[] numeric [10, 2] 10 2 - - false [null] false - CHARACTER_SET - - - numeric (10, 2)[] +POSTGRESQL fragment timestamp(3) with time zone[] timestamp with time zone [3] 3 - - - false [null] false - CHARACTER_SET - - - timestamp(3) with time zone[] +POSTGRESQL fragment double precision[] double precision - - - - - false [null] false - CHARACTER_SET - - - double precision[] +POSTGRESQL fragment bit varying(3)[] bit varying [3] 3 - - - false [null] false - CHARACTER_SET - - - bit varying (3)[] +POSTGRESQL fragment interval day to second(3)[] interval - - - - - false [null] false - CHARACTER_SET day TO second(3) - - interval day TO second(3)[] +POSTGRESQL fragment citext_like citext_like - - - - - false [] false - CHARACTER_SET - - - citext_like +POSTGRESQL fragment "MyType" "MyType" - - - - - false [] false - CHARACTER_SET - - - "MyType" +ORACLE column char(10 byte) char [10 byte] 10 - - - false [] false - CHARACTER_SET - - - char (10 byte) +ORACLE column varchar2(10 char) varchar2 [10 char] 10 - - - false [] false - CHARACTER_SET - - - varchar2 (10 char) +ORACLE column number(*,2) number [*, 2] - 2 - - false [] false - CHARACTER_SET - - - number (*, 2) +ORACLE column number(10) number [10] 10 - - - false [] false - CHARACTER_SET - - - number (10) +ORACLE column raw(16) raw [16] 16 - - - false [] false - CHARACTER_SET - - - raw (16) +ORACLE column timestamp(6) with local time zone timestamp with local time zone [6] 6 - - - false [] false - CHARACTER_SET - - - timestamp(6) with local time zone +ORACLE column interval day(2) to second(6) interval - - - - - false [] false - CHARACTER_SET day(2) TO second(6) - - interval day(2) TO second(6) +ORACLE column interval year(2) to month interval - - - - - false [] false - CHARACTER_SET year(2) TO month - - interval year(2) TO month +SQLSERVER column varchar(max) varchar [max] - - - - false [] false - CHARACTER_SET - - - varchar (max) +SQLSERVER column nvarchar(max) nvarchar [max] - - - - false [] false - CHARACTER_SET - - - nvarchar (max) +SQLSERVER column datetimeoffset(7) datetimeoffset [7] 7 - - - false [] false - CHARACTER_SET - - - datetimeoffset (7) +SQLSERVER column decimal(38,4) decimal [38, 4] 38 4 - - false [] false - CHARACTER_SET - - - decimal (38, 4) +- column map(varchar, int) map [varchar, int] - - - - false [] false - CHARACTER_SET - - - map (varchar, int) +- column struct(a int, b varchar(10)) STRUCT [a int, b varchar (10)] - - - - false [] false - CHARACTER_SET - - - STRUCT (a int, b varchar (10)) +- column vector(3) vector [3] 3 - - - false [] false - CHARACTER_SET - - - vector (3) +- column varchar(10) array varchar [10] 10 - - - false [null] true - CHARACTER_SET - - - varchar (10) ARRAY +- column numeric(4294967296) numeric [4294967296] - - - - false [] false - CHARACTER_SET - - - numeric (4294967296)