Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CASSANDRA-20102: Support octet_length and length functions #3707

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions src/java/org/apache/cassandra/cql3/functions/LengthFcts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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.cassandra.cql3.functions;

import java.nio.ByteBuffer;
import java.util.HashSet;
import java.util.Set;

import org.apache.cassandra.cql3.CQL3Type;
import org.apache.cassandra.db.marshal.AbstractType;
import org.apache.cassandra.db.marshal.Int32Type;
import org.apache.cassandra.db.marshal.UTF8Type;
import org.apache.cassandra.exceptions.InvalidRequestException;
import org.apache.cassandra.transport.ProtocolVersion;
import org.apache.cassandra.utils.ByteBufferUtil;

/**
* Lengths of stored data without returning the data.
*/
public class LengthFcts
{
public static void addFunctionsTo(NativeFunctions functions)
{
// As all types ultimately end up as bytebuffers they should all be compatible with
// octetlength
Set<AbstractType<?>> types = new HashSet<>();
for (CQL3Type type : CQL3Type.Native.values())
{
AbstractType<?> udfType = type.getType().udfType();
if (!types.add(udfType))
continue;
functions.add(makeOctetLengthFunction(type.getType().udfType()));
}

// Special handling for string length which is number of UTF-8 codepoints
functions.add(length);
}


public static NativeFunction makeOctetLengthFunction(AbstractType<?> fromType)
{
// Matches SQL99 OCTET_LENGTH functions defined on bytestring and char strings
return new NativeScalarFunction("octet_length", Int32Type.instance, fromType)
{
// Do not deserialize
@Override
public Arguments newArguments(ProtocolVersion version)
{
return FunctionArguments.newNoopInstance(version, 1);
}

@Override
public ByteBuffer execute(Arguments arguments) throws InvalidRequestException
{
if (arguments.size() != 1)
throw new InvalidRequestException(String.format("octet_length() only accepts one argument (got %d)", argTypes.size()));

if (arguments.get(0) == null)
return null;

final ByteBuffer buffer = arguments.get(0);
return ByteBufferUtil.bytes(buffer.remaining());
}
};
}

// Matches PostgreSQL length function defined as returning the number of UTF-8 codepoints in the text string
public static final NativeFunction length = new NativeScalarFunction("length", Int32Type.instance, UTF8Type.instance)
{
@Override
public ByteBuffer execute(Arguments arguments) throws InvalidRequestException
{
if (arguments.size() != 1)
throw new InvalidRequestException(String.format("length() only accepts one argument (got %d)", argTypes.size()));

if (arguments.get(0) == null)
return null;

final String value = arguments.get(0);
return ByteBufferUtil.bytes(value.length());
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class NativeFunctions
AggregateFcts.addFunctionsTo(this);
CollectionFcts.addFunctionsTo(this);
BytesConversionFcts.addFunctionsTo(this);
LengthFcts.addFunctionsTo(this);
MathFcts.addFunctionsTo(this);
MaskingFcts.addFunctionsTo(this);
VectorFcts.addFunctionsTo(this);
Expand Down
107 changes: 107 additions & 0 deletions test/unit/org/apache/cassandra/cql3/functions/LengthFctsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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.cassandra.cql3.functions;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;

import org.apache.cassandra.cql3.CQLTester;
import org.apache.cassandra.utils.Pair;

public class LengthFctsTest extends CQLTester
{
@Test
public void testOctetLengthHNonStrings() throws Throwable
{
createTable("CREATE TABLE %s (a tinyint primary key,"
+ " b smallint,"
+ " c int,"
+ " d bigint,"
+ " e float,"
+ " f double,"
+ " g decimal,"
+ " h varint,"
+ " i int)");

execute("INSERT INTO %s (a, b, c, d, e, f, g, h) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
(byte) 1, (short) 2, 3, 4L, 5.2F, 6.3, BigDecimal.valueOf(6.3), BigInteger.valueOf(4));

assertRows(execute("SELECT OCTET_LENGTH(a), " +
"OCTET_LENGTH(b), " +
"OCTET_LENGTH(c), " +
"OCTET_LENGTH(d), " +
"OCTET_LENGTH(e), " +
"OCTET_LENGTH(f), " +
"OCTET_LENGTH(g), " +
"OCTET_LENGTH(h), " +
"OCTET_LENGTH(i) FROM %s"),
row(1, 2, 4, 8, 4, 8, 5, 1, null));
}

@Test
public void testStringLengthUTF8() throws Throwable
{
createTable("CREATE TABLE %s (key text primary key, value blob)");
// UTF-8 7 codepoint, 21 byte encoded string
String key = "こんにちは世界";
execute("INSERT INTO %s (key) VALUES (?)", key);

assertRows(execute("SELECT LENGTH(key), OCTET_LENGTH(key), OCTET_LENGTH(value) FROM %s where key = ?", key),
row(7, 21, null));
}

@Test
public void testOctetLengthStringFuzz() throws Throwable
{
createTable("CREATE TABLE %s (key text primary key, value blob)");

// TODO: quicktheories this once I figure that out
Random rand = new Random(0xCAFE);
Map<String, Pair<Integer, Integer>> expected = new HashMap<>();

for (int i = 0; i < 1000; i++) {
int sLen = 32 + rand.nextInt(100);
int bLen = rand.nextInt(4096);
String randString = RandomStringUtils.random(sLen, 0, 0, true, true, null, rand);
byte[] randBytes = new byte[bLen];
rand.nextBytes(randBytes);
expected.put(randString, (Pair.create(sLen, bLen)));
ByteBuffer bb = ByteBuffer.wrap(randBytes);
execute("INSERT INTO %s (key, value) VALUES (?, ?)", randString, bb);
}

for (Map.Entry<String, Pair<Integer, Integer>> entry : expected.entrySet())
{
int sLen = entry.getValue().left();
int bLen = entry.getValue().right();
assertRows(execute("SELECT LENGTH(key), OCTET_LENGTH(key), OCTET_LENGTH(value) FROM %s where key = ?", entry.getKey()),
row(entry.getKey().length(), sLen, bLen));
}
}



}