http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java-filtered/org/apache/calcite/avatica/util/FilteredConstants.java ---------------------------------------------------------------------- diff --git a/core/src/main/java-filtered/org/apache/calcite/avatica/util/FilteredConstants.java b/core/src/main/java-filtered/org/apache/calcite/avatica/util/FilteredConstants.java new file mode 100644 index 0000000..53b76ee --- /dev/null +++ b/core/src/main/java-filtered/org/apache/calcite/avatica/util/FilteredConstants.java @@ -0,0 +1,26 @@ +/* + * 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.calcite.avatica.util; + +/** + * A class which, at build time, will have build-specific variables substituted into it. + */ +public class FilteredConstants { + public static final String VERSION = "${avatica.release.version}"; +} + +// End FilteredConstants.java
http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/DataContext.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/DataContext.java b/core/src/main/java/org/apache/calcite/DataContext.java deleted file mode 100644 index 78642aa..0000000 --- a/core/src/main/java/org/apache/calcite/DataContext.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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.calcite; - -import org.apache.calcite.adapter.java.JavaTypeFactory; -import org.apache.calcite.linq4j.QueryProvider; -import org.apache.calcite.linq4j.tree.Expressions; -import org.apache.calcite.linq4j.tree.ParameterExpression; -import org.apache.calcite.schema.SchemaPlus; -import org.apache.calcite.sql.advise.SqlAdvisor; - -import com.google.common.base.CaseFormat; - -import java.lang.reflect.Modifier; -import java.util.TimeZone; -import java.util.concurrent.atomic.AtomicBoolean; - -/** - * Runtime context allowing access to the tables in a database. - */ -public interface DataContext { - ParameterExpression ROOT = - Expressions.parameter(Modifier.FINAL, DataContext.class, "root"); - - /** - * Returns a sub-schema with a given name, or null. - */ - SchemaPlus getRootSchema(); - - /** - * Returns the type factory. - */ - JavaTypeFactory getTypeFactory(); - - /** - * Returns the query provider. - */ - QueryProvider getQueryProvider(); - - /** - * Returns a context variable. - * - * <p>Supported variables include: "sparkContext", "currentTimestamp", - * "localTimestamp".</p> - * - * @param name Name of variable - */ - Object get(String name); - - /** Variable that may be asked for in a call to {@link DataContext#get}. */ - enum Variable { - UTC_TIMESTAMP("utcTimestamp", Long.class), - - /** The time at which the current statement started executing. In - * milliseconds after 1970-01-01 00:00:00, UTC. Required. */ - CURRENT_TIMESTAMP("currentTimestamp", Long.class), - - /** The time at which the current statement started executing. In - * milliseconds after 1970-01-01 00:00:00, in the time zone of the current - * statement. Required. */ - LOCAL_TIMESTAMP("localTimestamp", Long.class), - - /** The Spark engine. Available if Spark is on the class path. */ - SPARK_CONTEXT("sparkContext", Object.class), - - /** A mutable flag that indicates whether user has requested that the - * current statement be canceled. Cancellation may not be immediate, but - * implementations of relational operators should check the flag fairly - * frequently and cease execution (e.g. by returning end of data). */ - CANCEL_FLAG("cancelFlag", AtomicBoolean.class), - - /** Advisor that suggests completion hints for SQL statements. */ - SQL_ADVISOR("sqlAdvisor", SqlAdvisor.class), - - /** Time zone in which the current statement is executing. Required; - * defaults to the time zone of the JVM if the connection does not specify a - * time zone. */ - TIME_ZONE("timeZone", TimeZone.class); - - public final String camelName; - public final Class clazz; - - Variable(String camelName, Class clazz) { - this.camelName = camelName; - this.clazz = clazz; - assert camelName.equals( - CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name())); - } - - /** Returns the value of this variable in a given data context. */ - public <T> T get(DataContext dataContext) { - //noinspection unchecked - return (T) clazz.cast(dataContext.get(camelName)); - } - } -} - -// End DataContext.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/Demo.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/Demo.java b/core/src/main/java/org/apache/calcite/Demo.java deleted file mode 100644 index 27f65fe..0000000 --- a/core/src/main/java/org/apache/calcite/Demo.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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.calcite; - -import java.util.ArrayList; - -/** - * Demo. - */ -public class Demo { - private Demo() { - } - - public static void main(String[] args) { - ArrayList<String> names = new ArrayList<String>(); - names.add("John"); - names.add("Paul"); - names.add("George"); - names.add("Ringo"); - - Iterable<String> nameIterable = names; - - for (String name : nameIterable) { - System.out.println(name); - } - } -} - -// End Demo.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/clone/ArrayTable.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/clone/ArrayTable.java b/core/src/main/java/org/apache/calcite/adapter/clone/ArrayTable.java deleted file mode 100644 index d87165c..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/clone/ArrayTable.java +++ /dev/null @@ -1,900 +0,0 @@ -/* - * 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.calcite.adapter.clone; - -import org.apache.calcite.DataContext; -import org.apache.calcite.adapter.java.AbstractQueryableTable; -import org.apache.calcite.linq4j.AbstractEnumerable; -import org.apache.calcite.linq4j.Enumerable; -import org.apache.calcite.linq4j.Enumerator; -import org.apache.calcite.linq4j.Ord; -import org.apache.calcite.linq4j.QueryProvider; -import org.apache.calcite.linq4j.Queryable; -import org.apache.calcite.linq4j.tree.Primitive; -import org.apache.calcite.rel.RelCollation; -import org.apache.calcite.rel.RelCollations; -import org.apache.calcite.rel.type.RelDataType; -import org.apache.calcite.rel.type.RelDataTypeFactory; -import org.apache.calcite.rel.type.RelProtoDataType; -import org.apache.calcite.schema.ScannableTable; -import org.apache.calcite.schema.SchemaPlus; -import org.apache.calcite.schema.Statistic; -import org.apache.calcite.schema.Statistics; -import org.apache.calcite.schema.impl.AbstractTableQueryable; -import org.apache.calcite.util.ImmutableBitSet; -import org.apache.calcite.util.Pair; - -import com.google.common.base.Supplier; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; - -import java.lang.reflect.Array; -import java.lang.reflect.Type; -import java.util.AbstractList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -/** - * Implementation of table that reads rows from column stores, one per column. - * Column store formats are chosen based on the type and distribution of the - * values in the column; see {@link Representation} and - * {@link RepresentationType}. - */ -class ArrayTable extends AbstractQueryableTable implements ScannableTable { - private final RelProtoDataType protoRowType; - private final Supplier<Content> supplier; - - /** Creates an ArrayTable. */ - public ArrayTable(Type elementType, RelProtoDataType protoRowType, - Supplier<Content> supplier) { - super(elementType); - this.protoRowType = protoRowType; - this.supplier = supplier; - } - - public RelDataType getRowType(RelDataTypeFactory typeFactory) { - return protoRowType.apply(typeFactory); - } - - public Statistic getStatistic() { - final List<ImmutableBitSet> keys = Lists.newArrayList(); - final Content content = supplier.get(); - for (Ord<Column> ord : Ord.zip(content.columns)) { - if (ord.e.cardinality == content.size) { - keys.add(ImmutableBitSet.of(ord.i)); - } - } - return Statistics.of(content.size, keys, content.collations); - } - - public Enumerable<Object[]> scan(DataContext root) { - return new AbstractEnumerable<Object[]>() { - public Enumerator<Object[]> enumerator() { - final Content content = supplier.get(); - return content.arrayEnumerator(); - } - }; - } - - public <T> Queryable<T> asQueryable(final QueryProvider queryProvider, - SchemaPlus schema, String tableName) { - return new AbstractTableQueryable<T>(queryProvider, schema, this, - tableName) { - @SuppressWarnings("unchecked") - public Enumerator<T> enumerator() { - final Content content = supplier.get(); - return content.enumerator(); - } - }; - } - - @SuppressWarnings("unchecked") - private static <T> Pair<Object, T> toPair(Object dataSet) { - return (Pair<Object, T>) dataSet; - } - - /** How a column's values are represented. */ - enum RepresentationType { - /** Constant. Contains only one value. - * - * <p>We can't store 0-bit values in - * an array: we'd have no way of knowing how many there were.</p> - * - * @see Constant - */ - CONSTANT, - - /** Object array. Null values are represented by null. Values may or may - * not be canonized; if canonized, = and != can be implemented using - * pointer. - * - * @see ObjectArray - */ - OBJECT_ARRAY, - - /** - * Array of primitives. Null values not possible. Only for primitive - * types (and not optimal for boolean). - * - * @see PrimitiveArray - */ - PRIMITIVE_ARRAY, - - /** Bit-sliced primitive array. Values are {@code bitCount} bits each, - * and interpreted as signed. Stored as an array of long values. - * - * <p>If gcd(bitCount, 64) != 0, some values will cross boundaries. - * bits each. But for all of those values except 4, there is a primitive - * type (8 byte, 16 short, 32 int) which is more efficient. - * - * @see BitSlicedPrimitiveArray - */ - BIT_SLICED_PRIMITIVE_ARRAY, - - /** - * Dictionary of primitives. Use one of the previous methods to store - * unsigned offsets into the dictionary. Dictionary is canonized and - * sorted, so v1 < v2 if and only if code(v1) < code(v2). The - * dictionary may or may not contain a null value. - * - * <p>The dictionary is not beneficial unless the codes are - * significantly shorter than the values. A column of {@code long} - * values with many duplicates is a win; a column of mostly distinct - * {@code short} values is likely a loss. The other win is if there are - * null values; otherwise the best option would be an - * {@link #OBJECT_ARRAY}.</p> - * - * @see PrimitiveDictionary - */ - PRIMITIVE_DICTIONARY, - - /** - * Dictionary of objects. Use one of the previous methods to store - * unsigned offsets into the dictionary. - * - * @see ObjectDictionary - */ - OBJECT_DICTIONARY, - - /** - * Compressed string table. Block of char data. Strings represented - * using an unsigned offset into the table (stored using one of the - * previous methods). - * - * <p>First 2 bytes are unsigned length; subsequent bytes are string - * contents. The null value, strings longer than 64k and strings that - * occur very commonly are held in an 'exceptions' array and are - * recognized by their high offsets. Other strings are created on demand - * (this reduces the number of objects that need to be created during - * deserialization from cache.</p> - * - * @see StringDictionary - */ - STRING_DICTIONARY, - - /** - * Compressed byte array table. Similar to compressed string table. - * - * @see ByteStringDictionary - */ - BYTE_STRING_DICTIONARY, - } - - /** Column definition and value set. */ - public static class Column { - final Representation representation; - final Object dataSet; - final int cardinality; - - Column(Representation representation, Object data, int cardinality) { - this.representation = representation; - this.dataSet = data; - this.cardinality = cardinality; - } - - public Column permute(int[] sources) { - return new Column( - representation, - representation.permute(dataSet, sources), - cardinality); - } - - @Override public String toString() { - return "Column(representation=" + representation - + ", value=" + representation.toString(dataSet) + ")"; - } - - /** Returns a list view onto a data set. */ - public static List asList(final Representation representation, - final Object dataSet) { - // Cache size. It might be expensive to compute. - final int size = representation.size(dataSet); - return new AbstractList() { - public Object get(int index) { - return representation.getObject(dataSet, index); - } - - public int size() { - return size; - } - }; - } - } - - /** Representation of the values of a column. */ - public interface Representation { - /** Returns the representation type. */ - RepresentationType getType(); - - /** Converts a value set into a compact representation. If - * {@code sources} is not null, permutes. */ - Object freeze(ColumnLoader.ValueSet valueSet, int[] sources); - - Object getObject(Object dataSet, int ordinal); - int getInt(Object dataSet, int ordinal); - - /** Creates a data set that is the same as a given data set - * but re-ordered. */ - Object permute(Object dataSet, int[] sources); - - /** Returns the number of elements in a data set. (Some representations - * return the capacity, which may be slightly larger than the actual - * size.) */ - int size(Object dataSet); - - /** Converts a data set to a string. */ - String toString(Object dataSet); - } - - /** Representation that stores the column values in an array. */ - public static class ObjectArray implements Representation { - final int ordinal; - - public ObjectArray(int ordinal) { - this.ordinal = ordinal; - } - - public String toString() { - return "ObjectArray(ordinal=" + ordinal + ")"; - } - - public RepresentationType getType() { - return RepresentationType.OBJECT_ARRAY; - } - - public Object freeze(ColumnLoader.ValueSet valueSet, int[] sources) { - // We assume the values have been canonized. - final List<Comparable> list = permuteList(valueSet.values, sources); - return list.toArray(new Comparable[list.size()]); - } - - public Object permute(Object dataSet, int[] sources) { - Comparable[] list = (Comparable[]) dataSet; - final int size = list.length; - final Comparable[] comparables = new Comparable[size]; - for (int i = 0; i < size; i++) { - comparables[i] = list[sources[i]]; - } - return comparables; - } - - public Object getObject(Object dataSet, int ordinal) { - return ((Comparable[]) dataSet)[ordinal]; - } - - public int getInt(Object dataSet, int ordinal) { - return ((Number) getObject(dataSet, ordinal)).intValue(); - } - - public int size(Object dataSet) { - return ((Comparable[]) dataSet).length; - } - - public String toString(Object dataSet) { - return Arrays.toString((Comparable[]) dataSet); - } - } - - /** Representation that stores the values of a column in an array of - * primitive values. */ - public static class PrimitiveArray implements Representation { - final int ordinal; - private final Primitive primitive; - private final Primitive p; - - public PrimitiveArray(int ordinal, Primitive primitive, Primitive p) { - this.ordinal = ordinal; - this.primitive = primitive; - this.p = p; - } - - public String toString() { - return "PrimitiveArray(ordinal=" + ordinal - + ", primitive=" + primitive - + ", p=" + p - + ")"; - } - - public RepresentationType getType() { - return RepresentationType.PRIMITIVE_ARRAY; - } - - public Object freeze(ColumnLoader.ValueSet valueSet, int[] sources) { - //noinspection unchecked - return primitive.toArray2( - permuteList((List) valueSet.values, sources)); - } - - public Object permute(Object dataSet, int[] sources) { - return primitive.permute(dataSet, sources); - } - - public Object getObject(Object dataSet, int ordinal) { - return p.arrayItem(dataSet, ordinal); - } - - public int getInt(Object dataSet, int ordinal) { - return Array.getInt(dataSet, ordinal); - } - - public int size(Object dataSet) { - return Array.getLength(dataSet); - } - - public String toString(Object dataSet) { - return p.arrayToString(dataSet); - } - } - - /** Representation that stores column values in a dictionary of - * primitive values, then uses a short code for each row. */ - public static class PrimitiveDictionary implements Representation { - public PrimitiveDictionary() { - } - - public String toString() { - return "PrimitiveDictionary()"; - } - - public RepresentationType getType() { - return RepresentationType.PRIMITIVE_DICTIONARY; - } - - public Object freeze(ColumnLoader.ValueSet valueSet, int[] sources) { - throw new UnsupportedOperationException(); // TODO: - } - - public Object permute(Object dataSet, int[] sources) { - throw new UnsupportedOperationException(); // TODO: - } - - public Object getObject(Object dataSet, int ordinal) { - throw new UnsupportedOperationException(); // TODO: - } - - public int getInt(Object dataSet, int ordinal) { - throw new UnsupportedOperationException(); // TODO: - } - - public int size(Object dataSet) { - throw new UnsupportedOperationException(); // TODO: - } - - public String toString(Object dataSet) { - throw new UnsupportedOperationException(); // TODO: - } - } - - /** Representation that stores the values of a column as a - * dictionary of objects. */ - public static class ObjectDictionary implements Representation { - final int ordinal; - final Representation representation; - - public ObjectDictionary( - int ordinal, - Representation representation) { - this.ordinal = ordinal; - this.representation = representation; - } - - public String toString() { - return "ObjectDictionary(ordinal=" + ordinal - + ", representation=" + representation - + ")"; - } - - public RepresentationType getType() { - return RepresentationType.OBJECT_DICTIONARY; - } - - public Object freeze(ColumnLoader.ValueSet valueSet, int[] sources) { - final int n = valueSet.map.keySet().size(); - int extra = valueSet.containsNull ? 1 : 0; - Comparable[] codeValues = - valueSet.map.keySet().toArray(new Comparable[n + extra]); - Arrays.sort(codeValues, 0, n); - ColumnLoader.ValueSet codeValueSet = - new ColumnLoader.ValueSet(int.class); - final List<Comparable> list = permuteList(valueSet.values, sources); - for (Comparable value : list) { - int code; - if (value == null) { - code = n; - } else { - code = Arrays.binarySearch(codeValues, value); - assert code >= 0 : code + ", " + value; - } - codeValueSet.add(code); - } - Object codes = representation.freeze(codeValueSet, null); - return Pair.of(codes, codeValues); - } - - public Object permute(Object dataSet, int[] sources) { - final Pair<Object, Comparable[]> pair = toPair(dataSet); - Object codes = pair.left; - Comparable[] codeValues = pair.right; - return Pair.of(representation.permute(codes, sources), codeValues); - } - - public Object getObject(Object dataSet, int ordinal) { - final Pair<Object, Comparable[]> pair = toPair(dataSet); - int code = representation.getInt(pair.left, ordinal); - return pair.right[code]; - } - - public int getInt(Object dataSet, int ordinal) { - return ((Number) getObject(dataSet, ordinal)).intValue(); - } - - public int size(Object dataSet) { - final Pair<Object, Comparable[]> pair = toPair(dataSet); - return representation.size(pair.left); - } - - public String toString(Object dataSet) { - return Column.asList(this, dataSet).toString(); - } - } - - /** Representation that stores string column values. */ - public static class StringDictionary implements Representation { - public StringDictionary() { - } - - @Override public String toString() { - return "StringDictionary()"; - } - - public RepresentationType getType() { - return RepresentationType.STRING_DICTIONARY; - } - - public Object freeze(ColumnLoader.ValueSet valueSet, int[] sources) { - throw new UnsupportedOperationException(); // TODO: - } - - public Object permute(Object dataSet, int[] sources) { - throw new UnsupportedOperationException(); // TODO: - } - - public Object getObject(Object dataSet, int ordinal) { - throw new UnsupportedOperationException(); // TODO: - } - - public int getInt(Object dataSet, int ordinal) { - throw new UnsupportedOperationException(); // TODO: - } - - public int size(Object dataSet) { - throw new UnsupportedOperationException(); // TODO: - } - - public String toString(Object dataSet) { - return Column.asList(this, dataSet).toString(); - } - } - - /** Representation that stores byte-string column values. */ - public static class ByteStringDictionary implements Representation { - public ByteStringDictionary() { - } - - public String toString() { - return "ByteStringDictionary()"; - } - - public RepresentationType getType() { - return RepresentationType.BYTE_STRING_DICTIONARY; - } - - public Object freeze(ColumnLoader.ValueSet valueSet, int[] sources) { - throw new UnsupportedOperationException(); // TODO: - } - - public Object permute(Object dataSet, int[] sources) { - throw new UnsupportedOperationException(); // TODO: - } - - public Object getObject(Object dataSet, int ordinal) { - throw new UnsupportedOperationException(); // TODO: - } - - public int getInt(Object dataSet, int ordinal) { - throw new UnsupportedOperationException(); // TODO: - } - - public int size(Object dataSet) { - throw new UnsupportedOperationException(); // TODO: - } - - public String toString(Object dataSet) { - return Column.asList(this, dataSet).toString(); - } - } - - /** Representation of a column that has the same value for every row. */ - public static class Constant implements Representation { - final int ordinal; - - public Constant(int ordinal) { - this.ordinal = ordinal; - } - - public String toString() { - return "Constant(ordinal=" + ordinal + ")"; - } - - public RepresentationType getType() { - return RepresentationType.CONSTANT; - } - - public Object freeze(ColumnLoader.ValueSet valueSet, int[] sources) { - final int size = valueSet.values.size(); - return Pair.of(size == 0 ? null : valueSet.values.get(0), size); - } - - public Object permute(Object dataSet, int[] sources) { - return dataSet; - } - - public Object getObject(Object dataSet, int ordinal) { - Pair<Object, Integer> pair = toPair(dataSet); - return pair.left; - } - - public int getInt(Object dataSet, int ordinal) { - Pair<Object, Integer> pair = toPair(dataSet); - return ((Number) pair.left).intValue(); - } - - public int size(Object dataSet) { - Pair<Object, Integer> pair = toPair(dataSet); - return pair.right; - } - - public String toString(Object dataSet) { - Pair<Object, Integer> pair = toPair(dataSet); - return Collections.nCopies(pair.right, pair.left).toString(); - } - } - - /** Representation that stores numeric values in a bit-sliced - * array. Each value does not necessarily occupy 8, 16, 32 or 64 - * bits (the number of bits used by the built-in types). This - * representation is often used to store the value codes for a - * dictionary-based representation. */ - public static class BitSlicedPrimitiveArray implements Representation { - final int ordinal; - final int bitCount; - final Primitive primitive; - final boolean signed; - - BitSlicedPrimitiveArray( - int ordinal, int bitCount, Primitive primitive, boolean signed) { - assert bitCount > 0; - this.ordinal = ordinal; - this.bitCount = bitCount; - this.primitive = primitive; - this.signed = signed; - } - - @Override public String toString() { - return "BitSlicedPrimitiveArray(ordinal=" + ordinal - + ", bitCount=" + bitCount - + ", primitive=" + primitive - + ", signed=" + signed + ")"; - } - - public RepresentationType getType() { - return RepresentationType.BIT_SLICED_PRIMITIVE_ARRAY; - } - - public Object freeze(ColumnLoader.ValueSet valueSet, int[] sources) { - final int chunksPerWord = 64 / bitCount; - final List<Comparable> valueList = - permuteList(valueSet.values, sources); - final int valueCount = valueList.size(); - final int wordCount = - (valueCount + (chunksPerWord - 1)) / chunksPerWord; - final int remainingChunkCount = valueCount % chunksPerWord; - final long[] longs = new long[wordCount]; - final int n = valueCount / chunksPerWord; - int i; - int k = 0; - if (valueCount > 0 - && valueList.get(0) instanceof Boolean) { - @SuppressWarnings("unchecked") - final List<Boolean> booleans = (List) valueList; - for (i = 0; i < n; i++) { - long v = 0; - for (int j = 0; j < chunksPerWord; j++) { - v |= booleans.get(k++) ? (1 << (bitCount * j)) : 0; - } - longs[i] = v; - } - if (remainingChunkCount > 0) { - long v = 0; - for (int j = 0; j < remainingChunkCount; j++) { - v |= booleans.get(k++) ? (1 << (bitCount * j)) : 0; - } - longs[i] = v; - } - } else { - @SuppressWarnings("unchecked") - final List<Number> numbers = (List) valueList; - for (i = 0; i < n; i++) { - long v = 0; - for (int j = 0; j < chunksPerWord; j++) { - v |= numbers.get(k++).longValue() << (bitCount * j); - } - longs[i] = v; - } - if (remainingChunkCount > 0) { - long v = 0; - for (int j = 0; j < remainingChunkCount; j++) { - v |= numbers.get(k++).longValue() << (bitCount * j); - } - longs[i] = v; - } - } - return longs; - } - - public Object permute(Object dataSet, int[] sources) { - final long[] longs0 = (long[]) dataSet; - int n = sources.length; - final long[] longs = new long[longs0.length]; - for (int i = 0; i < n; i++) { - orLong( - bitCount, longs, i, - getLong(bitCount, longs0, sources[i])); - } - return longs; - } - - public Object getObject(Object dataSet, int ordinal) { - final long[] longs = (long[]) dataSet; - final int chunksPerWord = 64 / bitCount; - final int word = ordinal / chunksPerWord; - final long v = longs[word]; - final int chunk = ordinal % chunksPerWord; - final int mask = (1 << bitCount) - 1; - final int signMask = 1 << (bitCount - 1); - final int shift = chunk * bitCount; - final long w = v >> shift; - long x = w & mask; - if (signed && (x & signMask) != 0) { - x = -x; - } - switch (primitive) { - case BOOLEAN: - return x != 0; - case BYTE: - return (byte) x; - case CHAR: - return (char) x; - case SHORT: - return (short) x; - case INT: - return (int) x; - case LONG: - return x; - default: - throw new AssertionError(primitive + " unexpected"); - } - } - - public int getInt(Object dataSet, int ordinal) { - final long[] longs = (long[]) dataSet; - final int chunksPerWord = 64 / bitCount; - final int word = ordinal / chunksPerWord; - final long v = longs[word]; - final int chunk = ordinal % chunksPerWord; - final int mask = (1 << bitCount) - 1; - final int signMask = 1 << (bitCount - 1); - final int shift = chunk * bitCount; - final long w = v >> shift; - long x = w & mask; - if (signed && (x & signMask) != 0) { - x = -x; - } - return (int) x; - } - - public static long getLong(int bitCount, long[] values, int ordinal) { - return getLong( - bitCount, 64 / bitCount, (1L << bitCount) - 1L, - values, ordinal); - } - - public static long getLong( - int bitCount, - int chunksPerWord, - long mask, - long[] values, - int ordinal) { - final int word = ordinal / chunksPerWord; - final int chunk = ordinal % chunksPerWord; - final long value = values[word]; - final int shift = chunk * bitCount; - return (value >> shift) & mask; - } - - public static void orLong( - int bitCount, long[] values, int ordinal, long value) { - orLong(bitCount, 64 / bitCount, values, ordinal, value); - } - - public static void orLong( - int bitCount, int chunksPerWord, long[] values, int ordinal, - long value) { - final int word = ordinal / chunksPerWord; - final int chunk = ordinal % chunksPerWord; - final int shift = chunk * bitCount; - values[word] |= value << shift; - } - - public int size(Object dataSet) { - final long[] longs = (long[]) dataSet; - final int chunksPerWord = 64 / bitCount; - return longs.length * chunksPerWord; // may be slightly too high - } - - public String toString(Object dataSet) { - return Column.asList(this, dataSet).toString(); - } - } - - private static <E> List<E> permuteList( - final List<E> list, final int[] sources) { - if (sources == null) { - return list; - } - return new AbstractList<E>() { - public E get(int index) { - return list.get(sources[index]); - } - - public int size() { - return list.size(); - } - }; - } - - /** Contents of a table. */ - public static class Content { - private final List<Column> columns; - private final int size; - private final ImmutableList<RelCollation> collations; - - public Content(List<? extends Column> columns, int size, - Iterable<? extends RelCollation> collations) { - this.columns = ImmutableList.copyOf(columns); - this.size = size; - this.collations = ImmutableList.copyOf(collations); - } - - @Deprecated // to be removed before 2.0 - public Content(List<? extends Column> columns, int size, int sortField) { - this(columns, size, - sortField >= 0 - ? RelCollations.createSingleton(sortField) - : ImmutableList.<RelCollation>of()); - } - - @SuppressWarnings("unchecked") - public <T> Enumerator<T> enumerator() { - if (columns.size() == 1) { - return (Enumerator<T>) new ObjectEnumerator(size, columns.get(0)); - } else { - return (Enumerator<T>) new ArrayEnumerator(size, columns); - } - } - - public Enumerator<Object[]> arrayEnumerator() { - return new ArrayEnumerator(size, columns); - } - - /** Enumerator over a table with a single column; each element - * returned is an object. */ - private static class ObjectEnumerator implements Enumerator<Object> { - final int rowCount; - final Object dataSet; - final Representation representation; - int i = -1; - - public ObjectEnumerator(int rowCount, Column column) { - this.rowCount = rowCount; - this.dataSet = column.dataSet; - this.representation = column.representation; - } - - public Object current() { - return representation.getObject(dataSet, i); - } - - public boolean moveNext() { - return ++i < rowCount; - } - - public void reset() { - i = -1; - } - - public void close() { - } - } - - /** Enumerator over a table with more than one column; each element - * returned is an array. */ - private static class ArrayEnumerator implements Enumerator<Object[]> { - final int rowCount; - final List<Column> columns; - int i = -1; - - public ArrayEnumerator(int rowCount, List<Column> columns) { - this.rowCount = rowCount; - this.columns = columns; - } - - public Object[] current() { - Object[] objects = new Object[columns.size()]; - for (int j = 0; j < objects.length; j++) { - final Column pair = columns.get(j); - objects[j] = pair.representation.getObject(pair.dataSet, i); - } - return objects; - } - - public boolean moveNext() { - return ++i < rowCount; - } - - public void reset() { - i = -1; - } - - public void close() { - } - } - } -} - -// End ArrayTable.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/clone/CloneSchema.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/clone/CloneSchema.java b/core/src/main/java/org/apache/calcite/adapter/clone/CloneSchema.java deleted file mode 100644 index afd3798..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/clone/CloneSchema.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * 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.calcite.adapter.clone; - -import org.apache.calcite.adapter.java.JavaTypeFactory; -import org.apache.calcite.adapter.jdbc.JdbcSchema; -import org.apache.calcite.avatica.ColumnMetaData; -import org.apache.calcite.jdbc.CalciteConnection; -import org.apache.calcite.linq4j.Enumerable; -import org.apache.calcite.linq4j.QueryProvider; -import org.apache.calcite.linq4j.Queryable; -import org.apache.calcite.rel.RelCollation; -import org.apache.calcite.rel.RelCollations; -import org.apache.calcite.rel.type.RelProtoDataType; -import org.apache.calcite.schema.QueryableTable; -import org.apache.calcite.schema.Schema; -import org.apache.calcite.schema.SchemaFactory; -import org.apache.calcite.schema.SchemaPlus; -import org.apache.calcite.schema.Schemas; -import org.apache.calcite.schema.Table; -import org.apache.calcite.schema.impl.AbstractSchema; - -import com.google.common.base.Supplier; -import com.google.common.base.Suppliers; -import com.google.common.collect.ImmutableList; - -import java.lang.reflect.Type; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import static org.apache.calcite.schema.impl.MaterializedViewTable.MATERIALIZATION_CONNECTION; - -/** - * Schema that contains in-memory copies of tables from a JDBC schema. - */ -public class CloneSchema extends AbstractSchema { - // TODO: implement 'driver' property - // TODO: implement 'source' property - // TODO: test Factory - - private final SchemaPlus sourceSchema; - - /** - * Creates a CloneSchema. - * - * @param sourceSchema JDBC data source - */ - public CloneSchema(SchemaPlus sourceSchema) { - super(); - this.sourceSchema = sourceSchema; - } - - @Override protected Map<String, Table> getTableMap() { - final Map<String, Table> map = new LinkedHashMap<>(); - for (String name : sourceSchema.getTableNames()) { - final Table table = sourceSchema.getTable(name); - if (table instanceof QueryableTable) { - final QueryableTable sourceTable = (QueryableTable) table; - map.put(name, - createCloneTable(MATERIALIZATION_CONNECTION, sourceTable, name)); - } - } - return map; - } - - private Table createCloneTable(QueryProvider queryProvider, - QueryableTable sourceTable, String name) { - final Queryable<Object> queryable = - sourceTable.asQueryable(queryProvider, sourceSchema, name); - final JavaTypeFactory typeFactory = - ((CalciteConnection) queryProvider).getTypeFactory(); - return createCloneTable(typeFactory, Schemas.proto(sourceTable), - ImmutableList.<RelCollation>of(), null, queryable); - } - - @Deprecated // to be removed before 2.0 - public static <T> Table createCloneTable(final JavaTypeFactory typeFactory, - final RelProtoDataType protoRowType, - final List<ColumnMetaData.Rep> repList, - final Enumerable<T> source) { - return createCloneTable(typeFactory, protoRowType, - ImmutableList.<RelCollation>of(), repList, source); - } - - public static <T> Table createCloneTable(final JavaTypeFactory typeFactory, - final RelProtoDataType protoRowType, final List<RelCollation> collations, - final List<ColumnMetaData.Rep> repList, final Enumerable<T> source) { - final Type elementType; - if (source instanceof QueryableTable) { - elementType = ((QueryableTable) source).getElementType(); - } else if (protoRowType.apply(typeFactory).getFieldCount() == 1) { - if (repList != null) { - elementType = repList.get(0).clazz; - } else { - elementType = Object.class; - } - } else { - elementType = Object[].class; - } - return new ArrayTable( - elementType, - protoRowType, - Suppliers.memoize( - new Supplier<ArrayTable.Content>() { - public ArrayTable.Content get() { - final ColumnLoader loader = - new ColumnLoader<>(typeFactory, source, protoRowType, - repList); - final List<RelCollation> collation2 = - collations.isEmpty() - && loader.sortField >= 0 - ? RelCollations.createSingleton(loader.sortField) - : collations; - return new ArrayTable.Content(loader.representationValues, - loader.size(), collation2); - } - })); - } - - /** Schema factory that creates a - * {@link org.apache.calcite.adapter.clone.CloneSchema}. - * This allows you to create a clone schema inside a model.json file. - * - * <pre>{@code - * { - * version: '1.0', - * defaultSchema: 'FOODMART_CLONE', - * schemas: [ - * { - * name: 'FOODMART_CLONE', - * type: 'custom', - * factory: 'org.apache.calcite.adapter.clone.CloneSchema$Factory', - * operand: { - * jdbcDriver: 'com.mysql.jdbc.Driver', - * jdbcUrl: 'jdbc:mysql://localhost/foodmart', - * jdbcUser: 'foodmart', - * jdbcPassword: 'foodmart' - * } - * } - * ] - * } - * }</pre> - */ - public static class Factory implements SchemaFactory { - public Schema create( - SchemaPlus parentSchema, - String name, - Map<String, Object> operand) { - SchemaPlus schema = - parentSchema.add(name, - JdbcSchema.create(parentSchema, name + "$source", operand)); - return new CloneSchema(schema); - } - } -} - -// End CloneSchema.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/clone/ColumnLoader.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/clone/ColumnLoader.java b/core/src/main/java/org/apache/calcite/adapter/clone/ColumnLoader.java deleted file mode 100644 index d33ee61..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/clone/ColumnLoader.java +++ /dev/null @@ -1,476 +0,0 @@ -/* - * 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.calcite.adapter.clone; - -import org.apache.calcite.adapter.java.JavaTypeFactory; -import org.apache.calcite.avatica.ColumnMetaData; -import org.apache.calcite.avatica.util.DateTimeUtils; -import org.apache.calcite.linq4j.Enumerable; -import org.apache.calcite.linq4j.Ord; -import org.apache.calcite.linq4j.tree.Primitive; -import org.apache.calcite.rel.type.RelDataType; -import org.apache.calcite.rel.type.RelDataTypeField; -import org.apache.calcite.rel.type.RelProtoDataType; - -import com.google.common.base.Function; -import com.google.common.collect.Lists; - -import java.lang.reflect.Type; -import java.sql.Date; -import java.sql.Time; -import java.sql.Timestamp; -import java.util.AbstractList; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Column loader. - * - * @param <T> Element type of source table - */ -class ColumnLoader<T> { - static final int[] INT_B = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000}; - static final int[] INT_S = {1, 2, 4, 8, 16}; - static final long[] LONG_B = { - 0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000, 0xFFFFFFFF00000000L}; - static final int[] LONG_S = {1, 2, 4, 8, 16, 32}; - - private static final Function<Timestamp, Long> TIMESTAMP_TO_LONG = - new Function<Timestamp, Long>() { - public Long apply(Timestamp a0) { - return a0 == null ? null : a0.getTime(); - } - }; - - private static final Function<Time, Integer> TIME_TO_INT = - new Function<Time, Integer>() { - public Integer apply(Time a0) { - return a0 == null - ? null - : (int) (a0.getTime() % DateTimeUtils.MILLIS_PER_DAY); - } - }; - - private static final Function<Date, Integer> DATE_TO_INT = - new Function<Date, Integer>() { - public Integer apply(Date a0) { - return a0 == null - ? null - : (int) (a0.getTime() / DateTimeUtils.MILLIS_PER_DAY); - } - }; - - public final List<T> list = new ArrayList<>(); - public final List<ArrayTable.Column> representationValues = new ArrayList<>(); - private final JavaTypeFactory typeFactory; - public final int sortField; - - /** Creates a column loader, and performs the load. - * - * @param typeFactory Type factory - * @param sourceTable Source data - * @param protoRowType Logical row type - * @param repList Physical row types, or null if not known */ - ColumnLoader(JavaTypeFactory typeFactory, - Enumerable<T> sourceTable, - RelProtoDataType protoRowType, - List<ColumnMetaData.Rep> repList) { - this.typeFactory = typeFactory; - final RelDataType rowType = protoRowType.apply(typeFactory); - if (repList == null) { - repList = - Collections.nCopies(rowType.getFieldCount(), - ColumnMetaData.Rep.OBJECT); - } - sourceTable.into(list); - final int[] sorts = {-1}; - load(rowType, repList, sorts); - this.sortField = sorts[0]; - } - - static int nextPowerOf2(int v) { - v--; - v |= v >>> 1; - v |= v >>> 2; - v |= v >>> 4; - v |= v >>> 8; - v |= v >>> 16; - v++; - return v; - } - - static long nextPowerOf2(long v) { - v--; - v |= v >>> 1; - v |= v >>> 2; - v |= v >>> 4; - v |= v >>> 8; - v |= v >>> 16; - v |= v >>> 32; - v++; - return v; - } - - static int log2(int v) { - int r = 0; - for (int i = 4; i >= 0; i--) { - if ((v & INT_B[i]) != 0) { - v >>= INT_S[i]; - r |= INT_S[i]; - } - } - return r; - } - - static int log2(long v) { - int r = 0; - for (int i = 5; i >= 0; i--) { - if ((v & LONG_B[i]) != 0) { - v >>= LONG_S[i]; - r |= LONG_S[i]; - } - } - return r; - } - - static int[] invert(int[] targets) { - final int[] sources = new int[targets.length]; - for (int i = 0; i < targets.length; i++) { - sources[targets[i]] = i; - } - return sources; - } - - static boolean isIdentity(int[] sources) { - for (int i = 0; i < sources.length; i++) { - if (sources[i] != i) { - return false; - } - } - return true; - } - - public int size() { - return list.size(); - } - - private void load(final RelDataType elementType, - List<ColumnMetaData.Rep> repList, int[] sort) { - final List<Type> types = - new AbstractList<Type>() { - final List<RelDataTypeField> fields = - elementType.getFieldList(); - public Type get(int index) { - return typeFactory.getJavaClass( - fields.get(index).getType()); - } - - public int size() { - return fields.size(); - } - }; - int[] sources = null; - for (final Ord<Type> pair : Ord.zip(types)) { - @SuppressWarnings("unchecked") - final List<?> sliceList = - types.size() == 1 - ? list - : new AbstractList<Object>() { - final int slice = pair.i; - - public Object get(int index) { - return ((Object[]) list.get(index))[slice]; - } - - public int size() { - return list.size(); - } - }; - final List<?> list2 = - wrap( - repList.get(pair.i), - sliceList, - elementType.getFieldList().get(pair.i).getType()); - final Class clazz = pair.e instanceof Class - ? (Class) pair.e - : Object.class; - ValueSet valueSet = new ValueSet(clazz); - for (Object o : list2) { - valueSet.add((Comparable) o); - } - if (sort != null - && sort[0] < 0 - && valueSet.map.keySet().size() == list.size()) { - // We have discovered a the first unique key in the table. - sort[0] = pair.i; - final Comparable[] values = - valueSet.values.toArray(new Comparable[list.size()]); - final Kev[] kevs = new Kev[list.size()]; - for (int i = 0; i < kevs.length; i++) { - kevs[i] = new Kev(i, values[i]); - } - Arrays.sort(kevs); - sources = new int[list.size()]; - for (int i = 0; i < sources.length; i++) { - sources[i] = kevs[i].source; - } - - if (isIdentity(sources)) { - // Table was already sorted. Clear the permutation. - // We've already set sort[0], so we won't check for another - // sorted column. - sources = null; - } else { - // Re-sort all previous columns. - for (int i = 0; i < pair.i; i++) { - representationValues.set( - i, representationValues.get(i).permute(sources)); - } - } - } - representationValues.add(valueSet.freeze(pair.i, sources)); - } - } - - /** Adapt for some types that we represent differently internally than their - * JDBC types. {@link java.sql.Timestamp} values that are not null are - * converted to {@code long}, but nullable timestamps are acquired using - * {@link java.sql.ResultSet#getObject(int)} and therefore the Timestamp - * value needs to be converted to a {@link Long}. Similarly - * {@link java.sql.Date} and {@link java.sql.Time} values to - * {@link Integer}. */ - private static List wrap(ColumnMetaData.Rep rep, List list, - RelDataType type) { - switch (type.getSqlTypeName()) { - case TIMESTAMP: - switch (rep) { - case OBJECT: - case JAVA_SQL_TIMESTAMP: - return Lists.transform(list, TIMESTAMP_TO_LONG); - } - break; - case TIME: - switch (rep) { - case OBJECT: - case JAVA_SQL_TIME: - return Lists.transform(list, TIME_TO_INT); - } - break; - case DATE: - switch (rep) { - case OBJECT: - case JAVA_SQL_DATE: - return Lists.transform(list, DATE_TO_INT); - } - break; - } - return list; - } - - /** - * Set of values of a column, created during the load process, and converted - * to a serializable (and more compact) form before load completes. - */ - static class ValueSet { - final Class clazz; - final Map<Comparable, Comparable> map = new HashMap<>(); - final List<Comparable> values = new ArrayList<>(); - Comparable min; - Comparable max; - boolean containsNull; - - ValueSet(Class clazz) { - this.clazz = clazz; - } - - void add(Comparable e) { - if (e != null) { - final Comparable old = e; - e = map.get(e); - if (e == null) { - e = old; - map.put(e, e); - //noinspection unchecked - if (min == null || min.compareTo(e) > 0) { - min = e; - } - //noinspection unchecked - if (max == null || max.compareTo(e) < 0) { - max = e; - } - } - } else { - containsNull = true; - } - values.add(e); - } - - /** Freezes the contents of this value set into a column, optionally - * re-ordering if {@code sources} is specified. */ - ArrayTable.Column freeze(int ordinal, int[] sources) { - ArrayTable.Representation representation = chooseRep(ordinal); - final int cardinality = map.size() + (containsNull ? 1 : 0); - final Object data = representation.freeze(this, sources); - return new ArrayTable.Column(representation, data, cardinality); - } - - ArrayTable.Representation chooseRep(int ordinal) { - Primitive primitive = Primitive.of(clazz); - Primitive boxPrimitive = Primitive.ofBox(clazz); - Primitive p = primitive != null ? primitive : boxPrimitive; - if (!containsNull && p != null) { - switch (p) { - case FLOAT: - case DOUBLE: - return new ArrayTable.PrimitiveArray(ordinal, p, p); - case OTHER: - case VOID: - throw new AssertionError("wtf?!"); - } - if (canBeLong(min) && canBeLong(max)) { - return chooseFixedRep( - ordinal, p, toLong(min), toLong(max)); - } - } - - // We don't want to use a dictionary if: - // (a) there are so many values that an object pointer (with one - // indirection) has about as many bits as a code (with two - // indirections); or - // (b) if there are very few copies of each value. - // The condition kind of captures this, but needs to be tuned. - final int codeCount = map.size() + (containsNull ? 1 : 0); - final int codeBitCount = log2(nextPowerOf2(codeCount)); - if (codeBitCount < 10 && values.size() > 2000) { - final ArrayTable.Representation representation = - chooseFixedRep(-1, Primitive.INT, 0, codeCount - 1); - return new ArrayTable.ObjectDictionary(ordinal, representation); - } - return new ArrayTable.ObjectArray(ordinal); - } - - private long toLong(Object o) { - // We treat Boolean and Character as if they were subclasses of - // Number but actually they are not. - if (o instanceof Boolean) { - return (Boolean) o ? 1 : 0; - } else if (o instanceof Character) { - return (long) (Character) o; - } else { - return ((Number) o).longValue(); - } - } - - private boolean canBeLong(Object o) { - return o instanceof Boolean - || o instanceof Character - || o instanceof Number; - } - - /** Chooses a representation for a fixed-precision primitive type - * (boolean, byte, char, short, int, long). - * - * @param ordinal Ordinal of this column in table - * @param p Type that values are to be returned as (not necessarily the - * same as they will be stored) - * @param min Minimum value to be encoded - * @param max Maximum value to be encoded (inclusive) - */ - private ArrayTable.Representation chooseFixedRep( - int ordinal, Primitive p, long min, long max) { - if (min == max) { - return new ArrayTable.Constant(ordinal); - } - final int bitCountMax = log2(nextPowerOf2(abs2(max) + 1)); - int bitCount; // 1 for sign - boolean signed; - - if (min >= 0) { - signed = false; - bitCount = bitCountMax; - } else { - signed = true; - int bitCountMin = log2(nextPowerOf2(abs2(min) + 1)); - bitCount = Math.max(bitCountMin, bitCountMax) + 1; - } - - // Must be a fixed point primitive. - if (bitCount > 21 && bitCount < 32) { - // Can't get more than 2 into a word. - signed = true; - bitCount = 32; - } - if (bitCount >= 33 && bitCount < 64) { - // Can't get more than one into a word. - signed = true; - bitCount = 64; - } - if (signed) { - switch (bitCount) { - case 8: - return new ArrayTable.PrimitiveArray( - ordinal, Primitive.BYTE, p); - case 16: - return new ArrayTable.PrimitiveArray( - ordinal, Primitive.SHORT, p); - case 32: - return new ArrayTable.PrimitiveArray( - ordinal, Primitive.INT, p); - case 64: - return new ArrayTable.PrimitiveArray( - ordinal, Primitive.LONG, p); - } - } - return new ArrayTable.BitSlicedPrimitiveArray( - ordinal, bitCount, p, signed); - } - - /** Two's complement absolute on int value. */ - private static int abs2(int v) { - // -128 becomes +127 - return v < 0 ? ~v : v; - } - - /** Two's complement absolute on long value. */ - private static long abs2(long v) { - // -128 becomes +127 - return v < 0 ? ~v : v; - } - } - - /** Key-value pair. */ - private static class Kev implements Comparable<Kev> { - private final int source; - private final Comparable key; - - public Kev(int source, Comparable key) { - this.source = source; - this.key = key; - } - - public int compareTo(Kev o) { - //noinspection unchecked - return key.compareTo(o.key); - } - } -} - -// End ColumnLoader.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/clone/ListTable.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/clone/ListTable.java b/core/src/main/java/org/apache/calcite/adapter/clone/ListTable.java deleted file mode 100644 index e75fb07..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/clone/ListTable.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * 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.calcite.adapter.clone; - -import org.apache.calcite.adapter.java.AbstractQueryableTable; -import org.apache.calcite.linq4j.AbstractQueryable; -import org.apache.calcite.linq4j.Enumerator; -import org.apache.calcite.linq4j.Linq4j; -import org.apache.calcite.linq4j.QueryProvider; -import org.apache.calcite.linq4j.Queryable; -import org.apache.calcite.linq4j.tree.Expression; -import org.apache.calcite.rel.type.RelDataType; -import org.apache.calcite.rel.type.RelDataTypeFactory; -import org.apache.calcite.rel.type.RelProtoDataType; -import org.apache.calcite.schema.SchemaPlus; -import org.apache.calcite.schema.Statistic; -import org.apache.calcite.schema.Statistics; -import org.apache.calcite.util.ImmutableBitSet; - -import com.google.common.collect.ImmutableList; - -import java.lang.reflect.Type; -import java.util.Iterator; -import java.util.List; - -/** - * Implementation of table that reads rows from a read-only list and returns - * an enumerator of rows. Each row is object (if there is just one column) or - * an object array (if there are multiple columns). - */ -class ListTable extends AbstractQueryableTable { - private final RelProtoDataType protoRowType; - private final Expression expression; - private final List list; - - /** Creates a ListTable. */ - public ListTable( - Type elementType, - RelProtoDataType protoRowType, - Expression expression, - List list) { - super(elementType); - this.protoRowType = protoRowType; - this.expression = expression; - this.list = list; - } - - public RelDataType getRowType(RelDataTypeFactory typeFactory) { - return protoRowType.apply(typeFactory); - } - - public Statistic getStatistic() { - return Statistics.of(list.size(), ImmutableList.<ImmutableBitSet>of()); - } - - public <T> Queryable<T> asQueryable(final QueryProvider queryProvider, - SchemaPlus schema, String tableName) { - return new AbstractQueryable<T>() { - public Type getElementType() { - return elementType; - } - - public Expression getExpression() { - return expression; - } - - public QueryProvider getProvider() { - return queryProvider; - } - - public Iterator<T> iterator() { - //noinspection unchecked - return list.iterator(); - } - - public Enumerator<T> enumerator() { - //noinspection unchecked - return Linq4j.enumerator(list); - } - }; - } -} - -// End ListTable.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/clone/package-info.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/clone/package-info.java b/core/src/main/java/org/apache/calcite/adapter/clone/package-info.java deleted file mode 100644 index e7f4b89..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/clone/package-info.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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. - */ - -/** - * Provides utility classes. - */ -@PackageMarker -package org.apache.calcite.adapter.clone; - -import org.apache.calcite.avatica.util.PackageMarker; - -// End package-info.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/AggAddContext.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/AggAddContext.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/AggAddContext.java deleted file mode 100644 index 4da0447..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/AggAddContext.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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.calcite.adapter.enumerable; - -import org.apache.calcite.linq4j.tree.Expression; -import org.apache.calcite.rex.RexNode; - -import java.util.List; - -/** - * Information for a call to - * {@link org.apache.calcite.adapter.enumerable.AggImplementor#implementAdd(AggContext, AggAddContext)}. - * - * <p>Typically, the aggregation implementation will use {@link #arguments()} - * or {@link #rexArguments()} to update aggregate value. - */ -public interface AggAddContext extends AggResultContext { - /** - * Returns {@link org.apache.calcite.rex.RexNode} representation of arguments. - * This can be useful for manual translation of required arguments with - * different {@link NullPolicy}. - * @return {@link org.apache.calcite.rex.RexNode} representation of arguments - */ - List<RexNode> rexArguments(); - - /** - * Returns {@link org.apache.calcite.rex.RexNode} representation of the - * filter, or null. - */ - RexNode rexFilterArgument(); - - /** - * Returns Linq4j form of arguments. - * The resulting value is equivalent to - * {@code rowTranslator().translateList(rexArguments())}. - * This is handy if you need just operate on argument. - * @return Linq4j form of arguments. - */ - List<Expression> arguments(); - - /** - * Returns a - * {@link org.apache.calcite.adapter.enumerable.RexToLixTranslator} - * suitable to transform the arguments. - * - * @return {@link RexToLixTranslator} suitable to transform the arguments - */ - RexToLixTranslator rowTranslator(); -} - -// End AggAddContext.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/AggContext.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/AggContext.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/AggContext.java deleted file mode 100644 index 542e3a2..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/AggContext.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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.calcite.adapter.enumerable; - -import org.apache.calcite.rel.type.RelDataType; -import org.apache.calcite.sql.SqlAggFunction; - -import java.lang.reflect.Type; -import java.util.List; - -/** - * Information on the aggregate calculation context. - * {@link AggAddContext} provides basic static information on types of arguments - * and the return value of the aggregate being implemented. - */ -public interface AggContext { - /** - * Returns the aggregation being implemented. - * @return aggregation being implemented. - */ - SqlAggFunction aggregation(); - - /** - * Returns the return type of the aggregate as - * {@link org.apache.calcite.rel.type.RelDataType}. - * This can be helpful to test - * {@link org.apache.calcite.rel.type.RelDataType#isNullable()}. - * - * @return return type of the aggregate - */ - RelDataType returnRelType(); - - /** - * Returns the return type of the aggregate as {@link java.lang.reflect.Type}. - * @return return type of the aggregate as {@link java.lang.reflect.Type} - */ - Type returnType(); - - /** - * Returns the parameter types of the aggregate as - * {@link org.apache.calcite.rel.type.RelDataType}. - * This can be helpful to test - * {@link org.apache.calcite.rel.type.RelDataType#isNullable()}. - * - * @return Parameter types of the aggregate - */ - List<? extends RelDataType> parameterRelTypes(); - - /** - * Returns the parameter types of the aggregate as - * {@link java.lang.reflect.Type}. - * - * @return Parameter types of the aggregate - */ - List<? extends Type> parameterTypes(); -} - -// End AggContext.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/AggImpState.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/AggImpState.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/AggImpState.java deleted file mode 100644 index b100cec..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/AggImpState.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.calcite.adapter.enumerable; - -import org.apache.calcite.linq4j.tree.Expression; -import org.apache.calcite.rel.core.AggregateCall; - -import java.util.List; - -/** - * Represents internal state when implementing aggregate functions. - */ -public class AggImpState { - public final int aggIdx; - public final AggregateCall call; - public final AggImplementor implementor; - public AggContext context; - public Expression result; - public List<Expression> state; - - public AggImpState(int aggIdx, AggregateCall call, boolean windowContext) { - this.aggIdx = aggIdx; - this.call = call; - this.implementor = - RexImpTable.INSTANCE.get(call.getAggregation(), windowContext); - if (implementor == null) { - throw new IllegalArgumentException( - "Unable to get aggregate implementation for aggregate " - + call.getAggregation() - + (windowContext ? " in window context" : "")); - } - } -} - -// End AggImpState.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/AggImplementor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/AggImplementor.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/AggImplementor.java deleted file mode 100644 index 9c18376..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/AggImplementor.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * 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.calcite.adapter.enumerable; - -import org.apache.calcite.linq4j.tree.Expression; - -import java.lang.reflect.Type; -import java.util.List; - -/** - * Implements an aggregate function by generating expressions to - * initialize, add to, and get a result from, an accumulator. - * - * @see org.apache.calcite.adapter.enumerable.StrictAggImplementor - * @see org.apache.calcite.adapter.enumerable.StrictWinAggImplementor - * @see org.apache.calcite.adapter.enumerable.RexImpTable.CountImplementor - * @see org.apache.calcite.adapter.enumerable.RexImpTable.SumImplementor - */ -public interface AggImplementor { - /** - * Returns the types of the intermediate variables used by the aggregate - * implementation. - * - * <p>For instance, for "concatenate to string" this can be - * {@link java.lang.StringBuilder}. - * Calcite calls this method before all other {@code implement*} methods. - * - * @param info Aggregate context - * @return Types of the intermediate variables used by the aggregate - * implementation - */ - List<Type> getStateType(AggContext info); - - /** - * Implements reset of the intermediate variables to the initial state. - * {@link AggResetContext#accumulator()} should be used to reference - * the state variables. - * For instance, to zero the count, use the following code: - * - * <blockquote><code>reset.currentBlock().add(<br> - * Expressions.statement(<br> - * Expressions.assign(reset.accumulator().get(0),<br> - * Expressions.constant(0)));</code></blockquote> - * - * @param info Aggregate context - * @param reset Reset context - */ - void implementReset(AggContext info, AggResetContext reset); - - /** - * Updates intermediate values to account for the newly added value. - * {@link AggResetContext#accumulator()} should be used to reference - * the state variables. - * - * @param info Aggregate context - * @param add Add context - */ - void implementAdd(AggContext info, AggAddContext add); - - /** - * Calculates the resulting value based on the intermediate variables. - * Note: this method must NOT destroy the intermediate variables as - * calcite might reuse the state when calculating sliding aggregates. - * {@link AggResetContext#accumulator()} should be used to reference - * the state variables. - * - * @param info Aggregate context - * @param result Result context - * @return Expression that is a result of calculating final value of - * the aggregate being implemented - */ - Expression implementResult(AggContext info, AggResultContext result); -} - -// End AggImplementor.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/AggResetContext.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/AggResetContext.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/AggResetContext.java deleted file mode 100644 index b3efb21..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/AggResetContext.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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.calcite.adapter.enumerable; - -import org.apache.calcite.linq4j.tree.Expression; - -import java.util.List; - -/** - * Information for a call to - * {@link AggImplementor#implementReset(AggContext, AggResetContext)}. - * - * {@link AggResetContext} provides access to the accumulator variables - * that should be reset. - */ -public interface AggResetContext extends NestedBlockBuilder { - /** - * Returns accumulator variables that should be reset. - * There MUST be an assignment even if you just assign the default value. - * - * @return accumulator variables that should be reset or empty list when no - * accumulator variables are used by the aggregate implementation. - * - * @see AggImplementor#getStateType(org.apache.calcite.adapter.enumerable.AggContext) - */ - List<Expression> accumulator(); -} - -// End AggResetContext.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/AggResultContext.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/AggResultContext.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/AggResultContext.java deleted file mode 100644 index fe7ee06..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/AggResultContext.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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.calcite.adapter.enumerable; - -/** - * Information for a call to - * {@link AggImplementor#implementResult(AggContext, AggResultContext)} - * - * <p>Typically, the aggregation implementation will convert - * {@link #accumulator()} to the resulting value of the aggregation. The - * implementation MUST NOT destroy the contents of {@link #accumulator()}. - */ -public interface AggResultContext extends NestedBlockBuilder, AggResetContext { -} - -// End AggResultContext.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/CallImplementor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/CallImplementor.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/CallImplementor.java deleted file mode 100644 index a258082..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/CallImplementor.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.calcite.adapter.enumerable; - -import org.apache.calcite.linq4j.tree.Expression; -import org.apache.calcite.rex.RexCall; - -/** - * Implements a call via given translator. - * - * @see org.apache.calcite.schema.ScalarFunction - * @see org.apache.calcite.schema.TableFunction - * @see org.apache.calcite.adapter.enumerable.RexImpTable - */ -public interface CallImplementor { - /** - * Implements a call. - * - * @param translator Translator for the call - * @param call Call that should be implemented - * @param nullAs The desired mode of {@code null} translation - * @return Translated call - */ - Expression implement( - RexToLixTranslator translator, - RexCall call, - RexImpTable.NullAs nullAs); -} - -// End CallImplementor.java
