kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 013267ad730db7a46788079c5e25d02a1fbd051c2b996510d20d8936f33b263c |
|---|---|
| Author | Alexandre Dutra <adutra@users.noreply.github.com> · 2015-11-25 18:07 +01:00 |
| Committer | Alexandre Dutra <adutra@users.noreply.github.com> · 2015-11-25 18:07 +01:00 |
| Parents | 0e80934e7fe4, 0029343b4ddc |
16 files changed
modified changelog/README.md +1 −0
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | |
| 5 | 5 | - [bug] JAVA-993: Support for "custom" types after CASSANDRA-10365. |
| 6 | 6 | - [bug] JAVA-999: Handle unset parameters in QueryLogger. |
| 7 | +- [bug] JAVA-998: SchemaChangeListener not invoked for Functions or Aggregates having UDT arguments. | |
| 7 | 8 | |
| 8 | 9 | |
| 9 | 10 | ### 3.0.0-alpha5 |
modified driver-core/src/main/java/com/datastax/driver/core/AggregateMetadata.java +35 −34
| @@ -29,7 +29,6 @@ | ||
| 29 | 29 | public class AggregateMetadata { |
| 30 | 30 | |
| 31 | 31 | private final KeyspaceMetadata keyspace; |
| 32 | - private final String fullName; | |
| 33 | 32 | private final String simpleName; |
| 34 | 33 | private final List<DataType> argumentTypes; |
| 35 | 34 | private final String finalFuncSimpleName; |
| @@ -41,11 +40,10 @@ | ||
| 41 | 40 | private final DataType stateType; |
| 42 | 41 | private final TypeCodec<Object> stateTypeCodec; |
| 43 | 42 | |
| 44 | - private AggregateMetadata(KeyspaceMetadata keyspace, String fullName, String simpleName, List<DataType> argumentTypes, | |
| 43 | + private AggregateMetadata(KeyspaceMetadata keyspace, String simpleName, List<DataType> argumentTypes, | |
| 45 | 44 | String finalFuncSimpleName, String finalFuncFullName, Object initCond, DataType returnType, |
| 46 | 45 | String stateFuncSimpleName, String stateFuncFullName, DataType stateType, TypeCodec<Object> stateTypeCodec) { |
| 47 | 46 | this.keyspace = keyspace; |
| 48 | - this.fullName = fullName; | |
| 49 | 47 | this.simpleName = simpleName; |
| 50 | 48 | this.argumentTypes = argumentTypes; |
| 51 | 49 | this.finalFuncSimpleName = finalFuncSimpleName; |
| @@ -89,7 +87,6 @@ | ||
| 89 | 87 | ProtocolVersion protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); |
| 90 | 88 | String simpleName = row.getString("aggregate_name"); |
| 91 | 89 | List<DataType> argumentTypes = parseTypes(ksm, row.getList("argument_types", String.class), version, cluster); |
| 92 | - String fullName = Metadata.fullFunctionName(simpleName, argumentTypes); | |
| 93 | 90 | String finalFuncSimpleName = row.getString("final_func"); |
| 94 | 91 | DataType returnType; |
| 95 | 92 | if(version.getMajor() >= 3) { |
| @@ -111,10 +108,10 @@ | ||
| 111 | 108 | initCond = rawInitCond == null ? null : codecRegistry.codecFor(stateType).deserialize(rawInitCond, protocolVersion); |
| 112 | 109 | } |
| 113 | 110 | |
| 114 | - String finalFuncFullName = finalFuncSimpleName == null ? null : String.format("%s(%s)", finalFuncSimpleName, stateType); | |
| 111 | + String finalFuncFullName = finalFuncSimpleName == null ? null : Metadata.fullFunctionName(finalFuncSimpleName, Collections.singletonList(stateType)); | |
| 115 | 112 | String stateFuncFullName = makeStateFuncFullName(stateFuncSimpleName, stateType, argumentTypes); |
| 116 | 113 | |
| 117 | - return new AggregateMetadata(ksm, fullName, simpleName, argumentTypes, | |
| 114 | + return new AggregateMetadata(ksm, simpleName, argumentTypes, | |
| 118 | 115 | finalFuncSimpleName, finalFuncFullName, initCond, returnType, stateFuncSimpleName, |
| 119 | 116 | stateFuncFullName, stateType, codecRegistry.codecFor(stateType)); |
| 120 | 117 | } |
| @@ -173,37 +170,23 @@ | ||
| 173 | 170 | } |
| 174 | 171 | |
| 175 | 172 | private String asCQLQuery(boolean formatted) { |
| 176 | - //create aggregate test.prettysum(int) SFUNC plus STYPE int FINALFUNC announce INITCOND 0; | |
| 177 | 173 | |
| 178 | - StringBuilder sb = new StringBuilder(); | |
| 179 | - | |
| 180 | - sb | |
| 181 | - .append("CREATE AGGREGATE ") | |
| 174 | + StringBuilder sb = new StringBuilder("CREATE AGGREGATE ") | |
| 182 | 175 | .append(Metadata.escapeId(keyspace.getName())) |
| 183 | - .append('.') | |
| 184 | - .append(Metadata.escapeId(simpleName)) | |
| 185 | - .append('('); | |
| 176 | + .append('.'); | |
| 186 | 177 | |
| 187 | - boolean first = true; | |
| 188 | - for (DataType type : argumentTypes) { | |
| 189 | - if (first) | |
| 190 | - first = false; | |
| 191 | - else | |
| 192 | - sb.append(','); | |
| 193 | - sb.append(type); | |
| 194 | - } | |
| 195 | - sb.append(')'); | |
| 178 | + appendSignature(sb); | |
| 196 | 179 | |
| 197 | 180 | TableMetadata.spaceOrNewLine(sb, formatted) |
| 198 | 181 | .append("SFUNC ") |
| 199 | - .append(stateFuncSimpleName) | |
| 182 | + .append(Metadata.escapeId(stateFuncSimpleName)) | |
| 200 | 183 | .append(" STYPE ") |
| 201 | - .append(stateType); | |
| 184 | + .append(stateType.asFunctionParameterString()); | |
| 202 | 185 | |
| 203 | 186 | if (finalFuncSimpleName != null) |
| 204 | 187 | TableMetadata.spaceOrNewLine(sb, formatted) |
| 205 | 188 | .append("FINALFUNC ") |
| 206 | - .append(finalFuncSimpleName); | |
| 189 | + .append(Metadata.escapeId(finalFuncSimpleName)); | |
| 207 | 190 | |
| 208 | 191 | if (initCond != null) |
| 209 | 192 | TableMetadata.spaceOrNewLine(sb, formatted) |
| @@ -215,6 +198,21 @@ | ||
| 215 | 198 | return sb.toString(); |
| 216 | 199 | } |
| 217 | 200 | |
| 201 | + private void appendSignature(StringBuilder sb) { | |
| 202 | + sb | |
| 203 | + .append(Metadata.escapeId(simpleName)) | |
| 204 | + .append('('); | |
| 205 | + boolean first = true; | |
| 206 | + for (DataType type : argumentTypes) { | |
| 207 | + if (first) | |
| 208 | + first = false; | |
| 209 | + else | |
| 210 | + sb.append(','); | |
| 211 | + sb.append(type.asFunctionParameterString()); | |
| 212 | + } | |
| 213 | + sb.append(')'); | |
| 214 | + } | |
| 215 | + | |
| 218 | 216 | /** |
| 219 | 217 | * Returns the keyspace this aggregate belongs to. |
| 220 | 218 | * |
| @@ -225,15 +223,19 @@ | ||
| 225 | 223 | } |
| 226 | 224 | |
| 227 | 225 | /** |
| 228 | - * Returns the full name of this aggregate. | |
| 226 | + * Returns the CQL signature of this aggregate. | |
| 229 | 227 | * <p> |
| 230 | 228 | * This is the name of the aggregate, followed by the names of the argument types between parentheses, |
| 231 | 229 | * like it was specified in the {@code CREATE AGGREGATE...} statement, for example {@code sum(int)}. |
| 230 | + * <p> | |
| 231 | + * Note that the returned signature is not qualified with the keyspace name. | |
| 232 | 232 | * |
| 233 | - * @return the full name. | |
| 233 | + * @return the signature of this aggregate. | |
| 234 | 234 | */ |
| 235 | - public String getFullName() { | |
| 236 | - return fullName; | |
| 235 | + public String getSignature() { | |
| 236 | + StringBuilder sb = new StringBuilder(); | |
| 237 | + appendSignature(sb); | |
| 238 | + return sb.toString(); | |
| 237 | 239 | } |
| 238 | 240 | |
| 239 | 241 | /** |
| @@ -243,9 +245,9 @@ | ||
| 243 | 245 | * different argument lists, therefore the simple name may not be unique. For example, |
| 244 | 246 | * {@code sum(int)} and {@code sum(int,int)} both have the simple name {@code sum}. |
| 245 | 247 | * |
| 246 | - * @return the simple name. | |
| 248 | + * @return the simple name of this aggregate. | |
| 247 | 249 | * |
| 248 | - * @see #getFullName() | |
| 250 | + * @see #getSignature() | |
| 249 | 251 | */ |
| 250 | 252 | public String getSimpleName() { |
| 251 | 253 | return simpleName; |
| @@ -332,7 +334,6 @@ | ||
| 332 | 334 | if (other instanceof AggregateMetadata) { |
| 333 | 335 | AggregateMetadata that = (AggregateMetadata)other; |
| 334 | 336 | return this.keyspace.getName().equals(that.keyspace.getName()) && |
| 335 | - this.fullName.equals(that.fullName) && | |
| 336 | 337 | this.argumentTypes.equals(that.argumentTypes) && |
| 337 | 338 | Objects.equal(this.finalFuncFullName, that.finalFuncFullName) && |
| 338 | 339 | // Note: this might be a problem if a custom codec has been registered for the initCond's type, with a target Java type that |
| @@ -348,7 +349,7 @@ | ||
| 348 | 349 | |
| 349 | 350 | @Override |
| 350 | 351 | public int hashCode() { |
| 351 | - return Objects.hashCode(this.keyspace.getName(), this.fullName, this.argumentTypes, | |
| 352 | + return Objects.hashCode(this.keyspace.getName(), this.argumentTypes, | |
| 352 | 353 | this.finalFuncFullName, this.initCond, this.returnType, this.stateFuncFullName, this.stateType); |
| 353 | 354 | } |
| 354 | 355 | } |
modified driver-core/src/main/java/com/datastax/driver/core/DataType.java +29 −0
| @@ -583,6 +583,23 @@ | ||
| 583 | 583 | }); |
| 584 | 584 | } |
| 585 | 585 | |
| 586 | + /** | |
| 587 | + * Returns a String representation of this data type | |
| 588 | + * suitable for inclusion as a parameter type | |
| 589 | + * in a function or aggregate signature. | |
| 590 | + * <p> | |
| 591 | + * In such places, the String representation might vary | |
| 592 | + * from the canonical one as returned by {@link #toString()}; | |
| 593 | + * e.g. the {@code frozen} keyword is not accepted. | |
| 594 | + * | |
| 595 | + * @return a String representation of this data type | |
| 596 | + * suitable for inclusion as a parameter type | |
| 597 | + * in a function or aggregate signature. | |
| 598 | + */ | |
| 599 | + public String asFunctionParameterString() { | |
| 600 | + return toString(); | |
| 601 | + } | |
| 602 | + | |
| 586 | 603 | public static class NativeType extends DataType { |
| 587 | 604 | |
| 588 | 605 | private NativeType(DataType.Name name) { |
| @@ -662,6 +679,18 @@ | ||
| 662 | 679 | return String.format(template, name, typeArguments.get(0)); |
| 663 | 680 | } |
| 664 | 681 | } |
| 682 | + | |
| 683 | + @Override | |
| 684 | + public String asFunctionParameterString() { | |
| 685 | + if (name == Name.MAP) { | |
| 686 | + String template = "%s<%s, %s>"; | |
| 687 | + return String.format(template, name, typeArguments.get(0), typeArguments.get(1)); | |
| 688 | + } | |
| 689 | + else { | |
| 690 | + String template = "%s<%s>"; | |
| 691 | + return String.format(template, name, typeArguments.get(0)); | |
| 692 | + } | |
| 693 | + } | |
| 665 | 694 | } |
| 666 | 695 | |
| 667 | 696 | public static class CustomType extends DataType { |
modified driver-core/src/main/java/com/datastax/driver/core/FunctionMetadata.java +26 −19
| @@ -32,7 +32,6 @@ | ||
| 32 | 32 | private static final Logger logger = LoggerFactory.getLogger(FunctionMetadata.class); |
| 33 | 33 | |
| 34 | 34 | private final KeyspaceMetadata keyspace; |
| 35 | - private final String fullName; | |
| 36 | 35 | private final String simpleName; |
| 37 | 36 | private final Map<String, DataType> arguments; |
| 38 | 37 | private final String body; |
| @@ -41,7 +40,6 @@ | ||
| 41 | 40 | private final DataType returnType; |
| 42 | 41 | |
| 43 | 42 | private FunctionMetadata(KeyspaceMetadata keyspace, |
| 44 | - String fullName, | |
| 45 | 43 | String simpleName, |
| 46 | 44 | Map<String, DataType> arguments, |
| 47 | 45 | String body, |
| @@ -49,7 +47,6 @@ | ||
| 49 | 47 | String language, |
| 50 | 48 | DataType returnType) { |
| 51 | 49 | this.keyspace = keyspace; |
| 52 | - this.fullName = fullName; | |
| 53 | 50 | this.simpleName = simpleName; |
| 54 | 51 | this.arguments = arguments; |
| 55 | 52 | this.body = body; |
| @@ -94,8 +91,8 @@ | ||
| 94 | 91 | // this will be a list of C* types in 2.2 and a list of CQL types in 3.0 |
| 95 | 92 | List<String> argumentTypes = row.getList("argument_types", String.class); |
| 96 | 93 | Map<String, DataType> arguments = buildArguments(ksm, argumentNames, argumentTypes, version, cluster); |
| 97 | - String fullName = Metadata.fullFunctionName(simpleName, arguments.values()); | |
| 98 | 94 | if (argumentNames.size() != argumentTypes.size()) { |
| 95 | + String fullName = Metadata.fullFunctionName(simpleName, arguments.values()); | |
| 99 | 96 | logger.error(String.format("Error parsing definition of function %1$s.%2$s: the number of argument names and types don't match." |
| 100 | 97 | + "Cluster.getMetadata().getKeyspace(\"%1$s\").getFunction(\"%2$s\") will be missing.", |
| 101 | 98 | ksm.getName(), fullName)); |
| @@ -110,8 +107,7 @@ | ||
| 110 | 107 | } else { |
| 111 | 108 | returnType = DataTypeClassNameParser.parseOne(row.getString("return_type"), protocolVersion, codecRegistry); |
| 112 | 109 | } |
| 113 | - return new FunctionMetadata(ksm, fullName, simpleName, arguments, body, | |
| 114 | - calledOnNullInput, language, returnType); | |
| 110 | + return new FunctionMetadata(ksm, simpleName, arguments, body, calledOnNullInput, language, returnType); | |
| 115 | 111 | } |
| 116 | 112 | |
| 117 | 113 | // Note: the caller ensures that names and types have the same size |
| @@ -119,7 +115,6 @@ | ||
| 119 | 115 | if (names.isEmpty()) |
| 120 | 116 | return Collections.emptyMap(); |
| 121 | 117 | ImmutableMap.Builder<String, DataType> builder = ImmutableMap.builder(); |
| 122 | - Metadata metadata = cluster.getMetadata(); | |
| 123 | 118 | CodecRegistry codecRegistry = cluster.getConfiguration().getCodecRegistry(); |
| 124 | 119 | ProtocolVersion protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); |
| 125 | 120 | Iterator<String> iterTypes = types.iterator(); |
| @@ -164,12 +159,10 @@ | ||
| 164 | 159 | } |
| 165 | 160 | |
| 166 | 161 | private String asCQLQuery(boolean formatted) { |
| 167 | - // create function test.sum(a int, b int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java as 'return a+b;'; | |
| 168 | 162 | |
| 169 | - StringBuilder sb = new StringBuilder(); | |
| 163 | + StringBuilder sb = new StringBuilder("CREATE FUNCTION "); | |
| 170 | 164 | |
| 171 | 165 | sb |
| 172 | - .append("CREATE FUNCTION ") | |
| 173 | 166 | .append(Metadata.escapeId(keyspace.getName())) |
| 174 | 167 | .append('.') |
| 175 | 168 | .append(Metadata.escapeId(simpleName)) |
| @@ -188,7 +181,7 @@ | ||
| 188 | 181 | .append(TableMetadata.spaces(4, formatted)) |
| 189 | 182 | .append(Metadata.escapeId(name)) |
| 190 | 183 | .append(' ') |
| 191 | - .append(type); | |
| 184 | + .append(type.asFunctionParameterString()); | |
| 192 | 185 | } |
| 193 | 186 | sb.append(')'); |
| 194 | 187 | |
| @@ -221,15 +214,30 @@ | ||
| 221 | 214 | } |
| 222 | 215 | |
| 223 | 216 | /** |
| 224 | - * Returns the full name of this function. | |
| 217 | + * Returns the CQL signature of this function. | |
| 225 | 218 | * <p> |
| 226 | 219 | * This is the name of the function, followed by the names of the argument types between parentheses, |
| 227 | 220 | * for example {@code sum(int,int)}. |
| 221 | + * <p> | |
| 222 | + * Note that the returned signature is not qualified with the keyspace name. | |
| 228 | 223 | * |
| 229 | - * @return the full name. | |
| 224 | + * @return the signature of this function. | |
| 230 | 225 | */ |
| 231 | - public String getFullName() { | |
| 232 | - return fullName; | |
| 226 | + public String getSignature() { | |
| 227 | + StringBuilder sb = new StringBuilder(); | |
| 228 | + sb | |
| 229 | + .append(Metadata.escapeId(simpleName)) | |
| 230 | + .append('('); | |
| 231 | + boolean first = true; | |
| 232 | + for (DataType type : arguments.values()) { | |
| 233 | + if (first) | |
| 234 | + first = false; | |
| 235 | + else | |
| 236 | + sb.append(','); | |
| 237 | + sb.append(type.asFunctionParameterString()); | |
| 238 | + } | |
| 239 | + sb.append(')'); | |
| 240 | + return sb.toString(); | |
| 233 | 241 | } |
| 234 | 242 | |
| 235 | 243 | /** |
| @@ -239,9 +247,9 @@ | ||
| 239 | 247 | * different argument lists, therefore the simple name may not be unique. For example, |
| 240 | 248 | * {@code sum(int,int)} and {@code sum(int,int,int)} both have the simple name {@code sum}. |
| 241 | 249 | * |
| 242 | - * @return the simple name. | |
| 250 | + * @return the simple name of this function. | |
| 243 | 251 | * |
| 244 | - * @see #getFullName() | |
| 252 | + * @see #getSignature() | |
| 245 | 253 | */ |
| 246 | 254 | public String getSimpleName() { |
| 247 | 255 | return simpleName; |
| @@ -303,7 +311,6 @@ | ||
| 303 | 311 | if (other instanceof FunctionMetadata) { |
| 304 | 312 | FunctionMetadata that = (FunctionMetadata)other; |
| 305 | 313 | return this.keyspace.getName().equals(that.keyspace.getName()) && |
| 306 | - this.fullName.equals(that.fullName) && | |
| 307 | 314 | this.arguments.equals(that.arguments) && |
| 308 | 315 | this.body.equals(that.body) && |
| 309 | 316 | this.calledOnNullInput == that.calledOnNullInput && |
| @@ -315,6 +322,6 @@ | ||
| 315 | 322 | |
| 316 | 323 | @Override |
| 317 | 324 | public int hashCode() { |
| 318 | - return Objects.hashCode(keyspace.getName(), fullName, arguments, body, calledOnNullInput, language, returnType); | |
| 325 | + return Objects.hashCode(keyspace.getName(), arguments, body, calledOnNullInput, language, returnType); | |
| 319 | 326 | } |
| 320 | 327 | } |
modified driver-core/src/main/java/com/datastax/driver/core/KeyspaceMetadata.java +4 −2
| @@ -353,11 +353,13 @@ | ||
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | void add(FunctionMetadata function) { |
| 356 | - functions.put(function.getFullName(), function); | |
| 356 | + String functionName = Metadata.fullFunctionName(function.getSimpleName(), function.getArguments().values()); | |
| 357 | + functions.put(functionName, function); | |
| 357 | 358 | } |
| 358 | 359 | |
| 359 | 360 | void add(AggregateMetadata aggregate) { |
| 360 | - aggregates.put(aggregate.getFullName(), aggregate); | |
| 361 | + String aggregateName = Metadata.fullFunctionName(aggregate.getSimpleName(), aggregate.getArgumentTypes()); | |
| 362 | + aggregates.put(aggregateName, aggregate); | |
| 361 | 363 | } |
| 362 | 364 | |
| 363 | 365 | void add(UserType type) { |
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +37 −7
| @@ -134,15 +134,45 @@ | ||
| 134 | 134 | return lowercaseAlphanumeric.matcher(ident).matches() ? ident : quote(ident); |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | - // Builds the internal name of a function/aggregate | |
| 138 | - // Note that if simpleName comes from the user, the caller must call handleId on it before passing it to this method | |
| 137 | + /** | |
| 138 | + * Builds the internal name of a function/aggregate, which is similar, but not identical, | |
| 139 | + * to the function/aggregate signature. | |
| 140 | + * This is only used to generate keys for internal metadata maps (KeyspaceMetadata.functions and. | |
| 141 | + * KeyspaceMetadata.aggregates). | |
| 142 | + * Note that if simpleName comes from the user, the caller must call handleId on it before passing it to this method. | |
| 143 | + * Note that this method does not necessarily generates a valid CQL function signature. | |
| 144 | + * Note that argumentTypes can be either a list of strings (schema change events) | |
| 145 | + * or a list of DataTypes (function lookup from client code). | |
| 146 | + * This method must ensure that both cases produce the same identifier. | |
| 147 | + */ | |
| 139 | 148 | static String fullFunctionName(String simpleName, Collection<?> argumentTypes) { |
| 140 | - return String.format("%s(%s)", | |
| 141 | - simpleName, COMMAS.join(argumentTypes)); | |
| 149 | + StringBuilder sb = new StringBuilder(simpleName); | |
| 150 | + sb.append('('); | |
| 151 | + boolean first = true; | |
| 152 | + for (Object argumentType : argumentTypes) { | |
| 153 | + if (first) | |
| 154 | + first = false; | |
| 155 | + else | |
| 156 | + sb.append(','); | |
| 157 | + // user types must be represented by their names only, | |
| 158 | + // without keyspace prefix, because that's how | |
| 159 | + // they appear in a schema change event (in targetSignature) | |
| 160 | + if (argumentType instanceof UserType) { | |
| 161 | + UserType userType = (UserType)argumentType; | |
| 162 | + String typeName = Metadata.escapeId(userType.getTypeName()); | |
| 163 | + if (userType.isFrozen()) | |
| 164 | + sb.append("frozen<"); | |
| 165 | + sb.append(typeName); | |
| 166 | + if (userType.isFrozen()) | |
| 167 | + sb.append(">"); | |
| 168 | + } else { | |
| 169 | + sb.append(argumentType); | |
| 170 | + } | |
| 171 | + } | |
| 172 | + sb.append(')'); | |
| 173 | + return sb.toString(); | |
| 142 | 174 | } |
| 143 | 175 | |
| 144 | - static final Joiner COMMAS = Joiner.on(","); | |
| 145 | - | |
| 146 | 176 | /** |
| 147 | 177 | * Quote a keyspace, table or column identifier to make it case sensitive. |
| 148 | 178 | * <p> |
| @@ -160,7 +190,7 @@ | ||
| 160 | 190 | * or even {@link Cluster#connect(String)}. |
| 161 | 191 | */ |
| 162 | 192 | public static String quote(String id) { |
| 163 | - return '"' + id + '"'; | |
| 193 | + return '"' + id.replace("\"", "\"\"") + '"'; | |
| 164 | 194 | } |
| 165 | 195 | |
| 166 | 196 | /** |
modified driver-core/src/main/java/com/datastax/driver/core/SchemaParser.java +16 −10
| @@ -205,8 +205,10 @@ | ||
| 205 | 205 | if (functionRows != null) { |
| 206 | 206 | for (Row functionRow : functionRows) { |
| 207 | 207 | FunctionMetadata function = FunctionMetadata.build(keyspace, functionRow, cassandraVersion, cluster); |
| 208 | - if (function != null) | |
| 209 | - functions.put(function.getFullName(), function); | |
| 208 | + if (function != null) { | |
| 209 | + String name = Metadata.fullFunctionName(function.getSimpleName(), function.getArguments().values()); | |
| 210 | + functions.put(name, function); | |
| 211 | + } | |
| 210 | 212 | } |
| 211 | 213 | } |
| 212 | 214 | return functions; |
| @@ -217,8 +219,10 @@ | ||
| 217 | 219 | if (aggregateRows != null) { |
| 218 | 220 | for (Row aggregateRow : aggregateRows) { |
| 219 | 221 | AggregateMetadata aggregate = AggregateMetadata.build(keyspace, aggregateRow, cassandraVersion, cluster); |
| 220 | - if (aggregate != null) | |
| 221 | - aggregates.put(aggregate.getFullName(), aggregate); | |
| 222 | + if (aggregate != null) { | |
| 223 | + String name = Metadata.fullFunctionName(aggregate.getSimpleName(), aggregate.getArgumentTypes()); | |
| 224 | + aggregates.put(name, aggregate); | |
| 225 | + } | |
| 222 | 226 | } |
| 223 | 227 | } |
| 224 | 228 | return aggregates; |
| @@ -328,14 +332,15 @@ | ||
| 328 | 332 | Iterator<FunctionMetadata> it = oldFunctions.values().iterator(); |
| 329 | 333 | while (it.hasNext()) { |
| 330 | 334 | FunctionMetadata oldFunction = it.next(); |
| 331 | - String functionName = oldFunction.getFullName(); | |
| 332 | - if ((functionToRebuild == null || functionToRebuild.equals(functionName)) && !newFunctions.containsKey(functionName)) { | |
| 335 | + String oldFunctionName = Metadata.fullFunctionName(oldFunction.getSimpleName(), oldFunction.getArguments().values()); | |
| 336 | + if ((functionToRebuild == null || functionToRebuild.equals(oldFunctionName)) && !newFunctions.containsKey(oldFunctionName)) { | |
| 333 | 337 | it.remove(); |
| 334 | 338 | metadata.triggerOnFunctionRemoved(oldFunction); |
| 335 | 339 | } |
| 336 | 340 | } |
| 337 | 341 | for (FunctionMetadata newFunction : newFunctions.values()) { |
| 338 | - FunctionMetadata oldFunction = oldFunctions.put(newFunction.getFullName(), newFunction); | |
| 342 | + String newFunctionName = Metadata.fullFunctionName(newFunction.getSimpleName(), newFunction.getArguments().values()); | |
| 343 | + FunctionMetadata oldFunction = oldFunctions.put(newFunctionName, newFunction); | |
| 339 | 344 | if (oldFunction == null) { |
| 340 | 345 | metadata.triggerOnFunctionAdded(newFunction); |
| 341 | 346 | } else if (!newFunction.equals(oldFunction)) { |
| @@ -348,14 +353,15 @@ | ||
| 348 | 353 | Iterator<AggregateMetadata> it = oldAggregates.values().iterator(); |
| 349 | 354 | while (it.hasNext()) { |
| 350 | 355 | AggregateMetadata oldAggregate = it.next(); |
| 351 | - String aggregateName = oldAggregate.getFullName(); | |
| 352 | - if ((aggregateToRebuild == null || aggregateToRebuild.equals(aggregateName)) && !newAggregates.containsKey(aggregateName)) { | |
| 356 | + String oldAggregateName = Metadata.fullFunctionName(oldAggregate.getSimpleName(), oldAggregate.getArgumentTypes()); | |
| 357 | + if ((aggregateToRebuild == null || aggregateToRebuild.equals(oldAggregateName)) && !newAggregates.containsKey(oldAggregateName)) { | |
| 353 | 358 | it.remove(); |
| 354 | 359 | metadata.triggerOnAggregateRemoved(oldAggregate); |
| 355 | 360 | } |
| 356 | 361 | } |
| 357 | 362 | for (AggregateMetadata newAggregate : newAggregates.values()) { |
| 358 | - AggregateMetadata oldAggregate = oldAggregates.put(newAggregate.getFullName(), newAggregate); | |
| 363 | + String newAggregateName = Metadata.fullFunctionName(newAggregate.getSimpleName(), newAggregate.getArgumentTypes()); | |
| 364 | + AggregateMetadata oldAggregate = oldAggregates.put(newAggregateName, newAggregate); | |
| 359 | 365 | if (oldAggregate == null) { |
| 360 | 366 | metadata.triggerOnAggregateAdded(newAggregate); |
| 361 | 367 | } else if (!newAggregate.equals(oldAggregate)) { |
modified driver-core/src/main/java/com/datastax/driver/core/TupleType.java +7 −3
| @@ -185,12 +185,16 @@ | ||
| 185 | 185 | |
| 186 | 186 | @Override |
| 187 | 187 | public String toString() { |
| 188 | + return "frozen<" + asFunctionParameterString() + ">"; | |
| 189 | + } | |
| 190 | + | |
| 191 | + @Override | |
| 192 | + public String asFunctionParameterString() { | |
| 188 | 193 | StringBuilder sb = new StringBuilder(); |
| 189 | 194 | for (DataType type : types) { |
| 190 | - sb.append(sb.length() == 0 ? "frozen<tuple<" : ", "); | |
| 195 | + sb.append(sb.length() == 0 ? "tuple<" : ", "); | |
| 191 | 196 | sb.append(type); |
| 192 | 197 | } |
| 193 | - return sb.append(">>").toString(); | |
| 198 | + return sb.append(">").toString(); | |
| 194 | 199 | } |
| 195 | - | |
| 196 | 200 | } |
modified driver-core/src/main/java/com/datastax/driver/core/UserType.java +5 −0
| @@ -273,6 +273,11 @@ | ||
| 273 | 273 | return "frozen<" + Metadata.escapeId(getKeyspace()) + '.' + Metadata.escapeId(getTypeName()) + ">"; |
| 274 | 274 | } |
| 275 | 275 | |
| 276 | + @Override | |
| 277 | + public String asFunctionParameterString() { | |
| 278 | + return Metadata.escapeId(getTypeName()); | |
| 279 | + } | |
| 280 | + | |
| 276 | 281 | /** |
| 277 | 282 | * A UDT field. |
| 278 | 283 | */ |
modified driver-core/src/test/java/com/datastax/driver/core/AggregateMetadataAssert.java +2 −2
| @@ -24,8 +24,8 @@ | ||
| 24 | 24 | super(actual, AggregateMetadataAssert.class); |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | - public AggregateMetadataAssert hasFullName(String name) { | |
| 28 | - assertThat(actual.getFullName()).isEqualTo(name); | |
| 27 | + public AggregateMetadataAssert hasSignature(String name) { | |
| 28 | + assertThat(actual.getSignature()).isEqualTo(name); | |
| 29 | 29 | return this; |
| 30 | 30 | } |
| 31 | 31 |
modified driver-core/src/test/java/com/datastax/driver/core/AggregateMetadataTest.java +50 −6
| @@ -16,10 +16,11 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import java.util.Collection; |
| 19 | -import java.util.Collections; | |
| 20 | 19 | |
| 21 | 20 | import org.testng.annotations.Test; |
| 22 | 21 | |
| 22 | +import static com.google.common.collect.Lists.newArrayList; | |
| 23 | + | |
| 23 | 24 | import com.datastax.driver.core.utils.CassandraVersion; |
| 24 | 25 | |
| 25 | 26 | import static com.datastax.driver.core.Assertions.assertThat; |
| @@ -42,7 +43,7 @@ | ||
| 42 | 43 | FunctionMetadata stateFunc = keyspace.getFunction("cat", text(), cint()); |
| 43 | 44 | AggregateMetadata aggregate = keyspace.getAggregate("cat_tos", cint()); |
| 44 | 45 | assertThat(aggregate).isNotNull(); |
| 45 | - assertThat(aggregate.getFullName()).isEqualTo("cat_tos(int)"); | |
| 46 | + assertThat(aggregate.getSignature()).isEqualTo("cat_tos(int)"); | |
| 46 | 47 | assertThat(aggregate.getSimpleName()).isEqualTo("cat_tos"); |
| 47 | 48 | assertThat(aggregate.getArgumentTypes()).containsExactly(cint()); |
| 48 | 49 | assertThat(aggregate.getFinalFunc()).isNull(); |
| @@ -69,7 +70,7 @@ | ||
| 69 | 70 | FunctionMetadata stateFunc = keyspace.getFunction("inc", cint()); |
| 70 | 71 | AggregateMetadata aggregate = keyspace.getAggregate("mycount"); |
| 71 | 72 | assertThat(aggregate).isNotNull(); |
| 72 | - assertThat(aggregate.getFullName()).isEqualTo("mycount()"); | |
| 73 | + assertThat(aggregate.getSignature()).isEqualTo("mycount()"); | |
| 73 | 74 | assertThat(aggregate.getSimpleName()).isEqualTo("mycount"); |
| 74 | 75 | assertThat(aggregate.getArgumentTypes()).isEmpty(); |
| 75 | 76 | assertThat(aggregate.getFinalFunc()).isNull(); |
| @@ -99,7 +100,7 @@ | ||
| 99 | 100 | FunctionMetadata finalFunc = keyspace.getFunction("announce", cint()); |
| 100 | 101 | AggregateMetadata aggregate = keyspace.getAggregate("prettysum", cint()); |
| 101 | 102 | assertThat(aggregate).isNotNull(); |
| 102 | - assertThat(aggregate.getFullName()).isEqualTo("prettysum(int)"); | |
| 103 | + assertThat(aggregate.getSignature()).isEqualTo("prettysum(int)"); | |
| 103 | 104 | assertThat(aggregate.getSimpleName()).isEqualTo("prettysum"); |
| 104 | 105 | assertThat(aggregate.getArgumentTypes()).containsExactly(cint()); |
| 105 | 106 | assertThat(aggregate.getFinalFunc()).isEqualTo(finalFunc); |
| @@ -127,7 +128,7 @@ | ||
| 127 | 128 | FunctionMetadata stateFunc = keyspace.getFunction("plus2", cint(), cint()); |
| 128 | 129 | AggregateMetadata aggregate = keyspace.getAggregate("sum", cint()); |
| 129 | 130 | assertThat(aggregate).isNotNull(); |
| 130 | - assertThat(aggregate.getFullName()).isEqualTo("sum(int)"); | |
| 131 | + assertThat(aggregate.getSignature()).isEqualTo("sum(int)"); | |
| 131 | 132 | assertThat(aggregate.getSimpleName()).isEqualTo("sum"); |
| 132 | 133 | assertThat(aggregate.getArgumentTypes()).containsExactly(cint()); |
| 133 | 134 | assertThat(aggregate.getFinalFunc()).isNull(); |
| @@ -140,9 +141,52 @@ | ||
| 140 | 141 | + "SFUNC plus2 STYPE int;", this.keyspace)); |
| 141 | 142 | } |
| 142 | 143 | |
| 144 | + @Test(groups = "short") | |
| 145 | + public void should_parse_and_format_aggregate_with_udts() { | |
| 146 | + // given | |
| 147 | + String cqlFunction = String.format( | |
| 148 | + "CREATE FUNCTION %s.\"MY_FUNC\"(address1 \"Address\", address2 \"Address\") " | |
| 149 | + + "CALLED ON NULL INPUT " | |
| 150 | + + "RETURNS \"Address\" " | |
| 151 | + + "LANGUAGE java " | |
| 152 | + + "AS 'return address1;'", keyspace); | |
| 153 | + String cqlAggregate = String.format( | |
| 154 | + "CREATE AGGREGATE %s.\"MY_AGGREGATE\"(\"Address\") " | |
| 155 | + + "SFUNC \"MY_FUNC\" " | |
| 156 | + + "STYPE \"Address\";", | |
| 157 | + keyspace); | |
| 158 | + // when | |
| 159 | + session.execute(cqlFunction); | |
| 160 | + session.execute(cqlAggregate); | |
| 161 | + // then | |
| 162 | + KeyspaceMetadata keyspace = cluster.getMetadata().getKeyspace(this.keyspace); | |
| 163 | + UserType addressType = keyspace.getUserType("\"Address\""); | |
| 164 | + FunctionMetadata stateFunc = keyspace.getFunction("\"MY_FUNC\"", addressType, addressType); | |
| 165 | + AggregateMetadata aggregate = keyspace.getAggregate("\"MY_AGGREGATE\"", addressType); | |
| 166 | + assertThat(aggregate).isNotNull(); | |
| 167 | + assertThat(aggregate.getSignature()).isEqualTo("\"MY_AGGREGATE\"(\"Address\")"); | |
| 168 | + assertThat(aggregate.getSimpleName()).isEqualTo("MY_AGGREGATE"); | |
| 169 | + assertThat(aggregate.getArgumentTypes()).containsExactly(addressType); | |
| 170 | + assertThat(aggregate.getFinalFunc()).isNull(); | |
| 171 | + assertThat(aggregate.getInitCond()).isNull(); | |
| 172 | + assertThat(aggregate.getReturnType()).isEqualTo(addressType); | |
| 173 | + assertThat(aggregate.getStateFunc()).isEqualTo(stateFunc); | |
| 174 | + assertThat(aggregate.getStateType()).isEqualTo(addressType); | |
| 175 | + assertThat(aggregate.toString()).isEqualTo(cqlAggregate); | |
| 176 | + } | |
| 177 | + | |
| 143 | 178 | @Override |
| 144 | 179 | protected Collection<String> getTableDefinitions() { |
| 145 | - return Collections.emptyList(); | |
| 180 | + return newArrayList( | |
| 181 | + String.format("CREATE TYPE IF NOT EXISTS %s.phone (number text)", keyspace), | |
| 182 | + String.format("CREATE TYPE IF NOT EXISTS %s.\"Address\" (" | |
| 183 | + + " street text," | |
| 184 | + + " city text," | |
| 185 | + + " zip int," | |
| 186 | + + " phones frozen<set<frozen<phone>>>," | |
| 187 | + + " location frozen<tuple<float, float>>" | |
| 188 | + + ")", keyspace) | |
| 189 | + ); | |
| 146 | 190 | } |
| 147 | 191 | |
| 148 | 192 | } |
modified driver-core/src/test/java/com/datastax/driver/core/DataTypeCqlNameParserTest.java +3 −2
| @@ -25,6 +25,7 @@ | ||
| 25 | 25 | import static com.datastax.driver.core.Assertions.assertThat; |
| 26 | 26 | import static com.datastax.driver.core.DataType.*; |
| 27 | 27 | import static com.datastax.driver.core.DataTypeCqlNameParser.parse; |
| 28 | +import static com.datastax.driver.core.Metadata.quote; | |
| 28 | 29 | |
| 29 | 30 | @CassandraVersion(major = 3.0) |
| 30 | 31 | public class DataTypeCqlNameParserTest extends CCMBridge.PerClassSingleNodeCluster { |
| @@ -88,8 +89,8 @@ | ||
| 88 | 89 | assertThat(parse("list<list<int>>", cluster, null, null, null, false, false)).isEqualTo(list(list(cint()))); |
| 89 | 90 | assertThat(parse("set<list<frozen<map<bigint,varchar>>>>", cluster, null, null, null, false, false)).isEqualTo(set(list(map(bigint(), varchar(), true)))); |
| 90 | 91 | |
| 91 | - UserType keyType = keyspaceMetadata.getUserType("Incr,edibly\" EvilTy<>><<><p\"e"); | |
| 92 | - UserType valueType = keyspaceMetadata.getUserType("\"A\""); | |
| 92 | + UserType keyType = keyspaceMetadata.getUserType(quote("Incr,edibly\" EvilTy<>><<><p\"e")); | |
| 93 | + UserType valueType = keyspaceMetadata.getUserType(quote("A")); | |
| 93 | 94 | assertThat(parse("map<frozen<\"Incr,edibly\"\" EvilTy<>><<><p\"\"e\">,frozen<\"A\">>", cluster, keyspace, keyspaceMetadata.userTypes, null, false, false)) |
| 94 | 95 | .isEqualTo(map(keyType, valueType, false)); |
| 95 | 96 | } |
modified driver-core/src/test/java/com/datastax/driver/core/FunctionMetadataAssert.java +2 −2
| @@ -24,8 +24,8 @@ | ||
| 24 | 24 | super(actual, FunctionMetadataAssert.class); |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | - public FunctionMetadataAssert hasFullName(String name) { | |
| 28 | - assertThat(actual.getFullName()).isEqualTo(name); | |
| 27 | + public FunctionMetadataAssert hasSignature(String name) { | |
| 28 | + assertThat(actual.getSignature()).isEqualTo(name); | |
| 29 | 29 | return this; |
| 30 | 30 | } |
| 31 | 31 |
modified driver-core/src/test/java/com/datastax/driver/core/FunctionMetadataTest.java +52 −5
| @@ -16,10 +16,12 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import java.util.Collection; |
| 19 | -import java.util.Collections; | |
| 20 | 19 | |
| 21 | 20 | import org.testng.annotations.Test; |
| 22 | 21 | |
| 22 | +import static com.google.common.collect.Lists.newArrayList; | |
| 23 | +import static org.assertj.core.api.Assertions.entry; | |
| 24 | + | |
| 23 | 25 | import com.datastax.driver.core.utils.CassandraVersion; |
| 24 | 26 | |
| 25 | 27 | import static com.datastax.driver.core.Assertions.assertThat; |
| @@ -39,7 +41,7 @@ | ||
| 39 | 41 | FunctionMetadata function = keyspace.getFunction("plus", cint(), cint()); |
| 40 | 42 | assertThat(function).isNotNull(); |
| 41 | 43 | assertThat(function.getKeyspace()).isEqualTo(keyspace); |
| 42 | - assertThat(function.getFullName()).isEqualTo("plus(int,int)"); | |
| 44 | + assertThat(function.getSignature()).isEqualTo("plus(int,int)"); | |
| 43 | 45 | assertThat(function.getSimpleName()).isEqualTo("plus"); |
| 44 | 46 | assertThat(function.getReturnType()).isEqualTo(cint()); |
| 45 | 47 | assertThat(function.getArguments()) |
| @@ -71,14 +73,13 @@ | ||
| 71 | 73 | FunctionMetadata function = keyspace.getFunction("pi"); |
| 72 | 74 | assertThat(function).isNotNull(); |
| 73 | 75 | assertThat(function.getKeyspace()).isEqualTo(keyspace); |
| 74 | - assertThat(function.getFullName()).isEqualTo("pi()"); | |
| 76 | + assertThat(function.getSignature()).isEqualTo("pi()"); | |
| 75 | 77 | assertThat(function.getSimpleName()).isEqualTo("pi"); |
| 76 | 78 | assertThat(function.getReturnType()).isEqualTo(DataType.cdouble()); |
| 77 | 79 | assertThat(function.getArguments()).isEmpty(); |
| 78 | 80 | assertThat(function.getLanguage()).isEqualTo("java"); |
| 79 | 81 | assertThat(function.getBody()).isEqualTo("return Math.PI;"); |
| 80 | 82 | assertThat(function.isCalledOnNullInput()).isTrue(); |
| 81 | - assertThat(keyspace.getFunction("pi")).isEqualTo(function); | |
| 82 | 83 | assertThat(function.toString()) |
| 83 | 84 | .isEqualTo(cql); |
| 84 | 85 | assertThat(function.exportAsString()) |
| @@ -89,9 +90,55 @@ | ||
| 89 | 90 | + "AS 'return Math.PI;';", this.keyspace)); |
| 90 | 91 | } |
| 91 | 92 | |
| 93 | + @Test(groups = "short") | |
| 94 | + public void should_parse_and_format_function_with_udts() { | |
| 95 | + // given | |
| 96 | + String body = | |
| 97 | + "//If \"called on null input\", handle nulls\n" | |
| 98 | + + "if(ADDRESS == null) return previous_total + 0;\n" | |
| 99 | + + "//User types are converted to com.datastax.driver.core.UDTValue types\n" | |
| 100 | + + "java.util.Set phones = ADDRESS.getSet(\"phones\", com.datastax.driver.core.UDTValue.class);\n" | |
| 101 | + + "return previous_total + phones.size();\n"; | |
| 102 | + String cqlFunction = String.format( | |
| 103 | + "CREATE FUNCTION %s.\"NUM_PHONES_ACCU\"(previous_total int,\"ADDRESS\" \"Address\") " | |
| 104 | + + "CALLED ON NULL INPUT " | |
| 105 | + + "RETURNS int " | |
| 106 | + + "LANGUAGE java " | |
| 107 | + + "AS " | |
| 108 | + + "'" | |
| 109 | + + body | |
| 110 | + + "';", keyspace); | |
| 111 | + // when | |
| 112 | + session.execute(cqlFunction); | |
| 113 | + // then | |
| 114 | + KeyspaceMetadata keyspace = cluster.getMetadata().getKeyspace(this.keyspace); | |
| 115 | + UserType addressType = keyspace.getUserType("\"Address\""); | |
| 116 | + FunctionMetadata function = keyspace.getFunction("\"NUM_PHONES_ACCU\"", cint(), addressType); | |
| 117 | + assertThat(function).isNotNull(); | |
| 118 | + assertThat(function.getKeyspace()).isEqualTo(keyspace); | |
| 119 | + | |
| 120 | + assertThat(function.getSignature()).isEqualTo("\"NUM_PHONES_ACCU\"(int,\"Address\")"); | |
| 121 | + assertThat(function.getSimpleName()).isEqualTo("NUM_PHONES_ACCU"); | |
| 122 | + assertThat(function.getReturnType()).isEqualTo(cint()); | |
| 123 | + assertThat(function.getArguments()).containsExactly(entry("previous_total", cint()), entry("ADDRESS", addressType)); | |
| 124 | + assertThat(function.getLanguage()).isEqualTo("java"); | |
| 125 | + assertThat(function.getBody()).isEqualTo(body); | |
| 126 | + assertThat(function.isCalledOnNullInput()).isTrue(); | |
| 127 | + assertThat(function.toString()).isEqualTo(cqlFunction); | |
| 128 | + } | |
| 129 | + | |
| 92 | 130 | @Override |
| 93 | 131 | protected Collection<String> getTableDefinitions() { |
| 94 | - return Collections.emptyList(); | |
| 132 | + return newArrayList( | |
| 133 | + String.format("CREATE TYPE IF NOT EXISTS %s.\"Phone\" (number text)", keyspace), | |
| 134 | + String.format("CREATE TYPE IF NOT EXISTS %s.\"Address\" (" | |
| 135 | + + " street text," | |
| 136 | + + " city text," | |
| 137 | + + " zip int," | |
| 138 | + + " phones frozen<set<frozen<\"Phone\">>>," | |
| 139 | + + " location frozen<tuple<float, float>>" | |
| 140 | + + ")", keyspace) | |
| 141 | + ); | |
| 95 | 142 | } |
| 96 | 143 | |
| 97 | 144 | } |
modified driver-core/src/test/java/com/datastax/driver/core/SchemaChangesTest.java +38 −37
| @@ -268,98 +268,99 @@ | ||
| 268 | 268 | @Test(groups = "short", dataProvider = "existingKeyspaceName") |
| 269 | 269 | @CassandraVersion(major = 2.2) |
| 270 | 270 | public void should_notify_of_function_creation(String keyspace) { |
| 271 | - session.execute(String.format("CREATE FUNCTION %s.id(i int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return i;'", keyspace)); | |
| 271 | + session.execute(String.format("CREATE FUNCTION %s.\"ID\"(i int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return i;'", keyspace)); | |
| 272 | 272 | |
| 273 | 273 | for (SchemaChangeListener listener : listeners) { |
| 274 | 274 | ArgumentCaptor<FunctionMetadata> added = ArgumentCaptor.forClass(FunctionMetadata.class); |
| 275 | 275 | verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onFunctionAdded(added.capture()); |
| 276 | 276 | assertThat(added.getValue()) |
| 277 | 277 | .isInKeyspace(handleId(keyspace)) |
| 278 | - .hasFullName("id(int)"); | |
| 278 | + .hasSignature("\"ID\"(int)"); | |
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | for (Metadata m : metadatas) |
| 282 | - assertThat(m.getKeyspace(keyspace).getFunction("id", DataType.cint())) | |
| 282 | + assertThat(m.getKeyspace(keyspace).getFunction("\"ID\"", DataType.cint())) | |
| 283 | 283 | .isNotNull(); |
| 284 | 284 | } |
| 285 | 285 | |
| 286 | 286 | @Test(groups = "short", dataProvider = "existingKeyspaceName") |
| 287 | 287 | @CassandraVersion(major = 2.2) |
| 288 | 288 | public void should_notify_of_function_update(String keyspace) { |
| 289 | - session.execute(String.format("CREATE FUNCTION %s.id(i int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java " | |
| 290 | - + "AS 'return i;'", keyspace)); | |
| 289 | + session.execute(String.format("CREATE TYPE IF NOT EXISTS %s.user (\"ID\" int, name text)", keyspace)); | |
| 290 | + session.execute(String.format("CREATE FUNCTION %s.\"ID\"(user user) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return user.getInt(\"ID\");'", keyspace)); | |
| 291 | 291 | |
| 292 | 292 | for (Metadata m : metadatas) |
| 293 | - assertThat(m.getKeyspace(keyspace).getFunction("id", DataType.cint()).getBody()) | |
| 294 | - .isEqualTo("return i;"); | |
| 293 | + assertThat(m.getKeyspace(keyspace).getFunction("\"ID\"", m.getKeyspace(keyspace).getUserType("user")).getBody()) | |
| 294 | + .isEqualTo("return user.getInt(\"ID\");"); | |
| 295 | 295 | |
| 296 | - session.execute(String.format("CREATE OR REPLACE FUNCTION %s.id(i int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java " | |
| 297 | - + "AS 'return i + 1;'", keyspace)); | |
| 296 | + session.execute(String.format("CREATE OR REPLACE FUNCTION %s.\"ID\"(user user) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java " | |
| 297 | + + "AS 'return 1 + user.getInt(\"ID\");'", keyspace)); | |
| 298 | 298 | |
| 299 | 299 | for (SchemaChangeListener listener : listeners) { |
| 300 | 300 | ArgumentCaptor<FunctionMetadata> current = ArgumentCaptor.forClass(FunctionMetadata.class); |
| 301 | 301 | ArgumentCaptor<FunctionMetadata> previous = ArgumentCaptor.forClass(FunctionMetadata.class); |
| 302 | 302 | verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onFunctionChanged(current.capture(), previous.capture()); |
| 303 | - assertThat(previous.getValue()).hasBody("return i;"); | |
| 304 | - assertThat(current.getValue()).hasBody("return i + 1;"); | |
| 303 | + assertThat(previous.getValue()).hasBody("return user.getInt(\"ID\");"); | |
| 304 | + assertThat(current.getValue()).hasBody("return 1 + user.getInt(\"ID\");"); | |
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | for (Metadata m : metadatas) |
| 308 | - assertThat(m.getKeyspace(keyspace).getFunction("id", DataType.cint()).getBody()) | |
| 309 | - .isEqualTo("return i + 1;"); | |
| 308 | + assertThat(m.getKeyspace(keyspace).getFunction("\"ID\"", m.getKeyspace(keyspace).getUserType("user")).getBody()) | |
| 309 | + .isEqualTo("return 1 + user.getInt(\"ID\");"); | |
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | @Test(groups = "short", dataProvider = "existingKeyspaceName") |
| 313 | 313 | @CassandraVersion(major = 2.2) |
| 314 | 314 | public void should_notify_of_function_drop(String keyspace) { |
| 315 | - session.execute(String.format("CREATE FUNCTION %s.id(i int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return i;'", keyspace)); | |
| 316 | - session.execute(String.format("DROP FUNCTION %s.id", keyspace)); | |
| 315 | + session.execute(String.format("CREATE TYPE IF NOT EXISTS %s.user (\"ID\" int, name text)", keyspace)); | |
| 316 | + session.execute(String.format("CREATE FUNCTION %s.\"ID\"(user user) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return user.getInt(\"ID\");'", keyspace)); | |
| 317 | + session.execute(String.format("DROP FUNCTION %s.\"ID\"", keyspace)); | |
| 317 | 318 | |
| 318 | 319 | for (SchemaChangeListener listener : listeners) { |
| 319 | 320 | ArgumentCaptor<FunctionMetadata> removed = ArgumentCaptor.forClass(FunctionMetadata.class); |
| 320 | 321 | verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onFunctionRemoved(removed.capture()); |
| 321 | 322 | assertThat(removed.getValue()) |
| 322 | 323 | .isInKeyspace(handleId(keyspace)) |
| 323 | - .hasFullName("id(int)"); | |
| 324 | + .hasSignature("\"ID\"(user)"); | |
| 324 | 325 | } |
| 325 | 326 | |
| 326 | 327 | for (Metadata m : metadatas) |
| 327 | - assertThat(m.getKeyspace(keyspace).getFunction("id", DataType.cint())) | |
| 328 | + assertThat(m.getKeyspace(keyspace).getFunction("\"ID\"", m.getKeyspace(keyspace).getUserType("user"))) | |
| 328 | 329 | .isNull(); |
| 329 | 330 | } |
| 330 | 331 | |
| 331 | 332 | @Test(groups = "short", dataProvider = "existingKeyspaceName") |
| 332 | 333 | @CassandraVersion(major = 2.2) |
| 333 | 334 | public void should_notify_of_aggregate_creation(String keyspace) { |
| 334 | - session.execute(String.format("CREATE FUNCTION %s.plus(s int, v int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java" | |
| 335 | + session.execute(String.format("CREATE FUNCTION %s.\"PLUS\"(s int, v int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java" | |
| 335 | 336 | + " AS 'return s+v;'", keyspace)); |
| 336 | - session.execute(String.format("CREATE AGGREGATE %s.sum(int) SFUNC plus STYPE int INITCOND 0;", keyspace)); | |
| 337 | + session.execute(String.format("CREATE AGGREGATE %s.\"SUM\"(int) SFUNC \"PLUS\" STYPE int INITCOND 0;", keyspace)); | |
| 337 | 338 | |
| 338 | 339 | for (SchemaChangeListener listener : listeners) { |
| 339 | 340 | ArgumentCaptor<AggregateMetadata> added = ArgumentCaptor.forClass(AggregateMetadata.class); |
| 340 | 341 | verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onAggregateAdded(added.capture()); |
| 341 | 342 | assertThat(added.getValue()) |
| 342 | 343 | .isInKeyspace(handleId(keyspace)) |
| 343 | - .hasFullName("sum(int)"); | |
| 344 | + .hasSignature("\"SUM\"(int)"); | |
| 344 | 345 | } |
| 345 | 346 | |
| 346 | 347 | for (Metadata m : metadatas) |
| 347 | - assertThat(m.getKeyspace(keyspace).getAggregate("sum", DataType.cint())) | |
| 348 | + assertThat(m.getKeyspace(keyspace).getAggregate("\"SUM\"", DataType.cint())) | |
| 348 | 349 | .isNotNull(); |
| 349 | 350 | } |
| 350 | 351 | |
| 351 | 352 | @Test(groups = "short", dataProvider = "existingKeyspaceName") |
| 352 | 353 | @CassandraVersion(major = 2.2) |
| 353 | 354 | public void should_notify_of_aggregate_update(String keyspace) { |
| 354 | - session.execute(String.format("CREATE FUNCTION %s.plus(s int, v int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java" | |
| 355 | + session.execute(String.format("CREATE FUNCTION %s.\"PLUS\"(s int, v int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java" | |
| 355 | 356 | + " AS 'return s+v;'", keyspace)); |
| 356 | - session.execute(String.format("CREATE AGGREGATE %s.sum(int) SFUNC plus STYPE int INITCOND 0", keyspace)); | |
| 357 | + session.execute(String.format("CREATE AGGREGATE %s.\"SUM\"(int) SFUNC \"PLUS\" STYPE int INITCOND 0", keyspace)); | |
| 357 | 358 | |
| 358 | 359 | for (Metadata m : metadatas) |
| 359 | - assertThat(m.getKeyspace(keyspace).getAggregate("sum", DataType.cint()).getInitCond()) | |
| 360 | + assertThat(m.getKeyspace(keyspace).getAggregate("\"SUM\"", DataType.cint()).getInitCond()) | |
| 360 | 361 | .isEqualTo(0); |
| 361 | 362 | |
| 362 | - session.execute(String.format("CREATE OR REPLACE AGGREGATE %s.sum(int) SFUNC plus STYPE int INITCOND 1", keyspace)); | |
| 363 | + session.execute(String.format("CREATE OR REPLACE AGGREGATE %s.\"SUM\"(int) SFUNC \"PLUS\" STYPE int INITCOND 1", keyspace)); | |
| 363 | 364 | |
| 364 | 365 | for (SchemaChangeListener listener : listeners) { |
| 365 | 366 | ArgumentCaptor<AggregateMetadata> current = ArgumentCaptor.forClass(AggregateMetadata.class); |
| @@ -370,28 +371,28 @@ | ||
| 370 | 371 | } |
| 371 | 372 | |
| 372 | 373 | for (Metadata m : metadatas) |
| 373 | - assertThat(m.getKeyspace(keyspace).getAggregate("sum", DataType.cint()).getInitCond()) | |
| 374 | + assertThat(m.getKeyspace(keyspace).getAggregate("\"SUM\"", DataType.cint()).getInitCond()) | |
| 374 | 375 | .isEqualTo(1); |
| 375 | 376 | } |
| 376 | 377 | |
| 377 | 378 | @Test(groups = "short", dataProvider = "existingKeyspaceName") |
| 378 | 379 | @CassandraVersion(major = 2.2) |
| 379 | 380 | public void should_notify_of_aggregate_drop(String keyspace) { |
| 380 | - session.execute(String.format("CREATE FUNCTION %s.plus(s int, v int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java" | |
| 381 | + session.execute(String.format("CREATE FUNCTION %s.\"PLUS\"(s int, v int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java" | |
| 381 | 382 | + " AS 'return s+v;'", keyspace)); |
| 382 | - session.execute(String.format("CREATE AGGREGATE %s.sum(int) SFUNC plus STYPE int INITCOND 0", keyspace)); | |
| 383 | - session.execute(String.format("DROP AGGREGATE %s.sum", keyspace)); | |
| 383 | + session.execute(String.format("CREATE AGGREGATE %s.\"SUM\"(int) SFUNC \"PLUS\" STYPE int INITCOND 0", keyspace)); | |
| 384 | + session.execute(String.format("DROP AGGREGATE %s.\"SUM\"", keyspace)); | |
| 384 | 385 | |
| 385 | 386 | for (SchemaChangeListener listener : listeners) { |
| 386 | 387 | ArgumentCaptor<AggregateMetadata> removed = ArgumentCaptor.forClass(AggregateMetadata.class); |
| 387 | 388 | verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onAggregateRemoved(removed.capture()); |
| 388 | 389 | assertThat(removed.getValue()) |
| 389 | 390 | .isInKeyspace(handleId(keyspace)) |
| 390 | - .hasFullName("sum(int)"); | |
| 391 | + .hasSignature("\"SUM\"(int)"); | |
| 391 | 392 | } |
| 392 | 393 | |
| 393 | 394 | for (Metadata m : metadatas) |
| 394 | - assertThat(m.getKeyspace(keyspace).getAggregate("sum", DataType.cint())) | |
| 395 | + assertThat(m.getKeyspace(keyspace).getAggregate("\"SUM\"", DataType.cint())) | |
| 395 | 396 | .isNull(); |
| 396 | 397 | } |
| 397 | 398 | |
| @@ -608,12 +609,12 @@ | ||
| 608 | 609 | session1.executeAsync("DROP TABLE \"CaseSensitive\".table1"), |
| 609 | 610 | session1.executeAsync("DROP TYPE lowercase.type1"), |
| 610 | 611 | session1.executeAsync("DROP TYPE \"CaseSensitive\".type1"), |
| 611 | - session1.executeAsync("DROP FUNCTION lowercase.id"), | |
| 612 | - session1.executeAsync("DROP FUNCTION \"CaseSensitive\".id"), | |
| 613 | - session1.executeAsync("DROP FUNCTION lowercase.plus"), | |
| 614 | - session1.executeAsync("DROP FUNCTION \"CaseSensitive\".plus"), | |
| 615 | - session1.executeAsync("DROP AGGREGATE lowercase.sum"), | |
| 616 | - session1.executeAsync("DROP AGGREGATE \"CaseSensitive\".sum"), | |
| 612 | + session1.executeAsync("DROP FUNCTION lowercase.\"ID\""), | |
| 613 | + session1.executeAsync("DROP FUNCTION \"CaseSensitive\".\"ID\""), | |
| 614 | + session1.executeAsync("DROP FUNCTION lowercase.\"PLUS\""), | |
| 615 | + session1.executeAsync("DROP FUNCTION \"CaseSensitive\".\"PLUS\""), | |
| 616 | + session1.executeAsync("DROP AGGREGATE lowercase.\"SUM\""), | |
| 617 | + session1.executeAsync("DROP AGGREGATE \"CaseSensitive\".\"SUM\""), | |
| 617 | 618 | session1.executeAsync("DROP MATERIALIZED VIEW lowercase.mv1"), |
| 618 | 619 | session1.executeAsync("DROP MATERIALIZED VIEW \"CaseSensitive\".mv1"), |
| 619 | 620 | session1.executeAsync("DROP KEYSPACE lowercase2"), |
modified driver-core/src/test/java/com/datastax/driver/core/UserTypesTest.java +9 −9
| @@ -49,9 +49,9 @@ | ||
| 49 | 49 | @Override |
| 50 | 50 | protected Collection<String> getTableDefinitions() { |
| 51 | 51 | String type1 = "CREATE TYPE phone (alias text, number text)"; |
| 52 | - String type2 = "CREATE TYPE address (street text, \"ZIP\" int, phones set<frozen<phone>>)"; | |
| 52 | + String type2 = "CREATE TYPE \"\"\"User Address\"\"\" (street text, \"ZIP\"\"\" int, phones set<frozen<phone>>)"; | |
| 53 | 53 | |
| 54 | - String table = "CREATE TABLE user (id int PRIMARY KEY, addr frozen<address>)"; | |
| 54 | + String table = "CREATE TABLE user (id int PRIMARY KEY, addr frozen<\"\"\"User Address\"\"\">)"; | |
| 55 | 55 | |
| 56 | 56 | return Arrays.asList(type1, type2, table); |
| 57 | 57 | } |
| @@ -70,13 +70,13 @@ | ||
| 70 | 70 | PreparedStatement ins = session.prepare("INSERT INTO user(id, addr) VALUES (?, ?)"); |
| 71 | 71 | PreparedStatement sel = session.prepare("SELECT * FROM user WHERE id=?"); |
| 72 | 72 | |
| 73 | - UserType addrDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("address"); | |
| 73 | + UserType addrDef = cluster.getMetadata().getKeyspace(keyspace).getUserType(quote("\"User Address\"")); | |
| 74 | 74 | UserType phoneDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("phone"); |
| 75 | 75 | |
| 76 | 76 | UDTValue phone1 = phoneDef.newValue().setString("alias", "home").setString("number", "0123548790"); |
| 77 | 77 | UDTValue phone2 = phoneDef.newValue().setString("alias", "work").setString("number", "0698265251"); |
| 78 | 78 | |
| 79 | - UDTValue addr = addrDef.newValue().setString("street", "1600 Pennsylvania Ave NW").setInt(quote("ZIP"), 20500).setSet("phones", ImmutableSet.of(phone1, phone2)); | |
| 79 | + UDTValue addr = addrDef.newValue().setString("street", "1600 Pennsylvania Ave NW").setInt(quote("ZIP\""), 20500).setSet("phones", ImmutableSet.of(phone1, phone2)); | |
| 80 | 80 | |
| 81 | 81 | session.execute(ins.bind(userId, addr)); |
| 82 | 82 | |
| @@ -100,13 +100,13 @@ | ||
| 100 | 100 | |
| 101 | 101 | try { |
| 102 | 102 | session.execute("USE " + keyspace); |
| 103 | - UserType addrDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("address"); | |
| 103 | + UserType addrDef = cluster.getMetadata().getKeyspace(keyspace).getUserType(quote("\"User Address\"")); | |
| 104 | 104 | UserType phoneDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("phone"); |
| 105 | 105 | |
| 106 | 106 | UDTValue phone1 = phoneDef.newValue().setString("alias", "home").setString("number", "0123548790"); |
| 107 | 107 | UDTValue phone2 = phoneDef.newValue().setString("alias", "work").setString("number", "0698265251"); |
| 108 | 108 | |
| 109 | - UDTValue addr = addrDef.newValue().setString("street", "1600 Pennsylvania Ave NW").setInt(quote("ZIP"), 20500).setSet("phones", ImmutableSet.of(phone1, phone2)); | |
| 109 | + UDTValue addr = addrDef.newValue().setString("street", "1600 Pennsylvania Ave NW").setInt(quote("ZIP\""), 20500).setSet("phones", ImmutableSet.of(phone1, phone2)); | |
| 110 | 110 | |
| 111 | 111 | session.execute("INSERT INTO user(id, addr) VALUES (?, ?)", userId, addr); |
| 112 | 112 | |
| @@ -134,7 +134,7 @@ | ||
| 134 | 134 | assertEquals(addrDef, null); |
| 135 | 135 | assertEquals(phoneDef, null); |
| 136 | 136 | |
| 137 | - addrDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("address"); | |
| 137 | + addrDef = cluster.getMetadata().getKeyspace(keyspace).getUserType(quote("\"User Address\"")); | |
| 138 | 138 | phoneDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("phone"); |
| 139 | 139 | assertNotEquals(addrDef, null); |
| 140 | 140 | assertNotEquals(phoneDef, null); |
| @@ -145,14 +145,14 @@ | ||
| 145 | 145 | "WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1'}"); |
| 146 | 146 | session.execute("USE " + nonExistingKeyspace); |
| 147 | 147 | |
| 148 | - addrDef = cluster.getMetadata().getKeyspace(nonExistingKeyspace).getUserType("address"); | |
| 148 | + addrDef = cluster.getMetadata().getKeyspace(nonExistingKeyspace).getUserType(quote("\"User Address\"")); | |
| 149 | 149 | phoneDef = cluster.getMetadata().getKeyspace(nonExistingKeyspace).getUserType("phone"); |
| 150 | 150 | assertEquals(addrDef, null); |
| 151 | 151 | assertEquals(phoneDef, null); |
| 152 | 152 | |
| 153 | 153 | session.execute("USE " + keyspace); |
| 154 | 154 | |
| 155 | - addrDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("address"); | |
| 155 | + addrDef = cluster.getMetadata().getKeyspace(keyspace).getUserType(quote("\"User Address\"")); | |
| 156 | 156 | phoneDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("phone"); |
| 157 | 157 | assertNotEquals(addrDef, null); |
| 158 | 158 | assertNotEquals(phoneDef, null); |