From 998315075343beecef962657b8cbf440d53cc13b Mon Sep 17 00:00:00 2001 From: kasiafi <30203062+kasiafi@users.noreply.github.com> Date: Thu, 12 May 2022 13:57:00 +0200 Subject: [PATCH] Add builders for table function arguments --- .../trino/sql/analyzer/StatementAnalyzer.java | 10 ++- .../trino/spi/ptf/ArgumentSpecification.java | 2 +- .../io/trino/spi/ptf/DescriptorArgument.java | 33 +++++++--- .../ptf/DescriptorArgumentSpecification.java | 35 ++++++++-- .../java/io/trino/spi/ptf/ScalarArgument.java | 30 +++++++++ .../spi/ptf/ScalarArgumentSpecification.java | 51 ++++++++++++--- .../java/io/trino/spi/ptf/TableArgument.java | 65 +++++++++++++++++++ .../spi/ptf/TableArgumentSpecification.java | 52 +++++++++++++-- 8 files changed, 248 insertions(+), 30 deletions(-) diff --git a/core/trino-main/src/main/java/io/trino/sql/analyzer/StatementAnalyzer.java b/core/trino-main/src/main/java/io/trino/sql/analyzer/StatementAnalyzer.java index 443b8a11710a..0f8ac9a2540e 100644 --- a/core/trino-main/src/main/java/io/trino/sql/analyzer/StatementAnalyzer.java +++ b/core/trino-main/src/main/java/io/trino/sql/analyzer/StatementAnalyzer.java @@ -1669,7 +1669,10 @@ else if (argument.getValue() instanceof Expression) { Type expectedArgumentType = ((ScalarArgumentSpecification) argumentSpecification).getType(); // currently, only constant arguments are supported Object constantValue = ExpressionInterpreter.evaluateConstantExpression(expression, expectedArgumentType, plannerContext, session, accessControl, analysis.getParameters()); - return new ScalarArgument(expectedArgumentType, constantValue); // TODO test coercion, test parameter + return ScalarArgument.builder() + .type(expectedArgumentType) + .value(constantValue) + .build(); // TODO test coercion, test parameter } throw new IllegalStateException("Unexpected argument specification: " + argumentSpecification.getClass().getSimpleName()); @@ -1687,7 +1690,10 @@ private Argument analyzeDefault(ArgumentSpecification argumentSpecification, Nod throw semanticException(NOT_SUPPORTED, errorLocation, "Descriptor arguments are not yet supported for table functions"); } if (argumentSpecification instanceof ScalarArgumentSpecification) { - return new ScalarArgument(((ScalarArgumentSpecification) argumentSpecification).getType(), argumentSpecification.getDefaultValue()); + return ScalarArgument.builder() + .type(((ScalarArgumentSpecification) argumentSpecification).getType()) + .value(argumentSpecification.getDefaultValue()) + .build(); } throw new IllegalStateException("Unexpected argument specification: " + argumentSpecification.getClass().getSimpleName()); diff --git a/core/trino-spi/src/main/java/io/trino/spi/ptf/ArgumentSpecification.java b/core/trino-spi/src/main/java/io/trino/spi/ptf/ArgumentSpecification.java index 3f99872897d4..fc473f1041c5 100644 --- a/core/trino-spi/src/main/java/io/trino/spi/ptf/ArgumentSpecification.java +++ b/core/trino-spi/src/main/java/io/trino/spi/ptf/ArgumentSpecification.java @@ -36,7 +36,7 @@ public abstract class ArgumentSpecification // native representation private final Object defaultValue; - public ArgumentSpecification(String name, boolean required, @Nullable Object defaultValue) + ArgumentSpecification(String name, boolean required, @Nullable Object defaultValue) { this.name = checkNotNullOrEmpty(name, "name"); checkArgument(!required || defaultValue == null, "non-null default value for a required argument"); diff --git a/core/trino-spi/src/main/java/io/trino/spi/ptf/DescriptorArgument.java b/core/trino-spi/src/main/java/io/trino/spi/ptf/DescriptorArgument.java index ea87f17f8e20..01cb5525b69b 100644 --- a/core/trino-spi/src/main/java/io/trino/spi/ptf/DescriptorArgument.java +++ b/core/trino-spi/src/main/java/io/trino/spi/ptf/DescriptorArgument.java @@ -30,17 +30,11 @@ public class DescriptorArgument extends Argument { - public static final DescriptorArgument NULL_DESCRIPTOR = new DescriptorArgument(Optional.empty()); + public static final DescriptorArgument NULL_DESCRIPTOR = builder().build(); private final Optional descriptor; - public static DescriptorArgument descriptorArgument(Descriptor descriptor) - { - requireNonNull(descriptor, "descriptor is null"); - return new DescriptorArgument(Optional.of(descriptor)); - } - @JsonCreator - private DescriptorArgument(@JsonProperty("descriptor") Optional descriptor) + public DescriptorArgument(@JsonProperty("descriptor") Optional descriptor) { this.descriptor = requireNonNull(descriptor, "descriptor is null"); } @@ -50,4 +44,27 @@ public Optional getDescriptor() { return descriptor; } + + public static Builder builder() + { + return new Builder(); + } + + public static final class Builder + { + private Descriptor descriptor; + + private Builder() {} + + public Builder descriptor(Descriptor descriptor) + { + this.descriptor = descriptor; + return this; + } + + public DescriptorArgument build() + { + return new DescriptorArgument(Optional.ofNullable(descriptor)); + } + } } diff --git a/core/trino-spi/src/main/java/io/trino/spi/ptf/DescriptorArgumentSpecification.java b/core/trino-spi/src/main/java/io/trino/spi/ptf/DescriptorArgumentSpecification.java index 5a9f1a36c374..b447d28b7d86 100644 --- a/core/trino-spi/src/main/java/io/trino/spi/ptf/DescriptorArgumentSpecification.java +++ b/core/trino-spi/src/main/java/io/trino/spi/ptf/DescriptorArgumentSpecification.java @@ -16,13 +16,40 @@ public class DescriptorArgumentSpecification extends ArgumentSpecification { - public DescriptorArgumentSpecification(String name) + private DescriptorArgumentSpecification(String name, boolean required, Descriptor defaultValue) { - super(name, true, null); + super(name, required, defaultValue); } - public DescriptorArgumentSpecification(String name, Descriptor defaultValue) + public static Builder builder() { - super(name, false, defaultValue); + return new Builder(); + } + + public static final class Builder + { + private String name; + private boolean required = true; + private Descriptor defaultValue; + + private Builder() {} + + public Builder name(String name) + { + this.name = name; + return this; + } + + public Builder defaultValue(Descriptor defaultValue) + { + this.required = false; + this.defaultValue = defaultValue; + return this; + } + + public DescriptorArgumentSpecification build() + { + return new DescriptorArgumentSpecification(name, required, defaultValue); + } } } diff --git a/core/trino-spi/src/main/java/io/trino/spi/ptf/ScalarArgument.java b/core/trino-spi/src/main/java/io/trino/spi/ptf/ScalarArgument.java index 06ce9308ea08..e2a54b594101 100644 --- a/core/trino-spi/src/main/java/io/trino/spi/ptf/ScalarArgument.java +++ b/core/trino-spi/src/main/java/io/trino/spi/ptf/ScalarArgument.java @@ -55,4 +55,34 @@ public Object getValue() { return value; } + + public static Builder builder() + { + return new Builder(); + } + + public static final class Builder + { + private Type type; + private Object value; + + private Builder() {} + + public Builder type(Type type) + { + this.type = type; + return this; + } + + public Builder value(Object value) + { + this.value = value; + return this; + } + + public ScalarArgument build() + { + return new ScalarArgument(type, value); + } + } } diff --git a/core/trino-spi/src/main/java/io/trino/spi/ptf/ScalarArgumentSpecification.java b/core/trino-spi/src/main/java/io/trino/spi/ptf/ScalarArgumentSpecification.java index ac581b6bfcdb..32139faf864b 100644 --- a/core/trino-spi/src/main/java/io/trino/spi/ptf/ScalarArgumentSpecification.java +++ b/core/trino-spi/src/main/java/io/trino/spi/ptf/ScalarArgumentSpecification.java @@ -24,21 +24,56 @@ public class ScalarArgumentSpecification { private final Type type; - public ScalarArgumentSpecification(String name, Type type) + private ScalarArgumentSpecification(String name, Type type, boolean required, Object defaultValue) { - super(name, true, null); + super(name, required, defaultValue); this.type = requireNonNull(type, "type is null"); + if (defaultValue != null) { + checkArgument(type.getJavaType().equals(defaultValue.getClass()), format("default value %s does not match the declared type: %s", defaultValue, type)); + } } - public ScalarArgumentSpecification(String name, Type type, Object defaultValue) + public Type getType() { - super(name, false, defaultValue); - this.type = requireNonNull(type, "type is null"); - checkArgument(type.getJavaType().equals(defaultValue.getClass()), format("default value %s does not match the declared type: %s", defaultValue, type)); + return type; } - public Type getType() + public static Builder builder() { - return type; + return new Builder(); + } + + public static final class Builder + { + private String name; + private Type type; + private boolean required = true; + private Object defaultValue; + + private Builder() {} + + public Builder name(String name) + { + this.name = name; + return this; + } + + public Builder type(Type type) + { + this.type = type; + return this; + } + + public Builder defaultValue(Object defaultValue) + { + this.required = false; + this.defaultValue = defaultValue; + return this; + } + + public ScalarArgumentSpecification build() + { + return new ScalarArgumentSpecification(name, type, required, defaultValue); + } } } diff --git a/core/trino-spi/src/main/java/io/trino/spi/ptf/TableArgument.java b/core/trino-spi/src/main/java/io/trino/spi/ptf/TableArgument.java index 7a6b2ddbaba7..9742c39fd1ff 100644 --- a/core/trino-spi/src/main/java/io/trino/spi/ptf/TableArgument.java +++ b/core/trino-spi/src/main/java/io/trino/spi/ptf/TableArgument.java @@ -102,6 +102,71 @@ public boolean isPassThroughColumns() return passThroughColumns; } + public static Builder builder() + { + return new Builder(); + } + + public static final class Builder + { + private Optional name; + private RowType rowType; + private List partitionBy = List.of(); + private List orderBy = List.of(); + private boolean rowSemantics; + private boolean pruneWhenEmpty; + private boolean passThroughColumns; + + private Builder() {} + + public Builder name(Optional name) + { + this.name = name; + return this; + } + + public Builder rowType(RowType rowType) + { + this.rowType = rowType; + return this; + } + + public Builder partitionBy(List partitionBy) + { + this.partitionBy = partitionBy; + return this; + } + + public Builder orderBy(List orderBy) + { + this.orderBy = orderBy; + return this; + } + + public Builder rowSemantics(boolean rowSemantics) + { + this.rowSemantics = rowSemantics; + return this; + } + + public Builder pruneWhenEmpty(boolean pruneWhenEmpty) + { + this.pruneWhenEmpty = pruneWhenEmpty; + return this; + } + + public Builder passThroughColumns(boolean passThroughColumns) + { + this.passThroughColumns = passThroughColumns; + return this; + } + + public TableArgument build() + { + return new TableArgument(name, rowType, partitionBy, orderBy, rowSemantics, pruneWhenEmpty, passThroughColumns); + } + } + public static class QualifiedName { private final String catalogName; diff --git a/core/trino-spi/src/main/java/io/trino/spi/ptf/TableArgumentSpecification.java b/core/trino-spi/src/main/java/io/trino/spi/ptf/TableArgumentSpecification.java index 6bdf202fcec4..db5374e9051b 100644 --- a/core/trino-spi/src/main/java/io/trino/spi/ptf/TableArgumentSpecification.java +++ b/core/trino-spi/src/main/java/io/trino/spi/ptf/TableArgumentSpecification.java @@ -20,7 +20,7 @@ public class TableArgumentSpecification private final boolean pruneWhenEmpty; private final boolean passThroughColumns; - public TableArgumentSpecification(String name, boolean rowSemantics, boolean pruneWhenEmpty, boolean passThroughColumns) + private TableArgumentSpecification(String name, boolean rowSemantics, boolean pruneWhenEmpty, boolean passThroughColumns) { super(name, true, null); @@ -29,12 +29,6 @@ public TableArgumentSpecification(String name, boolean rowSemantics, boolean pru this.passThroughColumns = passThroughColumns; } - public TableArgumentSpecification(String name) - { - // defaults - this(name, false, false, false); - } - public boolean isRowSemantics() { return rowSemantics; @@ -49,4 +43,48 @@ public boolean isPassThroughColumns() { return passThroughColumns; } + + public static Builder builder(String name) + { + return new Builder(); + } + + public static final class Builder + { + private String name; + private boolean rowSemantics; + private boolean pruneWhenEmpty; + private boolean passThroughColumns; + + private Builder() {} + + public Builder name(String name) + { + this.name = name; + return this; + } + + public Builder rowSemantics(boolean rowSemantics) + { + this.rowSemantics = rowSemantics; + return this; + } + + public Builder pruneWhenEmpty(boolean pruneWhenEmpty) + { + this.pruneWhenEmpty = pruneWhenEmpty; + return this; + } + + public Builder passThroughColumns(boolean passThroughColumns) + { + this.passThroughColumns = passThroughColumns; + return this; + } + + public TableArgumentSpecification build() + { + return new TableArgumentSpecification(name, rowSemantics, pruneWhenEmpty, passThroughColumns); + } + } }