diff options
author | Jon Bratseth <bratseth@gmail.com> | 2022-03-30 09:33:31 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-30 09:33:31 +0200 |
commit | 77c749f65ec23660b42c00855b0e7db400d9ce83 (patch) | |
tree | c6e1c1d16ee7b3999e3a7a26ae6f695404edc4d4 /container-search/src/main/java/com/yahoo/search/query/profile | |
parent | d8325551d8e84599bc6ef6ad2c14f83d82490a73 (diff) | |
parent | e4114d024511827ce09e2f6510552c2b958591cf (diff) |
Merge pull request #21870 from vespa-engine/bratseth/features-type-checking
Bratseth/features type checking
Diffstat (limited to 'container-search/src/main/java/com/yahoo/search/query/profile')
3 files changed, 37 insertions, 14 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/query/profile/QueryProfileProperties.java b/container-search/src/main/java/com/yahoo/search/query/profile/QueryProfileProperties.java index 5b3758f103d..19e0e441359 100644 --- a/container-search/src/main/java/com/yahoo/search/query/profile/QueryProfileProperties.java +++ b/container-search/src/main/java/com/yahoo/search/query/profile/QueryProfileProperties.java @@ -14,6 +14,7 @@ import com.yahoo.search.query.profile.types.ConversionContext; import com.yahoo.search.query.profile.types.FieldDescription; import com.yahoo.search.query.profile.types.QueryProfileFieldType; import com.yahoo.search.query.profile.types.QueryProfileType; +import com.yahoo.tensor.Tensor; import java.util.ArrayList; import java.util.Collections; @@ -91,6 +92,15 @@ public class QueryProfileProperties extends Properties { */ @Override public void set(CompoundName name, Object value, Map<String, String> context) { + setOrCheckSettable(name, value, context, true); + } + + @Override + public void requireSettable(CompoundName name, Object value, Map<String, String> context) { + setOrCheckSettable(name, value, context, false); + } + + private void setOrCheckSettable(CompoundName name, Object value, Map<String, String> context, boolean set) { try { name = unalias(name, context); @@ -110,29 +120,36 @@ public class QueryProfileProperties extends Properties { if (value instanceof String && value.toString().startsWith("ref:")) { if (profile.getRegistry() == null) throw new IllegalInputException("Runtime query profile references does not work when the " + - "QueryProfileProperties are constructed without a registry"); + "QueryProfileProperties are constructed without a registry"); String queryProfileId = value.toString().substring(4); value = profile.getRegistry().findQueryProfile(queryProfileId); if (value == null) throw new IllegalInputException("Query profile '" + queryProfileId + "' is not found"); } - if (value instanceof CompiledQueryProfile) { // this will be due to one of the two clauses above - if (references == null) - references = new ArrayList<>(); - references.add(0, new Pair<>(name, (CompiledQueryProfile)value)); // references set later has precedence - put first - } - else { - if (values == null) - values = new HashMap<>(); - values.put(name, value); + if (set) { + if (value instanceof CompiledQueryProfile) { // this will be due to one of the two clauses above + if (references == null) + references = new ArrayList<>(); + // references set later has precedence - put first + references.add(0, new Pair<>(name, (CompiledQueryProfile) value)); + } else { + if (values == null) + values = new HashMap<>(); + values.put(name, value); + } } } catch (IllegalArgumentException e) { - throw new IllegalInputException("Could not set '" + name + "' to '" + value + "'", e); + throw new IllegalInputException("Could not set '" + name + "' to '" + toShortString(value) + "'", e); } } + private String toShortString(Object value) { + if ( ! (value instanceof Tensor)) return value.toString(); + return ((Tensor)value).toShortString(); + } + private Object convertByType(CompoundName name, Object value, Map<String, String> context) { QueryProfileType type; QueryProfileType explicitTypeFromField = null; diff --git a/container-search/src/main/java/com/yahoo/search/query/profile/types/FieldDescription.java b/container-search/src/main/java/com/yahoo/search/query/profile/types/FieldDescription.java index a0f38f61672..f30a3cc5ae6 100644 --- a/container-search/src/main/java/com/yahoo/search/query/profile/types/FieldDescription.java +++ b/container-search/src/main/java/com/yahoo/search/query/profile/types/FieldDescription.java @@ -4,10 +4,8 @@ package com.yahoo.search.query.profile.types; import com.google.common.collect.ImmutableList; import com.yahoo.processing.request.CompoundName; import com.yahoo.search.query.profile.QueryProfile; -import com.yahoo.tensor.TensorType; import java.util.Arrays; -import java.util.Collections; import java.util.List; /** diff --git a/container-search/src/main/java/com/yahoo/search/query/profile/types/TensorFieldType.java b/container-search/src/main/java/com/yahoo/search/query/profile/types/TensorFieldType.java index 6f1cfccc16b..db6a58a4dd3 100644 --- a/container-search/src/main/java/com/yahoo/search/query/profile/types/TensorFieldType.java +++ b/container-search/src/main/java/com/yahoo/search/query/profile/types/TensorFieldType.java @@ -51,7 +51,15 @@ public class TensorFieldType extends FieldType { @Override public Object convertFrom(Object o, ConversionContext context) { - if (o instanceof Tensor) return o; + Tensor tensor = toTensor(o, context); + if (tensor == null) return null; + if (! tensor.type().isAssignableTo(type)) + throw new IllegalArgumentException("Require a tensor of type " + type); + return tensor; + } + + private Tensor toTensor(Object o, ConversionContext context) { + if (o instanceof Tensor) return (Tensor)o; if (o instanceof String && ((String)o).startsWith("embed(")) return encode((String)o, context); if (o instanceof String) return Tensor.from(type, (String)o); return null; |