Repository: asterixdb Updated Branches: refs/heads/master 1c3dc6181 -> c9a398ccb
http://git-wip-us.apache.org/repos/asf/asterixdb/blob/c9a398cc/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AbstractBooleanConstructorEvaluator.java ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AbstractBooleanConstructorEvaluator.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AbstractBooleanConstructorEvaluator.java new file mode 100644 index 0000000..9b9bf71 --- /dev/null +++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AbstractBooleanConstructorEvaluator.java @@ -0,0 +1,103 @@ +/* + * 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.asterix.runtime.evaluators.constructors; + +import java.io.DataOutput; +import java.io.IOException; + +import org.apache.asterix.formats.nontagged.BinaryComparatorFactoryProvider; +import org.apache.asterix.formats.nontagged.SerializerDeserializerProvider; +import org.apache.asterix.om.base.ABoolean; +import org.apache.asterix.om.types.ATypeTag; +import org.apache.asterix.om.types.BuiltinType; +import org.apache.asterix.runtime.exceptions.InvalidDataFormatException; +import org.apache.asterix.runtime.exceptions.TypeMismatchException; +import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier; +import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator; +import org.apache.hyracks.api.dataflow.value.IBinaryComparator; +import org.apache.hyracks.api.dataflow.value.ISerializerDeserializer; +import org.apache.hyracks.api.exceptions.HyracksDataException; +import org.apache.hyracks.data.std.api.IPointable; +import org.apache.hyracks.data.std.primitive.VoidPointable; +import org.apache.hyracks.data.std.util.ArrayBackedValueStorage; +import org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference; +import org.apache.hyracks.util.string.UTF8StringUtil; + +public abstract class AbstractBooleanConstructorEvaluator implements IScalarEvaluator { + @SuppressWarnings("unchecked") + protected static final ISerializerDeserializer<ABoolean> BOOLEAN_SERDE = + SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ABOOLEAN); + + protected static final IBinaryComparator UTF8_BINARY_CMP = + BinaryComparatorFactoryProvider.UTF8STRING_POINTABLE_INSTANCE.createBinaryComparator(); + + protected static final byte[] TRUE = UTF8StringUtil.writeStringToBytes("true"); + protected static final byte[] FALSE = UTF8StringUtil.writeStringToBytes("false"); + + protected final IScalarEvaluator inputEval; + protected final IPointable inputArg; + protected final ArrayBackedValueStorage resultStorage; + protected final DataOutput out; + + protected AbstractBooleanConstructorEvaluator(IScalarEvaluator inputEval) { + this.inputEval = inputEval; + inputArg = new VoidPointable(); + resultStorage = new ArrayBackedValueStorage(); + out = resultStorage.getDataOutput(); + } + + @Override + public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException { + try { + inputEval.evaluate(tuple, inputArg); + resultStorage.reset(); + evaluateImpl(result); + } catch (IOException e) { + throw new InvalidDataFormatException(getIdentifier(), e, ATypeTag.SERIALIZED_BOOLEAN_TYPE_TAG); + } + } + + protected void evaluateImpl(IPointable result) throws HyracksDataException { + byte[] bytes = inputArg.getByteArray(); + int startOffset = inputArg.getStartOffset(); + byte tt = bytes[startOffset]; + if (tt == ATypeTag.SERIALIZED_BOOLEAN_TYPE_TAG) { + result.set(inputArg); + } else if (tt == ATypeTag.SERIALIZED_STRING_TYPE_TAG) { + int len = inputArg.getLength(); + if (UTF8_BINARY_CMP.compare(bytes, startOffset + 1, len - 1, TRUE, 0, TRUE.length) == 0) { + setBoolean(result, true); + } else if (UTF8_BINARY_CMP.compare(bytes, startOffset + 1, len - 1, FALSE, 0, FALSE.length) == 0) { + setBoolean(result, false); + } else { + throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_BOOLEAN_TYPE_TAG); + } + } else { + throw new TypeMismatchException(getIdentifier(), 0, tt, ATypeTag.SERIALIZED_STRING_TYPE_TAG); + } + } + + protected void setBoolean(IPointable result, boolean value) throws HyracksDataException { + BOOLEAN_SERDE.serialize(ABoolean.valueOf(value), out); + result.set(resultStorage); + } + + protected abstract FunctionIdentifier getIdentifier(); +} http://git-wip-us.apache.org/repos/asf/asterixdb/blob/c9a398cc/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AbstractDoubleConstructorEvaluator.java ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AbstractDoubleConstructorEvaluator.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AbstractDoubleConstructorEvaluator.java new file mode 100644 index 0000000..332bfc2 --- /dev/null +++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AbstractDoubleConstructorEvaluator.java @@ -0,0 +1,122 @@ +/* + * 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.asterix.runtime.evaluators.constructors; + +import java.io.DataOutput; +import java.io.IOException; + +import org.apache.asterix.formats.nontagged.BinaryComparatorFactoryProvider; +import org.apache.asterix.formats.nontagged.SerializerDeserializerProvider; +import org.apache.asterix.om.base.ADouble; +import org.apache.asterix.om.base.AMutableDouble; +import org.apache.asterix.om.types.ATypeTag; +import org.apache.asterix.om.types.BuiltinType; +import org.apache.asterix.runtime.exceptions.InvalidDataFormatException; +import org.apache.asterix.runtime.exceptions.TypeMismatchException; +import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier; +import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator; +import org.apache.hyracks.api.dataflow.value.IBinaryComparator; +import org.apache.hyracks.api.dataflow.value.ISerializerDeserializer; +import org.apache.hyracks.api.exceptions.HyracksDataException; +import org.apache.hyracks.data.std.api.IPointable; +import org.apache.hyracks.data.std.primitive.UTF8StringPointable; +import org.apache.hyracks.data.std.primitive.VoidPointable; +import org.apache.hyracks.data.std.util.ArrayBackedValueStorage; +import org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference; +import org.apache.hyracks.util.string.UTF8StringUtil; + +public abstract class AbstractDoubleConstructorEvaluator implements IScalarEvaluator { + @SuppressWarnings("unchecked") + protected static final ISerializerDeserializer<ADouble> DOUBLE_SERDE = + SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADOUBLE); + + protected static final IBinaryComparator UTF8_BINARY_CMP = + BinaryComparatorFactoryProvider.UTF8STRING_POINTABLE_INSTANCE.createBinaryComparator(); + + protected static final byte[] POSITIVE_INF = UTF8StringUtil.writeStringToBytes("INF"); + protected static final byte[] NEGATIVE_INF = UTF8StringUtil.writeStringToBytes("-INF"); + protected static final byte[] NAN = UTF8StringUtil.writeStringToBytes("NaN"); + + protected final IScalarEvaluator inputEval; + protected final ArrayBackedValueStorage resultStorage; + protected final DataOutput out; + protected final IPointable inputArg; + protected final AMutableDouble aDouble; + protected final UTF8StringPointable utf8Ptr; + + protected AbstractDoubleConstructorEvaluator(IScalarEvaluator inputEval) { + this.inputEval = inputEval; + resultStorage = new ArrayBackedValueStorage(); + out = resultStorage.getDataOutput(); + inputArg = new VoidPointable(); + aDouble = new AMutableDouble(0); + utf8Ptr = new UTF8StringPointable(); + } + + @Override + public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException { + try { + inputEval.evaluate(tuple, inputArg); + resultStorage.reset(); + evaluateImpl(result); + } catch (IOException e) { + throw new InvalidDataFormatException(getIdentifier(), e, ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG); + } + } + + protected void evaluateImpl(IPointable result) throws IOException { + byte[] bytes = inputArg.getByteArray(); + int offset = inputArg.getStartOffset(); + byte tt = bytes[offset]; + if (tt == ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG) { + result.set(inputArg); + } else if (tt == ATypeTag.SERIALIZED_STRING_TYPE_TAG) { + int len = inputArg.getLength(); + if (UTF8_BINARY_CMP.compare(bytes, offset + 1, len - 1, POSITIVE_INF, 0, 5) == 0) { + setDouble(result, Double.POSITIVE_INFINITY); + } else if (UTF8_BINARY_CMP.compare(bytes, offset + 1, len - 1, NEGATIVE_INF, 0, 6) == 0) { + setDouble(result, Double.NEGATIVE_INFINITY); + } else if (UTF8_BINARY_CMP.compare(bytes, offset + 1, len - 1, NAN, 0, 5) == 0) { + setDouble(result, Double.NaN); + } else { + utf8Ptr.set(bytes, offset + 1, len - 1); + try { + setDouble(result, Double.parseDouble(utf8Ptr.toString())); + } catch (NumberFormatException e) { + handleUparseableString(result, e); + } + } + } else { + throw new TypeMismatchException(getIdentifier(), 0, tt, ATypeTag.SERIALIZED_STRING_TYPE_TAG); + } + } + + protected void handleUparseableString(IPointable result, NumberFormatException e) throws HyracksDataException { + throw new InvalidDataFormatException(getIdentifier(), e, ATypeTag.SERIALIZED_DOUBLE_TYPE_TAG); + } + + protected void setDouble(IPointable result, double value) throws HyracksDataException { + aDouble.setValue(value); + DOUBLE_SERDE.serialize(aDouble, out); + result.set(resultStorage); + } + + protected abstract FunctionIdentifier getIdentifier(); +} http://git-wip-us.apache.org/repos/asf/asterixdb/blob/c9a398cc/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AbstractInt64ConstructorEvaluator.java ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AbstractInt64ConstructorEvaluator.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AbstractInt64ConstructorEvaluator.java new file mode 100644 index 0000000..8cf9bed --- /dev/null +++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AbstractInt64ConstructorEvaluator.java @@ -0,0 +1,135 @@ +/* + * 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.asterix.runtime.evaluators.constructors; + +import java.io.DataOutput; +import java.io.IOException; + +import org.apache.asterix.formats.nontagged.SerializerDeserializerProvider; +import org.apache.asterix.om.base.AInt64; +import org.apache.asterix.om.base.AMutableInt64; +import org.apache.asterix.om.types.ATypeTag; +import org.apache.asterix.om.types.BuiltinType; +import org.apache.asterix.runtime.exceptions.InvalidDataFormatException; +import org.apache.asterix.runtime.exceptions.TypeMismatchException; +import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier; +import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator; +import org.apache.hyracks.api.dataflow.value.ISerializerDeserializer; +import org.apache.hyracks.api.exceptions.HyracksDataException; +import org.apache.hyracks.data.std.api.IPointable; +import org.apache.hyracks.data.std.primitive.UTF8StringPointable; +import org.apache.hyracks.data.std.primitive.VoidPointable; +import org.apache.hyracks.data.std.util.ArrayBackedValueStorage; +import org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference; + +public abstract class AbstractInt64ConstructorEvaluator implements IScalarEvaluator { + @SuppressWarnings("unchecked") + protected static final ISerializerDeserializer<AInt64> INT64_SERDE = + SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64); + + protected final IScalarEvaluator inputEval; + protected final IPointable inputArg; + protected final ArrayBackedValueStorage resultStorage; + protected final DataOutput out; + protected final AMutableInt64 aInt64; + protected final UTF8StringPointable utf8Ptr; + + protected AbstractInt64ConstructorEvaluator(IScalarEvaluator inputEval) { + this.inputEval = inputEval; + inputArg = new VoidPointable(); + resultStorage = new ArrayBackedValueStorage(); + out = resultStorage.getDataOutput(); + aInt64 = new AMutableInt64(0); + utf8Ptr = new UTF8StringPointable(); + } + + @Override + public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException { + try { + inputEval.evaluate(tuple, inputArg); + resultStorage.reset(); + evaluateImpl(result); + } catch (IOException e) { + throw new InvalidDataFormatException(getIdentifier(), e, ATypeTag.SERIALIZED_INT64_TYPE_TAG); + } + } + + protected void evaluateImpl(IPointable result) throws IOException { + byte[] bytes = inputArg.getByteArray(); + int startOffset = inputArg.getStartOffset(); + + byte tt = bytes[startOffset]; + if (tt == ATypeTag.SERIALIZED_INT64_TYPE_TAG) { + result.set(inputArg); + } else if (tt == ATypeTag.SERIALIZED_STRING_TYPE_TAG) { + int len = inputArg.getLength(); + utf8Ptr.set(bytes, startOffset + 1, len - 1); + int offset = utf8Ptr.getCharStartOffset(); + //accumulating value in negative domain + //otherwise Long.MIN_VALUE = -(Long.MAX_VALUE + 1) would have caused overflow + long value = 0; + boolean positive = true; + long limit = -Long.MAX_VALUE; + if (bytes[offset] == '+') { + offset++; + } else if (bytes[offset] == '-') { + offset++; + positive = false; + limit = Long.MIN_VALUE; + } + int end = startOffset + len; + for (; offset < end; offset++) { + int digit; + if (bytes[offset] >= '0' && bytes[offset] <= '9') { + value *= 10; + digit = bytes[offset] - '0'; + } else if (bytes[offset] == 'i' && bytes[offset + 1] == '6' && bytes[offset + 2] == '4' + && offset + 3 == end) { + break; + } else { + handleUnparseableString(result); + return; + } + if (value < limit + digit) { + handleUnparseableString(result); + } + value -= digit; + } + if (value > 0) { + handleUnparseableString(result); + } + if (value < 0 && positive) { + value *= -1; + } + + aInt64.setValue(value); + INT64_SERDE.serialize(aInt64, out); + result.set(resultStorage); + } else { + throw new TypeMismatchException(getIdentifier(), 0, tt, ATypeTag.SERIALIZED_STRING_TYPE_TAG); + } + } + + protected void handleUnparseableString(IPointable result) throws HyracksDataException { + throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INT64_TYPE_TAG); + } + + protected abstract FunctionIdentifier getIdentifier(); +} http://git-wip-us.apache.org/repos/asf/asterixdb/blob/c9a398cc/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AbstractStringConstructorEvaluator.java ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AbstractStringConstructorEvaluator.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AbstractStringConstructorEvaluator.java new file mode 100644 index 0000000..6918bb9 --- /dev/null +++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/constructors/AbstractStringConstructorEvaluator.java @@ -0,0 +1,152 @@ +/* + * 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.asterix.runtime.evaluators.constructors; + +import java.io.DataOutput; +import java.io.IOException; + +import org.apache.asterix.dataflow.data.nontagged.serde.ABooleanSerializerDeserializer; +import org.apache.asterix.dataflow.data.nontagged.serde.ADoubleSerializerDeserializer; +import org.apache.asterix.dataflow.data.nontagged.serde.AFloatSerializerDeserializer; +import org.apache.asterix.dataflow.data.nontagged.serde.AInt16SerializerDeserializer; +import org.apache.asterix.dataflow.data.nontagged.serde.AInt32SerializerDeserializer; +import org.apache.asterix.dataflow.data.nontagged.serde.AInt64SerializerDeserializer; +import org.apache.asterix.dataflow.data.nontagged.serde.AInt8SerializerDeserializer; +import org.apache.asterix.om.types.ATypeTag; +import org.apache.asterix.runtime.exceptions.InvalidDataFormatException; +import org.apache.asterix.runtime.exceptions.UnsupportedTypeException; +import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier; +import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator; +import org.apache.hyracks.api.exceptions.HyracksDataException; +import org.apache.hyracks.data.std.api.IPointable; +import org.apache.hyracks.data.std.primitive.VoidPointable; +import org.apache.hyracks.data.std.util.ArrayBackedValueStorage; +import org.apache.hyracks.data.std.util.GrowableArray; +import org.apache.hyracks.data.std.util.UTF8StringBuilder; +import org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference; + +public abstract class AbstractStringConstructorEvaluator implements IScalarEvaluator { + + protected final IScalarEvaluator inputEval; + protected final IPointable inputArg; + protected final ArrayBackedValueStorage resultStorage; + protected final DataOutput out; + protected final UTF8StringBuilder builder; + protected final GrowableArray baaos; + + protected AbstractStringConstructorEvaluator(IScalarEvaluator inputEval) { + resultStorage = new ArrayBackedValueStorage(); + out = resultStorage.getDataOutput(); + inputArg = new VoidPointable(); + this.inputEval = inputEval; + builder = new UTF8StringBuilder(); + baaos = new GrowableArray(); + } + + @Override + public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException { + try { + inputEval.evaluate(tuple, inputArg); + resultStorage.reset(); + evaluateImpl(result); + } catch (IOException e) { + throw new InvalidDataFormatException(getIdentifier(), e, ATypeTag.SERIALIZED_STRING_TYPE_TAG); + } + } + + protected void evaluateImpl(IPointable result) throws IOException { + byte[] serString = inputArg.getByteArray(); + int offset = inputArg.getStartOffset(); + + ATypeTag tt = ATypeTag.VALUE_TYPE_MAPPING[serString[offset]]; + if (tt == ATypeTag.STRING) { + result.set(inputArg); + } else { + int len = inputArg.getLength(); + baaos.reset(); + builder.reset(baaos, len); + int startOffset = offset + 1; + switch (tt) { + case TINYINT: { + int i = AInt8SerializerDeserializer.getByte(serString, startOffset); + builder.appendString(String.valueOf(i)); + break; + } + case SMALLINT: { + int i = AInt16SerializerDeserializer.getShort(serString, startOffset); + builder.appendString(String.valueOf(i)); + break; + } + case INTEGER: { + int i = AInt32SerializerDeserializer.getInt(serString, startOffset); + builder.appendString(String.valueOf(i)); + break; + } + case BIGINT: { + long l = AInt64SerializerDeserializer.getLong(serString, startOffset); + builder.appendString(String.valueOf(l)); + break; + } + case DOUBLE: { + double d = ADoubleSerializerDeserializer.getDouble(serString, startOffset); + builder.appendString(String.valueOf(d)); + break; + } + case FLOAT: { + float f = AFloatSerializerDeserializer.getFloat(serString, startOffset); + builder.appendString(String.valueOf(f)); + break; + } + case BOOLEAN: { + boolean b = ABooleanSerializerDeserializer.getBoolean(serString, startOffset); + builder.appendString(String.valueOf(b)); + break; + } + + // NotYetImplemented + case CIRCLE: + case DATE: + case DATETIME: + case LINE: + case TIME: + case DURATION: + case YEARMONTHDURATION: + case DAYTIMEDURATION: + case INTERVAL: + case ARRAY: + case POINT: + case POINT3D: + case RECTANGLE: + case POLYGON: + case OBJECT: + case MULTISET: + case UUID: + default: + throw new UnsupportedTypeException(getIdentifier(), serString[offset]); + } + builder.finish(); + out.write(ATypeTag.SERIALIZED_STRING_TYPE_TAG); + out.write(baaos.getByteArray(), 0, baaos.getLength()); + result.set(resultStorage); + } + } + + protected abstract FunctionIdentifier getIdentifier(); +} http://git-wip-us.apache.org/repos/asf/asterixdb/blob/c9a398cc/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ToBigIntDescriptor.java ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ToBigIntDescriptor.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ToBigIntDescriptor.java new file mode 100644 index 0000000..5cf9af7 --- /dev/null +++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ToBigIntDescriptor.java @@ -0,0 +1,129 @@ +/* + * 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.asterix.runtime.evaluators.functions; + +import java.io.IOException; + +import org.apache.asterix.dataflow.data.nontagged.serde.ABooleanSerializerDeserializer; +import org.apache.asterix.formats.nontagged.SerializerDeserializerProvider; +import org.apache.asterix.om.base.ANull; +import org.apache.asterix.om.functions.BuiltinFunctions; +import org.apache.asterix.om.functions.IFunctionDescriptor; +import org.apache.asterix.om.functions.IFunctionDescriptorFactory; +import org.apache.asterix.om.types.ATypeTag; +import org.apache.asterix.om.types.BuiltinType; +import org.apache.asterix.om.types.hierachy.ATypeHierarchy; +import org.apache.asterix.om.types.hierachy.ITypeConvertComputer; +import org.apache.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor; +import org.apache.asterix.runtime.evaluators.constructors.AbstractInt64ConstructorEvaluator; +import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier; +import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator; +import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory; +import org.apache.hyracks.api.context.IHyracksTaskContext; +import org.apache.hyracks.api.dataflow.value.ISerializerDeserializer; +import org.apache.hyracks.api.exceptions.HyracksDataException; +import org.apache.hyracks.data.std.api.IPointable; + +public class ToBigIntDescriptor extends AbstractScalarFunctionDynamicDescriptor { + private static final long serialVersionUID = 1L; + public static final IFunctionDescriptorFactory FACTORY = new IFunctionDescriptorFactory() { + @Override + public IFunctionDescriptor createFunctionDescriptor() { + return new ToBigIntDescriptor(); + } + }; + + @Override + public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) { + return new IScalarEvaluatorFactory() { + private static final long serialVersionUID = 1L; + + @Override + public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException { + return new AbstractInt64ConstructorEvaluator(args[0].createScalarEvaluator(ctx)) { + @SuppressWarnings("unchecked") + private final ISerializerDeserializer<ANull> nullSerde = + SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ANULL); + + @Override + protected void evaluateImpl(IPointable result) throws IOException { + byte[] bytes = inputArg.getByteArray(); + int offset = inputArg.getStartOffset(); + ATypeTag tt = ATypeTag.VALUE_TYPE_MAPPING[bytes[offset]]; + switch (tt) { + case BOOLEAN: + boolean b = ABooleanSerializerDeserializer.getBoolean(bytes, offset + 1); + aInt64.setValue(b ? 1 : 0); + INT64_SERDE.serialize(aInt64, out); + result.set(resultStorage); + break; + + case TINYINT: + case SMALLINT: + case INTEGER: + ITypeConvertComputer tpcc = ATypeHierarchy.getTypePromoteComputer(tt, ATypeTag.BIGINT); + tpcc.convertType(bytes, offset + 1, inputArg.getLength() - 1, out); + result.set(resultStorage); + break; + + case FLOAT: + case DOUBLE: + ITypeConvertComputer tdcc = + ATypeHierarchy.getTypeDemoteComputer(tt, ATypeTag.BIGINT, false); + tdcc.convertType(bytes, offset + 1, inputArg.getLength() - 1, out); + result.set(resultStorage); + break; + + case ARRAY: + case MULTISET: + case OBJECT: + setNull(result); + break; + + default: + super.evaluateImpl(result); + break; + } + } + + @Override + protected void handleUnparseableString(IPointable result) throws HyracksDataException { + setNull(result); + } + + private void setNull(IPointable result) throws HyracksDataException { + nullSerde.serialize(ANull.NULL, out); + result.set(resultStorage); + } + + @Override + protected FunctionIdentifier getIdentifier() { + return ToBigIntDescriptor.this.getIdentifier(); + } + }; + } + }; + } + + @Override + public FunctionIdentifier getIdentifier() { + return BuiltinFunctions.TO_BIGINT; + } +} http://git-wip-us.apache.org/repos/asf/asterixdb/blob/c9a398cc/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ToBooleanDescriptor.java ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ToBooleanDescriptor.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ToBooleanDescriptor.java new file mode 100644 index 0000000..ae6aca5 --- /dev/null +++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ToBooleanDescriptor.java @@ -0,0 +1,138 @@ +/* + * 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.asterix.runtime.evaluators.functions; + +import org.apache.asterix.dataflow.data.nontagged.serde.ADoubleSerializerDeserializer; +import org.apache.asterix.dataflow.data.nontagged.serde.AFloatSerializerDeserializer; +import org.apache.asterix.dataflow.data.nontagged.serde.AInt16SerializerDeserializer; +import org.apache.asterix.dataflow.data.nontagged.serde.AInt32SerializerDeserializer; +import org.apache.asterix.dataflow.data.nontagged.serde.AInt64SerializerDeserializer; +import org.apache.asterix.dataflow.data.nontagged.serde.AInt8SerializerDeserializer; +import org.apache.asterix.dataflow.data.nontagged.serde.AOrderedListSerializerDeserializer; +import org.apache.asterix.dataflow.data.nontagged.serde.ARecordSerializerDeserializer; +import org.apache.asterix.dataflow.data.nontagged.serde.AUnorderedListSerializerDeserializer; +import org.apache.asterix.om.functions.BuiltinFunctions; +import org.apache.asterix.om.functions.IFunctionDescriptor; +import org.apache.asterix.om.functions.IFunctionDescriptorFactory; +import org.apache.asterix.om.types.ATypeTag; +import org.apache.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor; +import org.apache.asterix.runtime.evaluators.constructors.AbstractBooleanConstructorEvaluator; +import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier; +import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator; +import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory; +import org.apache.hyracks.api.context.IHyracksTaskContext; +import org.apache.hyracks.api.exceptions.HyracksDataException; +import org.apache.hyracks.data.std.api.IPointable; +import org.apache.hyracks.util.string.UTF8StringUtil; + +public class ToBooleanDescriptor extends AbstractScalarFunctionDynamicDescriptor { + private static final long serialVersionUID = 1L; + + private static final long BITS_NAN = Double.doubleToLongBits(Double.NaN); + + private static final long BITS_ZERO_POS = Double.doubleToLongBits(+0.0d); + + private static final long BITS_ZERO_NEG = Double.doubleToLongBits(-0.0d); + + public static final IFunctionDescriptorFactory FACTORY = new IFunctionDescriptorFactory() { + @Override + public IFunctionDescriptor createFunctionDescriptor() { + return new ToBooleanDescriptor(); + } + }; + + @Override + public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) { + return new IScalarEvaluatorFactory() { + private static final long serialVersionUID = 1L; + + @Override + public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException { + return new AbstractBooleanConstructorEvaluator(args[0].createScalarEvaluator(ctx)) { + @Override + protected void evaluateImpl(IPointable result) throws HyracksDataException { + byte[] bytes = inputArg.getByteArray(); + int offset = inputArg.getStartOffset(); + + ATypeTag tt = ATypeTag.VALUE_TYPE_MAPPING[bytes[offset]]; + switch (tt) { + case TINYINT: + setInteger(AInt8SerializerDeserializer.getByte(bytes, offset + 1), result); + break; + case SMALLINT: + setInteger(AInt16SerializerDeserializer.getShort(bytes, offset + 1), result); + break; + case INTEGER: + setInteger(AInt32SerializerDeserializer.getInt(bytes, offset + 1), result); + break; + case BIGINT: + setInteger(AInt64SerializerDeserializer.getLong(bytes, offset + 1), result); + break; + case FLOAT: + setDouble(AFloatSerializerDeserializer.getFloat(bytes, offset + 1), result); + break; + case DOUBLE: + setDouble(ADoubleSerializerDeserializer.getDouble(bytes, offset + 1), result); + break; + case STRING: + setInteger(UTF8StringUtil.getStringLength(bytes, offset + 1), result); + break; + case ARRAY: + setInteger(AOrderedListSerializerDeserializer.getNumberOfItems(bytes, offset), + result); + break; + case MULTISET: + setInteger(AUnorderedListSerializerDeserializer.getNumberOfItems(bytes, offset), + result); + break; + case OBJECT: + setBoolean(result, !ARecordSerializerDeserializer.hasNoFields(bytes, offset, + inputArg.getLength())); + break; + default: + super.evaluateImpl(result); + break; + } + } + + private void setInteger(long v, IPointable result) throws HyracksDataException { + setBoolean(result, v != 0); + } + + private void setDouble(double v, IPointable result) throws HyracksDataException { + long bits = Double.doubleToLongBits(v); + boolean zeroOrNaN = bits == BITS_ZERO_POS || bits == BITS_ZERO_NEG || bits == BITS_NAN; + setBoolean(result, !zeroOrNaN); + } + + @Override + protected FunctionIdentifier getIdentifier() { + return ToBooleanDescriptor.this.getIdentifier(); + } + }; + } + }; + } + + @Override + public FunctionIdentifier getIdentifier() { + return BuiltinFunctions.TO_BOOLEAN; + } +} http://git-wip-us.apache.org/repos/asf/asterixdb/blob/c9a398cc/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ToDoubleDescriptor.java ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ToDoubleDescriptor.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ToDoubleDescriptor.java new file mode 100644 index 0000000..a7e4d94 --- /dev/null +++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ToDoubleDescriptor.java @@ -0,0 +1,124 @@ +/* + * 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.asterix.runtime.evaluators.functions; + +import java.io.IOException; + +import org.apache.asterix.dataflow.data.nontagged.serde.ABooleanSerializerDeserializer; +import org.apache.asterix.formats.nontagged.SerializerDeserializerProvider; +import org.apache.asterix.om.base.ANull; +import org.apache.asterix.om.functions.BuiltinFunctions; +import org.apache.asterix.om.functions.IFunctionDescriptor; +import org.apache.asterix.om.functions.IFunctionDescriptorFactory; +import org.apache.asterix.om.types.ATypeTag; +import org.apache.asterix.om.types.BuiltinType; +import org.apache.asterix.om.types.hierachy.ATypeHierarchy; +import org.apache.asterix.om.types.hierachy.ITypeConvertComputer; +import org.apache.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor; +import org.apache.asterix.runtime.evaluators.constructors.AbstractDoubleConstructorEvaluator; +import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier; +import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator; +import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory; +import org.apache.hyracks.api.context.IHyracksTaskContext; +import org.apache.hyracks.api.dataflow.value.ISerializerDeserializer; +import org.apache.hyracks.api.exceptions.HyracksDataException; +import org.apache.hyracks.data.std.api.IPointable; + +public class ToDoubleDescriptor extends AbstractScalarFunctionDynamicDescriptor { + private static final long serialVersionUID = 1L; + public static final IFunctionDescriptorFactory FACTORY = new IFunctionDescriptorFactory() { + @Override + public IFunctionDescriptor createFunctionDescriptor() { + return new ToDoubleDescriptor(); + } + }; + + @Override + public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) { + return new IScalarEvaluatorFactory() { + private static final long serialVersionUID = 1L; + + @Override + public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException { + return new AbstractDoubleConstructorEvaluator(args[0].createScalarEvaluator(ctx)) { + @SuppressWarnings("unchecked") + private final ISerializerDeserializer<ANull> nullSerde = + SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ANULL); + + @Override + protected void evaluateImpl(IPointable result) throws IOException { + byte[] bytes = inputArg.getByteArray(); + int offset = inputArg.getStartOffset(); + ATypeTag tt = ATypeTag.VALUE_TYPE_MAPPING[bytes[offset]]; + switch (tt) { + case BOOLEAN: + boolean b = ABooleanSerializerDeserializer.getBoolean(bytes, offset + 1); + aDouble.setValue(b ? 1 : 0); + DOUBLE_SERDE.serialize(aDouble, out); + result.set(resultStorage); + break; + + case TINYINT: + case SMALLINT: + case INTEGER: + case BIGINT: + case FLOAT: + ITypeConvertComputer tcc = ATypeHierarchy.getTypePromoteComputer(tt, ATypeTag.DOUBLE); + tcc.convertType(bytes, offset + 1, inputArg.getLength() - 1, out); + result.set(resultStorage); + break; + + case ARRAY: + case MULTISET: + case OBJECT: + setNull(result); + break; + + default: + super.evaluateImpl(result); + break; + } + } + + @Override + protected void handleUparseableString(IPointable result, NumberFormatException e) + throws HyracksDataException { + setNull(result); + } + + private void setNull(IPointable result) throws HyracksDataException { + nullSerde.serialize(ANull.NULL, out); + result.set(resultStorage); + } + + @Override + protected FunctionIdentifier getIdentifier() { + return ToDoubleDescriptor.this.getIdentifier(); + } + }; + } + }; + } + + @Override + public FunctionIdentifier getIdentifier() { + return BuiltinFunctions.TO_DOUBLE; + } +} http://git-wip-us.apache.org/repos/asf/asterixdb/blob/c9a398cc/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ToStringDescriptor.java ---------------------------------------------------------------------- diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ToStringDescriptor.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ToStringDescriptor.java new file mode 100644 index 0000000..9559da4 --- /dev/null +++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ToStringDescriptor.java @@ -0,0 +1,97 @@ +/* + * 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.asterix.runtime.evaluators.functions; + +import org.apache.asterix.formats.nontagged.SerializerDeserializerProvider; +import org.apache.asterix.om.base.ANull; +import org.apache.asterix.om.functions.BuiltinFunctions; +import org.apache.asterix.om.functions.IFunctionDescriptor; +import org.apache.asterix.om.functions.IFunctionDescriptorFactory; +import org.apache.asterix.om.types.ATypeTag; +import org.apache.asterix.om.types.BuiltinType; +import org.apache.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor; +import org.apache.asterix.runtime.evaluators.constructors.AbstractStringConstructorEvaluator; +import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier; +import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator; +import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory; +import org.apache.hyracks.api.context.IHyracksTaskContext; +import org.apache.hyracks.api.dataflow.value.ISerializerDeserializer; +import org.apache.hyracks.api.exceptions.HyracksDataException; +import org.apache.hyracks.data.std.api.IPointable; + +import java.io.IOException; + +public class ToStringDescriptor extends AbstractScalarFunctionDynamicDescriptor { + private static final long serialVersionUID = 1L; + public static final IFunctionDescriptorFactory FACTORY = new IFunctionDescriptorFactory() { + @Override + public IFunctionDescriptor createFunctionDescriptor() { + return new ToStringDescriptor(); + } + }; + + @Override + public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) { + return new IScalarEvaluatorFactory() { + private static final long serialVersionUID = 1L; + + @Override + public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException { + return new AbstractStringConstructorEvaluator(args[0].createScalarEvaluator(ctx)) { + @SuppressWarnings("unchecked") + private final ISerializerDeserializer<ANull> nullSerde = + SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ANULL); + + @Override + protected void evaluateImpl(IPointable result) throws IOException { + byte[] bytes = inputArg.getByteArray(); + int offset = inputArg.getStartOffset(); + ATypeTag tt = ATypeTag.VALUE_TYPE_MAPPING[bytes[offset]]; + switch (tt) { + case ARRAY: + case MULTISET: + case OBJECT: + setNull(result); + break; + default: + super.evaluateImpl(result); + break; + } + } + + private void setNull(IPointable result) throws HyracksDataException { + nullSerde.serialize(ANull.NULL, out); + result.set(resultStorage); + } + + @Override + protected FunctionIdentifier getIdentifier() { + return ToStringDescriptor.this.getIdentifier(); + } + }; + } + }; + } + + @Override + public FunctionIdentifier getIdentifier() { + return BuiltinFunctions.TO_STRING; + } +}
