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

perf(sql): avoid redundant parsing of UUID bind variables #4281

Merged
merged 1 commit into from Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions core/src/main/java/io/questdb/cairo/RecordChain.java
Expand Up @@ -210,6 +210,11 @@ public void putFloat(float value) {
mem.putFloat(value);
}

@Override
public void putIPv4(int value) {
putInt(value);
}

@Override
public void putInt(int value) {
mem.putInt(value);
Expand All @@ -232,12 +237,12 @@ public void putLong256(Long256 value) {

@Override
public void putLong256(long l0, long l1, long l2, long l3) {
mem.putLong256(l0, l1, l2, l3);
mem.putLong256(l0, l1, l2, l3);
}

@Override
public void putRecord(Record value) {
// noop
// no-op
}

@Override
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/java/io/questdb/cairo/RecordSinkFactory.java
Expand Up @@ -124,8 +124,9 @@ public static RecordSink getInstance(
final int fGetBin = asm.poolInterfaceMethod(Function.class, "getBin", "(Lio/questdb/cairo/sql/Record;)Lio/questdb/std/BinarySequence;");
final int fGetRecord = asm.poolInterfaceMethod(Function.class, "getRecord", "(Lio/questdb/cairo/sql/Record;)Lio/questdb/cairo/sql/Record;");

final int wPutInt = asm.poolInterfaceMethod(RecordSinkSPI.class, "putInt", "(I)V");
final int wSkip = asm.poolInterfaceMethod(RecordSinkSPI.class, "skip", "(I)V");
final int wPutInt = asm.poolInterfaceMethod(RecordSinkSPI.class, "putInt", "(I)V");
final int wPutIPv4 = asm.poolInterfaceMethod(RecordSinkSPI.class, "putIPv4", "(I)V");
final int wPutLong = asm.poolInterfaceMethod(RecordSinkSPI.class, "putLong", "(J)V");
final int wPutLong256 = asm.poolInterfaceMethod(RecordSinkSPI.class, "putLong256", "(Lio/questdb/std/Long256;)V");
final int wPutLong128 = asm.poolInterfaceMethod(RecordSinkSPI.class, "putLong128", "(JJ)V");
Expand Down Expand Up @@ -208,7 +209,7 @@ public static RecordSink getInstance(
asm.aload(1);
asm.iconst(getSkewedIndex(index, skewIndex));
asm.invokeInterface(rGetIPv4, 1);
asm.invokeInterface(wPutInt, 1);
asm.invokeInterface(wPutIPv4, 1);
break;
case ColumnType.SYMBOL:
asm.aload(2);
Expand Down Expand Up @@ -392,7 +393,7 @@ public static RecordSink getInstance(
asm.getfield(firstFieldIndex + (i * FIELD_POOL_OFFSET));
asm.aload(1);
asm.invokeInterface(fGetIPv4, 1);
asm.invokeInterface(wPutInt, 1);
asm.invokeInterface(wPutIPv4, 1);
break;
case ColumnType.SYMBOL:
asm.aload(2);
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/io/questdb/cairo/RecordSinkSPI.java
Expand Up @@ -29,6 +29,7 @@
import io.questdb.std.Long256;

public interface RecordSinkSPI {

void putBin(BinarySequence value);

void putBool(boolean value);
Expand All @@ -43,6 +44,8 @@ public interface RecordSinkSPI {

void putFloat(float value);

void putIPv4(int value);

void putInt(int value);

void putLong(long value);
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/java/io/questdb/cairo/TableWriter.java
Expand Up @@ -8017,6 +8017,8 @@ public interface Row {

void putGeoStr(int columnIndex, CharSequence value);

void putIPv4(int columnIndex, int value);

void putInt(int columnIndex, int value);

void putLong(int columnIndex, long value);
Expand Down Expand Up @@ -8137,6 +8139,11 @@ public void putGeoStr(int columnIndex, CharSequence value) {
// no-op
}

@Override
public void putIPv4(int columnIndex, int value) {
// no-op
}

@Override
public void putInt(int columnIndex, int value) {
// no-op
Expand Down Expand Up @@ -8314,6 +8321,11 @@ public void putGeoStr(int columnIndex, CharSequence hash) {
WriterRowUtils.putGeoStr(columnIndex, hash, type, this);
}

@Override
public void putIPv4(int columnIndex, int value) {
putInt(columnIndex, value);
}

@Override
public void putInt(int columnIndex, int value) {
getPrimaryColumn(columnIndex).putInt(value);
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/io/questdb/cairo/map/OrderedMap.java
Expand Up @@ -652,6 +652,11 @@ public void putFloat(float value) {
appendAddress += 4L;
}

@Override
public void putIPv4(int value) {
putInt(value);
}

@Override
public void putInt(int value) {
Unsafe.getUnsafe().putInt(appendAddress, value);
Expand Down Expand Up @@ -907,6 +912,11 @@ public void putFloat(float value) {
appendAddress += 4L;
}

@Override
public void putIPv4(int value) {
putInt(value);
}

@Override
public void putInt(int value) {
checkCapacity(4L);
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/io/questdb/cairo/map/Unordered16Map.java
Expand Up @@ -576,6 +576,11 @@ public void putFloat(float value) {
appendAddress += 4L;
}

@Override
public void putIPv4(int value) {
putInt(value);
}

@Override
public void putInt(int value) {
Unsafe.getUnsafe().putInt(appendAddress, value);
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/io/questdb/cairo/map/Unordered2Map.java
Expand Up @@ -383,6 +383,11 @@ public void putFloat(float value) {
throw new UnsupportedOperationException();
}

@Override
public void putIPv4(int value) {
putInt(value);
}

@Override
public void putInt(int value) {
throw new UnsupportedOperationException();
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/io/questdb/cairo/map/Unordered4Map.java
Expand Up @@ -560,6 +560,11 @@ public void putFloat(float value) {
appendAddress += 4L;
}

@Override
public void putIPv4(int value) {
putInt(value);
}

@Override
public void putInt(int value) {
Unsafe.getUnsafe().putInt(appendAddress, value);
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/io/questdb/cairo/map/Unordered8Map.java
Expand Up @@ -561,6 +561,11 @@ public void putFloat(float value) {
appendAddress += 4L;
}

@Override
public void putIPv4(int value) {
putInt(value);
}

@Override
public void putInt(int value) {
Unsafe.getUnsafe().putInt(appendAddress, value);
Expand Down
13 changes: 9 additions & 4 deletions core/src/main/java/io/questdb/cairo/wal/WalWriter.java
Expand Up @@ -1093,6 +1093,10 @@ private long getColumnStructureVersion() {
return metadata.getMetadataVersion();
}

private long getDataAppendPageSize() {
return tableToken.isSystem() ? configuration.getSystemWalDataAppendPageSize() : configuration.getWalDataAppendPageSize();
}

private MemoryMA getPrimaryColumn(int column) {
assert column < columnCount : "Column index is out of bounds: " + column + " >= " + columnCount;
return columns.getQuick(getPrimaryColumnIndex(column));
Expand Down Expand Up @@ -1166,10 +1170,6 @@ private void mkWalDir() {
path.trimTo(walDirLength);
}

private long getDataAppendPageSize() {
return tableToken.isSystem() ? configuration.getSystemWalDataAppendPageSize() : configuration.getWalDataAppendPageSize();
}

private void openColumnFiles(CharSequence name, int columnIndex, int pathTrimToLen) {
try {
final MemoryMA mem1 = getPrimaryColumn(columnIndex);
Expand Down Expand Up @@ -1942,6 +1942,11 @@ public void putGeoStr(int columnIndex, CharSequence hash) {
WriterRowUtils.putGeoStr(columnIndex, hash, type, this);
}

@Override
public void putIPv4(int columnIndex, int value) {
putInt(columnIndex, value);
}

@Override
public void putInt(int columnIndex, int value) {
getPrimaryColumn(columnIndex).putInt(value);
Expand Down
Expand Up @@ -141,7 +141,7 @@ public static void putValue(
row.putInt(columnIndex, Numbers.parseInt(value, 0, value.length() - 1));
break;
case ColumnType.IPv4:
row.putInt(columnIndex, Numbers.parseIPv4UDP(value));
row.putIPv4(columnIndex, Numbers.parseIPv4UDP(value));
break;
case ColumnType.SHORT:
row.putShort(columnIndex, Numbers.parseShort(value, 0, value.length() - 1));
Expand Down Expand Up @@ -264,7 +264,7 @@ private static void putNullValue(TableWriter.Row row, int columnIndex, int colum
row.putInt(columnIndex, Numbers.INT_NaN);
break;
case ColumnType.IPv4:
row.putInt(columnIndex, Numbers.IPv4_NULL);
row.putIPv4(columnIndex, Numbers.IPv4_NULL);
case ColumnType.SHORT:
row.putShort(columnIndex, (short) 0);
break;
Expand Down
Expand Up @@ -63,7 +63,7 @@ public boolean probe(DirectUtf8Sequence text) {

@Override
public void write(TableWriter.Row row, int column, DirectUtf8Sequence value) throws Exception {
row.putInt(column, SqlKeywords.isNullKeyword(value) ? Numbers.IPv4_NULL : parseIPv4(value));
row.putIPv4(column, SqlKeywords.isNullKeyword(value) ? Numbers.IPv4_NULL : parseIPv4(value));
}

private int parseIPv4(DirectUtf8Sequence value) throws NumericException {
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/java/io/questdb/griffin/JsonPlanSink.java
Expand Up @@ -251,6 +251,18 @@ public PlanSink val(long hash, int geoHashBits) {
return this;
}

@Override
public PlanSink valIPv4(int ip) {
quoteValue = true;
checkType(NODE_VALUE);
if (ip == Numbers.IPv4_NULL) {
sink.put("null");
} else {
Numbers.intToIPv4Sink(sink, ip);
}
return this;
}

@Override
public PlanSink valUuid(long lo, long hi) {
quoteValue = true;
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/io/questdb/griffin/PlanSink.java
Expand Up @@ -114,6 +114,8 @@ public interface PlanSink {

PlanSink val(long hash, int geoHashBits);

PlanSink valIPv4(int ip);

PlanSink valISODate(long l);

PlanSink valUuid(long lo, long hi);
Expand Down
Expand Up @@ -57,8 +57,8 @@ public static RecordToRowCopier generateCopier(
// C char Unicode character code point in the Basic Multilingual Plane, encoded with UTF-16
// D double double-precision floating-point value
// F float single-precision floating-point value
// I int integer
// J long long integer
// I int 32-bit integer
// J long 64-bit integer
// L ClassName ; reference an instance of class ClassName
// S short signed short
// Z boolean true or false
Expand Down Expand Up @@ -89,6 +89,7 @@ public static RecordToRowCopier generateCopier(
int rGetBin = asm.poolInterfaceMethod(Record.class, "getBin", "(I)Lio/questdb/std/BinarySequence;");
//
int wPutInt = asm.poolInterfaceMethod(TableWriter.Row.class, "putInt", "(II)V");
int wPutIPv4 = asm.poolInterfaceMethod(TableWriter.Row.class, "putIPv4", "(II)V");
int wPutLong = asm.poolInterfaceMethod(TableWriter.Row.class, "putLong", "(IJ)V");
int wPutLong256 = asm.poolInterfaceMethod(TableWriter.Row.class, "putLong256", "(ILio/questdb/std/Long256;)V");
int wPutLong128 = asm.poolInterfaceMethod(TableWriter.Row.class, "putLong128", "(IJJ)V");
Expand Down Expand Up @@ -237,7 +238,7 @@ public static RecordToRowCopier generateCopier(
case ColumnType.IPv4:
assert toColumnTypeTag == ColumnType.IPv4;
asm.invokeInterface(rGetIPv4);
asm.invokeInterface(wPutInt, 2);
asm.invokeInterface(wPutIPv4, 2);
break;
case ColumnType.LONG:
asm.invokeInterface(rGetLong);
Expand Down Expand Up @@ -617,7 +618,7 @@ public static RecordToRowCopier generateCopier(
break;
case ColumnType.IPv4:
asm.invokeStatic(implicitCastStrAsIPv4);
asm.invokeInterface(wPutInt, 2);
asm.invokeInterface(wPutIPv4, 2);
break;
case ColumnType.LONG:
asm.invokeStatic(implicitCastStrAsLong);
Expand Down
19 changes: 17 additions & 2 deletions core/src/main/java/io/questdb/griffin/TextPlanSink.java
Expand Up @@ -28,6 +28,7 @@
import io.questdb.cairo.sql.RecordCursorFactory;
import io.questdb.std.IntList;
import io.questdb.std.Numbers;
import io.questdb.std.Uuid;
import io.questdb.std.str.Sinkable;

/**
Expand Down Expand Up @@ -113,7 +114,7 @@ public void of(RecordCursorFactory factory, SqlExecutionContext executionContext
this.childIndent = "&nbsp;&nbsp;&nbsp;&nbsp;";
this.attrIndent = "&nbsp;&nbsp;";
this.sink = htmlSink;
} else {//pg wire
} else { // pg wire
this.childIndent = " ";
this.attrIndent = " ";
this.sink = textSink;
Expand Down Expand Up @@ -192,8 +193,22 @@ public PlanSink val(long hash, int geoHashBits) {
return this;
}

@Override
public PlanSink valIPv4(int ip) {
if (ip == Numbers.IPv4_NULL) {
sink.put("null");
} else {
Numbers.intToIPv4Sink(sink, ip);
}
return this;
}

public PlanSink valUuid(long lo, long hi) {
Numbers.appendUuid(lo, hi, sink);
if (Uuid.isNull(lo, hi)) {
sink.put("null");
} else {
Numbers.appendUuid(lo, hi, sink);
}
return this;
}

Expand Down
Expand Up @@ -33,6 +33,7 @@
import io.questdb.std.IntList;
import io.questdb.std.ObjList;
import io.questdb.std.Transient;
import org.jetbrains.annotations.TestOnly;

import static io.questdb.griffin.FunctionFactoryDescriptor.replaceSignatureName;

Expand All @@ -45,6 +46,11 @@ public NegatingFunctionFactory(String name, FunctionFactory delegate) throws Sql
this.delegate = delegate;
}

@TestOnly
public FunctionFactory getDelegate() {
return delegate;
}

@Override
public String getSignature() {
return signature;
Expand All @@ -60,9 +66,9 @@ public Function newInstance(
) throws SqlException {
Function function = delegate.newInstance(position, args, argPositions, configuration, sqlExecutionContext);
if (function instanceof NegatableBooleanFunction) {
NegatableBooleanFunction negateableFunction = (NegatableBooleanFunction) function;
negateableFunction.setNegated();
return negateableFunction;
NegatableBooleanFunction negatableFunction = (NegatableBooleanFunction) function;
negatableFunction.setNegated();
return negatableFunction;
}
if (function instanceof BooleanConstant) {
return BooleanConstant.of(!function.getBool(null));
Expand Down