|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.painless; |
| 21 | + |
| 22 | +import org.elasticsearch.painless.lookup.PainlessLookup; |
| 23 | +import org.elasticsearch.painless.lookup.PainlessLookupBuilder; |
| 24 | +import org.elasticsearch.painless.spi.Whitelist; |
| 25 | +import org.elasticsearch.painless.symbol.ScriptScope; |
| 26 | +import org.elasticsearch.script.ScriptContext; |
| 27 | + |
| 28 | +import java.security.AccessController; |
| 29 | +import java.security.PrivilegedAction; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | + |
| 34 | +public class DocFieldsPhaseTests extends ScriptTestCase { |
| 35 | + PainlessLookup lookup = PainlessLookupBuilder.buildFromWhitelists(Whitelist.BASE_WHITELISTS); |
| 36 | + |
| 37 | + ScriptScope compile(String script) { |
| 38 | + Compiler compiler = new Compiler( |
| 39 | + MockDocTestScript.CONTEXT.instanceClazz, |
| 40 | + MockDocTestScript.CONTEXT.factoryClazz, |
| 41 | + MockDocTestScript.CONTEXT.statefulFactoryClazz, lookup |
| 42 | + ); |
| 43 | + |
| 44 | + // Create our loader (which loads compiled code with no permissions). |
| 45 | + final Compiler.Loader loader = AccessController.doPrivileged(new PrivilegedAction<>() { |
| 46 | + @Override |
| 47 | + public Compiler.Loader run() { |
| 48 | + return compiler.createLoader(getClass().getClassLoader()); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + return compiler.compile(loader,"test", script, new CompilerSettings()); |
| 53 | + } |
| 54 | + |
| 55 | + public abstract static class MockDocTestScript { |
| 56 | + public static final String[] PARAMETERS = {"doc", "other"}; |
| 57 | + public abstract void execute(Map<String, Object> doc, Map<String, Object> other); |
| 58 | + |
| 59 | + public interface Factory { |
| 60 | + MockDocTestScript newInstance(); |
| 61 | + } |
| 62 | + |
| 63 | + public static final ScriptContext<Factory> CONTEXT = |
| 64 | + new ScriptContext<>("test", MockDocTestScript.Factory.class); |
| 65 | + } |
| 66 | + |
| 67 | + public void testArray() { |
| 68 | + List<String> expected = List.of("my_field"); |
| 69 | + // Order shouldn't matter |
| 70 | + assertEquals(expected, compile("def a = doc['my_field']; def b = other['foo']").docFields()); |
| 71 | + assertEquals(expected, compile("def b = other['foo']; def a = doc['my_field']").docFields()); |
| 72 | + |
| 73 | + // Only collect array on doc |
| 74 | + assertEquals(Collections.emptyList(), compile("def a = other['bar']").docFields()); |
| 75 | + |
| 76 | + // Only handle str const |
| 77 | + assertEquals(Collections.emptyList(), compile("String f = 'bar'; def a = other[f]").docFields()); |
| 78 | + } |
| 79 | + |
| 80 | + public void testDot() { |
| 81 | + List<String> expected = List.of("my_field"); |
| 82 | + // Order shouldn't matter |
| 83 | + assertEquals(expected, compile("def a = doc.my_field; def b = other.foo").docFields()); |
| 84 | + assertEquals(expected, compile("def b = other.foo; def a = doc.my_field").docFields()); |
| 85 | + |
| 86 | + // Only collect doc dots |
| 87 | + assertEquals(Collections.emptyList(), compile("def a = other.bar").docFields()); |
| 88 | + } |
| 89 | + |
| 90 | + public void testGet() { |
| 91 | + // Order shouldn't matter |
| 92 | + List<String> expected = List.of("my_field"); |
| 93 | + assertEquals(expected, compile("def a = doc.get('my_field'); def b = other.get('foo')").docFields()); |
| 94 | + assertEquals(expected, compile("def b = other.get('foo'); def a = doc.get('my_field')").docFields()); |
| 95 | + |
| 96 | + // Should work in Lambda |
| 97 | + assertEquals(expected, compile("[].sort((a, b) -> doc.get('my_field')); [].sort((a, b) -> doc.equals('bar') ? 1:2)").docFields()); |
| 98 | + |
| 99 | + // Only collect get on doc |
| 100 | + assertEquals(Collections.emptyList(), compile("def a = other.get('bar')").docFields()); |
| 101 | + assertEquals(Collections.emptyList(), compile("def a = doc.equals('bar')").docFields()); |
| 102 | + |
| 103 | + // Only handle str const |
| 104 | + assertEquals(Collections.emptyList(), compile("String f = 'bar'; def b = doc.get(f)").docFields()); |
| 105 | + } |
| 106 | +} |
0 commit comments