Fix logical error in evaluate constant expression

This commit is contained in:
vdimir 2023-03-17 15:08:16 +00:00
parent d3a514d221
commit 2958c5f0f1
No known key found for this signature in database
GPG Key ID: 6EE4CE2BEDC51862
13 changed files with 50 additions and 0 deletions

View File

@ -44,6 +44,9 @@ static std::pair<Field, std::shared_ptr<const IDataType>> getFieldAndDataTypeFro
std::pair<Field, std::shared_ptr<const IDataType>> evaluateConstantExpression(const ASTPtr & node, const ContextPtr & context)
{
if (!node->hasColumnName())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Expression '{}' is not a constant expression", node->formatForErrorMessage());
if (ASTLiteral * literal = node->as<ASTLiteral>())
return getFieldAndDataTypeFromLiteral(literal);

View File

@ -15,6 +15,7 @@ public:
String getID(char) const override { return "Asterisk"; }
ASTPtr clone() const override;
void appendColumnName(WriteBuffer & ostr) const override;
bool hasColumnName() const override { return true; }
ASTPtr expression;
ASTPtr transformers;

View File

@ -23,6 +23,8 @@ public:
ASTPtr clone() const override;
void appendColumnName(WriteBuffer & ostr) const override;
bool hasColumnName() const override { return true; }
void setPattern(String pattern);
const String & getPattern() const;
const std::shared_ptr<re2::RE2> & getMatcher() const;
@ -46,6 +48,7 @@ public:
String getID(char) const override { return "ColumnsListMatcher"; }
ASTPtr clone() const override;
void appendColumnName(WriteBuffer & ostr) const override;
bool hasColumnName() const override { return true; }
ASTPtr expression;
ASTPtr column_list;
@ -62,6 +65,8 @@ public:
ASTPtr clone() const override;
void appendColumnName(WriteBuffer & ostr) const override;
bool hasColumnName() const override { return true; }
const std::shared_ptr<re2::RE2> & getMatcher() const;
void setPattern(String pattern, bool set_matcher = true);
void setMatcher(std::shared_ptr<re2::RE2> matcher);
@ -84,6 +89,7 @@ public:
String getID(char) const override { return "QualifiedColumnsListMatcher"; }
ASTPtr clone() const override;
void appendColumnName(WriteBuffer & ostr) const override;
bool hasColumnName() const override { return true; }
ASTPtr qualifier;
ASTPtr column_list;

View File

@ -48,6 +48,8 @@ public:
}
void transform(ASTs & nodes) const override;
void appendColumnName(WriteBuffer & ostr) const override;
bool hasColumnName() const override { return true; }
void updateTreeHashImpl(SipHash & hash_state) const override;
// Case 1 APPLY (quantile(0.9))
@ -80,6 +82,7 @@ public:
const std::shared_ptr<re2::RE2> & getMatcher() const;
bool isColumnMatching(const String & column_name) const;
void appendColumnName(WriteBuffer & ostr) const override;
bool hasColumnName() const override { return true; }
void updateTreeHashImpl(SipHash & hash_state) const override;
protected:
@ -103,6 +106,8 @@ public:
}
void appendColumnName(WriteBuffer & ostr) const override;
bool hasColumnName() const override { return true; }
void updateTreeHashImpl(SipHash & hash_state) const override;
String name;
@ -121,6 +126,8 @@ public:
}
void transform(ASTs & nodes) const override;
void appendColumnName(WriteBuffer & ostr) const override;
bool hasColumnName() const override { return true; }
void updateTreeHashImpl(SipHash & hash_state) const override;
protected:

View File

@ -468,6 +468,27 @@ namespace
};
}
bool ASTFunction::hasColumnName() const
{
if (parameters)
{
for (const auto & child : parameters->children)
{
if (!child->hasColumnName())
return false;
}
}
if (arguments)
{
for (const auto & child : arguments->children)
{
if (!child->hasColumnName())
return false;
}
}
return true;
}
void ASTFunction::appendColumnNameImpl(WriteBuffer & ostr) const
{

View File

@ -79,6 +79,8 @@ public:
protected:
void formatImplWithoutAlias(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override;
void appendColumnNameImpl(WriteBuffer & ostr) const override;
bool hasColumnName() const override;
private:
void finishFormatWithWindow(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const;
};

View File

@ -58,6 +58,7 @@ protected:
void formatImplWithoutAlias(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override;
void appendColumnNameImpl(WriteBuffer & ostr) const override;
bool hasColumnName() const override { return true; }
private:
using ASTWithAlias::children; /// ASTIdentifier is child free

View File

@ -47,6 +47,7 @@ protected:
void formatImplWithoutAlias(const FormatSettings & settings, FormatState &, FormatStateStacked) const override;
void appendColumnNameImpl(WriteBuffer & ostr) const override;
bool hasColumnName() const override { return true; }
private:
/// Legacy version of 'appendColumnNameImpl'. It differs only with tuple literals.

View File

@ -31,6 +31,7 @@ public:
return clone;
}
void appendColumnName(WriteBuffer & ostr) const override;
bool hasColumnName() const override { return true; }
ASTPtr qualifier;
ASTPtr transformers;

View File

@ -21,6 +21,8 @@ public:
using IAST::IAST;
void appendColumnName(WriteBuffer & ostr) const final;
bool hasColumnName() const override { return true; }
void appendColumnNameWithoutAlias(WriteBuffer & ostr) const final;
String getAliasOrColumnName() const override { return alias.empty() ? getColumnName() : alias; }
String tryGetAlias() const override { return alias; }

View File

@ -56,6 +56,9 @@ public:
throw Exception(ErrorCodes::LOGICAL_ERROR, "Trying to get name of not a column: {}", getID());
}
/* This method should be overridden with appendColumnName */
virtual bool hasColumnName() const { return false; }
/** Get the alias, if any, or the canonical name of the column, if it is not. */
virtual String getAliasOrColumnName() const { return getColumnName(); }

View File

@ -0,0 +1,2 @@
SELECT count() FROM mysql(mysql('127.0.0.1:9004', currentDatabase(), 'foo', 'default', '', SETTINGS connection_pool_size = 1), '127.0.0.1:9004', currentDatabase(), 'foo', '', ''); -- { serverError BAD_ARGUMENTS }