Added: hive/trunk/common/src/java/org/apache/hadoop/hive/conf/Validator.java URL: http://svn.apache.org/viewvc/hive/trunk/common/src/java/org/apache/hadoop/hive/conf/Validator.java?rev=1569164&view=auto ============================================================================== --- hive/trunk/common/src/java/org/apache/hadoop/hive/conf/Validator.java (added) +++ hive/trunk/common/src/java/org/apache/hadoop/hive/conf/Validator.java Tue Feb 18 02:18:36 2014 @@ -0,0 +1,159 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hive.conf; + +import java.util.ArrayList; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import java.util.regex.Pattern; + +/** + * validate value for a ConfVar, return non-null string for fail message + */ +public interface Validator { + + String validate(String value); + + static class StringSet implements Validator { + + private final Set<String> expected = new LinkedHashSet<String>(); + + public StringSet(String... values) { + for (String value : values) { + expected.add(value.toLowerCase()); + } + } + + @Override + public String validate(String value) { + if (value == null || !expected.contains(value.toLowerCase())) { + return "Invalid value.. expects one of " + expected; + } + return null; + } + } + + static enum RANGE_TYPE { + INT { + @Override + protected boolean inRange(String value, Object lower, Object upper) { + int ivalue = Integer.parseInt(value); + return (Integer)lower <= ivalue && ivalue <= (Integer)upper; + } + }, + LONG { + @Override + protected boolean inRange(String value, Object lower, Object upper) { + long lvalue = Long.parseLong(value); + return (Long)lower <= lvalue && lvalue <= (Long)upper; + } + }, + FLOAT { + @Override + protected boolean inRange(String value, Object lower, Object upper) { + float fvalue = Float.parseFloat(value); + return (Float)lower <= fvalue && fvalue <= (Float)upper; + } + }; + + public static RANGE_TYPE valueOf(Object lower, Object upper) { + if (lower instanceof Integer && upper instanceof Integer) { + assert (Integer)lower < (Integer)upper; + return INT; + } else if (lower instanceof Long && upper instanceof Long) { + assert (Long)lower < (Long)upper; + return LONG; + } else if (lower instanceof Float && upper instanceof Float) { + assert (Float)lower < (Float)upper; + return FLOAT; + } + throw new IllegalArgumentException("invalid range from " + lower + " to " + upper); + } + + protected abstract boolean inRange(String value, Object lower, Object upper); + } + + static class RangeValidator implements Validator { + + private final RANGE_TYPE type; + private final Object lower, upper; + + public RangeValidator(Object lower, Object upper) { + this.lower = lower; + this.upper = upper; + this.type = RANGE_TYPE.valueOf(lower, upper); + } + + @Override + public String validate(String value) { + try { + if (value == null) { + return "Value cannot be null"; + } + if (!type.inRange(value.trim(), lower, upper)) { + return "Invalid value " + value + ", which should be in between " + lower + " and " + upper; + } + } catch (Exception e) { + return e.toString(); + } + return null; + } + } + + static class PatternSet implements Validator { + + private final List<Pattern> expected = new ArrayList<Pattern>(); + + public PatternSet(String... values) { + for (String value : values) { + expected.add(Pattern.compile(value)); + } + } + + @Override + public String validate(String value) { + if (value == null) { + return "Invalid value.. expects one of patterns " + expected; + } + for (Pattern pattern : expected) { + if (pattern.matcher(value).matches()) { + return null; + } + } + return "Invalid value.. expects one of patterns " + expected; + } + } + + static class RatioValidator implements Validator { + + @Override + public String validate(String value) { + try { + float fvalue = Float.valueOf(value); + if (fvalue <= 0 || fvalue >= 1) { + return "Invalid ratio " + value + ", which should be in between 0 to 1"; + } + } catch (NumberFormatException e) { + return e.toString(); + } + return null; + } + } +}
Added: hive/trunk/common/src/java/org/apache/hive/common/util/SystemVariables.java URL: http://svn.apache.org/viewvc/hive/trunk/common/src/java/org/apache/hive/common/util/SystemVariables.java?rev=1569164&view=auto ============================================================================== --- hive/trunk/common/src/java/org/apache/hive/common/util/SystemVariables.java (added) +++ hive/trunk/common/src/java/org/apache/hive/common/util/SystemVariables.java Tue Feb 18 02:18:36 2014 @@ -0,0 +1,83 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hive.common.util; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.conf.HiveConf.ConfVars; + +public class SystemVariables { + + private static final Log l4j = LogFactory.getLog(SystemVariables.class); + protected static Pattern varPat = Pattern.compile("\\$\\{[^\\}\\$\u0020]+\\}"); + + public static final String ENV_PREFIX = "env:"; + public static final String SYSTEM_PREFIX = "system:"; + public static final String HIVECONF_PREFIX = "hiveconf:"; + public static final String HIVEVAR_PREFIX = "hivevar:"; + public static final String SET_COLUMN_NAME = "set"; + + protected String getSubstitute(HiveConf conf, String var) { + String val = null; + try { + if (var.startsWith(SYSTEM_PREFIX)) { + val = System.getProperty(var.substring(SYSTEM_PREFIX.length())); + } + } catch(SecurityException se) { + l4j.warn("Unexpected SecurityException in Configuration", se); + } + if (val == null) { + if (var.startsWith(ENV_PREFIX)) { + val = System.getenv(var.substring(ENV_PREFIX.length())); + } + } + return val; + } + + public String substitute(HiveConf conf, String expr) { + int depth = conf.getIntVar(ConfVars.HIVEVARIABLESUBSTITUTEDEPTH); + return substitute(conf, expr, depth); + } + + public String substitute(HiveConf conf, String expr, int depth) { + Matcher match = varPat.matcher(""); + String eval = expr; + for(int s = 0; s < depth; s++) { + match.reset(eval); + if (!match.find()) { + return eval; + } + String var = match.group(); + var = var.substring(2, var.length()-1); // remove ${ .. } + String val = getSubstitute(conf, var); + + if (val == null) { + l4j.debug("Interpolation result: " + eval); + return eval; // return literal, no substitution found + } + // substitute + eval = eval.substring(0, match.start()) + val + eval.substring(match.end()); + } + throw new IllegalStateException("Variable substitution depth too large: " + + conf.getIntVar(ConfVars.HIVEVARIABLESUBSTITUTEDEPTH) + " " + expr); + } +} Modified: hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveConf.java URL: http://svn.apache.org/viewvc/hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveConf.java?rev=1569164&r1=1569163&r2=1569164&view=diff ============================================================================== --- hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveConf.java (original) +++ hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveConf.java Tue Feb 18 02:18:36 2014 @@ -18,7 +18,6 @@ package org.apache.hadoop.hive.conf; import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.fs.Path; import org.apache.hadoop.hive.conf.HiveConf.ConfVars; import org.apache.hive.common.util.HiveTestUtils; import org.junit.Assert; @@ -43,7 +42,7 @@ public class TestHiveConf { } private void checkConfVar(ConfVars var, String expectedConfVarVal) throws Exception { - Assert.assertEquals(expectedConfVarVal, var.defaultVal); + Assert.assertEquals(expectedConfVarVal, var.getDefaultValue()); } private void checkHiveConf(String name, String expectedHiveVal) throws Exception { @@ -80,7 +79,7 @@ public class TestHiveConf { checkHiveConf("test.property1", "hive-site.xml"); // Test HiveConf property variable substitution in hive-site.xml - checkHiveConf("test.var.hiveconf.property", ConfVars.DEFAULTPARTITIONNAME.defaultVal); + checkHiveConf("test.var.hiveconf.property", ConfVars.DEFAULTPARTITIONNAME.getDefaultValue()); } @Test Modified: hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveConfRestrictList.java URL: http://svn.apache.org/viewvc/hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveConfRestrictList.java?rev=1569164&r1=1569163&r2=1569164&view=diff ============================================================================== --- hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveConfRestrictList.java (original) +++ hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveConfRestrictList.java Tue Feb 18 02:18:36 2014 @@ -19,7 +19,6 @@ package org.apache.hadoop.hive.conf; import junit.framework.TestCase; -import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.conf.HiveConf.ConfVars; import org.junit.Test; Modified: hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveLogging.java URL: http://svn.apache.org/viewvc/hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveLogging.java?rev=1569164&r1=1569163&r2=1569164&view=diff ============================================================================== --- hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveLogging.java (original) +++ hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveLogging.java Tue Feb 18 02:18:36 2014 @@ -18,7 +18,6 @@ package org.apache.hadoop.hive.conf; import java.io.BufferedReader; -import java.io.IOException; import java.io.InputStreamReader; import junit.framework.TestCase; @@ -26,7 +25,6 @@ import junit.framework.TestCase; import org.apache.hadoop.hive.common.LogUtils; import org.apache.hadoop.hive.conf.HiveConf.ConfVars; import org.apache.hive.common.util.HiveTestUtils; -import org.apache.hadoop.hive.conf.HiveConf.ConfVars; /** * TestHiveLogging
