Skip to content

Commit

Permalink
Use switch expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
wendigo committed Jan 22, 2025
1 parent 34e095e commit 8c85bd9
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,43 +80,35 @@ public static Optional<TypedValue> getTextTypedValue(JsonNode jsonNode)
public static Optional<TypedValue> getNumericTypedValue(JsonNode jsonNode)
{
if (jsonNode.getNodeType() == JsonNodeType.NUMBER) {
if (jsonNode instanceof BigIntegerNode) {
if (jsonNode.canConvertToInt()) {
return Optional.of(new TypedValue(INTEGER, jsonNode.longValue()));
return switch (jsonNode) {
case BigIntegerNode _ -> {
if (jsonNode.canConvertToInt()) {
yield Optional.of(new TypedValue(INTEGER, jsonNode.longValue()));
}
if (jsonNode.canConvertToLong()) {
yield Optional.of(new TypedValue(BIGINT, jsonNode.longValue()));
}
throw new JsonLiteralConversionException(jsonNode, "value too big");
}
if (jsonNode.canConvertToLong()) {
return Optional.of(new TypedValue(BIGINT, jsonNode.longValue()));
case DecimalNode _ -> {
BigDecimal jsonDecimal = jsonNode.decimalValue();
int precision = jsonDecimal.precision();
if (precision > MAX_PRECISION) {
throw new JsonLiteralConversionException(jsonNode, "precision too big");
}
int scale = jsonDecimal.scale();
DecimalType decimalType = createDecimalType(precision, scale);
Object value = decimalType.isShort() ? encodeShortScaledValue(jsonDecimal, scale) : encodeScaledValue(jsonDecimal, scale);
yield Optional.of(TypedValue.fromValueAsObject(decimalType, value));
}
throw new JsonLiteralConversionException(jsonNode, "value too big");
}
if (jsonNode instanceof DecimalNode) {
BigDecimal jsonDecimal = jsonNode.decimalValue();
int precision = jsonDecimal.precision();
if (precision > MAX_PRECISION) {
throw new JsonLiteralConversionException(jsonNode, "precision too big");
}
int scale = jsonDecimal.scale();
DecimalType decimalType = createDecimalType(precision, scale);
Object value = decimalType.isShort() ? encodeShortScaledValue(jsonDecimal, scale) : encodeScaledValue(jsonDecimal, scale);
return Optional.of(TypedValue.fromValueAsObject(decimalType, value));
}
if (jsonNode instanceof DoubleNode) {
return Optional.of(new TypedValue(DOUBLE, jsonNode.doubleValue()));
}
if (jsonNode instanceof FloatNode) {
return Optional.of(new TypedValue(REAL, floatToRawIntBits(jsonNode.floatValue())));
}
if (jsonNode instanceof IntNode) {
return Optional.of(new TypedValue(INTEGER, jsonNode.longValue()));
}
if (jsonNode instanceof LongNode) {
return Optional.of(new TypedValue(BIGINT, jsonNode.longValue()));
}
if (jsonNode instanceof ShortNode) {
return Optional.of(new TypedValue(SMALLINT, jsonNode.longValue()));
}
case DoubleNode _ -> Optional.of(new TypedValue(DOUBLE, jsonNode.doubleValue()));
case FloatNode _ -> Optional.of(new TypedValue(REAL, floatToRawIntBits(jsonNode.floatValue())));
case IntNode _ -> Optional.of(new TypedValue(INTEGER, jsonNode.longValue()));
case LongNode _ -> Optional.of(new TypedValue(BIGINT, jsonNode.longValue()));
case ShortNode _ -> Optional.of(new TypedValue(SMALLINT, jsonNode.longValue()));
default -> Optional.empty();
};
}

return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1241,16 +1241,12 @@ boolean isPrimitiveType()

public BytecodeExpression initialValueExpression()
{
if (initialValue == null) {
return defaultValue(type);
}
if (initialValue instanceof Number) {
return constantNumber((Number) initialValue);
}
if (initialValue instanceof Boolean) {
return constantBoolean((boolean) initialValue);
}
throw new IllegalArgumentException("Unsupported initial value type: " + initialValue.getClass());
return switch (initialValue) {
case null -> defaultValue(type);
case Number number -> constantNumber(number);
case Boolean _ -> constantBoolean((boolean) initialValue);
default -> throw new IllegalArgumentException("Unsupported initial value type: " + initialValue.getClass());
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,19 @@ private void validateAudience(Claims claims)
}

Object tokenAudience = claims.get(AUDIENCE);
if (tokenAudience == null) {
throw new InvalidClaimException(format("Expected %s claim to be: %s, but was not present in the JWT claims.", AUDIENCE, requiredAudience.get()));
}

if (tokenAudience instanceof String) {
if (!requiredAudience.get().equals((String) tokenAudience)) {
throw new InvalidClaimException(format("Invalid Audience: %s. Allowed audiences: %s", tokenAudience, requiredAudience.get()));
switch (tokenAudience) {
case String value -> {
if (!requiredAudience.get().equals(value)) {
throw new InvalidClaimException(format("Invalid Audience: %s. Allowed audiences: %s", tokenAudience, requiredAudience.get()));
}
}
}
else if (tokenAudience instanceof Collection) {
if (((Collection<?>) tokenAudience).stream().map(String.class::cast).noneMatch(aud -> requiredAudience.get().equals(aud))) {
throw new InvalidClaimException(format("Invalid Audience: %s. Allowed audiences: %s", tokenAudience, requiredAudience.get()));
case Collection<?> collection -> {
if (collection.stream().noneMatch(aud -> requiredAudience.get().equals(aud))) {
throw new InvalidClaimException(format("Invalid Audience: %s. Allowed audiences: %s", tokenAudience, requiredAudience.get()));
}
}
}
else {
throw new InvalidClaimException(format("Invalid Audience: %s", tokenAudience));
case null -> throw new InvalidClaimException(format("Expected %s claim to be: %s, but was not present in the JWT claims.", AUDIENCE, requiredAudience.get()));
default -> throw new InvalidClaimException(format("Invalid Audience: %s", tokenAudience));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1903,19 +1903,12 @@ private ArgumentsAnalysis analyzeArguments(List<ArgumentSpecification> argumentS

private ArgumentAnalysis analyzeArgument(ArgumentSpecification argumentSpecification, TableFunctionArgument argument, Optional<Scope> scope)
{
String actualType;
if (argument.getValue() instanceof TableFunctionTableArgument) {
actualType = "table";
}
else if (argument.getValue() instanceof TableFunctionDescriptorArgument) {
actualType = "descriptor";
}
else if (argument.getValue() instanceof Expression) {
actualType = "expression";
}
else {
throw semanticException(INVALID_FUNCTION_ARGUMENT, argument, "Unexpected table function argument type: %s", argument.getClass().getSimpleName());
}
String actualType = switch (argument.getValue()) {
case TableFunctionTableArgument _ -> "table";
case TableFunctionDescriptorArgument _ -> "descriptor";
case Expression _ -> "expression";
default -> throw semanticException(INVALID_FUNCTION_ARGUMENT, argument, "Unexpected table function argument type: %s", argument.getClass().getSimpleName());
};

if (argumentSpecification instanceof TableArgumentSpecification) {
if (!(argument.getValue() instanceof TableFunctionTableArgument)) {
Expand Down Expand Up @@ -4023,66 +4016,66 @@ private void analyzeJsonTableColumns(
JsonTable jsonTable)
{
for (JsonTableColumnDefinition column : columns) {
if (column instanceof OrdinalityColumn ordinalityColumn) {
String name = ordinalityColumn.getName().getCanonicalValue();
if (!uniqueNames.add(name)) {
throw semanticException(DUPLICATE_COLUMN_OR_PATH_NAME, ordinalityColumn.getName(), "All column and path names in JSON_TABLE invocation must be unique");
}
outputFields.add(Field.newUnqualified(name, BIGINT));
orderedOutputColumns.add(NodeRef.of(ordinalityColumn));
}
else if (column instanceof ValueColumn valueColumn) {
String name = valueColumn.getName().getCanonicalValue();
if (!uniqueNames.add(name)) {
throw semanticException(DUPLICATE_COLUMN_OR_PATH_NAME, valueColumn.getName(), "All column and path names in JSON_TABLE invocation must be unique");
switch (column) {
case OrdinalityColumn ordinalityColumn -> {
String name = ordinalityColumn.getName().getCanonicalValue();
if (!uniqueNames.add(name)) {
throw semanticException(DUPLICATE_COLUMN_OR_PATH_NAME, ordinalityColumn.getName(), "All column and path names in JSON_TABLE invocation must be unique");
}
outputFields.add(Field.newUnqualified(name, BIGINT));
orderedOutputColumns.add(NodeRef.of(ordinalityColumn));
}
valueColumn.getEmptyDefault().ifPresent(expression -> verifyNoAggregateWindowOrGroupingFunctions(session, functionResolver, accessControl, expression, "default expression for JSON_TABLE column"));
valueColumn.getErrorDefault().ifPresent(expression -> verifyNoAggregateWindowOrGroupingFunctions(session, functionResolver, accessControl, expression, "default expression for JSON_TABLE column"));
JsonPathAnalysis pathAnalysis = valueColumn.getJsonPath()
.map(this::analyzeJsonPath)
.orElseGet(() -> analyzeImplicitJsonPath(getImplicitJsonPath(name), valueColumn.getLocation()));
analysis.setJsonPathAnalysis(valueColumn, pathAnalysis);
TypeAndAnalysis typeAndAnalysis = analyzeJsonValueExpression(
valueColumn,
pathAnalysis,
session,
plannerContext,
statementAnalyzerFactory,
accessControl,
enclosingScope,
analysis,
warningCollector,
correlationSupport);
// default values can contain subqueries - the subqueries are recorded under the enclosing JsonTable node
analysis.recordSubqueries(jsonTable, typeAndAnalysis.analysis());
outputFields.add(Field.newUnqualified(name, typeAndAnalysis.type()));
orderedOutputColumns.add(NodeRef.of(valueColumn));
}
else if (column instanceof QueryColumn queryColumn) {
String name = queryColumn.getName().getCanonicalValue();
if (!uniqueNames.add(name)) {
throw semanticException(DUPLICATE_COLUMN_OR_PATH_NAME, queryColumn.getName(), "All column and path names in JSON_TABLE invocation must be unique");
case ValueColumn valueColumn -> {
String name = valueColumn.getName().getCanonicalValue();
if (!uniqueNames.add(name)) {
throw semanticException(DUPLICATE_COLUMN_OR_PATH_NAME, valueColumn.getName(), "All column and path names in JSON_TABLE invocation must be unique");
}
valueColumn.getEmptyDefault().ifPresent(expression -> verifyNoAggregateWindowOrGroupingFunctions(session, functionResolver, accessControl, expression, "default expression for JSON_TABLE column"));
valueColumn.getErrorDefault().ifPresent(expression -> verifyNoAggregateWindowOrGroupingFunctions(session, functionResolver, accessControl, expression, "default expression for JSON_TABLE column"));
JsonPathAnalysis pathAnalysis = valueColumn.getJsonPath()
.map(this::analyzeJsonPath)
.orElseGet(() -> analyzeImplicitJsonPath(getImplicitJsonPath(name), valueColumn.getLocation()));
analysis.setJsonPathAnalysis(valueColumn, pathAnalysis);
TypeAndAnalysis typeAndAnalysis = analyzeJsonValueExpression(
valueColumn,
pathAnalysis,
session,
plannerContext,
statementAnalyzerFactory,
accessControl,
enclosingScope,
analysis,
warningCollector,
correlationSupport);
// default values can contain subqueries - the subqueries are recorded under the enclosing JsonTable node
analysis.recordSubqueries(jsonTable, typeAndAnalysis.analysis());
outputFields.add(Field.newUnqualified(name, typeAndAnalysis.type()));
orderedOutputColumns.add(NodeRef.of(valueColumn));
}
JsonPathAnalysis pathAnalysis = queryColumn.getJsonPath()
.map(this::analyzeJsonPath)
.orElseGet(() -> analyzeImplicitJsonPath(getImplicitJsonPath(name), queryColumn.getLocation()));
analysis.setJsonPathAnalysis(queryColumn, pathAnalysis);
Type type = analyzeJsonQueryExpression(queryColumn, session, plannerContext, statementAnalyzerFactory, accessControl, enclosingScope, analysis, warningCollector);
outputFields.add(Field.newUnqualified(name, type));
orderedOutputColumns.add(NodeRef.of(queryColumn));
}
else if (column instanceof NestedColumns nestedColumns) {
nestedColumns.getPathName().ifPresent(name -> {
if (!uniqueNames.add(name.getCanonicalValue())) {
throw semanticException(DUPLICATE_COLUMN_OR_PATH_NAME, name, "All column and path names in JSON_TABLE invocation must be unique");
case QueryColumn queryColumn -> {
String name = queryColumn.getName().getCanonicalValue();
if (!uniqueNames.add(name)) {
throw semanticException(DUPLICATE_COLUMN_OR_PATH_NAME, queryColumn.getName(), "All column and path names in JSON_TABLE invocation must be unique");
}
});
JsonPathAnalysis pathAnalysis = analyzeJsonPath(nestedColumns.getJsonPath());
analysis.setJsonPathAnalysis(nestedColumns, pathAnalysis);
analyzeJsonTableColumns(nestedColumns.getColumns(), uniqueNames, outputFields, orderedOutputColumns, enclosingScope, jsonTable);
}
else {
throw new IllegalArgumentException("unexpected type of JSON_TABLE column: " + column.getClass().getSimpleName());
JsonPathAnalysis pathAnalysis = queryColumn.getJsonPath()
.map(this::analyzeJsonPath)
.orElseGet(() -> analyzeImplicitJsonPath(getImplicitJsonPath(name), queryColumn.getLocation()));
analysis.setJsonPathAnalysis(queryColumn, pathAnalysis);
Type type = analyzeJsonQueryExpression(queryColumn, session, plannerContext, statementAnalyzerFactory, accessControl, enclosingScope, analysis, warningCollector);
outputFields.add(Field.newUnqualified(name, type));
orderedOutputColumns.add(NodeRef.of(queryColumn));
}
case NestedColumns nestedColumns -> {
nestedColumns.getPathName().ifPresent(name -> {
if (!uniqueNames.add(name.getCanonicalValue())) {
throw semanticException(DUPLICATE_COLUMN_OR_PATH_NAME, name, "All column and path names in JSON_TABLE invocation must be unique");
}
});
JsonPathAnalysis pathAnalysis = analyzeJsonPath(nestedColumns.getJsonPath());
analysis.setJsonPathAnalysis(nestedColumns, pathAnalysis);
analyzeJsonTableColumns(nestedColumns.getColumns(), uniqueNames, outputFields, orderedOutputColumns, enclosingScope, jsonTable);
}
default -> throw new IllegalArgumentException("unexpected type of JSON_TABLE column: " + column.getClass().getSimpleName());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,12 @@ public Optional<SetOperationNode> mergeFirstSource()
addOriginalMappings(source, i, newMappingsBuilder);
}

if (node instanceof UnionNode) {
return Optional.of(new UnionNode(node.getId(), newSources, newMappingsBuilder.build(), node.getOutputSymbols()));
}
if (node instanceof IntersectNode) {
return Optional.of(new IntersectNode(node.getId(), newSources, newMappingsBuilder.build(), node.getOutputSymbols(), mergedQuantifier.get()));
}
if (node instanceof ExceptNode) {
return Optional.of(new ExceptNode(node.getId(), newSources, newMappingsBuilder.build(), node.getOutputSymbols(), mergedQuantifier.get()));
}
throw new IllegalArgumentException("unexpected node type: " + node.getClass().getSimpleName());
return switch (node) {
case UnionNode _ -> Optional.of(new UnionNode(node.getId(), newSources, newMappingsBuilder.build(), node.getOutputSymbols()));
case IntersectNode _ -> Optional.of(new IntersectNode(node.getId(), newSources, newMappingsBuilder.build(), node.getOutputSymbols(), mergedQuantifier.get()));
case ExceptNode _ -> Optional.of(new ExceptNode(node.getId(), newSources, newMappingsBuilder.build(), node.getOutputSymbols(), mergedQuantifier.get()));
default -> throw new IllegalArgumentException("unexpected node type: " + node.getClass().getSimpleName());
};
}

/**
Expand Down
Loading

0 comments on commit 8c85bd9

Please sign in to comment.