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

No dots in variables #104

Merged
merged 7 commits into from
Jan 17, 2025
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package se.kuseman.payloadbuilder.api.execution;

import se.kuseman.payloadbuilder.api.QualifiedName;
import se.kuseman.payloadbuilder.api.execution.vector.IVectorFactory;
import se.kuseman.payloadbuilder.api.expression.IExpressionFactory;

Expand All @@ -20,5 +19,5 @@ public interface IExecutionContext
IExpressionFactory getExpressionFactory();

/** Return value of provided variable name. */
ValueVector getVariableValue(QualifiedName qname);
ValueVector getVariableValue(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,41 @@ public interface ValueVector
/** Get string at provided row. */
default UTF8String getString(int row)
{
Type type = type().getType();

// Implicit casts
if (type == Type.Boolean)
{
return UTF8String.from(getBoolean(row));
}
else if (type == Type.DateTime)
{
return UTF8String.from(getDateTime(row));
}
else if (type == Type.DateTimeOffset)
{
return UTF8String.from(getDateTimeOffset(row));
}
else if (type == Type.Decimal)
{
return UTF8String.from(getDecimal(row));
}
else if (type == Type.Double)
{
return UTF8String.from(getDouble(row));
}
else if (type == Type.Float)
{
return UTF8String.from(getFloat(row));
}
else if (type == Type.Int)
{
return UTF8String.from(getInt(row));
}
else if (type == Type.Long)
{
return UTF8String.from(getInt(row));
}
return UTF8String.from(getAny(row));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package se.kuseman.payloadbuilder.api.expression;

import se.kuseman.payloadbuilder.api.QualifiedName;

/** Variable expression @var */
public interface IVariableExpression extends IExpression
{
/** Return name of variable */
QualifiedName getName();
String getName();

/** Return true if this is a system variable otherwise false */
boolean isSystem();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public static IVariableExpression var(String name, Object value)
IVariableExpression mock = Mockito.mock(IVariableExpression.class);
when(mock.eval(any(IExecutionContext.class))).thenReturn(VectorTestUtils.vv(Type.Any, new Object[] { value }));
when(mock.accept(any(), any())).thenCallRealMethod();
when(mock.getName()).thenReturn(QualifiedName.of(name));
when(mock.getName()).thenReturn(name);
when(mock.getType()).thenReturn(ResolvedType.of(Type.Any));
return mock;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ private static void appendDataStreamMappings(IQuerySession session, String catal
}
catch (Exception e)
{
// Skip logging error about alias
if (e.getMessage()
.toLowerCase()
.contains("specify the corresponding concrete indices instead"))
{
return;
}
// Swallow this since all ES versions doesn't have datastreams
LOGGER.error("Error fetching data stream mappings", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ public void test_collect() throws JsonMappingException, JsonProcessingException
StringBuilder filter = new StringBuilder();
StringBuilder filterNot = new StringBuilder();

context.setVariable(QualifiedName.of("var"), ValueVector.literalString("world", 1));
context.setVariable(QualifiedName.of("int"), ValueVector.literalInt(1, 1));
context.setVariable("var", ValueVector.literalString("world", 1));
context.setVariable("int", ValueVector.literalInt(1, 1));

for (IPropertyPredicate pp : predicatesResult)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import se.kuseman.payloadbuilder.api.OutputWriter;

Expand Down Expand Up @@ -124,14 +126,18 @@ public void initResult(String[] columns)
return;
}

startRow();

for (String column : columns)
if (Arrays.stream(columns)
.anyMatch(StringUtils::isNotBlank))
{
writeValue(column);
}
startRow();

for (String column : columns)
{
writeValue(column);
}

endRow();
endRow();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public void flush()
@Override
public void close()
{
if (settings.allResultSetsAsOneArray)
if (settings.allResultSetsAsOneArray
&& !firstResultSet)
{
endArray();
}
Expand All @@ -132,7 +133,8 @@ public void close()
@Override
public void endResult()
{
if (settings.resultSetsAsArrays)
if (settings.resultSetsAsArrays
&& !firstResultSet)
{
endArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,37 +227,42 @@ private void writeInternal(IPhysicalPlan plan, OutputWriter writer)
try
{
Schema schema = plan.getSchema();
if (writer != null)
{
// Asterisk schema, then we cannot init the result with it since
// it's not the actual one that will come
if (SchemaUtils.isAsterisk(schema))
{
writer.initResult(ArrayUtils.EMPTY_STRING_ARRAY);
}
else
{
writer.initResult(schema.getColumns()
.stream()
.filter(c -> !(c instanceof CoreColumn)
|| !((CoreColumn) c).isInternal())
.map(c ->
{
String outputName = c.getName();
if (c instanceof CoreColumn)
{
outputName = ((CoreColumn) c).getOutputName();
}
return outputName;
})
.toArray(String[]::new));
}
}
statementContext.setOuterTupleVector(null);

boolean initCompleted = false;
iterator = plan.execute(context);
while (iterator.hasNext())
{
if (!initCompleted)
{
if (writer != null)
{
// Asterisk schema, then we cannot init the result with it since
// it's not the actual one that will come
if (SchemaUtils.isAsterisk(schema))
{
writer.initResult(ArrayUtils.EMPTY_STRING_ARRAY);
}
else
{
writer.initResult(schema.getColumns()
.stream()
.filter(c -> !(c instanceof CoreColumn)
|| !((CoreColumn) c).isInternal())
.map(c ->
{
String outputName = c.getName();
if (c instanceof CoreColumn)
{
outputName = ((CoreColumn) c).getOutputName();
}
return outputName;
})
.toArray(String[]::new));
}
}
initCompleted = true;
}

if (session.abortQuery())
{
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,12 @@ else if ("variables".equalsIgnoreCase(type))
{
vector = ctx ->
{
final List<Entry<QualifiedName, ValueVector>> variables = new ArrayList<>(((ExecutionContext) ctx).getVariables()
final List<Entry<String, ValueVector>> variables = new ArrayList<>(((ExecutionContext) ctx).getVariables()
.entrySet());
return new ObjectTupleVector(data.getSchema()
.get(), variables.size(), (row, col) ->
{
Entry<QualifiedName, ValueVector> e = variables.get(row);
Entry<String, ValueVector> e = variables.get(row);
if (col == 0)
{
return e.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.HashMap;
import java.util.Map;

import se.kuseman.payloadbuilder.api.QualifiedName;
import se.kuseman.payloadbuilder.api.execution.IExecutionContext;
import se.kuseman.payloadbuilder.api.execution.ValueVector;
import se.kuseman.payloadbuilder.api.execution.vector.IVectorFactory;
Expand All @@ -30,7 +29,7 @@ public class ExecutionContext implements IExecutionContext
private final ExpressionFactory expressionFactory;

/** Variables in context */
private Map<QualifiedName, ValueVector> variables;
private Map<String, ValueVector> variables;

public ExecutionContext(QuerySession session)
{
Expand Down Expand Up @@ -77,13 +76,13 @@ public BufferAllocator getBufferAllocator()
}

/** Get variables map */
public Map<QualifiedName, ValueVector> getVariables()
public Map<String, ValueVector> getVariables()
{
return defaultIfNull(variables, emptyMap());
}

/** Set variable to context */
public void setVariable(QualifiedName name, ValueVector value)
public void setVariable(String name, ValueVector value)
{
if (variables == null)
{
Expand All @@ -94,7 +93,7 @@ public void setVariable(QualifiedName name, ValueVector value)

/** Get variable from context */
@Override
public ValueVector getVariableValue(QualifiedName name)
public ValueVector getVariableValue(String name)
{
return variables != null ? variables.get(name)
: null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ public class QuerySession implements IQuerySession
{
/* System properties */
/** Enable to print logical and physical plans to session print writer */
public static final QualifiedName PRINT_PLAN = QualifiedName.of("printplan");
public static final String PRINT_PLAN = "printplan";
/** Enable to print all logical plans for each optimisation rule. */
public static final QualifiedName DEBUG_PLAN = QualifiedName.of("debugplan");
public static final String DEBUG_PLAN = "debugplan";

/** Force a nested loop where default would have been a hash match */
public static final QualifiedName FORCE_NESTED_LOOP = QualifiedName.of("force_nested_loop");
public static final String FORCE_NESTED_LOOP = "force_nested_loop";

/** Force no inner cache for non correlated nested loops */
public static final QualifiedName FORCE_NO_INNER_CACHE = QualifiedName.of("force_no_inner_cache");
public static final String FORCE_NO_INNER_CACHE = "force_no_inner_cache";
/* End system properties */

/* Compile fields */
Expand All @@ -73,11 +73,11 @@ public class QuerySession implements IQuerySession

/* Execution fields */
/** Variable values for {@link VariableExpression}'s */
private final Map<QualifiedName, ValueVector> variables;
private final Map<String, ValueVector> variables;
/** Catalog properties by catalog alias */
private Map<String, Map<String, ValueVector>> catalogProperties;
/** System properties */
private Map<QualifiedName, ValueVector> systemProperties;
private Map<String, ValueVector> systemProperties;
private Writer printWriter;
private ExceptionHandler exceptionHandler;
private BooleanSupplier abortSupplier;
Expand All @@ -99,8 +99,8 @@ public QuerySession(CatalogRegistry catalogRegistry, Map<String, Object> variabl
this.catalogRegistry = requireNonNull(catalogRegistry, "catalogRegistry");
this.variables = requireNonNull(variables, "variables").entrySet()
.stream()
.collect(toMap(k -> QualifiedName.of(k.getKey()
.toLowerCase()), v -> ValueVector.literalAny(v.getValue())));
.collect(toMap(e -> e.getKey()
.toLowerCase(), v -> ValueVector.literalAny(v.getValue())));
}

@Override
Expand Down Expand Up @@ -299,7 +299,7 @@ public CatalogRegistry getCatalogRegistry()
}

/** Return variables map */
Map<QualifiedName, ValueVector> getVariables()
Map<String, ValueVector> getVariables()
{
return variables;
}
Expand Down Expand Up @@ -414,7 +414,7 @@ public ValueVector getCatalogProperty(String alias, String key)
}

/** Set system property */
public void setSystemProperty(QualifiedName name, ValueVector value)
public void setSystemProperty(String name, ValueVector value)
{
requireNonNull(value);
if (systemProperties == null)
Expand All @@ -425,7 +425,7 @@ public void setSystemProperty(QualifiedName name, ValueVector value)
}

/** Get system property */
public ValueVector getSystemProperty(QualifiedName name)
public ValueVector getSystemProperty(String name)
{
if (systemProperties == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public void setNull(int row)
@Override
public boolean isNull(int row)
{
if (buffer == null)
{
return true;
}
return buffer[row] == null;
}

Expand Down
Loading
Loading