<commit_before>/**
 * This file is part of the "libfnord" project
 *   Copyright (c) 2015 <NAME>
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include <eventql/sql/qtree/ColumnReferenceNode.h>

using namespace stx;

namespace csql {

ColumnReferenceNode::ColumnReferenceNode(
    const ColumnReferenceNode& other) :
    column_name_(other.column_name_),
    column_index_(other.column_index_) {}

ColumnReferenceNode::ColumnReferenceNode(
    const String& column_name) :
    column_name_(column_name) {}

ColumnReferenceNode::ColumnReferenceNode(
    size_t column_index) :
    column_name_(StringUtil::toString(column_index)),
    column_index_(Some(column_index)) {}

const String& ColumnReferenceNode::fieldName() const {
  return column_name_;
}

const String& ColumnReferenceNode::columnName() const {
  return column_name_;
}

void ColumnReferenceNode::setColumnName(const String& name) {
  column_name_ = name;
}

Vector<RefPtr<ValueExpressionNode>> ColumnReferenceNode::arguments() const {
  return Vector<RefPtr<ValueExpressionNode>>{};
}

size_t ColumnReferenceNode::columnIndex() const {
  if (column_index_.isEmpty()) {
    RAISE(
        kRuntimeError,
        "internal error: columnIndex called on a unresolved ColumnReference. "
        "did you try to execute an expression without resolving columns?");
  }

  return column_index_.get();
}

bool ColumnReferenceNode::hasColumnIndex() const {
  return !column_index_.isEmpty();
}

void ColumnReferenceNode::setColumnIndex(size_t index) {
  if (index == size_t(-1)) {
    return;
  }

  column_index_ = Some(index);
}

RefPtr<QueryTreeNode> ColumnReferenceNode::deepCopy() const {
  return new ColumnReferenceNode(*this);
}

String ColumnReferenceNode::toSQL() const {
  if (!column_index_.isEmpty() && column_index_.get() != size_t(-1)) {
    return StringUtil::format("subquery_column($0)", column_index_.get());
  }

  return "`" + column_name_ + "`";
}

void ColumnReferenceNode::encode(
    QueryTreeCoder* coder,
    const ColumnReferenceNode& node,
    stx::OutputStream* os) {
  if (!node.column_index_.isEmpty() && node.column_index_.get() != size_t(-1)) {
    os->appendUInt8(1);
    os->appendUInt64(node.column_index_.get());
  } else {
    os->appendUInt8(0);
    os->appendLenencString(node.column_name_);
  }
}

RefPtr<QueryTreeNode> ColumnReferenceNode::decode (
    QueryTreeCoder* coder,
    stx::InputStream* is) {
  if (is->readUInt8()) {
    auto column_index = is->readUInt64();
    return new ColumnReferenceNode(column_index);
  } else {
    auto column_name = is->readLenencString();
    return new ColumnReferenceNode(column_name);
  }
}

} // namespace csql<commit_msg>ColumnReferenceNode handle both column_name and column_index are set<commit_after>/**
 * This file is part of the "libfnord" project
 *   Copyright (c) 2015 <NAME>
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include <eventql/sql/qtree/ColumnReferenceNode.h>

using namespace stx;

namespace csql {

ColumnReferenceNode::ColumnReferenceNode(
    const ColumnReferenceNode& other) :
    column_name_(other.column_name_),
    column_index_(other.column_index_) {}

ColumnReferenceNode::ColumnReferenceNode(
    const String& column_name) :
    column_name_(column_name) {}

ColumnReferenceNode::ColumnReferenceNode(
    size_t column_index) :
    column_name_(StringUtil::toString(column_index)),
    column_index_(Some(column_index)) {}

const String& ColumnReferenceNode::fieldName() const {
  return column_name_;
}

const String& ColumnReferenceNode::columnName() const {
  return column_name_;
}

void ColumnReferenceNode::setColumnName(const String& name) {
  column_name_ = name;
}

Vector<RefPtr<ValueExpressionNode>> ColumnReferenceNode::arguments() const {
  return Vector<RefPtr<ValueExpressionNode>>{};
}

size_t ColumnReferenceNode::columnIndex() const {
  if (column_index_.isEmpty()) {
    RAISE(
        kRuntimeError,
        "internal error: columnIndex called on a unresolved ColumnReference. "
        "did you try to execute an expression without resolving columns?");
  }

  return column_index_.get();
}

bool ColumnReferenceNode::hasColumnIndex() const {
  return !column_index_.isEmpty();
}

void ColumnReferenceNode::setColumnIndex(size_t index) {
  if (index == size_t(-1)) {
    return;
  }

  column_index_ = Some(index);
}

RefPtr<QueryTreeNode> ColumnReferenceNode::deepCopy() const {
  return new ColumnReferenceNode(*this);
}

String ColumnReferenceNode::toSQL() const {
  if (!column_index_.isEmpty() && column_index_.get() != size_t(-1)) {
    return StringUtil::format("subquery_column($0)", column_index_.get());
  }

  return "`" + column_name_ + "`";
}

void ColumnReferenceNode::encode(
    QueryTreeCoder* coder,
    const ColumnReferenceNode& node,
    stx::OutputStream* os) {
  os->appendLenencString(node.column_name_);

  if (!node.column_index_.isEmpty() && node.column_index_.get() != size_t(-1)) {
    os->appendUInt8(1);
    os->appendVarUInt(node.column_index_.get());
  } else {
    os->appendUInt8(0);
  }
}

RefPtr<QueryTreeNode> ColumnReferenceNode::decode (
    QueryTreeCoder* coder,
    stx::InputStream* is) {

  auto column_name = is->readLenencString();
  auto node = new ColumnReferenceNode(column_name);

  if (is->readUInt8()) {
    node->setColumnIndex(is->readVarUInt());
  }

  return node;
}

} // namespace csql
{'fraction_non_alphanumeric': 0.12143102855177158, 'fraction_numerical': 0.005847953216374269, 'mean_word_length': 4.780318091451292, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2880466', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1758, 'n_words': 403}